1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "base/callback_helpers.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/test/gmock_callback_support.h"
15 #include "base/test/mock_callback.h"
16 #include "base/test/task_environment.h"
17 #include "build/build_config.h"
18 #include "media/base/fake_demuxer_stream.h"
19 #include "media/base/mock_filters.h"
20 #include "media/base/mock_media_log.h"
21 #include "media/base/test_helpers.h"
22 #include "media/base/timestamp_constants.h"
23 #include "media/filters/decoder_stream.h"
24 #include "media/filters/fake_video_decoder.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 #if !BUILDFLAG(IS_ANDROID)
28 #include "media/filters/decrypting_video_decoder.h"
33 using ::base::test::RunCallback;
34 using ::base::test::RunOnceCallback;
36 using ::testing::AnyNumber;
37 using ::testing::Assign;
38 using ::testing::HasSubstr;
39 using ::testing::InSequence;
40 using ::testing::Invoke;
41 using ::testing::InvokeWithoutArgs;
42 using ::testing::NiceMock;
43 using ::testing::Return;
44 using ::testing::SaveArg;
45 using ::testing::StrictMock;
50 const int kNumConfigs = 4;
51 const int kNumBuffersInOneConfig = 5;
52 constexpr base::TimeDelta kPrepareDelay = base::Milliseconds(5);
54 static int GetDecoderId(int i) {
58 DecoderPriority MockDecoderPriority(const VideoDecoderConfig& config,
59 const VideoDecoder& decoder) {
60 auto const at_or_above_cutoff = config.visible_rect().height() >=
61 TestVideoConfig::LargeCodedSize().height();
62 return at_or_above_cutoff == decoder.IsPlatformDecoder()
63 ? DecoderPriority::kNormal
64 : DecoderPriority::kDeprioritized;
69 struct VideoDecoderStreamTestParams {
70 VideoDecoderStreamTestParams(bool is_encrypted,
74 int parallel_decoding)
75 : is_encrypted(is_encrypted),
76 has_decryptor(has_decryptor),
77 has_prepare(has_prepare),
78 decoding_delay(decoding_delay),
79 parallel_decoding(parallel_decoding) {}
85 int parallel_decoding;
88 class VideoDecoderStreamTest
89 : public testing::Test,
90 public testing::WithParamInterface<VideoDecoderStreamTestParams> {
92 VideoDecoderStreamTest()
93 : is_initialized_(false),
94 num_decoded_frames_(0),
95 pending_initialize_(false),
97 pending_reset_(false),
99 num_decoded_bytes_unreported_(0),
101 video_decoder_stream_ = std::make_unique<VideoDecoderStream>(
102 std::make_unique<VideoDecoderStream::StreamTraits>(&media_log_),
103 task_environment_.GetMainThreadTaskRunner(),
104 base::BindRepeating(&VideoDecoderStreamTest::CreateVideoDecodersForTest,
105 base::Unretained(this)),
107 video_decoder_stream_->set_decoder_change_observer(base::BindRepeating(
108 &VideoDecoderStreamTest::OnDecoderChanged, base::Unretained(this)));
109 video_decoder_stream_
110 ->GetDecoderSelectorForTesting(base::PassKey<VideoDecoderStreamTest>())
111 .OverrideDecoderPriorityCBForTesting(
112 base::BindRepeating(MockDecoderPriority));
113 if (GetParam().has_prepare) {
114 video_decoder_stream_->SetPrepareCB(base::BindRepeating(
115 &VideoDecoderStreamTest::PrepareFrame, base::Unretained(this)));
118 if (GetParam().is_encrypted && GetParam().has_decryptor) {
119 decryptor_ = std::make_unique<NiceMock<MockDecryptor>>();
121 // Decryptor can only decrypt (not decrypt-and-decode) so that
122 // DecryptingDemuxerStream will be used.
123 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
124 .WillRepeatedly(RunOnceCallback<1>(false));
125 EXPECT_CALL(*decryptor_, Decrypt(_, _, _))
126 .WillRepeatedly(Invoke(this, &VideoDecoderStreamTest::Decrypt));
129 if (GetParam().is_encrypted) {
130 cdm_context_ = std::make_unique<StrictMock<MockCdmContext>>();
132 EXPECT_CALL(*cdm_context_, RegisterEventCB(_)).Times(AnyNumber());
133 EXPECT_CALL(*cdm_context_, GetDecryptor())
134 .WillRepeatedly(Return(decryptor_.get()));
137 // Covering most MediaLog messages for now.
138 // TODO(wolenetz/xhwang): Fix tests to have better MediaLog checking.
139 EXPECT_MEDIA_LOG(HasSubstr("video")).Times(AnyNumber());
140 EXPECT_MEDIA_LOG(HasSubstr("Video")).Times(AnyNumber());
141 EXPECT_MEDIA_LOG(HasSubstr("audio")).Times(AnyNumber());
142 EXPECT_MEDIA_LOG(HasSubstr("Audio")).Times(AnyNumber());
143 EXPECT_MEDIA_LOG(HasSubstr("decryptor")).Times(AnyNumber());
146 VideoDecoderStreamTest(const VideoDecoderStreamTest&) = delete;
147 VideoDecoderStreamTest& operator=(const VideoDecoderStreamTest&) = delete;
149 ~VideoDecoderStreamTest() {
150 // Check that the pipeline statistics callback was fired correctly.
151 EXPECT_EQ(num_decoded_bytes_unreported_, 0);
153 is_initialized_ = false;
155 video_decoder_stream_.reset();
156 base::RunLoop().RunUntilIdle();
158 DCHECK(!pending_initialize_);
159 DCHECK(!pending_read_);
160 DCHECK(!pending_reset_);
161 DCHECK(!pending_stop_);
164 void CreateDemuxerStream(gfx::Size start_size, gfx::Vector2dF size_delta) {
165 DCHECK(!demuxer_stream_);
166 demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
167 kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted,
168 start_size, size_delta);
171 void PrepareFrame(scoped_refptr<VideoFrame> frame,
172 VideoDecoderStream::OutputReadyCB output_ready_cb) {
173 // Simulate some delay in return of the output.
174 task_environment_.GetMainThreadTaskRunner()->PostTask(
176 base::BindOnce(std::move(output_ready_cb), std::move(frame)));
179 void PrepareFrameWithDelay(
180 scoped_refptr<VideoFrame> frame,
181 VideoDecoderStream::OutputReadyCB output_ready_cb) {
182 task_environment_.FastForwardBy(kPrepareDelay);
183 task_environment_.GetMainThreadTaskRunner()->PostTask(
185 base::BindOnce(std::move(output_ready_cb), std::move(frame)));
188 void OnBytesDecoded(int count) { num_decoded_bytes_unreported_ += count; }
190 // Callback to create a list of decoders for the DecoderSelector to select
191 // from. Decoder selection happens
192 // - on the initial selection in Initialize(),
193 // - on decoder reinitialization failure, which can be simulated by calling
194 // decoder_->SimulateFailureToInit(), and
195 // - on decode error of the first buffer, which can be simulated by calling
196 // decoder_->SimulateError() before reading the first frame.
197 std::vector<std::unique_ptr<VideoDecoder>> CreateVideoDecodersForTest() {
198 // Previously decoders could have been destroyed on decoder reselection.
201 // Provide 3 decoders to test fallback cases.
202 // TODO(xhwang): We should test the case where only certain decoder
203 // supports encrypted streams. Currently this is hard to test because we use
204 // parameterized tests which need to pass in all combinations.
205 std::vector<std::unique_ptr<VideoDecoder>> decoders;
207 #if !BUILDFLAG(IS_ANDROID)
208 // Note this is _not_ inserted into |decoders_| below, so we don't need to
209 // adjust the indices used below to compensate.
210 decoders.push_back(std::make_unique<DecryptingVideoDecoder>(
211 task_environment_.GetMainThreadTaskRunner(), &media_log_));
214 for (int i = 0; i < 3; ++i) {
215 auto decoder = std::make_unique<FakeVideoDecoder>(
216 GetDecoderId(i), GetParam().decoding_delay,
217 GetParam().parallel_decoding,
218 base::BindRepeating(&VideoDecoderStreamTest::OnBytesDecoded,
219 base::Unretained(this)));
221 if (GetParam().is_encrypted && !GetParam().has_decryptor)
222 decoder->EnableEncryptedConfigSupport();
224 // Keep a reference so we can change the behavior of each decoder.
225 decoders_.push_back(decoder->GetWeakPtr());
227 decoders.push_back(std::move(decoder));
230 for (const auto i : decoder_indices_to_fail_init_)
231 decoders_[i]->SimulateFailureToInit();
233 for (const auto i : decoder_indices_to_hold_init_)
234 decoders_[i]->HoldNextInit();
236 for (const auto i : decoder_indices_to_hold_decode_)
237 decoders_[i]->HoldDecode();
239 for (const auto i : platform_decoder_indices_)
240 decoders_[i]->SetIsPlatformDecoder(true);
245 void ClearDecoderInitExpectations() {
246 decoder_indices_to_fail_init_.clear();
247 decoder_indices_to_hold_init_.clear();
248 decoder_indices_to_hold_decode_.clear();
249 platform_decoder_indices_.clear();
252 // On next decoder selection, fail initialization on decoders specified by
253 // |decoder_indices|.
254 void FailDecoderInitOnSelection(std::vector<int> decoder_indices) {
255 decoder_indices_to_fail_init_ = std::move(decoder_indices);
256 for (int i : decoder_indices_to_fail_init_) {
257 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
258 decoders_[i]->SimulateFailureToInit();
262 // On next decoder selection, hold initialization on decoders specified by
263 // |decoder_indices|.
264 void HoldDecoderInitOnSelection(std::vector<int> decoder_indices) {
265 decoder_indices_to_hold_init_ = std::move(decoder_indices);
266 for (int i : decoder_indices_to_hold_init_) {
267 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
268 decoders_[i]->HoldNextInit();
272 // After next decoder selection, hold decode on decoders specified by
273 // |decoder_indices|. This is needed because after decoder selection decode
274 // may be resumed immediately and it'll be too late to hold decode then.
275 void HoldDecodeAfterSelection(std::vector<int> decoder_indices) {
276 decoder_indices_to_hold_decode_ = std::move(decoder_indices);
277 for (int i : decoder_indices_to_hold_decode_) {
278 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
279 decoders_[i]->HoldDecode();
283 void EnablePlatformDecoders(std::vector<int> decoder_indices) {
284 platform_decoder_indices_ = std::move(decoder_indices);
285 for (int i : platform_decoder_indices_) {
286 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
287 decoders_[i]->SetIsPlatformDecoder(true);
291 // Updates the |decoder_| currently being used by VideoDecoderStream.
292 void OnDecoderChanged(VideoDecoder* decoder) {
298 // Ensure there's a media log created whenever selecting a decoder.
299 EXPECT_MEDIA_LOG(HasSubstr("for video decoding, config"));
300 decoder_ = static_cast<FakeVideoDecoder*>(decoder);
301 ASSERT_TRUE(decoder_->GetDecoderId() == GetDecoderId(0) ||
302 decoder_->GetDecoderId() == GetDecoderId(1) ||
303 decoder_->GetDecoderId() == GetDecoderId(2));
306 MOCK_METHOD1(OnWaiting, void(WaitingReason));
308 void OnStatistics(const PipelineStatistics& statistics) {
309 num_decoded_bytes_unreported_ -= statistics.video_bytes_decoded;
312 void OnInitialized(bool success) {
313 DCHECK(!pending_read_);
314 DCHECK(!pending_reset_);
315 DCHECK(pending_initialize_);
316 pending_initialize_ = false;
318 is_initialized_ = success;
324 if (!demuxer_stream_) {
325 demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
326 kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted);
329 pending_initialize_ = true;
330 video_decoder_stream_->Initialize(
331 demuxer_stream_.get(),
332 base::BindOnce(&VideoDecoderStreamTest::OnInitialized,
333 base::Unretained(this)),
335 base::BindRepeating(&VideoDecoderStreamTest::OnStatistics,
336 base::Unretained(this)),
337 base::BindRepeating(&VideoDecoderStreamTest::OnWaiting,
338 base::Unretained(this)));
340 EXPECT_MEDIA_LOG(HasSubstr("video")).Times(AnyNumber());
341 EXPECT_MEDIA_LOG(HasSubstr("Video")).Times(AnyNumber());
342 EXPECT_MEDIA_LOG(HasSubstr("audio")).Times(AnyNumber());
343 EXPECT_MEDIA_LOG(HasSubstr("Audio")).Times(AnyNumber());
344 base::RunLoop().RunUntilIdle();
347 // Fake Decrypt() function used by DecryptingDemuxerStream. It does nothing
348 // but removes the DecryptConfig to make the buffer unencrypted.
349 void Decrypt(Decryptor::StreamType stream_type,
350 scoped_refptr<DecoderBuffer> encrypted,
351 Decryptor::DecryptCB decrypt_cb) {
352 DCHECK(encrypted->decrypt_config());
354 std::move(decrypt_cb).Run(Decryptor::kNoKey, nullptr);
358 DCHECK_EQ(stream_type, Decryptor::kVideo);
359 scoped_refptr<DecoderBuffer> decrypted =
360 DecoderBuffer::CopyFrom(encrypted->data(), encrypted->data_size());
361 if (encrypted->is_key_frame())
362 decrypted->set_is_key_frame(true);
363 decrypted->set_timestamp(encrypted->timestamp());
364 decrypted->set_duration(encrypted->duration());
365 std::move(decrypt_cb).Run(Decryptor::kSuccess, decrypted);
368 // Callback for VideoDecoderStream::Read().
369 void FrameReady(VideoDecoderStream::ReadResult result) {
370 DCHECK(pending_read_);
371 last_read_status_code_ = result.code();
372 scoped_refptr<VideoFrame> frame =
373 last_read_status_code_ == DecoderStatus::Codes::kOk
374 ? std::move(result).value()
377 if (frame && !frame->metadata().end_of_stream) {
378 EXPECT_EQ(*frame->metadata().frame_duration, demuxer_stream_->duration());
380 num_decoded_frames_++;
382 pending_read_ = false;
386 DCHECK(!pending_read_);
387 DCHECK(pending_reset_);
388 pending_reset_ = false;
391 void ReadOneFrame() {
392 frame_read_ = nullptr;
393 pending_read_ = true;
394 video_decoder_stream_->Read(base::BindOnce(
395 &VideoDecoderStreamTest::FrameReady, base::Unretained(this)));
396 base::RunLoop().RunUntilIdle();
399 void ReadUntilPending() {
402 } while (!pending_read_);
405 void ReadAllFrames(int expected_decoded_frames) {
406 // Reading all frames reinitializes the demuxer.
409 } while (frame_read_.get() && !frame_read_->metadata().end_of_stream);
411 DCHECK_EQ(expected_decoded_frames, num_decoded_frames_);
414 void ReadAllFrames() {
415 // No frames should have been dropped.
416 ReadAllFrames(kNumConfigs * kNumBuffersInOneConfig);
422 DEMUXER_READ_CONFIG_CHANGE,
429 void EnterPendingState(PendingState state) {
430 DCHECK_NE(state, NOT_PENDING);
432 case DEMUXER_READ_NORMAL:
433 demuxer_stream_->HoldNextRead();
437 case DEMUXER_READ_CONFIG_CHANGE:
438 demuxer_stream_->HoldNextConfigChangeRead();
442 case DECRYPTOR_NO_KEY:
443 if (GetParam().is_encrypted && GetParam().has_decryptor) {
444 EXPECT_MEDIA_LOG(HasSubstr("no key for key ID"));
445 EXPECT_CALL(*this, OnWaiting(WaitingReason::kNoDecryptionKey));
452 decoder_->HoldNextInit();
457 decoder_->HoldDecode();
462 decoder_->HoldNextReset();
463 pending_reset_ = true;
464 video_decoder_stream_->Reset(base::BindOnce(
465 &VideoDecoderStreamTest::OnReset, base::Unretained(this)));
466 base::RunLoop().RunUntilIdle();
475 void SatisfyPendingCallback(PendingState state) {
476 DCHECK_NE(state, NOT_PENDING);
478 case DEMUXER_READ_CONFIG_CHANGE:
479 EXPECT_MEDIA_LOG(HasSubstr("decoder config changed"))
480 .Times(testing::AtLeast(1));
482 case DEMUXER_READ_NORMAL:
483 demuxer_stream_->SatisfyRead();
486 // This is only interesting to test during VideoDecoderStream destruction.
487 // There's no need to satisfy a callback.
488 case DECRYPTOR_NO_KEY:
493 decoder_->SatisfyInit();
497 decoder_->SatisfyDecode();
501 decoder_->SatisfyReset();
509 base::RunLoop().RunUntilIdle();
513 EnterPendingState(DECODER_DECODE);
514 SatisfyPendingCallback(DECODER_DECODE);
518 EnterPendingState(DECODER_RESET);
519 SatisfyPendingCallback(DECODER_RESET);
522 void ReadUntilDecoderReinitialized() {
523 EnterPendingState(DECODER_REINIT);
524 SatisfyPendingCallback(DECODER_REINIT);
527 base::test::SingleThreadTaskEnvironment task_environment_{
528 base::test::TaskEnvironment::TimeSource::MOCK_TIME};
530 StrictMock<MockMediaLog> media_log_;
531 std::unique_ptr<VideoDecoderStream> video_decoder_stream_;
532 std::unique_ptr<FakeDemuxerStream> demuxer_stream_;
533 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
535 // Use NiceMock since we don't care about most of calls on the decryptor.
536 std::unique_ptr<NiceMock<MockDecryptor>> decryptor_;
538 // References to the list of decoders to be select from by DecoderSelector.
539 // Three decoders are needed to test that decoder fallback can occur more than
540 // once on a config change. They are owned by |video_decoder_stream_|.
541 std::vector<base::WeakPtr<FakeVideoDecoder>> decoders_;
543 std::vector<int> decoder_indices_to_fail_init_;
544 std::vector<int> decoder_indices_to_hold_init_;
545 std::vector<int> decoder_indices_to_hold_decode_;
546 std::vector<int> platform_decoder_indices_;
548 // The current decoder used by |video_decoder_stream_|.
549 raw_ptr<FakeVideoDecoder> decoder_ = nullptr;
551 bool is_initialized_;
552 int num_decoded_frames_;
553 bool pending_initialize_;
557 int num_decoded_bytes_unreported_;
558 scoped_refptr<VideoFrame> frame_read_;
559 DecoderStatus::Codes last_read_status_code_;
561 // Decryptor has no key to decrypt a frame.
565 INSTANTIATE_TEST_SUITE_P(
567 VideoDecoderStreamTest,
568 ::testing::Values(VideoDecoderStreamTestParams(false, false, false, 0, 1),
569 VideoDecoderStreamTestParams(false, false, false, 3, 1),
570 VideoDecoderStreamTestParams(false, false, false, 7, 1),
571 VideoDecoderStreamTestParams(false, false, true, 0, 1),
572 VideoDecoderStreamTestParams(false, false, true, 3, 1)));
574 INSTANTIATE_TEST_SUITE_P(
575 EncryptedWithDecryptor,
576 VideoDecoderStreamTest,
577 ::testing::Values(VideoDecoderStreamTestParams(true, true, false, 7, 1),
578 VideoDecoderStreamTestParams(true, true, true, 7, 1)));
580 INSTANTIATE_TEST_SUITE_P(
581 EncryptedWithoutDecryptor,
582 VideoDecoderStreamTest,
583 ::testing::Values(VideoDecoderStreamTestParams(true, false, false, 7, 1),
584 VideoDecoderStreamTestParams(true, false, true, 7, 1)));
586 INSTANTIATE_TEST_SUITE_P(
588 VideoDecoderStreamTest,
589 ::testing::Values(VideoDecoderStreamTestParams(false, false, false, 0, 3),
590 VideoDecoderStreamTestParams(false, false, false, 2, 3),
591 VideoDecoderStreamTestParams(false, false, true, 0, 3),
592 VideoDecoderStreamTestParams(false, false, true, 2, 3)));
594 TEST_P(VideoDecoderStreamTest, CanReadWithoutStallingAtAnyTime) {
595 ASSERT_FALSE(video_decoder_stream_->CanReadWithoutStalling());
598 TEST_P(VideoDecoderStreamTest, Initialization) {
600 EXPECT_TRUE(is_initialized_);
603 TEST_P(VideoDecoderStreamTest, AllDecoderInitializationFails) {
604 FailDecoderInitOnSelection({0, 1, 2});
606 EXPECT_FALSE(is_initialized_);
609 TEST_P(VideoDecoderStreamTest, PartialDecoderInitializationFails) {
610 FailDecoderInitOnSelection({0, 1});
612 EXPECT_TRUE(is_initialized_);
615 TEST_P(VideoDecoderStreamTest, ReadOneFrame) {
620 TEST_P(VideoDecoderStreamTest, ReadAllFrames) {
625 TEST_P(VideoDecoderStreamTest, Read_AfterReset) {
633 // Tests that the decoder stream will switch from a software decoder to a
634 // hardware decoder if the config size increases
635 TEST_P(VideoDecoderStreamTest, ConfigChangeSwToHw) {
636 EnablePlatformDecoders({1});
638 // Create a demuxer stream with a config that increases in size
639 auto const size_delta =
640 TestVideoConfig::LargeCodedSize() - TestVideoConfig::NormalCodedSize();
641 auto const width_delta = size_delta.width() / (kNumConfigs - 1);
642 auto const height_delta = size_delta.height() / (kNumConfigs - 1);
643 CreateDemuxerStream(TestVideoConfig::NormalCodedSize(),
644 gfx::Vector2dF(width_delta, height_delta));
647 // Initially we should be using a software decoder
648 EXPECT_TRUE(decoder_);
649 EXPECT_FALSE(decoder_->IsPlatformDecoder());
653 // We should end up on a hardware decoder
654 EXPECT_TRUE(decoder_->IsPlatformDecoder());
657 // Tests that the decoder stream will switch from a hardware decoder to a
658 // software decoder if the config size decreases
659 TEST_P(VideoDecoderStreamTest, ConfigChangeHwToSw) {
660 EnablePlatformDecoders({1});
662 // Create a demuxer stream with a config that progressively decreases in size
663 auto const size_delta =
664 TestVideoConfig::LargeCodedSize() - TestVideoConfig::NormalCodedSize();
665 auto const width_delta = size_delta.width() / kNumConfigs;
666 auto const height_delta = size_delta.height() / kNumConfigs;
667 CreateDemuxerStream(TestVideoConfig::LargeCodedSize(),
668 gfx::Vector2dF(-width_delta, -height_delta));
671 // We should initially be using a hardware decoder
672 EXPECT_TRUE(decoder_);
673 EXPECT_TRUE(decoder_->IsPlatformDecoder());
676 // We should end up on a software decoder
677 EXPECT_FALSE(decoder_->IsPlatformDecoder());
680 TEST_P(VideoDecoderStreamTest, Read_ProperMetadata) {
681 // For testing simplicity, omit parallel decode tests with a delay in frames.
682 if (GetParam().parallel_decoding > 1 && GetParam().decoding_delay > 0)
685 if (GetParam().has_prepare) {
686 // Override the basic PrepareFrame() for a version that moves the MockTime
687 // by kPrepareDelay. This simulates real work done (e.g. YUV conversion).
688 video_decoder_stream_->SetPrepareCB(
689 base::BindRepeating(&VideoDecoderStreamTest::PrepareFrameWithDelay,
690 base::Unretained(this)));
693 constexpr base::TimeDelta kDecodeDelay = base::Milliseconds(10);
697 // Simulate time elapsed by the decoder.
698 EnterPendingState(DECODER_DECODE);
699 task_environment_.FastForwardBy(kDecodeDelay);
701 SatisfyPendingCallback(DECODER_DECODE);
703 EXPECT_TRUE(frame_read_);
705 const VideoFrameMetadata& metadata = frame_read_->metadata();
707 // Verify the decoding metadata is accurate.
708 EXPECT_EQ(*metadata.decode_end_time - *metadata.decode_begin_time,
711 // Verify the processing metadata is accurate.
712 const base::TimeDelta expected_processing_time =
713 GetParam().has_prepare ? (kDecodeDelay + kPrepareDelay) : kDecodeDelay;
715 EXPECT_EQ(*metadata.processing_time, expected_processing_time);
718 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxer) {
720 demuxer_stream_->HoldNextRead();
722 EXPECT_TRUE(pending_read_);
724 int demuxed_buffers = 0;
726 // Pass frames from the demuxer to the VideoDecoderStream until the first read
727 // request is satisfied.
728 while (pending_read_) {
730 demuxer_stream_->SatisfyReadAndHoldNext();
731 base::RunLoop().RunUntilIdle();
734 EXPECT_EQ(std::min(GetParam().decoding_delay + 1, kNumBuffersInOneConfig + 1),
737 // At this point the stream is waiting on read from the demuxer, but there is
738 // no pending read from the stream. The stream should be blocked if we try
739 // reading from it again.
742 demuxer_stream_->SatisfyRead();
743 base::RunLoop().RunUntilIdle();
744 EXPECT_FALSE(pending_read_);
747 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxerAndDecoder) {
748 // Test applies only when the decoder allows multiple parallel requests.
749 if (GetParam().parallel_decoding == 1)
753 demuxer_stream_->HoldNextRead();
754 decoder_->HoldDecode();
756 EXPECT_TRUE(pending_read_);
758 int demuxed_buffers = 0;
760 // Pass frames from the demuxer to the VideoDecoderStream until the first read
761 // request is satisfied, while always keeping one decode request pending.
762 while (pending_read_) {
764 demuxer_stream_->SatisfyReadAndHoldNext();
765 base::RunLoop().RunUntilIdle();
767 // Always keep one decode request pending.
768 if (demuxed_buffers > 1) {
769 decoder_->SatisfySingleDecode();
770 base::RunLoop().RunUntilIdle();
775 EXPECT_TRUE(pending_read_);
777 // Unblocking one decode request should unblock read even when demuxer is
779 decoder_->SatisfySingleDecode();
780 base::RunLoop().RunUntilIdle();
781 EXPECT_FALSE(pending_read_);
783 // Stream should still be blocked on the demuxer after unblocking the decoder.
784 decoder_->SatisfyDecode();
786 EXPECT_TRUE(pending_read_);
788 // Verify that the stream has returned all frames that have been demuxed,
789 // accounting for the decoder delay.
790 EXPECT_EQ(demuxed_buffers - GetParam().decoding_delay, num_decoded_frames_);
792 // Unblocking the demuxer will unblock the stream.
793 demuxer_stream_->SatisfyRead();
794 base::RunLoop().RunUntilIdle();
795 EXPECT_FALSE(pending_read_);
798 TEST_P(VideoDecoderStreamTest, Read_DuringEndOfStreamDecode) {
799 // Test applies only when the decoder allows multiple parallel requests, and
800 // they are not satisfied in a single batch.
801 if (GetParam().parallel_decoding == 1 || GetParam().decoding_delay != 0)
805 decoder_->HoldDecode();
807 // Read all of the frames up to end of stream. Since parallel decoding is
808 // enabled, the end of stream buffer will be sent to the decoder immediately,
809 // but we don't satisfy it yet.
810 for (int configuration = 0; configuration < kNumConfigs; configuration++) {
811 for (int frame = 0; frame < kNumBuffersInOneConfig; frame++) {
813 while (pending_read_) {
814 decoder_->SatisfySingleDecode();
815 base::RunLoop().RunUntilIdle();
820 // Read() again. The callback must be delayed until the decode completes.
822 ASSERT_TRUE(pending_read_);
824 // Satisfy decoding of the end of stream buffer. The read should complete.
825 decoder_->SatisfySingleDecode();
826 base::RunLoop().RunUntilIdle();
827 ASSERT_FALSE(pending_read_);
828 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
830 // The read output should indicate end of stream.
831 ASSERT_TRUE(frame_read_.get());
832 EXPECT_TRUE(frame_read_->metadata().end_of_stream);
835 TEST_P(VideoDecoderStreamTest, Read_DemuxerStreamReadError) {
837 EnterPendingState(DEMUXER_READ_NORMAL);
841 if (GetParam().is_encrypted && GetParam().has_decryptor) {
843 HasSubstr("DecryptingDemuxerStream: demuxer stream read error"));
845 EXPECT_MEDIA_LOG(HasSubstr("video demuxer stream read error"));
847 demuxer_stream_->Error();
848 base::RunLoop().RunUntilIdle();
850 ASSERT_FALSE(pending_read_);
851 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
852 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
855 // No Reset() before initialization is successfully completed.
856 TEST_P(VideoDecoderStreamTest, Reset_AfterInitialization) {
862 TEST_P(VideoDecoderStreamTest, Reset_DuringReinitialization) {
865 EnterPendingState(DECODER_REINIT);
866 // VideoDecoder::Reset() is not called when we reset during reinitialization.
867 pending_reset_ = true;
868 video_decoder_stream_->Reset(
869 base::BindOnce(&VideoDecoderStreamTest::OnReset, base::Unretained(this)));
870 SatisfyPendingCallback(DECODER_REINIT);
874 TEST_P(VideoDecoderStreamTest, Reset_AfterReinitialization) {
876 EnterPendingState(DECODER_REINIT);
877 SatisfyPendingCallback(DECODER_REINIT);
882 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_Normal) {
884 EnterPendingState(DEMUXER_READ_NORMAL);
885 EnterPendingState(DECODER_RESET);
886 SatisfyPendingCallback(DEMUXER_READ_NORMAL);
887 SatisfyPendingCallback(DECODER_RESET);
891 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_ConfigChange) {
893 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
894 EnterPendingState(DECODER_RESET);
895 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
896 SatisfyPendingCallback(DECODER_RESET);
900 TEST_P(VideoDecoderStreamTest, Reset_DuringNormalDecoderDecode) {
902 EnterPendingState(DECODER_DECODE);
903 EnterPendingState(DECODER_RESET);
904 SatisfyPendingCallback(DECODER_DECODE);
905 SatisfyPendingCallback(DECODER_RESET);
909 TEST_P(VideoDecoderStreamTest, Reset_AfterNormalRead) {
916 TEST_P(VideoDecoderStreamTest, Reset_AfterDemuxerRead_ConfigChange) {
918 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
919 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
924 TEST_P(VideoDecoderStreamTest, Reset_AfterEndOfStream) {
928 num_decoded_frames_ = 0;
929 demuxer_stream_->SeekToStart();
933 TEST_P(VideoDecoderStreamTest, Reset_DuringNoKeyRead) {
935 EnterPendingState(DECRYPTOR_NO_KEY);
939 // In the following Destroy_* tests, |video_decoder_stream_| is destroyed in
940 // VideoDecoderStreamTest destructor.
942 TEST_P(VideoDecoderStreamTest, Destroy_BeforeInitialization) {}
944 TEST_P(VideoDecoderStreamTest, Destroy_DuringInitialization) {
945 HoldDecoderInitOnSelection({0});
949 TEST_P(VideoDecoderStreamTest, Destroy_AfterInitialization) {
953 TEST_P(VideoDecoderStreamTest, Destroy_DuringReinitialization) {
955 EnterPendingState(DECODER_REINIT);
958 TEST_P(VideoDecoderStreamTest, Destroy_AfterReinitialization) {
960 EnterPendingState(DECODER_REINIT);
961 SatisfyPendingCallback(DECODER_REINIT);
964 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_Normal) {
966 EnterPendingState(DEMUXER_READ_NORMAL);
969 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_ConfigChange) {
971 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
974 TEST_P(VideoDecoderStreamTest, Destroy_DuringNormalDecoderDecode) {
976 EnterPendingState(DECODER_DECODE);
979 TEST_P(VideoDecoderStreamTest, Destroy_AfterNormalRead) {
984 TEST_P(VideoDecoderStreamTest, Destroy_AfterConfigChangeRead) {
986 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
987 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
990 TEST_P(VideoDecoderStreamTest, Destroy_DuringDecoderReinitialization) {
992 EnterPendingState(DECODER_REINIT);
995 TEST_P(VideoDecoderStreamTest, Destroy_DuringNoKeyRead) {
997 EnterPendingState(DECRYPTOR_NO_KEY);
1000 TEST_P(VideoDecoderStreamTest, Destroy_DuringReset) {
1002 EnterPendingState(DECODER_RESET);
1005 TEST_P(VideoDecoderStreamTest, Destroy_AfterReset) {
1010 TEST_P(VideoDecoderStreamTest, Destroy_DuringRead_DuringReset) {
1012 EnterPendingState(DECODER_DECODE);
1013 EnterPendingState(DECODER_RESET);
1016 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_DuringReset) {
1018 EnterPendingState(DECODER_DECODE);
1019 EnterPendingState(DECODER_RESET);
1020 SatisfyPendingCallback(DECODER_DECODE);
1023 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_AfterReset) {
1029 // The following tests cover the fallback logic after reinitialization error or
1030 // decode error of the first buffer after initialization.
1032 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeError) {
1034 decoder_->SimulateError();
1037 // |video_decoder_stream_| should have fallen back to a new decoder.
1038 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1040 ASSERT_FALSE(pending_read_);
1041 ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1043 // Check that we fell back to Decoder2.
1044 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1046 // Verify no frame was dropped.
1050 TEST_P(VideoDecoderStreamTest,
1051 FallbackDecoder_EndOfStreamReachedBeforeFallback) {
1052 // Only consider cases where there is a decoder delay. For test simplicity,
1053 // omit the parallel case.
1054 if (GetParam().decoding_delay == 0 || GetParam().parallel_decoding > 1)
1058 decoder_->HoldDecode();
1061 // One buffer should have already pulled from the demuxer stream. Set the next
1062 // one to be an EOS.
1063 demuxer_stream_->SeekToEndOfStream();
1065 decoder_->SatisfySingleDecode();
1066 base::RunLoop().RunUntilIdle();
1068 // |video_decoder_stream_| should not have emitted a frame.
1069 EXPECT_TRUE(pending_read_);
1071 // Pending buffers should contain a regular buffer and an EOS buffer.
1072 EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 2);
1074 decoder_->SimulateError();
1075 base::RunLoop().RunUntilIdle();
1077 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1079 // A frame should have been emitted.
1080 EXPECT_FALSE(pending_read_);
1081 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1082 EXPECT_FALSE(frame_read_->metadata().end_of_stream);
1083 EXPECT_GT(decoder_->total_bytes_decoded(), 0);
1087 EXPECT_FALSE(pending_read_);
1088 EXPECT_EQ(0, video_decoder_stream_->get_fallback_buffers_size_for_testing());
1089 EXPECT_TRUE(frame_read_->metadata().end_of_stream);
1092 TEST_P(VideoDecoderStreamTest,
1093 FallbackDecoder_DoesReinitializeStompPendingRead) {
1094 // Test only the case where there is no decoding delay and parallel decoding.
1095 if (GetParam().decoding_delay != 0 || GetParam().parallel_decoding <= 1)
1099 decoder_->HoldDecode();
1101 // Queue one read, defer the second.
1102 frame_read_ = nullptr;
1103 pending_read_ = true;
1104 video_decoder_stream_->Read(base::BindOnce(
1105 &VideoDecoderStreamTest::FrameReady, base::Unretained(this)));
1106 demuxer_stream_->HoldNextRead();
1108 // Force an error to occur on the first decode, but ensure it isn't propagated
1109 // until after the next read has been started.
1110 decoder_->SimulateError();
1111 HoldDecodeAfterSelection({1});
1113 // Complete the fallback to the second decoder with the read still pending.
1114 base::RunLoop().RunUntilIdle();
1116 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1118 // Can't check the original decoder right now, it might have been destroyed
1119 // already. Verify that there was nothing decoded until we kicked the decoder.
1120 EXPECT_EQ(decoder_->total_bytes_decoded(), 0);
1121 decoder_->SatisfyDecode();
1122 const int first_decoded_bytes = decoder_->total_bytes_decoded();
1123 ASSERT_GT(first_decoded_bytes, 0);
1125 // Satisfy the previously pending read and ensure it is decoded.
1126 demuxer_stream_->SatisfyRead();
1127 base::RunLoop().RunUntilIdle();
1128 ASSERT_GT(decoder_->total_bytes_decoded(), first_decoded_bytes);
1131 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeErrorRepeated) {
1134 // Hold other decoders to simulate errors.
1135 HoldDecodeAfterSelection({1, 2});
1137 // Simulate decode error to trigger the fallback path.
1138 decoder_->SimulateError();
1140 base::RunLoop().RunUntilIdle();
1142 // Expect decoder 1 to be tried.
1143 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1144 decoder_->SimulateError();
1145 base::RunLoop().RunUntilIdle();
1148 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1149 decoder_->SimulateError();
1150 base::RunLoop().RunUntilIdle();
1152 // No decoders left, expect failure.
1153 EXPECT_EQ(decoder_, nullptr);
1154 EXPECT_FALSE(pending_read_);
1155 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1156 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1159 // This tests verifies that we properly fallback to a new decoder if the first
1160 // decode after a config change fails.
1161 TEST_P(VideoDecoderStreamTest,
1162 FallbackDecoder_SelectedOnMidstreamDecodeErrorAfterReinitialization) {
1163 // For simplicity of testing, this test applies only when there is no decoder
1164 // delay and parallel decoding is disabled.
1165 if (GetParam().decoding_delay != 0 || GetParam().parallel_decoding > 1)
1170 // Note: Completes decoding one frame, results in Decode() being called with
1171 // second frame that is not completed.
1174 // Verify that the first frame was decoded successfully.
1175 EXPECT_FALSE(pending_read_);
1176 EXPECT_GT(decoder_->total_bytes_decoded(), 0);
1177 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1179 // Continue up to the point of reinitialization.
1180 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1182 // Hold decodes to prevent a frame from being outputted upon reinitialization.
1183 decoder_->HoldDecode();
1184 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1186 // DecoderStream sends an EOS to flush the decoder during config changes.
1187 // Let the EOS decode be satisfied to properly complete the decoder reinit.
1188 decoder_->SatisfySingleDecode();
1189 base::RunLoop().RunUntilIdle();
1190 EXPECT_TRUE(pending_read_);
1192 // Fail the first decode, before a frame can be outputted.
1193 decoder_->SimulateError();
1194 base::RunLoop().RunUntilIdle();
1198 // Verify that fallback happened.
1199 EXPECT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1200 EXPECT_FALSE(pending_read_);
1201 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1202 EXPECT_GT(decoder_->total_bytes_decoded(), 0);
1205 TEST_P(VideoDecoderStreamTest,
1206 FallbackDecoder_DecodeErrorRepeated_AfterReinitialization) {
1209 // Simulate decode error to trigger fallback.
1210 decoder_->SimulateError();
1212 base::RunLoop().RunUntilIdle();
1214 // Simulate reinitialize error of decoder 1.
1215 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1216 decoder_->SimulateFailureToInit();
1217 HoldDecodeAfterSelection({0, 1, 2});
1218 ReadUntilDecoderReinitialized();
1220 // Decoder 0 should be selected again.
1221 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1222 decoder_->SimulateError();
1223 base::RunLoop().RunUntilIdle();
1226 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1227 decoder_->SimulateError();
1228 base::RunLoop().RunUntilIdle();
1231 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1232 decoder_->SimulateError();
1233 base::RunLoop().RunUntilIdle();
1235 // No decoders left.
1236 EXPECT_EQ(decoder_, nullptr);
1237 EXPECT_FALSE(pending_read_);
1238 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1239 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1242 TEST_P(VideoDecoderStreamTest,
1243 FallbackDecoder_ConfigChangeClearsPendingBuffers) {
1244 // Test case is only interesting if the decoder can receive a config change
1245 // before returning its first frame.
1246 if (GetParam().decoding_delay < kNumBuffersInOneConfig)
1250 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1251 ASSERT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1253 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1254 ASSERT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1255 EXPECT_FALSE(pending_read_);
1260 TEST_P(VideoDecoderStreamTest,
1261 FallbackDecoder_ErrorDuringConfigChangeFlushing) {
1262 // Test case is only interesting if the decoder can receive a config change
1263 // before returning its first frame.
1264 if (GetParam().decoding_delay < kNumBuffersInOneConfig)
1268 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1269 EXPECT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1271 decoder_->HoldDecode();
1272 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1274 // The flush request should have been sent and held.
1275 EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1276 EXPECT_TRUE(pending_read_);
1278 // Triggering an error here will cause the frames in selected decoder to be
1279 // lost. There are no pending buffers to give to |decoders_[1]| due to
1280 // http://crbug.com/603713
1281 decoder_->SimulateError();
1282 base::RunLoop().RunUntilIdle();
1284 // We want to make sure the fallback decoder can decode the rest of the frames
1285 // in the demuxer stream.
1286 ReadAllFrames(kNumBuffersInOneConfig * (kNumConfigs - 1));
1289 TEST_P(VideoDecoderStreamTest,
1290 FallbackDecoder_PendingBuffersIsFilledAndCleared) {
1291 // Test applies only when there is a decoder delay, and the decoder will not
1292 // receive a config change before outputting its first frame. Parallel
1293 // decoding is also disabled in this test case, for readability and simplicity
1294 // of the unit test.
1295 if (GetParam().decoding_delay == 0 ||
1296 GetParam().decoding_delay > kNumBuffersInOneConfig ||
1297 GetParam().parallel_decoding > 1) {
1303 // Block on demuxer read and decoder decode so we can step through.
1304 demuxer_stream_->HoldNextRead();
1305 decoder_->HoldDecode();
1308 int demuxer_reads_satisfied = 0;
1309 // Send back and requests buffers until the next one would fill the decoder
1311 while (demuxer_reads_satisfied < GetParam().decoding_delay - 1) {
1312 // Send a buffer back.
1313 demuxer_stream_->SatisfyReadAndHoldNext();
1314 base::RunLoop().RunUntilIdle();
1315 ++demuxer_reads_satisfied;
1317 // Decode one buffer.
1318 decoder_->SatisfySingleDecode();
1319 base::RunLoop().RunUntilIdle();
1320 EXPECT_TRUE(pending_read_);
1321 EXPECT_EQ(demuxer_reads_satisfied,
1322 video_decoder_stream_->get_pending_buffers_size_for_testing());
1323 // No fallback buffers should be queued up yet.
1325 video_decoder_stream_->get_fallback_buffers_size_for_testing());
1328 // Hold the init before triggering the error, to verify internal state.
1329 demuxer_stream_->SatisfyReadAndHoldNext();
1330 ++demuxer_reads_satisfied;
1332 decoder_->SimulateError();
1334 HoldDecoderInitOnSelection({1});
1335 HoldDecodeAfterSelection({1});
1337 base::RunLoop().RunUntilIdle();
1339 EXPECT_TRUE(pending_read_);
1340 EXPECT_EQ(demuxer_reads_satisfied,
1341 video_decoder_stream_->get_pending_buffers_size_for_testing());
1343 decoders_[1]->SatisfyInit();
1344 base::RunLoop().RunUntilIdle();
1346 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1348 // Make sure the pending buffers have been transferred to fallback buffers.
1349 // One call to Decode() during the initialization process, so we expect one
1350 // buffer to already have been consumed from the fallback buffers.
1351 // Pending buffers should never go down (unless we encounter a config change)
1352 EXPECT_EQ(demuxer_reads_satisfied - 1,
1353 video_decoder_stream_->get_fallback_buffers_size_for_testing());
1354 EXPECT_EQ(demuxer_reads_satisfied,
1355 video_decoder_stream_->get_pending_buffers_size_for_testing());
1357 decoder_->SatisfyDecode();
1358 base::RunLoop().RunUntilIdle();
1360 // Make sure all buffers consumed by |decoders_| have come from the fallback.
1361 // Pending buffers should not have been cleared yet.
1362 EXPECT_EQ(0, video_decoder_stream_->get_fallback_buffers_size_for_testing());
1363 EXPECT_EQ(demuxer_reads_satisfied,
1364 video_decoder_stream_->get_pending_buffers_size_for_testing());
1365 EXPECT_TRUE(pending_read_);
1367 // Give the decoder one more buffer, enough to release a frame.
1368 demuxer_stream_->SatisfyReadAndHoldNext();
1369 base::RunLoop().RunUntilIdle();
1371 // New buffers should not have been added after the frame was released.
1372 EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1373 EXPECT_FALSE(pending_read_);
1375 demuxer_stream_->SatisfyRead();
1377 // Confirm no frames were dropped.
1381 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnDecodeThenInitErrors) {
1383 decoder_->SimulateError();
1384 FailDecoderInitOnSelection({1});
1387 // Decoder 0 should be blocked, and decoder 1 fails to initialize, so
1388 // |video_decoder_stream_| should have fallen back to decoder 2.
1389 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1391 ASSERT_FALSE(pending_read_);
1392 ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1394 // Can't check previously selected decoder(s) right now, they might have been
1395 // destroyed already.
1396 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1398 // Verify no frame was dropped.
1402 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnInitThenDecodeErrors) {
1403 FailDecoderInitOnSelection({0});
1405 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1406 ClearDecoderInitExpectations();
1408 decoder_->HoldDecode();
1410 decoder_->SimulateError();
1411 base::RunLoop().RunUntilIdle();
1413 // |video_decoder_stream_| should have fallen back to decoder 2.
1414 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1416 ASSERT_FALSE(pending_read_);
1417 ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1419 // Can't check previously selected decoder(s) right now, they might have been
1420 // destroyed already.
1421 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1423 // Verify no frame was dropped.
1427 TEST_P(VideoDecoderStreamTest,
1428 FallbackDecoder_NotSelectedOnMidstreamDecodeError) {
1432 // Successfully received a frame.
1433 EXPECT_FALSE(pending_read_);
1434 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1436 decoder_->SimulateError();
1438 // The error must surface from Read() as DECODE_ERROR.
1439 while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1441 base::RunLoop().RunUntilIdle();
1442 EXPECT_FALSE(pending_read_);
1445 // Verify the error was surfaced, rather than falling back to other decoders.
1446 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1447 EXPECT_FALSE(pending_read_);
1448 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1449 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1452 TEST_P(VideoDecoderStreamTest, DecoderErrorWhenNotReading) {
1454 decoder_->HoldDecode();
1456 EXPECT_TRUE(pending_read_);
1458 // Satisfy decode requests until we get the first frame out.
1459 while (pending_read_) {
1460 decoder_->SatisfySingleDecode();
1461 base::RunLoop().RunUntilIdle();
1464 // Trigger an error in the decoding.
1465 decoder_->SimulateError();
1467 // The error must surface from Read() as DECODE_ERROR.
1468 while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1470 base::RunLoop().RunUntilIdle();
1471 EXPECT_FALSE(pending_read_);
1473 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1474 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1477 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Once) {
1479 decoder_->SimulateFailureToInit();
1480 ReadUntilDecoderReinitialized();
1481 // Should have fallen back to a new instance of decoder 0.
1482 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1484 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1487 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Twice) {
1490 // Trigger reinitialization error, and fallback to a new instance.
1491 decoder_->SimulateFailureToInit();
1492 ReadUntilDecoderReinitialized();
1493 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1497 // Trigger reinitialization error again. Since a frame was output, this will
1498 // be a new instance of decoder 0 again.
1499 decoder_->SimulateFailureToInit();
1500 ReadUntilDecoderReinitialized();
1501 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1505 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_OneUnsupportedDecoder) {
1508 // The current decoder will fail to reinitialize.
1509 decoder_->SimulateFailureToInit();
1511 // Decoder 1 will also fail to initialize on decoder selection.
1512 FailDecoderInitOnSelection({0, 1});
1514 ReadUntilDecoderReinitialized();
1516 // As a result, decoder 2 will be selected.
1517 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1522 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_NoSupportedDecoder) {
1525 // The current decoder will fail to reinitialize, triggering decoder
1527 decoder_->SimulateFailureToInit();
1529 // All of the decoders will fail in decoder selection.
1530 FailDecoderInitOnSelection({0, 1, 2});
1532 ReadUntilDecoderReinitialized();
1534 // The error will surface from Read() as DECODE_ERROR.
1535 while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1537 base::RunLoop().RunUntilIdle();
1538 EXPECT_FALSE(pending_read_);
1540 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1541 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1544 TEST_P(VideoDecoderStreamTest, Destroy_DuringFallbackDecoderSelection) {
1546 decoder_->SimulateFailureToInit();
1547 EnterPendingState(DECODER_REINIT);
1548 HoldDecoderInitOnSelection({1});
1549 SatisfyPendingCallback(DECODER_REINIT);
1552 } // namespace media