Revert "[M120 Migration]Fix for crash during chrome exit"
[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/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"
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     EXPECT_MEDIA_LOG(HasSubstr("clear to encrypted buffers"))
145         .Times(AnyNumber());
146   }
147
148   VideoDecoderStreamTest(const VideoDecoderStreamTest&) = delete;
149   VideoDecoderStreamTest& operator=(const VideoDecoderStreamTest&) = delete;
150
151   ~VideoDecoderStreamTest() {
152     // Check that the pipeline statistics callback was fired correctly.
153     EXPECT_EQ(num_decoded_bytes_unreported_, 0);
154
155     is_initialized_ = false;
156     decoders_.clear();
157     video_decoder_stream_.reset();
158     base::RunLoop().RunUntilIdle();
159
160     DCHECK(!pending_initialize_);
161     DCHECK(!pending_read_);
162     DCHECK(!pending_reset_);
163     DCHECK(!pending_stop_);
164   }
165
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);
171   }
172
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(
177         FROM_HERE,
178         base::BindOnce(std::move(output_ready_cb), std::move(frame)));
179   }
180
181   void PrepareFrameWithDelay(
182       scoped_refptr<VideoFrame> frame,
183       VideoDecoderStream::OutputReadyCB output_ready_cb) {
184     task_environment_.FastForwardBy(kPrepareDelay);
185     task_environment_.GetMainThreadTaskRunner()->PostTask(
186         FROM_HERE,
187         base::BindOnce(std::move(output_ready_cb), std::move(frame)));
188   }
189
190   void OnBytesDecoded(int count) { num_decoded_bytes_unreported_ += count; }
191
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.
201     decoders_.clear();
202
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;
208
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_));
214 #endif
215
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)));
222
223       if (GetParam().is_encrypted && !GetParam().has_decryptor)
224         decoder->EnableEncryptedConfigSupport();
225
226       // Keep a reference so we can change the behavior of each decoder.
227       decoders_.push_back(decoder->GetWeakPtr());
228
229       decoders.push_back(std::move(decoder));
230     }
231
232     for (const auto i : decoder_indices_to_fail_init_)
233       decoders_[i]->SimulateFailureToInit();
234
235     for (const auto i : decoder_indices_to_hold_init_)
236       decoders_[i]->HoldNextInit();
237
238     for (const auto i : decoder_indices_to_hold_decode_)
239       decoders_[i]->HoldDecode();
240
241     for (const auto i : platform_decoder_indices_)
242       decoders_[i]->SetIsPlatformDecoder(true);
243
244     return decoders;
245   }
246
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();
252   }
253
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();
261     }
262   }
263
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();
271     }
272   }
273
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();
282     }
283   }
284
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);
290     }
291   }
292
293   // Updates the |decoder_| currently being used by VideoDecoderStream.
294   void OnDecoderChanged(VideoDecoder* decoder) {
295     if (!decoder) {
296       decoder_ = nullptr;
297       return;
298     }
299
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));
306   }
307
308   MOCK_METHOD1(OnWaiting, void(WaitingReason));
309
310   void OnStatistics(const PipelineStatistics& statistics) {
311     num_decoded_bytes_unreported_ -= statistics.video_bytes_decoded;
312   }
313
314   void OnInitialized(bool success) {
315     DCHECK(!pending_read_);
316     DCHECK(!pending_reset_);
317     DCHECK(pending_initialize_);
318     pending_initialize_ = false;
319
320     is_initialized_ = success;
321     if (!success)
322       decoders_.clear();
323   }
324
325   void Initialize() {
326     if (!demuxer_stream_) {
327       demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
328           kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted);
329     }
330
331     pending_initialize_ = true;
332     video_decoder_stream_->Initialize(
333         demuxer_stream_.get(),
334         base::BindOnce(&VideoDecoderStreamTest::OnInitialized,
335                        base::Unretained(this)),
336         cdm_context_.get(),
337         base::BindRepeating(&VideoDecoderStreamTest::OnStatistics,
338                             base::Unretained(this)),
339         base::BindRepeating(&VideoDecoderStreamTest::OnWaiting,
340                             base::Unretained(this)));
341
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();
347   }
348
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());
355     if (has_no_key_) {
356       std::move(decrypt_cb).Run(Decryptor::kNoKey, nullptr);
357       return;
358     }
359
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);
368   }
369
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()
377             : nullptr;
378     frame_read_ = frame;
379     if (frame && !frame->metadata().end_of_stream) {
380       EXPECT_EQ(*frame->metadata().frame_duration, demuxer_stream_->duration());
381
382       num_decoded_frames_++;
383     }
384     pending_read_ = false;
385   }
386
387   void OnReset() {
388     DCHECK(!pending_read_);
389     DCHECK(pending_reset_);
390     pending_reset_ = false;
391   }
392
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();
399   }
400
401   void ReadUntilPending() {
402     do {
403       ReadOneFrame();
404     } while (!pending_read_);
405   }
406
407   void ReadAllFrames(int expected_decoded_frames) {
408     // Reading all frames reinitializes the demuxer.
409     do {
410       ReadOneFrame();
411     } while (frame_read_.get() && !frame_read_->metadata().end_of_stream);
412
413     DCHECK_EQ(expected_decoded_frames, num_decoded_frames_);
414   }
415
416   void ReadAllFrames() {
417     // No frames should have been dropped.
418     ReadAllFrames(kNumConfigs * kNumBuffersInOneConfig);
419   }
420
421   enum PendingState {
422     NOT_PENDING,
423     DEMUXER_READ_NORMAL,
424     DEMUXER_READ_CONFIG_CHANGE,
425     DECRYPTOR_NO_KEY,
426     DECODER_REINIT,
427     DECODER_DECODE,
428     DECODER_RESET
429   };
430
431   void EnterPendingState(PendingState state) {
432     DCHECK_NE(state, NOT_PENDING);
433     switch (state) {
434       case DEMUXER_READ_NORMAL:
435         demuxer_stream_->HoldNextRead();
436         ReadUntilPending();
437         break;
438
439       case DEMUXER_READ_CONFIG_CHANGE:
440         demuxer_stream_->HoldNextConfigChangeRead();
441         ReadUntilPending();
442         break;
443
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));
448           has_no_key_ = true;
449         }
450         ReadOneFrame();
451         break;
452
453       case DECODER_REINIT:
454         decoder_->HoldNextInit();
455         ReadUntilPending();
456         break;
457
458       case DECODER_DECODE:
459         decoder_->HoldDecode();
460         ReadUntilPending();
461         break;
462
463       case DECODER_RESET:
464         decoder_->HoldNextReset();
465         pending_reset_ = true;
466         video_decoder_stream_->Reset(base::BindOnce(
467             &VideoDecoderStreamTest::OnReset, base::Unretained(this)));
468         base::RunLoop().RunUntilIdle();
469         break;
470
471       case NOT_PENDING:
472         NOTREACHED();
473         break;
474     }
475   }
476
477   void SatisfyPendingCallback(PendingState state) {
478     DCHECK_NE(state, NOT_PENDING);
479     switch (state) {
480       case DEMUXER_READ_CONFIG_CHANGE:
481         EXPECT_MEDIA_LOG(HasSubstr("decoder config changed"))
482             .Times(testing::AtLeast(1));
483         [[fallthrough]];
484       case DEMUXER_READ_NORMAL:
485         demuxer_stream_->SatisfyRead();
486         break;
487
488       // This is only interesting to test during VideoDecoderStream destruction.
489       // There's no need to satisfy a callback.
490       case DECRYPTOR_NO_KEY:
491         NOTREACHED();
492         break;
493
494       case DECODER_REINIT:
495         decoder_->SatisfyInit();
496         break;
497
498       case DECODER_DECODE:
499         decoder_->SatisfyDecode();
500         break;
501
502       case DECODER_RESET:
503         decoder_->SatisfyReset();
504         break;
505
506       case NOT_PENDING:
507         NOTREACHED();
508         break;
509     }
510
511     base::RunLoop().RunUntilIdle();
512   }
513
514   void Read() {
515     EnterPendingState(DECODER_DECODE);
516     SatisfyPendingCallback(DECODER_DECODE);
517   }
518
519   void Reset() {
520     EnterPendingState(DECODER_RESET);
521     SatisfyPendingCallback(DECODER_RESET);
522   }
523
524   void ReadUntilDecoderReinitialized() {
525     EnterPendingState(DECODER_REINIT);
526     SatisfyPendingCallback(DECODER_REINIT);
527   }
528
529   base::test::SingleThreadTaskEnvironment task_environment_{
530       base::test::TaskEnvironment::TimeSource::MOCK_TIME};
531
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_;
536
537   // Use NiceMock since we don't care about most of calls on the decryptor.
538   std::unique_ptr<NiceMock<MockDecryptor>> decryptor_;
539
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_;
544
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_;
549
550   // The current decoder used by |video_decoder_stream_|.
551   raw_ptr<FakeVideoDecoder, AcrossTasksDanglingUntriaged> decoder_ = nullptr;
552
553   bool is_initialized_;
554   int num_decoded_frames_;
555   bool pending_initialize_;
556   bool pending_read_;
557   bool pending_reset_;
558   bool pending_stop_;
559   int num_decoded_bytes_unreported_;
560   scoped_refptr<VideoFrame> frame_read_;
561   DecoderStatus::Codes last_read_status_code_;
562
563   // Decryptor has no key to decrypt a frame.
564   bool has_no_key_;
565 };
566
567 INSTANTIATE_TEST_SUITE_P(
568     Clear,
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)));
575
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)));
581
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)));
587
588 INSTANTIATE_TEST_SUITE_P(
589     Clear_Parallel,
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)));
595
596 TEST_P(VideoDecoderStreamTest, CanReadWithoutStallingAtAnyTime) {
597   ASSERT_FALSE(video_decoder_stream_->CanReadWithoutStalling());
598 }
599
600 TEST_P(VideoDecoderStreamTest, Initialization) {
601   Initialize();
602   EXPECT_TRUE(is_initialized_);
603 }
604
605 TEST_P(VideoDecoderStreamTest, AllDecoderInitializationFails) {
606   FailDecoderInitOnSelection({0, 1, 2});
607   Initialize();
608   EXPECT_FALSE(is_initialized_);
609 }
610
611 TEST_P(VideoDecoderStreamTest, PartialDecoderInitializationFails) {
612   FailDecoderInitOnSelection({0, 1});
613   Initialize();
614   EXPECT_TRUE(is_initialized_);
615 }
616
617 TEST_P(VideoDecoderStreamTest, ReadOneFrame) {
618   Initialize();
619   Read();
620 }
621
622 TEST_P(VideoDecoderStreamTest, ReadAllFrames) {
623   Initialize();
624   ReadAllFrames();
625 }
626
627 TEST_P(VideoDecoderStreamTest, Read_AfterReset) {
628   Initialize();
629   Reset();
630   Read();
631   Reset();
632   Read();
633 }
634
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});
639
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));
647   Initialize();
648
649   // Initially we should be using a software decoder
650   EXPECT_TRUE(decoder_);
651   EXPECT_FALSE(decoder_->IsPlatformDecoder());
652
653   ReadAllFrames();
654
655   // We should end up on a hardware decoder
656   EXPECT_TRUE(decoder_->IsPlatformDecoder());
657 }
658
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});
663
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));
671   Initialize();
672
673   // We should initially be using a hardware decoder
674   EXPECT_TRUE(decoder_);
675   EXPECT_TRUE(decoder_->IsPlatformDecoder());
676   ReadAllFrames();
677
678   // We should end up on a software decoder
679   EXPECT_FALSE(decoder_->IsPlatformDecoder());
680 }
681
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)
685     return;
686
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)));
693   }
694
695   constexpr base::TimeDelta kDecodeDelay = base::Milliseconds(10);
696
697   Initialize();
698
699   // Simulate time elapsed by the decoder.
700   EnterPendingState(DECODER_DECODE);
701   task_environment_.FastForwardBy(kDecodeDelay);
702
703   SatisfyPendingCallback(DECODER_DECODE);
704
705   EXPECT_TRUE(frame_read_);
706
707   const VideoFrameMetadata& metadata = frame_read_->metadata();
708
709   // Verify the decoding metadata is accurate.
710   EXPECT_EQ(*metadata.decode_end_time - *metadata.decode_begin_time,
711             kDecodeDelay);
712
713   // Verify the processing metadata is accurate.
714   const base::TimeDelta expected_processing_time =
715       GetParam().has_prepare ? (kDecodeDelay + kPrepareDelay) : kDecodeDelay;
716
717   EXPECT_EQ(*metadata.processing_time, expected_processing_time);
718 }
719
720 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxer) {
721   Initialize();
722   demuxer_stream_->HoldNextRead();
723   ReadOneFrame();
724   EXPECT_TRUE(pending_read_);
725
726   int demuxed_buffers = 0;
727
728   // Pass frames from the demuxer to the VideoDecoderStream until the first read
729   // request is satisfied.
730   while (pending_read_) {
731     ++demuxed_buffers;
732     demuxer_stream_->SatisfyReadAndHoldNext();
733     base::RunLoop().RunUntilIdle();
734   }
735
736   EXPECT_EQ(std::min(GetParam().decoding_delay + 1, kNumBuffersInOneConfig + 1),
737             demuxed_buffers);
738
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.
742   ReadUntilPending();
743
744   demuxer_stream_->SatisfyRead();
745   base::RunLoop().RunUntilIdle();
746   EXPECT_FALSE(pending_read_);
747 }
748
749 TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxerAndDecoder) {
750   // Test applies only when the decoder allows multiple parallel requests.
751   if (GetParam().parallel_decoding == 1)
752     return;
753
754   Initialize();
755   demuxer_stream_->HoldNextRead();
756   decoder_->HoldDecode();
757   ReadOneFrame();
758   EXPECT_TRUE(pending_read_);
759
760   int demuxed_buffers = 0;
761
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_) {
765     ++demuxed_buffers;
766     demuxer_stream_->SatisfyReadAndHoldNext();
767     base::RunLoop().RunUntilIdle();
768
769     // Always keep one decode request pending.
770     if (demuxed_buffers > 1) {
771       decoder_->SatisfySingleDecode();
772       base::RunLoop().RunUntilIdle();
773     }
774   }
775
776   ReadUntilPending();
777   EXPECT_TRUE(pending_read_);
778
779   // Unblocking one decode request should unblock read even when demuxer is
780   // still blocked.
781   decoder_->SatisfySingleDecode();
782   base::RunLoop().RunUntilIdle();
783   EXPECT_FALSE(pending_read_);
784
785   // Stream should still be blocked on the demuxer after unblocking the decoder.
786   decoder_->SatisfyDecode();
787   ReadUntilPending();
788   EXPECT_TRUE(pending_read_);
789
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_);
793
794   // Unblocking the demuxer will unblock the stream.
795   demuxer_stream_->SatisfyRead();
796   base::RunLoop().RunUntilIdle();
797   EXPECT_FALSE(pending_read_);
798 }
799
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)
804     return;
805
806   Initialize();
807   decoder_->HoldDecode();
808
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++) {
814       ReadOneFrame();
815       while (pending_read_) {
816         decoder_->SatisfySingleDecode();
817         base::RunLoop().RunUntilIdle();
818       }
819     }
820   }
821
822   // Read() again. The callback must be delayed until the decode completes.
823   ReadOneFrame();
824   ASSERT_TRUE(pending_read_);
825
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);
831
832   // The read output should indicate end of stream.
833   ASSERT_TRUE(frame_read_.get());
834   EXPECT_TRUE(frame_read_->metadata().end_of_stream);
835 }
836
837 TEST_P(VideoDecoderStreamTest, Read_DemuxerStreamReadError) {
838   Initialize();
839   EnterPendingState(DEMUXER_READ_NORMAL);
840
841   InSequence s;
842
843   if (GetParam().is_encrypted && GetParam().has_decryptor) {
844     EXPECT_MEDIA_LOG(
845         HasSubstr("DecryptingDemuxerStream: demuxer stream read error"));
846   }
847   EXPECT_MEDIA_LOG(HasSubstr("video demuxer stream read error"));
848
849   demuxer_stream_->Error();
850   base::RunLoop().RunUntilIdle();
851
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);
855 }
856
857 // No Reset() before initialization is successfully completed.
858 TEST_P(VideoDecoderStreamTest, Reset_AfterInitialization) {
859   Initialize();
860   Reset();
861   Read();
862 }
863
864 TEST_P(VideoDecoderStreamTest, Reset_DuringReinitialization) {
865   Initialize();
866
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);
873   Read();
874 }
875
876 TEST_P(VideoDecoderStreamTest, Reset_AfterReinitialization) {
877   Initialize();
878   EnterPendingState(DECODER_REINIT);
879   SatisfyPendingCallback(DECODER_REINIT);
880   Reset();
881   Read();
882 }
883
884 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_Normal) {
885   Initialize();
886   EnterPendingState(DEMUXER_READ_NORMAL);
887   EnterPendingState(DECODER_RESET);
888   SatisfyPendingCallback(DEMUXER_READ_NORMAL);
889   SatisfyPendingCallback(DECODER_RESET);
890   Read();
891 }
892
893 TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_ConfigChange) {
894   Initialize();
895   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
896   EnterPendingState(DECODER_RESET);
897   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
898   SatisfyPendingCallback(DECODER_RESET);
899   Read();
900 }
901
902 TEST_P(VideoDecoderStreamTest, Reset_DuringNormalDecoderDecode) {
903   Initialize();
904   EnterPendingState(DECODER_DECODE);
905   EnterPendingState(DECODER_RESET);
906   SatisfyPendingCallback(DECODER_DECODE);
907   SatisfyPendingCallback(DECODER_RESET);
908   Read();
909 }
910
911 TEST_P(VideoDecoderStreamTest, Reset_AfterNormalRead) {
912   Initialize();
913   Read();
914   Reset();
915   Read();
916 }
917
918 TEST_P(VideoDecoderStreamTest, Reset_AfterDemuxerRead_ConfigChange) {
919   Initialize();
920   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
921   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
922   Reset();
923   Read();
924 }
925
926 TEST_P(VideoDecoderStreamTest, Reset_AfterEndOfStream) {
927   Initialize();
928   ReadAllFrames();
929   Reset();
930   num_decoded_frames_ = 0;
931   demuxer_stream_->SeekToStart();
932   ReadAllFrames();
933 }
934
935 TEST_P(VideoDecoderStreamTest, Reset_DuringNoKeyRead) {
936   Initialize();
937   EnterPendingState(DECRYPTOR_NO_KEY);
938   Reset();
939 }
940
941 // In the following Destroy_* tests, |video_decoder_stream_| is destroyed in
942 // VideoDecoderStreamTest destructor.
943
944 TEST_P(VideoDecoderStreamTest, Destroy_BeforeInitialization) {}
945
946 TEST_P(VideoDecoderStreamTest, Destroy_DuringInitialization) {
947   HoldDecoderInitOnSelection({0});
948   Initialize();
949 }
950
951 TEST_P(VideoDecoderStreamTest, Destroy_AfterInitialization) {
952   Initialize();
953 }
954
955 TEST_P(VideoDecoderStreamTest, Destroy_DuringReinitialization) {
956   Initialize();
957   EnterPendingState(DECODER_REINIT);
958 }
959
960 TEST_P(VideoDecoderStreamTest, Destroy_AfterReinitialization) {
961   Initialize();
962   EnterPendingState(DECODER_REINIT);
963   SatisfyPendingCallback(DECODER_REINIT);
964 }
965
966 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_Normal) {
967   Initialize();
968   EnterPendingState(DEMUXER_READ_NORMAL);
969 }
970
971 TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_ConfigChange) {
972   Initialize();
973   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
974 }
975
976 TEST_P(VideoDecoderStreamTest, Destroy_DuringNormalDecoderDecode) {
977   Initialize();
978   EnterPendingState(DECODER_DECODE);
979 }
980
981 TEST_P(VideoDecoderStreamTest, Destroy_AfterNormalRead) {
982   Initialize();
983   Read();
984 }
985
986 TEST_P(VideoDecoderStreamTest, Destroy_AfterConfigChangeRead) {
987   Initialize();
988   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
989   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
990 }
991
992 TEST_P(VideoDecoderStreamTest, Destroy_DuringDecoderReinitialization) {
993   Initialize();
994   EnterPendingState(DECODER_REINIT);
995 }
996
997 TEST_P(VideoDecoderStreamTest, Destroy_DuringNoKeyRead) {
998   Initialize();
999   EnterPendingState(DECRYPTOR_NO_KEY);
1000 }
1001
1002 TEST_P(VideoDecoderStreamTest, Destroy_DuringReset) {
1003   Initialize();
1004   EnterPendingState(DECODER_RESET);
1005 }
1006
1007 TEST_P(VideoDecoderStreamTest, Destroy_AfterReset) {
1008   Initialize();
1009   Reset();
1010 }
1011
1012 TEST_P(VideoDecoderStreamTest, Destroy_DuringRead_DuringReset) {
1013   Initialize();
1014   EnterPendingState(DECODER_DECODE);
1015   EnterPendingState(DECODER_RESET);
1016 }
1017
1018 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_DuringReset) {
1019   Initialize();
1020   EnterPendingState(DECODER_DECODE);
1021   EnterPendingState(DECODER_RESET);
1022   SatisfyPendingCallback(DECODER_DECODE);
1023 }
1024
1025 TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_AfterReset) {
1026   Initialize();
1027   Read();
1028   Reset();
1029 }
1030
1031 // The following tests cover the fallback logic after reinitialization error or
1032 // decode error of the first buffer after initialization.
1033
1034 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeError) {
1035   Initialize();
1036   decoder_->SimulateError();
1037   ReadOneFrame();
1038
1039   // |video_decoder_stream_| should have fallen back to a new decoder.
1040   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1041
1042   ASSERT_FALSE(pending_read_);
1043   ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1044
1045   // Check that we fell back to Decoder2.
1046   ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1047
1048   // Verify no frame was dropped.
1049   ReadAllFrames();
1050 }
1051
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)
1057     return;
1058
1059   Initialize();
1060   decoder_->HoldDecode();
1061   ReadOneFrame();
1062
1063   // One buffer should have already pulled from the demuxer stream. Set the next
1064   // one to be an EOS.
1065   demuxer_stream_->SeekToEndOfStream();
1066
1067   decoder_->SatisfySingleDecode();
1068   base::RunLoop().RunUntilIdle();
1069
1070   // |video_decoder_stream_| should not have emitted a frame.
1071   EXPECT_TRUE(pending_read_);
1072
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);
1075
1076   decoder_->SimulateError();
1077   base::RunLoop().RunUntilIdle();
1078
1079   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1080
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);
1086
1087   ReadOneFrame();
1088
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);
1092 }
1093
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)
1098     return;
1099
1100   Initialize();
1101   decoder_->HoldDecode();
1102
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();
1109
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});
1114
1115   // Complete the fallback to the second decoder with the read still pending.
1116   base::RunLoop().RunUntilIdle();
1117
1118   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1119
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);
1126
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);
1131 }
1132
1133 TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeErrorRepeated) {
1134   Initialize();
1135
1136   // Hold other decoders to simulate errors.
1137   HoldDecodeAfterSelection({1, 2});
1138
1139   // Simulate decode error to trigger the fallback path.
1140   decoder_->SimulateError();
1141   ReadOneFrame();
1142   base::RunLoop().RunUntilIdle();
1143
1144   // Expect decoder 1 to be tried.
1145   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1146   decoder_->SimulateError();
1147   base::RunLoop().RunUntilIdle();
1148
1149   // Then decoder 2.
1150   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1151   decoder_->SimulateError();
1152   base::RunLoop().RunUntilIdle();
1153
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);
1159 }
1160
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)
1168     return;
1169
1170   Initialize();
1171
1172   // Note: Completes decoding one frame, results in Decode() being called with
1173   // second frame that is not completed.
1174   ReadOneFrame();
1175
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);
1180
1181   // Continue up to the point of reinitialization.
1182   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1183
1184   // Hold decodes to prevent a frame from being outputted upon reinitialization.
1185   decoder_->HoldDecode();
1186   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1187
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_);
1193
1194   // Fail the first decode, before a frame can be outputted.
1195   decoder_->SimulateError();
1196   base::RunLoop().RunUntilIdle();
1197
1198   ReadOneFrame();
1199
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);
1205 }
1206
1207 TEST_P(VideoDecoderStreamTest,
1208        FallbackDecoder_DecodeErrorRepeated_AfterReinitialization) {
1209   Initialize();
1210
1211   // Simulate decode error to trigger fallback.
1212   decoder_->SimulateError();
1213   ReadOneFrame();
1214   base::RunLoop().RunUntilIdle();
1215
1216   // Simulate reinitialize error of decoder 1.
1217   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1218   decoder_->SimulateFailureToInit();
1219   HoldDecodeAfterSelection({0, 1, 2});
1220   ReadUntilDecoderReinitialized();
1221
1222   // Decoder 0 should be selected again.
1223   ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1224   decoder_->SimulateError();
1225   base::RunLoop().RunUntilIdle();
1226
1227   // Decoder 1.
1228   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1229   decoder_->SimulateError();
1230   base::RunLoop().RunUntilIdle();
1231
1232   // Decoder 2.
1233   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1234   decoder_->SimulateError();
1235   base::RunLoop().RunUntilIdle();
1236
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);
1242 }
1243
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)
1249     return;
1250
1251   Initialize();
1252   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1253   ASSERT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1254
1255   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1256   ASSERT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1257   EXPECT_FALSE(pending_read_);
1258
1259   ReadAllFrames();
1260 }
1261
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)
1267     return;
1268
1269   Initialize();
1270   EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
1271   EXPECT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
1272
1273   decoder_->HoldDecode();
1274   SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
1275
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_);
1279
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();
1285
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));
1289 }
1290
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) {
1300     return;
1301   }
1302
1303   Initialize();
1304
1305   // Block on demuxer read and decoder decode so we can step through.
1306   demuxer_stream_->HoldNextRead();
1307   decoder_->HoldDecode();
1308   ReadOneFrame();
1309
1310   int demuxer_reads_satisfied = 0;
1311   // Send back and requests buffers until the next one would fill the decoder
1312   // delay.
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;
1318
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.
1326     EXPECT_EQ(0,
1327               video_decoder_stream_->get_fallback_buffers_size_for_testing());
1328   }
1329
1330   // Hold the init before triggering the error, to verify internal state.
1331   demuxer_stream_->SatisfyReadAndHoldNext();
1332   ++demuxer_reads_satisfied;
1333
1334   decoder_->SimulateError();
1335
1336   HoldDecoderInitOnSelection({1});
1337   HoldDecodeAfterSelection({1});
1338
1339   base::RunLoop().RunUntilIdle();
1340
1341   EXPECT_TRUE(pending_read_);
1342   EXPECT_EQ(demuxer_reads_satisfied,
1343             video_decoder_stream_->get_pending_buffers_size_for_testing());
1344
1345   decoders_[1]->SatisfyInit();
1346   base::RunLoop().RunUntilIdle();
1347
1348   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1349
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());
1358
1359   decoder_->SatisfyDecode();
1360   base::RunLoop().RunUntilIdle();
1361
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_);
1368
1369   // Give the decoder one more buffer, enough to release a frame.
1370   demuxer_stream_->SatisfyReadAndHoldNext();
1371   base::RunLoop().RunUntilIdle();
1372
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_);
1376
1377   demuxer_stream_->SatisfyRead();
1378
1379   // Confirm no frames were dropped.
1380   ReadAllFrames();
1381 }
1382
1383 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnDecodeThenInitErrors) {
1384   Initialize();
1385   decoder_->SimulateError();
1386   FailDecoderInitOnSelection({1});
1387   ReadOneFrame();
1388
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());
1392
1393   ASSERT_FALSE(pending_read_);
1394   ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1395
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);
1399
1400   // Verify no frame was dropped.
1401   ReadAllFrames();
1402 }
1403
1404 TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnInitThenDecodeErrors) {
1405   FailDecoderInitOnSelection({0});
1406   Initialize();
1407   ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
1408   ClearDecoderInitExpectations();
1409
1410   decoder_->HoldDecode();
1411   ReadOneFrame();
1412   decoder_->SimulateError();
1413   base::RunLoop().RunUntilIdle();
1414
1415   // |video_decoder_stream_| should have fallen back to decoder 2.
1416   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1417
1418   ASSERT_FALSE(pending_read_);
1419   ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
1420
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);
1424
1425   // Verify no frame was dropped.
1426   ReadAllFrames();
1427 }
1428
1429 TEST_P(VideoDecoderStreamTest,
1430        FallbackDecoder_NotSelectedOnMidstreamDecodeError) {
1431   Initialize();
1432   ReadOneFrame();
1433
1434   // Successfully received a frame.
1435   EXPECT_FALSE(pending_read_);
1436   ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1437
1438   decoder_->SimulateError();
1439
1440   // The error must surface from Read() as DECODE_ERROR.
1441   while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1442     ReadOneFrame();
1443     base::RunLoop().RunUntilIdle();
1444     EXPECT_FALSE(pending_read_);
1445   }
1446
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);
1452 }
1453
1454 TEST_P(VideoDecoderStreamTest, DecoderErrorWhenNotReading) {
1455   Initialize();
1456   decoder_->HoldDecode();
1457   ReadOneFrame();
1458   EXPECT_TRUE(pending_read_);
1459
1460   // Satisfy decode requests until we get the first frame out.
1461   while (pending_read_) {
1462     decoder_->SatisfySingleDecode();
1463     base::RunLoop().RunUntilIdle();
1464   }
1465
1466   // Trigger an error in the decoding.
1467   decoder_->SimulateError();
1468
1469   // The error must surface from Read() as DECODE_ERROR.
1470   while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1471     ReadOneFrame();
1472     base::RunLoop().RunUntilIdle();
1473     EXPECT_FALSE(pending_read_);
1474   }
1475   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1476   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1477 }
1478
1479 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Once) {
1480   Initialize();
1481   decoder_->SimulateFailureToInit();
1482   ReadUntilDecoderReinitialized();
1483   // Should have fallen back to a new instance of decoder 0.
1484   ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1485   ReadAllFrames();
1486   ASSERT_GT(decoder_->total_bytes_decoded(), 0);
1487 }
1488
1489 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Twice) {
1490   Initialize();
1491
1492   // Trigger reinitialization error, and fallback to a new instance.
1493   decoder_->SimulateFailureToInit();
1494   ReadUntilDecoderReinitialized();
1495   ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
1496
1497   ReadOneFrame();
1498
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());
1504   ReadAllFrames();
1505 }
1506
1507 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_OneUnsupportedDecoder) {
1508   Initialize();
1509
1510   // The current decoder will fail to reinitialize.
1511   decoder_->SimulateFailureToInit();
1512
1513   // Decoder 1 will also fail to initialize on decoder selection.
1514   FailDecoderInitOnSelection({0, 1});
1515
1516   ReadUntilDecoderReinitialized();
1517
1518   // As a result, decoder 2 will be selected.
1519   ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
1520
1521   ReadAllFrames();
1522 }
1523
1524 TEST_P(VideoDecoderStreamTest, ReinitializeFailure_NoSupportedDecoder) {
1525   Initialize();
1526
1527   // The current decoder will fail to reinitialize, triggering decoder
1528   // selection.
1529   decoder_->SimulateFailureToInit();
1530
1531   // All of the decoders will fail in decoder selection.
1532   FailDecoderInitOnSelection({0, 1, 2});
1533
1534   ReadUntilDecoderReinitialized();
1535
1536   // The error will surface from Read() as DECODE_ERROR.
1537   while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
1538     ReadOneFrame();
1539     base::RunLoop().RunUntilIdle();
1540     EXPECT_FALSE(pending_read_);
1541   }
1542   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
1543   EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
1544 }
1545
1546 TEST_P(VideoDecoderStreamTest, Destroy_DuringFallbackDecoderSelection) {
1547   Initialize();
1548   decoder_->SimulateFailureToInit();
1549   EnterPendingState(DECODER_REINIT);
1550   HoldDecoderInitOnSelection({1});
1551   SatisfyPendingCallback(DECODER_REINIT);
1552 }
1553
1554 }  // namespace media