Bug 1332585 - Protect TrackEncoder members from races. r=jesup
authorAndreas Pehrson <pehrsons@gmail.com>
Fri, 20 Jan 2017 17:00:02 +0100
changeset 375499 f5a4d8f2a6e4f8274fa0ad6979d56b2cfe919c1b
parent 375498 9e240bc4bd3a3c28c54b0512bffdc675f4dab837
child 375500 63dcf0258009ca28d9a14d624a629c4b674e2ab7
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs1332585
milestone53.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 1332585 - Protect TrackEncoder members from races. r=jesup mCanceled is one member which is protected only in a couple of places. I hit a MOZ_CRASH without this in a gtest, but I'm not sure if we haven't seen it in release by chance or by design. MozReview-Commit-ID: 61KpjaBDyhB
dom/media/encoder/TrackEncoder.cpp
--- a/dom/media/encoder/TrackEncoder.cpp
+++ b/dom/media/encoder/TrackEncoder.cpp
@@ -52,16 +52,18 @@ void TrackEncoder::NotifyEvent(MediaStre
 
 void
 AudioTrackEncoder::NotifyQueuedTrackChanges(MediaStreamGraph* aGraph,
                                             TrackID aID,
                                             StreamTime aTrackOffset,
                                             uint32_t aTrackEvents,
                                             const MediaSegment& aQueuedMedia)
 {
+  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+
   if (mCanceled) {
     return;
   }
 
   const AudioSegment& audio = static_cast<const AudioSegment&>(aQueuedMedia);
 
   // Check and initialize parameters for codec encoder.
   if (!mInitialized) {
@@ -104,23 +106,24 @@ AudioTrackEncoder::NotifyQueuedTrackChan
     LOG("[AudioTrackEncoder]: Receive TRACK_EVENT_ENDED .");
     NotifyEndOfStream();
   }
 }
 
 void
 AudioTrackEncoder::NotifyEndOfStream()
 {
+  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+
   // If source audio track is completely silent till the end of encoding,
   // initialize the encoder with default channel counts and sampling rate.
   if (!mCanceled && !mInitialized) {
     Init(DEFAULT_CHANNELS, DEFAULT_SAMPLING_RATE);
   }
 
-  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
   mEndOfStream = true;
   mReentrantMonitor.NotifyAll();
 }
 
 nsresult
 AudioTrackEncoder::AppendAudioSegment(const AudioSegment& aSegment)
 {
   ReentrantMonitorAutoEnter mon(mReentrantMonitor);
@@ -190,16 +193,18 @@ size_t
 AudioTrackEncoder::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
 {
   return mRawSegment.SizeOfExcludingThis(aMallocSizeOf);
 }
 
 void
 VideoTrackEncoder::Init(const VideoSegment& aSegment)
 {
+  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+
   if (mInitialized) {
     return;
   }
 
   mInitCounter++;
   TRACK_LOG(LogLevel::Debug, ("Init the video encoder %d times", mInitCounter));
   VideoSegment::ConstChunkIterator iter(aSegment);
   while (!iter.IsEnded()) {
@@ -222,37 +227,40 @@ VideoTrackEncoder::Init(const VideoSegme
 
   mNotInitDuration += aSegment.GetDuration();
   if ((mNotInitDuration / mTrackRate > INIT_FAILED_DURATION) &&
       mInitCounter > 1) {
     LOG("[VideoTrackEncoder]: Initialize failed for %ds.", INIT_FAILED_DURATION);
     NotifyEndOfStream();
     return;
   }
-
 }
 
 void
 VideoTrackEncoder::SetCurrentFrames(const VideoSegment& aSegment)
 {
+  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+
   if (mCanceled) {
     return;
   }
 
   Init(aSegment);
   AppendVideoSegment(aSegment);
 }
 
 void
 VideoTrackEncoder::NotifyQueuedTrackChanges(MediaStreamGraph* aGraph,
                                             TrackID aID,
                                             StreamTime aTrackOffset,
                                             uint32_t aTrackEvents,
                                             const MediaSegment& aQueuedMedia)
 {
+  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+
   if (mCanceled) {
     return;
   }
 
   if (!(aTrackEvents == TRACK_EVENT_CREATED ||
        aTrackEvents == TRACK_EVENT_ENDED)) {
     return;
   }
@@ -385,25 +393,25 @@ VideoTrackEncoder::AppendVideoSegment(co
   }
 
   return NS_OK;
 }
 
 void
 VideoTrackEncoder::NotifyEndOfStream()
 {
+  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+
   // If source video track is muted till the end of encoding, initialize the
   // encoder with default frame width, frame height, and track rate.
   if (!mCanceled && !mInitialized) {
     Init(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT,
          DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
   }
 
-  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
-
   if (mEndOfStream) {
     // We have already been notified.
     return;
   }
 
   mEndOfStream = true;
   TRACK_LOG(LogLevel::Info, ("[VideoTrackEncoder]: Reached end of stream"));