private void OnAudioChunkPlaybackStarted(MvxAudioChunk audioChunk) { MVCommon.SharedRef <MVGraphAPI.Frame> newLastReceivedFrame = null; lock (m_audioChunkFramesQueue) { KeyValuePair <MvxAudioChunk, MVCommon.SharedRef <MVGraphAPI.Frame> > queuedAudioChunkFrame; while (m_audioChunkFramesQueue.Count > 0) { // find the frame associated with the audio chunk being played right now, discard preceding frames queuedAudioChunkFrame = m_audioChunkFramesQueue.Dequeue(); m_allocatedAudioChunks.Remove(queuedAudioChunkFrame.Key); if (queuedAudioChunkFrame.Key == audioChunk) { newLastReceivedFrame = queuedAudioChunkFrame.Value; break; } else { queuedAudioChunkFrame.Value.Dispose(); } } } if (newLastReceivedFrame != null) { lastReceivedFrame = newLastReceivedFrame; onNextFrameReceived.Invoke(lastReceivedFrame); } }
private MvxAudioChunk ExtractAudioFromFrame(MVCommon.SharedRef <MVGraphAPI.Frame> frame) { if (frame == null) { return(null); } UInt32 framePCMDataSize = MVGraphAPI.FrameAudioExtractor.GetPCMDataSize(frame.sharedObj); if (framePCMDataSize == 0) { return(null); } UInt32 frameChannelsCount; UInt32 frameBitsPerSample; UInt32 frameSampleRate; MVGraphAPI.FrameAudioExtractor.GetAudioSamplingInfo(frame.sharedObj, out frameChannelsCount, out frameBitsPerSample, out frameSampleRate); if (frameBitsPerSample != 8 && frameBitsPerSample != 16 && frameBitsPerSample != 32) { Debug.LogErrorFormat("Unsupported 'bits per sample' value {0}", frameBitsPerSample); return(null); } UInt32 frameBytesPerSample = frameBitsPerSample / 8; byte[] frameAudioBytes = new byte[framePCMDataSize]; MVGraphAPI.FrameAudioExtractor.CopyPCMData(frame.sharedObj, frameAudioBytes); return(MvxAudioChunksPool.instance.AllocateAudioChunk(frameAudioBytes, frameBytesPerSample, frameChannelsCount, frameSampleRate)); }
private void ReadFrames() { while (true) { if (m_stopReadingFrames) { return; } float queuedAudioDuration; lock (m_audioPlayer) queuedAudioDuration = m_audioPlayer.GetQueuedAudioDuration(m_outputSampleRate); while (queuedAudioDuration < minimalBufferedAudioDuration) { if (m_stopReadingFrames) { return; } MVCommon.SharedRef <MVGraphAPI.Frame> newFrame = null; lock (m_mvxRunner) { if (!m_mvxRunner.ProcessNextFrame()) { break; } newFrame = new MVCommon.SharedRef <MVGraphAPI.Frame>(m_frameAccess.GetRecentProcessedFrame()); } if (newFrame.sharedObj == null) { break; } MvxAudioChunk newAudioChunk = ExtractAudioFromFrame(newFrame); if (newAudioChunk == null) { newFrame.Dispose(); break; } lock (m_audioChunkFramesQueue) { m_audioChunkFramesQueue.Enqueue(new KeyValuePair <MvxAudioChunk, MVCommon.SharedRef <MVGraphAPI.Frame> >(newAudioChunk, newFrame)); m_allocatedAudioChunks.Add(newAudioChunk); } lock (m_audioPlayer) { m_audioPlayer.EnqueueAudioChunk(newAudioChunk); queuedAudioDuration = m_audioPlayer.GetQueuedAudioDuration(m_outputSampleRate); } } Thread.Sleep(10); } }
private void OnNextFrameReceived(MVCommon.SharedRef <MVGraphAPI.Frame> frame) { lock (m_frameLock) { if (frame != null) { SnapFrame(); } } }
private void HandleStreamOpen(MVGraphAPI.SourceInfo sourceInfo) { frameToProcess = null; m_canProcessStream = CanProcessStream(sourceInfo); ResetProcessedData(); if (mvxStream.lastReceivedFrame != null) { HandleNextFrameReceived(mvxStream.lastReceivedFrame); } }
protected void ReadFrame() { if (!m_mvxRunner.ProcessFrame(frameId)) { return; } lastReceivedFrame = new MVCommon.SharedRef <MVGraphAPI.Frame>(m_frameAccess.GetRecentProcessedFrame()); if (lastReceivedFrame.sharedObj != null) { onNextFrameReceived.Invoke(lastReceivedFrame); } }
public virtual void Update() { if (!Monitor.TryEnter(m_frameLock)) { return; // do not process the frame in case another frame is being received - waiting in Update() is unacceptable } if (frameToProcess != null && frameToProcess.sharedObj != null) { ProcessNextFrame(frameToProcess.sharedObj); frameToProcess = null; } Monitor.Exit(m_frameLock); }
private void HandleNextFrameReceived(MVCommon.SharedRef <MVGraphAPI.Frame> frame) { if (!m_canProcessStream) { return; } if (frame == null) { return; } MVCommon.SharedRef <MVGraphAPI.Frame> newFrame = frame.CloneRef(); // do not process the frame in this thread, instead store it and wait for the next Update() lock (m_frameLock) frameToProcess = newFrame; }
protected void HandleNextFrame(MVGraphAPI.Frame nextFrame) { lastReceivedFrame = new MVCommon.SharedRef <MVGraphAPI.Frame>(nextFrame); onNextFrameReceived.Invoke(lastReceivedFrame); }
private void OnNestedStreamReceivedNextFrame(MVCommon.SharedRef <MVGraphAPI.Frame> nextFrame) { lastReceivedFrame = nextFrame; onNextFrameReceived.Invoke(nextFrame); }