michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #if !defined(MediaMetadataManager_h__) michael@0: #define MediaMetadataManager_h__ michael@0: #include "VideoUtils.h" michael@0: #include "mozilla/LinkedList.h" michael@0: #include "AbstractMediaDecoder.h" michael@0: #include "nsAutoPtr.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: // A struct that contains the metadata of a media, and the time at which those michael@0: // metadata should start to be reported. michael@0: class TimedMetadata : public LinkedListElement { michael@0: public: michael@0: // The time, in microseconds, at which those metadata should be available. michael@0: int64_t mPublishTime; michael@0: // The metadata. The ownership is transfered to the element when dispatching to michael@0: // the main threads. michael@0: nsAutoPtr mTags; michael@0: // The sample rate of this media. michael@0: int mRate; michael@0: // The number of channel of this media. michael@0: int mChannels; michael@0: // True if this media has an audio track. michael@0: bool mHasAudio; michael@0: // True if this media has a video track. michael@0: bool mHasVideo; michael@0: }; michael@0: michael@0: // This class encapsulate the logic to give the metadata from the reader to michael@0: // the content, at the right time. michael@0: class MediaMetadataManager michael@0: { michael@0: public: michael@0: ~MediaMetadataManager() { michael@0: TimedMetadata* element; michael@0: while((element = mMetadataQueue.popFirst()) != nullptr) { michael@0: delete element; michael@0: } michael@0: } michael@0: void QueueMetadata(TimedMetadata* aMetadata) { michael@0: mMetadataQueue.insertBack(aMetadata); michael@0: } michael@0: michael@0: void DispatchMetadataIfNeeded(AbstractMediaDecoder* aDecoder, double aCurrentTime) { michael@0: TimedMetadata* metadata = mMetadataQueue.getFirst(); michael@0: while (metadata && aCurrentTime >= static_cast(metadata->mPublishTime) / USECS_PER_S) { michael@0: nsCOMPtr metadataUpdatedEvent = michael@0: new AudioMetadataEventRunner(aDecoder, michael@0: metadata->mChannels, michael@0: metadata->mRate, michael@0: metadata->mHasAudio, michael@0: metadata->mHasVideo, michael@0: metadata->mTags.forget()); michael@0: NS_DispatchToMainThread(metadataUpdatedEvent, NS_DISPATCH_NORMAL); michael@0: delete mMetadataQueue.popFirst(); michael@0: metadata = mMetadataQueue.getFirst(); michael@0: } michael@0: } michael@0: protected: michael@0: LinkedList mMetadataQueue; michael@0: }; michael@0: } michael@0: michael@0: #endif