|
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 #ifndef AbstractMediaDecoder_h_ |
|
8 #define AbstractMediaDecoder_h_ |
|
9 |
|
10 #include "mozilla/Attributes.h" |
|
11 #include "nsISupports.h" |
|
12 #include "nsDataHashtable.h" |
|
13 #include "nsThreadUtils.h" |
|
14 |
|
15 namespace mozilla |
|
16 { |
|
17 |
|
18 namespace layers |
|
19 { |
|
20 class ImageContainer; |
|
21 } |
|
22 class MediaResource; |
|
23 class ReentrantMonitor; |
|
24 class VideoFrameContainer; |
|
25 class TimedMetadata; |
|
26 class MediaDecoderOwner; |
|
27 |
|
28 typedef nsDataHashtable<nsCStringHashKey, nsCString> MetadataTags; |
|
29 |
|
30 static inline bool IsCurrentThread(nsIThread* aThread) { |
|
31 return NS_GetCurrentThread() == aThread; |
|
32 } |
|
33 |
|
34 /** |
|
35 * The AbstractMediaDecoder class describes the public interface for a media decoder |
|
36 * and is used by the MediaReader classes. |
|
37 */ |
|
38 class AbstractMediaDecoder : public nsISupports |
|
39 { |
|
40 public: |
|
41 // Returns the monitor for other threads to synchronise access to |
|
42 // state. |
|
43 virtual ReentrantMonitor& GetReentrantMonitor() = 0; |
|
44 |
|
45 // Returns true if the decoder is shut down. |
|
46 virtual bool IsShutdown() const = 0; |
|
47 |
|
48 virtual bool OnStateMachineThread() const = 0; |
|
49 |
|
50 virtual bool OnDecodeThread() const = 0; |
|
51 |
|
52 // Get the current MediaResource being used. Its URI will be returned |
|
53 // by currentSrc. Returns what was passed to Load(), if Load() has been called. |
|
54 virtual MediaResource* GetResource() const = 0; |
|
55 |
|
56 // Called by the decode thread to keep track of the number of bytes read |
|
57 // from the resource. |
|
58 virtual void NotifyBytesConsumed(int64_t aBytes, int64_t aOffset) = 0; |
|
59 |
|
60 // Increments the parsed and decoded frame counters by the passed in counts. |
|
61 // Can be called on any thread. |
|
62 virtual void NotifyDecodedFrames(uint32_t aParsed, uint32_t aDecoded) = 0; |
|
63 |
|
64 // Returns the end time of the last sample in the media. Note that a media |
|
65 // can have a non-zero start time, so the end time may not necessarily be |
|
66 // the same as the duration (i.e. duration is (end_time - start_time)). |
|
67 virtual int64_t GetEndMediaTime() const = 0; |
|
68 |
|
69 // Return the duration of the media in microseconds. |
|
70 virtual int64_t GetMediaDuration() = 0; |
|
71 |
|
72 // Set the duration of the media in microseconds. |
|
73 virtual void SetMediaDuration(int64_t aDuration) = 0; |
|
74 |
|
75 // Sets the duration of the media in microseconds. The MediaDecoder |
|
76 // fires a durationchange event to its owner (e.g., an HTML audio |
|
77 // tag). |
|
78 virtual void UpdateEstimatedMediaDuration(int64_t aDuration) = 0; |
|
79 |
|
80 // Set the media as being seekable or not. |
|
81 virtual void SetMediaSeekable(bool aMediaSeekable) = 0; |
|
82 |
|
83 // Set the transport level as being seekable or not. |
|
84 virtual void SetTransportSeekable(bool aTransportSeekable) = 0; |
|
85 |
|
86 virtual VideoFrameContainer* GetVideoFrameContainer() = 0; |
|
87 virtual mozilla::layers::ImageContainer* GetImageContainer() = 0; |
|
88 |
|
89 // Return true if the media layer supports seeking. |
|
90 virtual bool IsTransportSeekable() = 0; |
|
91 |
|
92 // Return true if the transport layer supports seeking. |
|
93 virtual bool IsMediaSeekable() = 0; |
|
94 |
|
95 virtual void MetadataLoaded(int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0; |
|
96 virtual void QueueMetadata(int64_t aTime, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) = 0; |
|
97 |
|
98 // Set the media end time in microseconds |
|
99 virtual void SetMediaEndTime(int64_t aTime) = 0; |
|
100 |
|
101 // Make the decoder state machine update the playback position. Called by |
|
102 // the reader on the decoder thread (Assertions for this checked by |
|
103 // mDecoderStateMachine). This must be called with the decode monitor |
|
104 // held. |
|
105 virtual void UpdatePlaybackPosition(int64_t aTime) = 0; |
|
106 |
|
107 // May be called by the reader to notify this decoder that the metadata from |
|
108 // the media file has been read. Call on the decode thread only. |
|
109 virtual void OnReadMetadataCompleted() = 0; |
|
110 |
|
111 // Returns the owner of this media decoder. The owner should only be used |
|
112 // on the main thread. |
|
113 virtual MediaDecoderOwner* GetOwner() = 0; |
|
114 |
|
115 // May be called by the reader to notify the decoder that the resources |
|
116 // required to begin playback have been acquired. Can be called on any thread. |
|
117 virtual void NotifyWaitingForResourcesStatusChanged() = 0; |
|
118 |
|
119 // Called by Reader if the current audio track can be offloaded |
|
120 virtual void SetCanOffloadAudio(bool aCanOffloadAudio) {} |
|
121 |
|
122 // Called from HTMLMediaElement when owner document activity changes |
|
123 virtual void SetElementVisibility(bool aIsVisible) {} |
|
124 |
|
125 // Stack based class to assist in notifying the frame statistics of |
|
126 // parsed and decoded frames. Use inside video demux & decode functions |
|
127 // to ensure all parsed and decoded frames are reported on all return paths. |
|
128 class AutoNotifyDecoded { |
|
129 public: |
|
130 AutoNotifyDecoded(AbstractMediaDecoder* aDecoder, uint32_t& aParsed, uint32_t& aDecoded) |
|
131 : mDecoder(aDecoder), mParsed(aParsed), mDecoded(aDecoded) {} |
|
132 ~AutoNotifyDecoded() { |
|
133 mDecoder->NotifyDecodedFrames(mParsed, mDecoded); |
|
134 } |
|
135 private: |
|
136 AbstractMediaDecoder* mDecoder; |
|
137 uint32_t& mParsed; |
|
138 uint32_t& mDecoded; |
|
139 }; |
|
140 }; |
|
141 |
|
142 class AudioMetadataEventRunner : public nsRunnable |
|
143 { |
|
144 private: |
|
145 nsRefPtr<AbstractMediaDecoder> mDecoder; |
|
146 public: |
|
147 AudioMetadataEventRunner(AbstractMediaDecoder* aDecoder, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags) |
|
148 : mDecoder(aDecoder), |
|
149 mChannels(aChannels), |
|
150 mRate(aRate), |
|
151 mHasAudio(aHasAudio), |
|
152 mHasVideo(aHasVideo), |
|
153 mTags(aTags) |
|
154 {} |
|
155 |
|
156 NS_IMETHOD Run() MOZ_OVERRIDE |
|
157 { |
|
158 mDecoder->MetadataLoaded(mChannels, mRate, mHasAudio, mHasVideo, mTags); |
|
159 return NS_OK; |
|
160 } |
|
161 |
|
162 int mChannels; |
|
163 int mRate; |
|
164 bool mHasAudio; |
|
165 bool mHasVideo; |
|
166 MetadataTags* mTags; |
|
167 }; |
|
168 |
|
169 |
|
170 } |
|
171 |
|
172 #endif |
|
173 |