OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "media/filters/decoder_stream.h" | 5 #include "media/filters/decoder_stream.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 template <> | 40 template <> |
41 const char* GetTraceString<DemuxerStream::AUDIO>() { | 41 const char* GetTraceString<DemuxerStream::AUDIO>() { |
42 return "DecoderStream<AUDIO>::Decode"; | 42 return "DecoderStream<AUDIO>::Decode"; |
43 } | 43 } |
44 | 44 |
45 template <DemuxerStream::Type StreamType> | 45 template <DemuxerStream::Type StreamType> |
46 DecoderStream<StreamType>::DecoderStream( | 46 DecoderStream<StreamType>::DecoderStream( |
47 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 47 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
48 ScopedVector<Decoder> decoders, | 48 ScopedVector<Decoder> decoders, |
49 const scoped_refptr<MediaLog>& media_log) | 49 const scoped_refptr<MediaLog>& media_log) |
50 : task_runner_(task_runner), | 50 : traits_(media_log), |
| 51 task_runner_(task_runner), |
51 media_log_(media_log), | 52 media_log_(media_log), |
52 state_(STATE_UNINITIALIZED), | 53 state_(STATE_UNINITIALIZED), |
53 stream_(NULL), | 54 stream_(NULL), |
54 decoder_selector_(new DecoderSelector<StreamType>(task_runner, | 55 decoder_selector_(new DecoderSelector<StreamType>(task_runner, |
55 std::move(decoders), | 56 std::move(decoders), |
56 media_log)), | 57 media_log)), |
57 decoded_frames_since_fallback_(0), | 58 decoded_frames_since_fallback_(0), |
58 active_splice_(false), | 59 active_splice_(false), |
59 decoding_eos_(false), | 60 decoding_eos_(false), |
60 pending_decode_requests_(0), | 61 pending_decode_requests_(0), |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 DCHECK(task_runner_->BelongsToCurrentThread()); | 104 DCHECK(task_runner_->BelongsToCurrentThread()); |
104 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 105 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
105 DCHECK(init_cb_.is_null()); | 106 DCHECK(init_cb_.is_null()); |
106 DCHECK(!init_cb.is_null()); | 107 DCHECK(!init_cb.is_null()); |
107 | 108 |
108 statistics_cb_ = statistics_cb; | 109 statistics_cb_ = statistics_cb; |
109 init_cb_ = init_cb; | 110 init_cb_ = init_cb; |
110 waiting_for_decryption_key_cb_ = waiting_for_decryption_key_cb; | 111 waiting_for_decryption_key_cb_ = waiting_for_decryption_key_cb; |
111 stream_ = stream; | 112 stream_ = stream; |
112 | 113 |
| 114 traits_.OnStreamReset(stream_); |
| 115 |
113 state_ = STATE_INITIALIZING; | 116 state_ = STATE_INITIALIZING; |
114 SelectDecoder(cdm_context); | 117 SelectDecoder(cdm_context); |
115 } | 118 } |
116 | 119 |
117 template <DemuxerStream::Type StreamType> | 120 template <DemuxerStream::Type StreamType> |
118 void DecoderStream<StreamType>::Read(const ReadCB& read_cb) { | 121 void DecoderStream<StreamType>::Read(const ReadCB& read_cb) { |
119 FUNCTION_DVLOG(2); | 122 FUNCTION_DVLOG(2); |
120 DCHECK(task_runner_->BelongsToCurrentThread()); | 123 DCHECK(task_runner_->BelongsToCurrentThread()); |
121 DCHECK(state_ != STATE_UNINITIALIZED && state_ != STATE_INITIALIZING) | 124 DCHECK(state_ != STATE_UNINITIALIZED && state_ != STATE_INITIALIZING) |
122 << state_; | 125 << state_; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 161 |
159 reset_cb_ = closure; | 162 reset_cb_ = closure; |
160 | 163 |
161 if (!read_cb_.is_null()) { | 164 if (!read_cb_.is_null()) { |
162 task_runner_->PostTask(FROM_HERE, | 165 task_runner_->PostTask(FROM_HERE, |
163 base::Bind(base::ResetAndReturn(&read_cb_), ABORTED, | 166 base::Bind(base::ResetAndReturn(&read_cb_), ABORTED, |
164 scoped_refptr<Output>())); | 167 scoped_refptr<Output>())); |
165 } | 168 } |
166 | 169 |
167 ready_outputs_.clear(); | 170 ready_outputs_.clear(); |
| 171 traits_.OnStreamReset(stream_); |
168 | 172 |
169 // It's possible to have received a DECODE_ERROR and entered STATE_ERROR right | 173 // It's possible to have received a DECODE_ERROR and entered STATE_ERROR right |
170 // before a Reset() is executed. If we are still waiting for a demuxer read, | 174 // before a Reset() is executed. If we are still waiting for a demuxer read, |
171 // OnBufferReady() will handle the reset callback. | 175 // OnBufferReady() will handle the reset callback. |
172 // See crbug.com/597605 and crbug.com/607454. | 176 // See crbug.com/597605 and crbug.com/607454. |
173 if (state_ == STATE_ERROR && !pending_demuxer_read_) { | 177 if (state_ == STATE_ERROR && !pending_demuxer_read_) { |
174 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&reset_cb_)); | 178 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&reset_cb_)); |
175 return; | 179 return; |
176 } | 180 } |
177 | 181 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 template <DemuxerStream::Type StreamType> | 242 template <DemuxerStream::Type StreamType> |
239 base::TimeDelta DecoderStream<StreamType>::AverageDuration() const { | 243 base::TimeDelta DecoderStream<StreamType>::AverageDuration() const { |
240 DCHECK(task_runner_->BelongsToCurrentThread()); | 244 DCHECK(task_runner_->BelongsToCurrentThread()); |
241 return duration_tracker_.count() ? duration_tracker_.Average() | 245 return duration_tracker_.count() ? duration_tracker_.Average() |
242 : base::TimeDelta(); | 246 : base::TimeDelta(); |
243 } | 247 } |
244 | 248 |
245 template <DemuxerStream::Type StreamType> | 249 template <DemuxerStream::Type StreamType> |
246 void DecoderStream<StreamType>::SelectDecoder(CdmContext* cdm_context) { | 250 void DecoderStream<StreamType>::SelectDecoder(CdmContext* cdm_context) { |
247 decoder_selector_->SelectDecoder( | 251 decoder_selector_->SelectDecoder( |
248 stream_, cdm_context, | 252 &traits_, stream_, cdm_context, |
249 base::Bind(&DecoderStream<StreamType>::OnDecoderSelected, | 253 base::Bind(&DecoderStream<StreamType>::OnDecoderSelected, |
250 weak_factory_.GetWeakPtr()), | 254 weak_factory_.GetWeakPtr()), |
251 base::Bind(&DecoderStream<StreamType>::OnDecodeOutputReady, | 255 base::Bind(&DecoderStream<StreamType>::OnDecodeOutputReady, |
252 fallback_weak_factory_.GetWeakPtr()), | 256 fallback_weak_factory_.GetWeakPtr()), |
253 waiting_for_decryption_key_cb_); | 257 waiting_for_decryption_key_cb_); |
254 } | 258 } |
255 | 259 |
256 template <DemuxerStream::Type StreamType> | 260 template <DemuxerStream::Type StreamType> |
257 void DecoderStream<StreamType>::OnDecoderSelected( | 261 void DecoderStream<StreamType>::OnDecoderSelected( |
258 std::unique_ptr<Decoder> selected_decoder, | 262 std::unique_ptr<Decoder> selected_decoder, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 | 357 |
354 template <DemuxerStream::Type StreamType> | 358 template <DemuxerStream::Type StreamType> |
355 void DecoderStream<StreamType>::DecodeInternal( | 359 void DecoderStream<StreamType>::DecodeInternal( |
356 const scoped_refptr<DecoderBuffer>& buffer) { | 360 const scoped_refptr<DecoderBuffer>& buffer) { |
357 FUNCTION_DVLOG(2); | 361 FUNCTION_DVLOG(2); |
358 DCHECK(state_ == STATE_NORMAL || state_ == STATE_FLUSHING_DECODER) << state_; | 362 DCHECK(state_ == STATE_NORMAL || state_ == STATE_FLUSHING_DECODER) << state_; |
359 DCHECK_LT(pending_decode_requests_, GetMaxDecodeRequests()); | 363 DCHECK_LT(pending_decode_requests_, GetMaxDecodeRequests()); |
360 DCHECK(reset_cb_.is_null()); | 364 DCHECK(reset_cb_.is_null()); |
361 DCHECK(buffer.get()); | 365 DCHECK(buffer.get()); |
362 | 366 |
| 367 traits_.OnDecode(buffer); |
| 368 |
363 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size(); | 369 int buffer_size = buffer->end_of_stream() ? 0 : buffer->data_size(); |
364 | 370 |
365 TRACE_EVENT_ASYNC_BEGIN2( | 371 TRACE_EVENT_ASYNC_BEGIN2( |
366 "media", GetTraceString<StreamType>(), this, "key frame", | 372 "media", GetTraceString<StreamType>(), this, "key frame", |
367 !buffer->end_of_stream() && buffer->is_key_frame(), "timestamp (ms)", | 373 !buffer->end_of_stream() && buffer->is_key_frame(), "timestamp (ms)", |
368 !buffer->end_of_stream() ? buffer->timestamp().InMilliseconds() : 0); | 374 !buffer->end_of_stream() ? buffer->timestamp().InMilliseconds() : 0); |
369 | 375 |
370 if (buffer->end_of_stream()) | 376 if (buffer->end_of_stream()) |
371 decoding_eos_ = true; | 377 decoding_eos_ = true; |
372 else if (buffer->duration() != kNoTimestamp()) | 378 else if (buffer->duration() != kNoTimestamp()) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 case DecodeStatus::DECODE_ERROR: | 424 case DecodeStatus::DECODE_ERROR: |
419 if (!decoded_frames_since_fallback_) { | 425 if (!decoded_frames_since_fallback_) { |
420 pending_decode_requests_ = 0; | 426 pending_decode_requests_ = 0; |
421 | 427 |
422 // Prevent all pending decode requests and outputs form those requests | 428 // Prevent all pending decode requests and outputs form those requests |
423 // from being called back. | 429 // from being called back. |
424 fallback_weak_factory_.InvalidateWeakPtrs(); | 430 fallback_weak_factory_.InvalidateWeakPtrs(); |
425 | 431 |
426 state_ = STATE_REINITIALIZING_DECODER; | 432 state_ = STATE_REINITIALIZING_DECODER; |
427 decoder_selector_->SelectDecoder( | 433 decoder_selector_->SelectDecoder( |
428 stream_, nullptr, | 434 &traits_, stream_, nullptr, |
429 base::Bind(&DecoderStream<StreamType>::OnDecoderSelected, | 435 base::Bind(&DecoderStream<StreamType>::OnDecoderSelected, |
430 weak_factory_.GetWeakPtr()), | 436 weak_factory_.GetWeakPtr()), |
431 base::Bind(&DecoderStream<StreamType>::OnDecodeOutputReady, | 437 base::Bind(&DecoderStream<StreamType>::OnDecodeOutputReady, |
432 fallback_weak_factory_.GetWeakPtr()), | 438 fallback_weak_factory_.GetWeakPtr()), |
433 waiting_for_decryption_key_cb_); | 439 waiting_for_decryption_key_cb_); |
434 return; | 440 return; |
435 } | 441 } |
436 state_ = STATE_ERROR; | 442 state_ = STATE_ERROR; |
437 MEDIA_LOG(ERROR, media_log_) << GetStreamTypeString() << " decode error"; | 443 MEDIA_LOG(ERROR, media_log_) << GetStreamTypeString() << " decode error"; |
438 ready_outputs_.clear(); | 444 ready_outputs_.clear(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 if (state_ == STATE_ERROR) { | 487 if (state_ == STATE_ERROR) { |
482 DCHECK(read_cb_.is_null()); | 488 DCHECK(read_cb_.is_null()); |
483 return; | 489 return; |
484 } | 490 } |
485 | 491 |
486 // Drop decoding result if Reset() was called during decoding. | 492 // Drop decoding result if Reset() was called during decoding. |
487 // The resetting process will be handled when the decoder is reset. | 493 // The resetting process will be handled when the decoder is reset. |
488 if (!reset_cb_.is_null()) | 494 if (!reset_cb_.is_null()) |
489 return; | 495 return; |
490 | 496 |
| 497 traits_.OnDecodeDone(output); |
| 498 |
491 ++decoded_frames_since_fallback_; | 499 ++decoded_frames_since_fallback_; |
492 | 500 |
493 // |decoder_| sucessfully decoded a frame. No need to keep buffers for a | 501 // |decoder_| sucessfully decoded a frame. No need to keep buffers for a |
494 // fallback decoder. | 502 // fallback decoder. |
495 // Note: |fallback_buffers_| might still have buffers, and we will keep | 503 // Note: |fallback_buffers_| might still have buffers, and we will keep |
496 // reading from there before requesting new buffers from |stream_|. | 504 // reading from there before requesting new buffers from |stream_|. |
497 pending_buffers_.clear(); | 505 pending_buffers_.clear(); |
498 | 506 |
499 if (!read_cb_.is_null()) { | 507 if (!read_cb_.is_null()) { |
500 // If |ready_outputs_| was non-empty, the read would have already been | 508 // If |ready_outputs_| was non-empty, the read would have already been |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 | 689 |
682 template <DemuxerStream::Type StreamType> | 690 template <DemuxerStream::Type StreamType> |
683 void DecoderStream<StreamType>::ReinitializeDecoder() { | 691 void DecoderStream<StreamType>::ReinitializeDecoder() { |
684 FUNCTION_DVLOG(2); | 692 FUNCTION_DVLOG(2); |
685 DCHECK(task_runner_->BelongsToCurrentThread()); | 693 DCHECK(task_runner_->BelongsToCurrentThread()); |
686 DCHECK_EQ(state_, STATE_FLUSHING_DECODER); | 694 DCHECK_EQ(state_, STATE_FLUSHING_DECODER); |
687 DCHECK_EQ(pending_decode_requests_, 0); | 695 DCHECK_EQ(pending_decode_requests_, 0); |
688 | 696 |
689 state_ = STATE_REINITIALIZING_DECODER; | 697 state_ = STATE_REINITIALIZING_DECODER; |
690 // Decoders should not need a new CDM during reinitialization. | 698 // Decoders should not need a new CDM during reinitialization. |
691 DecoderStreamTraits<StreamType>::InitializeDecoder( | 699 traits_.InitializeDecoder( |
692 decoder_.get(), stream_, nullptr, | 700 decoder_.get(), stream_, nullptr, |
693 base::Bind(&DecoderStream<StreamType>::OnDecoderReinitialized, | 701 base::Bind(&DecoderStream<StreamType>::OnDecoderReinitialized, |
694 weak_factory_.GetWeakPtr()), | 702 weak_factory_.GetWeakPtr()), |
695 base::Bind(&DecoderStream<StreamType>::OnDecodeOutputReady, | 703 base::Bind(&DecoderStream<StreamType>::OnDecodeOutputReady, |
696 fallback_weak_factory_.GetWeakPtr())); | 704 fallback_weak_factory_.GetWeakPtr())); |
697 } | 705 } |
698 | 706 |
699 template <DemuxerStream::Type StreamType> | 707 template <DemuxerStream::Type StreamType> |
700 void DecoderStream<StreamType>::OnDecoderReinitialized(bool success) { | 708 void DecoderStream<StreamType>::OnDecoderReinitialized(bool success) { |
701 FUNCTION_DVLOG(2); | 709 FUNCTION_DVLOG(2); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 } | 789 } |
782 | 790 |
783 // The resetting process will be continued in OnDecoderReinitialized(). | 791 // The resetting process will be continued in OnDecoderReinitialized(). |
784 ReinitializeDecoder(); | 792 ReinitializeDecoder(); |
785 } | 793 } |
786 | 794 |
787 template class DecoderStream<DemuxerStream::VIDEO>; | 795 template class DecoderStream<DemuxerStream::VIDEO>; |
788 template class DecoderStream<DemuxerStream::AUDIO>; | 796 template class DecoderStream<DemuxerStream::AUDIO>; |
789 | 797 |
790 } // namespace media | 798 } // namespace media |
OLD | NEW |