From e9c23fc1ad2b5cb061d4eccdb7aa91400ac85354 Mon Sep 17 00:00:00 2001 From: Kaku Kuo Date: Fri, 10 Mar 2017 16:17:46 +0800 Subject: [PATCH] Bug 1346498 part 2 - implement the VideoDecodeMode mechanism in MDSM; r=jwwang The MDSM::mVideoDecodeMode and MDSM::SetVideoDecodeMode() are merely a renaming of MDSM::mIsVisible and MDSM::VisibilityChanged(). However, the renaming explicitly reflects that MDSM provides mechanism only without participating in the policy decision. Will reremove the MDSM::mIsVisible and MDSM::VisibilityChanged() in following patches. MozReview-Commit-ID: JdMKQTgVCf3 --HG-- extra : rebase_source : 95977b205f235b6a456d12dfde93fe84f3b12aa7 extra : source : 4382a3c10f30653d5a70abb3bd4b8146a4272784 --- dom/media/MediaDecoderStateMachine.cpp | 51 ++++++++++++++++++++++++++ dom/media/MediaDecoderStateMachine.h | 14 +++++++ 2 files changed, 65 insertions(+) diff --git a/dom/media/MediaDecoderStateMachine.cpp b/dom/media/MediaDecoderStateMachine.cpp index bd6732384f9d..6f4363123a54 100644 --- a/dom/media/MediaDecoderStateMachine.cpp +++ b/dom/media/MediaDecoderStateMachine.cpp @@ -2611,6 +2611,7 @@ MediaDecoderStateMachine::MediaDecoderStateMachine(MediaDecoder* aDecoder, mVideoDecodeSuspendTimer(mTaskQueue), mOutputStreamManager(new OutputStreamManager()), mResource(aDecoder->GetResource()), + mVideoDecodeMode(VideoDecodeMode::Normal), mIsMSE(aDecoder->IsMSE()), INIT_MIRROR(mBuffered, TimeIntervals()), INIT_MIRROR(mEstimatedDuration, NullableTimeUnit()), @@ -3068,6 +3069,56 @@ void MediaDecoderStateMachine::SuspendTaintChanged() } } +void MediaDecoderStateMachine::SetVideoDecodeMode(VideoDecodeMode aMode) +{ + nsCOMPtr r = + NewRunnableMethod(this, + &MediaDecoderStateMachine::SetVideoDecodeModeInternal, + aMode); + OwnerThread()->DispatchStateChange(r.forget()); +} + +void MediaDecoderStateMachine::SetVideoDecodeModeInternal(VideoDecodeMode aMode) +{ + MOZ_ASSERT(OnTaskQueue()); + DECODER_LOG("VideoDecodeModeChanged: VideoDecodeMode=(%s->%s), mVideoDecodeSuspended=%c", + mVideoDecodeMode == VideoDecodeMode::Normal ? "Normal" : "Suspend", + aMode == VideoDecodeMode::Normal ? "Normal" : "Suspend", + mVideoDecodeSuspended ? 'T' : 'F'); + + if (!MediaPrefs::MDSMSuspendBackgroundVideoEnabled()) { + return; + } + + if (aMode == mVideoDecodeMode) { + return; + } + + // Set new video decode mode. + mVideoDecodeMode = aMode; + + // Start timer to trigger suspended video decoding. + if (mVideoDecodeMode == VideoDecodeMode::Suspend) { + TimeStamp target = TimeStamp::Now() + SuspendBackgroundVideoDelay(); + + RefPtr self = this; + mVideoDecodeSuspendTimer.Ensure(target, + [=]() { self->OnSuspendTimerResolved(); }, + [] () { MOZ_DIAGNOSTIC_ASSERT(false); }); + mOnPlaybackEvent.Notify(MediaEventType::StartVideoSuspendTimer); + return; + } + + // Resuming from suspended decoding + + // If suspend timer exists, destroy it. + CancelSuspendTimer(); + + if (mVideoDecodeSuspended) { + mStateObj->HandleResumeVideoDecoding(); + } +} + void MediaDecoderStateMachine::BufferedRangeUpdated() { MOZ_ASSERT(OnTaskQueue()); diff --git a/dom/media/MediaDecoderStateMachine.h b/dom/media/MediaDecoderStateMachine.h index 406873b84e3e..a95fb4809512 100644 --- a/dom/media/MediaDecoderStateMachine.h +++ b/dom/media/MediaDecoderStateMachine.h @@ -127,6 +127,12 @@ enum class MediaEventType : int8_t CancelVideoSuspendTimer }; +enum class VideoDecodeMode : uint8_t +{ + Normal, + Suspend +}; + /* 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 @@ -226,6 +232,9 @@ public: size_t SizeOfAudioQueue() const; + // Sets the video decode mode. Used by the suspend-video-decoder feature. + void SetVideoDecodeMode(VideoDecodeMode aMode); + private: class StateObject; class DecodeMetadataState; @@ -307,6 +316,8 @@ private: void ResetDecode(TrackSet aTracks = TrackSet(TrackInfo::kAudioTrack, TrackInfo::kVideoTrack)); + void SetVideoDecodeModeInternal(VideoDecodeMode aMode); + protected: virtual ~MediaDecoderStateMachine(); @@ -676,6 +687,9 @@ private: // Media data resource from the decoder. RefPtr mResource; + // Track the current video decode mode. + VideoDecodeMode mVideoDecodeMode; + // Track the complete & error for audio/video separately MozPromiseRequestHolder mMediaSinkAudioPromise; MozPromiseRequestHolder mMediaSinkVideoPromise;