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 <vector> 6 7 #include "base/bind.h" 8 #include "base/message_loop/message_loop.h" 9 #include "media/base/gmock_callback_support.h" 10 #include "media/base/mock_filters.h" 11 #include "media/base/test_helpers.h" 12 #include "media/filters/decoder_selector.h" 13 #include "media/filters/decrypting_demuxer_stream.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 using ::testing::_; 17 using ::testing::InvokeWithoutArgs; 18 using ::testing::IsNull; 19 using ::testing::NiceMock; 20 using ::testing::NotNull; 21 using ::testing::Return; 22 using ::testing::StrictMock; 23 24 // Use anonymous namespace here to prevent the actions to be defined multiple 25 // times across multiple test files. Sadly we can't use static for them. 26 namespace { 27 28 ACTION_P3(ExecuteCallbackWithVerifier, decryptor, done_cb, verifier) { 29 // verifier must be called first since |done_cb| call will invoke it as well. 30 verifier->RecordACalled(); 31 arg0.Run(decryptor, done_cb); 32 } 33 34 ACTION_P(ReportCallback, verifier) { 35 verifier->RecordBCalled(); 36 } 37 38 } // namespace 39 40 namespace media { 41 42 class AudioDecoderSelectorTest : public ::testing::Test { 43 public: 44 enum DecryptorCapability { 45 kNoDecryptor, 46 // Used to test destruction during DecryptingAudioDecoder::Initialize() and 47 // DecryptingDemuxerStream::Initialize(). We don't need this for normal 48 // AudioDecoders since we use MockAudioDecoder. 49 kHoldSetDecryptor, 50 kDecryptOnly, 51 kDecryptAndDecode 52 }; 53 54 AudioDecoderSelectorTest() 55 : demuxer_stream_( 56 new StrictMock<MockDemuxerStream>(DemuxerStream::AUDIO)), 57 decryptor_(new NiceMock<MockDecryptor>()), 58 decoder_1_(new StrictMock<MockAudioDecoder>()), 59 decoder_2_(new StrictMock<MockAudioDecoder>()) { 60 all_decoders_.push_back(decoder_1_); 61 all_decoders_.push_back(decoder_2_); 62 } 63 64 ~AudioDecoderSelectorTest() { 65 message_loop_.RunUntilIdle(); 66 } 67 68 MOCK_METHOD1(SetDecryptorReadyCallback, void(const media::DecryptorReadyCB&)); 69 MOCK_METHOD2(OnDecoderSelected, 70 void(AudioDecoder*, DecryptingDemuxerStream*)); 71 MOCK_METHOD1(DecryptorSet, void(bool)); 72 73 void MockOnDecoderSelected(scoped_ptr<AudioDecoder> decoder, 74 scoped_ptr<DecryptingDemuxerStream> stream) { 75 OnDecoderSelected(decoder.get(), stream.get()); 76 selected_decoder_ = decoder.Pass(); 77 } 78 79 void UseClearStream() { 80 AudioDecoderConfig clear_audio_config( 81 kCodecVorbis, kSampleFormatPlanarF32, CHANNEL_LAYOUT_STEREO, 44100, 82 NULL, 0, false); 83 demuxer_stream_->set_audio_decoder_config(clear_audio_config); 84 } 85 86 void UseEncryptedStream() { 87 AudioDecoderConfig encrypted_audio_config( 88 kCodecVorbis, kSampleFormatPlanarF32, CHANNEL_LAYOUT_STEREO, 44100, 89 NULL, 0, true); 90 demuxer_stream_->set_audio_decoder_config(encrypted_audio_config); 91 } 92 93 void InitializeDecoderSelector(DecryptorCapability decryptor_capability, 94 int num_decoders) { 95 SetDecryptorReadyCB set_decryptor_ready_cb; 96 if (decryptor_capability != kNoDecryptor) { 97 set_decryptor_ready_cb = 98 base::Bind(&AudioDecoderSelectorTest::SetDecryptorReadyCallback, 99 base::Unretained(this)); 100 } 101 102 if (decryptor_capability == kDecryptOnly || 103 decryptor_capability == kDecryptAndDecode) { 104 EXPECT_CALL(*this, SetDecryptorReadyCallback(_)) 105 .WillRepeatedly(ExecuteCallbackWithVerifier( 106 decryptor_.get(), 107 base::Bind(&AudioDecoderSelectorTest::DecryptorSet, 108 base::Unretained(this)), 109 &verifier_)); 110 EXPECT_CALL(*this, DecryptorSet(true)) 111 .WillRepeatedly(ReportCallback(&verifier_)); 112 113 if (decryptor_capability == kDecryptOnly) { 114 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _)) 115 .WillRepeatedly(RunCallback<1>(false)); 116 } else { 117 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _)) 118 .WillRepeatedly(RunCallback<1>(true)); 119 } 120 } else if (decryptor_capability == kHoldSetDecryptor) { 121 // Set and cancel DecryptorReadyCB but the callback is never fired. 122 EXPECT_CALL(*this, SetDecryptorReadyCallback(_)) 123 .Times(2); 124 } 125 126 DCHECK_GE(all_decoders_.size(), static_cast<size_t>(num_decoders)); 127 all_decoders_.erase( 128 all_decoders_.begin() + num_decoders, all_decoders_.end()); 129 130 decoder_selector_.reset(new AudioDecoderSelector( 131 message_loop_.message_loop_proxy(), 132 all_decoders_.Pass(), 133 set_decryptor_ready_cb)); 134 } 135 136 void SelectDecoder() { 137 decoder_selector_->SelectDecoder( 138 demuxer_stream_.get(), 139 false, 140 base::Bind(&AudioDecoderSelectorTest::MockOnDecoderSelected, 141 base::Unretained(this)), 142 base::Bind(&AudioDecoderSelectorTest::OnDecoderOutput)); 143 message_loop_.RunUntilIdle(); 144 } 145 146 void SelectDecoderAndDestroy() { 147 SelectDecoder(); 148 149 EXPECT_CALL(*this, OnDecoderSelected(IsNull(), IsNull())); 150 decoder_selector_.reset(); 151 message_loop_.RunUntilIdle(); 152 } 153 154 static void OnDecoderOutput(const scoped_refptr<AudioBuffer>& output) { 155 NOTREACHED(); 156 } 157 158 // Declare |decoder_selector_| after |demuxer_stream_| and |decryptor_| since 159 // |demuxer_stream_| and |decryptor_| should outlive |decoder_selector_|. 160 scoped_ptr<StrictMock<MockDemuxerStream> > demuxer_stream_; 161 162 // Use NiceMock since we don't care about most of calls on the decryptor, e.g. 163 // RegisterNewKeyCB(). 164 scoped_ptr<NiceMock<MockDecryptor> > decryptor_; 165 166 scoped_ptr<AudioDecoderSelector> decoder_selector_; 167 168 StrictMock<MockAudioDecoder>* decoder_1_; 169 StrictMock<MockAudioDecoder>* decoder_2_; 170 ScopedVector<AudioDecoder> all_decoders_; 171 scoped_ptr<AudioDecoder> selected_decoder_; 172 173 base::MessageLoop message_loop_; 174 175 CallbackPairChecker verifier_; 176 177 private: 178 DISALLOW_COPY_AND_ASSIGN(AudioDecoderSelectorTest); 179 }; 180 181 // The stream is not encrypted but we have no clear decoder. No decoder can be 182 // selected. 183 TEST_F(AudioDecoderSelectorTest, ClearStream_NoDecryptor_NoClearDecoder) { 184 UseClearStream(); 185 InitializeDecoderSelector(kNoDecryptor, 0); 186 187 EXPECT_CALL(*this, OnDecoderSelected(IsNull(), IsNull())); 188 189 SelectDecoder(); 190 } 191 192 // The stream is not encrypted and we have one clear decoder. The decoder 193 // will be selected. 194 TEST_F(AudioDecoderSelectorTest, ClearStream_NoDecryptor_OneClearDecoder) { 195 UseClearStream(); 196 InitializeDecoderSelector(kNoDecryptor, 1); 197 198 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) 199 .WillOnce(RunCallback<1>(PIPELINE_OK)); 200 EXPECT_CALL(*this, OnDecoderSelected(decoder_1_, IsNull())); 201 202 SelectDecoder(); 203 } 204 205 TEST_F(AudioDecoderSelectorTest, 206 Destroy_ClearStream_NoDecryptor_OneClearDecoder) { 207 UseClearStream(); 208 InitializeDecoderSelector(kNoDecryptor, 1); 209 210 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)); 211 212 SelectDecoderAndDestroy(); 213 } 214 215 // The stream is not encrypted and we have multiple clear decoders. The first 216 // decoder that can decode the input stream will be selected. 217 TEST_F(AudioDecoderSelectorTest, ClearStream_NoDecryptor_MultipleClearDecoder) { 218 UseClearStream(); 219 InitializeDecoderSelector(kNoDecryptor, 2); 220 221 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) 222 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)); 223 EXPECT_CALL(*decoder_2_, Initialize(_, _, _)) 224 .WillOnce(RunCallback<1>(PIPELINE_OK)); 225 EXPECT_CALL(*this, OnDecoderSelected(decoder_2_, IsNull())); 226 227 SelectDecoder(); 228 } 229 230 TEST_F(AudioDecoderSelectorTest, 231 Destroy_ClearStream_NoDecryptor_MultipleClearDecoder) { 232 UseClearStream(); 233 InitializeDecoderSelector(kNoDecryptor, 2); 234 235 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) 236 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)); 237 EXPECT_CALL(*decoder_2_, Initialize(_, _, _)); 238 239 SelectDecoderAndDestroy(); 240 } 241 242 // There is a decryptor but the stream is not encrypted. The decoder will be 243 // selected. 244 TEST_F(AudioDecoderSelectorTest, ClearStream_HasDecryptor) { 245 UseClearStream(); 246 InitializeDecoderSelector(kDecryptOnly, 1); 247 248 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) 249 .WillOnce(RunCallback<1>(PIPELINE_OK)); 250 EXPECT_CALL(*this, OnDecoderSelected(decoder_1_, IsNull())); 251 252 SelectDecoder(); 253 } 254 255 TEST_F(AudioDecoderSelectorTest, Destroy_ClearStream_HasDecryptor) { 256 UseClearStream(); 257 InitializeDecoderSelector(kDecryptOnly, 1); 258 259 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)); 260 261 SelectDecoderAndDestroy(); 262 } 263 264 // The stream is encrypted and there's no decryptor. No decoder can be selected. 265 TEST_F(AudioDecoderSelectorTest, EncryptedStream_NoDecryptor) { 266 UseEncryptedStream(); 267 InitializeDecoderSelector(kNoDecryptor, 1); 268 269 EXPECT_CALL(*this, OnDecoderSelected(IsNull(), IsNull())); 270 271 SelectDecoder(); 272 } 273 274 // Decryptor can only do decryption and there's no decoder available. No decoder 275 // can be selected. 276 TEST_F(AudioDecoderSelectorTest, EncryptedStream_DecryptOnly_NoClearDecoder) { 277 UseEncryptedStream(); 278 InitializeDecoderSelector(kDecryptOnly, 0); 279 280 EXPECT_CALL(*this, OnDecoderSelected(IsNull(), IsNull())); 281 282 SelectDecoder(); 283 } 284 285 TEST_F(AudioDecoderSelectorTest, 286 Destroy_EncryptedStream_DecryptOnly_NoClearDecoder) { 287 UseEncryptedStream(); 288 InitializeDecoderSelector(kHoldSetDecryptor, 0); 289 290 SelectDecoderAndDestroy(); 291 } 292 293 // Decryptor can do decryption-only and there's a decoder available. The decoder 294 // will be selected and a DecryptingDemuxerStream will be created. 295 TEST_F(AudioDecoderSelectorTest, EncryptedStream_DecryptOnly_OneClearDecoder) { 296 UseEncryptedStream(); 297 InitializeDecoderSelector(kDecryptOnly, 1); 298 299 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) 300 .WillOnce(RunCallback<1>(PIPELINE_OK)); 301 EXPECT_CALL(*this, OnDecoderSelected(decoder_1_, NotNull())); 302 303 SelectDecoder(); 304 } 305 306 TEST_F(AudioDecoderSelectorTest, 307 Destroy_EncryptedStream_DecryptOnly_OneClearDecoder) { 308 UseEncryptedStream(); 309 InitializeDecoderSelector(kDecryptOnly, 1); 310 311 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)); 312 313 SelectDecoderAndDestroy(); 314 } 315 316 // Decryptor can only do decryption and there are multiple decoders available. 317 // The first decoder that can decode the input stream will be selected and 318 // a DecryptingDemuxerStream will be created. 319 TEST_F(AudioDecoderSelectorTest, 320 EncryptedStream_DecryptOnly_MultipleClearDecoder) { 321 UseEncryptedStream(); 322 InitializeDecoderSelector(kDecryptOnly, 2); 323 324 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) 325 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)); 326 EXPECT_CALL(*decoder_2_, Initialize(_, _, _)) 327 .WillOnce(RunCallback<1>(PIPELINE_OK)); 328 EXPECT_CALL(*this, OnDecoderSelected(decoder_2_, NotNull())); 329 330 SelectDecoder(); 331 } 332 333 TEST_F(AudioDecoderSelectorTest, 334 Destroy_EncryptedStream_DecryptOnly_MultipleClearDecoder) { 335 UseEncryptedStream(); 336 InitializeDecoderSelector(kDecryptOnly, 2); 337 338 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) 339 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)); 340 EXPECT_CALL(*decoder_2_, Initialize(_, _, _)); 341 342 SelectDecoderAndDestroy(); 343 } 344 345 // Decryptor can do decryption and decoding. A DecryptingAudioDecoder will be 346 // created and selected. The clear decoders should not be touched at all. 347 // No DecryptingDemuxerStream should to be created. 348 TEST_F(AudioDecoderSelectorTest, EncryptedStream_DecryptAndDecode) { 349 UseEncryptedStream(); 350 InitializeDecoderSelector(kDecryptAndDecode, 1); 351 352 EXPECT_CALL(*this, OnDecoderSelected(NotNull(), IsNull())); 353 354 SelectDecoder(); 355 } 356 357 TEST_F(AudioDecoderSelectorTest, Destroy_EncryptedStream_DecryptAndDecode) { 358 UseEncryptedStream(); 359 InitializeDecoderSelector(kHoldSetDecryptor, 1); 360 361 SelectDecoderAndDestroy(); 362 } 363 364 } // namespace media 365