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