Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / filters / decrypting_video_decoder_unittest.cc
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.
4
5 #include <string>
6 #include <vector>
7
8 #include "base/bind.h"
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"
19
20 using ::testing::_;
21 using ::testing::AtMost;
22 using ::testing::IsNull;
23 using ::testing::ReturnRef;
24 using ::testing::SaveArg;
25 using ::testing::StrictMock;
26
27 namespace media {
28
29 static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 };
30 static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 };
31
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>())));
41   return buffer;
42 }
43
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.
46 namespace {
47
48 ACTION_P(RunCallbackIfNotNull, param) {
49   if (!arg0.is_null())
50     arg0.Run(param);
51 }
52
53 ACTION_P2(ResetAndRunCallback, callback, param) {
54   base::ResetAndReturn(callback).Run(param);
55 }
56
57 MATCHER(IsEndOfStream, "end of stream") {
58   return (arg->end_of_stream());
59 }
60
61 }  // namespace
62
63 class DecryptingVideoDecoderTest : public testing::Test {
64  public:
65   DecryptingVideoDecoderTest()
66       : decoder_(new DecryptingVideoDecoder(
67             message_loop_.message_loop_proxy(),
68             base::Bind(
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()));
79   }
80
81   virtual ~DecryptingVideoDecoderTest() {
82     Stop();
83   }
84
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();
91   }
92
93   // Initialize the |decoder_| and expects it to succeed.
94   void Initialize() {
95     EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
96         .WillOnce(RunCallback<1>(true));
97     EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kVideo, _))
98         .WillOnce(SaveArg<1>(&key_added_cb_));
99
100     InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(), PIPELINE_OK);
101   }
102
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_));
110
111     InitializeAndExpectStatus(TestVideoConfig::LargeEncrypted(), PIPELINE_OK);
112   }
113
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()));
122     else
123       EXPECT_CALL(*this, FrameReady(status, video_frame));
124
125     decoder_->Decode(buffer,
126                      base::Bind(&DecryptingVideoDecoderTest::FrameReady,
127                                 base::Unretained(this)));
128     message_loop_.RunUntilIdle();
129   }
130
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_);
140   }
141
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(),
147                                 VideoDecoder::kOk,
148                                 end_of_stream_video_frame_);
149   }
150
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_));
156
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
162     // the decryptor.
163     EXPECT_FALSE(pending_video_decode_cb_.is_null());
164   }
165
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();
173   }
174
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));
179     }
180   }
181
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);
186       return;
187     }
188
189     AbortPendingVideoDecodeCB();
190   }
191
192   void Reset() {
193     EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kVideo))
194         .WillRepeatedly(InvokeWithoutArgs(
195             this, &DecryptingVideoDecoderTest::AbortPendingVideoDecodeCB));
196
197     decoder_->Reset(NewExpectedClosure());
198     message_loop_.RunUntilIdle();
199   }
200
201   void Stop() {
202     EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kVideo))
203         .WillRepeatedly(InvokeWithoutArgs(
204             this, &DecryptingVideoDecoderTest::AbortAllPendingCBs));
205
206     decoder_->Stop(NewExpectedClosure());
207     message_loop_.RunUntilIdle();
208   }
209
210   MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&));
211
212   MOCK_METHOD2(FrameReady, void(VideoDecoder::Status,
213                                 const scoped_refptr<VideoFrame>&));
214
215   base::MessageLoop message_loop_;
216   scoped_ptr<DecryptingVideoDecoder> decoder_;
217   scoped_ptr<StrictMock<MockDecryptor> > decryptor_;
218
219   Decryptor::DecoderInitCB pending_init_cb_;
220   Decryptor::NewKeyCB key_added_cb_;
221   Decryptor::VideoDecodeCB pending_video_decode_cb_;
222
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_;
228
229  private:
230   DISALLOW_COPY_AND_ASSIGN(DecryptingVideoDecoderTest);
231 };
232
233 TEST_F(DecryptingVideoDecoderTest, Initialize_Normal) {
234   Initialize();
235 }
236
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);
242 }
243
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_));
249
250   InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
251                             DECODER_ERROR_NOT_SUPPORTED);
252 }
253
254 TEST_F(DecryptingVideoDecoderTest, Reinitialize_Normal) {
255   Initialize();
256   EnterNormalDecodingState();
257   Reinitialize();
258 }
259
260 TEST_F(DecryptingVideoDecoderTest, Reinitialize_Failure) {
261   Initialize();
262   EnterNormalDecodingState();
263
264   EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kVideo));
265   EXPECT_CALL(*decryptor_, InitializeVideoDecoder(_, _))
266       .WillOnce(RunCallback<1>(false));
267
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);
272 }
273
274 // Test normal decrypt and decode case.
275 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_Normal) {
276   Initialize();
277   EnterNormalDecodingState();
278 }
279
280 // Test the case where the decryptor returns error when doing decrypt and
281 // decode.
282 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_DecodeError) {
283   Initialize();
284
285   EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _))
286       .WillRepeatedly(RunCallback<1>(Decryptor::kError,
287                                      scoped_refptr<VideoFrame>(NULL)));
288
289   ReadAndExpectFrameReadyWith(
290       encrypted_buffer_, VideoDecoder::kDecodeError, null_video_frame_);
291
292   // After a decode error occurred, all following decode returns kDecodeError.
293   ReadAndExpectFrameReadyWith(
294       encrypted_buffer_, VideoDecoder::kDecodeError, null_video_frame_);
295 }
296
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) {
300   Initialize();
301
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_));
307
308   ReadAndExpectFrameReadyWith(
309       encrypted_buffer_, VideoDecoder::kNotEnoughData, decoded_video_frame_);
310   ReadAndExpectFrameReadyWith(
311       encrypted_buffer_, VideoDecoder::kOk, decoded_video_frame_);
312 }
313
314 // Test the case where the decryptor receives end-of-stream buffer.
315 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_EndOfStream) {
316   Initialize();
317   EnterNormalDecodingState();
318   EnterEndOfStreamState();
319 }
320
321 // Test the case where the a key is added when the decryptor is in
322 // kWaitingForKey state.
323 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DuringWaitingForKey) {
324   Initialize();
325   EnterWaitingForKeyState();
326
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_));
331   key_added_cb_.Run();
332   message_loop_.RunUntilIdle();
333 }
334
335 // Test the case where the a key is added when the decryptor is in
336 // kPendingDecode state.
337 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DruingPendingDecode) {
338   Initialize();
339   EnterPendingDecodeState();
340
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
346   // added.
347   key_added_cb_.Run();
348   base::ResetAndReturn(&pending_video_decode_cb_).Run(Decryptor::kNoKey,
349                                                       null_video_frame_);
350   message_loop_.RunUntilIdle();
351 }
352
353 // Test resetting when the decoder is in kIdle state but has not decoded any
354 // frame.
355 TEST_F(DecryptingVideoDecoderTest, Reset_DuringIdleAfterInitialization) {
356   Initialize();
357   Reset();
358 }
359
360 // Test resetting when the decoder is in kIdle state after it has decoded one
361 // frame.
362 TEST_F(DecryptingVideoDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
363   Initialize();
364   EnterNormalDecodingState();
365   Reset();
366 }
367
368 // Test resetting when the decoder is in kPendingDecode state.
369 TEST_F(DecryptingVideoDecoderTest, Reset_DuringPendingDecode) {
370   Initialize();
371   EnterPendingDecodeState();
372
373   EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
374
375   Reset();
376 }
377
378 // Test resetting when the decoder is in kWaitingForKey state.
379 TEST_F(DecryptingVideoDecoderTest, Reset_DuringWaitingForKey) {
380   Initialize();
381   EnterWaitingForKeyState();
382
383   EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
384
385   Reset();
386 }
387
388 // Test resetting when the decoder has hit end of stream and is in
389 // kDecodeFinished state.
390 TEST_F(DecryptingVideoDecoderTest, Reset_AfterDecodeFinished) {
391   Initialize();
392   EnterNormalDecodingState();
393   EnterEndOfStreamState();
394   Reset();
395 }
396
397 // Test resetting after the decoder has been reset.
398 TEST_F(DecryptingVideoDecoderTest, Reset_AfterReset) {
399   Initialize();
400   EnterNormalDecodingState();
401   Reset();
402   Reset();
403 }
404
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());
415
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)));
421   Stop();
422 }
423
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_));
428
429   InitializeAndExpectStatus(TestVideoConfig::NormalEncrypted(),
430                             DECODER_ERROR_NOT_SUPPORTED);
431   EXPECT_FALSE(pending_init_cb_.is_null());
432
433   Stop();
434 }
435
436 // Test stopping when the decoder is in kIdle state but has not decoded any
437 // frame.
438 TEST_F(DecryptingVideoDecoderTest, Stop_DuringIdleAfterInitialization) {
439   Initialize();
440   Stop();
441 }
442
443 // Test stopping when the decoder is in kIdle state after it has decoded one
444 // frame.
445 TEST_F(DecryptingVideoDecoderTest, Stop_DuringIdleAfterDecodedOneFrame) {
446   Initialize();
447   EnterNormalDecodingState();
448   Stop();
449 }
450
451 // Test stopping when the decoder is in kPendingDecode state.
452 TEST_F(DecryptingVideoDecoderTest, Stop_DuringPendingDecode) {
453   Initialize();
454   EnterPendingDecodeState();
455
456   EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
457
458   Stop();
459 }
460
461 // Test stopping when the decoder is in kWaitingForKey state.
462 TEST_F(DecryptingVideoDecoderTest, Stop_DuringWaitingForKey) {
463   Initialize();
464   EnterWaitingForKeyState();
465
466   EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
467
468   Stop();
469 }
470
471 // Test stopping when the decoder has hit end of stream and is in
472 // kDecodeFinished state.
473 TEST_F(DecryptingVideoDecoderTest, Stop_AfterDecodeFinished) {
474   Initialize();
475   EnterNormalDecodingState();
476   EnterEndOfStreamState();
477   Stop();
478 }
479
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
482 // is pending.
483 TEST_F(DecryptingVideoDecoderTest, Stop_DuringPendingReset) {
484   Initialize();
485   EnterPendingDecodeState();
486
487   EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kVideo));
488   EXPECT_CALL(*this, FrameReady(VideoDecoder::kAborted, IsNull()));
489
490   decoder_->Reset(NewExpectedClosure());
491   Stop();
492 }
493
494 // Test stopping after the decoder has been reset.
495 TEST_F(DecryptingVideoDecoderTest, Stop_AfterReset) {
496   Initialize();
497   EnterNormalDecodingState();
498   Reset();
499   Stop();
500 }
501
502 // Test stopping after the decoder has been stopped.
503 TEST_F(DecryptingVideoDecoderTest, Stop_AfterStop) {
504   Initialize();
505   EnterNormalDecodingState();
506   Stop();
507   Stop();
508 }
509
510 }  // namespace media