OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | |
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | |
7 | |
8 #include "base/android/scoped_java_ref.h" | |
9 #include "base/callback.h" | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/single_thread_task_runner.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "base/threading/thread.h" | |
16 #include "base/time/time.h" | |
17 #include "media/base/android/access_unit_queue.h" | |
18 #include "media/base/android/demuxer_stream_player_params.h" | |
19 | |
20 namespace media { | |
21 | |
22 class MediaCodecBridge; | |
23 | |
24 // The decoder for MediaCodecPlayer. | |
25 // This class accepts the incoming data into AccessUnitQueue and works with | |
26 // MediaCodecBridge for decoding and rendering the frames. The MediaCodecPlayer | |
27 // has two decoder objects: audio and video. | |
28 // | |
29 // The decoder works on two threads. The data from demuxer comes on Media | |
30 // thread. The commands from MediaCodecPlayer, such as Prefetch, Start, | |
31 // RequestToStop also come on the Media thread. The operations with MediaCodec | |
32 // buffers and rendering happen on a separate thread called Decoder thread. | |
33 // This class creates, starts and stops it as necessary. | |
34 // | |
35 // Decoder's internal state machine goes through the following states: | |
36 // | |
37 // [ Stopped ] <------------------- (any state except Error) | |
38 // | | | | |
39 // | Prefetch |--- internal ------| | |
40 // v | transition v | |
41 // [ Prefetching ] | [ Error ] | |
42 // | | | |
43 // | internal transition | | |
44 // v | Error recovery: | |
45 // [ Prefetched ] | | |
46 // | | (any state including Error) | |
47 // | Start | | | |
48 // v | | ReleaseDecoderResources | |
49 // [ Running ] | v | |
50 // | | [ Stopped ] | |
51 // | RequestToStop | | |
52 // v | | |
53 // [ Stopping ] ------------------- | |
54 // | |
55 // | |
56 // [ Stopped ] -------------------- | |
57 // ^ | | |
58 // | Flush | | |
59 // --------------------------- | |
60 | |
61 class MediaCodecDecoder { | |
62 public: | |
63 // The result of MediaCodec configuration, used by MediaCodecPlayer. | |
64 enum ConfigStatus { | |
65 CONFIG_FAILURE = 0, | |
66 CONFIG_OK, | |
67 CONFIG_KEY_FRAME_REQUIRED, | |
68 }; | |
69 | |
70 // The decoder reports current playback time to the MediaCodecPlayer. | |
71 // For audio, the parameters designate the beginning and end of a time | |
72 // interval. The beginning is the estimated time that is playing right now. | |
73 // The end is the playback time of the last buffered data. During normal | |
74 // playback the subsequent intervals overlap. | |
75 // For video both values are PTS of the corresponding frame, i.e. the interval | |
76 // has zero width. | |
77 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)> | |
78 SetTimeCallback; | |
79 | |
80 // MediaCodecDecoder constructor. | |
81 // Parameters: | |
82 // media_task_runner: | |
83 // A task runner for the controlling thread. All public methods should be | |
84 // called on this thread, and callbacks are delivered on this thread. | |
85 // The MediaCodecPlayer uses a dedicated (Media) thread for this. | |
86 // request_data_cb: | |
87 // Called periodically as the amount of internally stored data decreased. | |
88 // The receiver should call OnDemuxerDataAvailable() with more data. | |
89 // starvation_cb: | |
90 // Called when starvation is detected. The decoder state does not change. | |
91 // The player is supposed to stop and then prefetch the decoder. | |
92 // stop_done_cb: | |
93 // Called when async stop request is completed. | |
94 // error_cb: | |
95 // Called when a MediaCodec error occurred. If this happens, a player has | |
96 // to either call ReleaseDecoderResources() or destroy the decoder object. | |
97 // decoder_thread_name: | |
98 // The thread name to be passed to decoder thread constructor. | |
99 MediaCodecDecoder( | |
100 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | |
101 const base::Closure& request_data_cb, | |
102 const base::Closure& starvation_cb, | |
103 const base::Closure& stop_done_cb, | |
104 const base::Closure& error_cb, | |
105 const char* decoder_thread_name); | |
106 virtual ~MediaCodecDecoder(); | |
107 | |
108 virtual const char* class_name() const; | |
109 | |
110 // MediaCodecDecoder exists through the whole lifetime of the player | |
111 // to support dynamic addition and removal of the streams. | |
112 // This method returns true if the current stream (audio or video) | |
113 // is currently active. | |
114 virtual bool HasStream() const = 0; | |
115 | |
116 // Stores configuration for the use of upcoming Configure() | |
117 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; | |
118 | |
119 // Stops decoder thread, releases the MediaCodecBridge and other resources. | |
120 virtual void ReleaseDecoderResources(); | |
121 | |
122 // Flushes the MediaCodec and resets the AccessUnitQueue. | |
123 // Decoder thread should not be running. | |
124 virtual void Flush(); | |
125 | |
126 // Releases MediaCodecBridge. | |
127 void ReleaseMediaCodec(); | |
128 | |
129 // Returns corresponding conditions. | |
130 bool IsPrefetchingOrPlaying() const; | |
131 bool IsStopped() const; | |
132 bool IsCompleted() const; | |
133 | |
134 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | |
135 | |
136 // Starts prefetching: accumulates enough data in AccessUnitQueue. | |
137 // Decoder thread is not running. | |
138 void Prefetch(const base::Closure& prefetch_done_cb); | |
139 | |
140 // Configures MediaCodec. | |
141 ConfigStatus Configure(); | |
142 | |
143 // Starts the decoder thread and resumes the playback. | |
144 bool Start(base::TimeDelta current_time); | |
145 | |
146 // Stops the playback process synchronously. This method stops the decoder | |
147 // thread synchronously, and then releases all MediaCodec buffers. | |
148 void SyncStop(); | |
149 | |
150 // Requests to stop the playback and returns. | |
151 // Decoder will stop asynchronously after all the dequeued output buffers | |
152 // are rendered. | |
153 void RequestToStop(); | |
154 | |
155 // Notification posted when asynchronous stop is done or playback completed. | |
156 void OnLastFrameRendered(bool completed); | |
157 | |
158 // Puts the incoming data into AccessUnitQueue. | |
159 void OnDemuxerDataAvailable(const DemuxerData& data); | |
160 | |
161 protected: | |
162 // Returns true if the new DemuxerConfigs requires MediaCodec | |
163 // reconfiguration. | |
164 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, | |
165 const DemuxerConfigs& next) const = 0; | |
166 | |
167 // Does the part of MediaCodecBridge configuration that is specific | |
168 // to audio or video. | |
169 virtual ConfigStatus ConfigureInternal() = 0; | |
170 | |
171 // Associates PTS with device time so we can calculate delays. | |
172 // We use delays for video decoder only. | |
173 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {} | |
174 | |
175 // Processes the change of the output format, varies by stream. | |
176 virtual void OnOutputFormatChanged() = 0; | |
177 | |
178 // Renders the decoded frame and releases output buffer, or posts | |
179 // a delayed task to do it at a later time, | |
180 virtual void Render(int buffer_index, | |
181 size_t size, | |
182 bool render_output, | |
183 base::TimeDelta pts, | |
184 bool eos_encountered) = 0; | |
185 | |
186 // Returns the number of delayed task (we might have them for video). | |
187 virtual int NumDelayedRenderTasks() const; | |
188 | |
189 // Releases output buffers that are dequeued and not released yet | |
190 // because their rendering is delayed (video). | |
191 virtual void ReleaseDelayedBuffers() {} | |
192 | |
193 // Helper methods. | |
194 | |
195 // Notifies the decoder if the frame is the last one. | |
196 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); | |
197 | |
198 // Protected data. | |
199 | |
200 // Object for posting tasks on Media thread. | |
201 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | |
202 | |
203 // Controls Android MediaCodec | |
204 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | |
205 | |
206 // We call MediaCodecBridge on this thread for both | |
207 // input and output buffers. | |
208 base::Thread decoder_thread_; | |
209 | |
210 // The queue of access units. | |
211 AccessUnitQueue au_queue_; | |
212 | |
213 private: | |
214 enum DecoderState { | |
215 kStopped = 0, | |
216 kPrefetching, | |
217 kPrefetched, | |
218 kRunning, | |
219 kStopping, | |
220 kError, | |
221 }; | |
222 | |
223 // Helper method that processes an error from MediaCodec. | |
224 void OnCodecError(); | |
225 | |
226 // Prefetching callback that is posted to Media thread | |
227 // in the kPrefetching state. | |
228 void PrefetchNextChunk(); | |
229 | |
230 // The callback to do actual playback. Posted to Decoder thread | |
231 // in the kRunning state. | |
232 void ProcessNextFrame(); | |
233 | |
234 // Helper method for ProcessNextFrame. | |
235 // Pushes one input buffer to the MediaCodec if the codec can accept it. | |
236 // Returns false if there was MediaCodec error. | |
237 bool EnqueueInputBuffer(); | |
238 | |
239 // Helper method for ProcessNextFrame. | |
240 // Pulls all currently available output frames and renders them. | |
241 // Returns false if there was MediaCodec error. | |
242 bool DepleteOutputBufferQueue(bool* eos_encountered); | |
243 | |
244 DecoderState GetState() const; | |
245 void SetState(DecoderState state); | |
246 const char* AsString(DecoderState state); | |
247 | |
248 // Private Data. | |
249 | |
250 // Callback used to request more data. | |
251 base::Closure request_data_cb_; | |
252 | |
253 // These notifications are called on corresponding conditions. | |
254 base::Closure prefetch_done_cb_; | |
255 base::Closure starvation_cb_; | |
256 base::Closure stop_done_cb_; | |
257 base::Closure error_cb_; | |
258 | |
259 // Callback used to post OnCodecError method. | |
260 base::Closure internal_error_cb_; | |
261 | |
262 // Internal state. | |
263 DecoderState state_; | |
264 mutable base::Lock state_lock_; | |
265 | |
266 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | |
267 bool eos_enqueued_; | |
268 | |
269 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | |
270 bool completed_; | |
271 | |
272 // Flag to ensure we post last frame notification once. | |
273 bool last_frame_posted_; | |
274 | |
275 // NOTE: Weak pointers must be invalidated before all other member variables. | |
276 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | |
277 | |
278 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | |
279 }; | |
280 | |
281 } // namespace media | |
282 | |
283 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | |
OLD | NEW |