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