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 14 matching lines...) Expand all Loading... | |
25 // This class accepts the incoming data into AccessUnitQueue and works with | 25 // This class accepts the incoming data into AccessUnitQueue and works with |
26 // MediaCodecBridge for decoding and rendering the frames. The MediaCodecPlayer | 26 // MediaCodecBridge for decoding and rendering the frames. The MediaCodecPlayer |
27 // has two decoder objects: audio and video. | 27 // has two decoder objects: audio and video. |
28 // | 28 // |
29 // The decoder works on two threads. The data from demuxer comes on Media | 29 // The decoder works on two threads. The data from demuxer comes on Media |
30 // thread. The commands from MediaCodecPlayer, such as Prefetch, Start, | 30 // thread. The commands from MediaCodecPlayer, such as Prefetch, Start, |
31 // RequestToStop also come on the Media thread. The operations with MediaCodec | 31 // RequestToStop also come on the Media thread. The operations with MediaCodec |
32 // buffers and rendering happen on a separate thread called Decoder thread. | 32 // buffers and rendering happen on a separate thread called Decoder thread. |
33 // This class creates, starts and stops it as necessary. | 33 // This class creates, starts and stops it as necessary. |
34 // | 34 // |
35 // Decoder's internal state machine goes through the following states: | 35 // Decoder's internal state machine goes through the following states: |
wolenetz
2015/07/29 22:37:03
nit: InEmergencyStop is missing from these diagram
Tima Vaisburd
2015/07/30 20:28:35
Done.
| |
36 // | 36 // |
37 // [ Stopped ] <------------------- (any state except Error) | 37 // [ Stopped ] <------------------- (any state except Error) |
38 // | | | | 38 // | | | |
39 // | Prefetch |--- internal ------| | 39 // | Prefetch |--- internal ------| |
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 ] | |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 // (*) Demuxer configs is a precondition to Configure(), but MediaCodecPlayer | 99 // (*) Demuxer configs is a precondition to Configure(), but MediaCodecPlayer |
100 // has stricter requirements and they are set before Prefetch(). | 100 // has stricter requirements and they are set before Prefetch(). |
101 // | 101 // |
102 // (**) VideoSurface is a precondition to video decoder Configure(), can be set | 102 // (**) VideoSurface is a precondition to video decoder Configure(), can be set |
103 // any time before Configure(). | 103 // any time before Configure(). |
104 | 104 |
105 class MediaCodecDecoder { | 105 class MediaCodecDecoder { |
106 public: | 106 public: |
107 // The result of MediaCodec configuration, used by MediaCodecPlayer. | 107 // The result of MediaCodec configuration, used by MediaCodecPlayer. |
108 enum ConfigStatus { | 108 enum ConfigStatus { |
109 CONFIG_FAILURE = 0, | 109 CONFIG_FAILURE = 0, |
wolenetz
2015/07/29 22:37:02
Hmm. Here and elsewhere in the new mediacodecplaye
Tima Vaisburd
2015/07/30 20:28:35
Done.
| |
110 CONFIG_OK, | 110 CONFIG_OK, |
111 CONFIG_KEY_FRAME_REQUIRED, | 111 CONFIG_KEY_FRAME_REQUIRED, |
112 }; | 112 }; |
113 | 113 |
114 // The decoder reports current playback time to the MediaCodecPlayer. | 114 // The decoder reports current playback time to the MediaCodecPlayer. |
115 // For audio, the parameters designate the beginning and end of a time | 115 // 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. | 116 // 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 | 117 // The end is the playback time of the last buffered data. During normal |
118 // playback the subsequent intervals overlap. | 118 // playback the subsequent intervals overlap. |
119 // For video both values are PTS of the corresponding frame, i.e. the interval | 119 // For video both values are PTS of the corresponding frame, i.e. the interval |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 // a delayed task to do it at a later time, | 223 // a delayed task to do it at a later time, |
224 virtual void Render(int buffer_index, | 224 virtual void Render(int buffer_index, |
225 size_t size, | 225 size_t size, |
226 bool render_output, | 226 bool render_output, |
227 base::TimeDelta pts, | 227 base::TimeDelta pts, |
228 bool eos_encountered) = 0; | 228 bool eos_encountered) = 0; |
229 | 229 |
230 // Returns the number of delayed task (we might have them for video). | 230 // Returns the number of delayed task (we might have them for video). |
231 virtual int NumDelayedRenderTasks() const; | 231 virtual int NumDelayedRenderTasks() const; |
232 | 232 |
233 // Releases output buffers that are dequeued and not released yet | 233 // Releases output buffers that are dequeued and not released yet (video) |
234 // because their rendering is delayed (video). | 234 // if the |release| parameter is set and then remove the references to them. |
235 virtual void ReleaseDelayedBuffers() {} | 235 virtual void ClearDelayedBuffers(bool release) {} |
236 | 236 |
237 #ifndef NDEBUG | 237 #ifndef NDEBUG |
238 // For video, checks that access unit is the key frame or stand-alone EOS. | 238 // For video, checks that access unit is the key frame or stand-alone EOS. |
239 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} | 239 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} |
240 #endif | 240 #endif |
241 | 241 |
242 // Helper methods. | 242 // Helper methods. |
243 | 243 |
244 // Notifies the decoder if the frame is the last one. | 244 // Notifies the decoder if the frame is the last one. |
245 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); | 245 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); |
246 | 246 |
247 // Protected data. | 247 // Protected data. |
248 | 248 |
249 // Object for posting tasks on Media thread. | 249 // Object for posting tasks on Media thread. |
250 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 250 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
251 | 251 |
252 // Controls Android MediaCodec | 252 // Controls Android MediaCodec |
253 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 253 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
254 | 254 |
255 // We call MediaCodecBridge on this thread for both | 255 // We call MediaCodecBridge on this thread for both |
256 // input and output buffers. | 256 // input and output buffers. |
257 base::Thread decoder_thread_; | 257 base::Thread decoder_thread_; |
258 | 258 |
259 // The queue of access units. | 259 // The queue of access units. |
260 AccessUnitQueue au_queue_; | 260 AccessUnitQueue au_queue_; |
261 | 261 |
262 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | |
263 // is set by video decoder when the video surface changes. | |
264 bool needs_reconfigure_; | |
265 | |
262 private: | 266 private: |
263 enum DecoderState { | 267 enum DecoderState { |
264 kStopped = 0, | 268 kStopped = 0, |
265 kPrefetching, | 269 kPrefetching, |
266 kPrefetched, | 270 kPrefetched, |
267 kRunning, | 271 kRunning, |
268 kStopping, | 272 kStopping, |
273 kInEmergencyStop, | |
269 kError, | 274 kError, |
270 }; | 275 }; |
271 | 276 |
272 // Helper method that processes an error from MediaCodec. | 277 // Helper method that processes an error from MediaCodec. |
273 void OnCodecError(); | 278 void OnCodecError(); |
274 | 279 |
275 // Requests data. Ensures there is no more than one request at a time. | 280 // Requests data. Ensures there is no more than one request at a time. |
276 void RequestData(); | 281 void RequestData(); |
277 | 282 |
278 // Prefetching callback that is posted to Media thread | 283 // Prefetching callback that is posted to Media thread |
279 // in the kPrefetching state. | 284 // in the kPrefetching state. |
280 void PrefetchNextChunk(); | 285 void PrefetchNextChunk(); |
281 | 286 |
282 // The callback to do actual playback. Posted to Decoder thread | 287 // The callback to do actual playback. Posted to Decoder thread |
283 // in the kRunning state. | 288 // in the kRunning state. |
284 void ProcessNextFrame(); | 289 void ProcessNextFrame(); |
285 | 290 |
286 // Helper method for ProcessNextFrame. | 291 // Helper method for ProcessNextFrame. |
287 // Pushes one input buffer to the MediaCodec if the codec can accept it. | 292 // Pushes one input buffer to the MediaCodec if the codec can accept it. |
288 // Returns false if there was MediaCodec error. | 293 // Returns false if there was MediaCodec error. |
289 bool EnqueueInputBuffer(); | 294 bool EnqueueInputBuffer(); |
290 | 295 |
291 // Helper method for ProcessNextFrame. | 296 // Helper method for ProcessNextFrame. |
292 // Pulls all currently available output frames and renders them. | 297 // Pulls all currently available output frames and renders them. |
293 // Returns false if there was MediaCodec error. | 298 // Returns true if we need to continue decoding process, i.e post next |
294 bool DepleteOutputBufferQueue(bool* eos_encountered); | 299 // ProcessNextFrame method, and false if we need to stop decoding. |
300 bool DepleteOutputBufferQueue(); | |
295 | 301 |
296 DecoderState GetState() const; | 302 DecoderState GetState() const; |
297 void SetState(DecoderState state); | 303 void SetState(DecoderState state); |
298 const char* AsString(DecoderState state); | 304 const char* AsString(DecoderState state); |
299 | 305 |
300 // Private Data. | 306 // Private Data. |
301 | 307 |
302 // External data request callback that is passed to decoder. | 308 // External data request callback that is passed to decoder. |
303 base::Closure external_request_data_cb_; | 309 base::Closure external_request_data_cb_; |
304 | 310 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 | 346 |
341 // NOTE: Weak pointers must be invalidated before all other member variables. | 347 // NOTE: Weak pointers must be invalidated before all other member variables. |
342 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 348 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
343 | 349 |
344 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 350 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
345 }; | 351 }; |
346 | 352 |
347 } // namespace media | 353 } // namespace media |
348 | 354 |
349 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 355 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |