OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 // v | transition v | 40 // v | transition v |
41 // [ Prefetching ] | [ Error ] | 41 // [ Prefetching ] | [ Error ] |
42 // | | | 42 // | | |
43 // | internal transition | | 43 // | internal transition | |
44 // v | Error recovery: | 44 // v | Error recovery: |
45 // [ Prefetched ] | | 45 // [ Prefetched ] | |
46 // | | (any state including Error) | 46 // | | (any state including Error) |
47 // | Configure and Start | | | 47 // | Configure and Start | | |
48 // v | | ReleaseDecoderResources | 48 // v | | ReleaseDecoderResources |
49 // [ Running ] | v | 49 // [ Running ] | v |
50 // | | [ Stopped ] | 50 // | | [ InEmergencyStop ] |
51 // | RequestToStop | | 51 // | RequestToStop | | |
52 // v | | 52 // v | |(decoder thread stopped) |
53 // [ Stopping ] ------------------- | 53 // [ Stopping ] ------------------- v |
54 // | 54 // [ Stopped ] |
55 // | 55 // |
56 // [ Stopped ] -------------------- | 56 // [ Stopped ] -------------------- |
57 // ^ | | 57 // ^ | |
58 // | Flush | | 58 // | Flush | |
59 // --------------------------- | 59 // --------------------------- |
60 | 60 |
| 61 // (any state except Error) |
| 62 // | |
| 63 // | SyncStop |
| 64 // v |
| 65 // [ InEmergencyStop ] |
| 66 // | |
| 67 // |(decoder thread stopped) |
| 68 // v |
| 69 // [ Stopped ] |
| 70 |
61 // Here is the workflow that is expected to be maintained by a caller, which is | 71 // Here is the workflow that is expected to be maintained by a caller, which is |
62 // MediaCodecPlayer currently. | 72 // MediaCodecPlayer currently. |
63 // | 73 // |
64 // [ Stopped ] | 74 // [ Stopped ] |
65 // | | 75 // | |
66 // | Prefetch | 76 // | Prefetch |
67 // v | 77 // v |
68 // [ Prefetching ] | 78 // [ Prefetching ] |
69 // | | 79 // | |
70 // | (Enough data received) | 80 // | (Enough data received) |
(...skipping 28 matching lines...) Expand all Loading... |
99 // (*) Demuxer configs is a precondition to Configure(), but MediaCodecPlayer | 109 // (*) Demuxer configs is a precondition to Configure(), but MediaCodecPlayer |
100 // has stricter requirements and they are set before Prefetch(). | 110 // has stricter requirements and they are set before Prefetch(). |
101 // | 111 // |
102 // (**) VideoSurface is a precondition to video decoder Configure(), can be set | 112 // (**) VideoSurface is a precondition to video decoder Configure(), can be set |
103 // any time before Configure(). | 113 // any time before Configure(). |
104 | 114 |
105 class MediaCodecDecoder { | 115 class MediaCodecDecoder { |
106 public: | 116 public: |
107 // The result of MediaCodec configuration, used by MediaCodecPlayer. | 117 // The result of MediaCodec configuration, used by MediaCodecPlayer. |
108 enum ConfigStatus { | 118 enum ConfigStatus { |
109 CONFIG_FAILURE = 0, | 119 kConfigFailure = 0, |
110 CONFIG_OK, | 120 kConfigOk, |
111 CONFIG_KEY_FRAME_REQUIRED, | 121 kConfigKeyFrameRequired, |
112 }; | 122 }; |
113 | 123 |
114 // The decoder reports current playback time to the MediaCodecPlayer. | 124 // The decoder reports current playback time to the MediaCodecPlayer. |
115 // For audio, the parameters designate the beginning and end of a time | 125 // For audio, the parameters designate the beginning and end of a time |
116 // interval. The beginning is the estimated time that is playing right now. | 126 // interval. The beginning is the estimated time that is playing right now. |
117 // The end is the playback time of the last buffered data. During normal | 127 // The end is the playback time of the last buffered data. During normal |
118 // playback the subsequent intervals overlap. | 128 // playback the subsequent intervals overlap. |
119 // For video both values are PTS of the corresponding frame, i.e. the interval | 129 // For video both values are PTS of the corresponding frame, i.e. the interval |
120 // has zero width. | 130 // has zero width. |
121 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)> | 131 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)> |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 // a delayed task to do it at a later time, | 233 // a delayed task to do it at a later time, |
224 virtual void Render(int buffer_index, | 234 virtual void Render(int buffer_index, |
225 size_t size, | 235 size_t size, |
226 bool render_output, | 236 bool render_output, |
227 base::TimeDelta pts, | 237 base::TimeDelta pts, |
228 bool eos_encountered) = 0; | 238 bool eos_encountered) = 0; |
229 | 239 |
230 // Returns the number of delayed task (we might have them for video). | 240 // Returns the number of delayed task (we might have them for video). |
231 virtual int NumDelayedRenderTasks() const; | 241 virtual int NumDelayedRenderTasks() const; |
232 | 242 |
233 // Releases output buffers that are dequeued and not released yet | 243 // Releases output buffers that are dequeued and not released yet (video) |
234 // because their rendering is delayed (video). | 244 // if the |release| parameter is set and then remove the references to them. |
235 virtual void ReleaseDelayedBuffers() {} | 245 virtual void ClearDelayedBuffers(bool release) {} |
236 | 246 |
237 #ifndef NDEBUG | 247 #ifndef NDEBUG |
238 // For video, checks that access unit is the key frame or stand-alone EOS. | 248 // For video, checks that access unit is the key frame or stand-alone EOS. |
239 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} | 249 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} |
240 #endif | 250 #endif |
241 | 251 |
242 // Helper methods. | 252 // Helper methods. |
243 | 253 |
244 // Notifies the decoder if the frame is the last one. | 254 // Notifies the decoder if the frame is the last one. |
245 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); | 255 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); |
246 | 256 |
247 // Protected data. | 257 // Protected data. |
248 | 258 |
249 // Object for posting tasks on Media thread. | 259 // Object for posting tasks on Media thread. |
250 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 260 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
251 | 261 |
252 // Controls Android MediaCodec | 262 // Controls Android MediaCodec |
253 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 263 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
254 | 264 |
255 // We call MediaCodecBridge on this thread for both | 265 // We call MediaCodecBridge on this thread for both |
256 // input and output buffers. | 266 // input and output buffers. |
257 base::Thread decoder_thread_; | 267 base::Thread decoder_thread_; |
258 | 268 |
259 // The queue of access units. | 269 // The queue of access units. |
260 AccessUnitQueue au_queue_; | 270 AccessUnitQueue au_queue_; |
261 | 271 |
| 272 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
| 273 // is set by video decoder when the video surface changes. |
| 274 bool needs_reconfigure_; |
| 275 |
262 private: | 276 private: |
263 enum DecoderState { | 277 enum DecoderState { |
264 kStopped = 0, | 278 kStopped = 0, |
265 kPrefetching, | 279 kPrefetching, |
266 kPrefetched, | 280 kPrefetched, |
267 kRunning, | 281 kRunning, |
268 kStopping, | 282 kStopping, |
| 283 kInEmergencyStop, |
269 kError, | 284 kError, |
270 }; | 285 }; |
271 | 286 |
272 // Helper method that processes an error from MediaCodec. | 287 // Helper method that processes an error from MediaCodec. |
273 void OnCodecError(); | 288 void OnCodecError(); |
274 | 289 |
275 // Requests data. Ensures there is no more than one request at a time. | 290 // Requests data. Ensures there is no more than one request at a time. |
276 void RequestData(); | 291 void RequestData(); |
277 | 292 |
278 // Prefetching callback that is posted to Media thread | 293 // Prefetching callback that is posted to Media thread |
279 // in the kPrefetching state. | 294 // in the kPrefetching state. |
280 void PrefetchNextChunk(); | 295 void PrefetchNextChunk(); |
281 | 296 |
282 // The callback to do actual playback. Posted to Decoder thread | 297 // The callback to do actual playback. Posted to Decoder thread |
283 // in the kRunning state. | 298 // in the kRunning state. |
284 void ProcessNextFrame(); | 299 void ProcessNextFrame(); |
285 | 300 |
286 // Helper method for ProcessNextFrame. | 301 // Helper method for ProcessNextFrame. |
287 // Pushes one input buffer to the MediaCodec if the codec can accept it. | 302 // Pushes one input buffer to the MediaCodec if the codec can accept it. |
288 // Returns false if there was MediaCodec error. | 303 // Returns false if there was MediaCodec error. |
289 bool EnqueueInputBuffer(); | 304 bool EnqueueInputBuffer(); |
290 | 305 |
291 // Helper method for ProcessNextFrame. | 306 // Helper method for ProcessNextFrame. |
292 // Pulls all currently available output frames and renders them. | 307 // Pulls all currently available output frames and renders them. |
293 // Returns false if there was MediaCodec error. | 308 // Returns true if we need to continue decoding process, i.e post next |
294 bool DepleteOutputBufferQueue(bool* eos_encountered); | 309 // ProcessNextFrame method, and false if we need to stop decoding. |
| 310 bool DepleteOutputBufferQueue(); |
295 | 311 |
296 DecoderState GetState() const; | 312 DecoderState GetState() const; |
297 void SetState(DecoderState state); | 313 void SetState(DecoderState state); |
298 const char* AsString(DecoderState state); | 314 const char* AsString(DecoderState state); |
299 | 315 |
300 // Private Data. | 316 // Private Data. |
301 | 317 |
302 // External data request callback that is passed to decoder. | 318 // External data request callback that is passed to decoder. |
303 base::Closure external_request_data_cb_; | 319 base::Closure external_request_data_cb_; |
304 | 320 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 356 |
341 // NOTE: Weak pointers must be invalidated before all other member variables. | 357 // NOTE: Weak pointers must be invalidated before all other member variables. |
342 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 358 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
343 | 359 |
344 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 360 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
345 }; | 361 }; |
346 | 362 |
347 } // namespace media | 363 } // namespace media |
348 | 364 |
349 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 365 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |