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" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "media/base/android/access_unit_queue.h" | 17 #include "media/base/android/access_unit_queue.h" |
18 #include "media/base/android/demuxer_stream_player_params.h" | 18 #include "media/base/android/demuxer_stream_player_params.h" |
19 | 19 |
20 namespace media { | 20 namespace media { |
21 | 21 |
22 class MediaCodecBridge; | 22 class MediaCodecBridge; |
| 23 class MediaDrmBridge; |
23 | 24 |
24 // The decoder for MediaCodecPlayer. | 25 // The decoder for MediaCodecPlayer. |
25 // This class accepts the incoming data into AccessUnitQueue and works with | 26 // This class accepts the incoming data into AccessUnitQueue and works with |
26 // MediaCodecBridge for decoding and rendering the frames. The MediaCodecPlayer | 27 // MediaCodecBridge for decoding and rendering the frames. The MediaCodecPlayer |
27 // has two decoder objects: audio and video. | 28 // has two decoder objects: audio and video. |
28 // | 29 // |
29 // The decoder works on two threads. The data from demuxer comes on Media | 30 // The decoder works on two threads. The data from demuxer comes on Media |
30 // thread. The commands from MediaCodecPlayer, such as Prefetch, Start, | 31 // thread. The commands from MediaCodecPlayer, such as Prefetch, Start, |
31 // RequestToStop also come on the Media thread. The operations with MediaCodec | 32 // RequestToStop also come on the Media thread. The operations with MediaCodec |
32 // buffers and rendering happen on a separate thread called Decoder thread. | 33 // buffers and rendering happen on a separate thread called Decoder thread. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // | 112 // |
112 // (**) VideoSurface is a precondition to video decoder Configure(), can be set | 113 // (**) VideoSurface is a precondition to video decoder Configure(), can be set |
113 // any time before Configure(). | 114 // any time before Configure(). |
114 | 115 |
115 class MediaCodecDecoder { | 116 class MediaCodecDecoder { |
116 public: | 117 public: |
117 // The result of MediaCodec configuration, used by MediaCodecPlayer. | 118 // The result of MediaCodec configuration, used by MediaCodecPlayer. |
118 enum ConfigStatus { | 119 enum ConfigStatus { |
119 kConfigFailure = 0, | 120 kConfigFailure = 0, |
120 kConfigOk, | 121 kConfigOk, |
| 122 kConfigNoCrypto, |
121 kConfigKeyFrameRequired, | 123 kConfigKeyFrameRequired, |
122 }; | 124 }; |
123 | 125 |
124 // The decoder reports current playback time to the MediaCodecPlayer. | 126 // The decoder reports current playback time to the MediaCodecPlayer. |
125 // For audio, the parameters designate the beginning and end of a time | 127 // For audio, the parameters designate the beginning and end of a time |
126 // interval. The beginning is the estimated time that is playing right now. | 128 // interval. The beginning is the estimated time that is playing right now. |
127 // The end is the playback time of the last buffered data. During normal | 129 // The end is the playback time of the last buffered data. During normal |
128 // playback the subsequent intervals overlap. | 130 // playback the subsequent intervals overlap. |
129 // For video both values are PTS of the corresponding frame, i.e. the interval | 131 // For video both values are PTS of the corresponding frame, i.e. the interval |
130 // has zero width. | 132 // has zero width. |
(...skipping 24 matching lines...) Expand all Loading... |
155 // Called when a MediaCodec error occurred. If this happens, a player has | 157 // Called when a MediaCodec error occurred. If this happens, a player has |
156 // to either call ReleaseDecoderResources() or destroy the decoder object. | 158 // to either call ReleaseDecoderResources() or destroy the decoder object. |
157 // decoder_thread_name: | 159 // decoder_thread_name: |
158 // The thread name to be passed to decoder thread constructor. | 160 // The thread name to be passed to decoder thread constructor. |
159 MediaCodecDecoder( | 161 MediaCodecDecoder( |
160 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 162 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
161 const base::Closure& external_request_data_cb, | 163 const base::Closure& external_request_data_cb, |
162 const base::Closure& starvation_cb, | 164 const base::Closure& starvation_cb, |
163 const base::Closure& decoder_drained_cb, | 165 const base::Closure& decoder_drained_cb, |
164 const base::Closure& stop_done_cb, | 166 const base::Closure& stop_done_cb, |
| 167 const base::Closure& key_required_cb, |
165 const base::Closure& error_cb, | 168 const base::Closure& error_cb, |
166 const char* decoder_thread_name); | 169 const char* decoder_thread_name); |
167 virtual ~MediaCodecDecoder(); | 170 virtual ~MediaCodecDecoder(); |
168 | 171 |
169 virtual const char* class_name() const; | 172 virtual const char* class_name() const; |
170 | 173 |
171 // MediaCodecDecoder exists through the whole lifetime of the player | 174 // MediaCodecDecoder exists through the whole lifetime of the player |
172 // to support dynamic addition and removal of the streams. | 175 // to support dynamic addition and removal of the streams. |
173 // This method returns true if the current stream (audio or video) | 176 // This method returns true if the current stream (audio or video) |
174 // is currently active. | 177 // is currently active. |
175 virtual bool HasStream() const = 0; | 178 virtual bool HasStream() const = 0; |
176 | 179 |
177 // Stores configuration for the use of upcoming Configure() | 180 // Stores configuration for the use of upcoming Configure() |
178 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; | 181 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; |
179 | 182 |
| 183 // Returns true if the DemuxerConfigs announce that content is encrypted and |
| 184 // that MediaCrypto is required for configuration. |
| 185 virtual bool IsContentEncrypted() const = 0; |
| 186 |
180 // Stops decoder thread, releases the MediaCodecBridge and other resources. | 187 // Stops decoder thread, releases the MediaCodecBridge and other resources. |
181 virtual void ReleaseDecoderResources() = 0; | 188 virtual void ReleaseDecoderResources() = 0; |
182 | 189 |
183 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks | 190 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks |
184 // the input. Decoder thread should not be running. | 191 // the input. Decoder thread should not be running. |
185 virtual void Flush(); | 192 virtual void Flush(); |
186 | 193 |
187 // Releases MediaCodecBridge and any related buffers or references. | 194 // Releases MediaCodecBridge and any related buffers or references. |
188 virtual void ReleaseMediaCodec(); | 195 virtual void ReleaseMediaCodec(); |
189 | 196 |
190 // Returns corresponding conditions. | 197 // Returns corresponding conditions. |
191 bool IsPrefetchingOrPlaying() const; | 198 bool IsPrefetchingOrPlaying() const; |
192 bool IsStopped() const; | 199 bool IsStopped() const; |
193 bool IsCompleted() const; | 200 bool IsCompleted() const; |
194 bool NotCompletedAndNeedsPreroll() const; | 201 bool NotCompletedAndNeedsPreroll() const; |
195 | 202 |
196 // Sets preroll timestamp and requests preroll. | 203 // Sets preroll timestamp and requests preroll. |
197 void SetPrerollTimestamp(base::TimeDelta preroll_ts); | 204 void SetPrerollTimestamp(base::TimeDelta preroll_ts); |
198 | 205 |
| 206 // Sets the DRM bridge pointer. |
| 207 void SetDrmBridge(MediaDrmBridge* drm_bridge); |
| 208 |
| 209 // Extracts MediaCrypto from DRM bridge. |
199 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 210 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
200 | 211 |
201 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 212 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
202 // Decoder thread is not running. | 213 // Decoder thread is not running. |
203 void Prefetch(const base::Closure& prefetch_done_cb); | 214 void Prefetch(const base::Closure& prefetch_done_cb); |
204 | 215 |
205 // Configures MediaCodec. | 216 // Configures MediaCodec. |
206 ConfigStatus Configure(); | 217 ConfigStatus Configure(); |
207 | 218 |
208 // Starts the decoder for prerolling. This method starts the decoder thread. | 219 // Starts the decoder for prerolling. This method starts the decoder thread. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 // Controls Android MediaCodec | 318 // Controls Android MediaCodec |
308 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 319 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
309 | 320 |
310 // We call MediaCodecBridge on this thread for both | 321 // We call MediaCodecBridge on this thread for both |
311 // input and output buffers. | 322 // input and output buffers. |
312 base::Thread decoder_thread_; | 323 base::Thread decoder_thread_; |
313 | 324 |
314 // The queue of access units. | 325 // The queue of access units. |
315 AccessUnitQueue au_queue_; | 326 AccessUnitQueue au_queue_; |
316 | 327 |
| 328 // Pointer to a DRM object that will be used for encrypted streams. |
| 329 MediaDrmBridge* drm_bridge_; |
| 330 |
317 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 331 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
318 // is set by video decoder when the video surface changes. | 332 // is set by video decoder when the video surface changes. |
319 bool needs_reconfigure_; | 333 bool needs_reconfigure_; |
320 | 334 |
321 // Flag forces to drain decoder in the process of dynamic reconfiguration. | 335 // Flag forces to drain decoder in the process of dynamic reconfiguration. |
322 bool drain_decoder_; | 336 bool drain_decoder_; |
323 | 337 |
324 // For tests only. Forces to always reconfigure for |kConfigChanged| unit. | 338 // For tests only. Forces to always reconfigure for |kConfigChanged| unit. |
325 bool always_reconfigure_for_tests_; | 339 bool always_reconfigure_for_tests_; |
326 | 340 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 | 395 |
382 // External data request callback that is passed to decoder. | 396 // External data request callback that is passed to decoder. |
383 base::Closure external_request_data_cb_; | 397 base::Closure external_request_data_cb_; |
384 | 398 |
385 // These notifications are called on corresponding conditions. | 399 // These notifications are called on corresponding conditions. |
386 base::Closure prefetch_done_cb_; | 400 base::Closure prefetch_done_cb_; |
387 base::Closure starvation_cb_; | 401 base::Closure starvation_cb_; |
388 base::Closure preroll_done_cb_; | 402 base::Closure preroll_done_cb_; |
389 base::Closure decoder_drained_cb_; | 403 base::Closure decoder_drained_cb_; |
390 base::Closure stop_done_cb_; | 404 base::Closure stop_done_cb_; |
| 405 base::Closure key_required_cb_; |
391 base::Closure error_cb_; | 406 base::Closure error_cb_; |
392 | 407 |
393 // Data request callback that is posted by decoder internally. | 408 // Data request callback that is posted by decoder internally. |
394 base::Closure request_data_cb_; | 409 base::Closure request_data_cb_; |
395 | 410 |
396 // Callback used to post OnCodecError method. | 411 // Callback used to post OnCodecError method. |
397 base::Closure internal_error_cb_; | 412 base::Closure internal_error_cb_; |
398 | 413 |
399 // Callback for posting OnPrerollDone method. | 414 // Callback for posting OnPrerollDone method. |
400 base::Closure internal_preroll_done_cb_; | 415 base::Closure internal_preroll_done_cb_; |
401 | 416 |
402 // Internal state. | 417 // Internal state. |
403 DecoderState state_; | 418 DecoderState state_; |
404 mutable base::Lock state_lock_; | 419 mutable base::Lock state_lock_; |
405 | 420 |
406 // Preroll timestamp is set if we need preroll and cleared after we done it. | 421 // Preroll timestamp is set if we need preroll and cleared after we done it. |
407 base::TimeDelta preroll_timestamp_; | 422 base::TimeDelta preroll_timestamp_; |
408 | 423 |
409 // Set to true when MediaCodec internal buffers are filled up. | 424 // Set to true when MediaCodec internal buffers are filled up. |
410 bool is_prepared_; | 425 bool is_prepared_; |
411 | 426 |
412 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 427 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
413 bool eos_enqueued_; | 428 bool eos_enqueued_; |
414 | 429 |
| 430 // Flag is set when NO_KEY error is received from QueueSecureInputBuffer. |
| 431 // Reset after we stop. |
| 432 bool key_request_posted_; |
| 433 |
415 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 434 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
416 bool completed_; | 435 bool completed_; |
417 | 436 |
418 // Flag to ensure we post last frame notification once. | 437 // Flag to ensure we post last frame notification once. |
419 bool last_frame_posted_; | 438 bool last_frame_posted_; |
420 | 439 |
421 // Indicates whether the data request is in progress. | 440 // Indicates whether the data request is in progress. |
422 bool is_data_request_in_progress_; | 441 bool is_data_request_in_progress_; |
423 | 442 |
424 // Indicates whether the incoming data should be ignored. | 443 // Indicates whether the incoming data should be ignored. |
425 bool is_incoming_data_invalid_; | 444 bool is_incoming_data_invalid_; |
426 | 445 |
427 #ifndef NDEBUG | 446 #ifndef NDEBUG |
428 // When set, we check that the following video frame is the key frame. | 447 // When set, we check that the following video frame is the key frame. |
429 bool verify_next_frame_is_key_; | 448 bool verify_next_frame_is_key_; |
430 #endif | 449 #endif |
431 | 450 |
432 // NOTE: Weak pointers must be invalidated before all other member variables. | 451 // NOTE: Weak pointers must be invalidated before all other member variables. |
433 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 452 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
434 | 453 |
435 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 454 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
436 }; | 455 }; |
437 | 456 |
438 } // namespace media | 457 } // namespace media |
439 | 458 |
440 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 459 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |