Bug 1388228. P1 - move some members to ChannelMediaDecoder. draft
authorJW Wang <jwwang@mozilla.com>
Fri, 04 Aug 2017 16:02:40 +0800
changeset 642344 e798a152f933606b960da43467d1c47a2f5aa05f
parent 642343 63e8767275aaf989fdedf8eb577e895d97953355
child 642345 6fb5b74536c05dba010290d6edd0357fd0bf1870
push id72707
push userjwwang@mozilla.com
push dateTue, 08 Aug 2017 02:32:36 +0000
bugs1388228
milestone57.0a1
Bug 1388228. P1 - move some members to ChannelMediaDecoder. These members are used only by ChannelMediaDecoder. MozReview-Commit-ID: 10CHV1sjY9k
dom/media/ChannelMediaDecoder.cpp
dom/media/ChannelMediaDecoder.h
dom/media/MediaDecoder.cpp
dom/media/MediaDecoder.h
--- a/dom/media/ChannelMediaDecoder.cpp
+++ b/dom/media/ChannelMediaDecoder.cpp
@@ -8,16 +8,21 @@
 #include "DecoderTraits.h"
 #include "MediaDecoderStateMachine.h"
 #include "MediaFormatReader.h"
 #include "MediaResource.h"
 #include "MediaShutdownManager.h"
 
 namespace mozilla {
 
+extern LazyLogModule gMediaDecoderLog;
+#define LOG(x, ...)                                                            \
+  MOZ_LOG(                                                                     \
+    gMediaDecoderLog, LogLevel::Debug, ("Decoder=%p " x, this, ##__VA_ARGS__))
+
 ChannelMediaDecoder::ResourceCallback::ResourceCallback(
   AbstractThread* aMainThread)
   : mAbstractMainThread(aMainThread)
 {
   MOZ_ASSERT(aMainThread);
 }
 
 void
@@ -146,18 +151,22 @@ ChannelMediaDecoder::ResourceCallback::N
     }
   });
   mAbstractMainThread->Dispatch(r.forget());
 }
 
 ChannelMediaDecoder::ChannelMediaDecoder(MediaDecoderInit& aInit)
   : MediaDecoder(aInit)
   , mResourceCallback(new ResourceCallback(aInit.mOwner->AbstractMainThread()))
+  , mWatchManager(this, aInit.mOwner->AbstractMainThread())
 {
   mResourceCallback->Connect(this);
+
+  // mIgnoreProgressData
+  mWatchManager.Watch(mLogicallySeeking, &ChannelMediaDecoder::SeekingChanged);
 }
 
 bool
 ChannelMediaDecoder::CanClone()
 {
   MOZ_ASSERT(NS_IsMainThread());
   return mResource && mResource->CanClone();
 }
@@ -197,16 +206,17 @@ MediaDecoderStateMachine* ChannelMediaDe
   init.mResource = mResource;
   mReader = DecoderTraits::CreateReader(ContainerType(), init);
   return new MediaDecoderStateMachine(this, mReader);
 }
 
 void
 ChannelMediaDecoder::Shutdown()
 {
+  mWatchManager.Shutdown();
   mResourceCallback->Disconnect();
   MediaDecoder::Shutdown();
 }
 
 nsresult
 ChannelMediaDecoder::OpenResource(nsIStreamListener** aStreamListener)
 {
   MOZ_ASSERT(NS_IsMainThread());
@@ -264,9 +274,66 @@ ChannelMediaDecoder::Load(BaseMediaResou
   NS_ENSURE_SUCCESS(rv, rv);
 
   SetStateMachine(CreateStateMachine());
   NS_ENSURE_TRUE(GetStateMachine(), NS_ERROR_FAILURE);
 
   return InitializeStateMachine();
 }
 
+void
+ChannelMediaDecoder::NotifyDownloadEnded(nsresult aStatus)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
+  AbstractThread::AutoEnter context(AbstractMainThread());
+
+  LOG("NotifyDownloadEnded, status=%" PRIx32, static_cast<uint32_t>(aStatus));
+
+  if (aStatus == NS_BINDING_ABORTED) {
+    // Download has been cancelled by user.
+    GetOwner()->LoadAborted();
+    return;
+  }
+
+  UpdatePlaybackRate();
+
+  if (NS_SUCCEEDED(aStatus)) {
+    // A final progress event will be fired by the MediaResource calling
+    // DownloadSuspended on the element.
+    // Also NotifySuspendedStatusChanged() will be called to update readyState
+    // if download ended with success.
+  } else if (aStatus != NS_BASE_STREAM_CLOSED) {
+    NetworkError();
+  }
+}
+
+void
+ChannelMediaDecoder::NotifyBytesConsumed(int64_t aBytes, int64_t aOffset)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
+  AbstractThread::AutoEnter context(AbstractMainThread());
+
+  if (mIgnoreProgressData) {
+    return;
+  }
+
+  MOZ_ASSERT(GetStateMachine());
+  if (aOffset >= mDecoderPosition) {
+    mPlaybackStatistics.AddBytes(aBytes);
+  }
+  mDecoderPosition = aOffset + aBytes;
+}
+
+void
+ChannelMediaDecoder::SeekingChanged()
+{
+  // Stop updating the bytes downloaded for progress notifications when
+  // seeking to prevent wild changes to the progress notification.
+  MOZ_ASSERT(NS_IsMainThread());
+  mIgnoreProgressData = mLogicallySeeking;
+}
+
 } // namespace mozilla
+
+// avoid redefined macro in unified build
+#undef LOG
--- a/dom/media/ChannelMediaDecoder.h
+++ b/dom/media/ChannelMediaDecoder.h
@@ -74,13 +74,32 @@ public:
   nsresult Load(nsIChannel* aChannel,
                 bool aIsPrivateBrowsing,
                 nsIStreamListener** aStreamListener);
 
 private:
   virtual ChannelMediaDecoder* CloneImpl(MediaDecoderInit& aInit) = 0;
   nsresult OpenResource(nsIStreamListener** aStreamListener);
   nsresult Load(BaseMediaResource* aOriginal);
+
+  // Called by MediaResource when the download has ended.
+  // Called on the main thread only. aStatus is the result from OnStopRequest.
+  void NotifyDownloadEnded(nsresult aStatus);
+
+  // Called by the MediaResource to keep track of the number of bytes read
+  // 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);
+
+  void SeekingChanged();
+
+  WatchManager<ChannelMediaDecoder> mWatchManager;
+
+  // True when seeking or otherwise moving the play position around in
+  // such a manner that progress event data is inaccurate. This is set
+  // during seek and duration operations to prevent the progress indicator
+  // from jumping around. Read/Write on the main thread only.
+  bool mIgnoreProgressData = false;
 };
 
 } // namespace mozilla
 
 #endif // ChannelMediaDecoder_h_
--- a/dom/media/MediaDecoder.cpp
+++ b/dom/media/MediaDecoder.cpp
@@ -358,17 +358,16 @@ MediaDecoder::IsInfinite() const
 #define INIT_CANONICAL(name, val) \
   name(mOwner->AbstractMainThread(), val, "MediaDecoder::" #name " (Canonical)")
 
 MediaDecoder::MediaDecoder(MediaDecoderInit& aInit)
   : mWatchManager(this, aInit.mOwner->AbstractMainThread())
   , mLogicalPosition(0.0)
   , mDuration(std::numeric_limits<double>::quiet_NaN())
   , mCDMProxyPromise(mCDMProxyPromiseHolder.Ensure(__func__))
-  , mIgnoreProgressData(false)
   , mOwner(aInit.mOwner)
   , mAbstractMainThread(aInit.mOwner->AbstractMainThread())
   , mFrameStats(new FrameStatistics())
   , mVideoFrameContainer(aInit.mOwner->GetVideoFrameContainer())
   , mPinnedForSeek(false)
   , mAudioChannel(aInit.mAudioChannel)
   , mMinimizePreroll(aInit.mMinimizePreroll)
   , mFiredMetadataLoaded(false)
@@ -417,19 +416,16 @@ MediaDecoder::MediaDecoder(MediaDecoderI
   // depends on the download rate.
   mWatchManager.Watch(mBuffered, &MediaDecoder::UpdateReadyState);
 
   // mLogicalPosition
   mWatchManager.Watch(mCurrentPosition, &MediaDecoder::UpdateLogicalPosition);
   mWatchManager.Watch(mPlayState, &MediaDecoder::UpdateLogicalPosition);
   mWatchManager.Watch(mLogicallySeeking, &MediaDecoder::UpdateLogicalPosition);
 
-  // mIgnoreProgressData
-  mWatchManager.Watch(mLogicallySeeking, &MediaDecoder::SeekingChanged);
-
   mWatchManager.Watch(mIsAudioDataAudible,
                       &MediaDecoder::NotifyAudibleStateChanged);
 
   MediaShutdownManager::InitStatics();
   mVideoDecodingOberver->RegisterEvent();
 }
 
 #undef INIT_MIRROR
@@ -1038,72 +1034,27 @@ MediaDecoder::DownloadProgressed()
   MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
   AbstractThread::AutoEnter context(AbstractMainThread());
   UpdatePlaybackRate();
   GetOwner()->DownloadProgressed();
   GetResource()->ThrottleReadahead(ShouldThrottleDownload());
 }
 
 void
-MediaDecoder::NotifyDownloadEnded(nsresult aStatus)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
-  AbstractThread::AutoEnter context(AbstractMainThread());
-
-  LOG("NotifyDownloadEnded, status=%" PRIx32, static_cast<uint32_t>(aStatus));
-
-  if (aStatus == NS_BINDING_ABORTED) {
-    // Download has been cancelled by user.
-    GetOwner()->LoadAborted();
-    return;
-  }
-
-  UpdatePlaybackRate();
-
-  if (NS_SUCCEEDED(aStatus)) {
-    // A final progress event will be fired by the MediaResource calling
-    // DownloadSuspended on the element.
-    // Also NotifySuspendedStatusChanged() will be called to update readyState
-    // if download ended with success.
-  } else if (aStatus != NS_BASE_STREAM_CLOSED) {
-    NetworkError();
-  }
-}
-
-void
 MediaDecoder::NotifyPrincipalChanged()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
   AbstractThread::AutoEnter context(AbstractMainThread());
   nsCOMPtr<nsIPrincipal> newPrincipal = GetCurrentPrincipal();
   mMediaPrincipalHandle = MakePrincipalHandle(newPrincipal);
   GetOwner()->NotifyDecoderPrincipalChanged();
 }
 
 void
-MediaDecoder::NotifyBytesConsumed(int64_t aBytes, int64_t aOffset)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
-  AbstractThread::AutoEnter context(AbstractMainThread());
-
-  if (mIgnoreProgressData) {
-    return;
-  }
-
-  MOZ_ASSERT(mDecoderStateMachine);
-  if (aOffset >= mDecoderPosition) {
-    mPlaybackStatistics.AddBytes(aBytes);
-  }
-  mDecoderPosition = aOffset + aBytes;
-}
-
-void
 MediaDecoder::OnSeekResolved()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
   AbstractThread::AutoEnter context(AbstractMainThread());
   mSeekRequest.Complete();
 
   {
--- a/dom/media/MediaDecoder.h
+++ b/dom/media/MediaDecoder.h
@@ -348,24 +348,16 @@ private:
 
   // Called when the video has completed playing.
   // Call on the main thread only.
   void PlaybackEnded();
 
   void OnSeekRejected();
   void OnSeekResolved();
 
-  void SeekingChanged()
-  {
-    // Stop updating the bytes downloaded for progress notifications when
-    // seeking to prevent wild changes to the progress notification.
-    MOZ_ASSERT(NS_IsMainThread());
-    mIgnoreProgressData = mLogicallySeeking;
-  }
-
   // Seeking has started. Inform the element on the main thread.
   void SeekingStarted();
 
   void UpdateLogicalPositionInternal();
   void UpdateLogicalPosition()
   {
     MOZ_ASSERT(NS_IsMainThread());
     MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
@@ -566,34 +558,18 @@ protected:
   // should stop buffering or otherwise waiting for download progress and
   // start consuming data, if possible, because the cache is full.
   void NotifySuspendedStatusChanged();
 
   // Called by MediaResource when the principal of the resource has
   // changed. Called on main thread only.
   void NotifyPrincipalChanged();
 
-  // Called by the MediaResource to keep track of the number of bytes read
-  // 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);
-
   MozPromiseRequestHolder<SeekPromise> mSeekRequest;
 
-  // True when seeking or otherwise moving the play position around in
-  // such a manner that progress event data is inaccurate. This is set
-  // during seek and duration operations to prevent the progress indicator
-  // from jumping around. Read/Write on the main thread only.
-  bool mIgnoreProgressData;
-
   // Ensures our media stream has been pinned.
   void PinForSeek();
 
   // Ensures our media stream has been unpinned.
   void UnpinForSeek();
 
   const char* PlayStateStr();