Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / content / media / MediaQueue.h
blob88e41985d7c12bdc0f4d6ecadd1891e9049b736d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #if !defined(MediaQueue_h_)
7 #define MediaQueue_h_
9 #include "nsDeque.h"
10 #include "nsTArray.h"
11 #include "mozilla/ReentrantMonitor.h"
12 #include "mozilla/RefPtr.h"
13 #include "MediaTaskQueue.h"
15 namespace mozilla {
17 // Thread and type safe wrapper around nsDeque.
18 template <class T>
19 class MediaQueueDeallocator : public nsDequeFunctor {
20 virtual void* operator() (void* aObject) {
21 delete static_cast<T*>(aObject);
22 return nullptr;
26 template <class T> class MediaQueue : private nsDeque {
27 public:
29 MediaQueue()
30 : nsDeque(new MediaQueueDeallocator<T>()),
31 mReentrantMonitor("mediaqueue"),
32 mEndOfStream(false)
35 ~MediaQueue() {
36 Reset();
39 inline int32_t GetSize() {
40 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
41 return nsDeque::GetSize();
44 inline void Push(T* aItem) {
45 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
46 MOZ_ASSERT(aItem);
47 nsDeque::Push(aItem);
50 inline void PushFront(T* aItem) {
51 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
52 MOZ_ASSERT(aItem);
53 nsDeque::PushFront(aItem);
56 inline T* PopFront() {
57 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
58 T* rv = static_cast<T*>(nsDeque::PopFront());
59 if (rv) {
60 NotifyPopListeners();
62 return rv;
65 inline T* Peek() {
66 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
67 return static_cast<T*>(nsDeque::Peek());
70 inline T* PeekFront() {
71 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
72 return static_cast<T*>(nsDeque::PeekFront());
75 inline void Empty() {
76 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
77 nsDeque::Empty();
80 void Reset() {
81 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
82 while (GetSize() > 0) {
83 T* x = PopFront();
84 delete x;
86 mEndOfStream = false;
89 bool AtEndOfStream() {
90 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
91 return GetSize() == 0 && mEndOfStream;
94 // Returns true if the media queue has had its last item added to it.
95 // This happens when the media stream has been completely decoded. Note this
96 // does not mean that the corresponding stream has finished playback.
97 bool IsFinished() {
98 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
99 return mEndOfStream;
102 // Informs the media queue that it won't be receiving any more items.
103 void Finish() {
104 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
105 mEndOfStream = true;
108 // Returns the approximate number of microseconds of items in the queue.
109 int64_t Duration() {
110 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
111 if (GetSize() < 2) {
112 return 0;
114 T* last = Peek();
115 T* first = PeekFront();
116 return last->mTime - first->mTime;
119 void LockedForEach(nsDequeFunctor& aFunctor) const {
120 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
121 ForEach(aFunctor);
124 // Extracts elements from the queue into aResult, in order.
125 // Elements whose start time is before aTime are ignored.
126 void GetElementsAfter(int64_t aTime, nsTArray<T*>* aResult) {
127 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
128 if (!GetSize())
129 return;
130 int32_t i;
131 for (i = GetSize() - 1; i > 0; --i) {
132 T* v = static_cast<T*>(ObjectAt(i));
133 if (v->GetEndTime() < aTime)
134 break;
136 // Elements less than i have a end time before aTime. It's also possible
137 // that the element at i has a end time before aTime, but that's OK.
138 for (; i < GetSize(); ++i) {
139 aResult->AppendElement(static_cast<T*>(ObjectAt(i)));
143 uint32_t FrameCount() {
144 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
145 uint32_t frames = 0;
146 for (int32_t i = 0; i < GetSize(); ++i) {
147 T* v = static_cast<T*>(ObjectAt(i));
148 frames += v->mFrames;
150 return frames;
153 void ClearListeners() {
154 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
155 mPopListeners.Clear();
158 void AddPopListener(nsIRunnable* aRunnable, MediaTaskQueue* aTaskQueue) {
159 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
160 mPopListeners.AppendElement(Listener(aRunnable, aTaskQueue));
163 private:
164 mutable ReentrantMonitor mReentrantMonitor;
166 struct Listener {
167 Listener(nsIRunnable* aRunnable, MediaTaskQueue* aTaskQueue)
168 : mRunnable(aRunnable)
169 , mTarget(aTaskQueue)
172 Listener(const Listener& aOther)
173 : mRunnable(aOther.mRunnable)
174 , mTarget(aOther.mTarget)
177 RefPtr<nsIRunnable> mRunnable;
178 RefPtr<MediaTaskQueue> mTarget;
181 nsTArray<Listener> mPopListeners;
183 void NotifyPopListeners() {
184 for (uint32_t i = 0; i < mPopListeners.Length(); i++) {
185 Listener& l = mPopListeners[i];
186 l.mTarget->Dispatch(l.mRunnable);
190 // True when we've decoded the last frame of data in the
191 // bitstream for which we're queueing frame data.
192 bool mEndOfStream;
195 } // namespace mozilla
197 #endif