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.
9 #include "base/callback_helpers.h"
10 #include "base/message_loop.h"
11 #include "media/base/decoder_buffer.h"
12 #include "media/base/decrypt_config.h"
13 #include "media/base/gmock_callback_support.h"
14 #include "media/base/mock_filters.h"
15 #include "media/base/test_helpers.h"
16 #include "media/base/video_frame.h"
17 #include "media/filters/decrypting_video_decoder.h"
18 #include "testing/gmock/include/gmock/gmock.h"
21 using ::testing::AtMost
;
22 using ::testing::IsNull
;
23 using ::testing::ReturnRef
;
24 using ::testing::SaveArg
;
25 using ::testing::StrictMock
;
29 static const uint8 kFakeKeyId
[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 };
30 static const uint8 kFakeIv
[DecryptConfig::kDecryptionKeySize
] = { 0 };
32 // Create a fake non-empty encrypted buffer.
33 static scoped_refptr
<DecoderBuffer
> CreateFakeEncryptedBuffer() {
34 const int buffer_size
= 16; // Need a non-empty buffer;
35 scoped_refptr
<DecoderBuffer
> buffer(new DecoderBuffer(buffer_size
));
36 buffer
->SetDecryptConfig(scoped_ptr
<DecryptConfig
>(new DecryptConfig(
37 std::string(reinterpret_cast<const char*>(kFakeKeyId
),
38 arraysize(kFakeKeyId
)),
39 std::string(reinterpret_cast<const char*>(kFakeIv
), arraysize(kFakeIv
)),
41 std::vector
<SubsampleEntry
>())));
45 // Use anonymous namespace here to prevent the actions to be defined multiple
46 // times across multiple test files. Sadly we can't use static for them.
49 ACTION_P(ReturnBuffer
, buffer
) {
50 arg0
.Run(buffer
.get() ? DemuxerStream::kOk
: DemuxerStream::kAborted
, buffer
);
53 ACTION_P(RunCallbackIfNotNull
, param
) {
58 ACTION_P2(ResetAndRunCallback
, callback
, param
) {
59 base::ResetAndReturn(callback
).Run(param
);
62 MATCHER(IsEndOfStream
, "end of stream") {
63 return (arg
->IsEndOfStream());
68 class DecryptingVideoDecoderTest
: public testing::Test
{
70 DecryptingVideoDecoderTest()
71 : decoder_(new DecryptingVideoDecoder(
72 message_loop_
.message_loop_proxy(),
74 &DecryptingVideoDecoderTest::RequestDecryptorNotification
,
75 base::Unretained(this)))),
76 decryptor_(new StrictMock
<MockDecryptor
>()),
77 demuxer_(new StrictMock
<MockDemuxerStream
>(DemuxerStream::VIDEO
)),
78 encrypted_buffer_(CreateFakeEncryptedBuffer()),
79 decoded_video_frame_(VideoFrame::CreateBlackFrame(
80 TestVideoConfig::NormalCodedSize())),
81 null_video_frame_(scoped_refptr
<VideoFrame
>()),
82 end_of_stream_video_frame_(VideoFrame::CreateEmptyFrame()) {
83 EXPECT_CALL(*this, RequestDecryptorNotification(_
))
84 .WillRepeatedly(RunCallbackIfNotNull(decryptor_
.get()));
87 virtual ~DecryptingVideoDecoderTest() {
91 void InitializeAndExpectStatus(const VideoDecoderConfig
& config
,
92 PipelineStatus status
) {
93 demuxer_
->set_video_decoder_config(config
);
94 decoder_
->Initialize(demuxer_
.get(), NewExpectedStatusCB(status
),
95 base::Bind(&MockStatisticsCB::OnStatistics
,
96 base::Unretained(&statistics_cb_
)));
97 message_loop_
.RunUntilIdle();
101 EXPECT_CALL(*decryptor_
, InitializeVideoDecoder(_
, _
))
102 .WillRepeatedly(RunCallback
<1>(true));
103 EXPECT_CALL(*decryptor_
, RegisterNewKeyCB(Decryptor::kVideo
, _
))
104 .WillRepeatedly(SaveArg
<1>(&key_added_cb_
));
106 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), PIPELINE_OK
);
109 void Reinitialize() {
110 EXPECT_CALL(*decryptor_
, DeinitializeDecoder(Decryptor::kVideo
));
111 InitializeAndExpectStatus(TestVideoConfig::LargeEncrypted(), PIPELINE_OK
);
114 void ReadAndExpectFrameReadyWith(
115 VideoDecoder::Status status
,
116 const scoped_refptr
<VideoFrame
>& video_frame
) {
117 if (status
!= VideoDecoder::kOk
)
118 EXPECT_CALL(*this, FrameReady(status
, IsNull()));
119 else if (video_frame
.get() && video_frame
->IsEndOfStream())
120 EXPECT_CALL(*this, FrameReady(status
, IsEndOfStream()));
122 EXPECT_CALL(*this, FrameReady(status
, video_frame
));
124 decoder_
->Read(base::Bind(&DecryptingVideoDecoderTest::FrameReady
,
125 base::Unretained(this)));
126 message_loop_
.RunUntilIdle();
129 // Sets up expectations and actions to put DecryptingVideoDecoder in an
130 // active normal decoding state.
131 void EnterNormalDecodingState() {
132 EXPECT_CALL(*demuxer_
, Read(_
))
133 .WillOnce(ReturnBuffer(encrypted_buffer_
))
134 .WillRepeatedly(ReturnBuffer(DecoderBuffer::CreateEOSBuffer()));
135 EXPECT_CALL(*decryptor_
, DecryptAndDecodeVideo(_
, _
))
136 .WillOnce(RunCallback
<1>(Decryptor::kSuccess
, decoded_video_frame_
))
137 .WillRepeatedly(RunCallback
<1>(Decryptor::kNeedMoreData
,
138 scoped_refptr
<VideoFrame
>()));
139 EXPECT_CALL(statistics_cb_
, OnStatistics(_
));
141 ReadAndExpectFrameReadyWith(VideoDecoder::kOk
, decoded_video_frame_
);
144 // Sets up expectations and actions to put DecryptingVideoDecoder in an end
145 // of stream state. This function must be called after
146 // EnterNormalDecodingState() to work.
147 void EnterEndOfStreamState() {
148 ReadAndExpectFrameReadyWith(VideoDecoder::kOk
, end_of_stream_video_frame_
);
151 // Make the read callback pending by saving and not firing it.
152 void EnterPendingReadState() {
153 EXPECT_TRUE(pending_demuxer_read_cb_
.is_null());
154 EXPECT_CALL(*demuxer_
, Read(_
))
155 .WillOnce(SaveArg
<0>(&pending_demuxer_read_cb_
));
156 decoder_
->Read(base::Bind(&DecryptingVideoDecoderTest::FrameReady
,
157 base::Unretained(this)));
158 message_loop_
.RunUntilIdle();
159 // Make sure the Read() on the decoder triggers a Read() on the demuxer.
160 EXPECT_FALSE(pending_demuxer_read_cb_
.is_null());
163 // Make the video decode callback pending by saving and not firing it.
164 void EnterPendingDecodeState() {
165 EXPECT_TRUE(pending_video_decode_cb_
.is_null());
166 EXPECT_CALL(*demuxer_
, Read(_
))
167 .WillRepeatedly(ReturnBuffer(encrypted_buffer_
));
168 EXPECT_CALL(*decryptor_
, DecryptAndDecodeVideo(encrypted_buffer_
, _
))
169 .WillOnce(SaveArg
<1>(&pending_video_decode_cb_
));
171 decoder_
->Read(base::Bind(&DecryptingVideoDecoderTest::FrameReady
,
172 base::Unretained(this)));
173 message_loop_
.RunUntilIdle();
174 // Make sure the Read() on the decoder triggers a DecryptAndDecode() on the
176 EXPECT_FALSE(pending_video_decode_cb_
.is_null());
179 void EnterWaitingForKeyState() {
180 EXPECT_CALL(*demuxer_
, Read(_
))
181 .WillRepeatedly(ReturnBuffer(encrypted_buffer_
));
182 EXPECT_CALL(*decryptor_
, DecryptAndDecodeVideo(_
, _
))
183 .WillRepeatedly(RunCallback
<1>(Decryptor::kNoKey
, null_video_frame_
));
184 decoder_
->Read(base::Bind(&DecryptingVideoDecoderTest::FrameReady
,
185 base::Unretained(this)));
186 message_loop_
.RunUntilIdle();
189 void AbortPendingVideoDecodeCB() {
190 if (!pending_video_decode_cb_
.is_null()) {
191 base::ResetAndReturn(&pending_video_decode_cb_
).Run(
192 Decryptor::kSuccess
, scoped_refptr
<VideoFrame
>(NULL
));
196 void AbortAllPendingCBs() {
197 if (!pending_init_cb_
.is_null()) {
198 ASSERT_TRUE(pending_video_decode_cb_
.is_null());
199 base::ResetAndReturn(&pending_init_cb_
).Run(false);
203 AbortPendingVideoDecodeCB();
207 EXPECT_CALL(*decryptor_
, ResetDecoder(Decryptor::kVideo
))
208 .WillRepeatedly(InvokeWithoutArgs(
209 this, &DecryptingVideoDecoderTest::AbortPendingVideoDecodeCB
));
211 decoder_
->Reset(NewExpectedClosure());
212 message_loop_
.RunUntilIdle();
216 EXPECT_CALL(*decryptor_
, RegisterNewKeyCB(Decryptor::kVideo
,
219 EXPECT_CALL(*decryptor_
, DeinitializeDecoder(Decryptor::kVideo
))
220 .WillRepeatedly(InvokeWithoutArgs(
221 this, &DecryptingVideoDecoderTest::AbortAllPendingCBs
));
223 decoder_
->Stop(NewExpectedClosure());
224 message_loop_
.RunUntilIdle();
227 MOCK_METHOD1(RequestDecryptorNotification
, void(const DecryptorReadyCB
&));
229 MOCK_METHOD2(FrameReady
, void(VideoDecoder::Status
,
230 const scoped_refptr
<VideoFrame
>&));
232 base::MessageLoop message_loop_
;
233 scoped_ptr
<DecryptingVideoDecoder
> decoder_
;
234 scoped_ptr
<StrictMock
<MockDecryptor
> > decryptor_
;
235 scoped_ptr
<StrictMock
<MockDemuxerStream
> > demuxer_
;
236 MockStatisticsCB statistics_cb_
;
238 DemuxerStream::ReadCB pending_demuxer_read_cb_
;
239 Decryptor::DecoderInitCB pending_init_cb_
;
240 Decryptor::NewKeyCB key_added_cb_
;
241 Decryptor::VideoDecodeCB pending_video_decode_cb_
;
243 // Constant buffer/frames to be returned by the |demuxer_| and |decryptor_|.
244 scoped_refptr
<DecoderBuffer
> encrypted_buffer_
;
245 scoped_refptr
<VideoFrame
> decoded_video_frame_
;
246 scoped_refptr
<VideoFrame
> null_video_frame_
;
247 scoped_refptr
<VideoFrame
> end_of_stream_video_frame_
;
250 DISALLOW_COPY_AND_ASSIGN(DecryptingVideoDecoderTest
);
253 TEST_F(DecryptingVideoDecoderTest
, Initialize_Normal
) {
257 TEST_F(DecryptingVideoDecoderTest
, Initialize_NullDecryptor
) {
258 EXPECT_CALL(*this, RequestDecryptorNotification(_
))
259 .WillRepeatedly(RunCallbackIfNotNull(static_cast<Decryptor
*>(NULL
)));
260 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
261 DECODER_ERROR_NOT_SUPPORTED
);
264 TEST_F(DecryptingVideoDecoderTest
, Initialize_Failure
) {
265 EXPECT_CALL(*decryptor_
, InitializeVideoDecoder(_
, _
))
266 .WillRepeatedly(RunCallback
<1>(false));
267 EXPECT_CALL(*decryptor_
, RegisterNewKeyCB(Decryptor::kVideo
, _
))
268 .WillRepeatedly(SaveArg
<1>(&key_added_cb_
));
270 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
271 DECODER_ERROR_NOT_SUPPORTED
);
274 TEST_F(DecryptingVideoDecoderTest
, Reinitialize_Normal
) {
276 EnterNormalDecodingState();
280 TEST_F(DecryptingVideoDecoderTest
, Reinitialize_Failure
) {
282 EnterNormalDecodingState();
284 EXPECT_CALL(*decryptor_
, DeinitializeDecoder(Decryptor::kVideo
));
285 EXPECT_CALL(*decryptor_
, InitializeVideoDecoder(_
, _
))
286 .WillOnce(RunCallback
<1>(false));
288 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
289 DECODER_ERROR_NOT_SUPPORTED
);
292 // Test normal decrypt and decode case.
293 TEST_F(DecryptingVideoDecoderTest
, DecryptAndDecode_Normal
) {
295 EnterNormalDecodingState();
298 // Test the case where the decryptor returns error when doing decrypt and
300 TEST_F(DecryptingVideoDecoderTest
, DecryptAndDecode_DecodeError
) {
303 EXPECT_CALL(*demuxer_
, Read(_
))
304 .WillRepeatedly(ReturnBuffer(encrypted_buffer_
));
305 EXPECT_CALL(*decryptor_
, DecryptAndDecodeVideo(_
, _
))
306 .WillRepeatedly(RunCallback
<1>(Decryptor::kError
,
307 scoped_refptr
<VideoFrame
>(NULL
)));
309 ReadAndExpectFrameReadyWith(VideoDecoder::kDecodeError
, null_video_frame_
);
311 // After a decode error occurred, all following read returns kDecodeError.
312 ReadAndExpectFrameReadyWith(VideoDecoder::kDecodeError
, null_video_frame_
);
315 // Test the case where the decryptor returns kNeedMoreData to ask for more
316 // buffers before it can produce a frame.
317 TEST_F(DecryptingVideoDecoderTest
, DecryptAndDecode_NeedMoreData
) {
320 EXPECT_CALL(*demuxer_
, Read(_
))
322 .WillRepeatedly(ReturnBuffer(encrypted_buffer_
));
323 EXPECT_CALL(*decryptor_
, DecryptAndDecodeVideo(_
, _
))
324 .WillOnce(RunCallback
<1>(Decryptor::kNeedMoreData
,
325 scoped_refptr
<VideoFrame
>()))
326 .WillRepeatedly(RunCallback
<1>(Decryptor::kSuccess
,
327 decoded_video_frame_
));
328 EXPECT_CALL(statistics_cb_
, OnStatistics(_
))
331 ReadAndExpectFrameReadyWith(VideoDecoder::kOk
, decoded_video_frame_
);
334 // Test the case where the decryptor receives end-of-stream buffer.
335 TEST_F(DecryptingVideoDecoderTest
, DecryptAndDecode_EndOfStream
) {
337 EnterNormalDecodingState();
338 EnterEndOfStreamState();
341 // Test aborted read on the demuxer stream.
342 TEST_F(DecryptingVideoDecoderTest
, DemuxerRead_Aborted
) {
345 // ReturnBuffer() with NULL triggers aborted demuxer read.
346 EXPECT_CALL(*demuxer_
, Read(_
))
347 .WillOnce(ReturnBuffer(scoped_refptr
<DecoderBuffer
>()));
349 ReadAndExpectFrameReadyWith(VideoDecoder::kOk
, null_video_frame_
);
352 // Test the case where the a key is added when the decryptor is in
353 // kWaitingForKey state.
354 TEST_F(DecryptingVideoDecoderTest
, KeyAdded_DuringWaitingForKey
) {
356 EnterWaitingForKeyState();
358 EXPECT_CALL(*decryptor_
, DecryptAndDecodeVideo(_
, _
))
359 .WillRepeatedly(RunCallback
<1>(Decryptor::kSuccess
,
360 decoded_video_frame_
));
361 EXPECT_CALL(statistics_cb_
, OnStatistics(_
));
362 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, decoded_video_frame_
));
364 message_loop_
.RunUntilIdle();
367 // Test the case where the a key is added when the decryptor is in
368 // kPendingDecode state.
369 TEST_F(DecryptingVideoDecoderTest
, KeyAdded_DruingPendingDecode
) {
371 EnterPendingDecodeState();
373 EXPECT_CALL(*decryptor_
, DecryptAndDecodeVideo(_
, _
))
374 .WillRepeatedly(RunCallback
<1>(Decryptor::kSuccess
,
375 decoded_video_frame_
));
376 EXPECT_CALL(statistics_cb_
, OnStatistics(_
));
377 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, decoded_video_frame_
));
378 // The video decode callback is returned after the correct decryption key is
381 base::ResetAndReturn(&pending_video_decode_cb_
).Run(Decryptor::kNoKey
,
383 message_loop_
.RunUntilIdle();
386 // Test resetting when the decoder is in kIdle state but has not decoded any
388 TEST_F(DecryptingVideoDecoderTest
, Reset_DuringIdleAfterInitialization
) {
393 // Test resetting when the decoder is in kIdle state after it has decoded one
395 TEST_F(DecryptingVideoDecoderTest
, Reset_DuringIdleAfterDecodedOneFrame
) {
397 EnterNormalDecodingState();
401 // Test resetting when the decoder is in kPendingDemuxerRead state and the read
402 // callback is returned with kOk.
403 TEST_F(DecryptingVideoDecoderTest
, Reset_DuringDemuxerRead_Ok
) {
405 EnterPendingReadState();
407 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
410 base::ResetAndReturn(&pending_demuxer_read_cb_
).Run(DemuxerStream::kOk
,
412 message_loop_
.RunUntilIdle();
415 // Test resetting when the decoder is in kPendingDemuxerRead state and the read
416 // callback is returned with kAborted.
417 TEST_F(DecryptingVideoDecoderTest
, Reset_DuringDemuxerRead_Aborted
) {
419 EnterPendingReadState();
421 // Make sure we get a NULL video frame returned.
422 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
425 base::ResetAndReturn(&pending_demuxer_read_cb_
).Run(DemuxerStream::kAborted
,
427 message_loop_
.RunUntilIdle();
430 // Test resetting when the decoder is in kPendingDecode state.
431 TEST_F(DecryptingVideoDecoderTest
, Reset_DuringPendingDecode
) {
433 EnterPendingDecodeState();
435 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
440 // Test resetting when the decoder is in kWaitingForKey state.
441 TEST_F(DecryptingVideoDecoderTest
, Reset_DuringWaitingForKey
) {
443 EnterWaitingForKeyState();
445 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
450 // Test resetting when the decoder has hit end of stream and is in
451 // kDecodeFinished state.
452 TEST_F(DecryptingVideoDecoderTest
, Reset_AfterDecodeFinished
) {
454 EnterNormalDecodingState();
455 EnterEndOfStreamState();
459 // Test resetting after the decoder has been reset.
460 TEST_F(DecryptingVideoDecoderTest
, Reset_AfterReset
) {
462 EnterNormalDecodingState();
467 // Test stopping when the decoder is in kDecryptorRequested state.
468 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringDecryptorRequested
) {
469 demuxer_
->set_video_decoder_config(TestVideoConfig::NormalEncrypted());
470 DecryptorReadyCB decryptor_ready_cb
;
471 EXPECT_CALL(*this, RequestDecryptorNotification(_
))
472 .WillOnce(SaveArg
<0>(&decryptor_ready_cb
));
473 decoder_
->Initialize(demuxer_
.get(),
474 NewExpectedStatusCB(DECODER_ERROR_NOT_SUPPORTED
),
475 base::Bind(&MockStatisticsCB::OnStatistics
,
476 base::Unretained(&statistics_cb_
)));
477 message_loop_
.RunUntilIdle();
478 // |decryptor_ready_cb| is saved but not called here.
479 EXPECT_FALSE(decryptor_ready_cb
.is_null());
481 // During stop, RequestDecryptorNotification() should be called with a NULL
482 // callback to cancel the |decryptor_ready_cb|.
483 EXPECT_CALL(*this, RequestDecryptorNotification(IsNullCallback()))
484 .WillOnce(ResetAndRunCallback(&decryptor_ready_cb
,
485 reinterpret_cast<Decryptor
*>(NULL
)));
489 // Test stopping when the decoder is in kPendingDecoderInit state.
490 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringPendingDecoderInit
) {
491 EXPECT_CALL(*decryptor_
, InitializeVideoDecoder(_
, _
))
492 .WillOnce(SaveArg
<1>(&pending_init_cb_
));
494 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
495 DECODER_ERROR_NOT_SUPPORTED
);
496 EXPECT_FALSE(pending_init_cb_
.is_null());
501 // Test stopping when the decoder is in kIdle state but has not decoded any
503 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringIdleAfterInitialization
) {
508 // Test stopping when the decoder is in kIdle state after it has decoded one
510 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringIdleAfterDecodedOneFrame
) {
512 EnterNormalDecodingState();
516 // Test stopping when the decoder is in kPendingDemuxerRead state.
517 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringPendingDemuxerRead
) {
519 EnterPendingReadState();
521 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
524 base::ResetAndReturn(&pending_demuxer_read_cb_
).Run(DemuxerStream::kOk
,
526 message_loop_
.RunUntilIdle();
529 // Test stopping when the decoder is in kPendingDecode state.
530 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringPendingDecode
) {
532 EnterPendingDecodeState();
534 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
539 // Test stopping when the decoder is in kWaitingForKey state.
540 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringWaitingForKey
) {
542 EnterWaitingForKeyState();
544 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
549 // Test stopping when the decoder has hit end of stream and is in
550 // kDecodeFinished state.
551 TEST_F(DecryptingVideoDecoderTest
, Stop_AfterDecodeFinished
) {
553 EnterNormalDecodingState();
554 EnterEndOfStreamState();
558 // Test stopping when there is a pending reset on the decoder.
559 // Reset is pending because it cannot complete when the video decode callback
561 TEST_F(DecryptingVideoDecoderTest
, Stop_DuringPendingReset
) {
563 EnterPendingDecodeState();
565 EXPECT_CALL(*decryptor_
, ResetDecoder(Decryptor::kVideo
));
566 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk
, IsNull()));
568 decoder_
->Reset(NewExpectedClosure());
572 // Test stopping after the decoder has been reset.
573 TEST_F(DecryptingVideoDecoderTest
, Stop_AfterReset
) {
575 EnterNormalDecodingState();
580 // Test stopping after the decoder has been stopped.
581 TEST_F(DecryptingVideoDecoderTest
, Stop_AfterStop
) {
583 EnterNormalDecodingState();