Bug 802661: Clean up getUserMedia MediaStream handling r=roc,anant
authorRandell Jesup <rjesup@jesup.org>
Wed, 17 Oct 2012 17:40:14 -0400
changeset 110606 f7cb509ab5b063e1293fea5730ed7682e70347cf
parent 110605 b486c3782846bf28ff8b8b8e86d304317ec0537f
child 110607 5142bbd4da12bce9e87d95d189d7a56c6ab5d431
child 110611 20c4309100e148edc38bf69ce45631c225a439c8
push id23700
push userryanvm@gmail.com
push dateThu, 18 Oct 2012 02:10:26 +0000
treeherdermozilla-central@5142bbd4da12 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc, anant
bugs802661
milestone19.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 802661: Clean up getUserMedia MediaStream handling r=roc,anant
content/media/webrtc/MediaEngineWebRTCAudio.cpp
content/media/webrtc/MediaEngineWebRTCVideo.cpp
dom/media/MediaManager.h
--- a/content/media/webrtc/MediaEngineWebRTCAudio.cpp
+++ b/content/media/webrtc/MediaEngineWebRTCAudio.cpp
@@ -120,17 +120,22 @@ MediaEngineWebRTCAudioSource::Stop()
 
   if (mVoEBase->StopSend(mChannel)) {
     return NS_ERROR_FAILURE;
   }
   if (mVoEBase->StopReceive(mChannel)) {
     return NS_ERROR_FAILURE;
   }
 
-  mState = kStopped;
+  {
+    ReentrantMonitorAutoEnter enter(mMonitor);
+    mState = kStopped;
+    mSource->EndTrack(mTrackID);
+  }
+
   return NS_OK;
 }
 
 void
 MediaEngineWebRTCAudioSource::NotifyPull(MediaStreamGraph* aGraph,
                                          StreamTime aDesiredTime)
 {
   // Ignore - we push audio data
@@ -219,16 +224,18 @@ MediaEngineWebRTCAudioSource::Shutdown()
 typedef WebRtc_Word16 sample;
 
 void
 MediaEngineWebRTCAudioSource::Process(const int channel,
   const webrtc::ProcessingTypes type, sample* audio10ms,
   const int length, const int samplingFreq, const bool isStereo)
 {
   ReentrantMonitorAutoEnter enter(mMonitor);
+  if (mState != kStarted)
+    return;
 
   nsRefPtr<SharedBuffer> buffer = SharedBuffer::Create(length * sizeof(sample));
 
   sample* dest = static_cast<sample*>(buffer->Data());
   memcpy(dest, audio10ms, length * sizeof(sample));
 
   AudioSegment segment;
   segment.Init(CHANNELS);
--- a/content/media/webrtc/MediaEngineWebRTCVideo.cpp
+++ b/content/media/webrtc/MediaEngineWebRTCVideo.cpp
@@ -97,17 +97,16 @@ MediaEngineWebRTCVideoSource::DeliverFra
 // this means that no *real* frame can be inserted during this period.
 void
 MediaEngineWebRTCVideoSource::NotifyPull(MediaStreamGraph* aGraph,
                                          StreamTime aDesiredTime)
 {
   VideoSegment segment;
 
   ReentrantMonitorAutoEnter enter(mMonitor);
-
   if (mState != kStarted)
     return;
 
   // Note: we're not giving up mImage here
   nsRefPtr<layers::Image> image = mImage;
   TrackTicks target = TimeToTicksRoundUp(USECS_PER_S, aDesiredTime);
   TrackTicks delta = target - mLastEndTime;
 #ifdef LOG_ALL_FRAMES
@@ -273,24 +272,26 @@ MediaEngineWebRTCVideoSource::Start(Sour
 
 nsresult
 MediaEngineWebRTCVideoSource::Stop()
 {
   if (mState != kStarted) {
     return NS_ERROR_FAILURE;
   }
 
-  mSource->EndTrack(mTrackID);
-  mSource->Finish();
+  {
+    ReentrantMonitorAutoEnter enter(mMonitor);
+    mState = kStopped;
+    mSource->EndTrack(mTrackID);
+  }
 
   mViERender->StopRender(mCaptureIndex);
   mViERender->RemoveRenderer(mCaptureIndex);
   mViECapture->StopCapture(mCaptureIndex);
 
-  mState = kStopped;
   return NS_OK;
 }
 
 nsresult
 MediaEngineWebRTCVideoSource::Snapshot(uint32_t aDuration, nsIDOMFile** aFile)
 {
   /**
    * To get a Snapshot we do the following:
--- a/dom/media/MediaManager.h
+++ b/dom/media/MediaManager.h
@@ -92,16 +92,19 @@ public:
     if (mAudioSource) {
       mAudioSource->Stop();
       mAudioSource->Deallocate();
     }
     if (mVideoSource) {
       mVideoSource->Stop();
       mVideoSource->Deallocate();
     }
+    // Do this after stopping all tracks with EndTrack()
+    mStream->GetStream()->AsSourceStream()->Finish();
+
     nsCOMPtr<GetUserMediaNotificationEvent> event =
       new GetUserMediaNotificationEvent(GetUserMediaNotificationEvent::STOPPING);
 
     NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
   }
 
   void
   NotifyConsumptionChanged(MediaStreamGraph* aGraph, Consumption aConsuming)