video_decoder_stream_unittest.cc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <memory>
  5. #include <utility>
  6. #include <vector>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/run_loop.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/test/gmock_callback_support.h"
  13. #include "base/test/mock_callback.h"
  14. #include "base/test/task_environment.h"
  15. #include "build/build_config.h"
  16. #include "media/base/fake_demuxer_stream.h"
  17. #include "media/base/mock_filters.h"
  18. #include "media/base/mock_media_log.h"
  19. #include "media/base/test_helpers.h"
  20. #include "media/base/timestamp_constants.h"
  21. #include "media/filters/decoder_stream.h"
  22. #include "media/filters/fake_video_decoder.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. #if !BUILDFLAG(IS_ANDROID)
  25. #include "media/filters/decrypting_video_decoder.h"
  26. #endif
  27. #include <iostream>
  28. using ::base::test::RunCallback;
  29. using ::base::test::RunOnceCallback;
  30. using ::testing::_;
  31. using ::testing::AnyNumber;
  32. using ::testing::Assign;
  33. using ::testing::HasSubstr;
  34. using ::testing::InSequence;
  35. using ::testing::Invoke;
  36. using ::testing::InvokeWithoutArgs;
  37. using ::testing::NiceMock;
  38. using ::testing::Return;
  39. using ::testing::SaveArg;
  40. using ::testing::StrictMock;
  41. namespace media {
  42. namespace {
  43. const int kNumConfigs = 4;
  44. const int kNumBuffersInOneConfig = 5;
  45. constexpr base::TimeDelta kPrepareDelay = base::Milliseconds(5);
  46. static int GetDecoderId(int i) {
  47. return i;
  48. }
  49. DecoderPriority MockDecoderPriority(const VideoDecoderConfig& config,
  50. const VideoDecoder& decoder) {
  51. auto const at_or_above_cutoff = config.visible_rect().height() >=
  52. TestVideoConfig::LargeCodedSize().height();
  53. return at_or_above_cutoff == decoder.IsPlatformDecoder()
  54. ? DecoderPriority::kNormal
  55. : DecoderPriority::kDeprioritized;
  56. }
  57. } // namespace
  58. struct VideoDecoderStreamTestParams {
  59. VideoDecoderStreamTestParams(bool is_encrypted,
  60. bool has_decryptor,
  61. bool has_prepare,
  62. int decoding_delay,
  63. int parallel_decoding)
  64. : is_encrypted(is_encrypted),
  65. has_decryptor(has_decryptor),
  66. has_prepare(has_prepare),
  67. decoding_delay(decoding_delay),
  68. parallel_decoding(parallel_decoding) {}
  69. bool is_encrypted;
  70. bool has_decryptor;
  71. bool has_prepare;
  72. int decoding_delay;
  73. int parallel_decoding;
  74. };
  75. class VideoDecoderStreamTest
  76. : public testing::Test,
  77. public testing::WithParamInterface<VideoDecoderStreamTestParams> {
  78. public:
  79. VideoDecoderStreamTest()
  80. : is_initialized_(false),
  81. num_decoded_frames_(0),
  82. pending_initialize_(false),
  83. pending_read_(false),
  84. pending_reset_(false),
  85. pending_stop_(false),
  86. num_decoded_bytes_unreported_(0),
  87. has_no_key_(false) {
  88. video_decoder_stream_ = std::make_unique<VideoDecoderStream>(
  89. std::make_unique<VideoDecoderStream::StreamTraits>(&media_log_),
  90. task_environment_.GetMainThreadTaskRunner(),
  91. base::BindRepeating(&VideoDecoderStreamTest::CreateVideoDecodersForTest,
  92. base::Unretained(this)),
  93. &media_log_);
  94. video_decoder_stream_->set_decoder_change_observer(base::BindRepeating(
  95. &VideoDecoderStreamTest::OnDecoderChanged, base::Unretained(this)));
  96. video_decoder_stream_
  97. ->GetDecoderSelectorForTesting(base::PassKey<VideoDecoderStreamTest>())
  98. .OverrideDecoderPriorityCBForTesting(
  99. base::BindRepeating(MockDecoderPriority));
  100. if (GetParam().has_prepare) {
  101. video_decoder_stream_->SetPrepareCB(base::BindRepeating(
  102. &VideoDecoderStreamTest::PrepareFrame, base::Unretained(this)));
  103. }
  104. if (GetParam().is_encrypted && GetParam().has_decryptor) {
  105. decryptor_ = std::make_unique<NiceMock<MockDecryptor>>();
  106. // Decryptor can only decrypt (not decrypt-and-decode) so that
  107. // DecryptingDemuxerStream will be used.
  108. EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
  109. .WillRepeatedly(RunOnceCallback<1>(false));
  110. EXPECT_CALL(*decryptor_, Decrypt(_, _, _))
  111. .WillRepeatedly(Invoke(this, &VideoDecoderStreamTest::Decrypt));
  112. }
  113. if (GetParam().is_encrypted) {
  114. cdm_context_ = std::make_unique<StrictMock<MockCdmContext>>();
  115. EXPECT_CALL(*cdm_context_, RegisterEventCB(_)).Times(AnyNumber());
  116. EXPECT_CALL(*cdm_context_, GetDecryptor())
  117. .WillRepeatedly(Return(decryptor_.get()));
  118. }
  119. // Covering most MediaLog messages for now.
  120. // TODO(wolenetz/xhwang): Fix tests to have better MediaLog checking.
  121. EXPECT_MEDIA_LOG(HasSubstr("video")).Times(AnyNumber());
  122. EXPECT_MEDIA_LOG(HasSubstr("Video")).Times(AnyNumber());
  123. EXPECT_MEDIA_LOG(HasSubstr("audio")).Times(AnyNumber());
  124. EXPECT_MEDIA_LOG(HasSubstr("Audio")).Times(AnyNumber());
  125. EXPECT_MEDIA_LOG(HasSubstr("decryptor")).Times(AnyNumber());
  126. }
  127. VideoDecoderStreamTest(const VideoDecoderStreamTest&) = delete;
  128. VideoDecoderStreamTest& operator=(const VideoDecoderStreamTest&) = delete;
  129. ~VideoDecoderStreamTest() {
  130. // Check that the pipeline statistics callback was fired correctly.
  131. EXPECT_EQ(num_decoded_bytes_unreported_, 0);
  132. is_initialized_ = false;
  133. decoders_.clear();
  134. video_decoder_stream_.reset();
  135. base::RunLoop().RunUntilIdle();
  136. DCHECK(!pending_initialize_);
  137. DCHECK(!pending_read_);
  138. DCHECK(!pending_reset_);
  139. DCHECK(!pending_stop_);
  140. }
  141. void CreateDemuxerStream(gfx::Size start_size, gfx::Vector2dF size_delta) {
  142. DCHECK(!demuxer_stream_);
  143. demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
  144. kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted,
  145. start_size, size_delta);
  146. }
  147. void PrepareFrame(scoped_refptr<VideoFrame> frame,
  148. VideoDecoderStream::OutputReadyCB output_ready_cb) {
  149. // Simulate some delay in return of the output.
  150. task_environment_.GetMainThreadTaskRunner()->PostTask(
  151. FROM_HERE,
  152. base::BindOnce(std::move(output_ready_cb), std::move(frame)));
  153. }
  154. void PrepareFrameWithDelay(
  155. scoped_refptr<VideoFrame> frame,
  156. VideoDecoderStream::OutputReadyCB output_ready_cb) {
  157. task_environment_.FastForwardBy(kPrepareDelay);
  158. task_environment_.GetMainThreadTaskRunner()->PostTask(
  159. FROM_HERE,
  160. base::BindOnce(std::move(output_ready_cb), std::move(frame)));
  161. }
  162. void OnBytesDecoded(int count) { num_decoded_bytes_unreported_ += count; }
  163. // Callback to create a list of decoders for the DecoderSelector to select
  164. // from. Decoder selection happens
  165. // - on the initial selection in Initialize(),
  166. // - on decoder reinitialization failure, which can be simulated by calling
  167. // decoder_->SimulateFailureToInit(), and
  168. // - on decode error of the first buffer, which can be simulated by calling
  169. // decoder_->SimulateError() before reading the first frame.
  170. std::vector<std::unique_ptr<VideoDecoder>> CreateVideoDecodersForTest() {
  171. // Previously decoders could have been destroyed on decoder reselection.
  172. decoders_.clear();
  173. // Provide 3 decoders to test fallback cases.
  174. // TODO(xhwang): We should test the case where only certain decoder
  175. // supports encrypted streams. Currently this is hard to test because we use
  176. // parameterized tests which need to pass in all combinations.
  177. std::vector<std::unique_ptr<VideoDecoder>> decoders;
  178. #if !BUILDFLAG(IS_ANDROID)
  179. // Note this is _not_ inserted into |decoders_| below, so we don't need to
  180. // adjust the indices used below to compensate.
  181. decoders.push_back(std::make_unique<DecryptingVideoDecoder>(
  182. task_environment_.GetMainThreadTaskRunner(), &media_log_));
  183. #endif
  184. for (int i = 0; i < 3; ++i) {
  185. auto decoder = std::make_unique<FakeVideoDecoder>(
  186. GetDecoderId(i), GetParam().decoding_delay,
  187. GetParam().parallel_decoding,
  188. base::BindRepeating(&VideoDecoderStreamTest::OnBytesDecoded,
  189. base::Unretained(this)));
  190. if (GetParam().is_encrypted && !GetParam().has_decryptor)
  191. decoder->EnableEncryptedConfigSupport();
  192. // Keep a reference so we can change the behavior of each decoder.
  193. decoders_.push_back(decoder->GetWeakPtr());
  194. decoders.push_back(std::move(decoder));
  195. }
  196. for (const auto i : decoder_indices_to_fail_init_)
  197. decoders_[i]->SimulateFailureToInit();
  198. for (const auto i : decoder_indices_to_hold_init_)
  199. decoders_[i]->HoldNextInit();
  200. for (const auto i : decoder_indices_to_hold_decode_)
  201. decoders_[i]->HoldDecode();
  202. for (const auto i : platform_decoder_indices_)
  203. decoders_[i]->SetIsPlatformDecoder(true);
  204. return decoders;
  205. }
  206. void ClearDecoderInitExpectations() {
  207. decoder_indices_to_fail_init_.clear();
  208. decoder_indices_to_hold_init_.clear();
  209. decoder_indices_to_hold_decode_.clear();
  210. platform_decoder_indices_.clear();
  211. }
  212. // On next decoder selection, fail initialization on decoders specified by
  213. // |decoder_indices|.
  214. void FailDecoderInitOnSelection(std::vector<int> decoder_indices) {
  215. decoder_indices_to_fail_init_ = std::move(decoder_indices);
  216. for (int i : decoder_indices_to_fail_init_) {
  217. if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
  218. decoders_[i]->SimulateFailureToInit();
  219. }
  220. }
  221. // On next decoder selection, hold initialization on decoders specified by
  222. // |decoder_indices|.
  223. void HoldDecoderInitOnSelection(std::vector<int> decoder_indices) {
  224. decoder_indices_to_hold_init_ = std::move(decoder_indices);
  225. for (int i : decoder_indices_to_hold_init_) {
  226. if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
  227. decoders_[i]->HoldNextInit();
  228. }
  229. }
  230. // After next decoder selection, hold decode on decoders specified by
  231. // |decoder_indices|. This is needed because after decoder selection decode
  232. // may be resumed immediately and it'll be too late to hold decode then.
  233. void HoldDecodeAfterSelection(std::vector<int> decoder_indices) {
  234. decoder_indices_to_hold_decode_ = std::move(decoder_indices);
  235. for (int i : decoder_indices_to_hold_decode_) {
  236. if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
  237. decoders_[i]->HoldDecode();
  238. }
  239. }
  240. void EnablePlatformDecoders(std::vector<int> decoder_indices) {
  241. platform_decoder_indices_ = std::move(decoder_indices);
  242. for (int i : platform_decoder_indices_) {
  243. if (!decoders_.empty() && decoders_[i] && decoders_[i].get() != decoder_)
  244. decoders_[i]->SetIsPlatformDecoder(true);
  245. }
  246. }
  247. // Updates the |decoder_| currently being used by VideoDecoderStream.
  248. void OnDecoderChanged(VideoDecoder* decoder) {
  249. if (!decoder) {
  250. decoder_ = nullptr;
  251. return;
  252. }
  253. // Ensure there's a media log created whenever selecting a decoder.
  254. EXPECT_MEDIA_LOG(HasSubstr("for video decoding, config"));
  255. decoder_ = static_cast<FakeVideoDecoder*>(decoder);
  256. ASSERT_TRUE(decoder_->GetDecoderId() == GetDecoderId(0) ||
  257. decoder_->GetDecoderId() == GetDecoderId(1) ||
  258. decoder_->GetDecoderId() == GetDecoderId(2));
  259. }
  260. MOCK_METHOD1(OnWaiting, void(WaitingReason));
  261. void OnStatistics(const PipelineStatistics& statistics) {
  262. num_decoded_bytes_unreported_ -= statistics.video_bytes_decoded;
  263. }
  264. void OnInitialized(bool success) {
  265. DCHECK(!pending_read_);
  266. DCHECK(!pending_reset_);
  267. DCHECK(pending_initialize_);
  268. pending_initialize_ = false;
  269. is_initialized_ = success;
  270. if (!success)
  271. decoders_.clear();
  272. }
  273. void Initialize() {
  274. if (!demuxer_stream_) {
  275. demuxer_stream_ = std::make_unique<FakeDemuxerStream>(
  276. kNumConfigs, kNumBuffersInOneConfig, GetParam().is_encrypted);
  277. }
  278. pending_initialize_ = true;
  279. video_decoder_stream_->Initialize(
  280. demuxer_stream_.get(),
  281. base::BindOnce(&VideoDecoderStreamTest::OnInitialized,
  282. base::Unretained(this)),
  283. cdm_context_.get(),
  284. base::BindRepeating(&VideoDecoderStreamTest::OnStatistics,
  285. base::Unretained(this)),
  286. base::BindRepeating(&VideoDecoderStreamTest::OnWaiting,
  287. base::Unretained(this)));
  288. EXPECT_MEDIA_LOG(HasSubstr("video")).Times(AnyNumber());
  289. EXPECT_MEDIA_LOG(HasSubstr("Video")).Times(AnyNumber());
  290. EXPECT_MEDIA_LOG(HasSubstr("audio")).Times(AnyNumber());
  291. EXPECT_MEDIA_LOG(HasSubstr("Audio")).Times(AnyNumber());
  292. base::RunLoop().RunUntilIdle();
  293. }
  294. // Fake Decrypt() function used by DecryptingDemuxerStream. It does nothing
  295. // but removes the DecryptConfig to make the buffer unencrypted.
  296. void Decrypt(Decryptor::StreamType stream_type,
  297. scoped_refptr<DecoderBuffer> encrypted,
  298. Decryptor::DecryptCB decrypt_cb) {
  299. DCHECK(encrypted->decrypt_config());
  300. if (has_no_key_) {
  301. std::move(decrypt_cb).Run(Decryptor::kNoKey, nullptr);
  302. return;
  303. }
  304. DCHECK_EQ(stream_type, Decryptor::kVideo);
  305. scoped_refptr<DecoderBuffer> decrypted =
  306. DecoderBuffer::CopyFrom(encrypted->data(), encrypted->data_size());
  307. if (encrypted->is_key_frame())
  308. decrypted->set_is_key_frame(true);
  309. decrypted->set_timestamp(encrypted->timestamp());
  310. decrypted->set_duration(encrypted->duration());
  311. std::move(decrypt_cb).Run(Decryptor::kSuccess, decrypted);
  312. }
  313. // Callback for VideoDecoderStream::Read().
  314. void FrameReady(VideoDecoderStream::ReadResult result) {
  315. DCHECK(pending_read_);
  316. last_read_status_code_ = result.code();
  317. scoped_refptr<VideoFrame> frame =
  318. last_read_status_code_ == DecoderStatus::Codes::kOk
  319. ? std::move(result).value()
  320. : nullptr;
  321. frame_read_ = frame;
  322. if (frame && !frame->metadata().end_of_stream) {
  323. EXPECT_EQ(*frame->metadata().frame_duration, demuxer_stream_->duration());
  324. num_decoded_frames_++;
  325. }
  326. pending_read_ = false;
  327. }
  328. void OnReset() {
  329. DCHECK(!pending_read_);
  330. DCHECK(pending_reset_);
  331. pending_reset_ = false;
  332. }
  333. void ReadOneFrame() {
  334. frame_read_ = nullptr;
  335. pending_read_ = true;
  336. video_decoder_stream_->Read(base::BindOnce(
  337. &VideoDecoderStreamTest::FrameReady, base::Unretained(this)));
  338. base::RunLoop().RunUntilIdle();
  339. }
  340. void ReadUntilPending() {
  341. do {
  342. ReadOneFrame();
  343. } while (!pending_read_);
  344. }
  345. void ReadAllFrames(int expected_decoded_frames) {
  346. // Reading all frames reinitializes the demuxer.
  347. do {
  348. ReadOneFrame();
  349. } while (frame_read_.get() && !frame_read_->metadata().end_of_stream);
  350. DCHECK_EQ(expected_decoded_frames, num_decoded_frames_);
  351. }
  352. void ReadAllFrames() {
  353. // No frames should have been dropped.
  354. ReadAllFrames(kNumConfigs * kNumBuffersInOneConfig);
  355. }
  356. enum PendingState {
  357. NOT_PENDING,
  358. DEMUXER_READ_NORMAL,
  359. DEMUXER_READ_CONFIG_CHANGE,
  360. DECRYPTOR_NO_KEY,
  361. DECODER_REINIT,
  362. DECODER_DECODE,
  363. DECODER_RESET
  364. };
  365. void EnterPendingState(PendingState state) {
  366. DCHECK_NE(state, NOT_PENDING);
  367. switch (state) {
  368. case DEMUXER_READ_NORMAL:
  369. demuxer_stream_->HoldNextRead();
  370. ReadUntilPending();
  371. break;
  372. case DEMUXER_READ_CONFIG_CHANGE:
  373. demuxer_stream_->HoldNextConfigChangeRead();
  374. ReadUntilPending();
  375. break;
  376. case DECRYPTOR_NO_KEY:
  377. if (GetParam().is_encrypted && GetParam().has_decryptor) {
  378. EXPECT_MEDIA_LOG(HasSubstr("no key for key ID"));
  379. EXPECT_CALL(*this, OnWaiting(WaitingReason::kNoDecryptionKey));
  380. has_no_key_ = true;
  381. }
  382. ReadOneFrame();
  383. break;
  384. case DECODER_REINIT:
  385. decoder_->HoldNextInit();
  386. ReadUntilPending();
  387. break;
  388. case DECODER_DECODE:
  389. decoder_->HoldDecode();
  390. ReadUntilPending();
  391. break;
  392. case DECODER_RESET:
  393. decoder_->HoldNextReset();
  394. pending_reset_ = true;
  395. video_decoder_stream_->Reset(base::BindOnce(
  396. &VideoDecoderStreamTest::OnReset, base::Unretained(this)));
  397. base::RunLoop().RunUntilIdle();
  398. break;
  399. case NOT_PENDING:
  400. NOTREACHED();
  401. break;
  402. }
  403. }
  404. void SatisfyPendingCallback(PendingState state) {
  405. DCHECK_NE(state, NOT_PENDING);
  406. switch (state) {
  407. case DEMUXER_READ_CONFIG_CHANGE:
  408. EXPECT_MEDIA_LOG(HasSubstr("decoder config changed"))
  409. .Times(testing::AtLeast(1));
  410. [[fallthrough]];
  411. case DEMUXER_READ_NORMAL:
  412. demuxer_stream_->SatisfyRead();
  413. break;
  414. // This is only interesting to test during VideoDecoderStream destruction.
  415. // There's no need to satisfy a callback.
  416. case DECRYPTOR_NO_KEY:
  417. NOTREACHED();
  418. break;
  419. case DECODER_REINIT:
  420. decoder_->SatisfyInit();
  421. break;
  422. case DECODER_DECODE:
  423. decoder_->SatisfyDecode();
  424. break;
  425. case DECODER_RESET:
  426. decoder_->SatisfyReset();
  427. break;
  428. case NOT_PENDING:
  429. NOTREACHED();
  430. break;
  431. }
  432. base::RunLoop().RunUntilIdle();
  433. }
  434. void Read() {
  435. EnterPendingState(DECODER_DECODE);
  436. SatisfyPendingCallback(DECODER_DECODE);
  437. }
  438. void Reset() {
  439. EnterPendingState(DECODER_RESET);
  440. SatisfyPendingCallback(DECODER_RESET);
  441. }
  442. void ReadUntilDecoderReinitialized() {
  443. EnterPendingState(DECODER_REINIT);
  444. SatisfyPendingCallback(DECODER_REINIT);
  445. }
  446. base::test::SingleThreadTaskEnvironment task_environment_{
  447. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  448. StrictMock<MockMediaLog> media_log_;
  449. std::unique_ptr<VideoDecoderStream> video_decoder_stream_;
  450. std::unique_ptr<FakeDemuxerStream> demuxer_stream_;
  451. std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
  452. // Use NiceMock since we don't care about most of calls on the decryptor.
  453. std::unique_ptr<NiceMock<MockDecryptor>> decryptor_;
  454. // References to the list of decoders to be select from by DecoderSelector.
  455. // Three decoders are needed to test that decoder fallback can occur more than
  456. // once on a config change. They are owned by |video_decoder_stream_|.
  457. std::vector<base::WeakPtr<FakeVideoDecoder>> decoders_;
  458. std::vector<int> decoder_indices_to_fail_init_;
  459. std::vector<int> decoder_indices_to_hold_init_;
  460. std::vector<int> decoder_indices_to_hold_decode_;
  461. std::vector<int> platform_decoder_indices_;
  462. // The current decoder used by |video_decoder_stream_|.
  463. raw_ptr<FakeVideoDecoder> decoder_ = nullptr;
  464. bool is_initialized_;
  465. int num_decoded_frames_;
  466. bool pending_initialize_;
  467. bool pending_read_;
  468. bool pending_reset_;
  469. bool pending_stop_;
  470. int num_decoded_bytes_unreported_;
  471. scoped_refptr<VideoFrame> frame_read_;
  472. DecoderStatus::Codes last_read_status_code_;
  473. // Decryptor has no key to decrypt a frame.
  474. bool has_no_key_;
  475. };
  476. INSTANTIATE_TEST_SUITE_P(
  477. Clear,
  478. VideoDecoderStreamTest,
  479. ::testing::Values(VideoDecoderStreamTestParams(false, false, false, 0, 1),
  480. VideoDecoderStreamTestParams(false, false, false, 3, 1),
  481. VideoDecoderStreamTestParams(false, false, false, 7, 1),
  482. VideoDecoderStreamTestParams(false, false, true, 0, 1),
  483. VideoDecoderStreamTestParams(false, false, true, 3, 1)));
  484. INSTANTIATE_TEST_SUITE_P(
  485. EncryptedWithDecryptor,
  486. VideoDecoderStreamTest,
  487. ::testing::Values(VideoDecoderStreamTestParams(true, true, false, 7, 1),
  488. VideoDecoderStreamTestParams(true, true, true, 7, 1)));
  489. INSTANTIATE_TEST_SUITE_P(
  490. EncryptedWithoutDecryptor,
  491. VideoDecoderStreamTest,
  492. ::testing::Values(VideoDecoderStreamTestParams(true, false, false, 7, 1),
  493. VideoDecoderStreamTestParams(true, false, true, 7, 1)));
  494. INSTANTIATE_TEST_SUITE_P(
  495. Clear_Parallel,
  496. VideoDecoderStreamTest,
  497. ::testing::Values(VideoDecoderStreamTestParams(false, false, false, 0, 3),
  498. VideoDecoderStreamTestParams(false, false, false, 2, 3),
  499. VideoDecoderStreamTestParams(false, false, true, 0, 3),
  500. VideoDecoderStreamTestParams(false, false, true, 2, 3)));
  501. TEST_P(VideoDecoderStreamTest, CanReadWithoutStallingAtAnyTime) {
  502. ASSERT_FALSE(video_decoder_stream_->CanReadWithoutStalling());
  503. }
  504. TEST_P(VideoDecoderStreamTest, Initialization) {
  505. Initialize();
  506. EXPECT_TRUE(is_initialized_);
  507. }
  508. TEST_P(VideoDecoderStreamTest, AllDecoderInitializationFails) {
  509. FailDecoderInitOnSelection({0, 1, 2});
  510. Initialize();
  511. EXPECT_FALSE(is_initialized_);
  512. }
  513. TEST_P(VideoDecoderStreamTest, PartialDecoderInitializationFails) {
  514. FailDecoderInitOnSelection({0, 1});
  515. Initialize();
  516. EXPECT_TRUE(is_initialized_);
  517. }
  518. TEST_P(VideoDecoderStreamTest, ReadOneFrame) {
  519. Initialize();
  520. Read();
  521. }
  522. TEST_P(VideoDecoderStreamTest, ReadAllFrames) {
  523. Initialize();
  524. ReadAllFrames();
  525. }
  526. TEST_P(VideoDecoderStreamTest, Read_AfterReset) {
  527. Initialize();
  528. Reset();
  529. Read();
  530. Reset();
  531. Read();
  532. }
  533. // Tests that the decoder stream will switch from a software decoder to a
  534. // hardware decoder if the config size increases
  535. TEST_P(VideoDecoderStreamTest, ConfigChangeSwToHw) {
  536. EnablePlatformDecoders({1});
  537. // Create a demuxer stream with a config that increases in size
  538. auto const size_delta =
  539. TestVideoConfig::LargeCodedSize() - TestVideoConfig::NormalCodedSize();
  540. auto const width_delta = size_delta.width() / (kNumConfigs - 1);
  541. auto const height_delta = size_delta.height() / (kNumConfigs - 1);
  542. CreateDemuxerStream(TestVideoConfig::NormalCodedSize(),
  543. gfx::Vector2dF(width_delta, height_delta));
  544. Initialize();
  545. // Initially we should be using a software decoder
  546. EXPECT_TRUE(decoder_);
  547. EXPECT_FALSE(decoder_->IsPlatformDecoder());
  548. ReadAllFrames();
  549. // We should end up on a hardware decoder
  550. EXPECT_TRUE(decoder_->IsPlatformDecoder());
  551. }
  552. // Tests that the decoder stream will switch from a hardware decoder to a
  553. // software decoder if the config size decreases
  554. TEST_P(VideoDecoderStreamTest, ConfigChangeHwToSw) {
  555. EnablePlatformDecoders({1});
  556. // Create a demuxer stream with a config that progressively decreases in size
  557. auto const size_delta =
  558. TestVideoConfig::LargeCodedSize() - TestVideoConfig::NormalCodedSize();
  559. auto const width_delta = size_delta.width() / kNumConfigs;
  560. auto const height_delta = size_delta.height() / kNumConfigs;
  561. CreateDemuxerStream(TestVideoConfig::LargeCodedSize(),
  562. gfx::Vector2dF(-width_delta, -height_delta));
  563. Initialize();
  564. // We should initially be using a hardware decoder
  565. EXPECT_TRUE(decoder_);
  566. EXPECT_TRUE(decoder_->IsPlatformDecoder());
  567. ReadAllFrames();
  568. // We should end up on a software decoder
  569. EXPECT_FALSE(decoder_->IsPlatformDecoder());
  570. }
  571. TEST_P(VideoDecoderStreamTest, Read_ProperMetadata) {
  572. // For testing simplicity, omit parallel decode tests with a delay in frames.
  573. if (GetParam().parallel_decoding > 1 && GetParam().decoding_delay > 0)
  574. return;
  575. if (GetParam().has_prepare) {
  576. // Override the basic PrepareFrame() for a version that moves the MockTime
  577. // by kPrepareDelay. This simulates real work done (e.g. YUV conversion).
  578. video_decoder_stream_->SetPrepareCB(
  579. base::BindRepeating(&VideoDecoderStreamTest::PrepareFrameWithDelay,
  580. base::Unretained(this)));
  581. }
  582. constexpr base::TimeDelta kDecodeDelay = base::Milliseconds(10);
  583. Initialize();
  584. // Simulate time elapsed by the decoder.
  585. EnterPendingState(DECODER_DECODE);
  586. task_environment_.FastForwardBy(kDecodeDelay);
  587. SatisfyPendingCallback(DECODER_DECODE);
  588. EXPECT_TRUE(frame_read_);
  589. const VideoFrameMetadata& metadata = frame_read_->metadata();
  590. // Verify the decoding metadata is accurate.
  591. EXPECT_EQ(*metadata.decode_end_time - *metadata.decode_begin_time,
  592. kDecodeDelay);
  593. // Verify the processing metadata is accurate.
  594. const base::TimeDelta expected_processing_time =
  595. GetParam().has_prepare ? (kDecodeDelay + kPrepareDelay) : kDecodeDelay;
  596. EXPECT_EQ(*metadata.processing_time, expected_processing_time);
  597. }
  598. TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxer) {
  599. Initialize();
  600. demuxer_stream_->HoldNextRead();
  601. ReadOneFrame();
  602. EXPECT_TRUE(pending_read_);
  603. int demuxed_buffers = 0;
  604. // Pass frames from the demuxer to the VideoDecoderStream until the first read
  605. // request is satisfied.
  606. while (pending_read_) {
  607. ++demuxed_buffers;
  608. demuxer_stream_->SatisfyReadAndHoldNext();
  609. base::RunLoop().RunUntilIdle();
  610. }
  611. EXPECT_EQ(std::min(GetParam().decoding_delay + 1, kNumBuffersInOneConfig + 1),
  612. demuxed_buffers);
  613. // At this point the stream is waiting on read from the demuxer, but there is
  614. // no pending read from the stream. The stream should be blocked if we try
  615. // reading from it again.
  616. ReadUntilPending();
  617. demuxer_stream_->SatisfyRead();
  618. base::RunLoop().RunUntilIdle();
  619. EXPECT_FALSE(pending_read_);
  620. }
  621. TEST_P(VideoDecoderStreamTest, Read_BlockedDemuxerAndDecoder) {
  622. // Test applies only when the decoder allows multiple parallel requests.
  623. if (GetParam().parallel_decoding == 1)
  624. return;
  625. Initialize();
  626. demuxer_stream_->HoldNextRead();
  627. decoder_->HoldDecode();
  628. ReadOneFrame();
  629. EXPECT_TRUE(pending_read_);
  630. int demuxed_buffers = 0;
  631. // Pass frames from the demuxer to the VideoDecoderStream until the first read
  632. // request is satisfied, while always keeping one decode request pending.
  633. while (pending_read_) {
  634. ++demuxed_buffers;
  635. demuxer_stream_->SatisfyReadAndHoldNext();
  636. base::RunLoop().RunUntilIdle();
  637. // Always keep one decode request pending.
  638. if (demuxed_buffers > 1) {
  639. decoder_->SatisfySingleDecode();
  640. base::RunLoop().RunUntilIdle();
  641. }
  642. }
  643. ReadUntilPending();
  644. EXPECT_TRUE(pending_read_);
  645. // Unblocking one decode request should unblock read even when demuxer is
  646. // still blocked.
  647. decoder_->SatisfySingleDecode();
  648. base::RunLoop().RunUntilIdle();
  649. EXPECT_FALSE(pending_read_);
  650. // Stream should still be blocked on the demuxer after unblocking the decoder.
  651. decoder_->SatisfyDecode();
  652. ReadUntilPending();
  653. EXPECT_TRUE(pending_read_);
  654. // Verify that the stream has returned all frames that have been demuxed,
  655. // accounting for the decoder delay.
  656. EXPECT_EQ(demuxed_buffers - GetParam().decoding_delay, num_decoded_frames_);
  657. // Unblocking the demuxer will unblock the stream.
  658. demuxer_stream_->SatisfyRead();
  659. base::RunLoop().RunUntilIdle();
  660. EXPECT_FALSE(pending_read_);
  661. }
  662. TEST_P(VideoDecoderStreamTest, Read_DuringEndOfStreamDecode) {
  663. // Test applies only when the decoder allows multiple parallel requests, and
  664. // they are not satisfied in a single batch.
  665. if (GetParam().parallel_decoding == 1 || GetParam().decoding_delay != 0)
  666. return;
  667. Initialize();
  668. decoder_->HoldDecode();
  669. // Read all of the frames up to end of stream. Since parallel decoding is
  670. // enabled, the end of stream buffer will be sent to the decoder immediately,
  671. // but we don't satisfy it yet.
  672. for (int configuration = 0; configuration < kNumConfigs; configuration++) {
  673. for (int frame = 0; frame < kNumBuffersInOneConfig; frame++) {
  674. ReadOneFrame();
  675. while (pending_read_) {
  676. decoder_->SatisfySingleDecode();
  677. base::RunLoop().RunUntilIdle();
  678. }
  679. }
  680. }
  681. // Read() again. The callback must be delayed until the decode completes.
  682. ReadOneFrame();
  683. ASSERT_TRUE(pending_read_);
  684. // Satisfy decoding of the end of stream buffer. The read should complete.
  685. decoder_->SatisfySingleDecode();
  686. base::RunLoop().RunUntilIdle();
  687. ASSERT_FALSE(pending_read_);
  688. EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
  689. // The read output should indicate end of stream.
  690. ASSERT_TRUE(frame_read_.get());
  691. EXPECT_TRUE(frame_read_->metadata().end_of_stream);
  692. }
  693. TEST_P(VideoDecoderStreamTest, Read_DemuxerStreamReadError) {
  694. Initialize();
  695. EnterPendingState(DEMUXER_READ_NORMAL);
  696. InSequence s;
  697. if (GetParam().is_encrypted && GetParam().has_decryptor) {
  698. EXPECT_MEDIA_LOG(
  699. HasSubstr("DecryptingDemuxerStream: demuxer stream read error"));
  700. }
  701. EXPECT_MEDIA_LOG(HasSubstr("video demuxer stream read error"));
  702. demuxer_stream_->Error();
  703. base::RunLoop().RunUntilIdle();
  704. ASSERT_FALSE(pending_read_);
  705. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
  706. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
  707. }
  708. // No Reset() before initialization is successfully completed.
  709. TEST_P(VideoDecoderStreamTest, Reset_AfterInitialization) {
  710. Initialize();
  711. Reset();
  712. Read();
  713. }
  714. TEST_P(VideoDecoderStreamTest, Reset_DuringReinitialization) {
  715. Initialize();
  716. EnterPendingState(DECODER_REINIT);
  717. // VideoDecoder::Reset() is not called when we reset during reinitialization.
  718. pending_reset_ = true;
  719. video_decoder_stream_->Reset(
  720. base::BindOnce(&VideoDecoderStreamTest::OnReset, base::Unretained(this)));
  721. SatisfyPendingCallback(DECODER_REINIT);
  722. Read();
  723. }
  724. TEST_P(VideoDecoderStreamTest, Reset_AfterReinitialization) {
  725. Initialize();
  726. EnterPendingState(DECODER_REINIT);
  727. SatisfyPendingCallback(DECODER_REINIT);
  728. Reset();
  729. Read();
  730. }
  731. TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_Normal) {
  732. Initialize();
  733. EnterPendingState(DEMUXER_READ_NORMAL);
  734. EnterPendingState(DECODER_RESET);
  735. SatisfyPendingCallback(DEMUXER_READ_NORMAL);
  736. SatisfyPendingCallback(DECODER_RESET);
  737. Read();
  738. }
  739. TEST_P(VideoDecoderStreamTest, Reset_DuringDemuxerRead_ConfigChange) {
  740. Initialize();
  741. EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
  742. EnterPendingState(DECODER_RESET);
  743. SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
  744. SatisfyPendingCallback(DECODER_RESET);
  745. Read();
  746. }
  747. TEST_P(VideoDecoderStreamTest, Reset_DuringNormalDecoderDecode) {
  748. Initialize();
  749. EnterPendingState(DECODER_DECODE);
  750. EnterPendingState(DECODER_RESET);
  751. SatisfyPendingCallback(DECODER_DECODE);
  752. SatisfyPendingCallback(DECODER_RESET);
  753. Read();
  754. }
  755. TEST_P(VideoDecoderStreamTest, Reset_AfterNormalRead) {
  756. Initialize();
  757. Read();
  758. Reset();
  759. Read();
  760. }
  761. TEST_P(VideoDecoderStreamTest, Reset_AfterDemuxerRead_ConfigChange) {
  762. Initialize();
  763. EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
  764. SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
  765. Reset();
  766. Read();
  767. }
  768. TEST_P(VideoDecoderStreamTest, Reset_AfterEndOfStream) {
  769. Initialize();
  770. ReadAllFrames();
  771. Reset();
  772. num_decoded_frames_ = 0;
  773. demuxer_stream_->SeekToStart();
  774. ReadAllFrames();
  775. }
  776. TEST_P(VideoDecoderStreamTest, Reset_DuringNoKeyRead) {
  777. Initialize();
  778. EnterPendingState(DECRYPTOR_NO_KEY);
  779. Reset();
  780. }
  781. // In the following Destroy_* tests, |video_decoder_stream_| is destroyed in
  782. // VideoDecoderStreamTest destructor.
  783. TEST_P(VideoDecoderStreamTest, Destroy_BeforeInitialization) {}
  784. TEST_P(VideoDecoderStreamTest, Destroy_DuringInitialization) {
  785. HoldDecoderInitOnSelection({0});
  786. Initialize();
  787. }
  788. TEST_P(VideoDecoderStreamTest, Destroy_AfterInitialization) {
  789. Initialize();
  790. }
  791. TEST_P(VideoDecoderStreamTest, Destroy_DuringReinitialization) {
  792. Initialize();
  793. EnterPendingState(DECODER_REINIT);
  794. }
  795. TEST_P(VideoDecoderStreamTest, Destroy_AfterReinitialization) {
  796. Initialize();
  797. EnterPendingState(DECODER_REINIT);
  798. SatisfyPendingCallback(DECODER_REINIT);
  799. }
  800. TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_Normal) {
  801. Initialize();
  802. EnterPendingState(DEMUXER_READ_NORMAL);
  803. }
  804. TEST_P(VideoDecoderStreamTest, Destroy_DuringDemuxerRead_ConfigChange) {
  805. Initialize();
  806. EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
  807. }
  808. TEST_P(VideoDecoderStreamTest, Destroy_DuringNormalDecoderDecode) {
  809. Initialize();
  810. EnterPendingState(DECODER_DECODE);
  811. }
  812. TEST_P(VideoDecoderStreamTest, Destroy_AfterNormalRead) {
  813. Initialize();
  814. Read();
  815. }
  816. TEST_P(VideoDecoderStreamTest, Destroy_AfterConfigChangeRead) {
  817. Initialize();
  818. EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
  819. SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
  820. }
  821. TEST_P(VideoDecoderStreamTest, Destroy_DuringDecoderReinitialization) {
  822. Initialize();
  823. EnterPendingState(DECODER_REINIT);
  824. }
  825. TEST_P(VideoDecoderStreamTest, Destroy_DuringNoKeyRead) {
  826. Initialize();
  827. EnterPendingState(DECRYPTOR_NO_KEY);
  828. }
  829. TEST_P(VideoDecoderStreamTest, Destroy_DuringReset) {
  830. Initialize();
  831. EnterPendingState(DECODER_RESET);
  832. }
  833. TEST_P(VideoDecoderStreamTest, Destroy_AfterReset) {
  834. Initialize();
  835. Reset();
  836. }
  837. TEST_P(VideoDecoderStreamTest, Destroy_DuringRead_DuringReset) {
  838. Initialize();
  839. EnterPendingState(DECODER_DECODE);
  840. EnterPendingState(DECODER_RESET);
  841. }
  842. TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_DuringReset) {
  843. Initialize();
  844. EnterPendingState(DECODER_DECODE);
  845. EnterPendingState(DECODER_RESET);
  846. SatisfyPendingCallback(DECODER_DECODE);
  847. }
  848. TEST_P(VideoDecoderStreamTest, Destroy_AfterRead_AfterReset) {
  849. Initialize();
  850. Read();
  851. Reset();
  852. }
  853. // The following tests cover the fallback logic after reinitialization error or
  854. // decode error of the first buffer after initialization.
  855. TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeError) {
  856. Initialize();
  857. decoder_->SimulateError();
  858. ReadOneFrame();
  859. // |video_decoder_stream_| should have fallen back to a new decoder.
  860. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  861. ASSERT_FALSE(pending_read_);
  862. ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
  863. // Check that we fell back to Decoder2.
  864. ASSERT_GT(decoder_->total_bytes_decoded(), 0);
  865. // Verify no frame was dropped.
  866. ReadAllFrames();
  867. }
  868. TEST_P(VideoDecoderStreamTest,
  869. FallbackDecoder_EndOfStreamReachedBeforeFallback) {
  870. // Only consider cases where there is a decoder delay. For test simplicity,
  871. // omit the parallel case.
  872. if (GetParam().decoding_delay == 0 || GetParam().parallel_decoding > 1)
  873. return;
  874. Initialize();
  875. decoder_->HoldDecode();
  876. ReadOneFrame();
  877. // One buffer should have already pulled from the demuxer stream. Set the next
  878. // one to be an EOS.
  879. demuxer_stream_->SeekToEndOfStream();
  880. decoder_->SatisfySingleDecode();
  881. base::RunLoop().RunUntilIdle();
  882. // |video_decoder_stream_| should not have emitted a frame.
  883. EXPECT_TRUE(pending_read_);
  884. // Pending buffers should contain a regular buffer and an EOS buffer.
  885. EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 2);
  886. decoder_->SimulateError();
  887. base::RunLoop().RunUntilIdle();
  888. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  889. // A frame should have been emitted.
  890. EXPECT_FALSE(pending_read_);
  891. EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
  892. EXPECT_FALSE(frame_read_->metadata().end_of_stream);
  893. EXPECT_GT(decoder_->total_bytes_decoded(), 0);
  894. ReadOneFrame();
  895. EXPECT_FALSE(pending_read_);
  896. EXPECT_EQ(0, video_decoder_stream_->get_fallback_buffers_size_for_testing());
  897. EXPECT_TRUE(frame_read_->metadata().end_of_stream);
  898. }
  899. TEST_P(VideoDecoderStreamTest,
  900. FallbackDecoder_DoesReinitializeStompPendingRead) {
  901. // Test only the case where there is no decoding delay and parallel decoding.
  902. if (GetParam().decoding_delay != 0 || GetParam().parallel_decoding <= 1)
  903. return;
  904. Initialize();
  905. decoder_->HoldDecode();
  906. // Queue one read, defer the second.
  907. frame_read_ = nullptr;
  908. pending_read_ = true;
  909. video_decoder_stream_->Read(base::BindOnce(
  910. &VideoDecoderStreamTest::FrameReady, base::Unretained(this)));
  911. demuxer_stream_->HoldNextRead();
  912. // Force an error to occur on the first decode, but ensure it isn't propagated
  913. // until after the next read has been started.
  914. decoder_->SimulateError();
  915. HoldDecodeAfterSelection({1});
  916. // Complete the fallback to the second decoder with the read still pending.
  917. base::RunLoop().RunUntilIdle();
  918. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  919. // Can't check the original decoder right now, it might have been destroyed
  920. // already. Verify that there was nothing decoded until we kicked the decoder.
  921. EXPECT_EQ(decoder_->total_bytes_decoded(), 0);
  922. decoder_->SatisfyDecode();
  923. const int first_decoded_bytes = decoder_->total_bytes_decoded();
  924. ASSERT_GT(first_decoded_bytes, 0);
  925. // Satisfy the previously pending read and ensure it is decoded.
  926. demuxer_stream_->SatisfyRead();
  927. base::RunLoop().RunUntilIdle();
  928. ASSERT_GT(decoder_->total_bytes_decoded(), first_decoded_bytes);
  929. }
  930. TEST_P(VideoDecoderStreamTest, FallbackDecoder_DecodeErrorRepeated) {
  931. Initialize();
  932. // Hold other decoders to simulate errors.
  933. HoldDecodeAfterSelection({1, 2});
  934. // Simulate decode error to trigger the fallback path.
  935. decoder_->SimulateError();
  936. ReadOneFrame();
  937. base::RunLoop().RunUntilIdle();
  938. // Expect decoder 1 to be tried.
  939. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  940. decoder_->SimulateError();
  941. base::RunLoop().RunUntilIdle();
  942. // Then decoder 2.
  943. ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
  944. decoder_->SimulateError();
  945. base::RunLoop().RunUntilIdle();
  946. // No decoders left, expect failure.
  947. EXPECT_EQ(decoder_, nullptr);
  948. EXPECT_FALSE(pending_read_);
  949. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
  950. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
  951. }
  952. // This tests verifies that we properly fallback to a new decoder if the first
  953. // decode after a config change fails.
  954. TEST_P(VideoDecoderStreamTest,
  955. FallbackDecoder_SelectedOnMidstreamDecodeErrorAfterReinitialization) {
  956. // For simplicity of testing, this test applies only when there is no decoder
  957. // delay and parallel decoding is disabled.
  958. if (GetParam().decoding_delay != 0 || GetParam().parallel_decoding > 1)
  959. return;
  960. Initialize();
  961. // Note: Completes decoding one frame, results in Decode() being called with
  962. // second frame that is not completed.
  963. ReadOneFrame();
  964. // Verify that the first frame was decoded successfully.
  965. EXPECT_FALSE(pending_read_);
  966. EXPECT_GT(decoder_->total_bytes_decoded(), 0);
  967. EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
  968. // Continue up to the point of reinitialization.
  969. EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
  970. // Hold decodes to prevent a frame from being outputted upon reinitialization.
  971. decoder_->HoldDecode();
  972. SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
  973. // DecoderStream sends an EOS to flush the decoder during config changes.
  974. // Let the EOS decode be satisfied to properly complete the decoder reinit.
  975. decoder_->SatisfySingleDecode();
  976. base::RunLoop().RunUntilIdle();
  977. EXPECT_TRUE(pending_read_);
  978. // Fail the first decode, before a frame can be outputted.
  979. decoder_->SimulateError();
  980. base::RunLoop().RunUntilIdle();
  981. ReadOneFrame();
  982. // Verify that fallback happened.
  983. EXPECT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
  984. EXPECT_FALSE(pending_read_);
  985. EXPECT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
  986. EXPECT_GT(decoder_->total_bytes_decoded(), 0);
  987. }
  988. TEST_P(VideoDecoderStreamTest,
  989. FallbackDecoder_DecodeErrorRepeated_AfterReinitialization) {
  990. Initialize();
  991. // Simulate decode error to trigger fallback.
  992. decoder_->SimulateError();
  993. ReadOneFrame();
  994. base::RunLoop().RunUntilIdle();
  995. // Simulate reinitialize error of decoder 1.
  996. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  997. decoder_->SimulateFailureToInit();
  998. HoldDecodeAfterSelection({0, 1, 2});
  999. ReadUntilDecoderReinitialized();
  1000. // Decoder 0 should be selected again.
  1001. ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
  1002. decoder_->SimulateError();
  1003. base::RunLoop().RunUntilIdle();
  1004. // Decoder 1.
  1005. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  1006. decoder_->SimulateError();
  1007. base::RunLoop().RunUntilIdle();
  1008. // Decoder 2.
  1009. ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
  1010. decoder_->SimulateError();
  1011. base::RunLoop().RunUntilIdle();
  1012. // No decoders left.
  1013. EXPECT_EQ(decoder_, nullptr);
  1014. EXPECT_FALSE(pending_read_);
  1015. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
  1016. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
  1017. }
  1018. TEST_P(VideoDecoderStreamTest,
  1019. FallbackDecoder_ConfigChangeClearsPendingBuffers) {
  1020. // Test case is only interesting if the decoder can receive a config change
  1021. // before returning its first frame.
  1022. if (GetParam().decoding_delay < kNumBuffersInOneConfig)
  1023. return;
  1024. Initialize();
  1025. EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
  1026. ASSERT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
  1027. SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
  1028. ASSERT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
  1029. EXPECT_FALSE(pending_read_);
  1030. ReadAllFrames();
  1031. }
  1032. TEST_P(VideoDecoderStreamTest,
  1033. FallbackDecoder_ErrorDuringConfigChangeFlushing) {
  1034. // Test case is only interesting if the decoder can receive a config change
  1035. // before returning its first frame.
  1036. if (GetParam().decoding_delay < kNumBuffersInOneConfig)
  1037. return;
  1038. Initialize();
  1039. EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
  1040. EXPECT_GT(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
  1041. decoder_->HoldDecode();
  1042. SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
  1043. // The flush request should have been sent and held.
  1044. EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
  1045. EXPECT_TRUE(pending_read_);
  1046. // Triggering an error here will cause the frames in selected decoder to be
  1047. // lost. There are no pending buffers to give to |decoders_[1]| due to
  1048. // http://crbug.com/603713
  1049. decoder_->SimulateError();
  1050. base::RunLoop().RunUntilIdle();
  1051. // We want to make sure the fallback decoder can decode the rest of the frames
  1052. // in the demuxer stream.
  1053. ReadAllFrames(kNumBuffersInOneConfig * (kNumConfigs - 1));
  1054. }
  1055. TEST_P(VideoDecoderStreamTest,
  1056. FallbackDecoder_PendingBuffersIsFilledAndCleared) {
  1057. // Test applies only when there is a decoder delay, and the decoder will not
  1058. // receive a config change before outputting its first frame. Parallel
  1059. // decoding is also disabled in this test case, for readability and simplicity
  1060. // of the unit test.
  1061. if (GetParam().decoding_delay == 0 ||
  1062. GetParam().decoding_delay > kNumBuffersInOneConfig ||
  1063. GetParam().parallel_decoding > 1) {
  1064. return;
  1065. }
  1066. Initialize();
  1067. // Block on demuxer read and decoder decode so we can step through.
  1068. demuxer_stream_->HoldNextRead();
  1069. decoder_->HoldDecode();
  1070. ReadOneFrame();
  1071. int demuxer_reads_satisfied = 0;
  1072. // Send back and requests buffers until the next one would fill the decoder
  1073. // delay.
  1074. while (demuxer_reads_satisfied < GetParam().decoding_delay - 1) {
  1075. // Send a buffer back.
  1076. demuxer_stream_->SatisfyReadAndHoldNext();
  1077. base::RunLoop().RunUntilIdle();
  1078. ++demuxer_reads_satisfied;
  1079. // Decode one buffer.
  1080. decoder_->SatisfySingleDecode();
  1081. base::RunLoop().RunUntilIdle();
  1082. EXPECT_TRUE(pending_read_);
  1083. EXPECT_EQ(demuxer_reads_satisfied,
  1084. video_decoder_stream_->get_pending_buffers_size_for_testing());
  1085. // No fallback buffers should be queued up yet.
  1086. EXPECT_EQ(0,
  1087. video_decoder_stream_->get_fallback_buffers_size_for_testing());
  1088. }
  1089. // Hold the init before triggering the error, to verify internal state.
  1090. demuxer_stream_->SatisfyReadAndHoldNext();
  1091. ++demuxer_reads_satisfied;
  1092. decoder_->SimulateError();
  1093. HoldDecoderInitOnSelection({1});
  1094. HoldDecodeAfterSelection({1});
  1095. base::RunLoop().RunUntilIdle();
  1096. EXPECT_TRUE(pending_read_);
  1097. EXPECT_EQ(demuxer_reads_satisfied,
  1098. video_decoder_stream_->get_pending_buffers_size_for_testing());
  1099. decoders_[1]->SatisfyInit();
  1100. base::RunLoop().RunUntilIdle();
  1101. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  1102. // Make sure the pending buffers have been transferred to fallback buffers.
  1103. // One call to Decode() during the initialization process, so we expect one
  1104. // buffer to already have been consumed from the fallback buffers.
  1105. // Pending buffers should never go down (unless we encounter a config change)
  1106. EXPECT_EQ(demuxer_reads_satisfied - 1,
  1107. video_decoder_stream_->get_fallback_buffers_size_for_testing());
  1108. EXPECT_EQ(demuxer_reads_satisfied,
  1109. video_decoder_stream_->get_pending_buffers_size_for_testing());
  1110. decoder_->SatisfyDecode();
  1111. base::RunLoop().RunUntilIdle();
  1112. // Make sure all buffers consumed by |decoders_| have come from the fallback.
  1113. // Pending buffers should not have been cleared yet.
  1114. EXPECT_EQ(0, video_decoder_stream_->get_fallback_buffers_size_for_testing());
  1115. EXPECT_EQ(demuxer_reads_satisfied,
  1116. video_decoder_stream_->get_pending_buffers_size_for_testing());
  1117. EXPECT_TRUE(pending_read_);
  1118. // Give the decoder one more buffer, enough to release a frame.
  1119. demuxer_stream_->SatisfyReadAndHoldNext();
  1120. base::RunLoop().RunUntilIdle();
  1121. // New buffers should not have been added after the frame was released.
  1122. EXPECT_EQ(video_decoder_stream_->get_pending_buffers_size_for_testing(), 0);
  1123. EXPECT_FALSE(pending_read_);
  1124. demuxer_stream_->SatisfyRead();
  1125. // Confirm no frames were dropped.
  1126. ReadAllFrames();
  1127. }
  1128. TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnDecodeThenInitErrors) {
  1129. Initialize();
  1130. decoder_->SimulateError();
  1131. FailDecoderInitOnSelection({1});
  1132. ReadOneFrame();
  1133. // Decoder 0 should be blocked, and decoder 1 fails to initialize, so
  1134. // |video_decoder_stream_| should have fallen back to decoder 2.
  1135. ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
  1136. ASSERT_FALSE(pending_read_);
  1137. ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
  1138. // Can't check previously selected decoder(s) right now, they might have been
  1139. // destroyed already.
  1140. ASSERT_GT(decoder_->total_bytes_decoded(), 0);
  1141. // Verify no frame was dropped.
  1142. ReadAllFrames();
  1143. }
  1144. TEST_P(VideoDecoderStreamTest, FallbackDecoder_SelectedOnInitThenDecodeErrors) {
  1145. FailDecoderInitOnSelection({0});
  1146. Initialize();
  1147. ASSERT_EQ(GetDecoderId(1), decoder_->GetDecoderId());
  1148. ClearDecoderInitExpectations();
  1149. decoder_->HoldDecode();
  1150. ReadOneFrame();
  1151. decoder_->SimulateError();
  1152. base::RunLoop().RunUntilIdle();
  1153. // |video_decoder_stream_| should have fallen back to decoder 2.
  1154. ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
  1155. ASSERT_FALSE(pending_read_);
  1156. ASSERT_EQ(last_read_status_code_, DecoderStatus::Codes::kOk);
  1157. // Can't check previously selected decoder(s) right now, they might have been
  1158. // destroyed already.
  1159. ASSERT_GT(decoder_->total_bytes_decoded(), 0);
  1160. // Verify no frame was dropped.
  1161. ReadAllFrames();
  1162. }
  1163. TEST_P(VideoDecoderStreamTest,
  1164. FallbackDecoder_NotSelectedOnMidstreamDecodeError) {
  1165. Initialize();
  1166. ReadOneFrame();
  1167. // Successfully received a frame.
  1168. EXPECT_FALSE(pending_read_);
  1169. ASSERT_GT(decoder_->total_bytes_decoded(), 0);
  1170. decoder_->SimulateError();
  1171. // The error must surface from Read() as DECODE_ERROR.
  1172. while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
  1173. ReadOneFrame();
  1174. base::RunLoop().RunUntilIdle();
  1175. EXPECT_FALSE(pending_read_);
  1176. }
  1177. // Verify the error was surfaced, rather than falling back to other decoders.
  1178. ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
  1179. EXPECT_FALSE(pending_read_);
  1180. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
  1181. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
  1182. }
  1183. TEST_P(VideoDecoderStreamTest, DecoderErrorWhenNotReading) {
  1184. Initialize();
  1185. decoder_->HoldDecode();
  1186. ReadOneFrame();
  1187. EXPECT_TRUE(pending_read_);
  1188. // Satisfy decode requests until we get the first frame out.
  1189. while (pending_read_) {
  1190. decoder_->SatisfySingleDecode();
  1191. base::RunLoop().RunUntilIdle();
  1192. }
  1193. // Trigger an error in the decoding.
  1194. decoder_->SimulateError();
  1195. // The error must surface from Read() as DECODE_ERROR.
  1196. while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
  1197. ReadOneFrame();
  1198. base::RunLoop().RunUntilIdle();
  1199. EXPECT_FALSE(pending_read_);
  1200. }
  1201. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
  1202. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
  1203. }
  1204. TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Once) {
  1205. Initialize();
  1206. decoder_->SimulateFailureToInit();
  1207. ReadUntilDecoderReinitialized();
  1208. // Should have fallen back to a new instance of decoder 0.
  1209. ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
  1210. ReadAllFrames();
  1211. ASSERT_GT(decoder_->total_bytes_decoded(), 0);
  1212. }
  1213. TEST_P(VideoDecoderStreamTest, ReinitializeFailure_Twice) {
  1214. Initialize();
  1215. // Trigger reinitialization error, and fallback to a new instance.
  1216. decoder_->SimulateFailureToInit();
  1217. ReadUntilDecoderReinitialized();
  1218. ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
  1219. ReadOneFrame();
  1220. // Trigger reinitialization error again. Since a frame was output, this will
  1221. // be a new instance of decoder 0 again.
  1222. decoder_->SimulateFailureToInit();
  1223. ReadUntilDecoderReinitialized();
  1224. ASSERT_EQ(GetDecoderId(0), decoder_->GetDecoderId());
  1225. ReadAllFrames();
  1226. }
  1227. TEST_P(VideoDecoderStreamTest, ReinitializeFailure_OneUnsupportedDecoder) {
  1228. Initialize();
  1229. // The current decoder will fail to reinitialize.
  1230. decoder_->SimulateFailureToInit();
  1231. // Decoder 1 will also fail to initialize on decoder selection.
  1232. FailDecoderInitOnSelection({0, 1});
  1233. ReadUntilDecoderReinitialized();
  1234. // As a result, decoder 2 will be selected.
  1235. ASSERT_EQ(GetDecoderId(2), decoder_->GetDecoderId());
  1236. ReadAllFrames();
  1237. }
  1238. TEST_P(VideoDecoderStreamTest, ReinitializeFailure_NoSupportedDecoder) {
  1239. Initialize();
  1240. // The current decoder will fail to reinitialize, triggering decoder
  1241. // selection.
  1242. decoder_->SimulateFailureToInit();
  1243. // All of the decoders will fail in decoder selection.
  1244. FailDecoderInitOnSelection({0, 1, 2});
  1245. ReadUntilDecoderReinitialized();
  1246. // The error will surface from Read() as DECODE_ERROR.
  1247. while (last_read_status_code_ == DecoderStatus::Codes::kOk) {
  1248. ReadOneFrame();
  1249. base::RunLoop().RunUntilIdle();
  1250. EXPECT_FALSE(pending_read_);
  1251. }
  1252. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kOk);
  1253. EXPECT_NE(last_read_status_code_, DecoderStatus::Codes::kAborted);
  1254. }
  1255. TEST_P(VideoDecoderStreamTest, Destroy_DuringFallbackDecoderSelection) {
  1256. Initialize();
  1257. decoder_->SimulateFailureToInit();
  1258. EnterPendingState(DECODER_REINIT);
  1259. HoldDecoderInitOnSelection({1});
  1260. SatisfyPendingCallback(DECODER_REINIT);
  1261. }
  1262. } // namespace media