1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/MediaMetadataManager.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#if !defined(MediaMetadataManager_h__) 1.11 +#define MediaMetadataManager_h__ 1.12 +#include "VideoUtils.h" 1.13 +#include "mozilla/LinkedList.h" 1.14 +#include "AbstractMediaDecoder.h" 1.15 +#include "nsAutoPtr.h" 1.16 + 1.17 +namespace mozilla { 1.18 + 1.19 + // A struct that contains the metadata of a media, and the time at which those 1.20 + // metadata should start to be reported. 1.21 + class TimedMetadata : public LinkedListElement<TimedMetadata> { 1.22 + public: 1.23 + // The time, in microseconds, at which those metadata should be available. 1.24 + int64_t mPublishTime; 1.25 + // The metadata. The ownership is transfered to the element when dispatching to 1.26 + // the main threads. 1.27 + nsAutoPtr<MetadataTags> mTags; 1.28 + // The sample rate of this media. 1.29 + int mRate; 1.30 + // The number of channel of this media. 1.31 + int mChannels; 1.32 + // True if this media has an audio track. 1.33 + bool mHasAudio; 1.34 + // True if this media has a video track. 1.35 + bool mHasVideo; 1.36 + }; 1.37 + 1.38 + // This class encapsulate the logic to give the metadata from the reader to 1.39 + // the content, at the right time. 1.40 + class MediaMetadataManager 1.41 + { 1.42 + public: 1.43 + ~MediaMetadataManager() { 1.44 + TimedMetadata* element; 1.45 + while((element = mMetadataQueue.popFirst()) != nullptr) { 1.46 + delete element; 1.47 + } 1.48 + } 1.49 + void QueueMetadata(TimedMetadata* aMetadata) { 1.50 + mMetadataQueue.insertBack(aMetadata); 1.51 + } 1.52 + 1.53 + void DispatchMetadataIfNeeded(AbstractMediaDecoder* aDecoder, double aCurrentTime) { 1.54 + TimedMetadata* metadata = mMetadataQueue.getFirst(); 1.55 + while (metadata && aCurrentTime >= static_cast<double>(metadata->mPublishTime) / USECS_PER_S) { 1.56 + nsCOMPtr<nsIRunnable> metadataUpdatedEvent = 1.57 + new AudioMetadataEventRunner(aDecoder, 1.58 + metadata->mChannels, 1.59 + metadata->mRate, 1.60 + metadata->mHasAudio, 1.61 + metadata->mHasVideo, 1.62 + metadata->mTags.forget()); 1.63 + NS_DispatchToMainThread(metadataUpdatedEvent, NS_DISPATCH_NORMAL); 1.64 + delete mMetadataQueue.popFirst(); 1.65 + metadata = mMetadataQueue.getFirst(); 1.66 + } 1.67 + } 1.68 + protected: 1.69 + LinkedList<TimedMetadata> mMetadataQueue; 1.70 + }; 1.71 +} 1.72 + 1.73 +#endif