| 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 #include "media/base/android/media_drm_bridge.h" |
| 19 | 20 |
| 20 namespace media { | 21 namespace media { |
| 21 | 22 |
| 22 class MediaCodecBridge; | 23 class MediaCodecBridge; |
| 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 // |
| (...skipping 82 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 |
| 199 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 206 // Sets the DRM bridge pointer. |
| 207 void SetDrmBridge(MediaDrmBridge* drm_bridge); |
| 200 | 208 |
| 201 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 209 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
| 202 // Decoder thread is not running. | 210 // Decoder thread is not running. |
| 203 void Prefetch(const base::Closure& prefetch_done_cb); | 211 void Prefetch(const base::Closure& prefetch_done_cb); |
| 204 | 212 |
| 205 // Configures MediaCodec. | 213 // Configures MediaCodec. |
| 206 ConfigStatus Configure(); | 214 ConfigStatus Configure(jobject media_crypto); |
| 207 | 215 |
| 208 // Starts the decoder for prerolling. This method starts the decoder thread. | 216 // Starts the decoder for prerolling. This method starts the decoder thread. |
| 209 bool Preroll(const base::Closure& preroll_done_cb); | 217 bool Preroll(const base::Closure& preroll_done_cb); |
| 210 | 218 |
| 211 // Starts the decoder after preroll is not needed, starting decoder thread | 219 // Starts the decoder after preroll is not needed, starting decoder thread |
| 212 // if it has not started yet. | 220 // if it has not started yet. |
| 213 bool Start(base::TimeDelta start_timestamp); | 221 bool Start(base::TimeDelta start_timestamp); |
| 214 | 222 |
| 215 // Stops the playback process synchronously. This method stops the decoder | 223 // Stops the playback process synchronously. This method stops the decoder |
| 216 // thread synchronously, and then releases all MediaCodec buffers. | 224 // thread synchronously, and then releases all MediaCodec buffers. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 247 kRenderAfterPreroll, | 255 kRenderAfterPreroll, |
| 248 kRenderNow, | 256 kRenderNow, |
| 249 }; | 257 }; |
| 250 | 258 |
| 251 // Returns true if the new DemuxerConfigs requires MediaCodec | 259 // Returns true if the new DemuxerConfigs requires MediaCodec |
| 252 // reconfiguration. | 260 // reconfiguration. |
| 253 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& next) const = 0; | 261 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& next) const = 0; |
| 254 | 262 |
| 255 // Does the part of MediaCodecBridge configuration that is specific | 263 // Does the part of MediaCodecBridge configuration that is specific |
| 256 // to audio or video. | 264 // to audio or video. |
| 257 virtual ConfigStatus ConfigureInternal() = 0; | 265 virtual ConfigStatus ConfigureInternal(jobject media_crypto) = 0; |
| 258 | 266 |
| 259 // Associates PTS with device time so we can calculate delays. | 267 // Associates PTS with device time so we can calculate delays. |
| 260 // We use delays for video decoder only. | 268 // We use delays for video decoder only. |
| 261 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} | 269 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} |
| 262 | 270 |
| 263 // Invalidate delay calculation. We use delays for video decoder only. | 271 // Invalidate delay calculation. We use delays for video decoder only. |
| 264 virtual void DissociatePTSFromTime() {} | 272 virtual void DissociatePTSFromTime() {} |
| 265 | 273 |
| 266 // Processes the change of the output format, varies by stream. | 274 // Processes the change of the output format, varies by stream. |
| 267 virtual void OnOutputFormatChanged() = 0; | 275 virtual void OnOutputFormatChanged() = 0; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 // Controls Android MediaCodec | 315 // Controls Android MediaCodec |
| 308 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 316 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
| 309 | 317 |
| 310 // We call MediaCodecBridge on this thread for both | 318 // We call MediaCodecBridge on this thread for both |
| 311 // input and output buffers. | 319 // input and output buffers. |
| 312 base::Thread decoder_thread_; | 320 base::Thread decoder_thread_; |
| 313 | 321 |
| 314 // The queue of access units. | 322 // The queue of access units. |
| 315 AccessUnitQueue au_queue_; | 323 AccessUnitQueue au_queue_; |
| 316 | 324 |
| 325 // Pointer to a DRM object that will be used for encrypted streams. |
| 326 MediaDrmBridge* drm_bridge_; |
| 327 |
| 317 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 328 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
| 318 // is set by video decoder when the video surface changes. | 329 // is set by video decoder when the video surface changes. |
| 319 bool needs_reconfigure_; | 330 bool needs_reconfigure_; |
| 320 | 331 |
| 321 // Flag forces to drain decoder in the process of dynamic reconfiguration. | 332 // Flag forces to drain decoder in the process of dynamic reconfiguration. |
| 322 bool drain_decoder_; | 333 bool drain_decoder_; |
| 323 | 334 |
| 324 // For tests only. Forces to always reconfigure for |kConfigChanged| unit. | 335 // For tests only. Forces to always reconfigure for |kConfigChanged| unit. |
| 325 bool always_reconfigure_for_tests_; | 336 bool always_reconfigure_for_tests_; |
| 326 | 337 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 | 392 |
| 382 // External data request callback that is passed to decoder. | 393 // External data request callback that is passed to decoder. |
| 383 base::Closure external_request_data_cb_; | 394 base::Closure external_request_data_cb_; |
| 384 | 395 |
| 385 // These notifications are called on corresponding conditions. | 396 // These notifications are called on corresponding conditions. |
| 386 base::Closure prefetch_done_cb_; | 397 base::Closure prefetch_done_cb_; |
| 387 base::Closure starvation_cb_; | 398 base::Closure starvation_cb_; |
| 388 base::Closure preroll_done_cb_; | 399 base::Closure preroll_done_cb_; |
| 389 base::Closure decoder_drained_cb_; | 400 base::Closure decoder_drained_cb_; |
| 390 base::Closure stop_done_cb_; | 401 base::Closure stop_done_cb_; |
| 402 base::Closure key_required_cb_; |
| 391 base::Closure error_cb_; | 403 base::Closure error_cb_; |
| 392 | 404 |
| 393 // Data request callback that is posted by decoder internally. | 405 // Data request callback that is posted by decoder internally. |
| 394 base::Closure request_data_cb_; | 406 base::Closure request_data_cb_; |
| 395 | 407 |
| 396 // Callback used to post OnCodecError method. | 408 // Callback used to post OnCodecError method. |
| 397 base::Closure internal_error_cb_; | 409 base::Closure internal_error_cb_; |
| 398 | 410 |
| 399 // Callback for posting OnPrerollDone method. | 411 // Callback for posting OnPrerollDone method. |
| 400 base::Closure internal_preroll_done_cb_; | 412 base::Closure internal_preroll_done_cb_; |
| 401 | 413 |
| 402 // Internal state. | 414 // Internal state. |
| 403 DecoderState state_; | 415 DecoderState state_; |
| 404 mutable base::Lock state_lock_; | 416 mutable base::Lock state_lock_; |
| 405 | 417 |
| 406 // Preroll timestamp is set if we need preroll and cleared after we done it. | 418 // Preroll timestamp is set if we need preroll and cleared after we done it. |
| 407 base::TimeDelta preroll_timestamp_; | 419 base::TimeDelta preroll_timestamp_; |
| 408 | 420 |
| 409 // Set to true when MediaCodec internal buffers are filled up. | 421 // Set to true when MediaCodec internal buffers are filled up. |
| 410 bool is_prepared_; | 422 bool is_prepared_; |
| 411 | 423 |
| 412 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 424 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
| 413 bool eos_enqueued_; | 425 bool eos_enqueued_; |
| 414 | 426 |
| 427 // Flag is set when NO_KEY error is received from QueueSecureInputBuffer. |
| 428 // Reset after we stop. |
| 429 bool key_request_posted_; |
| 430 |
| 415 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 431 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
| 416 bool completed_; | 432 bool completed_; |
| 417 | 433 |
| 418 // Flag to ensure we post last frame notification once. | 434 // Flag to ensure we post last frame notification once. |
| 419 bool last_frame_posted_; | 435 bool last_frame_posted_; |
| 420 | 436 |
| 421 // Indicates whether the data request is in progress. | 437 // Indicates whether the data request is in progress. |
| 422 bool is_data_request_in_progress_; | 438 bool is_data_request_in_progress_; |
| 423 | 439 |
| 424 // Indicates whether the incoming data should be ignored. | 440 // Indicates whether the incoming data should be ignored. |
| 425 bool is_incoming_data_invalid_; | 441 bool is_incoming_data_invalid_; |
| 426 | 442 |
| 427 #ifndef NDEBUG | 443 #ifndef NDEBUG |
| 428 // When set, we check that the following video frame is the key frame. | 444 // When set, we check that the following video frame is the key frame. |
| 429 bool verify_next_frame_is_key_; | 445 bool verify_next_frame_is_key_; |
| 430 #endif | 446 #endif |
| 431 | 447 |
| 432 // NOTE: Weak pointers must be invalidated before all other member variables. | 448 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 433 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 449 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
| 434 | 450 |
| 435 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 451 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
| 436 }; | 452 }; |
| 437 | 453 |
| 438 } // namespace media | 454 } // namespace media |
| 439 | 455 |
| 440 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 456 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
| OLD | NEW |