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.
6 #include "base/callback_helpers.h"
7 #include "base/message_loop/message_loop.h"
8 #include "media/base/gmock_callback_support.h"
9 #include "media/base/mock_filters.h"
10 #include "media/base/test_helpers.h"
11 #include "media/filters/fake_demuxer_stream.h"
12 #include "media/filters/fake_video_decoder.h"
13 #include "media/filters/video_frame_stream.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 using ::testing::Assign;
18 using ::testing::Invoke;
19 using ::testing::NiceMock;
20 using ::testing::Return;
21 using ::testing::SaveArg;
23 static const int kNumConfigs = 3;
24 static const int kNumBuffersInOneConfig = 5;
25 static const int kDecodingDelay = 7;
29 class VideoFrameStreamTest : public testing::TestWithParam<bool> {
31 VideoFrameStreamTest()
32 : demuxer_stream_(new FakeDemuxerStream(kNumConfigs,
33 kNumBuffersInOneConfig,
35 decryptor_(new NiceMock<MockDecryptor>()),
36 decoder_(new FakeVideoDecoder(kDecodingDelay)),
37 is_initialized_(false),
38 num_decoded_frames_(0),
39 pending_initialize_(false),
41 pending_reset_(false),
43 total_bytes_decoded_(0),
45 ScopedVector<VideoDecoder> decoders;
46 decoders.push_back(decoder_);
48 video_frame_stream_.reset(new VideoFrameStream(
49 message_loop_.message_loop_proxy(),
51 base::Bind(&VideoFrameStreamTest::SetDecryptorReadyCallback,
52 base::Unretained(this))));
54 // Decryptor can only decrypt (not decrypt-and-decode) so that
55 // DecryptingDemuxerStream will be used.
56 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
57 .WillRepeatedly(RunCallback<1>(false));
58 EXPECT_CALL(*decryptor_, Decrypt(_, _, _))
59 .WillRepeatedly(Invoke(this, &VideoFrameStreamTest::Decrypt));
62 ~VideoFrameStreamTest() {
63 DCHECK(!pending_initialize_);
64 DCHECK(!pending_read_);
65 DCHECK(!pending_reset_);
66 DCHECK(!pending_stop_);
70 EXPECT_FALSE(is_initialized_);
73 MOCK_METHOD1(SetDecryptorReadyCallback, void(const media::DecryptorReadyCB&));
75 void OnStatistics(const PipelineStatistics& statistics) {
76 total_bytes_decoded_ += statistics.video_bytes_decoded;
79 void OnInitialized(bool success, bool has_alpha) {
80 DCHECK(!pending_read_);
81 DCHECK(!pending_reset_);
82 DCHECK(pending_initialize_);
83 pending_initialize_ = false;
85 is_initialized_ = success;
90 void InitializeVideoFrameStream() {
91 pending_initialize_ = true;
92 video_frame_stream_->Initialize(
93 demuxer_stream_.get(),
94 base::Bind(&VideoFrameStreamTest::OnStatistics, base::Unretained(this)),
95 base::Bind(&VideoFrameStreamTest::OnInitialized,
96 base::Unretained(this)));
97 message_loop_.RunUntilIdle();
100 // Fake Decrypt() function used by DecryptingDemuxerStream. It does nothing
101 // but removes the DecryptConfig to make the buffer unencrypted.
102 void Decrypt(Decryptor::StreamType stream_type,
103 const scoped_refptr<DecoderBuffer>& encrypted,
104 const Decryptor::DecryptCB& decrypt_cb) {
106 decrypt_cb.Run(Decryptor::kNoKey, NULL);
110 DCHECK_EQ(stream_type, Decryptor::kVideo);
111 scoped_refptr<DecoderBuffer> decrypted = DecoderBuffer::CopyFrom(
112 encrypted->data(), encrypted->data_size());
113 decrypted->set_timestamp(encrypted->timestamp());
114 decrypted->set_duration(encrypted->duration());
115 decrypt_cb.Run(Decryptor::kSuccess, decrypted);
118 // Callback for VideoFrameStream::Read().
119 void FrameReady(VideoFrameStream::Status status,
120 const scoped_refptr<VideoFrame>& frame) {
121 DCHECK(pending_read_);
122 // TODO(xhwang): Add test cases where the fake decoder returns error or
123 // the fake demuxer aborts demuxer read.
124 ASSERT_TRUE(status == VideoFrameStream::OK ||
125 status == VideoFrameStream::ABORTED) << status;
127 if (frame.get() && !frame->IsEndOfStream())
128 num_decoded_frames_++;
129 pending_read_ = false;
133 DCHECK(!pending_read_);
134 DCHECK(pending_reset_);
135 pending_reset_ = false;
139 DCHECK(!pending_read_);
140 DCHECK(!pending_reset_);
141 DCHECK(pending_stop_);
142 pending_stop_ = false;
143 is_initialized_ = false;
147 void ReadOneFrame() {
149 pending_read_ = true;
150 video_frame_stream_->Read(base::Bind(
151 &VideoFrameStreamTest::FrameReady, base::Unretained(this)));
152 message_loop_.RunUntilIdle();
155 void ReadUntilPending() {
158 } while (!pending_read_);
164 DEMUXER_READ_CONFIG_CHANGE,
174 void EnterPendingState(PendingState state) {
175 DCHECK_NE(state, NOT_PENDING);
177 case DEMUXER_READ_NORMAL:
178 demuxer_stream_->HoldNextRead();
182 case DEMUXER_READ_CONFIG_CHANGE:
183 demuxer_stream_->HoldNextConfigChangeRead();
188 // Hold DecryptorReadyCB.
189 EXPECT_CALL(*this, SetDecryptorReadyCallback(_))
191 // Initialize will fail because no decryptor is available.
192 InitializeVideoFrameStream();
195 case DECRYPTOR_NO_KEY:
196 EXPECT_CALL(*this, SetDecryptorReadyCallback(_))
197 .WillRepeatedly(RunCallback<0>(decryptor_.get()));
203 EXPECT_CALL(*this, SetDecryptorReadyCallback(_))
204 .WillRepeatedly(RunCallback<0>(decryptor_.get()));
205 decoder_->HoldNextInit();
206 InitializeVideoFrameStream();
210 decoder_->HoldNextInit();
215 decoder_->HoldNextRead();
220 decoder_->HoldNextReset();
221 pending_reset_ = true;
222 video_frame_stream_->Reset(base::Bind(&VideoFrameStreamTest::OnReset,
223 base::Unretained(this)));
224 message_loop_.RunUntilIdle();
228 decoder_->HoldNextStop();
229 // Check that the pipeline statistics callback was fired correctly.
230 EXPECT_EQ(decoder_->total_bytes_decoded(), total_bytes_decoded_);
231 pending_stop_ = true;
232 video_frame_stream_->Stop(base::Bind(&VideoFrameStreamTest::OnStopped,
233 base::Unretained(this)));
234 message_loop_.RunUntilIdle();
243 void SatisfyPendingCallback(PendingState state) {
244 DCHECK_NE(state, NOT_PENDING);
246 case DEMUXER_READ_NORMAL:
247 case DEMUXER_READ_CONFIG_CHANGE:
248 demuxer_stream_->SatisfyRead();
251 // These two cases are only interesting to test during
252 // VideoFrameStream::Stop(). There's no need to satisfy a callback.
254 case DECRYPTOR_NO_KEY:
259 decoder_->SatisfyInit();
263 decoder_->SatisfyInit();
267 decoder_->SatisfyRead();
271 decoder_->SatisfyReset();
275 DCHECK(pending_stop_);
276 decoder_->SatisfyStop();
284 message_loop_.RunUntilIdle();
288 EnterPendingState(DECODER_INIT);
289 SatisfyPendingCallback(DECODER_INIT);
293 EnterPendingState(DECODER_READ);
294 SatisfyPendingCallback(DECODER_READ);
298 EnterPendingState(DECODER_RESET);
299 SatisfyPendingCallback(DECODER_RESET);
303 EnterPendingState(DECODER_STOP);
304 SatisfyPendingCallback(DECODER_STOP);
307 base::MessageLoop message_loop_;
309 scoped_ptr<VideoFrameStream> video_frame_stream_;
310 scoped_ptr<FakeDemuxerStream> demuxer_stream_;
311 // Use NiceMock since we don't care about most of calls on the decryptor,
312 // e.g. RegisterNewKeyCB().
313 scoped_ptr<NiceMock<MockDecryptor> > decryptor_;
314 FakeVideoDecoder* decoder_; // Owned by |video_frame_stream_|.
316 bool is_initialized_;
317 int num_decoded_frames_;
318 bool pending_initialize_;
322 int total_bytes_decoded_;
323 scoped_refptr<VideoFrame> frame_read_;
325 // Decryptor has no key to decrypt a frame.
329 DISALLOW_COPY_AND_ASSIGN(VideoFrameStreamTest);
332 INSTANTIATE_TEST_CASE_P(Clear, VideoFrameStreamTest, testing::Values(false));
333 INSTANTIATE_TEST_CASE_P(Encrypted, VideoFrameStreamTest, testing::Values(true));
335 TEST_P(VideoFrameStreamTest, Initialization) {
339 TEST_P(VideoFrameStreamTest, ReadOneFrame) {
344 TEST_P(VideoFrameStreamTest, ReadAllFrames) {
348 } while (frame_read_.get() && !frame_read_->IsEndOfStream());
350 const int total_num_frames = kNumConfigs * kNumBuffersInOneConfig;
351 DCHECK_EQ(num_decoded_frames_, total_num_frames);
354 TEST_P(VideoFrameStreamTest, Read_AfterReset) {
362 // No Reset() before initialization is successfully completed.
364 TEST_P(VideoFrameStreamTest, Reset_AfterInitialization) {
370 TEST_P(VideoFrameStreamTest, Reset_DuringReinitialization) {
372 EnterPendingState(DECODER_REINIT);
373 // VideoDecoder::Reset() is not called when we reset during reinitialization.
374 pending_reset_ = true;
375 video_frame_stream_->Reset(
376 base::Bind(&VideoFrameStreamTest::OnReset, base::Unretained(this)));
377 SatisfyPendingCallback(DECODER_REINIT);
381 TEST_P(VideoFrameStreamTest, Reset_AfterReinitialization) {
383 EnterPendingState(DECODER_REINIT);
384 SatisfyPendingCallback(DECODER_REINIT);
389 TEST_P(VideoFrameStreamTest, Reset_DuringDemuxerRead_Normal) {
391 EnterPendingState(DEMUXER_READ_NORMAL);
392 EnterPendingState(DECODER_RESET);
393 SatisfyPendingCallback(DEMUXER_READ_NORMAL);
394 SatisfyPendingCallback(DECODER_RESET);
398 TEST_P(VideoFrameStreamTest, Reset_DuringDemuxerRead_ConfigChange) {
400 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
401 EnterPendingState(DECODER_RESET);
402 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
403 SatisfyPendingCallback(DECODER_RESET);
407 TEST_P(VideoFrameStreamTest, Reset_DuringNormalDecoderRead) {
409 EnterPendingState(DECODER_READ);
410 EnterPendingState(DECODER_RESET);
411 SatisfyPendingCallback(DECODER_READ);
412 SatisfyPendingCallback(DECODER_RESET);
416 TEST_P(VideoFrameStreamTest, Reset_AfterNormalRead) {
423 TEST_P(VideoFrameStreamTest, Reset_AfterDemuxerRead_ConfigChange) {
425 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
426 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
431 TEST_P(VideoFrameStreamTest, Reset_DuringNoKeyRead) {
433 EnterPendingState(DECRYPTOR_NO_KEY);
437 TEST_P(VideoFrameStreamTest, Stop_BeforeInitialization) {
438 pending_stop_ = true;
439 video_frame_stream_->Stop(
440 base::Bind(&VideoFrameStreamTest::OnStopped, base::Unretained(this)));
441 message_loop_.RunUntilIdle();
444 TEST_P(VideoFrameStreamTest, Stop_DuringSetDecryptor) {
446 DVLOG(1) << "SetDecryptor test only runs when the stream is encrytped.";
450 EnterPendingState(SET_DECRYPTOR);
451 pending_stop_ = true;
452 video_frame_stream_->Stop(
453 base::Bind(&VideoFrameStreamTest::OnStopped, base::Unretained(this)));
454 message_loop_.RunUntilIdle();
457 TEST_P(VideoFrameStreamTest, Stop_DuringInitialization) {
458 EnterPendingState(DECODER_INIT);
459 EnterPendingState(DECODER_STOP);
460 SatisfyPendingCallback(DECODER_INIT);
461 SatisfyPendingCallback(DECODER_STOP);
464 TEST_P(VideoFrameStreamTest, Stop_AfterInitialization) {
469 TEST_P(VideoFrameStreamTest, Stop_DuringReinitialization) {
471 EnterPendingState(DECODER_REINIT);
472 EnterPendingState(DECODER_STOP);
473 SatisfyPendingCallback(DECODER_REINIT);
474 SatisfyPendingCallback(DECODER_STOP);
477 TEST_P(VideoFrameStreamTest, Stop_AfterReinitialization) {
479 EnterPendingState(DECODER_REINIT);
480 SatisfyPendingCallback(DECODER_REINIT);
484 TEST_P(VideoFrameStreamTest, Stop_DuringDemuxerRead_Normal) {
486 EnterPendingState(DEMUXER_READ_NORMAL);
487 EnterPendingState(DECODER_STOP);
488 SatisfyPendingCallback(DEMUXER_READ_NORMAL);
489 SatisfyPendingCallback(DECODER_STOP);
492 TEST_P(VideoFrameStreamTest, Stop_DuringDemuxerRead_ConfigChange) {
494 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
495 EnterPendingState(DECODER_STOP);
496 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
497 SatisfyPendingCallback(DECODER_STOP);
500 TEST_P(VideoFrameStreamTest, Stop_DuringNormalDecoderRead) {
502 EnterPendingState(DECODER_READ);
503 EnterPendingState(DECODER_STOP);
504 SatisfyPendingCallback(DECODER_READ);
505 SatisfyPendingCallback(DECODER_STOP);
508 TEST_P(VideoFrameStreamTest, Stop_AfterNormalRead) {
514 TEST_P(VideoFrameStreamTest, Stop_AfterConfigChangeRead) {
516 EnterPendingState(DEMUXER_READ_CONFIG_CHANGE);
517 SatisfyPendingCallback(DEMUXER_READ_CONFIG_CHANGE);
521 TEST_P(VideoFrameStreamTest, Stop_DuringNoKeyRead) {
523 EnterPendingState(DECRYPTOR_NO_KEY);
527 TEST_P(VideoFrameStreamTest, Stop_DuringReset) {
529 EnterPendingState(DECODER_RESET);
530 EnterPendingState(DECODER_STOP);
531 SatisfyPendingCallback(DECODER_RESET);
532 SatisfyPendingCallback(DECODER_STOP);
535 TEST_P(VideoFrameStreamTest, Stop_AfterReset) {
541 TEST_P(VideoFrameStreamTest, Stop_DuringRead_DuringReset) {
543 EnterPendingState(DECODER_READ);
544 EnterPendingState(DECODER_RESET);
545 EnterPendingState(DECODER_STOP);
546 SatisfyPendingCallback(DECODER_READ);
547 SatisfyPendingCallback(DECODER_RESET);
548 SatisfyPendingCallback(DECODER_STOP);
551 TEST_P(VideoFrameStreamTest, Stop_AfterRead_DuringReset) {
553 EnterPendingState(DECODER_READ);
554 EnterPendingState(DECODER_RESET);
555 SatisfyPendingCallback(DECODER_READ);
556 EnterPendingState(DECODER_STOP);
557 SatisfyPendingCallback(DECODER_RESET);
558 SatisfyPendingCallback(DECODER_STOP);
561 TEST_P(VideoFrameStreamTest, Stop_AfterRead_AfterReset) {