2014-02-04 01:49:21 +00: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(MediaQueue_h_)
|
|
|
|
#define MediaQueue_h_
|
|
|
|
|
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2015-07-16 18:52:43 +00:00
|
|
|
#include "mozilla/TaskQueue.h"
|
|
|
|
|
|
|
|
#include "nsDeque.h"
|
2015-08-06 02:14:15 +00:00
|
|
|
#include "MediaEventSource.h"
|
2014-02-04 01:49:21 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Thread and type safe wrapper around nsDeque.
|
|
|
|
template <class T>
|
|
|
|
class MediaQueueDeallocator : public nsDequeFunctor {
|
2014-08-14 09:24:00 +00:00
|
|
|
virtual void* operator() (void* aObject) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<T> releaseMe = dont_AddRef(static_cast<T*>(aObject));
|
2014-02-04 01:49:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-23 06:12:00 +00:00
|
|
|
template <class T>
|
|
|
|
class MediaQueue : private nsDeque {
|
|
|
|
public:
|
|
|
|
MediaQueue()
|
|
|
|
: nsDeque(new MediaQueueDeallocator<T>()),
|
|
|
|
mReentrantMonitor("mediaqueue"),
|
|
|
|
mEndOfStream(false)
|
|
|
|
{}
|
2014-02-04 01:49:21 +00:00
|
|
|
|
|
|
|
~MediaQueue() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2016-04-25 02:13:55 +00:00
|
|
|
inline size_t GetSize() const {
|
2014-02-04 01:49:21 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
return nsDeque::GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Push(T* aItem) {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2014-06-18 05:07:02 +00:00
|
|
|
MOZ_ASSERT(aItem);
|
2014-11-19 21:01:10 +00:00
|
|
|
NS_ADDREF(aItem);
|
2014-02-04 01:49:21 +00:00
|
|
|
nsDeque::Push(aItem);
|
2015-11-25 07:15:05 +00:00
|
|
|
mPushEvent.Notify(RefPtr<T>(aItem));
|
2014-02-04 01:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void PushFront(T* aItem) {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2014-06-18 05:07:02 +00:00
|
|
|
MOZ_ASSERT(aItem);
|
2014-11-19 21:01:10 +00:00
|
|
|
NS_ADDREF(aItem);
|
2014-02-04 01:49:21 +00:00
|
|
|
nsDeque::PushFront(aItem);
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:01:10 +00:00
|
|
|
inline already_AddRefed<T> PopFront() {
|
2014-02-04 01:49:21 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<T> rv = dont_AddRef(static_cast<T*>(nsDeque::PopFront()));
|
2014-03-11 03:44:09 +00:00
|
|
|
if (rv) {
|
2015-08-06 02:14:15 +00:00
|
|
|
mPopEvent.Notify(rv);
|
2014-03-11 03:44:09 +00:00
|
|
|
}
|
2014-11-19 21:01:10 +00:00
|
|
|
return rv.forget();
|
2014-02-04 01:49:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 02:13:55 +00:00
|
|
|
inline RefPtr<T> Peek() const {
|
2014-02-04 01:49:21 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
return static_cast<T*>(nsDeque::Peek());
|
|
|
|
}
|
|
|
|
|
2016-04-25 02:13:55 +00:00
|
|
|
inline RefPtr<T> PeekFront() const {
|
2014-02-04 01:49:21 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
return static_cast<T*>(nsDeque::PeekFront());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
while (GetSize() > 0) {
|
2016-04-19 15:04:02 +00:00
|
|
|
RefPtr<T> x = dont_AddRef(static_cast<T*>(nsDeque::PopFront()));
|
2014-02-04 01:49:21 +00:00
|
|
|
}
|
|
|
|
mEndOfStream = false;
|
|
|
|
}
|
|
|
|
|
2016-04-25 02:13:55 +00:00
|
|
|
bool AtEndOfStream() const {
|
2014-02-04 01:49:21 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
return GetSize() == 0 && mEndOfStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the media queue has had its last item added to it.
|
|
|
|
// This happens when the media stream has been completely decoded. Note this
|
|
|
|
// does not mean that the corresponding stream has finished playback.
|
2016-04-25 02:13:55 +00:00
|
|
|
bool IsFinished() const {
|
2014-02-04 01:49:21 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
return mEndOfStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Informs the media queue that it won't be receiving any more items.
|
|
|
|
void Finish() {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
mEndOfStream = true;
|
2015-08-06 02:14:15 +00:00
|
|
|
mFinishEvent.Notify();
|
2014-02-04 01:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the approximate number of microseconds of items in the queue.
|
|
|
|
int64_t Duration() {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2014-12-24 10:10:24 +00:00
|
|
|
if (GetSize() == 0) {
|
2014-02-04 01:49:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-04-25 06:36:07 +00:00
|
|
|
T* last = static_cast<T*>(nsDeque::Peek());
|
|
|
|
T* first = static_cast<T*>(nsDeque::PeekFront());
|
2014-12-24 10:10:24 +00:00
|
|
|
return last->GetEndTime() - first->mTime;
|
2014-02-04 01:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LockedForEach(nsDequeFunctor& aFunctor) const {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
ForEach(aFunctor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extracts elements from the queue into aResult, in order.
|
|
|
|
// Elements whose start time is before aTime are ignored.
|
2015-10-18 05:24:48 +00:00
|
|
|
void GetElementsAfter(int64_t aTime, nsTArray<RefPtr<T>>* aResult) {
|
2014-02-04 01:49:21 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2015-11-15 13:48:08 +00:00
|
|
|
if (GetSize() == 0)
|
2014-02-04 01:49:21 +00:00
|
|
|
return;
|
2015-11-15 13:48:08 +00:00
|
|
|
size_t i;
|
2014-02-04 01:49:21 +00:00
|
|
|
for (i = GetSize() - 1; i > 0; --i) {
|
|
|
|
T* v = static_cast<T*>(ObjectAt(i));
|
|
|
|
if (v->GetEndTime() < aTime)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Elements less than i have a end time before aTime. It's also possible
|
|
|
|
// that the element at i has a end time before aTime, but that's OK.
|
|
|
|
for (; i < GetSize(); ++i) {
|
2015-11-15 13:48:08 +00:00
|
|
|
RefPtr<T> elem = static_cast<T*>(ObjectAt(static_cast<size_t>(i)));
|
2014-11-19 21:01:10 +00:00
|
|
|
aResult->AppendElement(elem);
|
2014-02-04 01:49:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
void GetFirstElements(uint32_t aMaxElements, nsTArray<RefPtr<T>>* aResult) {
|
2015-03-30 08:30:03 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2015-11-15 13:48:08 +00:00
|
|
|
for (size_t i = 0; i < aMaxElements && i < GetSize(); ++i) {
|
2015-03-30 08:30:03 +00:00
|
|
|
*aResult->AppendElement() = static_cast<T*>(ObjectAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-04 01:49:21 +00:00
|
|
|
uint32_t FrameCount() {
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
uint32_t frames = 0;
|
2015-11-15 13:48:08 +00:00
|
|
|
for (size_t i = 0; i < GetSize(); ++i) {
|
2014-02-04 01:49:21 +00:00
|
|
|
T* v = static_cast<T*>(ObjectAt(i));
|
|
|
|
frames += v->mFrames;
|
|
|
|
}
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
MediaEventSource<RefPtr<T>>& PopEvent() {
|
2015-08-06 02:14:15 +00:00
|
|
|
return mPopEvent;
|
2014-03-11 03:44:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:05 +00:00
|
|
|
MediaEventSource<RefPtr<T>>& PushEvent() {
|
2015-08-06 02:14:15 +00:00
|
|
|
return mPushEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaEventSource<void>& FinishEvent() {
|
|
|
|
return mFinishEvent;
|
2014-03-11 03:44:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 01:49:21 +00:00
|
|
|
private:
|
|
|
|
mutable ReentrantMonitor mReentrantMonitor;
|
2015-10-18 05:24:48 +00:00
|
|
|
MediaEventProducer<RefPtr<T>> mPopEvent;
|
2015-11-25 07:15:05 +00:00
|
|
|
MediaEventProducer<RefPtr<T>> mPushEvent;
|
2015-08-06 02:14:15 +00:00
|
|
|
MediaEventProducer<void> mFinishEvent;
|
2014-02-04 01:49:21 +00:00
|
|
|
// True when we've decoded the last frame of data in the
|
|
|
|
// bitstream for which we're queueing frame data.
|
|
|
|
bool mEndOfStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|