Bug 1298217. Part 11 - Move some code to the entry action of DECODER_STATE_BUFFERING. draft
authorJW Wang <jwwang@mozilla.com>
Mon, 29 Aug 2016 11:50:10 +0800
changeset 406604 2abe85fe914097f6d766f4a3335defa520f30527
parent 406603 52a67e88e9f6c10ae3201439de9a078f485e0edf
child 406937 805f24b6d41cd7ad1617035ad68d1be23362dfdf
push id27772
push userjwwang@mozilla.com
push dateMon, 29 Aug 2016 06:35:00 +0000
bugs1298217
milestone51.0a1
Bug 1298217. Part 11 - Move some code to the entry action of DECODER_STATE_BUFFERING. MozReview-Commit-ID: BCtmXQi2zNg
dom/media/MediaDecoderStateMachine.cpp
--- a/dom/media/MediaDecoderStateMachine.cpp
+++ b/dom/media/MediaDecoderStateMachine.cpp
@@ -987,21 +987,17 @@ MediaDecoderStateMachine::MaybeStartBuff
       shouldBuffer = HasLowDecodedData(EXHAUSTED_DATA_MARGIN_USECS) &&
                      (JustExitedQuickBuffering() || HasLowUndecodedData());
     } else {
       MOZ_ASSERT(mReader->IsWaitForDataSupported());
       shouldBuffer = (OutOfDecodedAudio() && mReader->IsWaitingAudioData()) ||
                      (OutOfDecodedVideo() && mReader->IsWaitingVideoData());
     }
     if (shouldBuffer) {
-      StartBuffering();
-      // Don't go straight back to the state machine loop since that might
-      // cause us to start decoding again and we could flip-flop between
-      // decoding and quick-buffering.
-      ScheduleStateMachineIn(USECS_PER_S);
+      SetState(DECODER_STATE_BUFFERING);
     }
   }
 }
 
 void MediaDecoderStateMachine::UpdatePlaybackPositionInternal(int64_t aTime)
 {
   MOZ_ASSERT(OnTaskQueue());
   SAMPLE_LOG("UpdatePlaybackPositionInternal(%lld)", aTime);
@@ -1093,16 +1089,19 @@ MediaDecoderStateMachine::EnterState(Sta
         StopPlayback();
       }
       Reset();
       mReader->ReleaseResources();
       break;
     case DECODER_STATE_DECODING:
       StartDecoding();
       break;
+    case DECODER_STATE_BUFFERING:
+      StartBuffering();
+      break;
     case DECODER_STATE_COMPLETED:
       ScheduleStateMachine();
       break;
     case DECODER_STATE_ERROR:
     case DECODER_STATE_SHUTDOWN:
       mIsShutdown = true;
       break;
     default:
@@ -2557,41 +2556,46 @@ MediaDecoderStateMachine::GetStatistics(
   result.mTotalBytes = mResource->GetLength();
   result.mPlaybackRate = mPlaybackBytesPerSecond;
   result.mPlaybackRateReliable = mPlaybackRateReliable;
   result.mDecoderPosition = mDecoderPosition;
   result.mPlaybackPosition = mPlaybackOffset;
   return result;
 }
 
-void MediaDecoderStateMachine::StartBuffering()
+void
+MediaDecoderStateMachine::StartBuffering()
 {
   MOZ_ASSERT(OnTaskQueue());
-  MOZ_ASSERT(mState == DECODER_STATE_DECODING);
+  MOZ_ASSERT(mState == DECODER_STATE_BUFFERING);
 
   if (IsPlaying()) {
     StopPlayback();
   }
 
   TimeDuration decodeDuration = TimeStamp::Now() - mDecodeStartTime;
   // Go into quick buffering mode provided we've not just left buffering using
   // a "quick exit". This stops us flip-flopping between playing and buffering
   // when the download speed is similar to the decode speed.
   mQuickBuffering =
     !JustExitedQuickBuffering() &&
     decodeDuration < UsecsToDuration(QUICK_BUFFER_THRESHOLD_USECS);
   mBufferingStart = TimeStamp::Now();
 
-  SetState(DECODER_STATE_BUFFERING);
   DECODER_LOG("Changed state from DECODING to BUFFERING, decoded for %.3lfs",
               decodeDuration.ToSeconds());
   MediaStatistics stats = GetStatistics();
   DECODER_LOG("Playback rate: %.1lfKB/s%s download rate: %.1lfKB/s%s",
               stats.mPlaybackRate/1024, stats.mPlaybackRateReliable ? "" : " (unreliable)",
               stats.mDownloadRate/1024, stats.mDownloadRateReliable ? "" : " (unreliable)");
+
+  // Don't go straight back to the state machine loop since that might
+  // cause us to start decoding again and we could flip-flop between
+  // decoding and quick-buffering.
+  ScheduleStateMachineIn(USECS_PER_S);
 }
 
 void
 MediaDecoderStateMachine::ScheduleStateMachine()
 {
   MOZ_ASSERT(OnTaskQueue());
   if (mDispatchedStateMachine) {
     return;