/* -*- 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 # include "mozilla/RecursiveMutex.h" # include "mozilla/TaskQueue.h" # include "nsDeque.h" # include "MediaEventSource.h" # include "TimeUnits.h" namespace mozilla { class AudioData; // Thread and type safe wrapper around nsDeque. template class MediaQueueDeallocator : public nsDequeFunctor { virtual void operator()(T* aObject) override { RefPtr releaseMe = dont_AddRef(aObject); } }; template class MediaQueue : private nsDeque { public: MediaQueue() : nsDeque(new MediaQueueDeallocator()), mRecursiveMutex("mediaqueue"), mEndOfStream(false) {} ~MediaQueue() { Reset(); } inline size_t GetSize() const { RecursiveMutexAutoLock lock(mRecursiveMutex); return nsDeque::GetSize(); } inline void Push(T* aItem) { MOZ_DIAGNOSTIC_ASSERT(aItem); Push(do_AddRef(aItem)); } inline void Push(already_AddRefed aItem) { RecursiveMutexAutoLock lock(mRecursiveMutex); T* item = aItem.take(); MOZ_DIAGNOSTIC_ASSERT(item); MOZ_DIAGNOSTIC_ASSERT(item->GetEndTime() >= item->mTime); nsDeque::Push(item); mPushEvent.Notify(RefPtr(item)); // Pushing new data after queue has ended means that the stream is active // again, so we should not mark it as ended. if (mEndOfStream) { mEndOfStream = false; } } inline already_AddRefed PopFront() { RecursiveMutexAutoLock lock(mRecursiveMutex); RefPtr rv = dont_AddRef(nsDeque::PopFront()); if (rv) { MOZ_DIAGNOSTIC_ASSERT(rv->GetEndTime() >= rv->mTime); mPopFrontEvent.Notify(rv); } return rv.forget(); } inline already_AddRefed PopBack() { RecursiveMutexAutoLock lock(mRecursiveMutex); RefPtr rv = dont_AddRef(nsDeque::Pop()); return rv.forget(); } inline RefPtr PeekFront() const { RecursiveMutexAutoLock lock(mRecursiveMutex); return nsDeque::PeekFront(); } inline RefPtr PeekBack() const { RecursiveMutexAutoLock lock(mRecursiveMutex); return nsDeque::Peek(); } void Reset() { RecursiveMutexAutoLock lock(mRecursiveMutex); while (GetSize() > 0) { RefPtr x = dont_AddRef(nsDeque::PopFront()); } mEndOfStream = false; } bool AtEndOfStream() const { RecursiveMutexAutoLock lock(mRecursiveMutex); 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. bool IsFinished() const { RecursiveMutexAutoLock lock(mRecursiveMutex); return mEndOfStream; } // Informs the media queue that it won't be receiving any more items. void Finish() { RecursiveMutexAutoLock lock(mRecursiveMutex); if (!mEndOfStream) { mEndOfStream = true; mFinishEvent.Notify(); } } // Returns the approximate number of microseconds of items in the queue. int64_t Duration() { RecursiveMutexAutoLock lock(mRecursiveMutex); if (GetSize() == 0) { return 0; } T* last = nsDeque::Peek(); T* first = nsDeque::PeekFront(); return (last->GetEndTime() - first->mTime).ToMicroseconds(); } void LockedForEach(nsDequeFunctor& aFunctor) const { RecursiveMutexAutoLock lock(mRecursiveMutex); nsDeque::ForEach(aFunctor); } // Extracts elements from the queue into aResult, in order. // Elements whose start time is before aTime are ignored. void GetElementsAfter(int64_t aTime, nsTArray>* aResult) { RecursiveMutexAutoLock lock(mRecursiveMutex); if (GetSize() == 0) return; size_t i; for (i = GetSize() - 1; i > 0; --i) { T* v = nsDeque::ObjectAt(i); if (v->GetEndTime().ToMicroseconds() < 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) { RefPtr elem = nsDeque::ObjectAt(i); aResult->AppendElement(elem); } } void GetElementsAfter(const media::TimeUnit& aTime, nsTArray>* aResult) { GetElementsAfter(aTime.ToMicroseconds(), aResult); } void GetFirstElements(uint32_t aMaxElements, nsTArray>* aResult) { RecursiveMutexAutoLock lock(mRecursiveMutex); for (size_t i = 0; i < aMaxElements && i < GetSize(); ++i) { *aResult->AppendElement() = nsDeque::ObjectAt(i); } } uint32_t AudioFramesCount() { static_assert(std::is_same_v, "Only usable with MediaQueue"); RecursiveMutexAutoLock lock(mRecursiveMutex); uint32_t frames = 0; for (size_t i = 0; i < GetSize(); ++i) { T* v = nsDeque::ObjectAt(i); frames += v->Frames(); } return frames; } MediaEventSource>& PopFrontEvent() { return mPopFrontEvent; } MediaEventSource>& PushEvent() { return mPushEvent; } MediaEventSource& FinishEvent() { return mFinishEvent; } private: mutable RecursiveMutex mRecursiveMutex; MediaEventProducer> mPopFrontEvent; MediaEventProducer> mPushEvent; MediaEventProducer mFinishEvent; // 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