content/media/MediaQueue.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial