Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / video_decoder_stream_unittest.cc
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.
4
5 #include <memory>
6 #include <utility>
7 #include <vector>
8
9 #include "base/bind.h"
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"
26
27 #if !BUILDFLAG(IS_ANDROID)
28 #include "media/filters/decrypting_video_decoder.h"
29 #endif
30
31 #include <iostream>
32
33 using ::base::test::RunCallback;
34 using ::base::test::RunOnceCallback;
35 using ::testing::_;
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;
46
47 namespace media {
48
49 namespace {
50 const int kNumConfigs = 4;
51 const int kNumBuffersInOneConfig = 5;
52 constexpr base::TimeDelta kPrepareDelay = base::Milliseconds(5);
53
54 static int GetDecoderId(int i) {
55   return i;
56 }
57
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;
65 }
66
67 }  // namespace
68
69 struct VideoDecoderStreamTestParams {
70   VideoDecoderStreamTestParams(bool is_encrypted,
71                                bool has_decryptor,
72                                bool has_prepare,
73                                int decoding_delay,
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) {}
80
81   bool is_encrypted;
82   bool has_decryptor;
83   bool has_prepare;
84   int decoding_delay;
85   int parallel_decoding;
86 };
87
88 class VideoDecoderStreamTest
89     : public testing::Test,
90       public testing::WithParamInterface<VideoDecoderStreamTestParams> {
91  public:
92   VideoDecoderStreamTest()
93       : is_initialized_(false),
94         num_decoded_frames_(0),
95         pending_initialize_(false),
96         pending_read_(false),
97         pending_reset_(false),
98         pending_stop_(false),
99         num_decoded_bytes_unreported_(0),
100         has_no_key_(false) {
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)),
106         &media_log_);
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)));
116     }
117
118     if (GetParam().is_encrypted && GetParam().has_decryptor) {
119       decryptor_ = std::make_unique<NiceMock<MockDecryptor>>();
120
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));
127     }
128
129     if (GetParam().is_encrypted) {
130       cdm_context_ = std::make_unique<StrictMock<MockCdmContext>>();
131
132       EXPECT_CALL(*cdm_context_, RegisterEventCB(_)).Times(AnyNumber());
133       EXPECT_CALL(*cdm_context_, GetDecryptor())
134           .WillRepeatedly(Return(decryptor_.get()));
135     }
136
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   }
145
146   VideoDecoderStreamTest(const VideoDecoderStreamTest&) = delete;
147   VideoDecoderStreamTest& operator=(const VideoDecoderStreamTest&) = delete;
148
149   ~VideoDecoderStreamTest() {
150     // Check that the pipeline statistics callback was fired correctly.
151     EXPECT_EQ(num_decoded_bytes_unreported_, 0);
152
153     is_initialized_ = false;
154     decoders_.clear();
155     video_decoder_stream_.reset();
156     base::RunLoop().RunUntilIdle();
157
158     DCHECK(!pending_initialize_);
159     DCHECK(!pending_read_);
160     DCHECK(!pending_reset_);
161     DCHECK(!pending_stop_);
162   }
163
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);
169   }
170
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(
175         FROM_HERE,
176         base::BindOnce(std::move(output_ready_cb), std::move(frame)));
177   }
178
179   void PrepareFrameWithDelay(
180       scoped_refptr<VideoFrame> frame,
181       VideoDecoderStream::OutputReadyCB output_ready_cb) {
182     task_environment_.FastForwardBy(kPrepareDelay);
183     task_environment_.GetMainThreadTaskRunner()->PostTask(
184         FROM_HERE,
185         base::BindOnce(std::move(output_ready_cb), std::move(frame)));
186   }
187
188   void OnBytesDecoded(int count) { num_decoded_bytes_unreported_ += count; }
189
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.
199     decoders_.clear();
200
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;
206
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_));
212 #endif
213
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)));
220
221       if (GetParam().is_encrypted && !GetParam().has_decryptor)
222         decoder->EnableEncryptedConfigSupport();
223
224       // Keep a reference so we can change the behavior of each decoder.
225       decoders_.push_back(decoder->GetWeakPtr());
226
227       decoders.push_back(std::move(decoder));
228     }
229
230     for (const auto i : decoder_indices_to_fail_init_)
231       decoders_[i]->SimulateFailureToInit();
232
233     for (const auto i : decoder_indices_to_hold_init_)
234       decoders_[i]->HoldNextInit();
235
236     for (const auto i : decoder_indices_to_hold_decode_)
237       decoders_[i]->HoldDecode();
238
239     for (const auto i : platform_decoder_indices_)
240       decoders_[i]->SetIsPlatformDecoder(true);
241
242     return decoders;
243   }
244
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();
250   }
251
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();
259     }
260   }
261
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();
269     }
270   }
271
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();
280     }
281   }
282
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);
288     }
289   }
290
291   // Updates the |decoder_| currently being used by VideoDecoderStream.
292   void OnDecoderChanged(VideoDecoder* decoder) {
293     if (!decoder) {
294       decoder_ = nullptr;
295       return;
296     }
297
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));
304   }
305
306   MOCK_METHOD1(OnWaiting, void(WaitingReason));
307
308   void OnStatistics(const PipelineStatistics& statistics) {
309     num_decoded_bytes_unreported_ -= statistics.video_bytes_decoded;
310   }
311
312   void OnInitialized(bool success) {
313     DCHECK(!pending_read_);
314     DCHECK(!pending_reset_);
315     DCHECK(pending_initialize_);
316     pending_initialize_ = false;
317
318     is_initialized_ = success;
319     if (!success)
320       decoders_.clear();
321   }
322
323   void Initialize() {
324     if (!demuxer_stream_) {
325       demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
326           kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted);
327     }
328
329     pending_initialize_ = true;
330     video_decoder_stream_->Initialize(
331         demuxer_stream_.get(),
332         base::BindOnce(&VideoDecoderStreamTest::OnInitialized,
333                        base::Unretained(this)),
334         cdm_context_.get(),
335         base::BindRepeating(&VideoDecoderStreamTest::OnStatistics,
336                             base::Unretained(this)),
337         base::BindRepeating(&VideoDecoderStreamTest::OnWaiting,
338                             base::Unretained(this)));
339
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();
345   }
346
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());
353     if (has_no_key_) {
354       std::move(decrypt_cb).Run(Decryptor::kNoKey, nullptr);
355       return;
356     }
357
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);
366   }
367
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()
375             : nullptr;
376     frame_read_ = frame;
377     if (frame && !frame->metadata().end_of_stream) {
378       EXPECT_EQ(*frame->metadata().frame_duration, demuxer_stream_->duration());
379
380       num_decoded_frames_++;
381     }
382     pending_read_ = false;
383   }
384
385   void OnReset() {
386     DCHECK(!pending_read_);
387     DCHECK(pending_reset_);
388     pending_reset_ = false;
389   }
390
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();
397   }
398
399   void ReadUntilPending() {
400     do {
401       ReadOneFrame();
402     } while (!pending_read_);
403   }
404
405   void ReadAllFrames(int expected_decoded_frames) {
406     // Reading all frames reinitializes the demuxer.
407     do {
408       ReadOneFrame();
409     } while (frame_read_.get() && !frame_read_->metadata().end_of_stream);
410
411     DCHECK_EQ(expected_decoded_frames, num_decoded_frames_);
412   }
413
414   void ReadAllFrames() {
415     // No frames should have been dropped.
416     ReadAllFrames(kNumConfigs * kNumBuffersInOneConfig);
417   }
418
419   enum PendingState {
420     NOT_PENDING,
421     DEMUXER_READ_NORMAL,
422     DEMUXER_READ_CONFIG_CHANGE,
423     DECRYPTOR_NO_KEY,
424     DECODER_REINIT,
425     DECODER_DECODE,
426     DECODER_RESET
427   };
428
429   void EnterPendingState(PendingState state) {
430     DCHECK_NE(state, NOT_PENDING);
431     switch (state) {
432       case DEMUXER_READ_NORMAL:
433         demuxer_stream_->HoldNextRead();
434         ReadUntilPending();
435         break;
436
437       case DEMUXER_READ_CONFIG_CHANGE:
438         demuxer_stream_->HoldNextConfigChangeRead();
439         ReadUntilPending();
440         break;
441
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));
446           has_no_key_ = true;
447         }
448         ReadOneFrame();
449         break;
450
451       case DECODER_REINIT:
452         decoder_->HoldNextInit();
453         ReadUntilPending();
454         break;
455
456       case DECODER_DECODE:
457         decoder_->HoldDecode();
458         ReadUntilPending();
459         break;
460
461       case DECODER_RESET:
462         decoder_->HoldNextReset();
463         pending_reset_ = true;
464         video_decoder_stream_->Reset(base::BindOnce(
465             &VideoDecoderStreamTest::OnReset, base::Unretained(this)));
466         base::RunLoop().RunUntilIdle();
467         break;
468
469       case NOT_PENDING:
470         NOTREACHED();
471         break;
472     }
473   }
474
475   void SatisfyPendingCallback(PendingState state) {
476     DCHECK_NE(state, NOT_PENDING);
477     switch (state) {
478       case DEMUXER_READ_CONFIG_CHANGE:
479         EXPECT_MEDIA_LOG(HasSubstr("decoder config changed"))
480             .Times(testing::AtLeast(1));
481         [[fallthrough]];
482       case DEMUXER_READ_NORMAL:
483         demuxer_stream_->SatisfyRead();
484         break;
485
486       // This is only interesting to test during VideoDecoderStream destruction.
487       // There's no need to satisfy a callback.
488       case DECRYPTOR_NO_KEY:
489         NOTREACHED();
490         break;
491
492       case DECODER_REINIT:
493         decoder_->SatisfyInit();
494         break;
495
496       case DECODER_DECODE:
497         decoder_->SatisfyDecode();
498         break;
499
500       case DECODER_RESET:
501         decoder_->SatisfyReset();
502         break;
503
504       case NOT_PENDING:
505         NOTREACHED();
506         break;
507     }
508
509     base::RunLoop().RunUntilIdle();
510   }
511
512   void Read() {
513     EnterPendingState(DECODER_DECODE);
514     SatisfyPendingCallback(DECODER_DECODE);
515   }
516
517   void Reset() {
518     EnterPendingState(DECODER_RESET);
519     SatisfyPendingCallback(DECODER_RESET);
520   }
521
522   void ReadUntilDecoderReinitialized() {
523     EnterPendingState(DECODER_REINIT);
524     SatisfyPendingCallback(DECODER_REINIT);
525   }
526
527   base::test::SingleThreadTaskEnvironment task_environment_{
528       base::test::TaskEnvironment::TimeSource::MOCK_TIME};
529
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_;
534
535   // Use NiceMock since we don't care about most of calls on the decryptor.
536   std::unique_ptr<NiceMock<MockDecryptor>> decryptor_;
537
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_;
542
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_;
547
548   // The current decoder used by |video_decoder_stream_|.
549   raw_ptr<FakeVideoDecoder> decoder_ = nullptr;
550
551   bool is_initialized_;
552   int num_decoded_frames_;
553   bool pending_initialize_;
554   bool pending_read_;
555   bool pending_reset_;
556   bool pending_stop_;
557   int num_decoded_bytes_unreported_;
558   scoped_refptr<VideoFrame> frame_read_;
559   DecoderStatus::Codes last_read_status_code_;
560
561   // Decryptor has no key to decrypt a frame.
562   bool has_no_key_;
563 };
564
565 INSTANTIATE_TEST_SUITE_P(
566     Clear,
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)));
573
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)));
579
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)));
585
586 INSTANTIATE_TEST_SUITE_P(
587     Clear_Parallel,
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)));
593
594 TEST_P(VideoDecoderStreamTest, CanReadWithoutStallingAtAnyTime) {
595   ASSERT_FALSE(video_decoder_stream_->CanReadWithoutStalling());
596 }
597
598 TEST_P(VideoDecoderStreamTest, Initialization) {
599   Initialize();
600   EXPECT_TRUE(is_initialized_);
601 }
602
603 TEST_P(VideoDecoderStreamTest, AllDecoderInitializationFails) {
604   FailDecoderInitOnSelection({0, 1, 2});
605   Initialize();
606   EXPECT_FALSE(is_initialized_);
607 }
608
609 TEST_P(VideoDecoderStreamTest, PartialDecoderInitializationFails) {
610   FailDecoderInitOnSelection({0, 1});
611   Initialize();
612   EXPECT_TRUE(is_initialized_);
613 }
614
615 TEST_P(VideoDecoderStreamTest, ReadOneFrame) {
616   Initialize();
617   Read();
618 }
619
620 TEST_P(VideoDecoderStreamTest, ReadAllFrames) {
621   Initialize();
622   ReadAllFrames();
623 }
624
625 TEST_P(VideoDecoderStreamTest, Read_AfterReset) {
626   Initialize();
627   Reset();
628   Read();
629   Reset();
630   Read();
631 }
632
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});
637
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));
645   Initialize();
646
647   // Initially we should be using a software decoder
648   EXPECT_TRUE(decoder_);
649   EXPECT_FALSE(decoder_->IsPlatformDecoder());
650
651   ReadAllFrames();
652
653   // We should end up on a hardware decoder
654   EXPECT_TRUE(decoder_->IsPlatformDecoder());
655 }
656
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});
661
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));
669   Initialize();
670
671   // We should initially be using a hardware decoder
672   EXPECT_TRUE(decoder_);
673   EXPECT_TRUE(decoder_->IsPlatformDecoder());
674   ReadAllFrames();
675
676   // We should end up on a software decoder
677   EXPECT_FALSE(decoder_->IsPlatformDecoder());
678 }
679
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)
683     return;
684
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)));
691   }
692
693   constexpr base::TimeDelta kDecodeDelay = base::Milliseconds(10);
694
695   Initialize();
696
697   // Simulate time elapsed by the decoder.
698   EnterPendingState(DECODER_DECODE);
699   task_environment_.FastForwardBy(kDecodeDelay);
700
701   SatisfyPendingCallback(DECODER_DECODE);
702
703   EXPECT_TRUE(frame_read_);
704
705   const VideoFrameMetadata& metadata = frame_read_->metadata();
706
707   // Verify the decoding metadata is accurate.
708   EXPECT_EQ(*metadata.decode_end_time - *metadata.decode_begin_time,
709             kDecodeDelay);
710
711   // Verify the processing metadata is accurate.
712   const base::TimeDelta expected_processing_time =
713       GetParam().has_prepare ? (kDecodeDelay + kPrepareDelay) : kDecodeDelay;
714
715   EXPECT_EQ(*metadata.processing_time, expected_processing_time);
716 }
717
718 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxer) {
719   Initialize();
720   demuxer_stream_->HoldNextRead();
721   ReadOneFrame();
722   EXPECT_TRUE(pending_read_);
723
724   int demuxed_buffers = 0;
725
726   // Pass frames from the demuxer to the VideoDecoderStream until the first read
727   // request is satisfied.
728   while (pending_read_) {
729     ++demuxed_buffers;
730     demuxer_stream_->SatisfyReadAndHoldNext();
731     base::RunLoop().RunUntilIdle();
732   }
733
734   EXPECT_EQ(std::min(GetParam().decoding_delay + 1, kNumBuffersInOneConfig + 1),
735             demuxed_buffers);
736
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.
740   ReadUntilPending();
741
742   demuxer_stream_->SatisfyRead();
743   base::RunLoop().RunUntilIdle();
744   EXPECT_FALSE(pending_read_);
745 }
746
747 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxerAndDecoder) {
748   // Test applies only when the decoder allows multiple parallel requests.
749   if (GetParam().parallel_decoding == 1)
750     return;
751
752   Initialize();
753   demuxer_stream_->HoldNextRead();
754   decoder_->HoldDecode();
755   ReadOneFrame();
756   EXPECT_TRUE(pending_read_);
757
758   int demuxed_buffers = 0;
759
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_) {
763     ++demuxed_buffers;
764     demuxer_stream_->SatisfyReadAndHoldNext();
765     base::RunLoop().RunUntilIdle();
766
767     // Always keep one decode request pending.
768     if (demuxed_buffers > 1) {
769       decoder_->SatisfySingleDecode();
770       base::RunLoop().RunUntilIdle();
771     }
772   }
773
774   ReadUntilPending();
775   EXPECT_TRUE(pending_read_);
776
777   // Unblocking one decode request should unblock read even when demuxer is
778   // still blocked.
779   decoder_->SatisfySingleDecode();
780   base::RunLoop().RunUntilIdle();
781   EXPECT_FALSE(pending_read_);
782
783   // Stream should still be blocked on the demuxer after unblocking the decoder.
784   decoder_->SatisfyDecode();
785   ReadUntilPending();
786   EXPECT_TRUE(pending_read_);
787
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_);
791
792   // Unblocking the demuxer will unblock the stream.
793   demuxer_stream_->SatisfyRead();
794   base::RunLoop().RunUntilIdle();
795   EXPECT_FALSE(pending_read_);
796 }
797
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)
802     return;
803
804   Initialize();
805   decoder_->HoldDecode();
806
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++) {
812       ReadOneFrame();
813       while (pending_read_) {
814         decoder_->SatisfySingleDecode();
815         base::RunLoop().RunUntilIdle();
816       }
817     }
818   }
819
820   // Read() again. The callback must be delayed until the decode completes.
821   ReadOneFrame();
822   ASSERT_TRUE(pending_read_);
823
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);
829
830   // The read output should indicate end of stream.
831   ASSERT_TRUE(frame_read_.get());
832   EXPECT_TRUE(frame_read_->metadata().end_of_stream);
833 }
834
835 TEST_P(VideoDecoderStreamTest, Read_DemuxerStreamReadError) {
836   Initialize();
837   EnterPendingState(DEMUXER_READ_NORMAL);
838
839   InSequence s;
840
841   if (GetParam().is_encrypted && GetParam().has_decryptor) {
842     EXPECT_MEDIA_LOG(
843         HasSubstr("DecryptingDemuxerStream: demuxer stream read error"));
844   }
845   EXPECT_MEDIA_LOG(HasSubstr("video demuxer stream read error"));
846
847   demuxer_stream_->Error();
848   base::RunLoop().RunUntilIdle();
849
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);
853 }
854
855 // No Reset() before initialization is successfully completed.
856 TEST_P(VideoDecoderStreamTest, Reset_AfterInitialization) {
857   Initialize();
858   Reset();
859   Read();
860 }
861
862 TEST_P(VideoDecoderStreamTest, Reset_DuringReinitialization) {
863   Initialize();
864
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);
871   Read();
872 }
873
874 TEST_P(VideoDecoderStreamTest, Reset_AfterReinitialization) {
875   Initialize();
876   EnterPendingState(DECODER_REINIT);
877   SatisfyPendingCallback(DECODER_REINIT);
878   Reset();
879   Read();
880 }
881
882 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_Normal) {
883   Initialize();
884   EnterPendingState(DEMUXER_READ_NORMAL);
885   EnterPendingState(DECODER_RESET);
886   SatisfyPendingCallback(DEMUXER_READ_NORMAL);
887   SatisfyPendingCallback(DECODER_RESET);
888   Read();
889 }
890
891 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_ConfigChange) {
892   Initialize();
893   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
894   EnterPendingState(DECODER_RESET);
895   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
896   SatisfyPendingCallback(DECODER_RESET);
897   Read();
898 }
899
900 TEST_P(VideoDecoderStreamTest, Reset_DuringNormalDecoderDecode) {
901   Initialize();
902   EnterPendingState(DECODER_DECODE);
903   EnterPendingState(DECODER_RESET);
904   SatisfyPendingCallback(DECODER_DECODE);
905   SatisfyPendingCallback(DECODER_RESET);
906   Read();
907 }
908
909 TEST_P(VideoDecoderStreamTest, Reset_AfterNormalRead) {
910   Initialize();
911   Read();
912   Reset();
913   Read();
914 }
915
916 TEST_P(VideoDecoderStreamTest, Reset_AfterDemuxerRead_ConfigChange) {
917   Initialize();
918   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
919   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
920   Reset();
921   Read();
922 }
923
924 TEST_P(VideoDecoderStreamTest, Reset_AfterEndOfStream) {
925   Initialize();
926   ReadAllFrames();
927   Reset();
928   num_decoded_frames_ = 0;
929   demuxer_stream_->SeekToStart();
930   ReadAllFrames();
931 }
932
933 TEST_P(VideoDecoderStreamTest, Reset_DuringNoKeyRead) {
934   Initialize();
935   EnterPendingState(DECRYPTOR_NO_KEY);
936   Reset();
937 }
938
939 // In the following Destroy_* tests, |video_decoder_stream_| is destroyed in
940 // VideoDecoderStreamTest destructor.
941
942 TEST_P(VideoDecoderStreamTest, Destroy_BeforeInitialization) {}
943
944 TEST_P(VideoDecoderStreamTest, Destroy_DuringInitialization) {
945   HoldDecoderInitOnSelection({0});
946   Initialize();
947 }
948
949 TEST_P(VideoDecoderStreamTest, Destroy_AfterInitialization) {
950   Initialize();
951 }
952
953 TEST_P(VideoDecoderStreamTest, Destroy_DuringReinitialization) {
954   Initialize();
955   EnterPendingState(DECODER_REINIT);
956 }
957
958 TEST_P(VideoDecoderStreamTest, Destroy_AfterReinitialization) {
959   Initialize();
960   EnterPendingState(DECODER_REINIT);
961   SatisfyPendingCallback(DECODER_REINIT);
962 }
963
964 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_Normal) {
965   Initialize();
966   EnterPendingState(DEMUXER_READ_NORMAL);
967 }
968
969 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_ConfigChange) {
970   Initialize();
971   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
972 }
973
974 TEST_P(VideoDecoderStreamTest, Destroy_DuringNormalDecoderDecode) {
975   Initialize();
976   EnterPendingState(DECODER_DECODE);
977 }
978
979 TEST_P(VideoDecoderStreamTest, Destroy_AfterNormalRead) {
980   Initialize();
981   Read();
982 }
983
984 TEST_P(VideoDecoderStreamTest, Destroy_AfterConfigChangeRead) {
985   Initialize();
986   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
987   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
988 }
989
990 TEST_P(VideoDecoderStreamTest, Destroy_DuringDecoderReinitialization) {
991   Initialize();
992   EnterPendingState(DECODER_REINIT);
993 }
994
995 TEST_P(VideoDecoderStreamTest, Destroy_DuringNoKeyRead) {
996   Initialize();
997   EnterPendingState(DECRYPTOR_NO_KEY);
998 }
999
1000 TEST_P(VideoDecoderStreamTest, Destroy_DuringReset) {
1001   Initialize();
1002   EnterPendingState(DECODER_RESET);
1003 }
1004
1005 TEST_P(VideoDecoderStreamTest, Destroy_AfterReset) {
1006   Initialize();
1007   Reset();
1008 }
1009
1010 TEST_P(VideoDecoderStreamTest, Destroy_DuringRead_DuringReset) {
1011   Initialize();
1012   EnterPendingState(DECODER_DECODE);
1013   EnterPendingState(DECODER_RESET);
1014 }
1015
1016 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_DuringReset) {
1017   Initialize();
1018   EnterPendingState(DECODER_DECODE);
1019   EnterPendingState(DECODER_RESET);
1020   SatisfyPendingCallback(DECODER_DECODE);
1021 }
1022
1023 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_AfterReset) {
1024   Initialize();
1025   Read();
1026   Reset();
1027 }
1028
1029 // The following tests cover the fallback logic after reinitialization error or
1030 // decode error of the first buffer after initialization.
1031
1032 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeError) {
1033   Initialize();
1034   decoder_->SimulateError();
1035   ReadOneFrame();
1036
1037   // |video_decoder_stream_| should have fallen back to a new decoder.
1038   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1039
1040   ASSERT_FALSE(pending_read_);
1041   ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1042
1043   // Check that we fell back to Decoder2.
1044   ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1045
1046   // Verify no frame was dropped.
1047   ReadAllFrames();
1048 }
1049
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)
1055     return;
1056
1057   Initialize();
1058   decoder_->HoldDecode();
1059   ReadOneFrame();
1060
1061   // One buffer should have already pulled from the demuxer stream. Set the next
1062   // one to be an EOS.
1063   demuxer_stream_->SeekToEndOfStream();
1064
1065   decoder_->SatisfySingleDecode();
1066   base::RunLoop().RunUntilIdle();
1067
1068   // |video_decoder_stream_| should not have emitted a frame.
1069   EXPECT_TRUE(pending_read_);
1070
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);
1073
1074   decoder_->SimulateError();
1075   base::RunLoop().RunUntilIdle();
1076
1077   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1078
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);
1084
1085   ReadOneFrame();
1086
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);
1090 }
1091
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)
1096     return;
1097
1098   Initialize();
1099   decoder_->HoldDecode();
1100
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();
1107
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});
1112
1113   // Complete the fallback to the second decoder with the read still pending.
1114   base::RunLoop().RunUntilIdle();
1115
1116   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1117
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);
1124
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);
1129 }
1130
1131 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeErrorRepeated) {
1132   Initialize();
1133
1134   // Hold other decoders to simulate errors.
1135   HoldDecodeAfterSelection({1, 2});
1136
1137   // Simulate decode error to trigger the fallback path.
1138   decoder_->SimulateError();
1139   ReadOneFrame();
1140   base::RunLoop().RunUntilIdle();
1141
1142   // Expect decoder 1 to be tried.
1143   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1144   decoder_->SimulateError();
1145   base::RunLoop().RunUntilIdle();
1146
1147   // Then decoder 2.
1148   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1149   decoder_->SimulateError();
1150   base::RunLoop().RunUntilIdle();
1151
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);
1157 }
1158
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)
1166     return;
1167
1168   Initialize();
1169
1170   // Note: Completes decoding one frame, results in Decode() being called with
1171   // second frame that is not completed.
1172   ReadOneFrame();
1173
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);
1178
1179   // Continue up to the point of reinitialization.
1180   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1181
1182   // Hold decodes to prevent a frame from being outputted upon reinitialization.
1183   decoder_->HoldDecode();
1184   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1185
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_);
1191
1192   // Fail the first decode, before a frame can be outputted.
1193   decoder_->SimulateError();
1194   base::RunLoop().RunUntilIdle();
1195
1196   ReadOneFrame();
1197
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);
1203 }
1204
1205 TEST_P(VideoDecoderStreamTest,
1206        FallbackDecoder_DecodeErrorRepeated_AfterReinitialization) {
1207   Initialize();
1208
1209   // Simulate decode error to trigger fallback.
1210   decoder_->SimulateError();
1211   ReadOneFrame();
1212   base::RunLoop().RunUntilIdle();
1213
1214   // Simulate reinitialize error of decoder 1.
1215   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1216   decoder_->SimulateFailureToInit();
1217   HoldDecodeAfterSelection({0, 1, 2});
1218   ReadUntilDecoderReinitialized();
1219
1220   // Decoder 0 should be selected again.
1221   ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1222   decoder_->SimulateError();
1223   base::RunLoop().RunUntilIdle();
1224
1225   // Decoder 1.
1226   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1227   decoder_->SimulateError();
1228   base::RunLoop().RunUntilIdle();
1229
1230   // Decoder 2.
1231   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1232   decoder_->SimulateError();
1233   base::RunLoop().RunUntilIdle();
1234
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);
1240 }
1241
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)
1247     return;
1248
1249   Initialize();
1250   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1251   ASSERT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1252
1253   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1254   ASSERT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1255   EXPECT_FALSE(pending_read_);
1256
1257   ReadAllFrames();
1258 }
1259
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)
1265     return;
1266
1267   Initialize();
1268   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1269   EXPECT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1270
1271   decoder_->HoldDecode();
1272   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1273
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_);
1277
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();
1283
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));
1287 }
1288
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) {
1298     return;
1299   }
1300
1301   Initialize();
1302
1303   // Block on demuxer read and decoder decode so we can step through.
1304   demuxer_stream_->HoldNextRead();
1305   decoder_->HoldDecode();
1306   ReadOneFrame();
1307
1308   int demuxer_reads_satisfied = 0;
1309   // Send back and requests buffers until the next one would fill the decoder
1310   // delay.
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;
1316
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.
1324     EXPECT_EQ(0,
1325               video_decoder_stream_->get_fallback_buffers_size_for_testing());
1326   }
1327
1328   // Hold the init before triggering the error, to verify internal state.
1329   demuxer_stream_->SatisfyReadAndHoldNext();
1330   ++demuxer_reads_satisfied;
1331
1332   decoder_->SimulateError();
1333
1334   HoldDecoderInitOnSelection({1});
1335   HoldDecodeAfterSelection({1});
1336
1337   base::RunLoop().RunUntilIdle();
1338
1339   EXPECT_TRUE(pending_read_);
1340   EXPECT_EQ(demuxer_reads_satisfied,
1341             video_decoder_stream_->get_pending_buffers_size_for_testing());
1342
1343   decoders_[1]->SatisfyInit();
1344   base::RunLoop().RunUntilIdle();
1345
1346   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1347
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());
1356
1357   decoder_->SatisfyDecode();
1358   base::RunLoop().RunUntilIdle();
1359
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_);
1366
1367   // Give the decoder one more buffer, enough to release a frame.
1368   demuxer_stream_->SatisfyReadAndHoldNext();
1369   base::RunLoop().RunUntilIdle();
1370
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_);
1374
1375   demuxer_stream_->SatisfyRead();
1376
1377   // Confirm no frames were dropped.
1378   ReadAllFrames();
1379 }
1380
1381 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnDecodeThenInitErrors) {
1382   Initialize();
1383   decoder_->SimulateError();
1384   FailDecoderInitOnSelection({1});
1385   ReadOneFrame();
1386
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());
1390
1391   ASSERT_FALSE(pending_read_);
1392   ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1393
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);
1397
1398   // Verify no frame was dropped.
1399   ReadAllFrames();
1400 }
1401
1402 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnInitThenDecodeErrors) {
1403   FailDecoderInitOnSelection({0});
1404   Initialize();
1405   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1406   ClearDecoderInitExpectations();
1407
1408   decoder_->HoldDecode();
1409   ReadOneFrame();
1410   decoder_->SimulateError();
1411   base::RunLoop().RunUntilIdle();
1412
1413   // |video_decoder_stream_| should have fallen back to decoder 2.
1414   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1415
1416   ASSERT_FALSE(pending_read_);
1417   ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1418
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);
1422
1423   // Verify no frame was dropped.
1424   ReadAllFrames();
1425 }
1426
1427 TEST_P(VideoDecoderStreamTest,
1428        FallbackDecoder_NotSelectedOnMidstreamDecodeError) {
1429   Initialize();
1430   ReadOneFrame();
1431
1432   // Successfully received a frame.
1433   EXPECT_FALSE(pending_read_);
1434   ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1435
1436   decoder_->SimulateError();
1437
1438   // The error must surface from Read() as DECODE_ERROR.
1439   while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1440     ReadOneFrame();
1441     base::RunLoop().RunUntilIdle();
1442     EXPECT_FALSE(pending_read_);
1443   }
1444
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);
1450 }
1451
1452 TEST_P(VideoDecoderStreamTest, DecoderErrorWhenNotReading) {
1453   Initialize();
1454   decoder_->HoldDecode();
1455   ReadOneFrame();
1456   EXPECT_TRUE(pending_read_);
1457
1458   // Satisfy decode requests until we get the first frame out.
1459   while (pending_read_) {
1460     decoder_->SatisfySingleDecode();
1461     base::RunLoop().RunUntilIdle();
1462   }
1463
1464   // Trigger an error in the decoding.
1465   decoder_->SimulateError();
1466
1467   // The error must surface from Read() as DECODE_ERROR.
1468   while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1469     ReadOneFrame();
1470     base::RunLoop().RunUntilIdle();
1471     EXPECT_FALSE(pending_read_);
1472   }
1473   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1474   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1475 }
1476
1477 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Once) {
1478   Initialize();
1479   decoder_->SimulateFailureToInit();
1480   ReadUntilDecoderReinitialized();
1481   // Should have fallen back to a new instance of decoder 0.
1482   ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1483   ReadAllFrames();
1484   ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1485 }
1486
1487 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Twice) {
1488   Initialize();
1489
1490   // Trigger reinitialization error, and fallback to a new instance.
1491   decoder_->SimulateFailureToInit();
1492   ReadUntilDecoderReinitialized();
1493   ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1494
1495   ReadOneFrame();
1496
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());
1502   ReadAllFrames();
1503 }
1504
1505 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_OneUnsupportedDecoder) {
1506   Initialize();
1507
1508   // The current decoder will fail to reinitialize.
1509   decoder_->SimulateFailureToInit();
1510
1511   // Decoder 1 will also fail to initialize on decoder selection.
1512   FailDecoderInitOnSelection({0, 1});
1513
1514   ReadUntilDecoderReinitialized();
1515
1516   // As a result, decoder 2 will be selected.
1517   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1518
1519   ReadAllFrames();
1520 }
1521
1522 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_NoSupportedDecoder) {
1523   Initialize();
1524
1525   // The current decoder will fail to reinitialize, triggering decoder
1526   // selection.
1527   decoder_->SimulateFailureToInit();
1528
1529   // All of the decoders will fail in decoder selection.
1530   FailDecoderInitOnSelection({0, 1, 2});
1531
1532   ReadUntilDecoderReinitialized();
1533
1534   // The error will surface from Read() as DECODE_ERROR.
1535   while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1536     ReadOneFrame();
1537     base::RunLoop().RunUntilIdle();
1538     EXPECT_FALSE(pending_read_);
1539   }
1540   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1541   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1542 }
1543
1544 TEST_P(VideoDecoderStreamTest, Destroy_DuringFallbackDecoderSelection) {
1545   Initialize();
1546   decoder_->SimulateFailureToInit();
1547   EnterPendingState(DECODER_REINIT);
1548   HoldDecoderInitOnSelection({1});
1549   SatisfyPendingCallback(DECODER_REINIT);
1550 }
1551
1552 }  // namespace media