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/. */
|
|
|
|
|
|
|
|
#if !defined(MediaTimer_h_)
|
|
|
|
# define MediaTimer_h_
|
|
|
|
|
2017-06-20 18:35:26 +03:00
|
|
|
# include "mozilla/AbstractThread.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
# include "mozilla/IntegerPrintfMacros.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
# include "mozilla/Monitor.h"
|
|
|
|
# include "mozilla/MozPromise.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
# include "mozilla/RefPtr.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
# include "mozilla/TimeStamp.h"
|
2018-02-07 17:37:51 +03:00
|
|
|
# include "mozilla/Unused.h"
|
2015-03-09 07:17:48 +03:00
|
|
|
# include "nsITimer.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
# include <queue>
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
extern LazyLogModule gMediaTimerLog;
|
2015-03-31 01:17:04 +03:00
|
|
|
|
|
|
|
# define TIMER_LOG(x, ...) \
|
|
|
|
MOZ_ASSERT(gMediaTimerLog); \
|
2016-12-16 06:16:31 +03:00
|
|
|
MOZ_LOG(gMediaTimerLog, LogLevel::Debug, \
|
|
|
|
("[MediaTimer=%p relative_t=%" PRId64 "]" x, this, \
|
2015-03-31 01:17:04 +03:00
|
|
|
RelativeMicroseconds(TimeStamp::Now()), ##__VA_ARGS__))
|
|
|
|
|
2015-03-09 07:17:48 +03:00
|
|
|
// This promise type is only exclusive because so far there isn't a reason for
|
|
|
|
// it not to be. Feel free to change that.
|
2015-07-16 21:06:49 +03:00
|
|
|
typedef MozPromise<bool, bool, /* IsExclusive = */ true> MediaTimerPromise;
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
// Timers only know how to fire at a given thread, which creates an impedence
|
2015-07-16 21:13:49 +03:00
|
|
|
// mismatch with code that operates with TaskQueues. This class solves
|
2015-07-16 21:06:49 +03:00
|
|
|
// that mismatch with a dedicated (but shared) thread and a nice MozPromise-y
|
2015-03-09 07:17:48 +03:00
|
|
|
// interface.
|
|
|
|
class MediaTimer {
|
|
|
|
public:
|
2018-01-16 11:07:21 +03:00
|
|
|
explicit MediaTimer(bool aFuzzy = false);
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
// We use a release with a custom Destroy().
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release(void);
|
|
|
|
|
2017-12-20 12:01:47 +03:00
|
|
|
RefPtr<MediaTimerPromise> WaitFor(const TimeDuration& aDuration,
|
|
|
|
const char* aCallSite);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaTimerPromise> WaitUntil(const TimeStamp& aTimeStamp,
|
|
|
|
const char* aCallSite);
|
2017-12-20 12:02:31 +03:00
|
|
|
void Cancel(); // Cancel and reject any unresolved promises with false.
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~MediaTimer() { MOZ_ASSERT(OnMediaTimerThread()); }
|
|
|
|
|
|
|
|
void DispatchDestroy(); // Invoked by Release on an arbitrary thread.
|
|
|
|
void Destroy(); // Runs on the timer thread.
|
|
|
|
|
|
|
|
bool OnMediaTimerThread();
|
|
|
|
void ScheduleUpdate();
|
|
|
|
void Update();
|
|
|
|
void UpdateLocked();
|
2018-01-16 11:07:21 +03:00
|
|
|
bool IsExpired(const TimeStamp& aTarget, const TimeStamp& aNow);
|
2017-12-20 12:02:31 +03:00
|
|
|
void Reject();
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
static void TimerCallback(nsITimer* aTimer, void* aClosure);
|
|
|
|
void TimerFired();
|
|
|
|
void ArmTimer(const TimeStamp& aTarget, const TimeStamp& aNow);
|
|
|
|
|
|
|
|
bool TimerIsArmed() { return !mCurrentTimerTarget.IsNull(); }
|
|
|
|
|
|
|
|
void CancelTimerIfArmed() {
|
|
|
|
MOZ_ASSERT(OnMediaTimerThread());
|
|
|
|
if (TimerIsArmed()) {
|
2015-03-31 01:17:04 +03:00
|
|
|
TIMER_LOG("MediaTimer::CancelTimerIfArmed canceling timer");
|
2015-03-09 07:17:48 +03:00
|
|
|
mTimer->Cancel();
|
|
|
|
mCurrentTimerTarget = TimeStamp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Entry {
|
|
|
|
TimeStamp mTimeStamp;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaTimerPromise::Private> mPromise;
|
2015-03-09 07:17:48 +03:00
|
|
|
|
|
|
|
explicit Entry(const TimeStamp& aTimeStamp, const char* aCallSite)
|
|
|
|
: mTimeStamp(aTimeStamp),
|
|
|
|
mPromise(new MediaTimerPromise::Private(aCallSite)) {}
|
|
|
|
|
2015-03-31 01:14:48 +03:00
|
|
|
// Define a < overload that reverses ordering because std::priority_queue
|
|
|
|
// provides access to the largest element, and we want the smallest
|
|
|
|
// (i.e. the soonest).
|
2015-03-09 07:17:48 +03:00
|
|
|
bool operator<(const Entry& aOther) const {
|
2015-03-31 01:14:48 +03:00
|
|
|
return mTimeStamp > aOther.mTimeStamp;
|
2015-03-09 07:17:48 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ThreadSafeAutoRefCnt mRefCnt;
|
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
nsCOMPtr<nsIEventTarget> mThread;
|
|
|
|
std::priority_queue<Entry> mEntries;
|
|
|
|
Monitor mMonitor;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
TimeStamp mCurrentTimerTarget;
|
2015-03-31 01:17:04 +03:00
|
|
|
|
|
|
|
// Timestamps only have relative meaning, so we need a base timestamp for
|
|
|
|
// logging purposes.
|
|
|
|
TimeStamp mCreationTimeStamp;
|
|
|
|
int64_t RelativeMicroseconds(const TimeStamp& aTimeStamp) {
|
|
|
|
return (int64_t)(aTimeStamp - mCreationTimeStamp).ToMicroseconds();
|
|
|
|
}
|
|
|
|
|
2015-03-09 07:17:48 +03:00
|
|
|
bool mUpdateScheduled;
|
2018-01-16 11:07:21 +03:00
|
|
|
const bool mFuzzy;
|
2015-03-09 07:17:48 +03:00
|
|
|
};
|
|
|
|
|
2015-10-13 10:39:01 +03:00
|
|
|
// Class for managing delayed dispatches on target thread.
|
|
|
|
class DelayedScheduler {
|
|
|
|
public:
|
2018-01-16 11:07:21 +03:00
|
|
|
explicit DelayedScheduler(AbstractThread* aTargetThread, bool aFuzzy = false)
|
|
|
|
: mTargetThread(aTargetThread), mMediaTimer(new MediaTimer(aFuzzy)) {
|
2015-10-13 10:39:01 +03:00
|
|
|
MOZ_ASSERT(mTargetThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsScheduled() const { return !mTarget.IsNull(); }
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
MOZ_ASSERT(mTargetThread->IsCurrentThreadIn(),
|
|
|
|
"Must be on target thread to disconnect");
|
|
|
|
if (IsScheduled()) {
|
|
|
|
mRequest.Disconnect();
|
|
|
|
mTarget = TimeStamp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ResolveFunc, typename RejectFunc>
|
|
|
|
void Ensure(mozilla::TimeStamp& aTarget, ResolveFunc&& aResolver,
|
|
|
|
RejectFunc&& aRejector) {
|
|
|
|
MOZ_ASSERT(mTargetThread->IsCurrentThreadIn());
|
|
|
|
if (IsScheduled() && mTarget <= aTarget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Reset();
|
|
|
|
mTarget = aTarget;
|
2017-01-11 11:33:29 +03:00
|
|
|
mMediaTimer->WaitUntil(mTarget, __func__)
|
2018-06-01 19:30:30 +03:00
|
|
|
->Then(mTargetThread, __func__, std::forward<ResolveFunc>(aResolver),
|
|
|
|
std::forward<RejectFunc>(aRejector))
|
2017-01-11 11:33:29 +03:00
|
|
|
->Track(mRequest);
|
2015-10-13 10:39:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompleteRequest() {
|
|
|
|
MOZ_ASSERT(mTargetThread->IsCurrentThreadIn());
|
|
|
|
mRequest.Complete();
|
|
|
|
mTarget = TimeStamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AbstractThread> mTargetThread;
|
|
|
|
RefPtr<MediaTimer> mMediaTimer;
|
2015-10-13 10:39:01 +03:00
|
|
|
MozPromiseRequestHolder<mozilla::MediaTimerPromise> mRequest;
|
|
|
|
TimeStamp mTarget;
|
|
|
|
};
|
|
|
|
|
2015-03-09 07:17:48 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|