/work/obj-fuzz/dist/include/MediaMetadataManager.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #if !defined(MediaMetadataManager_h__) |
8 | | #define MediaMetadataManager_h__ |
9 | | |
10 | | #include "mozilla/AbstractThread.h" |
11 | | #include "mozilla/LinkedList.h" |
12 | | |
13 | | #include "nsAutoPtr.h" |
14 | | #include "MediaEventSource.h" |
15 | | #include "TimeUnits.h" |
16 | | #include "VideoUtils.h" |
17 | | |
18 | | namespace mozilla { |
19 | | |
20 | | class TimedMetadata; |
21 | | typedef MediaEventProducerExc<TimedMetadata> TimedMetadataEventProducer; |
22 | | typedef MediaEventSourceExc<TimedMetadata> TimedMetadataEventSource; |
23 | | |
24 | | // A struct that contains the metadata of a media, and the time at which those |
25 | | // metadata should start to be reported. |
26 | | class TimedMetadata : public LinkedListElement<TimedMetadata> { |
27 | | public: |
28 | | TimedMetadata(const media::TimeUnit& aPublishTime, |
29 | | nsAutoPtr<MetadataTags>&& aTags, |
30 | | nsAutoPtr<MediaInfo>&& aInfo) |
31 | | : mPublishTime(aPublishTime) |
32 | | , mTags(std::move(aTags)) |
33 | 0 | , mInfo(std::move(aInfo)) {} |
34 | | |
35 | | // Define our move constructor because we don't want to move the members of |
36 | | // LinkedListElement to change the list. |
37 | | TimedMetadata(TimedMetadata&& aOther) |
38 | | : mPublishTime(aOther.mPublishTime) |
39 | | , mTags(std::move(aOther.mTags)) |
40 | | , mInfo(std::move(aOther.mInfo)) {} |
41 | | |
42 | | // The time, in microseconds, at which those metadata should be available. |
43 | | media::TimeUnit mPublishTime; |
44 | | // The metadata. The ownership is transfered to the element when dispatching to |
45 | | // the main threads. |
46 | | nsAutoPtr<MetadataTags> mTags; |
47 | | // The media info, including the info of audio tracks and video tracks. |
48 | | // The ownership is transfered to MediaDecoder when dispatching to the |
49 | | // main thread. |
50 | | nsAutoPtr<MediaInfo> mInfo; |
51 | | }; |
52 | | |
53 | | // This class encapsulate the logic to give the metadata from the reader to |
54 | | // the content, at the right time. |
55 | | class MediaMetadataManager { |
56 | | public: |
57 | | ~MediaMetadataManager() { |
58 | | TimedMetadata* element; |
59 | | while((element = mMetadataQueue.popFirst()) != nullptr) { |
60 | | delete element; |
61 | | } |
62 | | } |
63 | | |
64 | | // Connect to an event source to receive TimedMetadata events. |
65 | | void Connect(TimedMetadataEventSource& aEvent, AbstractThread* aThread) { |
66 | | mListener = aEvent.Connect( |
67 | | aThread, this, &MediaMetadataManager::OnMetadataQueued); |
68 | | } |
69 | | |
70 | | // Stop receiving TimedMetadata events. |
71 | | void Disconnect() { |
72 | | mListener.Disconnect(); |
73 | | } |
74 | | |
75 | | // Return an event source through which we will send TimedMetadata events |
76 | | // when playback position reaches the publish time. |
77 | | TimedMetadataEventSource& TimedMetadataEvent() { |
78 | | return mTimedMetadataEvent; |
79 | | } |
80 | | |
81 | | void DispatchMetadataIfNeeded(const media::TimeUnit& aCurrentTime) { |
82 | | TimedMetadata* metadata = mMetadataQueue.getFirst(); |
83 | | while (metadata && aCurrentTime >= metadata->mPublishTime) { |
84 | | // Our listener will figure out what to do with TimedMetadata. |
85 | | mTimedMetadataEvent.Notify(std::move(*metadata)); |
86 | | delete mMetadataQueue.popFirst(); |
87 | | metadata = mMetadataQueue.getFirst(); |
88 | | } |
89 | | } |
90 | | |
91 | | protected: |
92 | | void OnMetadataQueued(TimedMetadata&& aMetadata) { |
93 | | mMetadataQueue.insertBack(new TimedMetadata(std::move(aMetadata))); |
94 | | } |
95 | | |
96 | | LinkedList<TimedMetadata> mMetadataQueue; |
97 | | MediaEventListener mListener; |
98 | | TimedMetadataEventProducer mTimedMetadataEvent; |
99 | | }; |
100 | | |
101 | | } // namespace mozilla |
102 | | |
103 | | #endif |