From cff471d7a8b03b1b02dfdebe732060df348fbf37 Mon Sep 17 00:00:00 2001 From: JW Wang Date: Mon, 1 Aug 2016 14:39:39 +0800 Subject: [PATCH] Bug 1283724 - throttle notifications from stochastic network activity to save computation of buffer ranges. r=jya MozReview-Commit-ID: BRBv2Flqu3u --HG-- extra : rebase_source : fe28566c79f2f31a5054e4cda35d96c938985299 --- dom/media/MediaDecoder.cpp | 24 ++++++++++++++++++++++-- dom/media/MediaDecoder.h | 8 ++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/dom/media/MediaDecoder.cpp b/dom/media/MediaDecoder.cpp index 4ff04bf8c366..0e04afa4b35a 100644 --- a/dom/media/MediaDecoder.cpp +++ b/dom/media/MediaDecoder.cpp @@ -145,6 +145,7 @@ MediaDecoder::ResourceCallback::Connect(MediaDecoder* aDecoder) { MOZ_ASSERT(NS_IsMainThread()); mDecoder = aDecoder; + mTimer = do_CreateInstance("@mozilla.org/timer;1"); } void @@ -152,6 +153,8 @@ MediaDecoder::ResourceCallback::Disconnect() { MOZ_ASSERT(NS_IsMainThread()); mDecoder = nullptr; + mTimer->Cancel(); + mTimer = nullptr; } MediaDecoderOwner* @@ -216,13 +219,30 @@ MediaDecoder::ResourceCallback::NotifyDecodeError() AbstractThread::MainThread()->Dispatch(r.forget()); } +/* static */ void +MediaDecoder::ResourceCallback::TimerCallback(nsITimer* aTimer, void* aClosure) +{ + MOZ_ASSERT(NS_IsMainThread()); + ResourceCallback* thiz = static_cast(aClosure); + MOZ_ASSERT(thiz->mDecoder); + thiz->mDecoder->NotifyDataArrived(); + thiz->mTimerArmed = false; +} + void MediaDecoder::ResourceCallback::NotifyDataArrived() { MOZ_ASSERT(NS_IsMainThread()); - if (mDecoder) { - mDecoder->NotifyDataArrived(); + if (!mDecoder || mTimerArmed) { + return; } + // In situations where these notifications come from stochastic network + // activity, we can save significant computation by throttling the + // calls to MediaDecoder::NotifyDataArrived() which will update the buffer + // ranges of the reader. + mTimerArmed = true; + mTimer->InitWithFuncCallback( + TimerCallback, this, sDelay, nsITimer::TYPE_ONE_SHOT); } void diff --git a/dom/media/MediaDecoder.h b/dom/media/MediaDecoder.h index cc1c1d5ed91a..9e5d1fe2b57d 100644 --- a/dom/media/MediaDecoder.h +++ b/dom/media/MediaDecoder.h @@ -70,6 +70,10 @@ public: // Used to register with MediaResource to receive notifications which will // be forwarded to MediaDecoder. class ResourceCallback : public MediaResourceCallback { + // Throttle calls to MediaDecoder::NotifyDataArrived() + // to be at most once per 500ms. + static const uint32_t sDelay = 500; + public: // Start to receive notifications from ResourceCallback. void Connect(MediaDecoder* aDecoder); @@ -92,8 +96,12 @@ public: void NotifySuspendedStatusChanged() override; void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset) override; + static void TimerCallback(nsITimer* aTimer, void* aClosure); + // The decoder to send notifications. Main-thread only. MediaDecoder* mDecoder = nullptr; + nsCOMPtr mTimer; + bool mTimerArmed = false; }; typedef MozPromise SeekPromise;