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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // | 111 // |
112 // (**) VideoSurface is a precondition to video decoder Configure(), can be set | 112 // (**) VideoSurface is a precondition to video decoder Configure(), can be set |
113 // any time before Configure(). | 113 // any time before Configure(). |
114 | 114 |
115 class MediaCodecDecoder { | 115 class MediaCodecDecoder { |
116 public: | 116 public: |
117 // The result of MediaCodec configuration, used by MediaCodecPlayer. | 117 // The result of MediaCodec configuration, used by MediaCodecPlayer. |
118 enum ConfigStatus { | 118 enum ConfigStatus { |
119 kConfigFailure = 0, | 119 kConfigFailure = 0, |
120 kConfigOk, | 120 kConfigOk, |
| 121 kConfigNoCrypto, |
121 kConfigKeyFrameRequired, | 122 kConfigKeyFrameRequired, |
122 }; | 123 }; |
123 | 124 |
124 // The decoder reports current playback time to the MediaCodecPlayer. | 125 // The decoder reports current playback time to the MediaCodecPlayer. |
125 // For audio, the parameters designate the beginning and end of a time | 126 // 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. | 127 // 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 | 128 // The end is the playback time of the last buffered data. During normal |
128 // playback the subsequent intervals overlap. | 129 // playback the subsequent intervals overlap. |
129 // For video both values are PTS of the corresponding frame, i.e. the interval | 130 // For video both values are PTS of the corresponding frame, i.e. the interval |
130 // has zero width. | 131 // has zero width. |
(...skipping 24 matching lines...) Expand all Loading... |
155 // Called when a MediaCodec error occurred. If this happens, a player has | 156 // Called when a MediaCodec error occurred. If this happens, a player has |
156 // to either call ReleaseDecoderResources() or destroy the decoder object. | 157 // to either call ReleaseDecoderResources() or destroy the decoder object. |
157 // decoder_thread_name: | 158 // decoder_thread_name: |
158 // The thread name to be passed to decoder thread constructor. | 159 // The thread name to be passed to decoder thread constructor. |
159 MediaCodecDecoder( | 160 MediaCodecDecoder( |
160 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 161 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
161 const base::Closure& external_request_data_cb, | 162 const base::Closure& external_request_data_cb, |
162 const base::Closure& starvation_cb, | 163 const base::Closure& starvation_cb, |
163 const base::Closure& decoder_drained_cb, | 164 const base::Closure& decoder_drained_cb, |
164 const base::Closure& stop_done_cb, | 165 const base::Closure& stop_done_cb, |
| 166 const base::Closure& key_required_cb, |
165 const base::Closure& error_cb, | 167 const base::Closure& error_cb, |
166 const char* decoder_thread_name); | 168 const char* decoder_thread_name); |
167 virtual ~MediaCodecDecoder(); | 169 virtual ~MediaCodecDecoder(); |
168 | 170 |
169 virtual const char* class_name() const; | 171 virtual const char* class_name() const; |
170 | 172 |
171 // MediaCodecDecoder exists through the whole lifetime of the player | 173 // MediaCodecDecoder exists through the whole lifetime of the player |
172 // to support dynamic addition and removal of the streams. | 174 // to support dynamic addition and removal of the streams. |
173 // This method returns true if the current stream (audio or video) | 175 // This method returns true if the current stream (audio or video) |
174 // is currently active. | 176 // is currently active. |
175 virtual bool HasStream() const = 0; | 177 virtual bool HasStream() const = 0; |
176 | 178 |
177 // Stores configuration for the use of upcoming Configure() | 179 // Stores configuration for the use of upcoming Configure() |
178 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; | 180 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; |
179 | 181 |
| 182 // Returns true if the DemuxerConfigs announce that content is encrypted and |
| 183 // that MediaCrypto is required for configuration. |
| 184 virtual bool IsContentEncrypted() const = 0; |
| 185 |
180 // Stops decoder thread, releases the MediaCodecBridge and other resources. | 186 // Stops decoder thread, releases the MediaCodecBridge and other resources. |
181 virtual void ReleaseDecoderResources() = 0; | 187 virtual void ReleaseDecoderResources() = 0; |
182 | 188 |
183 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks | 189 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks |
184 // the input. Decoder thread should not be running. | 190 // the input. Decoder thread should not be running. |
185 virtual void Flush(); | 191 virtual void Flush(); |
186 | 192 |
187 // Releases MediaCodecBridge and any related buffers or references. | 193 // Releases MediaCodecBridge and any related buffers or references. |
188 virtual void ReleaseMediaCodec(); | 194 virtual void ReleaseMediaCodec(); |
189 | 195 |
190 // Returns corresponding conditions. | 196 // Returns corresponding conditions. |
191 bool IsPrefetchingOrPlaying() const; | 197 bool IsPrefetchingOrPlaying() const; |
192 bool IsStopped() const; | 198 bool IsStopped() const; |
193 bool IsCompleted() const; | 199 bool IsCompleted() const; |
194 bool NotCompletedAndNeedsPreroll() const; | 200 bool NotCompletedAndNeedsPreroll() const; |
195 | 201 |
| 202 // Forces reconfiguraton on the next Configure(). |
| 203 void SetNeedsReconfigure(); |
| 204 |
196 // Sets preroll timestamp and requests preroll. | 205 // Sets preroll timestamp and requests preroll. |
197 void SetPrerollTimestamp(base::TimeDelta preroll_ts); | 206 void SetPrerollTimestamp(base::TimeDelta preroll_ts); |
198 | 207 |
199 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | |
200 | |
201 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 208 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
202 // Decoder thread is not running. | 209 // Decoder thread is not running. |
203 void Prefetch(const base::Closure& prefetch_done_cb); | 210 void Prefetch(const base::Closure& prefetch_done_cb); |
204 | 211 |
205 // Configures MediaCodec. | 212 // Configures MediaCodec. |
206 ConfigStatus Configure(); | 213 ConfigStatus Configure(jobject media_crypto); |
207 | 214 |
208 // Starts the decoder for prerolling. This method starts the decoder thread. | 215 // Starts the decoder for prerolling. This method starts the decoder thread. |
209 bool Preroll(const base::Closure& preroll_done_cb); | 216 bool Preroll(const base::Closure& preroll_done_cb); |
210 | 217 |
211 // Starts the decoder after preroll is not needed, starting decoder thread | 218 // Starts the decoder after preroll is not needed, starting decoder thread |
212 // if it has not started yet. | 219 // if it has not started yet. |
213 bool Start(base::TimeDelta start_timestamp); | 220 bool Start(base::TimeDelta start_timestamp); |
214 | 221 |
215 // Stops the playback process synchronously. This method stops the decoder | 222 // Stops the playback process synchronously. This method stops the decoder |
216 // thread synchronously, and then releases all MediaCodec buffers. | 223 // thread synchronously, and then releases all MediaCodec buffers. |
(...skipping 30 matching lines...) Expand all Loading... |
247 kRenderAfterPreroll, | 254 kRenderAfterPreroll, |
248 kRenderNow, | 255 kRenderNow, |
249 }; | 256 }; |
250 | 257 |
251 // Returns true if the new DemuxerConfigs requires MediaCodec | 258 // Returns true if the new DemuxerConfigs requires MediaCodec |
252 // reconfiguration. | 259 // reconfiguration. |
253 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& next) const = 0; | 260 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& next) const = 0; |
254 | 261 |
255 // Does the part of MediaCodecBridge configuration that is specific | 262 // Does the part of MediaCodecBridge configuration that is specific |
256 // to audio or video. | 263 // to audio or video. |
257 virtual ConfigStatus ConfigureInternal() = 0; | 264 virtual ConfigStatus ConfigureInternal(jobject media_crypto) = 0; |
258 | 265 |
259 // Associates PTS with device time so we can calculate delays. | 266 // Associates PTS with device time so we can calculate delays. |
260 // We use delays for video decoder only. | 267 // We use delays for video decoder only. |
261 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} | 268 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} |
262 | 269 |
263 // Invalidate delay calculation. We use delays for video decoder only. | 270 // Invalidate delay calculation. We use delays for video decoder only. |
264 virtual void DissociatePTSFromTime() {} | 271 virtual void DissociatePTSFromTime() {} |
265 | 272 |
266 // Processes the change of the output format, varies by stream. | 273 // Processes the change of the output format, varies by stream. |
267 virtual void OnOutputFormatChanged() = 0; | 274 virtual void OnOutputFormatChanged() = 0; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 | 388 |
382 // External data request callback that is passed to decoder. | 389 // External data request callback that is passed to decoder. |
383 base::Closure external_request_data_cb_; | 390 base::Closure external_request_data_cb_; |
384 | 391 |
385 // These notifications are called on corresponding conditions. | 392 // These notifications are called on corresponding conditions. |
386 base::Closure prefetch_done_cb_; | 393 base::Closure prefetch_done_cb_; |
387 base::Closure starvation_cb_; | 394 base::Closure starvation_cb_; |
388 base::Closure preroll_done_cb_; | 395 base::Closure preroll_done_cb_; |
389 base::Closure decoder_drained_cb_; | 396 base::Closure decoder_drained_cb_; |
390 base::Closure stop_done_cb_; | 397 base::Closure stop_done_cb_; |
| 398 base::Closure key_required_cb_; |
391 base::Closure error_cb_; | 399 base::Closure error_cb_; |
392 | 400 |
393 // Data request callback that is posted by decoder internally. | 401 // Data request callback that is posted by decoder internally. |
394 base::Closure request_data_cb_; | 402 base::Closure request_data_cb_; |
395 | 403 |
396 // Callback used to post OnCodecError method. | 404 // Callback used to post OnCodecError method. |
397 base::Closure internal_error_cb_; | 405 base::Closure internal_error_cb_; |
398 | 406 |
399 // Callback for posting OnPrerollDone method. | 407 // Callback for posting OnPrerollDone method. |
400 base::Closure internal_preroll_done_cb_; | 408 base::Closure internal_preroll_done_cb_; |
401 | 409 |
402 // Internal state. | 410 // Internal state. |
403 DecoderState state_; | 411 DecoderState state_; |
404 mutable base::Lock state_lock_; | 412 mutable base::Lock state_lock_; |
405 | 413 |
406 // Preroll timestamp is set if we need preroll and cleared after we done it. | 414 // Preroll timestamp is set if we need preroll and cleared after we done it. |
407 base::TimeDelta preroll_timestamp_; | 415 base::TimeDelta preroll_timestamp_; |
408 | 416 |
409 // Set to true when MediaCodec internal buffers are filled up. | 417 // Set to true when MediaCodec internal buffers are filled up. |
410 bool is_prepared_; | 418 bool is_prepared_; |
411 | 419 |
412 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 420 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
413 bool eos_enqueued_; | 421 bool eos_enqueued_; |
414 | 422 |
| 423 // Flag is set when NO_KEY error is received from QueueSecureInputBuffer. |
| 424 // Reset after we stop. |
| 425 bool key_request_posted_; |
| 426 |
415 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 427 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
416 bool completed_; | 428 bool completed_; |
417 | 429 |
418 // Flag to ensure we post last frame notification once. | 430 // Flag to ensure we post last frame notification once. |
419 bool last_frame_posted_; | 431 bool last_frame_posted_; |
420 | 432 |
421 // Indicates whether the data request is in progress. | 433 // Indicates whether the data request is in progress. |
422 bool is_data_request_in_progress_; | 434 bool is_data_request_in_progress_; |
423 | 435 |
424 // Indicates whether the incoming data should be ignored. | 436 // Indicates whether the incoming data should be ignored. |
425 bool is_incoming_data_invalid_; | 437 bool is_incoming_data_invalid_; |
426 | 438 |
427 #ifndef NDEBUG | 439 #ifndef NDEBUG |
428 // When set, we check that the following video frame is the key frame. | 440 // When set, we check that the following video frame is the key frame. |
429 bool verify_next_frame_is_key_; | 441 bool verify_next_frame_is_key_; |
430 #endif | 442 #endif |
431 | 443 |
432 // NOTE: Weak pointers must be invalidated before all other member variables. | 444 // NOTE: Weak pointers must be invalidated before all other member variables. |
433 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 445 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
434 | 446 |
435 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 447 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
436 }; | 448 }; |
437 | 449 |
438 } // namespace media | 450 } // namespace media |
439 | 451 |
440 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 452 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |