Bug 1228923 - Merge some MediaEventSource for MDSM. r=jya.
authorJW Wang <jwwang@mozilla.com>
Tue, 01 Dec 2015 09:34:02 +0800
changeset 308949 ef6971d9f7196b030b23a96c8e81f23cac4b8e34
parent 308948 bec055709c6a342aedfba9e1e70380301a7ed5e4
child 308950 518be0ecf0713bb8424c74ee89aa76706e06182f
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1228923
milestone45.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 1228923 - Merge some MediaEventSource for MDSM. r=jya.
dom/media/MediaDecoder.cpp
dom/media/MediaDecoder.h
dom/media/MediaDecoderStateMachine.cpp
dom/media/MediaDecoderStateMachine.h
--- a/dom/media/MediaDecoder.cpp
+++ b/dom/media/MediaDecoder.cpp
@@ -610,21 +610,17 @@ MediaDecoder::Shutdown()
   // This changes the decoder state to SHUTDOWN and does other things
   // necessary to unblock the state machine thread if it's blocked, so
   // the asynchronous shutdown in nsDestroyStateMachine won't deadlock.
   if (mDecoderStateMachine) {
     mDecoderStateMachine->DispatchShutdown();
     mTimedMetadataListener.Disconnect();
     mMetadataLoadedListener.Disconnect();
     mFirstFrameLoadedListener.Disconnect();
-    mOnPlaybackStart.Disconnect();
-    mOnPlaybackStop.Disconnect();
-    mOnPlaybackEnded.Disconnect();
-    mOnDecodeError.Disconnect();
-    mOnInvalidate.Disconnect();
+    mOnPlaybackEvent.Disconnect();
     mOnSeekingStart.Disconnect();
   }
 
   // Force any outstanding seek and byterange requests to complete
   // to prevent shutdown from deadlocking.
   if (mResource) {
     mResource->Close();
   }
@@ -639,16 +635,39 @@ MediaDecoder::Shutdown()
 MediaDecoder::~MediaDecoder()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MediaMemoryTracker::RemoveMediaDecoder(this);
   UnpinForSeek();
   MOZ_COUNT_DTOR(MediaDecoder);
 }
 
+void
+MediaDecoder::OnPlaybackEvent(MediaEventType aEvent)
+{
+  switch (aEvent) {
+    case MediaEventType::PlaybackStarted:
+      mPlaybackStatistics->Start();
+      break;
+    case MediaEventType::PlaybackStopped:
+      mPlaybackStatistics->Stop();
+      ComputePlaybackRate();
+      break;
+    case MediaEventType::PlaybackEnded:
+      PlaybackEnded();
+      break;
+    case MediaEventType::DecodeError:
+      DecodeError();
+      break;
+    case MediaEventType::Invalidate:
+      Invalidate();
+      break;
+  }
+}
+
 MediaResourceCallback*
 MediaDecoder::GetResourceCallback() const
 {
   return mResourceCallback;
 }
 
 nsresult
 MediaDecoder::OpenResource(nsIStreamListener** aStreamListener)
@@ -700,26 +719,18 @@ MediaDecoder::SetStateMachineParameters(
   }
   mTimedMetadataListener = mDecoderStateMachine->TimedMetadataEvent().Connect(
     AbstractThread::MainThread(), this, &MediaDecoder::OnMetadataUpdate);
   mMetadataLoadedListener = mDecoderStateMachine->MetadataLoadedEvent().Connect(
     AbstractThread::MainThread(), this, &MediaDecoder::MetadataLoaded);
   mFirstFrameLoadedListener = mDecoderStateMachine->FirstFrameLoadedEvent().Connect(
     AbstractThread::MainThread(), this, &MediaDecoder::FirstFrameLoaded);
 
-  mOnPlaybackStart = mDecoderStateMachine->OnPlaybackStart().Connect(
-    AbstractThread::MainThread(), this, &MediaDecoder::OnPlaybackStarted);
-  mOnPlaybackStop = mDecoderStateMachine->OnPlaybackStop().Connect(
-    AbstractThread::MainThread(), this, &MediaDecoder::OnPlaybackStopped);
-  mOnPlaybackEnded = mDecoderStateMachine->OnPlaybackEnded().Connect(
-    AbstractThread::MainThread(), this, &MediaDecoder::PlaybackEnded);
-  mOnDecodeError = mDecoderStateMachine->OnDecodeError().Connect(
-    AbstractThread::MainThread(), this, &MediaDecoder::DecodeError);
-  mOnInvalidate = mDecoderStateMachine->OnInvalidate().Connect(
-    AbstractThread::MainThread(), this, &MediaDecoder::Invalidate);
+  mOnPlaybackEvent = mDecoderStateMachine->OnPlaybackEvent().Connect(
+    AbstractThread::MainThread(), this, &MediaDecoder::OnPlaybackEvent);
   mOnSeekingStart = mDecoderStateMachine->OnSeekingStart().Connect(
     AbstractThread::MainThread(), this, &MediaDecoder::SeekingStarted);
 }
 
 void
 MediaDecoder::SetMinimizePrerollUntilPlaybackStarts()
 {
   MOZ_ASSERT(NS_IsMainThread());
--- a/dom/media/MediaDecoder.h
+++ b/dom/media/MediaDecoder.h
@@ -217,16 +217,18 @@ destroying the MediaDecoder object.
 class nsIStreamListener;
 class nsIPrincipal;
 
 namespace mozilla {
 
 class VideoFrameContainer;
 class MediaDecoderStateMachine;
 
+enum class MediaEventType : int8_t;
+
 // GetCurrentTime is defined in winbase.h as zero argument macro forwarding to
 // GetTickCount() and conflicts with MediaDecoder::GetCurrentTime implementation.
 #ifdef GetCurrentTime
 #undef GetCurrentTime
 #endif
 
 // Stores the seek target; the time to seek to, and whether an Accurate,
 // or "Fast" (nearest keyframe) seek was requested.
@@ -795,27 +797,17 @@ private:
   // state machine. Call on the main thread only.
   void MetadataLoaded(nsAutoPtr<MediaInfo> aInfo,
                       nsAutoPtr<MetadataTags> aTags,
                       MediaDecoderEventVisibility aEventVisibility);
 
   MediaEventSource<void>*
   DataArrivedEvent() override { return &mDataArrivedEvent; }
 
-  // Used to estimate rates of data passing through the decoder's channel.
-  // Records activity stopping on the channel.
-  void OnPlaybackStarted() { mPlaybackStatistics->Start(); }
-
-  // Used to estimate rates of data passing through the decoder's channel.
-  // Records activity stopping on the channel.
-  void OnPlaybackStopped()
-  {
-    mPlaybackStatistics->Stop();
-    ComputePlaybackRate();
-  }
+  void OnPlaybackEvent(MediaEventType aEvent);
 
   MediaEventProducer<void> mDataArrivedEvent;
 
   // The state machine object for handling the decoding. It is safe to
   // call methods of this object from other threads. Its internal data
   // is synchronised on a monitor. The lifetime of this object is
   // after mPlayState is LOADING and before mPlayState is SHUTDOWN. It
   // is safe to access it during this period.
@@ -928,21 +920,17 @@ protected:
   nsCOMPtr<nsITimer> mDormantTimer;
 
   // A listener to receive metadata updates from MDSM.
   MediaEventListener mTimedMetadataListener;
 
   MediaEventListener mMetadataLoadedListener;
   MediaEventListener mFirstFrameLoadedListener;
 
-  MediaEventListener mOnPlaybackStart;
-  MediaEventListener mOnPlaybackStop;
-  MediaEventListener mOnPlaybackEnded;
-  MediaEventListener mOnDecodeError;
-  MediaEventListener mOnInvalidate;
+  MediaEventListener mOnPlaybackEvent;
   MediaEventListener mOnSeekingStart;
 
 protected:
   // Whether the state machine is shut down.
   Mirror<bool> mStateMachineIsShutdown;
 
   // Buffered range, mirrored from the reader.
   Mirror<media::TimeIntervals> mBuffered;
@@ -1089,15 +1077,15 @@ private:
   // from the resource. Called on the main by an event runner dispatched
   // by the MediaResource read functions.
   void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset);
 
   // Called by nsChannelToPipeListener or MediaResource when the
   // download has ended. Called on the main thread only. aStatus is
   // the result from OnStopRequest.
   void NotifyDownloadEnded(nsresult aStatus);
-  
+
   bool mTelemetryReported;
 };
 
 } // namespace mozilla
 
 #endif
--- a/dom/media/MediaDecoderStateMachine.cpp
+++ b/dom/media/MediaDecoderStateMachine.cpp
@@ -1049,17 +1049,17 @@ nsresult MediaDecoderStateMachine::Init(
   return NS_OK;
 }
 
 void MediaDecoderStateMachine::StopPlayback()
 {
   MOZ_ASSERT(OnTaskQueue());
   DECODER_LOG("StopPlayback()");
 
-  mOnPlaybackStop.Notify();
+  mOnPlaybackEvent.Notify(MediaEventType::PlaybackStopped);
 
   if (IsPlaying()) {
     mMediaSink->SetPlaying(false);
     MOZ_ASSERT(!IsPlaying());
   }
 
   DispatchDecodeTasksIfNeeded();
 }
@@ -1082,17 +1082,17 @@ void MediaDecoderStateMachine::MaybeStar
                 "mIsAudioPrerolling: %d, mIsVideoPrerolling: %d, "
                 "mAudioOffloading: %d]",
                 (int)playStatePermits, (int)mIsAudioPrerolling,
                 (int)mIsVideoPrerolling, (int)mAudioOffloading);
     return;
   }
 
   DECODER_LOG("MaybeStartPlayback() starting playback");
-  mOnPlaybackStart.Notify();
+  mOnPlaybackEvent.Notify(MediaEventType::PlaybackStarted);
   StartMediaSink();
 
   if (!IsPlaying()) {
     mMediaSink->SetPlaying(true);
     MOZ_ASSERT(IsPlaying());
   }
 
   DispatchDecodeTasksIfNeeded();
@@ -1887,17 +1887,17 @@ MediaDecoderStateMachine::DecodeError()
   // Change state to error, which will cause the state machine to wait until
   // the MediaDecoder shuts it down.
   SetState(DECODER_STATE_ERROR);
   ScheduleStateMachine();
   DECODER_WARN("Decode error, changed state to ERROR");
 
   // MediaDecoder::DecodeError notifies the owner, and then shuts down the state
   // machine.
-  mOnDecodeError.Notify();
+  mOnPlaybackEvent.Notify(MediaEventType::DecodeError);
 }
 
 void
 MediaDecoderStateMachine::OnMetadataRead(MetadataHolder* aMetadata)
 {
   MOZ_ASSERT(OnTaskQueue());
   MOZ_ASSERT(mState == DECODER_STATE_DECODING_METADATA);
   mMetadataRequest.Complete();
@@ -2146,17 +2146,17 @@ MediaDecoderStateMachine::SeekCompleted(
   // if we need to buffer after the seek.
   mQuickBuffering = false;
 
   mCurrentSeek.Resolve(mState == DECODER_STATE_COMPLETED, __func__);
   ScheduleStateMachine();
 
   if (video) {
     mMediaSink->Redraw();
-    mOnInvalidate.Notify();
+    mOnPlaybackEvent.Notify(MediaEventType::Invalidate);
   }
 }
 
 class DecoderDisposer
 {
 public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DecoderDisposer)
   DecoderDisposer(MediaDecoder* aDecoder, MediaDecoderStateMachine* aStateMachine)
@@ -2399,17 +2399,17 @@ nsresult MediaDecoderStateMachine::RunSt
       {
         int64_t clockTime = std::max(AudioEndTime(), VideoEndTime());
         clockTime = std::max(int64_t(0), std::max(clockTime, Duration().ToMicroseconds()));
         UpdatePlaybackPosition(clockTime);
 
         // Ensure readyState is updated before firing the 'ended' event.
         UpdateNextFrameStatus();
 
-        mOnPlaybackEnded.Notify();
+        mOnPlaybackEvent.Notify(MediaEventType::PlaybackEnded);
 
         mSentPlaybackEndedEvent = true;
 
         // MediaSink::GetEndTime() must be called before stopping playback.
         StopMediaSink();
       }
 
       return NS_OK;
--- a/dom/media/MediaDecoderStateMachine.h
+++ b/dom/media/MediaDecoderStateMachine.h
@@ -105,16 +105,24 @@ class MediaSink;
 
 class AudioSegment;
 class DecodedStream;
 class TaskQueue;
 
 extern LazyLogModule gMediaDecoderLog;
 extern LazyLogModule gMediaSampleLog;
 
+enum class MediaEventType : int8_t {
+  PlaybackStarted,
+  PlaybackStopped,
+  PlaybackEnded,
+  DecodeError,
+  Invalidate
+};
+
 /*
   The state machine class. This manages the decoding and seeking in the
   MediaDecoderReader on the decode task queue, and A/V sync on the shared
   state machine thread, and controls the audio "push" thread.
 
   All internal state is synchronised via the decoder monitor. State changes
   are propagated by scheduling the state machine to run another cycle on the
   shared state machine thread.
@@ -228,21 +236,18 @@ public:
                       nsAutoPtr<MetadataTags>,
                       MediaDecoderEventVisibility>&
   MetadataLoadedEvent() { return mMetadataLoadedEvent; }
 
   MediaEventSourceExc<nsAutoPtr<MediaInfo>,
                       MediaDecoderEventVisibility>&
   FirstFrameLoadedEvent() { return mFirstFrameLoadedEvent; }
 
-  MediaEventSource<void>& OnPlaybackStart() { return mOnPlaybackStart; }
-  MediaEventSource<void>& OnPlaybackStop() { return mOnPlaybackStop; }
-  MediaEventSource<void>& OnPlaybackEnded() { return mOnPlaybackEnded; }
-  MediaEventSource<void>& OnDecodeError() { return mOnDecodeError; }
-  MediaEventSource<void>& OnInvalidate() { return mOnInvalidate; }
+  MediaEventSource<MediaEventType>&
+  OnPlaybackEvent() { return mOnPlaybackEvent; }
 
   MediaEventSource<MediaDecoderEventVisibility>&
   OnSeekingStart() { return mOnSeekingStart; }
 
   // Immutable after construction - may be called on any thread.
   bool IsRealTime() const { return mRealTime; }
 
   size_t SizeOfVideoQueue() {
@@ -1205,21 +1210,17 @@ private:
   MediaEventListener mVideoQueueListener;
 
   MediaEventProducerExc<nsAutoPtr<MediaInfo>,
                         nsAutoPtr<MetadataTags>,
                         MediaDecoderEventVisibility> mMetadataLoadedEvent;
   MediaEventProducerExc<nsAutoPtr<MediaInfo>,
                         MediaDecoderEventVisibility> mFirstFrameLoadedEvent;
 
-  MediaEventProducer<void> mOnPlaybackStart;
-  MediaEventProducer<void> mOnPlaybackStop;
-  MediaEventProducer<void> mOnPlaybackEnded;
-  MediaEventProducer<void> mOnDecodeError;
-  MediaEventProducer<void> mOnInvalidate;
+  MediaEventProducer<MediaEventType> mOnPlaybackEvent;
   MediaEventProducer<MediaDecoderEventVisibility> mOnSeekingStart;
 
   // True if audio is offloading.
   // Playback will not start when audio is offloading.
   bool mAudioOffloading;
 
 #ifdef MOZ_EME
   void OnCDMProxyReady(RefPtr<CDMProxy> aProxy);