Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
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/. */
7 #if !defined(MediaMetadataManager_h__)
8 #define MediaMetadataManager_h__
9 #include "VideoUtils.h"
10 #include "mozilla/LinkedList.h"
11 #include "AbstractMediaDecoder.h"
12 #include "nsAutoPtr.h"
14 namespace mozilla {
16 // A struct that contains the metadata of a media, and the time at which those
17 // metadata should start to be reported.
18 class TimedMetadata : public LinkedListElement<TimedMetadata> {
19 public:
20 // The time, in microseconds, at which those metadata should be available.
21 int64_t mPublishTime;
22 // The metadata. The ownership is transfered to the element when dispatching to
23 // the main threads.
24 nsAutoPtr<MetadataTags> mTags;
25 // The sample rate of this media.
26 int mRate;
27 // The number of channel of this media.
28 int mChannels;
29 // True if this media has an audio track.
30 bool mHasAudio;
31 // True if this media has a video track.
32 bool mHasVideo;
33 };
35 // This class encapsulate the logic to give the metadata from the reader to
36 // the content, at the right time.
37 class MediaMetadataManager
38 {
39 public:
40 ~MediaMetadataManager() {
41 TimedMetadata* element;
42 while((element = mMetadataQueue.popFirst()) != nullptr) {
43 delete element;
44 }
45 }
46 void QueueMetadata(TimedMetadata* aMetadata) {
47 mMetadataQueue.insertBack(aMetadata);
48 }
50 void DispatchMetadataIfNeeded(AbstractMediaDecoder* aDecoder, double aCurrentTime) {
51 TimedMetadata* metadata = mMetadataQueue.getFirst();
52 while (metadata && aCurrentTime >= static_cast<double>(metadata->mPublishTime) / USECS_PER_S) {
53 nsCOMPtr<nsIRunnable> metadataUpdatedEvent =
54 new AudioMetadataEventRunner(aDecoder,
55 metadata->mChannels,
56 metadata->mRate,
57 metadata->mHasAudio,
58 metadata->mHasVideo,
59 metadata->mTags.forget());
60 NS_DispatchToMainThread(metadataUpdatedEvent, NS_DISPATCH_NORMAL);
61 delete mMetadataQueue.popFirst();
62 metadata = mMetadataQueue.getFirst();
63 }
64 }
65 protected:
66 LinkedList<TimedMetadata> mMetadataQueue;
67 };
68 }
70 #endif