fake_video_decoder_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // Copyright 2013 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 "media/filters/fake_video_decoder.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/task_environment.h"
  11. #include "media/base/decoder_buffer.h"
  12. #include "media/base/mock_filters.h"
  13. #include "media/base/test_helpers.h"
  14. #include "media/base/video_frame.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace media {
  17. static const int kTotalBuffers = 12;
  18. static const int kDurationMs = 30;
  19. struct FakeVideoDecoderTestParams {
  20. FakeVideoDecoderTestParams(int decoding_delay, int max_decode_requests)
  21. : decoding_delay(decoding_delay),
  22. max_decode_requests(max_decode_requests) {}
  23. int decoding_delay;
  24. int max_decode_requests;
  25. };
  26. class FakeVideoDecoderTest
  27. : public testing::Test,
  28. public testing::WithParamInterface<FakeVideoDecoderTestParams> {
  29. public:
  30. FakeVideoDecoderTest()
  31. : decoder_(new FakeVideoDecoder(
  32. 0xFACCE,
  33. GetParam().decoding_delay,
  34. GetParam().max_decode_requests,
  35. base::BindRepeating(&FakeVideoDecoderTest::OnBytesDecoded,
  36. base::Unretained(this)))),
  37. num_input_buffers_(0),
  38. num_decoded_frames_(0),
  39. num_bytes_decoded_(0),
  40. total_bytes_in_buffers_(0),
  41. last_decode_status_(DecoderStatus::Codes::kOk),
  42. pending_decode_requests_(0),
  43. is_reset_pending_(false) {}
  44. FakeVideoDecoderTest(const FakeVideoDecoderTest&) = delete;
  45. FakeVideoDecoderTest& operator=(const FakeVideoDecoderTest&) = delete;
  46. virtual ~FakeVideoDecoderTest() {
  47. Destroy();
  48. }
  49. void InitializeWithConfigAndExpectResult(const VideoDecoderConfig& config,
  50. bool success) {
  51. decoder_->Initialize(config, false, nullptr,
  52. base::BindOnce(
  53. [](bool success, DecoderStatus status) {
  54. EXPECT_EQ(status.is_ok(), success);
  55. },
  56. success),
  57. base::BindRepeating(&FakeVideoDecoderTest::FrameReady,
  58. base::Unretained(this)),
  59. base::NullCallback());
  60. base::RunLoop().RunUntilIdle();
  61. current_config_ = config;
  62. }
  63. void Initialize() {
  64. InitializeWithConfigAndExpectResult(TestVideoConfig::Normal(), true);
  65. }
  66. void EnterPendingInitState() {
  67. decoder_->HoldNextInit();
  68. Initialize();
  69. }
  70. void SatisfyInit() {
  71. decoder_->SatisfyInit();
  72. base::RunLoop().RunUntilIdle();
  73. }
  74. // Callback for VideoDecoder::Decode().
  75. void DecodeDone(DecoderStatus status) {
  76. DCHECK_GT(pending_decode_requests_, 0);
  77. --pending_decode_requests_;
  78. last_decode_status_ = std::move(status);
  79. }
  80. void FrameReady(scoped_refptr<VideoFrame> frame) {
  81. DCHECK(!frame->metadata().end_of_stream);
  82. last_decoded_frame_ = std::move(frame);
  83. num_decoded_frames_++;
  84. }
  85. void OnBytesDecoded(int count) {
  86. num_bytes_decoded_ += count;
  87. }
  88. enum CallbackResult {
  89. PENDING,
  90. OK,
  91. NOT_ENOUGH_DATA,
  92. ABORTED
  93. };
  94. void ExpectReadResult(CallbackResult result) {
  95. switch (result) {
  96. case PENDING:
  97. EXPECT_GT(pending_decode_requests_, 0);
  98. break;
  99. case OK:
  100. EXPECT_EQ(0, pending_decode_requests_);
  101. ASSERT_TRUE(last_decode_status_.is_ok());
  102. ASSERT_TRUE(last_decoded_frame_.get());
  103. break;
  104. case NOT_ENOUGH_DATA:
  105. EXPECT_EQ(0, pending_decode_requests_);
  106. ASSERT_TRUE(last_decode_status_.is_ok());
  107. ASSERT_FALSE(last_decoded_frame_.get());
  108. break;
  109. case ABORTED:
  110. EXPECT_EQ(0, pending_decode_requests_);
  111. ASSERT_EQ(DecoderStatus::Codes::kAborted, last_decode_status_.code());
  112. EXPECT_FALSE(last_decoded_frame_.get());
  113. break;
  114. }
  115. }
  116. void Decode() {
  117. scoped_refptr<DecoderBuffer> buffer;
  118. if (num_input_buffers_ < kTotalBuffers) {
  119. buffer = CreateFakeVideoBufferForTest(
  120. current_config_, base::Milliseconds(kDurationMs * num_input_buffers_),
  121. base::Milliseconds(kDurationMs));
  122. total_bytes_in_buffers_ += buffer->data_size();
  123. } else {
  124. buffer = DecoderBuffer::CreateEOSBuffer();
  125. }
  126. ++num_input_buffers_;
  127. ++pending_decode_requests_;
  128. decoder_->Decode(buffer, base::BindOnce(&FakeVideoDecoderTest::DecodeDone,
  129. base::Unretained(this)));
  130. base::RunLoop().RunUntilIdle();
  131. }
  132. void ReadOneFrame() {
  133. last_decoded_frame_.reset();
  134. do {
  135. Decode();
  136. } while (!last_decoded_frame_.get() && pending_decode_requests_ == 0);
  137. }
  138. void ReadAllFrames() {
  139. do {
  140. Decode();
  141. } while (num_input_buffers_ <= kTotalBuffers); // All input buffers + EOS.
  142. }
  143. void EnterPendingReadState() {
  144. // Pass the initial NOT_ENOUGH_DATA stage.
  145. ReadOneFrame();
  146. decoder_->HoldDecode();
  147. ReadOneFrame();
  148. ExpectReadResult(PENDING);
  149. }
  150. void SatisfyDecodeAndExpect(CallbackResult result) {
  151. decoder_->SatisfyDecode();
  152. base::RunLoop().RunUntilIdle();
  153. ExpectReadResult(result);
  154. }
  155. void SatisfyRead() {
  156. SatisfyDecodeAndExpect(OK);
  157. }
  158. // Callback for VideoDecoder::Reset().
  159. void OnDecoderReset() {
  160. DCHECK(is_reset_pending_);
  161. is_reset_pending_ = false;
  162. }
  163. void ExpectResetResult(CallbackResult result) {
  164. switch (result) {
  165. case PENDING:
  166. EXPECT_TRUE(is_reset_pending_);
  167. break;
  168. case OK:
  169. EXPECT_FALSE(is_reset_pending_);
  170. break;
  171. default:
  172. NOTREACHED();
  173. }
  174. }
  175. void ResetAndExpect(CallbackResult result) {
  176. is_reset_pending_ = true;
  177. decoder_->Reset(base::BindOnce(&FakeVideoDecoderTest::OnDecoderReset,
  178. base::Unretained(this)));
  179. base::RunLoop().RunUntilIdle();
  180. ExpectResetResult(result);
  181. }
  182. void EnterPendingResetState() {
  183. decoder_->HoldNextReset();
  184. ResetAndExpect(PENDING);
  185. }
  186. void SatisfyReset() {
  187. decoder_->SatisfyReset();
  188. base::RunLoop().RunUntilIdle();
  189. ExpectResetResult(OK);
  190. }
  191. void Destroy() {
  192. decoder_.reset();
  193. base::RunLoop().RunUntilIdle();
  194. // All pending callbacks must have been fired.
  195. DCHECK_EQ(pending_decode_requests_, 0);
  196. DCHECK(!is_reset_pending_);
  197. }
  198. base::test::SingleThreadTaskEnvironment task_environment_;
  199. VideoDecoderConfig current_config_;
  200. std::unique_ptr<FakeVideoDecoder> decoder_;
  201. int num_input_buffers_;
  202. int num_decoded_frames_;
  203. int num_bytes_decoded_;
  204. int total_bytes_in_buffers_;
  205. // Callback result/status.
  206. DecoderStatus last_decode_status_;
  207. scoped_refptr<VideoFrame> last_decoded_frame_;
  208. int pending_decode_requests_;
  209. bool is_reset_pending_;
  210. };
  211. INSTANTIATE_TEST_SUITE_P(NoParallelDecode,
  212. FakeVideoDecoderTest,
  213. ::testing::Values(FakeVideoDecoderTestParams(9, 1),
  214. FakeVideoDecoderTestParams(0, 1)));
  215. INSTANTIATE_TEST_SUITE_P(ParallelDecode,
  216. FakeVideoDecoderTest,
  217. ::testing::Values(FakeVideoDecoderTestParams(9, 3),
  218. FakeVideoDecoderTestParams(0, 3)));
  219. TEST_P(FakeVideoDecoderTest, Initialize) {
  220. Initialize();
  221. }
  222. TEST_P(FakeVideoDecoderTest, SimulateFailureToInitialize) {
  223. decoder_->SimulateFailureToInit();
  224. InitializeWithConfigAndExpectResult(TestVideoConfig::Normal(), false);
  225. Decode();
  226. EXPECT_THAT(last_decode_status_, IsDecodeErrorStatus());
  227. }
  228. TEST_P(FakeVideoDecoderTest, Read_AllFrames) {
  229. Initialize();
  230. ReadAllFrames();
  231. EXPECT_EQ(kTotalBuffers, num_decoded_frames_);
  232. EXPECT_EQ(total_bytes_in_buffers_, num_bytes_decoded_);
  233. }
  234. TEST_P(FakeVideoDecoderTest, Read_DecodingDelay) {
  235. Initialize();
  236. while (num_input_buffers_ < kTotalBuffers) {
  237. ReadOneFrame();
  238. EXPECT_EQ(num_input_buffers_,
  239. num_decoded_frames_ + GetParam().decoding_delay);
  240. }
  241. }
  242. TEST_P(FakeVideoDecoderTest, Read_ZeroDelay) {
  243. decoder_ = std::make_unique<FakeVideoDecoder>(
  244. 999, 0, 1,
  245. base::BindRepeating(&FakeVideoDecoderTest::OnBytesDecoded,
  246. base::Unretained(this)));
  247. Initialize();
  248. while (num_input_buffers_ < kTotalBuffers) {
  249. ReadOneFrame();
  250. EXPECT_EQ(num_input_buffers_, num_decoded_frames_);
  251. }
  252. }
  253. TEST_P(FakeVideoDecoderTest, Read_Pending_NotEnoughData) {
  254. if (GetParam().decoding_delay < 1)
  255. return;
  256. Initialize();
  257. decoder_->HoldDecode();
  258. ReadOneFrame();
  259. ExpectReadResult(PENDING);
  260. SatisfyDecodeAndExpect(NOT_ENOUGH_DATA);
  261. // Verify that FrameReady() hasn't been called.
  262. EXPECT_FALSE(last_decoded_frame_.get());
  263. }
  264. TEST_P(FakeVideoDecoderTest, Read_Pending_OK) {
  265. Initialize();
  266. EnterPendingReadState();
  267. SatisfyDecodeAndExpect(OK);
  268. }
  269. TEST_P(FakeVideoDecoderTest, Read_Parallel) {
  270. if (GetParam().max_decode_requests < 2)
  271. return;
  272. Initialize();
  273. decoder_->HoldDecode();
  274. for (int i = 0; i < GetParam().max_decode_requests; ++i) {
  275. ReadOneFrame();
  276. ExpectReadResult(PENDING);
  277. }
  278. EXPECT_EQ(GetParam().max_decode_requests, pending_decode_requests_);
  279. SatisfyDecodeAndExpect(
  280. GetParam().max_decode_requests > GetParam().decoding_delay
  281. ? OK
  282. : NOT_ENOUGH_DATA);
  283. }
  284. TEST_P(FakeVideoDecoderTest, ReadWithHold_DecodingDelay) {
  285. Initialize();
  286. // Hold all decodes and satisfy one decode at a time.
  287. decoder_->HoldDecode();
  288. int num_decodes_satisfied = 0;
  289. while (num_decoded_frames_ == 0) {
  290. while (pending_decode_requests_ < decoder_->GetMaxDecodeRequests())
  291. Decode();
  292. decoder_->SatisfySingleDecode();
  293. ++num_decodes_satisfied;
  294. base::RunLoop().RunUntilIdle();
  295. }
  296. DCHECK_EQ(num_decoded_frames_, 1);
  297. DCHECK_EQ(num_decodes_satisfied, GetParam().decoding_delay + 1);
  298. }
  299. TEST_P(FakeVideoDecoderTest, Reinitialize) {
  300. Initialize();
  301. ReadOneFrame();
  302. InitializeWithConfigAndExpectResult(TestVideoConfig::Large(), true);
  303. ReadOneFrame();
  304. }
  305. TEST_P(FakeVideoDecoderTest, SimulateFailureToReinitialize) {
  306. Initialize();
  307. ReadOneFrame();
  308. decoder_->SimulateFailureToInit();
  309. InitializeWithConfigAndExpectResult(TestVideoConfig::Normal(), false);
  310. Decode();
  311. EXPECT_THAT(last_decode_status_, IsDecodeErrorStatus());
  312. }
  313. // Reinitializing the decoder during the middle of the decoding process can
  314. // cause dropped frames.
  315. TEST_P(FakeVideoDecoderTest, Reinitialize_FrameDropped) {
  316. if (GetParam().decoding_delay < 1)
  317. return;
  318. Initialize();
  319. ReadOneFrame();
  320. Initialize();
  321. ReadAllFrames();
  322. EXPECT_LT(num_decoded_frames_, kTotalBuffers);
  323. }
  324. TEST_P(FakeVideoDecoderTest, Reset) {
  325. Initialize();
  326. ReadOneFrame();
  327. ResetAndExpect(OK);
  328. }
  329. TEST_P(FakeVideoDecoderTest, Reset_DuringPendingRead) {
  330. Initialize();
  331. EnterPendingReadState();
  332. ResetAndExpect(PENDING);
  333. SatisfyDecodeAndExpect(ABORTED);
  334. }
  335. TEST_P(FakeVideoDecoderTest, Reset_Pending) {
  336. Initialize();
  337. EnterPendingResetState();
  338. SatisfyReset();
  339. }
  340. TEST_P(FakeVideoDecoderTest, Reset_PendingDuringPendingRead) {
  341. Initialize();
  342. EnterPendingReadState();
  343. EnterPendingResetState();
  344. SatisfyDecodeAndExpect(ABORTED);
  345. SatisfyReset();
  346. }
  347. TEST_P(FakeVideoDecoderTest, Destroy) {
  348. Initialize();
  349. ReadOneFrame();
  350. ExpectReadResult(OK);
  351. Destroy();
  352. }
  353. TEST_P(FakeVideoDecoderTest, Destroy_DuringPendingInitialization) {
  354. EnterPendingInitState();
  355. Destroy();
  356. }
  357. TEST_P(FakeVideoDecoderTest, Destroy_DuringPendingRead) {
  358. Initialize();
  359. EnterPendingReadState();
  360. Destroy();
  361. }
  362. TEST_P(FakeVideoDecoderTest, Destroy_DuringPendingReset) {
  363. Initialize();
  364. EnterPendingResetState();
  365. Destroy();
  366. }
  367. TEST_P(FakeVideoDecoderTest, Destroy_DuringPendingReadAndPendingReset) {
  368. Initialize();
  369. EnterPendingReadState();
  370. EnterPendingResetState();
  371. Destroy();
  372. }
  373. } // namespace media