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/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->set_decrypt_config(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)),
40 std::vector<SubsampleEntry>())));
44 // Use anonymous namespace here to prevent the actions to be defined multiple
45 // times across multiple test files. Sadly we can't use static for them.
48 ACTION_P(RunCallbackIfNotNull, param) {
53 ACTION_P2(ResetAndRunCallback, callback, param) {
54 base::ResetAndReturn(callback).Run(param);
57 MATCHER(IsEndOfStream, "end of stream") {
58 return (arg->end_of_stream());
63 class DecryptingVideoDecoderTest : public testing::Test {
65 DecryptingVideoDecoderTest()
66 : decoder_(new DecryptingVideoDecoder(
67 message_loop_.message_loop_proxy(),
69 &DecryptingVideoDecoderTest::RequestDecryptorNotification,
70 base::Unretained(this)))),
71 decryptor_(new StrictMock<MockDecryptor>()),
72 encrypted_buffer_(CreateFakeEncryptedBuffer()),
73 decoded_video_frame_(VideoFrame::CreateBlackFrame(
74 TestVideoConfig::NormalCodedSize())),
75 null_video_frame_(scoped_refptr<VideoFrame>()),
76 end_of_stream_video_frame_(VideoFrame::CreateEOSFrame()) {
77 EXPECT_CALL(*this, RequestDecryptorNotification(_))
78 .WillRepeatedly(RunCallbackIfNotNull(decryptor_.get()));
81 virtual ~DecryptingVideoDecoderTest() {
85 // Initializes the |decoder_| and expects |status|. Note the initialization
86 // can succeed or fail.
87 void InitializeAndExpectStatus(const VideoDecoderConfig& config,
88 PipelineStatus status) {
89 decoder_->Initialize(config, NewExpectedStatusCB(status));
90 message_loop_.RunUntilIdle();
93 // Initialize the |decoder_| and expects it to succeed.
95 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
96 .WillOnce(RunCallback<1>(true));
97 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _))
98 .WillOnce(SaveArg<1>(&key_added_cb_));
100 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), PIPELINE_OK);
103 // Reinitialize the |decoder_| and expects it to succeed.
104 void Reinitialize() {
105 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kVideo));
106 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
107 .WillOnce(RunCallback<1>(true));
108 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _))
109 .WillOnce(SaveArg<1>(&key_added_cb_));
111 InitializeAndExpectStatus(TestVideoConfig::LargeEncrypted(), PIPELINE_OK);
114 void ReadAndExpectFrameReadyWith(
115 const scoped_refptr<DecoderBuffer>& buffer,
116 VideoDecoder::Status status,
117 const scoped_refptr<VideoFrame>& video_frame) {
118 if (status != VideoDecoder::kOk)
119 EXPECT_CALL(*this, FrameReady(status, IsNull()));
120 else if (video_frame.get() && video_frame->end_of_stream())
121 EXPECT_CALL(*this, FrameReady(status, IsEndOfStream()));
123 EXPECT_CALL(*this, FrameReady(status, video_frame));
125 decoder_->Decode(buffer,
126 base::Bind(&DecryptingVideoDecoderTest::FrameReady,
127 base::Unretained(this)));
128 message_loop_.RunUntilIdle();
131 // Sets up expectations and actions to put DecryptingVideoDecoder in an
132 // active normal decoding state.
133 void EnterNormalDecodingState() {
134 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _))
135 .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_video_frame_))
136 .WillRepeatedly(RunCallback<1>(Decryptor::kNeedMoreData,
137 scoped_refptr<VideoFrame>()));
138 ReadAndExpectFrameReadyWith(
139 encrypted_buffer_, VideoDecoder::kOk, decoded_video_frame_);
142 // Sets up expectations and actions to put DecryptingVideoDecoder in an end
143 // of stream state. This function must be called after
144 // EnterNormalDecodingState() to work.
145 void EnterEndOfStreamState() {
146 ReadAndExpectFrameReadyWith(DecoderBuffer::CreateEOSBuffer(),
148 end_of_stream_video_frame_);
151 // Make the video decode callback pending by saving and not firing it.
152 void EnterPendingDecodeState() {
153 EXPECT_TRUE(pending_video_decode_cb_.is_null());
154 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(encrypted_buffer_, _))
155 .WillOnce(SaveArg<1>(&pending_video_decode_cb_));
157 decoder_->Decode(encrypted_buffer_,
158 base::Bind(&DecryptingVideoDecoderTest::FrameReady,
159 base::Unretained(this)));
160 message_loop_.RunUntilIdle();
161 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on
163 EXPECT_FALSE(pending_video_decode_cb_.is_null());
166 void EnterWaitingForKeyState() {
167 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _))
168 .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey, null_video_frame_));
169 decoder_->Decode(encrypted_buffer_,
170 base::Bind(&DecryptingVideoDecoderTest::FrameReady,
171 base::Unretained(this)));
172 message_loop_.RunUntilIdle();
175 void AbortPendingVideoDecodeCB() {
176 if (!pending_video_decode_cb_.is_null()) {
177 base::ResetAndReturn(&pending_video_decode_cb_).Run(
178 Decryptor::kSuccess, scoped_refptr<VideoFrame>(NULL));
182 void AbortAllPendingCBs() {
183 if (!pending_init_cb_.is_null()) {
184 ASSERT_TRUE(pending_video_decode_cb_.is_null());
185 base::ResetAndReturn(&pending_init_cb_).Run(false);
189 AbortPendingVideoDecodeCB();
193 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kVideo))
194 .WillRepeatedly(InvokeWithoutArgs(
195 this, &DecryptingVideoDecoderTest::AbortPendingVideoDecodeCB));
197 decoder_->Reset(NewExpectedClosure());
198 message_loop_.RunUntilIdle();
202 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kVideo))
203 .WillRepeatedly(InvokeWithoutArgs(
204 this, &DecryptingVideoDecoderTest::AbortAllPendingCBs));
206 decoder_->Stop(NewExpectedClosure());
207 message_loop_.RunUntilIdle();
210 MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&));
212 MOCK_METHOD2(FrameReady, void(VideoDecoder::Status,
213 const scoped_refptr<VideoFrame>&));
215 base::MessageLoop message_loop_;
216 scoped_ptr<DecryptingVideoDecoder> decoder_;
217 scoped_ptr<StrictMock<MockDecryptor> > decryptor_;
219 Decryptor::DecoderInitCB pending_init_cb_;
220 Decryptor::NewKeyCB key_added_cb_;
221 Decryptor::VideoDecodeCB pending_video_decode_cb_;
223 // Constant buffer/frames.
224 scoped_refptr<DecoderBuffer> encrypted_buffer_;
225 scoped_refptr<VideoFrame> decoded_video_frame_;
226 scoped_refptr<VideoFrame> null_video_frame_;
227 scoped_refptr<VideoFrame> end_of_stream_video_frame_;
230 DISALLOW_COPY_AND_ASSIGN(DecryptingVideoDecoderTest);
233 TEST_F(DecryptingVideoDecoderTest, Initialize_Normal) {
237 TEST_F(DecryptingVideoDecoderTest, Initialize_NullDecryptor) {
238 EXPECT_CALL(*this, RequestDecryptorNotification(_))
239 .WillRepeatedly(RunCallbackIfNotNull(static_cast<Decryptor*>(NULL)));
240 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
241 DECODER_ERROR_NOT_SUPPORTED);
244 TEST_F(DecryptingVideoDecoderTest, Initialize_Failure) {
245 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
246 .WillRepeatedly(RunCallback<1>(false));
247 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _))
248 .WillRepeatedly(SaveArg<1>(&key_added_cb_));
250 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
251 DECODER_ERROR_NOT_SUPPORTED);
254 TEST_F(DecryptingVideoDecoderTest, Reinitialize_Normal) {
256 EnterNormalDecodingState();
260 TEST_F(DecryptingVideoDecoderTest, Reinitialize_Failure) {
262 EnterNormalDecodingState();
264 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kVideo));
265 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
266 .WillOnce(RunCallback<1>(false));
268 // Reinitialize() expects the reinitialization to succeed. Call
269 // InitializeAndExpectStatus() directly to test the reinitialization failure.
270 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
271 DECODER_ERROR_NOT_SUPPORTED);
274 // Test normal decrypt and decode case.
275 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_Normal) {
277 EnterNormalDecodingState();
280 // Test the case where the decryptor returns error when doing decrypt and
282 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_DecodeError) {
285 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _))
286 .WillRepeatedly(RunCallback<1>(Decryptor::kError,
287 scoped_refptr<VideoFrame>(NULL)));
289 ReadAndExpectFrameReadyWith(
290 encrypted_buffer_, VideoDecoder::kDecodeError, null_video_frame_);
292 // After a decode error occurred, all following decode returns kDecodeError.
293 ReadAndExpectFrameReadyWith(
294 encrypted_buffer_, VideoDecoder::kDecodeError, null_video_frame_);
297 // Test the case where the decryptor returns kNeedMoreData to ask for more
298 // buffers before it can produce a frame.
299 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_NeedMoreData) {
302 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _))
303 .WillOnce(RunCallback<1>(Decryptor::kNeedMoreData,
304 scoped_refptr<VideoFrame>()))
305 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess,
306 decoded_video_frame_));
308 ReadAndExpectFrameReadyWith(
309 encrypted_buffer_, VideoDecoder::kNotEnoughData, decoded_video_frame_);
310 ReadAndExpectFrameReadyWith(
311 encrypted_buffer_, VideoDecoder::kOk, decoded_video_frame_);
314 // Test the case where the decryptor receives end-of-stream buffer.
315 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_EndOfStream) {
317 EnterNormalDecodingState();
318 EnterEndOfStreamState();
321 // Test the case where the a key is added when the decryptor is in
322 // kWaitingForKey state.
323 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DuringWaitingForKey) {
325 EnterWaitingForKeyState();
327 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _))
328 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess,
329 decoded_video_frame_));
330 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_));
332 message_loop_.RunUntilIdle();
335 // Test the case where the a key is added when the decryptor is in
336 // kPendingDecode state.
337 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DruingPendingDecode) {
339 EnterPendingDecodeState();
341 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _))
342 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess,
343 decoded_video_frame_));
344 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_));
345 // The video decode callback is returned after the correct decryption key is
348 base::ResetAndReturn(&pending_video_decode_cb_).Run(Decryptor::kNoKey,
350 message_loop_.RunUntilIdle();
353 // Test resetting when the decoder is in kIdle state but has not decoded any
355 TEST_F(DecryptingVideoDecoderTest, Reset_DuringIdleAfterInitialization) {
360 // Test resetting when the decoder is in kIdle state after it has decoded one
362 TEST_F(DecryptingVideoDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
364 EnterNormalDecodingState();
368 // Test resetting when the decoder is in kPendingDecode state.
369 TEST_F(DecryptingVideoDecoderTest, Reset_DuringPendingDecode) {
371 EnterPendingDecodeState();
373 EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
378 // Test resetting when the decoder is in kWaitingForKey state.
379 TEST_F(DecryptingVideoDecoderTest, Reset_DuringWaitingForKey) {
381 EnterWaitingForKeyState();
383 EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
388 // Test resetting when the decoder has hit end of stream and is in
389 // kDecodeFinished state.
390 TEST_F(DecryptingVideoDecoderTest, Reset_AfterDecodeFinished) {
392 EnterNormalDecodingState();
393 EnterEndOfStreamState();
397 // Test resetting after the decoder has been reset.
398 TEST_F(DecryptingVideoDecoderTest, Reset_AfterReset) {
400 EnterNormalDecodingState();
405 // Test stopping when the decoder is in kDecryptorRequested state.
406 TEST_F(DecryptingVideoDecoderTest, Stop_DuringDecryptorRequested) {
407 DecryptorReadyCB decryptor_ready_cb;
408 EXPECT_CALL(*this, RequestDecryptorNotification(_))
409 .WillOnce(SaveArg<0>(&decryptor_ready_cb));
410 decoder_->Initialize(TestVideoConfig::NormalEncrypted(),
411 NewExpectedStatusCB(DECODER_ERROR_NOT_SUPPORTED));
412 message_loop_.RunUntilIdle();
413 // |decryptor_ready_cb| is saved but not called here.
414 EXPECT_FALSE(decryptor_ready_cb.is_null());
416 // During stop, RequestDecryptorNotification() should be called with a NULL
417 // callback to cancel the |decryptor_ready_cb|.
418 EXPECT_CALL(*this, RequestDecryptorNotification(IsNullCallback()))
419 .WillOnce(ResetAndRunCallback(&decryptor_ready_cb,
420 reinterpret_cast<Decryptor*>(NULL)));
424 // Test stopping when the decoder is in kPendingDecoderInit state.
425 TEST_F(DecryptingVideoDecoderTest, Stop_DuringPendingDecoderInit) {
426 EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
427 .WillOnce(SaveArg<1>(&pending_init_cb_));
429 InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
430 DECODER_ERROR_NOT_SUPPORTED);
431 EXPECT_FALSE(pending_init_cb_.is_null());
436 // Test stopping when the decoder is in kIdle state but has not decoded any
438 TEST_F(DecryptingVideoDecoderTest, Stop_DuringIdleAfterInitialization) {
443 // Test stopping when the decoder is in kIdle state after it has decoded one
445 TEST_F(DecryptingVideoDecoderTest, Stop_DuringIdleAfterDecodedOneFrame) {
447 EnterNormalDecodingState();
451 // Test stopping when the decoder is in kPendingDecode state.
452 TEST_F(DecryptingVideoDecoderTest, Stop_DuringPendingDecode) {
454 EnterPendingDecodeState();
456 EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
461 // Test stopping when the decoder is in kWaitingForKey state.
462 TEST_F(DecryptingVideoDecoderTest, Stop_DuringWaitingForKey) {
464 EnterWaitingForKeyState();
466 EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
471 // Test stopping when the decoder has hit end of stream and is in
472 // kDecodeFinished state.
473 TEST_F(DecryptingVideoDecoderTest, Stop_AfterDecodeFinished) {
475 EnterNormalDecodingState();
476 EnterEndOfStreamState();
480 // Test stopping when there is a pending reset on the decoder.
481 // Reset is pending because it cannot complete when the video decode callback
483 TEST_F(DecryptingVideoDecoderTest, Stop_DuringPendingReset) {
485 EnterPendingDecodeState();
487 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kVideo));
488 EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
490 decoder_->Reset(NewExpectedClosure());
494 // Test stopping after the decoder has been reset.
495 TEST_F(DecryptingVideoDecoderTest, Stop_AfterReset) {
497 EnterNormalDecodingState();
502 // Test stopping after the decoder has been stopped.
503 TEST_F(DecryptingVideoDecoderTest, Stop_AfterStop) {
505 EnterNormalDecodingState();