Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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.h" | |
| 9 #include "media/base/gmock_callback_support.h" | |
| 10 #include "media/base/mock_filters.h" | |
| 11 #include "media/filters/audio_decoder_selector.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using ::testing::_; | |
| 15 using ::testing::IsNull; | |
| 16 using ::testing::NiceMock; | |
| 17 using ::testing::NotNull; | |
| 18 using ::testing::Return; | |
| 19 using ::testing::ReturnRef; | |
| 20 using ::testing::StrictMock; | |
| 21 | |
| 22 namespace media { | |
| 23 | |
| 24 class AudioDecoderSelectorTest : public ::testing::Test { | |
| 25 public: | |
| 26 enum DecryptorCapability { | |
| 27 kNoDecryptor, | |
| 28 kDecryptOnly, | |
| 29 kDecryptAndDecode | |
| 30 }; | |
| 31 | |
| 32 AudioDecoderSelectorTest() | |
| 33 : clear_audio_config_( | |
| 34 kCodecVorbis, 16, CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, false), | |
| 35 encrypted_audio_config_( | |
| 36 kCodecVorbis, 16, CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, true), | |
| 37 demuxer_stream_(new StrictMock<MockDemuxerStream>()), | |
| 38 decryptor_(new NiceMock<MockDecryptor>()), | |
| 39 decoder_1_(new StrictMock<MockAudioDecoder>()), | |
| 40 decoder_2_(new StrictMock<MockAudioDecoder>()) { | |
| 41 all_decoders_.push_back(decoder_1_); | |
| 42 all_decoders_.push_back(decoder_2_); | |
| 43 | |
| 44 EXPECT_CALL(*demuxer_stream_, type()) | |
| 45 .WillRepeatedly(Return(DemuxerStream::AUDIO)); | |
| 46 } | |
| 47 | |
| 48 MOCK_METHOD1(OnStatistics, void(const PipelineStatistics&)); | |
| 49 MOCK_METHOD1(SetDecryptorReadyCallback, void(const media::DecryptorReadyCB&)); | |
| 50 MOCK_METHOD2(OnDecoderSelected, | |
| 51 void(const scoped_refptr<AudioDecoder>&, | |
| 52 const scoped_refptr<DecryptingDemuxerStream>&)); | |
| 53 | |
| 54 void UseClearStream() { | |
| 55 EXPECT_CALL(*demuxer_stream_, audio_decoder_config()) | |
| 56 .WillRepeatedly(ReturnRef(clear_audio_config_)); | |
| 57 } | |
| 58 | |
| 59 void UseEncryptedStream() { | |
| 60 EXPECT_CALL(*demuxer_stream_, audio_decoder_config()) | |
| 61 .WillRepeatedly(ReturnRef(encrypted_audio_config_)); | |
| 62 } | |
| 63 | |
| 64 void InitializeDecoderSelector(DecryptorCapability decryptor_capability, | |
| 65 int num_decoders) { | |
| 66 SetDecryptorReadyCB set_decryptor_ready_cb; | |
| 67 if (decryptor_capability == kDecryptOnly || | |
| 68 decryptor_capability == kDecryptAndDecode) { | |
| 69 set_decryptor_ready_cb = base::Bind( | |
| 70 &AudioDecoderSelectorTest::SetDecryptorReadyCallback, | |
| 71 base::Unretained(this)); | |
| 72 | |
| 73 EXPECT_CALL(*this, SetDecryptorReadyCallback(_)) | |
| 74 .WillRepeatedly(RunCallback<0>(decryptor_.get())); | |
| 75 | |
| 76 if (decryptor_capability == kDecryptOnly) { | |
| 77 EXPECT_CALL(*decryptor_, InitializeAudioDecoderMock(_, _)) | |
| 78 .WillRepeatedly(RunCallback<1>(false)); | |
| 79 } else { | |
| 80 EXPECT_CALL(*decryptor_, InitializeAudioDecoderMock(_, _)) | |
| 81 .WillRepeatedly(RunCallback<1>(true)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 DCHECK_GE(all_decoders_.size(), static_cast<size_t>(num_decoders)); | |
| 86 AudioDecoderSelector::AudioDecoderList decoders( | |
| 87 all_decoders_.begin(), all_decoders_.begin() + num_decoders); | |
| 88 | |
| 89 decoder_selector_.reset(new AudioDecoderSelector( | |
| 90 message_loop_.message_loop_proxy(), | |
| 91 decoders, | |
| 92 set_decryptor_ready_cb)); | |
| 93 } | |
| 94 | |
| 95 void SelectDecoder() { | |
| 96 decoder_selector_->SelectAudioDecoder( | |
| 97 demuxer_stream_, | |
| 98 base::Bind(&AudioDecoderSelectorTest::OnStatistics, | |
| 99 base::Unretained(this)), | |
| 100 base::Bind(&AudioDecoderSelectorTest::OnDecoderSelected, | |
| 101 base::Unretained(this))); | |
| 102 message_loop_.RunUntilIdle(); | |
| 103 } | |
| 104 | |
| 105 // Fixture members. | |
| 106 scoped_ptr<AudioDecoderSelector> decoder_selector_; | |
| 107 AudioDecoderConfig clear_audio_config_; | |
| 108 AudioDecoderConfig encrypted_audio_config_; | |
| 109 scoped_refptr<StrictMock<MockDemuxerStream> > demuxer_stream_; | |
| 110 // Use NiceMock since we don't care about most of calls on the decryptor, e.g. | |
| 111 // RegisterKeyAddedCB(). | |
| 112 scoped_ptr<NiceMock<MockDecryptor> > decryptor_; | |
| 113 scoped_refptr<StrictMock<MockAudioDecoder> > decoder_1_; | |
| 114 scoped_refptr<StrictMock<MockAudioDecoder> > decoder_2_; | |
| 115 std::vector<scoped_refptr<AudioDecoder> > all_decoders_; | |
| 116 MessageLoop message_loop_; | |
| 117 | |
| 118 private: | |
| 119 DISALLOW_COPY_AND_ASSIGN(AudioDecoderSelectorTest); | |
| 120 }; | |
| 121 | |
| 122 // The stream is not encrypted but we have no clear decoder. No decoder can be | |
| 123 // selected. | |
| 124 TEST_F(AudioDecoderSelectorTest, ClearStream_NoDecryptor_NoClearDecoder) { | |
| 125 UseClearStream(); | |
| 126 InitializeDecoderSelector(kNoDecryptor, 0); | |
| 127 | |
| 128 EXPECT_CALL(*this, OnDecoderSelected(IsNull(), IsNull())); | |
| 129 | |
| 130 SelectDecoder(); | |
| 131 } | |
| 132 | |
| 133 // The stream is not encrypted and we have one clear decoder. The decoder | |
| 134 // will be selected. | |
| 135 TEST_F(AudioDecoderSelectorTest, ClearStream_NoDecryptor_OneClearDecoder) { | |
| 136 UseClearStream(); | |
| 137 InitializeDecoderSelector(kNoDecryptor, 1); | |
| 138 | |
| 139 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) | |
| 140 .WillOnce(RunCallback<1>(PIPELINE_OK)); | |
| 141 EXPECT_CALL(*this, OnDecoderSelected(scoped_refptr<AudioDecoder>(decoder_1_), | |
| 142 IsNull())); | |
| 143 | |
| 144 SelectDecoder(); | |
| 145 } | |
| 146 | |
| 147 // The stream is not encrypted and we have multiple clear decoders. The first | |
| 148 // decoder that can decode the input stream will be selected. | |
| 149 TEST_F(AudioDecoderSelectorTest, ClearStream_NoDecryptor_MultipleClearDecoder) { | |
| 150 UseClearStream(); | |
| 151 InitializeDecoderSelector(kNoDecryptor, 2); | |
| 152 | |
| 153 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) | |
| 154 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)); | |
| 155 EXPECT_CALL(*decoder_2_, Initialize(_, _, _)) | |
| 156 .WillOnce(RunCallback<1>(PIPELINE_OK)); | |
| 157 EXPECT_CALL(*this, OnDecoderSelected(scoped_refptr<AudioDecoder>(decoder_2_), | |
| 158 IsNull())); | |
| 159 | |
| 160 SelectDecoder(); | |
| 161 } | |
| 162 | |
| 163 // There is a decryptor but the stream is not encrypted. The decoder will be | |
|
ddorwin
2012/12/14 05:17:01
Why does the decryptor get initialized (line 77) i
xhwang
2012/12/14 05:50:07
line 77 has WillRepeatedly(), so we allow the func
| |
| 164 // selected. | |
| 165 TEST_F(AudioDecoderSelectorTest, ClearStream_HasDecryptor) { | |
| 166 UseClearStream(); | |
| 167 InitializeDecoderSelector(kDecryptOnly, 1); | |
| 168 | |
| 169 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) | |
| 170 .WillOnce(RunCallback<1>(PIPELINE_OK)); | |
| 171 EXPECT_CALL(*this, OnDecoderSelected(scoped_refptr<AudioDecoder>(decoder_1_), | |
| 172 IsNull())); | |
| 173 | |
| 174 SelectDecoder(); | |
| 175 } | |
| 176 | |
| 177 // The stream is encrypted and there's no decryptor. No decoder can be selected. | |
| 178 TEST_F(AudioDecoderSelectorTest, EncryptedStream_NoDecryptor) { | |
| 179 UseEncryptedStream(); | |
| 180 InitializeDecoderSelector(kNoDecryptor, 1); | |
| 181 | |
| 182 EXPECT_CALL(*this, OnDecoderSelected(IsNull(), IsNull())); | |
| 183 | |
| 184 SelectDecoder(); | |
| 185 } | |
| 186 | |
| 187 // Decryptor can only do decryption and there's no decoder available. No decoder | |
| 188 // can be selected. | |
| 189 TEST_F(AudioDecoderSelectorTest, EncryptedStream_DecryptOnly_NoClearDecoder) { | |
| 190 UseEncryptedStream(); | |
| 191 InitializeDecoderSelector(kDecryptOnly, 0); | |
| 192 | |
| 193 EXPECT_CALL(*this, OnDecoderSelected(IsNull(), IsNull())); | |
| 194 | |
| 195 SelectDecoder(); | |
| 196 } | |
| 197 | |
| 198 // Decryptor can do decryption-only and there's a decoder available. The decoder | |
| 199 // will be selected and a DecryptingDemuxerStream will be created. | |
| 200 TEST_F(AudioDecoderSelectorTest, EncryptedStream_DecryptOnly_OneClearDecoder) { | |
| 201 UseEncryptedStream(); | |
| 202 InitializeDecoderSelector(kDecryptOnly, 1); | |
| 203 | |
| 204 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) | |
| 205 .WillOnce(RunCallback<1>(PIPELINE_OK)); | |
| 206 EXPECT_CALL(*this, OnDecoderSelected(scoped_refptr<AudioDecoder>(decoder_1_), | |
| 207 NotNull())); | |
| 208 | |
| 209 SelectDecoder(); | |
| 210 } | |
| 211 | |
| 212 // Decryptor can only do decryption and there are multiple decoders available. | |
| 213 // The first decoder that can decode the input stream will be selected and | |
| 214 // a DecryptingDemuxerStream will be created. | |
| 215 TEST_F(AudioDecoderSelectorTest, | |
| 216 EncryptedStream_DecryptOnly_MultipleClearDecoder) { | |
| 217 UseEncryptedStream(); | |
| 218 InitializeDecoderSelector(kDecryptOnly, 2); | |
| 219 | |
| 220 EXPECT_CALL(*decoder_1_, Initialize(_, _, _)) | |
| 221 .WillOnce(RunCallback<1>(DECODER_ERROR_NOT_SUPPORTED)); | |
| 222 EXPECT_CALL(*decoder_2_, Initialize(_, _, _)) | |
| 223 .WillOnce(RunCallback<1>(PIPELINE_OK)); | |
| 224 EXPECT_CALL(*this, OnDecoderSelected(scoped_refptr<AudioDecoder>(decoder_2_), | |
| 225 NotNull())); | |
| 226 | |
| 227 SelectDecoder(); | |
| 228 } | |
| 229 | |
| 230 // Decryptor can do decryption and decoding. A DecryptingAudioDecoder will be | |
| 231 // created and selected. The clear decoders should not be touched at all. | |
| 232 // No DecryptingDemuxerStream should to be created. | |
| 233 TEST_F(AudioDecoderSelectorTest, EncryptedStream_DecryptAndDecode) { | |
| 234 UseEncryptedStream(); | |
| 235 InitializeDecoderSelector(kDecryptAndDecode, 1); | |
| 236 | |
| 237 EXPECT_CALL(*this, OnDecoderSelected(NotNull(), IsNull())); | |
| 238 | |
| 239 SelectDecoder(); | |
| 240 } | |
| 241 | |
| 242 } // namespace media | |
| OLD | NEW |