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
This commit is contained in:
JW Wang 2016-08-01 14:39:39 +08:00
Родитель 91b64119c1
Коммит cff471d7a8
2 изменённых файлов: 30 добавлений и 2 удалений

Просмотреть файл

@ -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<ResourceCallback*>(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

Просмотреть файл

@ -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<nsITimer> mTimer;
bool mTimerArmed = false;
};
typedef MozPromise<SeekResolveValue, bool /* aIgnored */, /* IsExclusive = */ true> SeekPromise;