OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "media/base/decoder_buffer.h" | 11 #include "media/base/decoder_buffer.h" |
12 #include "media/base/decrypt_config.h" | 12 #include "media/base/decrypt_config.h" |
| 13 #include "media/base/gmock_callback_support.h" |
13 #include "media/base/mock_callback.h" | 14 #include "media/base/mock_callback.h" |
14 #include "media/base/mock_filters.h" | 15 #include "media/base/mock_filters.h" |
15 #include "media/base/video_frame.h" | 16 #include "media/base/video_frame.h" |
16 #include "media/filters/decrypting_video_decoder.h" | 17 #include "media/filters/decrypting_video_decoder.h" |
17 #include "media/filters/ffmpeg_decoder_unittest.h" | 18 #include "media/filters/ffmpeg_decoder_unittest.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
19 | 20 |
20 using ::testing::_; | 21 using ::testing::_; |
21 using ::testing::AtMost; | 22 using ::testing::AtMost; |
22 using ::testing::Invoke; | 23 using ::testing::Invoke; |
(...skipping 25 matching lines...) Expand all Loading... |
48 } | 49 } |
49 | 50 |
50 // Use anonymous namespace here to prevent the actions to be defined multiple | 51 // Use anonymous namespace here to prevent the actions to be defined multiple |
51 // times across multiple test files. Sadly we can't use static for them. | 52 // times across multiple test files. Sadly we can't use static for them. |
52 namespace { | 53 namespace { |
53 | 54 |
54 ACTION_P(ReturnBuffer, buffer) { | 55 ACTION_P(ReturnBuffer, buffer) { |
55 arg0.Run(buffer ? DemuxerStream::kOk : DemuxerStream::kAborted, buffer); | 56 arg0.Run(buffer ? DemuxerStream::kOk : DemuxerStream::kAborted, buffer); |
56 } | 57 } |
57 | 58 |
58 ACTION(ReturnConfigChanged) { | 59 ACTION_P(RunCallbackIfNotNull, param) { |
59 arg0.Run(DemuxerStream::kConfigChanged, scoped_refptr<DecoderBuffer>(NULL)); | |
60 } | |
61 | |
62 ACTION_P(RunCallback0, param) { | |
63 if (!arg0.is_null()) | 60 if (!arg0.is_null()) |
64 arg0.Run(param); | 61 arg0.Run(param); |
65 } | 62 } |
66 | 63 |
67 ACTION_P(RunCallback1, param) { | |
68 arg1.Run(param); | |
69 } | |
70 | |
71 ACTION_P2(RunCallback2, param1, param2) { | |
72 arg1.Run(param1, param2); | |
73 } | |
74 | |
75 ACTION_P2(ResetAndRunCallback, callback, param) { | 64 ACTION_P2(ResetAndRunCallback, callback, param) { |
76 base::ResetAndReturn(callback).Run(param); | 65 base::ResetAndReturn(callback).Run(param); |
77 } | 66 } |
78 | 67 |
79 MATCHER(IsNullCallback, "") { | 68 MATCHER(IsEndOfStream, "end of stream") { |
80 return (arg.is_null()); | |
81 } | |
82 | |
83 MATCHER(IsEndOfStream, "") { | |
84 return (arg->IsEndOfStream()); | 69 return (arg->IsEndOfStream()); |
85 } | 70 } |
86 | 71 |
87 } // namespace | 72 } // namespace |
88 | 73 |
89 class DecryptingVideoDecoderTest : public testing::Test { | 74 class DecryptingVideoDecoderTest : public testing::Test { |
90 public: | 75 public: |
91 DecryptingVideoDecoderTest() | 76 DecryptingVideoDecoderTest() |
92 : decoder_(new StrictMock<DecryptingVideoDecoder>( | 77 : decoder_(new StrictMock<DecryptingVideoDecoder>( |
93 base::Bind(&Identity<scoped_refptr<base::MessageLoopProxy> >, | 78 base::Bind(&Identity<scoped_refptr<base::MessageLoopProxy> >, |
(...skipping 11 matching lines...) Expand all Loading... |
105 | 90 |
106 virtual ~DecryptingVideoDecoderTest() { | 91 virtual ~DecryptingVideoDecoderTest() { |
107 Stop(); | 92 Stop(); |
108 } | 93 } |
109 | 94 |
110 void InitializeAndExpectStatus(const VideoDecoderConfig& config, | 95 void InitializeAndExpectStatus(const VideoDecoderConfig& config, |
111 PipelineStatus status) { | 96 PipelineStatus status) { |
112 EXPECT_CALL(*demuxer_, video_decoder_config()) | 97 EXPECT_CALL(*demuxer_, video_decoder_config()) |
113 .WillRepeatedly(ReturnRef(config)); | 98 .WillRepeatedly(ReturnRef(config)); |
114 EXPECT_CALL(*this, RequestDecryptorNotification(_)) | 99 EXPECT_CALL(*this, RequestDecryptorNotification(_)) |
115 .WillOnce(RunCallback0(decryptor_.get())); | 100 .WillOnce(RunCallbackIfNotNull(decryptor_.get())); |
116 | 101 |
117 decoder_->Initialize(demuxer_, NewExpectedStatusCB(status), | 102 decoder_->Initialize(demuxer_, NewExpectedStatusCB(status), |
118 base::Bind(&MockStatisticsCB::OnStatistics, | 103 base::Bind(&MockStatisticsCB::OnStatistics, |
119 base::Unretained(&statistics_cb_))); | 104 base::Unretained(&statistics_cb_))); |
120 message_loop_.RunAllPending(); | 105 message_loop_.RunAllPending(); |
121 } | 106 } |
122 | 107 |
123 void Initialize() { | 108 void Initialize() { |
124 EXPECT_CALL(*decryptor_, InitializeVideoDecoderMock(_, _)) | 109 EXPECT_CALL(*decryptor_, InitializeVideoDecoderMock(_, _)) |
125 .Times(AtMost(1)) | 110 .Times(AtMost(1)) |
126 .WillOnce(RunCallback1(true)); | 111 .WillOnce(RunCallback<1>(true)); |
127 EXPECT_CALL(*decryptor_, RegisterKeyAddedCB(Decryptor::kVideo, _)) | 112 EXPECT_CALL(*decryptor_, RegisterKeyAddedCB(Decryptor::kVideo, _)) |
128 .WillOnce(SaveArg<1>(&key_added_cb_)); | 113 .WillOnce(SaveArg<1>(&key_added_cb_)); |
129 | 114 |
130 config_.Initialize(kCodecVP8, VIDEO_CODEC_PROFILE_UNKNOWN, kVideoFormat, | 115 config_.Initialize(kCodecVP8, VIDEO_CODEC_PROFILE_UNKNOWN, kVideoFormat, |
131 kCodedSize, kVisibleRect, kNaturalSize, | 116 kCodedSize, kVisibleRect, kNaturalSize, |
132 NULL, 0, true, true); | 117 NULL, 0, true, true); |
133 | 118 |
134 InitializeAndExpectStatus(config_, PIPELINE_OK); | 119 InitializeAndExpectStatus(config_, PIPELINE_OK); |
135 } | 120 } |
136 | 121 |
(...skipping 12 matching lines...) Expand all Loading... |
149 message_loop_.RunAllPending(); | 134 message_loop_.RunAllPending(); |
150 } | 135 } |
151 | 136 |
152 // Sets up expectations and actions to put DecryptingVideoDecoder in an | 137 // Sets up expectations and actions to put DecryptingVideoDecoder in an |
153 // active normal decoding state. | 138 // active normal decoding state. |
154 void EnterNormalDecodingState() { | 139 void EnterNormalDecodingState() { |
155 EXPECT_CALL(*demuxer_, Read(_)) | 140 EXPECT_CALL(*demuxer_, Read(_)) |
156 .WillOnce(ReturnBuffer(encrypted_buffer_)) | 141 .WillOnce(ReturnBuffer(encrypted_buffer_)) |
157 .WillRepeatedly(ReturnBuffer(DecoderBuffer::CreateEOSBuffer())); | 142 .WillRepeatedly(ReturnBuffer(DecoderBuffer::CreateEOSBuffer())); |
158 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) | 143 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) |
159 .WillOnce(RunCallback2(Decryptor::kSuccess, decoded_video_frame_)) | 144 .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_video_frame_)) |
160 .WillRepeatedly(RunCallback2(Decryptor::kNeedMoreData, | 145 .WillRepeatedly(RunCallback<1>(Decryptor::kNeedMoreData, |
161 scoped_refptr<VideoFrame>())); | 146 scoped_refptr<VideoFrame>())); |
162 EXPECT_CALL(statistics_cb_, OnStatistics(_)); | 147 EXPECT_CALL(statistics_cb_, OnStatistics(_)); |
163 | 148 |
164 ReadAndExpectFrameReadyWith(VideoDecoder::kOk, decoded_video_frame_); | 149 ReadAndExpectFrameReadyWith(VideoDecoder::kOk, decoded_video_frame_); |
165 } | 150 } |
166 | 151 |
167 // Sets up expectations and actions to put DecryptingVideoDecoder in an end | 152 // Sets up expectations and actions to put DecryptingVideoDecoder in an end |
168 // of stream state. This function must be called after | 153 // of stream state. This function must be called after |
169 // EnterNormalDecodingState() to work. | 154 // EnterNormalDecodingState() to work. |
170 void EnterEndOfStreamState() { | 155 void EnterEndOfStreamState() { |
171 ReadAndExpectFrameReadyWith(VideoDecoder::kOk, end_of_stream_video_frame_); | 156 ReadAndExpectFrameReadyWith(VideoDecoder::kOk, end_of_stream_video_frame_); |
(...skipping 24 matching lines...) Expand all Loading... |
196 message_loop_.RunAllPending(); | 181 message_loop_.RunAllPending(); |
197 // Make sure the Read() on the decoder triggers a DecryptAndDecode() on the | 182 // Make sure the Read() on the decoder triggers a DecryptAndDecode() on the |
198 // decryptor. | 183 // decryptor. |
199 EXPECT_FALSE(pending_video_decode_cb_.is_null()); | 184 EXPECT_FALSE(pending_video_decode_cb_.is_null()); |
200 } | 185 } |
201 | 186 |
202 void EnterWaitingForKeyState() { | 187 void EnterWaitingForKeyState() { |
203 EXPECT_CALL(*demuxer_, Read(_)) | 188 EXPECT_CALL(*demuxer_, Read(_)) |
204 .WillRepeatedly(ReturnBuffer(encrypted_buffer_)); | 189 .WillRepeatedly(ReturnBuffer(encrypted_buffer_)); |
205 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) | 190 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) |
206 .WillRepeatedly(RunCallback2(Decryptor::kNoKey, null_video_frame_)); | 191 .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey, null_video_frame_)); |
207 decoder_->Read(base::Bind(&DecryptingVideoDecoderTest::FrameReady, | 192 decoder_->Read(base::Bind(&DecryptingVideoDecoderTest::FrameReady, |
208 base::Unretained(this))); | 193 base::Unretained(this))); |
209 message_loop_.RunAllPending(); | 194 message_loop_.RunAllPending(); |
210 } | 195 } |
211 | 196 |
212 void AbortPendingVideoDecodeCB() { | 197 void AbortPendingVideoDecodeCB() { |
213 if (!pending_video_decode_cb_.is_null()) { | 198 if (!pending_video_decode_cb_.is_null()) { |
214 base::ResetAndReturn(&pending_video_decode_cb_).Run( | 199 base::ResetAndReturn(&pending_video_decode_cb_).Run( |
215 Decryptor::kSuccess, scoped_refptr<VideoFrame>(NULL)); | 200 Decryptor::kSuccess, scoped_refptr<VideoFrame>(NULL)); |
216 } | 201 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 VideoFrame::INVALID, | 280 VideoFrame::INVALID, |
296 kCodedSize, kVisibleRect, kNaturalSize, | 281 kCodedSize, kVisibleRect, kNaturalSize, |
297 NULL, 0, true); | 282 NULL, 0, true); |
298 | 283 |
299 InitializeAndExpectStatus(config, PIPELINE_ERROR_DECODE); | 284 InitializeAndExpectStatus(config, PIPELINE_ERROR_DECODE); |
300 } | 285 } |
301 | 286 |
302 // Ensure decoder handles unsupported video configs without crashing. | 287 // Ensure decoder handles unsupported video configs without crashing. |
303 TEST_F(DecryptingVideoDecoderTest, Initialize_UnsupportedVideoConfig) { | 288 TEST_F(DecryptingVideoDecoderTest, Initialize_UnsupportedVideoConfig) { |
304 EXPECT_CALL(*decryptor_, InitializeVideoDecoderMock(_, _)) | 289 EXPECT_CALL(*decryptor_, InitializeVideoDecoderMock(_, _)) |
305 .WillOnce(RunCallback1(false)); | 290 .WillOnce(RunCallback<1>(false)); |
306 | 291 |
307 VideoDecoderConfig config(kCodecVP8, VIDEO_CODEC_PROFILE_UNKNOWN, | 292 VideoDecoderConfig config(kCodecVP8, VIDEO_CODEC_PROFILE_UNKNOWN, |
308 kVideoFormat, | 293 kVideoFormat, |
309 kCodedSize, kVisibleRect, kNaturalSize, | 294 kCodedSize, kVisibleRect, kNaturalSize, |
310 NULL, 0, true); | 295 NULL, 0, true); |
311 | 296 |
312 InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED); | 297 InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED); |
313 } | 298 } |
314 | 299 |
315 // Test normal decrypt and decode case. | 300 // Test normal decrypt and decode case. |
316 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_Normal) { | 301 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_Normal) { |
317 Initialize(); | 302 Initialize(); |
318 EnterNormalDecodingState(); | 303 EnterNormalDecodingState(); |
319 } | 304 } |
320 | 305 |
321 // Test the case where the decryptor returns error when doing decrypt and | 306 // Test the case where the decryptor returns error when doing decrypt and |
322 // decode. | 307 // decode. |
323 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_DecodeError) { | 308 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_DecodeError) { |
324 Initialize(); | 309 Initialize(); |
325 | 310 |
326 EXPECT_CALL(*demuxer_, Read(_)) | 311 EXPECT_CALL(*demuxer_, Read(_)) |
327 .WillRepeatedly(ReturnBuffer(encrypted_buffer_)); | 312 .WillRepeatedly(ReturnBuffer(encrypted_buffer_)); |
328 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) | 313 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) |
329 .WillRepeatedly(RunCallback2(Decryptor::kError, | 314 .WillRepeatedly(RunCallback<1>(Decryptor::kError, |
330 scoped_refptr<VideoFrame>(NULL))); | 315 scoped_refptr<VideoFrame>(NULL))); |
331 | 316 |
332 ReadAndExpectFrameReadyWith(VideoDecoder::kDecodeError, null_video_frame_); | 317 ReadAndExpectFrameReadyWith(VideoDecoder::kDecodeError, null_video_frame_); |
333 } | 318 } |
334 | 319 |
335 // Test the case where the decryptor returns kNeedMoreData to ask for more | 320 // Test the case where the decryptor returns kNeedMoreData to ask for more |
336 // buffers before it can produce a frame. | 321 // buffers before it can produce a frame. |
337 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_NeedMoreData) { | 322 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_NeedMoreData) { |
338 Initialize(); | 323 Initialize(); |
339 | 324 |
340 EXPECT_CALL(*demuxer_, Read(_)) | 325 EXPECT_CALL(*demuxer_, Read(_)) |
341 .Times(2) | 326 .Times(2) |
342 .WillRepeatedly(ReturnBuffer(encrypted_buffer_)); | 327 .WillRepeatedly(ReturnBuffer(encrypted_buffer_)); |
343 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) | 328 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) |
344 .WillOnce(RunCallback2(Decryptor::kNeedMoreData, | 329 .WillOnce(RunCallback<1>(Decryptor::kNeedMoreData, |
345 scoped_refptr<VideoFrame>())) | 330 scoped_refptr<VideoFrame>())) |
346 .WillRepeatedly(RunCallback2(Decryptor::kSuccess, decoded_video_frame_)); | 331 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, |
| 332 decoded_video_frame_)); |
347 EXPECT_CALL(statistics_cb_, OnStatistics(_)) | 333 EXPECT_CALL(statistics_cb_, OnStatistics(_)) |
348 .Times(2); | 334 .Times(2); |
349 | 335 |
350 ReadAndExpectFrameReadyWith(VideoDecoder::kOk, decoded_video_frame_); | 336 ReadAndExpectFrameReadyWith(VideoDecoder::kOk, decoded_video_frame_); |
351 } | 337 } |
352 | 338 |
353 // Test the case where the decryptor receives end-of-stream buffer. | 339 // Test the case where the decryptor receives end-of-stream buffer. |
354 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_EndOfStream) { | 340 TEST_F(DecryptingVideoDecoderTest, DecryptAndDecode_EndOfStream) { |
355 Initialize(); | 341 Initialize(); |
356 EnterNormalDecodingState(); | 342 EnterNormalDecodingState(); |
357 EnterEndOfStreamState(); | 343 EnterEndOfStreamState(); |
358 } | 344 } |
359 | 345 |
360 // Test the case where the a key is added when the decryptor is in | 346 // Test the case where the a key is added when the decryptor is in |
361 // kWaitingForKey state. | 347 // kWaitingForKey state. |
362 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DuringWaitingForKey) { | 348 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DuringWaitingForKey) { |
363 Initialize(); | 349 Initialize(); |
364 EnterWaitingForKeyState(); | 350 EnterWaitingForKeyState(); |
365 | 351 |
366 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) | 352 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) |
367 .WillRepeatedly(RunCallback2(Decryptor::kSuccess, decoded_video_frame_)); | 353 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, |
| 354 decoded_video_frame_)); |
368 EXPECT_CALL(statistics_cb_, OnStatistics(_)); | 355 EXPECT_CALL(statistics_cb_, OnStatistics(_)); |
369 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_)); | 356 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_)); |
370 key_added_cb_.Run(); | 357 key_added_cb_.Run(); |
371 message_loop_.RunAllPending(); | 358 message_loop_.RunAllPending(); |
372 } | 359 } |
373 | 360 |
374 // Test the case where the a key is added when the decryptor is in | 361 // Test the case where the a key is added when the decryptor is in |
375 // kPendingDecode state. | 362 // kPendingDecode state. |
376 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DruingPendingDecode) { | 363 TEST_F(DecryptingVideoDecoderTest, KeyAdded_DruingPendingDecode) { |
377 Initialize(); | 364 Initialize(); |
378 EnterPendingDecodeState(); | 365 EnterPendingDecodeState(); |
379 | 366 |
380 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) | 367 EXPECT_CALL(*decryptor_, DecryptAndDecodeVideo(_, _)) |
381 .WillRepeatedly(RunCallback2(Decryptor::kSuccess, decoded_video_frame_)); | 368 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, |
| 369 decoded_video_frame_)); |
382 EXPECT_CALL(statistics_cb_, OnStatistics(_)); | 370 EXPECT_CALL(statistics_cb_, OnStatistics(_)); |
383 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_)); | 371 EXPECT_CALL(*this, FrameReady(VideoDecoder::kOk, decoded_video_frame_)); |
384 // The video decode callback is returned after the correct decryption key is | 372 // The video decode callback is returned after the correct decryption key is |
385 // added. | 373 // added. |
386 key_added_cb_.Run(); | 374 key_added_cb_.Run(); |
387 base::ResetAndReturn(&pending_video_decode_cb_).Run(Decryptor::kNoKey, | 375 base::ResetAndReturn(&pending_video_decode_cb_).Run(Decryptor::kNoKey, |
388 null_video_frame_); | 376 null_video_frame_); |
389 message_loop_.RunAllPending(); | 377 message_loop_.RunAllPending(); |
390 } | 378 } |
391 | 379 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 base::ResetAndReturn(&pending_demuxer_read_cb_).Run(DemuxerStream::kAborted, | 592 base::ResetAndReturn(&pending_demuxer_read_cb_).Run(DemuxerStream::kAborted, |
605 NULL); | 593 NULL); |
606 message_loop_.RunAllPending(); | 594 message_loop_.RunAllPending(); |
607 } | 595 } |
608 | 596 |
609 // Test config change on the demuxer stream. | 597 // Test config change on the demuxer stream. |
610 TEST_F(DecryptingVideoDecoderTest, DemuxerRead_ConfigChanged) { | 598 TEST_F(DecryptingVideoDecoderTest, DemuxerRead_ConfigChanged) { |
611 Initialize(); | 599 Initialize(); |
612 | 600 |
613 EXPECT_CALL(*demuxer_, Read(_)) | 601 EXPECT_CALL(*demuxer_, Read(_)) |
614 .WillOnce(ReturnConfigChanged()); | 602 .WillOnce(RunCallback<0>(DemuxerStream::kConfigChanged, |
| 603 scoped_refptr<DecoderBuffer>())); |
615 | 604 |
616 // TODO(xhwang): Update this test when kConfigChanged is supported in | 605 // TODO(xhwang): Update this test when kConfigChanged is supported in |
617 // DecryptingVideoDecoder. | 606 // DecryptingVideoDecoder. |
618 ReadAndExpectFrameReadyWith(VideoDecoder::kDecodeError, null_video_frame_); | 607 ReadAndExpectFrameReadyWith(VideoDecoder::kDecodeError, null_video_frame_); |
619 } | 608 } |
620 | 609 |
621 } // namespace media | 610 } // namespace media |
OLD | NEW |