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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" |
14 #include "media/base/audio_buffer.h" | 15 #include "media/base/audio_buffer.h" |
15 #include "media/base/decoder_buffer.h" | 16 #include "media/base/decoder_buffer.h" |
16 #include "media/base/decrypt_config.h" | 17 #include "media/base/decrypt_config.h" |
17 #include "media/base/gmock_callback_support.h" | 18 #include "media/base/gmock_callback_support.h" |
18 #include "media/base/media_util.h" | 19 #include "media/base/media_util.h" |
19 #include "media/base/mock_filters.h" | 20 #include "media/base/mock_filters.h" |
20 #include "media/base/test_helpers.h" | 21 #include "media/base/test_helpers.h" |
21 #include "media/base/timestamp_constants.h" | 22 #include "media/base/timestamp_constants.h" |
22 #include "media/filters/decrypting_audio_decoder.h" | 23 #include "media/filters/decrypting_audio_decoder.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 decoded_frame_ = AudioBuffer::CreateEmptyBuffer(config.channel_layout(), | 93 decoded_frame_ = AudioBuffer::CreateEmptyBuffer(config.channel_layout(), |
93 channels, | 94 channels, |
94 kSampleRate, | 95 kSampleRate, |
95 kFakeAudioFrameSize, | 96 kFakeAudioFrameSize, |
96 kNoTimestamp()); | 97 kNoTimestamp()); |
97 decoded_frame_list_.push_back(decoded_frame_); | 98 decoded_frame_list_.push_back(decoded_frame_); |
98 | 99 |
99 decoder_->Initialize(config, cdm_context_.get(), NewExpectedBoolCB(success), | 100 decoder_->Initialize(config, cdm_context_.get(), NewExpectedBoolCB(success), |
100 base::Bind(&DecryptingAudioDecoderTest::FrameReady, | 101 base::Bind(&DecryptingAudioDecoderTest::FrameReady, |
101 base::Unretained(this))); | 102 base::Unretained(this))); |
102 message_loop_.RunUntilIdle(); | 103 base::RunLoop().RunUntilIdle(); |
103 } | 104 } |
104 | 105 |
105 enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR }; | 106 enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR }; |
106 | 107 |
107 void SetCdmType(CdmType cdm_type) { | 108 void SetCdmType(CdmType cdm_type) { |
108 const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR; | 109 const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR; |
109 EXPECT_CALL(*cdm_context_, GetDecryptor()) | 110 EXPECT_CALL(*cdm_context_, GetDecryptor()) |
110 .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr)); | 111 .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr)); |
111 } | 112 } |
112 | 113 |
(...skipping 26 matching lines...) Expand all Loading... |
139 base::Unretained(this))); | 140 base::Unretained(this))); |
140 } | 141 } |
141 | 142 |
142 // Decode |buffer| and expect DecodeDone to get called with |status|. | 143 // Decode |buffer| and expect DecodeDone to get called with |status|. |
143 void DecodeAndExpect(const scoped_refptr<DecoderBuffer>& buffer, | 144 void DecodeAndExpect(const scoped_refptr<DecoderBuffer>& buffer, |
144 DecodeStatus status) { | 145 DecodeStatus status) { |
145 EXPECT_CALL(*this, DecodeDone(status)); | 146 EXPECT_CALL(*this, DecodeDone(status)); |
146 decoder_->Decode(buffer, | 147 decoder_->Decode(buffer, |
147 base::Bind(&DecryptingAudioDecoderTest::DecodeDone, | 148 base::Bind(&DecryptingAudioDecoderTest::DecodeDone, |
148 base::Unretained(this))); | 149 base::Unretained(this))); |
149 message_loop_.RunUntilIdle(); | 150 base::RunLoop().RunUntilIdle(); |
150 } | 151 } |
151 | 152 |
152 // Helper function to simulate the decrypting and decoding process in the | 153 // Helper function to simulate the decrypting and decoding process in the |
153 // |decryptor_| with a decoding delay of kDecodingDelay buffers. | 154 // |decryptor_| with a decoding delay of kDecodingDelay buffers. |
154 void DecryptAndDecodeAudio(const scoped_refptr<DecoderBuffer>& encrypted, | 155 void DecryptAndDecodeAudio(const scoped_refptr<DecoderBuffer>& encrypted, |
155 const Decryptor::AudioDecodeCB& audio_decode_cb) { | 156 const Decryptor::AudioDecodeCB& audio_decode_cb) { |
156 num_decrypt_and_decode_calls_++; | 157 num_decrypt_and_decode_calls_++; |
157 if (!encrypted->end_of_stream()) | 158 if (!encrypted->end_of_stream()) |
158 num_frames_in_decryptor_++; | 159 num_frames_in_decryptor_++; |
159 | 160 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 192 |
192 // Make the audio decode callback pending by saving and not firing it. | 193 // Make the audio decode callback pending by saving and not firing it. |
193 void EnterPendingDecodeState() { | 194 void EnterPendingDecodeState() { |
194 EXPECT_TRUE(pending_audio_decode_cb_.is_null()); | 195 EXPECT_TRUE(pending_audio_decode_cb_.is_null()); |
195 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _)) | 196 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _)) |
196 .WillOnce(SaveArg<1>(&pending_audio_decode_cb_)); | 197 .WillOnce(SaveArg<1>(&pending_audio_decode_cb_)); |
197 | 198 |
198 decoder_->Decode(encrypted_buffer_, | 199 decoder_->Decode(encrypted_buffer_, |
199 base::Bind(&DecryptingAudioDecoderTest::DecodeDone, | 200 base::Bind(&DecryptingAudioDecoderTest::DecodeDone, |
200 base::Unretained(this))); | 201 base::Unretained(this))); |
201 message_loop_.RunUntilIdle(); | 202 base::RunLoop().RunUntilIdle(); |
202 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on | 203 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on |
203 // the decryptor. | 204 // the decryptor. |
204 EXPECT_FALSE(pending_audio_decode_cb_.is_null()); | 205 EXPECT_FALSE(pending_audio_decode_cb_.is_null()); |
205 } | 206 } |
206 | 207 |
207 void EnterWaitingForKeyState() { | 208 void EnterWaitingForKeyState() { |
208 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _)) | 209 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _)) |
209 .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey, | 210 .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey, |
210 Decryptor::AudioFrames())); | 211 Decryptor::AudioFrames())); |
211 EXPECT_CALL(*this, OnWaitingForDecryptionKey()); | 212 EXPECT_CALL(*this, OnWaitingForDecryptionKey()); |
212 decoder_->Decode(encrypted_buffer_, | 213 decoder_->Decode(encrypted_buffer_, |
213 base::Bind(&DecryptingAudioDecoderTest::DecodeDone, | 214 base::Bind(&DecryptingAudioDecoderTest::DecodeDone, |
214 base::Unretained(this))); | 215 base::Unretained(this))); |
215 message_loop_.RunUntilIdle(); | 216 base::RunLoop().RunUntilIdle(); |
216 } | 217 } |
217 | 218 |
218 void AbortPendingAudioDecodeCB() { | 219 void AbortPendingAudioDecodeCB() { |
219 if (!pending_audio_decode_cb_.is_null()) { | 220 if (!pending_audio_decode_cb_.is_null()) { |
220 base::ResetAndReturn(&pending_audio_decode_cb_).Run( | 221 base::ResetAndReturn(&pending_audio_decode_cb_).Run( |
221 Decryptor::kSuccess, Decryptor::AudioFrames()); | 222 Decryptor::kSuccess, Decryptor::AudioFrames()); |
222 } | 223 } |
223 } | 224 } |
224 | 225 |
225 void AbortAllPendingCBs() { | 226 void AbortAllPendingCBs() { |
226 if (!pending_init_cb_.is_null()) { | 227 if (!pending_init_cb_.is_null()) { |
227 ASSERT_TRUE(pending_audio_decode_cb_.is_null()); | 228 ASSERT_TRUE(pending_audio_decode_cb_.is_null()); |
228 base::ResetAndReturn(&pending_init_cb_).Run(false); | 229 base::ResetAndReturn(&pending_init_cb_).Run(false); |
229 return; | 230 return; |
230 } | 231 } |
231 | 232 |
232 AbortPendingAudioDecodeCB(); | 233 AbortPendingAudioDecodeCB(); |
233 } | 234 } |
234 | 235 |
235 void Reset() { | 236 void Reset() { |
236 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio)) | 237 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio)) |
237 .WillRepeatedly(InvokeWithoutArgs( | 238 .WillRepeatedly(InvokeWithoutArgs( |
238 this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB)); | 239 this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB)); |
239 | 240 |
240 decoder_->Reset(NewExpectedClosure()); | 241 decoder_->Reset(NewExpectedClosure()); |
241 message_loop_.RunUntilIdle(); | 242 base::RunLoop().RunUntilIdle(); |
242 } | 243 } |
243 | 244 |
244 void Destroy() { | 245 void Destroy() { |
245 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio)) | 246 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio)) |
246 .WillRepeatedly(InvokeWithoutArgs( | 247 .WillRepeatedly(InvokeWithoutArgs( |
247 this, &DecryptingAudioDecoderTest::AbortAllPendingCBs)); | 248 this, &DecryptingAudioDecoderTest::AbortAllPendingCBs)); |
248 | 249 |
249 decoder_.reset(); | 250 decoder_.reset(); |
250 message_loop_.RunUntilIdle(); | 251 base::RunLoop().RunUntilIdle(); |
251 } | 252 } |
252 | 253 |
253 MOCK_METHOD1(FrameReady, void(const scoped_refptr<AudioBuffer>&)); | 254 MOCK_METHOD1(FrameReady, void(const scoped_refptr<AudioBuffer>&)); |
254 MOCK_METHOD1(DecodeDone, void(DecodeStatus)); | 255 MOCK_METHOD1(DecodeDone, void(DecodeStatus)); |
255 | 256 |
256 MOCK_METHOD0(OnWaitingForDecryptionKey, void(void)); | 257 MOCK_METHOD0(OnWaitingForDecryptionKey, void(void)); |
257 | 258 |
258 base::MessageLoop message_loop_; | 259 base::MessageLoop message_loop_; |
259 std::unique_ptr<DecryptingAudioDecoder> decoder_; | 260 std::unique_ptr<DecryptingAudioDecoder> decoder_; |
260 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_; | 261 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 // The new config is different from the initial config in bits-per-channel, | 385 // The new config is different from the initial config in bits-per-channel, |
385 // channel layout and samples_per_second. | 386 // channel layout and samples_per_second. |
386 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16, | 387 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16, |
387 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(), | 388 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(), |
388 AesCtrEncryptionScheme()); | 389 AesCtrEncryptionScheme()); |
389 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel()); | 390 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel()); |
390 EXPECT_NE(new_config.channel_layout(), config_.channel_layout()); | 391 EXPECT_NE(new_config.channel_layout(), config_.channel_layout()); |
391 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second()); | 392 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second()); |
392 | 393 |
393 ReinitializeConfigChange(new_config); | 394 ReinitializeConfigChange(new_config); |
394 message_loop_.RunUntilIdle(); | 395 base::RunLoop().RunUntilIdle(); |
395 } | 396 } |
396 | 397 |
397 // Test the case where the a key is added when the decryptor is in | 398 // Test the case where the a key is added when the decryptor is in |
398 // kWaitingForKey state. | 399 // kWaitingForKey state. |
399 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) { | 400 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) { |
400 Initialize(); | 401 Initialize(); |
401 EnterWaitingForKeyState(); | 402 EnterWaitingForKeyState(); |
402 | 403 |
403 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) | 404 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) |
404 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_)); | 405 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_)); |
405 EXPECT_CALL(*this, FrameReady(decoded_frame_)); | 406 EXPECT_CALL(*this, FrameReady(decoded_frame_)); |
406 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK)); | 407 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK)); |
407 key_added_cb_.Run(); | 408 key_added_cb_.Run(); |
408 message_loop_.RunUntilIdle(); | 409 base::RunLoop().RunUntilIdle(); |
409 } | 410 } |
410 | 411 |
411 // Test the case where the a key is added when the decryptor is in | 412 // Test the case where the a key is added when the decryptor is in |
412 // kPendingDecode state. | 413 // kPendingDecode state. |
413 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) { | 414 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) { |
414 Initialize(); | 415 Initialize(); |
415 EnterPendingDecodeState(); | 416 EnterPendingDecodeState(); |
416 | 417 |
417 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) | 418 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) |
418 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_)); | 419 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_)); |
419 EXPECT_CALL(*this, FrameReady(decoded_frame_)); | 420 EXPECT_CALL(*this, FrameReady(decoded_frame_)); |
420 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK)); | 421 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK)); |
421 // The audio decode callback is returned after the correct decryption key is | 422 // The audio decode callback is returned after the correct decryption key is |
422 // added. | 423 // added. |
423 key_added_cb_.Run(); | 424 key_added_cb_.Run(); |
424 base::ResetAndReturn(&pending_audio_decode_cb_).Run( | 425 base::ResetAndReturn(&pending_audio_decode_cb_).Run( |
425 Decryptor::kNoKey, Decryptor::AudioFrames()); | 426 Decryptor::kNoKey, Decryptor::AudioFrames()); |
426 message_loop_.RunUntilIdle(); | 427 base::RunLoop().RunUntilIdle(); |
427 } | 428 } |
428 | 429 |
429 // Test resetting when the decoder is in kIdle state but has not decoded any | 430 // Test resetting when the decoder is in kIdle state but has not decoded any |
430 // frame. | 431 // frame. |
431 TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) { | 432 TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) { |
432 Initialize(); | 433 Initialize(); |
433 Reset(); | 434 Reset(); |
434 } | 435 } |
435 | 436 |
436 // Test resetting when the decoder is in kIdle state after it has decoded one | 437 // Test resetting when the decoder is in kIdle state after it has decoded one |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 | 473 |
473 // Test resetting after the decoder has been reset. | 474 // Test resetting after the decoder has been reset. |
474 TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) { | 475 TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) { |
475 Initialize(); | 476 Initialize(); |
476 EnterNormalDecodingState(); | 477 EnterNormalDecodingState(); |
477 Reset(); | 478 Reset(); |
478 Reset(); | 479 Reset(); |
479 } | 480 } |
480 | 481 |
481 } // namespace media | 482 } // namespace media |
OLD | NEW |