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.
9 #include "base/functional/bind.h"
10 #include "base/functional/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());
144 EXPECT_MEDIA_LOG(HasSubstr("clear to encrypted buffers"))
148 VideoDecoderStreamTest(const VideoDecoderStreamTest&) = delete;
149 VideoDecoderStreamTest& operator=(const VideoDecoderStreamTest&) = delete;
151 ~VideoDecoderStreamTest() {
152 // Check that the pipeline statistics callback was fired correctly.
153 EXPECT_EQ(num_decoded_bytes_unreported_, 0);
155 is_initialized_ = false;
157 video_decoder_stream_.reset();
158 base::RunLoop().RunUntilIdle();
160 DCHECK(!pending_initialize_);
161 DCHECK(!pending_read_);
162 DCHECK(!pending_reset_);
163 DCHECK(!pending_stop_);
166 void CreateDemuxerStream(gfx::Size start_size, gfx::Vector2dF size_delta) {
167 DCHECK(!demuxer_stream_);
168 demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
169 kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted,
170 start_size, size_delta);
173 void PrepareFrame(scoped_refptr<VideoFrame> frame,
174 VideoDecoderStream::OutputReadyCB output_ready_cb) {
175 // Simulate some delay in return of the output.
176 task_environment_.GetMainThreadTaskRunner()->PostTask(
178 base::BindOnce(std::move(output_ready_cb), std::move(frame)));
181 void PrepareFrameWithDelay(
182 scoped_refptr<VideoFrame> frame,
183 VideoDecoderStream::OutputReadyCB output_ready_cb) {
184 task_environment_.FastForwardBy(kPrepareDelay);
185 task_environment_.GetMainThreadTaskRunner()->PostTask(
187 base::BindOnce(std::move(output_ready_cb), std::move(frame)));
190 void OnBytesDecoded(int count) { num_decoded_bytes_unreported_ += count; }
192 // Callback to create a list of decoders for the DecoderSelector to select
193 // from. Decoder selection happens
194 // - on the initial selection in Initialize(),
195 // - on decoder reinitialization failure, which can be simulated by calling
196 // decoder_->SimulateFailureToInit(), and
197 // - on decode error of the first buffer, which can be simulated by calling
198 // decoder_->SimulateError() before reading the first frame.
199 std::vector<std::unique_ptr<VideoDecoder>> CreateVideoDecodersForTest() {
200 // Previously decoders could have been destroyed on decoder reselection.
203 // Provide 3 decoders to test fallback cases.
204 // TODO(xhwang): We should test the case where only certain decoder
205 // supports encrypted streams. Currently this is hard to test because we use
206 // parameterized tests which need to pass in all combinations.
207 std::vector<std::unique_ptr<VideoDecoder>> decoders;
209 #if !BUILDFLAG(IS_ANDROID)
210 // Note this is _not_ inserted into |decoders_| below, so we don't need to
211 // adjust the indices used below to compensate.
212 decoders.push_back(std::make_unique<DecryptingVideoDecoder>(
213 task_environment_.GetMainThreadTaskRunner(), &media_log_));
216 for (int i = 0; i < 3; ++i) {
217 auto decoder = std::make_unique<FakeVideoDecoder>(
218 GetDecoderId(i), GetParam().decoding_delay,
219 GetParam().parallel_decoding,
220 base::BindRepeating(&VideoDecoderStreamTest::OnBytesDecoded,
221 base::Unretained(this)));
223 if (GetParam().is_encrypted && !GetParam().has_decryptor)
224 decoder->EnableEncryptedConfigSupport();
226 // Keep a reference so we can change the behavior of each decoder.
227 decoders_.push_back(decoder->GetWeakPtr());
229 decoders.push_back(std::move(decoder));
232 for (const auto i : decoder_indices_to_fail_init_)
233 decoders_[i]->SimulateFailureToInit();
235 for (const auto i : decoder_indices_to_hold_init_)
236 decoders_[i]->HoldNextInit();
238 for (const auto i : decoder_indices_to_hold_decode_)
239 decoders_[i]->HoldDecode();
241 for (const auto i : platform_decoder_indices_)
242 decoders_[i]->SetIsPlatformDecoder(true);
247 void ClearDecoderInitExpectations() {
248 decoder_indices_to_fail_init_.clear();
249 decoder_indices_to_hold_init_.clear();
250 decoder_indices_to_hold_decode_.clear();
251 platform_decoder_indices_.clear();
254 // On next decoder selection, fail initialization on decoders specified by
255 // |decoder_indices|.
256 void FailDecoderInitOnSelection(std::vector<int> decoder_indices) {
257 decoder_indices_to_fail_init_ = std::move(decoder_indices);
258 for (int i : decoder_indices_to_fail_init_) {
259 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
260 decoders_[i]->SimulateFailureToInit();
264 // On next decoder selection, hold initialization on decoders specified by
265 // |decoder_indices|.
266 void HoldDecoderInitOnSelection(std::vector<int> decoder_indices) {
267 decoder_indices_to_hold_init_ = std::move(decoder_indices);
268 for (int i : decoder_indices_to_hold_init_) {
269 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
270 decoders_[i]->HoldNextInit();
274 // After next decoder selection, hold decode on decoders specified by
275 // |decoder_indices|. This is needed because after decoder selection decode
276 // may be resumed immediately and it'll be too late to hold decode then.
277 void HoldDecodeAfterSelection(std::vector<int> decoder_indices) {
278 decoder_indices_to_hold_decode_ = std::move(decoder_indices);
279 for (int i : decoder_indices_to_hold_decode_) {
280 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
281 decoders_[i]->HoldDecode();
285 void EnablePlatformDecoders(std::vector<int> decoder_indices) {
286 platform_decoder_indices_ = std::move(decoder_indices);
287 for (int i : platform_decoder_indices_) {
288 if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
289 decoders_[i]->SetIsPlatformDecoder(true);
293 // Updates the |decoder_| currently being used by VideoDecoderStream.
294 void OnDecoderChanged(VideoDecoder* decoder) {
300 // Ensure there's a media log created whenever selecting a decoder.
301 EXPECT_MEDIA_LOG(HasSubstr("for video decoding, config"));
302 decoder_ = static_cast<FakeVideoDecoder*>(decoder);
303 ASSERT_TRUE(decoder_->GetDecoderId() == GetDecoderId(0) ||
304 decoder_->GetDecoderId() == GetDecoderId(1) ||
305 decoder_->GetDecoderId() == GetDecoderId(2));
308 MOCK_METHOD1(OnWaiting, void(WaitingReason));
310 void OnStatistics(const PipelineStatistics& statistics) {
311 num_decoded_bytes_unreported_ -= statistics.video_bytes_decoded;
314 void OnInitialized(bool success) {
315 DCHECK(!pending_read_);
316 DCHECK(!pending_reset_);
317 DCHECK(pending_initialize_);
318 pending_initialize_ = false;
320 is_initialized_ = success;
326 if (!demuxer_stream_) {
327 demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
328 kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted);
331 pending_initialize_ = true;
332 video_decoder_stream_->Initialize(
333 demuxer_stream_.get(),
334 base::BindOnce(&VideoDecoderStreamTest::OnInitialized,
335 base::Unretained(this)),
337 base::BindRepeating(&VideoDecoderStreamTest::OnStatistics,
338 base::Unretained(this)),
339 base::BindRepeating(&VideoDecoderStreamTest::OnWaiting,
340 base::Unretained(this)));
342 EXPECT_MEDIA_LOG(HasSubstr("video")).Times(AnyNumber());
343 EXPECT_MEDIA_LOG(HasSubstr("Video")).Times(AnyNumber());
344 EXPECT_MEDIA_LOG(HasSubstr("audio")).Times(AnyNumber());
345 EXPECT_MEDIA_LOG(HasSubstr("Audio")).Times(AnyNumber());
346 base::RunLoop().RunUntilIdle();
349 // Fake Decrypt() function used by DecryptingDemuxerStream. It does nothing
350 // but removes the DecryptConfig to make the buffer unencrypted.
351 void Decrypt(Decryptor::StreamType stream_type,
352 scoped_refptr<DecoderBuffer> encrypted,
353 Decryptor::DecryptCB decrypt_cb) {
354 DCHECK(encrypted->decrypt_config());
356 std::move(decrypt_cb).Run(Decryptor::kNoKey, nullptr);
360 DCHECK_EQ(stream_type, Decryptor::kVideo);
361 scoped_refptr<DecoderBuffer> decrypted =
362 DecoderBuffer::CopyFrom(encrypted->data(), encrypted->data_size());
363 if (encrypted->is_key_frame())
364 decrypted->set_is_key_frame(true);
365 decrypted->set_timestamp(encrypted->timestamp());
366 decrypted->set_duration(encrypted->duration());
367 std::move(decrypt_cb).Run(Decryptor::kSuccess, decrypted);
370 // Callback for VideoDecoderStream::Read().
371 void FrameReady(VideoDecoderStream::ReadResult result) {
372 DCHECK(pending_read_);
373 last_read_status_code_ = result.code();
374 scoped_refptr<VideoFrame> frame =
375 last_read_status_code_ == DecoderStatus::Codes::kOk
376 ? std::move(result).value()
379 if (frame && !frame->metadata().end_of_stream) {
380 EXPECT_EQ(*frame->metadata().frame_duration, demuxer_stream_->duration());
382 num_decoded_frames_++;
384 pending_read_ = false;
388 DCHECK(!pending_read_);
389 DCHECK(pending_reset_);
390 pending_reset_ = false;
393 void ReadOneFrame() {
394 frame_read_ = nullptr;
395 pending_read_ = true;
396 video_decoder_stream_->Read(base::BindOnce(
397 &VideoDecoderStreamTest::FrameReady, base::Unretained(this)));
398 base::RunLoop().RunUntilIdle();
401 void ReadUntilPending() {
404 } while (!pending_read_);
407 void ReadAllFrames(int expected_decoded_frames) {
408 // Reading all frames reinitializes the demuxer.
411 } while (frame_read_.get() && !frame_read_->metadata().end_of_stream);
413 DCHECK_EQ(expected_decoded_frames, num_decoded_frames_);
416 void ReadAllFrames() {
417 // No frames should have been dropped.
418 ReadAllFrames(kNumConfigs * kNumBuffersInOneConfig);
424 DEMUXER_READ_CONFIG_CHANGE,
431 void EnterPendingState(PendingState state) {
432 DCHECK_NE(state, NOT_PENDING);
434 case DEMUXER_READ_NORMAL:
435 demuxer_stream_->HoldNextRead();
439 case DEMUXER_READ_CONFIG_CHANGE:
440 demuxer_stream_->HoldNextConfigChangeRead();
444 case DECRYPTOR_NO_KEY:
445 if (GetParam().is_encrypted && GetParam().has_decryptor) {
446 EXPECT_MEDIA_LOG(HasSubstr("no key for key ID"));
447 EXPECT_CALL(*this, OnWaiting(WaitingReason::kNoDecryptionKey));
454 decoder_->HoldNextInit();
459 decoder_->HoldDecode();
464 decoder_->HoldNextReset();
465 pending_reset_ = true;
466 video_decoder_stream_->Reset(base::BindOnce(
467 &VideoDecoderStreamTest::OnReset, base::Unretained(this)));
468 base::RunLoop().RunUntilIdle();
477 void SatisfyPendingCallback(PendingState state) {
478 DCHECK_NE(state, NOT_PENDING);
480 case DEMUXER_READ_CONFIG_CHANGE:
481 EXPECT_MEDIA_LOG(HasSubstr("decoder config changed"))
482 .Times(testing::AtLeast(1));
484 case DEMUXER_READ_NORMAL:
485 demuxer_stream_->SatisfyRead();
488 // This is only interesting to test during VideoDecoderStream destruction.
489 // There's no need to satisfy a callback.
490 case DECRYPTOR_NO_KEY:
495 decoder_->SatisfyInit();
499 decoder_->SatisfyDecode();
503 decoder_->SatisfyReset();
511 base::RunLoop().RunUntilIdle();
515 EnterPendingState(DECODER_DECODE);
516 SatisfyPendingCallback(DECODER_DECODE);
520 EnterPendingState(DECODER_RESET);
521 SatisfyPendingCallback(DECODER_RESET);
524 void ReadUntilDecoderReinitialized() {
525 EnterPendingState(DECODER_REINIT);
526 SatisfyPendingCallback(DECODER_REINIT);
529 base::test::SingleThreadTaskEnvironment task_environment_{
530 base::test::TaskEnvironment::TimeSource::MOCK_TIME};
532 StrictMock<MockMediaLog> media_log_;
533 std::unique_ptr<VideoDecoderStream> video_decoder_stream_;
534 std::unique_ptr<FakeDemuxerStream> demuxer_stream_;
535 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
537 // Use NiceMock since we don't care about most of calls on the decryptor.
538 std::unique_ptr<NiceMock<MockDecryptor>> decryptor_;
540 // References to the list of decoders to be select from by DecoderSelector.
541 // Three decoders are needed to test that decoder fallback can occur more than
542 // once on a config change. They are owned by |video_decoder_stream_|.
543 std::vector<base::WeakPtr<FakeVideoDecoder>> decoders_;
545 std::vector<int> decoder_indices_to_fail_init_;
546 std::vector<int> decoder_indices_to_hold_init_;
547 std::vector<int> decoder_indices_to_hold_decode_;
548 std::vector<int> platform_decoder_indices_;
550 // The current decoder used by |video_decoder_stream_|.
551 raw_ptr<FakeVideoDecoder, AcrossTasksDanglingUntriaged> decoder_ = nullptr;
553 bool is_initialized_;
554 int num_decoded_frames_;
555 bool pending_initialize_;
559 int num_decoded_bytes_unreported_;
560 scoped_refptr<VideoFrame> frame_read_;
561 DecoderStatus::Codes last_read_status_code_;
563 // Decryptor has no key to decrypt a frame.
567 INSTANTIATE_TEST_SUITE_P(
569 VideoDecoderStreamTest,
570 ::testing::Values(VideoDecoderStreamTestParams(false, false, false, 0, 1),
571 VideoDecoderStreamTestParams(false, false, false, 3, 1),
572 VideoDecoderStreamTestParams(false, false, false, 7, 1),
573 VideoDecoderStreamTestParams(false, false, true, 0, 1),
574 VideoDecoderStreamTestParams(false, false, true, 3, 1)));
576 INSTANTIATE_TEST_SUITE_P(
577 EncryptedWithDecryptor,
578 VideoDecoderStreamTest,
579 ::testing::Values(VideoDecoderStreamTestParams(true, true, false, 7, 1),
580 VideoDecoderStreamTestParams(true, true, true, 7, 1)));
582 INSTANTIATE_TEST_SUITE_P(
583 EncryptedWithoutDecryptor,
584 VideoDecoderStreamTest,
585 ::testing::Values(VideoDecoderStreamTestParams(true, false, false, 7, 1),
586 VideoDecoderStreamTestParams(true, false, true, 7, 1)));
588 INSTANTIATE_TEST_SUITE_P(
590 VideoDecoderStreamTest,
591 ::testing::Values(VideoDecoderStreamTestParams(false, false, false, 0, 3),
592 VideoDecoderStreamTestParams(false, false, false, 2, 3),
593 VideoDecoderStreamTestParams(false, false, true, 0, 3),
594 VideoDecoderStreamTestParams(false, false, true, 2, 3)));
596 TEST_P(VideoDecoderStreamTest, CanReadWithoutStallingAtAnyTime) {
597 ASSERT_FALSE(video_decoder_stream_->CanReadWithoutStalling());
600 TEST_P(VideoDecoderStreamTest, Initialization) {
602 EXPECT_TRUE(is_initialized_);
605 TEST_P(VideoDecoderStreamTest, AllDecoderInitializationFails) {
606 FailDecoderInitOnSelection({0, 1, 2});
608 EXPECT_FALSE(is_initialized_);
611 TEST_P(VideoDecoderStreamTest, PartialDecoderInitializationFails) {
612 FailDecoderInitOnSelection({0, 1});
614 EXPECT_TRUE(is_initialized_);
617 TEST_P(VideoDecoderStreamTest, ReadOneFrame) {
622 TEST_P(VideoDecoderStreamTest, ReadAllFrames) {
627 TEST_P(VideoDecoderStreamTest, Read_AfterReset) {
635 // Tests that the decoder stream will switch from a software decoder to a
636 // hardware decoder if the config size increases
637 TEST_P(VideoDecoderStreamTest, ConfigChangeSwToHw) {
638 EnablePlatformDecoders({1});
640 // Create a demuxer stream with a config that increases in size
641 auto const size_delta =
642 TestVideoConfig::LargeCodedSize() - TestVideoConfig::NormalCodedSize();
643 auto const width_delta = size_delta.width() / (kNumConfigs - 1);
644 auto const height_delta = size_delta.height() / (kNumConfigs - 1);
645 CreateDemuxerStream(TestVideoConfig::NormalCodedSize(),
646 gfx::Vector2dF(width_delta, height_delta));
649 // Initially we should be using a software decoder
650 EXPECT_TRUE(decoder_);
651 EXPECT_FALSE(decoder_->IsPlatformDecoder());
655 // We should end up on a hardware decoder
656 EXPECT_TRUE(decoder_->IsPlatformDecoder());
659 // Tests that the decoder stream will switch from a hardware decoder to a
660 // software decoder if the config size decreases
661 TEST_P(VideoDecoderStreamTest, ConfigChangeHwToSw) {
662 EnablePlatformDecoders({1});
664 // Create a demuxer stream with a config that progressively decreases in size
665 auto const size_delta =
666 TestVideoConfig::LargeCodedSize() - TestVideoConfig::NormalCodedSize();
667 auto const width_delta = size_delta.width() / kNumConfigs;
668 auto const height_delta = size_delta.height() / kNumConfigs;
669 CreateDemuxerStream(TestVideoConfig::LargeCodedSize(),
670 gfx::Vector2dF(-width_delta, -height_delta));
673 // We should initially be using a hardware decoder
674 EXPECT_TRUE(decoder_);
675 EXPECT_TRUE(decoder_->IsPlatformDecoder());
678 // We should end up on a software decoder
679 EXPECT_FALSE(decoder_->IsPlatformDecoder());
682 TEST_P(VideoDecoderStreamTest, Read_ProperMetadata) {
683 // For testing simplicity, omit parallel decode tests with a delay in frames.
684 if (GetParam().parallel_decoding > 1 && GetParam().decoding_delay > 0)
687 if (GetParam().has_prepare) {
688 // Override the basic PrepareFrame() for a version that moves the MockTime
689 // by kPrepareDelay. This simulates real work done (e.g. YUV conversion).
690 video_decoder_stream_->SetPrepareCB(
691 base::BindRepeating(&VideoDecoderStreamTest::PrepareFrameWithDelay,
692 base::Unretained(this)));
695 constexpr base::TimeDelta kDecodeDelay = base::Milliseconds(10);
699 // Simulate time elapsed by the decoder.
700 EnterPendingState(DECODER_DECODE);
701 task_environment_.FastForwardBy(kDecodeDelay);
703 SatisfyPendingCallback(DECODER_DECODE);
705 EXPECT_TRUE(frame_read_);
707 const VideoFrameMetadata& metadata = frame_read_->metadata();
709 // Verify the decoding metadata is accurate.
710 EXPECT_EQ(*metadata.decode_end_time - *metadata.decode_begin_time,
713 // Verify the processing metadata is accurate.
714 const base::TimeDelta expected_processing_time =
715 GetParam().has_prepare ? (kDecodeDelay + kPrepareDelay) : kDecodeDelay;
717 EXPECT_EQ(*metadata.processing_time, expected_processing_time);
720 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxer) {
722 demuxer_stream_->HoldNextRead();
724 EXPECT_TRUE(pending_read_);
726 int demuxed_buffers = 0;
728 // Pass frames from the demuxer to the VideoDecoderStream until the first read
729 // request is satisfied.
730 while (pending_read_) {
732 demuxer_stream_->SatisfyReadAndHoldNext();
733 base::RunLoop().RunUntilIdle();
736 EXPECT_EQ(std::min(GetParam().decoding_delay + 1, kNumBuffersInOneConfig + 1),
739 // At this point the stream is waiting on read from the demuxer, but there is
740 // no pending read from the stream. The stream should be blocked if we try
741 // reading from it again.
744 demuxer_stream_->SatisfyRead();
745 base::RunLoop().RunUntilIdle();
746 EXPECT_FALSE(pending_read_);
749 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxerAndDecoder) {
750 // Test applies only when the decoder allows multiple parallel requests.
751 if (GetParam().parallel_decoding == 1)
755 demuxer_stream_->HoldNextRead();
756 decoder_->HoldDecode();
758 EXPECT_TRUE(pending_read_);
760 int demuxed_buffers = 0;
762 // Pass frames from the demuxer to the VideoDecoderStream until the first read
763 // request is satisfied, while always keeping one decode request pending.
764 while (pending_read_) {
766 demuxer_stream_->SatisfyReadAndHoldNext();
767 base::RunLoop().RunUntilIdle();
769 // Always keep one decode request pending.
770 if (demuxed_buffers > 1) {
771 decoder_->SatisfySingleDecode();
772 base::RunLoop().RunUntilIdle();
777 EXPECT_TRUE(pending_read_);
779 // Unblocking one decode request should unblock read even when demuxer is
781 decoder_->SatisfySingleDecode();
782 base::RunLoop().RunUntilIdle();
783 EXPECT_FALSE(pending_read_);
785 // Stream should still be blocked on the demuxer after unblocking the decoder.
786 decoder_->SatisfyDecode();
788 EXPECT_TRUE(pending_read_);
790 // Verify that the stream has returned all frames that have been demuxed,
791 // accounting for the decoder delay.
792 EXPECT_EQ(demuxed_buffers - GetParam().decoding_delay, num_decoded_frames_);
794 // Unblocking the demuxer will unblock the stream.
795 demuxer_stream_->SatisfyRead();
796 base::RunLoop().RunUntilIdle();
797 EXPECT_FALSE(pending_read_);
800 TEST_P(VideoDecoderStreamTest, Read_DuringEndOfStreamDecode) {
801 // Test applies only when the decoder allows multiple parallel requests, and
802 // they are not satisfied in a single batch.
803 if (GetParam().parallel_decoding == 1 || GetParam().decoding_delay != 0)
807 decoder_->HoldDecode();
809 // Read all of the frames up to end of stream. Since parallel decoding is
810 // enabled, the end of stream buffer will be sent to the decoder immediately,
811 // but we don't satisfy it yet.
812 for (int configuration = 0; configuration < kNumConfigs; configuration++) {
813 for (int frame = 0; frame < kNumBuffersInOneConfig; frame++) {
815 while (pending_read_) {
816 decoder_->SatisfySingleDecode();
817 base::RunLoop().RunUntilIdle();
822 // Read() again. The callback must be delayed until the decode completes.
824 ASSERT_TRUE(pending_read_);
826 // Satisfy decoding of the end of stream buffer. The read should complete.
827 decoder_->SatisfySingleDecode();
828 base::RunLoop().RunUntilIdle();
829 ASSERT_FALSE(pending_read_);
830 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
832 // The read output should indicate end of stream.
833 ASSERT_TRUE(frame_read_.get());
834 EXPECT_TRUE(frame_read_->metadata().end_of_stream);
837 TEST_P(VideoDecoderStreamTest, Read_DemuxerStreamReadError) {
839 EnterPendingState(DEMUXER_READ_NORMAL);
843 if (GetParam().is_encrypted && GetParam().has_decryptor) {
845 HasSubstr("DecryptingDemuxerStream: demuxer stream read error"));
847 EXPECT_MEDIA_LOG(HasSubstr("video demuxer stream read error"));
849 demuxer_stream_->Error();
850 base::RunLoop().RunUntilIdle();
852 ASSERT_FALSE(pending_read_);
853 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
854 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
857 // No Reset() before initialization is successfully completed.
858 TEST_P(VideoDecoderStreamTest, Reset_AfterInitialization) {
864 TEST_P(VideoDecoderStreamTest, Reset_DuringReinitialization) {
867 EnterPendingState(DECODER_REINIT);
868 // VideoDecoder::Reset() is not called when we reset during reinitialization.
869 pending_reset_ = true;
870 video_decoder_stream_->Reset(
871 base::BindOnce(&VideoDecoderStreamTest::OnReset, base::Unretained(this)));
872 SatisfyPendingCallback(DECODER_REINIT);
876 TEST_P(VideoDecoderStreamTest, Reset_AfterReinitialization) {
878 EnterPendingState(DECODER_REINIT);
879 SatisfyPendingCallback(DECODER_REINIT);
884 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_Normal) {
886 EnterPendingState(DEMUXER_READ_NORMAL);
887 EnterPendingState(DECODER_RESET);
888 SatisfyPendingCallback(DEMUXER_READ_NORMAL);
889 SatisfyPendingCallback(DECODER_RESET);
893 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_ConfigChange) {
895 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
896 EnterPendingState(DECODER_RESET);
897 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
898 SatisfyPendingCallback(DECODER_RESET);
902 TEST_P(VideoDecoderStreamTest, Reset_DuringNormalDecoderDecode) {
904 EnterPendingState(DECODER_DECODE);
905 EnterPendingState(DECODER_RESET);
906 SatisfyPendingCallback(DECODER_DECODE);
907 SatisfyPendingCallback(DECODER_RESET);
911 TEST_P(VideoDecoderStreamTest, Reset_AfterNormalRead) {
918 TEST_P(VideoDecoderStreamTest, Reset_AfterDemuxerRead_ConfigChange) {
920 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
921 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
926 TEST_P(VideoDecoderStreamTest, Reset_AfterEndOfStream) {
930 num_decoded_frames_ = 0;
931 demuxer_stream_->SeekToStart();
935 TEST_P(VideoDecoderStreamTest, Reset_DuringNoKeyRead) {
937 EnterPendingState(DECRYPTOR_NO_KEY);
941 // In the following Destroy_* tests, |video_decoder_stream_| is destroyed in
942 // VideoDecoderStreamTest destructor.
944 TEST_P(VideoDecoderStreamTest, Destroy_BeforeInitialization) {}
946 TEST_P(VideoDecoderStreamTest, Destroy_DuringInitialization) {
947 HoldDecoderInitOnSelection({0});
951 TEST_P(VideoDecoderStreamTest, Destroy_AfterInitialization) {
955 TEST_P(VideoDecoderStreamTest, Destroy_DuringReinitialization) {
957 EnterPendingState(DECODER_REINIT);
960 TEST_P(VideoDecoderStreamTest, Destroy_AfterReinitialization) {
962 EnterPendingState(DECODER_REINIT);
963 SatisfyPendingCallback(DECODER_REINIT);
966 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_Normal) {
968 EnterPendingState(DEMUXER_READ_NORMAL);
971 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_ConfigChange) {
973 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
976 TEST_P(VideoDecoderStreamTest, Destroy_DuringNormalDecoderDecode) {
978 EnterPendingState(DECODER_DECODE);
981 TEST_P(VideoDecoderStreamTest, Destroy_AfterNormalRead) {
986 TEST_P(VideoDecoderStreamTest, Destroy_AfterConfigChangeRead) {
988 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
989 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
992 TEST_P(VideoDecoderStreamTest, Destroy_DuringDecoderReinitialization) {
994 EnterPendingState(DECODER_REINIT);
997 TEST_P(VideoDecoderStreamTest, Destroy_DuringNoKeyRead) {
999 EnterPendingState(DECRYPTOR_NO_KEY);
1002 TEST_P(VideoDecoderStreamTest, Destroy_DuringReset) {
1004 EnterPendingState(DECODER_RESET);
1007 TEST_P(VideoDecoderStreamTest, Destroy_AfterReset) {
1012 TEST_P(VideoDecoderStreamTest, Destroy_DuringRead_DuringReset) {
1014 EnterPendingState(DECODER_DECODE);
1015 EnterPendingState(DECODER_RESET);
1018 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_DuringReset) {
1020 EnterPendingState(DECODER_DECODE);
1021 EnterPendingState(DECODER_RESET);
1022 SatisfyPendingCallback(DECODER_DECODE);
1025 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_AfterReset) {
1031 // The following tests cover the fallback logic after reinitialization error or
1032 // decode error of the first buffer after initialization.
1034 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeError) {
1036 decoder_->SimulateError();
1039 // |video_decoder_stream_| should have fallen back to a new decoder.
1040 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1042 ASSERT_FALSE(pending_read_);
1043 ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1045 // Check that we fell back to Decoder2.
1046 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1048 // Verify no frame was dropped.
1052 TEST_P(VideoDecoderStreamTest,
1053 FallbackDecoder_EndOfStreamReachedBeforeFallback) {
1054 // Only consider cases where there is a decoder delay. For test simplicity,
1055 // omit the parallel case.
1056 if (GetParam().decoding_delay == 0 || GetParam().parallel_decoding > 1)
1060 decoder_->HoldDecode();
1063 // One buffer should have already pulled from the demuxer stream. Set the next
1064 // one to be an EOS.
1065 demuxer_stream_->SeekToEndOfStream();
1067 decoder_->SatisfySingleDecode();
1068 base::RunLoop().RunUntilIdle();
1070 // |video_decoder_stream_| should not have emitted a frame.
1071 EXPECT_TRUE(pending_read_);
1073 // Pending buffers should contain a regular buffer and an EOS buffer.
1074 EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 2);
1076 decoder_->SimulateError();
1077 base::RunLoop().RunUntilIdle();
1079 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1081 // A frame should have been emitted.
1082 EXPECT_FALSE(pending_read_);
1083 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1084 EXPECT_FALSE(frame_read_->metadata().end_of_stream);
1085 EXPECT_GT(decoder_->total_bytes_decoded(), 0);
1089 EXPECT_FALSE(pending_read_);
1090 EXPECT_EQ(0, video_decoder_stream_->get_fallback_buffers_size_for_testing());
1091 EXPECT_TRUE(frame_read_->metadata().end_of_stream);
1094 TEST_P(VideoDecoderStreamTest,
1095 FallbackDecoder_DoesReinitializeStompPendingRead) {
1096 // Test only the case where there is no decoding delay and parallel decoding.
1097 if (GetParam().decoding_delay != 0 || GetParam().parallel_decoding <= 1)
1101 decoder_->HoldDecode();
1103 // Queue one read, defer the second.
1104 frame_read_ = nullptr;
1105 pending_read_ = true;
1106 video_decoder_stream_->Read(base::BindOnce(
1107 &VideoDecoderStreamTest::FrameReady, base::Unretained(this)));
1108 demuxer_stream_->HoldNextRead();
1110 // Force an error to occur on the first decode, but ensure it isn't propagated
1111 // until after the next read has been started.
1112 decoder_->SimulateError();
1113 HoldDecodeAfterSelection({1});
1115 // Complete the fallback to the second decoder with the read still pending.
1116 base::RunLoop().RunUntilIdle();
1118 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1120 // Can't check the original decoder right now, it might have been destroyed
1121 // already. Verify that there was nothing decoded until we kicked the decoder.
1122 EXPECT_EQ(decoder_->total_bytes_decoded(), 0);
1123 decoder_->SatisfyDecode();
1124 const int first_decoded_bytes = decoder_->total_bytes_decoded();
1125 ASSERT_GT(first_decoded_bytes, 0);
1127 // Satisfy the previously pending read and ensure it is decoded.
1128 demuxer_stream_->SatisfyRead();
1129 base::RunLoop().RunUntilIdle();
1130 ASSERT_GT(decoder_->total_bytes_decoded(), first_decoded_bytes);
1133 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeErrorRepeated) {
1136 // Hold other decoders to simulate errors.
1137 HoldDecodeAfterSelection({1, 2});
1139 // Simulate decode error to trigger the fallback path.
1140 decoder_->SimulateError();
1142 base::RunLoop().RunUntilIdle();
1144 // Expect decoder 1 to be tried.
1145 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1146 decoder_->SimulateError();
1147 base::RunLoop().RunUntilIdle();
1150 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1151 decoder_->SimulateError();
1152 base::RunLoop().RunUntilIdle();
1154 // No decoders left, expect failure.
1155 EXPECT_EQ(decoder_, nullptr);
1156 EXPECT_FALSE(pending_read_);
1157 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1158 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1161 // This tests verifies that we properly fallback to a new decoder if the first
1162 // decode after a config change fails.
1163 TEST_P(VideoDecoderStreamTest,
1164 FallbackDecoder_SelectedOnMidstreamDecodeErrorAfterReinitialization) {
1165 // For simplicity of testing, this test applies only when there is no decoder
1166 // delay and parallel decoding is disabled.
1167 if (GetParam().decoding_delay != 0 || GetParam().parallel_decoding > 1)
1172 // Note: Completes decoding one frame, results in Decode() being called with
1173 // second frame that is not completed.
1176 // Verify that the first frame was decoded successfully.
1177 EXPECT_FALSE(pending_read_);
1178 EXPECT_GT(decoder_->total_bytes_decoded(), 0);
1179 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1181 // Continue up to the point of reinitialization.
1182 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1184 // Hold decodes to prevent a frame from being outputted upon reinitialization.
1185 decoder_->HoldDecode();
1186 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1188 // DecoderStream sends an EOS to flush the decoder during config changes.
1189 // Let the EOS decode be satisfied to properly complete the decoder reinit.
1190 decoder_->SatisfySingleDecode();
1191 base::RunLoop().RunUntilIdle();
1192 EXPECT_TRUE(pending_read_);
1194 // Fail the first decode, before a frame can be outputted.
1195 decoder_->SimulateError();
1196 base::RunLoop().RunUntilIdle();
1200 // Verify that fallback happened.
1201 EXPECT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1202 EXPECT_FALSE(pending_read_);
1203 EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1204 EXPECT_GT(decoder_->total_bytes_decoded(), 0);
1207 TEST_P(VideoDecoderStreamTest,
1208 FallbackDecoder_DecodeErrorRepeated_AfterReinitialization) {
1211 // Simulate decode error to trigger fallback.
1212 decoder_->SimulateError();
1214 base::RunLoop().RunUntilIdle();
1216 // Simulate reinitialize error of decoder 1.
1217 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1218 decoder_->SimulateFailureToInit();
1219 HoldDecodeAfterSelection({0, 1, 2});
1220 ReadUntilDecoderReinitialized();
1222 // Decoder 0 should be selected again.
1223 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1224 decoder_->SimulateError();
1225 base::RunLoop().RunUntilIdle();
1228 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1229 decoder_->SimulateError();
1230 base::RunLoop().RunUntilIdle();
1233 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1234 decoder_->SimulateError();
1235 base::RunLoop().RunUntilIdle();
1237 // No decoders left.
1238 EXPECT_EQ(decoder_, nullptr);
1239 EXPECT_FALSE(pending_read_);
1240 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1241 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1244 TEST_P(VideoDecoderStreamTest,
1245 FallbackDecoder_ConfigChangeClearsPendingBuffers) {
1246 // Test case is only interesting if the decoder can receive a config change
1247 // before returning its first frame.
1248 if (GetParam().decoding_delay < kNumBuffersInOneConfig)
1252 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1253 ASSERT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1255 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1256 ASSERT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1257 EXPECT_FALSE(pending_read_);
1262 TEST_P(VideoDecoderStreamTest,
1263 FallbackDecoder_ErrorDuringConfigChangeFlushing) {
1264 // Test case is only interesting if the decoder can receive a config change
1265 // before returning its first frame.
1266 if (GetParam().decoding_delay < kNumBuffersInOneConfig)
1270 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1271 EXPECT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1273 decoder_->HoldDecode();
1274 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1276 // The flush request should have been sent and held.
1277 EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1278 EXPECT_TRUE(pending_read_);
1280 // Triggering an error here will cause the frames in selected decoder to be
1281 // lost. There are no pending buffers to give to |decoders_[1]| due to
1282 // http://crbug.com/603713
1283 decoder_->SimulateError();
1284 base::RunLoop().RunUntilIdle();
1286 // We want to make sure the fallback decoder can decode the rest of the frames
1287 // in the demuxer stream.
1288 ReadAllFrames(kNumBuffersInOneConfig * (kNumConfigs - 1));
1291 TEST_P(VideoDecoderStreamTest,
1292 FallbackDecoder_PendingBuffersIsFilledAndCleared) {
1293 // Test applies only when there is a decoder delay, and the decoder will not
1294 // receive a config change before outputting its first frame. Parallel
1295 // decoding is also disabled in this test case, for readability and simplicity
1296 // of the unit test.
1297 if (GetParam().decoding_delay == 0 ||
1298 GetParam().decoding_delay > kNumBuffersInOneConfig ||
1299 GetParam().parallel_decoding > 1) {
1305 // Block on demuxer read and decoder decode so we can step through.
1306 demuxer_stream_->HoldNextRead();
1307 decoder_->HoldDecode();
1310 int demuxer_reads_satisfied = 0;
1311 // Send back and requests buffers until the next one would fill the decoder
1313 while (demuxer_reads_satisfied < GetParam().decoding_delay - 1) {
1314 // Send a buffer back.
1315 demuxer_stream_->SatisfyReadAndHoldNext();
1316 base::RunLoop().RunUntilIdle();
1317 ++demuxer_reads_satisfied;
1319 // Decode one buffer.
1320 decoder_->SatisfySingleDecode();
1321 base::RunLoop().RunUntilIdle();
1322 EXPECT_TRUE(pending_read_);
1323 EXPECT_EQ(demuxer_reads_satisfied,
1324 video_decoder_stream_->get_pending_buffers_size_for_testing());
1325 // No fallback buffers should be queued up yet.
1327 video_decoder_stream_->get_fallback_buffers_size_for_testing());
1330 // Hold the init before triggering the error, to verify internal state.
1331 demuxer_stream_->SatisfyReadAndHoldNext();
1332 ++demuxer_reads_satisfied;
1334 decoder_->SimulateError();
1336 HoldDecoderInitOnSelection({1});
1337 HoldDecodeAfterSelection({1});
1339 base::RunLoop().RunUntilIdle();
1341 EXPECT_TRUE(pending_read_);
1342 EXPECT_EQ(demuxer_reads_satisfied,
1343 video_decoder_stream_->get_pending_buffers_size_for_testing());
1345 decoders_[1]->SatisfyInit();
1346 base::RunLoop().RunUntilIdle();
1348 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1350 // Make sure the pending buffers have been transferred to fallback buffers.
1351 // One call to Decode() during the initialization process, so we expect one
1352 // buffer to already have been consumed from the fallback buffers.
1353 // Pending buffers should never go down (unless we encounter a config change)
1354 EXPECT_EQ(demuxer_reads_satisfied - 1,
1355 video_decoder_stream_->get_fallback_buffers_size_for_testing());
1356 EXPECT_EQ(demuxer_reads_satisfied,
1357 video_decoder_stream_->get_pending_buffers_size_for_testing());
1359 decoder_->SatisfyDecode();
1360 base::RunLoop().RunUntilIdle();
1362 // Make sure all buffers consumed by |decoders_| have come from the fallback.
1363 // Pending buffers should not have been cleared yet.
1364 EXPECT_EQ(0, video_decoder_stream_->get_fallback_buffers_size_for_testing());
1365 EXPECT_EQ(demuxer_reads_satisfied,
1366 video_decoder_stream_->get_pending_buffers_size_for_testing());
1367 EXPECT_TRUE(pending_read_);
1369 // Give the decoder one more buffer, enough to release a frame.
1370 demuxer_stream_->SatisfyReadAndHoldNext();
1371 base::RunLoop().RunUntilIdle();
1373 // New buffers should not have been added after the frame was released.
1374 EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1375 EXPECT_FALSE(pending_read_);
1377 demuxer_stream_->SatisfyRead();
1379 // Confirm no frames were dropped.
1383 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnDecodeThenInitErrors) {
1385 decoder_->SimulateError();
1386 FailDecoderInitOnSelection({1});
1389 // Decoder 0 should be blocked, and decoder 1 fails to initialize, so
1390 // |video_decoder_stream_| should have fallen back to decoder 2.
1391 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1393 ASSERT_FALSE(pending_read_);
1394 ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1396 // Can't check previously selected decoder(s) right now, they might have been
1397 // destroyed already.
1398 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1400 // Verify no frame was dropped.
1404 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnInitThenDecodeErrors) {
1405 FailDecoderInitOnSelection({0});
1407 ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1408 ClearDecoderInitExpectations();
1410 decoder_->HoldDecode();
1412 decoder_->SimulateError();
1413 base::RunLoop().RunUntilIdle();
1415 // |video_decoder_stream_| should have fallen back to decoder 2.
1416 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1418 ASSERT_FALSE(pending_read_);
1419 ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1421 // Can't check previously selected decoder(s) right now, they might have been
1422 // destroyed already.
1423 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1425 // Verify no frame was dropped.
1429 TEST_P(VideoDecoderStreamTest,
1430 FallbackDecoder_NotSelectedOnMidstreamDecodeError) {
1434 // Successfully received a frame.
1435 EXPECT_FALSE(pending_read_);
1436 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1438 decoder_->SimulateError();
1440 // The error must surface from Read() as DECODE_ERROR.
1441 while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1443 base::RunLoop().RunUntilIdle();
1444 EXPECT_FALSE(pending_read_);
1447 // Verify the error was surfaced, rather than falling back to other decoders.
1448 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1449 EXPECT_FALSE(pending_read_);
1450 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1451 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1454 TEST_P(VideoDecoderStreamTest, DecoderErrorWhenNotReading) {
1456 decoder_->HoldDecode();
1458 EXPECT_TRUE(pending_read_);
1460 // Satisfy decode requests until we get the first frame out.
1461 while (pending_read_) {
1462 decoder_->SatisfySingleDecode();
1463 base::RunLoop().RunUntilIdle();
1466 // Trigger an error in the decoding.
1467 decoder_->SimulateError();
1469 // The error must surface from Read() as DECODE_ERROR.
1470 while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1472 base::RunLoop().RunUntilIdle();
1473 EXPECT_FALSE(pending_read_);
1475 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1476 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1479 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Once) {
1481 decoder_->SimulateFailureToInit();
1482 ReadUntilDecoderReinitialized();
1483 // Should have fallen back to a new instance of decoder 0.
1484 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1486 ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1489 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Twice) {
1492 // Trigger reinitialization error, and fallback to a new instance.
1493 decoder_->SimulateFailureToInit();
1494 ReadUntilDecoderReinitialized();
1495 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1499 // Trigger reinitialization error again. Since a frame was output, this will
1500 // be a new instance of decoder 0 again.
1501 decoder_->SimulateFailureToInit();
1502 ReadUntilDecoderReinitialized();
1503 ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1507 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_OneUnsupportedDecoder) {
1510 // The current decoder will fail to reinitialize.
1511 decoder_->SimulateFailureToInit();
1513 // Decoder 1 will also fail to initialize on decoder selection.
1514 FailDecoderInitOnSelection({0, 1});
1516 ReadUntilDecoderReinitialized();
1518 // As a result, decoder 2 will be selected.
1519 ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1524 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_NoSupportedDecoder) {
1527 // The current decoder will fail to reinitialize, triggering decoder
1529 decoder_->SimulateFailureToInit();
1531 // All of the decoders will fail in decoder selection.
1532 FailDecoderInitOnSelection({0, 1, 2});
1534 ReadUntilDecoderReinitialized();
1536 // The error will surface from Read() as DECODE_ERROR.
1537 while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1539 base::RunLoop().RunUntilIdle();
1540 EXPECT_FALSE(pending_read_);
1542 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1543 EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1546 TEST_P(VideoDecoderStreamTest, Destroy_DuringFallbackDecoderSelection) {
1548 decoder_->SimulateFailureToInit();
1549 EnterPendingState(DECODER_REINIT);
1550 HoldDecoderInitOnSelection({1});
1551 SatisfyPendingCallback(DECODER_REINIT);
1554 } // namespace media