2015-03-09 07:17:48 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "MediaTimer.h"
|
|
|
|
|
2015-03-31 01:17:04 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-08-03 21:44:10 +03:00
|
|
|
#include "mozilla/SharedThreadPool.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include <math.h>
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(MediaTimer)
|
|
|
|
NS_IMPL_RELEASE_WITH_DESTROY(MediaTimer, DispatchDestroy())
|
|
|
|
|
2018-01-16 11:07:21 +03:00
|
|
|
MediaTimer::MediaTimer(bool aFuzzy)
|
2015-03-09 07:17:48 +03:00
|
|
|
: mMonitor("MediaTimer Monitor")
|
2017-10-16 09:15:40 +03:00
|
|
|
, mTimer(NS_NewTimer())
|
2015-03-31 01:17:04 +03:00
|
|
|
, mCreationTimeStamp(TimeStamp::Now())
|
2015-03-09 07:17:48 +03:00
|
|
|
, mUpdateScheduled(false)
|
2018-01-16 11:07:21 +03:00
|
|
|
, mFuzzy(aFuzzy)
|
2015-03-09 07:17:48 +03:00
|
|
|
{
|
2015-03-31 01:17:04 +03:00
|
|
|
TIMER_LOG("MediaTimer::MediaTimer");
|
|
|
|
|
2015-03-09 07:17:48 +03:00
|
|
|
// Use the SharedThreadPool to create an nsIThreadPool with a maximum of one
|
|
|
|
// thread, which is equivalent to an nsIThread for our purposes.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedThreadPool> threadPool(
|
2015-03-09 07:17:48 +03:00
|
|
|
SharedThreadPool::Get(NS_LITERAL_CSTRING("MediaTimer"), 1));
|
|
|
|
mThread = threadPool.get();
|
|
|
|
mTimer->SetTarget(mThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTimer::DispatchDestroy()
|
|
|
|
{
|
2015-06-25 23:20:04 +03:00
|
|
|
// Hold a strong reference to the thread so that it doesn't get deleted in
|
|
|
|
// Destroy(), which may run completely before the stack if Dispatch() begins
|
|
|
|
// to unwind.
|
|
|
|
nsCOMPtr<nsIEventTarget> thread = mThread;
|
2017-06-12 22:34:10 +03:00
|
|
|
nsresult rv =
|
|
|
|
thread->Dispatch(NewNonOwningRunnableMethod(
|
|
|
|
"MediaTimer::Destroy", this, &MediaTimer::Destroy),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2015-03-09 07:17:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-12-21 07:12:42 +03:00
|
|
|
Unused << rv;
|
2015-03-09 07:17:48 +03:00
|
|
|
(void) rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTimer::Destroy()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnMediaTimerThread());
|
2015-03-31 01:17:04 +03:00
|
|
|
TIMER_LOG("MediaTimer::Destroy");
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
// Reject any outstanding entries. There's no need to acquire the monitor
|
|
|
|
// here, because we're on the timer thread and all other references to us
|
|
|
|
// must be gone.
|
|
|
|
while (!mEntries.empty()) {
|
|
|
|
mEntries.top().mPromise->Reject(false, __func__);
|
|
|
|
mEntries.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel the timer if necessary.
|
|
|
|
CancelTimerIfArmed();
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaTimer::OnMediaTimerThread()
|
|
|
|
{
|
|
|
|
bool rv = false;
|
|
|
|
mThread->IsOnCurrentThread(&rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:01:47 +03:00
|
|
|
RefPtr<MediaTimerPromise>
|
|
|
|
MediaTimer::WaitFor(const TimeDuration& aDuration, const char* aCallSite)
|
|
|
|
{
|
|
|
|
return WaitUntil(TimeStamp::Now() + aDuration, aCallSite);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaTimerPromise>
|
2015-03-09 07:17:48 +03:00
|
|
|
MediaTimer::WaitUntil(const TimeStamp& aTimeStamp, const char* aCallSite)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-12-16 06:16:31 +03:00
|
|
|
TIMER_LOG("MediaTimer::WaitUntil %" PRId64, RelativeMicroseconds(aTimeStamp));
|
2015-03-09 07:17:48 +03:00
|
|
|
Entry e(aTimeStamp, aCallSite);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaTimerPromise> p = e.mPromise.get();
|
2015-03-09 07:17:48 +03:00
|
|
|
mEntries.push(e);
|
|
|
|
ScheduleUpdate();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTimer::ScheduleUpdate()
|
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
if (mUpdateScheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mUpdateScheduled = true;
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
nsresult rv = mThread->Dispatch(
|
|
|
|
NewRunnableMethod("MediaTimer::Update", this, &MediaTimer::Update),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2015-03-09 07:17:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-12-21 07:12:42 +03:00
|
|
|
Unused << rv;
|
2015-03-09 07:17:48 +03:00
|
|
|
(void) rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTimer::Update()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
UpdateLocked();
|
|
|
|
}
|
|
|
|
|
2018-01-16 11:07:21 +03:00
|
|
|
bool
|
|
|
|
MediaTimer::IsExpired(const TimeStamp& aTarget, const TimeStamp& aNow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnMediaTimerThread());
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
// Treat this timer as expired in fuzzy mode even if it is fired
|
|
|
|
// slightly (< 1ms) before the schedule target. So we don't need to schedule a
|
|
|
|
// timer with very small timeout again when the client doesn't need a high-res
|
|
|
|
// timer.
|
|
|
|
TimeStamp t = mFuzzy ? aTarget - TimeDuration::FromMilliseconds(1) : aTarget;
|
|
|
|
return t <= aNow;
|
|
|
|
}
|
|
|
|
|
2015-03-09 07:17:48 +03:00
|
|
|
void
|
|
|
|
MediaTimer::UpdateLocked()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnMediaTimerThread());
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
mUpdateScheduled = false;
|
|
|
|
|
2015-03-31 01:17:04 +03:00
|
|
|
TIMER_LOG("MediaTimer::UpdateLocked");
|
|
|
|
|
2015-03-09 07:17:48 +03:00
|
|
|
// Resolve all the promises whose time is up.
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
2018-01-16 11:07:21 +03:00
|
|
|
while (!mEntries.empty() && IsExpired(mEntries.top().mTimeStamp, now)) {
|
2015-03-09 07:17:48 +03:00
|
|
|
mEntries.top().mPromise->Resolve(true, __func__);
|
2015-03-31 01:14:48 +03:00
|
|
|
DebugOnly<TimeStamp> poppedTimeStamp = mEntries.top().mTimeStamp;
|
2015-03-09 07:17:48 +03:00
|
|
|
mEntries.pop();
|
2015-03-31 01:14:48 +03:00
|
|
|
MOZ_ASSERT_IF(!mEntries.empty(), *&poppedTimeStamp <= mEntries.top().mTimeStamp);
|
2015-03-09 07:17:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we've got no more entries, cancel any pending timer and bail out.
|
|
|
|
if (mEntries.empty()) {
|
|
|
|
CancelTimerIfArmed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've got more entries - (re)arm the timer for the soonest one.
|
|
|
|
if (!TimerIsArmed() || mEntries.top().mTimeStamp < mCurrentTimerTarget) {
|
|
|
|
CancelTimerIfArmed();
|
|
|
|
ArmTimer(mEntries.top().mTimeStamp, now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use a callback function, rather than a callback method, to ensure that
|
|
|
|
* the nsITimer does not artifically keep the refcount of the MediaTimer above
|
|
|
|
* zero. When the MediaTimer is destroyed, it safely cancels the nsITimer so that
|
|
|
|
* we never fire against a dangling closure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
MediaTimer::TimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
static_cast<MediaTimer*>(aClosure)->TimerFired();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTimer::TimerFired()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
MOZ_ASSERT(OnMediaTimerThread());
|
|
|
|
mCurrentTimerTarget = TimeStamp();
|
|
|
|
UpdateLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTimer::ArmTimer(const TimeStamp& aTarget, const TimeStamp& aNow)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!TimerIsArmed());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aTarget > aNow);
|
|
|
|
|
|
|
|
// XPCOM timer resolution is in milliseconds. It's important to never resolve
|
|
|
|
// a timer when mTarget might compare < now (even if very close), so round up.
|
|
|
|
unsigned long delay = std::ceil((aTarget - aNow).ToMilliseconds());
|
2015-03-31 01:17:04 +03:00
|
|
|
TIMER_LOG("MediaTimer::ArmTimer delay=%lu", delay);
|
2015-03-09 07:17:48 +03:00
|
|
|
mCurrentTimerTarget = aTarget;
|
2015-09-10 10:50:51 +03:00
|
|
|
nsresult rv = mTimer->InitWithNamedFuncCallback(&TimerCallback, this, delay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"MediaTimer::TimerCallback");
|
2015-03-09 07:17:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-12-21 07:12:42 +03:00
|
|
|
Unused << rv;
|
2015-03-09 07:17:48 +03:00
|
|
|
(void) rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|