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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 // stop_done_cb: | 150 // stop_done_cb: |
151 // Called when async stop request is completed. | 151 // Called when async stop request is completed. |
| 152 // decoder_drained_cb: |
| 153 // Called when decoder is drained for reconfiguration. |
152 // error_cb: | 154 // error_cb: |
153 // Called when a MediaCodec error occurred. If this happens, a player has | 155 // Called when a MediaCodec error occurred. If this happens, a player has |
154 // to either call ReleaseDecoderResources() or destroy the decoder object. | 156 // to either call ReleaseDecoderResources() or destroy the decoder object. |
155 // decoder_thread_name: | 157 // decoder_thread_name: |
156 // The thread name to be passed to decoder thread constructor. | 158 // The thread name to be passed to decoder thread constructor. |
157 MediaCodecDecoder( | 159 MediaCodecDecoder( |
158 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 160 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
159 const base::Closure& external_request_data_cb, | 161 const base::Closure& external_request_data_cb, |
160 const base::Closure& starvation_cb, | 162 const base::Closure& starvation_cb, |
| 163 const base::Closure& decoder_drained_cb, |
161 const base::Closure& stop_done_cb, | 164 const base::Closure& stop_done_cb, |
162 const base::Closure& error_cb, | 165 const base::Closure& error_cb, |
163 const char* decoder_thread_name); | 166 const char* decoder_thread_name); |
164 virtual ~MediaCodecDecoder(); | 167 virtual ~MediaCodecDecoder(); |
165 | 168 |
166 virtual const char* class_name() const; | 169 virtual const char* class_name() const; |
167 | 170 |
168 // MediaCodecDecoder exists through the whole lifetime of the player | 171 // MediaCodecDecoder exists through the whole lifetime of the player |
169 // to support dynamic addition and removal of the streams. | 172 // to support dynamic addition and removal of the streams. |
170 // This method returns true if the current stream (audio or video) | 173 // This method returns true if the current stream (audio or video) |
(...skipping 12 matching lines...) Expand all Loading... |
183 | 186 |
184 // Releases MediaCodecBridge and any related buffers or references. | 187 // Releases MediaCodecBridge and any related buffers or references. |
185 virtual void ReleaseMediaCodec(); | 188 virtual void ReleaseMediaCodec(); |
186 | 189 |
187 // Returns corresponding conditions. | 190 // Returns corresponding conditions. |
188 bool IsPrefetchingOrPlaying() const; | 191 bool IsPrefetchingOrPlaying() const; |
189 bool IsStopped() const; | 192 bool IsStopped() const; |
190 bool IsCompleted() const; | 193 bool IsCompleted() const; |
191 bool NotCompletedAndNeedsPreroll() const; | 194 bool NotCompletedAndNeedsPreroll() const; |
192 | 195 |
| 196 // Requests an A/V sync mechanism that is similar to preroll, but stops at the |
| 197 // first available output frame rather than passing certain PTS. |
| 198 void SetDecodingUntilOutputIsPresent(); |
| 199 |
193 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 200 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
194 | 201 |
195 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 202 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
196 // Decoder thread is not running. | 203 // Decoder thread is not running. |
197 void Prefetch(const base::Closure& prefetch_done_cb); | 204 void Prefetch(const base::Closure& prefetch_done_cb); |
198 | 205 |
199 // Configures MediaCodec. | 206 // Configures MediaCodec. |
200 ConfigStatus Configure(); | 207 ConfigStatus Configure(); |
201 | 208 |
202 // Starts the decoder for prerolling. This method starts the decoder thread. | 209 // Starts the decoder for prerolling. This method starts the decoder thread. |
203 bool Preroll(base::TimeDelta preroll_timestamp, | 210 bool Preroll(base::TimeDelta preroll_timestamp, |
204 const base::Closure& preroll_done_cb); | 211 const base::Closure& preroll_done_cb); |
205 | 212 |
206 // Starts the decoder after preroll is not needed, starting decoder thread | 213 // Starts the decoder after preroll is not needed, starting decoder thread |
207 // if it has not started yet. | 214 // if it has not started yet. |
208 bool Start(base::TimeDelta start_timestamp); | 215 bool Start(base::TimeDelta start_timestamp); |
209 | 216 |
210 // Stops the playback process synchronously. This method stops the decoder | 217 // Stops the playback process synchronously. This method stops the decoder |
211 // thread synchronously, and then releases all MediaCodec buffers. | 218 // thread synchronously, and then releases all MediaCodec buffers. |
212 void SyncStop(); | 219 void SyncStop(); |
213 | 220 |
214 // Requests to stop the playback and returns. | 221 // Requests to stop the playback and returns. |
215 // Decoder will stop asynchronously after all the dequeued output buffers | 222 // Decoder will stop asynchronously after all the dequeued output buffers |
216 // are rendered. | 223 // are rendered. |
217 void RequestToStop(); | 224 void RequestToStop(); |
218 | 225 |
219 // Notification posted when asynchronous stop is done or playback completed. | 226 // Notification posted when asynchronous stop is done or playback completed. |
220 void OnLastFrameRendered(bool completed); | 227 void OnLastFrameRendered(bool eos_encountered); |
221 | 228 |
222 // Notification posted when last prerolled frame has been returned to codec. | 229 // Notification posted when last prerolled frame has been returned to codec. |
223 void OnPrerollDone(); | 230 void OnPrerollDone(); |
224 | 231 |
225 // Puts the incoming data into AccessUnitQueue. | 232 // Puts the incoming data into AccessUnitQueue. |
226 void OnDemuxerDataAvailable(const DemuxerData& data); | 233 void OnDemuxerDataAvailable(const DemuxerData& data); |
227 | 234 |
228 // For testing only. Returns true if the decoder is in kPrerolling state. | 235 // For testing only. |
| 236 |
| 237 // Returns true if the decoder is in kPrerolling state. |
229 bool IsPrerollingForTests() const; | 238 bool IsPrerollingForTests() const; |
230 | 239 |
| 240 // Drains decoder and reconfigures for each |kConfigChanged|. |
| 241 void SetAlwaysReconfigureForTests(); |
| 242 |
| 243 // Sets the notification to be called when MediaCodec is created. |
| 244 void SetCodecCreatedCallbackForTests(base::Closure cb); |
| 245 |
231 protected: | 246 protected: |
232 enum RenderMode { | 247 enum RenderMode { |
233 kRenderSkip = 0, | 248 kRenderSkip = 0, |
234 kRenderAfterPreroll, | 249 kRenderAfterPreroll, |
235 kRenderNow, | 250 kRenderNow, |
236 }; | 251 }; |
237 | 252 |
238 // Returns true if the new DemuxerConfigs requires MediaCodec | 253 // Returns true if the new DemuxerConfigs requires MediaCodec |
239 // reconfiguration. | 254 // reconfiguration. |
240 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, | 255 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& next) const = 0; |
241 const DemuxerConfigs& next) const = 0; | |
242 | 256 |
243 // Does the part of MediaCodecBridge configuration that is specific | 257 // Does the part of MediaCodecBridge configuration that is specific |
244 // to audio or video. | 258 // to audio or video. |
245 virtual ConfigStatus ConfigureInternal() = 0; | 259 virtual ConfigStatus ConfigureInternal() = 0; |
246 | 260 |
247 // Associates PTS with device time so we can calculate delays. | 261 // Associates PTS with device time so we can calculate delays. |
248 // We use delays for video decoder only. | 262 // We use delays for video decoder only. |
249 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} | 263 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} |
250 | 264 |
251 // Invalidate delay calculation. We use delays for video decoder only. | 265 // Invalidate delay calculation. We use delays for video decoder only. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 // input and output buffers. | 313 // input and output buffers. |
300 base::Thread decoder_thread_; | 314 base::Thread decoder_thread_; |
301 | 315 |
302 // The queue of access units. | 316 // The queue of access units. |
303 AccessUnitQueue au_queue_; | 317 AccessUnitQueue au_queue_; |
304 | 318 |
305 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 319 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
306 // is set by video decoder when the video surface changes. | 320 // is set by video decoder when the video surface changes. |
307 bool needs_reconfigure_; | 321 bool needs_reconfigure_; |
308 | 322 |
| 323 // Flag forces to drain decoder in the process of dynamic reconfiguration. |
| 324 bool drain_decoder_; |
| 325 |
| 326 // For tests only. Forces to always reconfigure for |kConfigChanged| unit. |
| 327 bool always_reconfigure_for_tests_; |
| 328 |
| 329 // For tests only. Callback to be callned when MediaCodec is created. |
| 330 base::Closure codec_created_for_tests_cb_; |
| 331 |
309 private: | 332 private: |
310 enum DecoderState { | 333 enum DecoderState { |
311 kStopped = 0, | 334 kStopped = 0, |
312 kPrefetching, | 335 kPrefetching, |
313 kPrefetched, | 336 kPrefetched, |
314 kPrerolling, | 337 kPrerolling, |
315 kPrerolled, | 338 kPrerolled, |
316 kRunning, | 339 kRunning, |
317 kStopping, | 340 kStopping, |
318 kInEmergencyStop, | 341 kInEmergencyStop, |
319 kError, | 342 kError, |
320 }; | 343 }; |
321 | 344 |
| 345 enum PrerollMode { |
| 346 kNoPreroll = 0, |
| 347 kPrerollTillOutputIsPresent, |
| 348 kPrerollTillPTS, |
| 349 }; |
| 350 |
322 // Helper method that processes an error from MediaCodec. | 351 // Helper method that processes an error from MediaCodec. |
323 void OnCodecError(); | 352 void OnCodecError(); |
324 | 353 |
325 // Requests data. Ensures there is no more than one request at a time. | 354 // Requests data. Ensures there is no more than one request at a time. |
326 void RequestData(); | 355 void RequestData(); |
327 | 356 |
328 // Prefetching callback that is posted to Media thread | 357 // Prefetching callback that is posted to Media thread |
329 // in the kPrefetching state. | 358 // in the kPrefetching state. |
330 void PrefetchNextChunk(); | 359 void PrefetchNextChunk(); |
331 | 360 |
332 // The callback to do actual playback. Posted to Decoder thread | 361 // The callback to do actual playback. Posted to Decoder thread |
333 // in the kRunning state. | 362 // in the kRunning state. |
334 void ProcessNextFrame(); | 363 void ProcessNextFrame(); |
335 | 364 |
336 // Helper method for ProcessNextFrame. | 365 // Helper method for ProcessNextFrame. |
337 // Pushes one input buffer to the MediaCodec if the codec can accept it. | 366 // Pushes one input buffer to the MediaCodec if the codec can accept it. |
338 // Returns false if there was MediaCodec error. | 367 // Returns false if there was MediaCodec error. |
339 bool EnqueueInputBuffer(); | 368 bool EnqueueInputBuffer(); |
340 | 369 |
| 370 // Helper method for EnqueueInputBuffer. |
| 371 // Gets the next data frame from the queue, requesting more data and saving |
| 372 // configuration changes on the way. Sets |drain_decoder| to true of any of |
| 373 // the configuration changes requires draining the decoder. Returns the Info |
| 374 // pointing to the current data unit ot empty Info if it got past the end of |
| 375 // the queue. |
| 376 AccessUnitQueue::Info AdvanceAccessUnitQueue(bool* drain_decoder); |
| 377 |
341 // Helper method for ProcessNextFrame. | 378 // Helper method for ProcessNextFrame. |
342 // Pulls all currently available output frames and renders them. | 379 // Pulls all currently available output frames and renders them. |
343 // Returns true if we need to continue decoding process, i.e post next | 380 // Returns true if we need to continue decoding process, i.e post next |
344 // ProcessNextFrame method, and false if we need to stop decoding. | 381 // ProcessNextFrame method, and false if we need to stop decoding. |
345 bool DepleteOutputBufferQueue(); | 382 bool DepleteOutputBufferQueue(); |
346 | 383 |
347 DecoderState GetState() const; | 384 DecoderState GetState() const; |
348 void SetState(DecoderState state); | 385 void SetState(DecoderState state); |
349 const char* AsString(DecoderState state); | 386 const char* AsString(DecoderState state); |
350 | 387 |
351 // Private Data. | 388 // Private Data. |
352 | 389 |
353 // External data request callback that is passed to decoder. | 390 // External data request callback that is passed to decoder. |
354 base::Closure external_request_data_cb_; | 391 base::Closure external_request_data_cb_; |
355 | 392 |
356 // These notifications are called on corresponding conditions. | 393 // These notifications are called on corresponding conditions. |
357 base::Closure prefetch_done_cb_; | 394 base::Closure prefetch_done_cb_; |
358 base::Closure starvation_cb_; | 395 base::Closure starvation_cb_; |
359 base::Closure preroll_done_cb_; | 396 base::Closure preroll_done_cb_; |
| 397 base::Closure decoder_drained_cb_; |
360 base::Closure stop_done_cb_; | 398 base::Closure stop_done_cb_; |
361 base::Closure error_cb_; | 399 base::Closure error_cb_; |
362 | 400 |
363 // Data request callback that is posted by decoder internally. | 401 // Data request callback that is posted by decoder internally. |
364 base::Closure request_data_cb_; | 402 base::Closure request_data_cb_; |
365 | 403 |
366 // Callback used to post OnCodecError method. | 404 // Callback used to post OnCodecError method. |
367 base::Closure internal_error_cb_; | 405 base::Closure internal_error_cb_; |
368 | 406 |
369 // Callback for posting OnPrerollDone method. | 407 // Callback for posting OnPrerollDone method. |
370 base::Closure internal_preroll_done_cb_; | 408 base::Closure internal_preroll_done_cb_; |
371 | 409 |
372 // Internal state. | 410 // Internal state. |
373 DecoderState state_; | 411 DecoderState state_; |
374 mutable base::Lock state_lock_; | 412 mutable base::Lock state_lock_; |
375 | 413 |
376 // 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. |
377 base::TimeDelta preroll_timestamp_; | 415 base::TimeDelta preroll_timestamp_; |
378 | 416 |
379 // Indicates that playback should start with preroll. | 417 // The preroll mode. If not |kNoPreroll|, the playback should start with |
380 bool needs_preroll_; | 418 // preroll. |
| 419 PrerollMode preroll_mode_; |
381 | 420 |
382 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 421 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
383 bool eos_enqueued_; | 422 bool eos_enqueued_; |
384 | 423 |
385 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 424 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
386 bool completed_; | 425 bool completed_; |
387 | 426 |
388 // Flag to ensure we post last frame notification once. | 427 // Flag to ensure we post last frame notification once. |
389 bool last_frame_posted_; | 428 bool last_frame_posted_; |
390 | 429 |
(...skipping 10 matching lines...) Expand all Loading... |
401 | 440 |
402 // NOTE: Weak pointers must be invalidated before all other member variables. | 441 // NOTE: Weak pointers must be invalidated before all other member variables. |
403 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 442 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
404 | 443 |
405 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 444 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
406 }; | 445 }; |
407 | 446 |
408 } // namespace media | 447 } // namespace media |
409 | 448 |
410 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 449 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |