xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 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" |
avi@chromium.org | 5821fa0e | 2013-07-18 04:32:35 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 11 | #include "media/base/audio_buffer.h" |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 12 | #include "media/base/buffers.h" |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 13 | #include "media/base/decoder_buffer.h" |
| 14 | #include "media/base/decrypt_config.h" |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 15 | #include "media/base/gmock_callback_support.h" |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 16 | #include "media/base/mock_filters.h" |
scherkus@chromium.org | 1848da0 | 2012-12-06 06:58:38 | [diff] [blame] | 17 | #include "media/base/test_helpers.h" |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 18 | #include "media/filters/decrypting_audio_decoder.h" |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 19 | #include "testing/gmock/include/gmock/gmock.h" |
| 20 | |
| 21 | using ::testing::_; |
| 22 | using ::testing::AtMost; |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 23 | using ::testing::SaveArg; |
| 24 | using ::testing::StrictMock; |
| 25 | |
| 26 | namespace media { |
| 27 | |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 28 | const int kSampleRate = 44100; |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 29 | |
xhwang@chromium.org | ecf1691 | 2013-01-12 16:55:22 | [diff] [blame] | 30 | // Make sure the kFakeAudioFrameSize is a valid frame size for all audio decoder |
| 31 | // configs used in this test. |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 32 | const int kFakeAudioFrameSize = 48; |
| 33 | const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 }; |
| 34 | const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 }; |
| 35 | const int kDecodingDelay = 3; |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 36 | |
| 37 | // Create a fake non-empty encrypted buffer. |
| 38 | static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() { |
| 39 | const int buffer_size = 16; // Need a non-empty buffer; |
| 40 | scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size)); |
tyoverby@chromium.org | 464f253b | 2013-07-18 18:49:43 | [diff] [blame] | 41 | buffer->set_decrypt_config(scoped_ptr<DecryptConfig>(new DecryptConfig( |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 42 | std::string(reinterpret_cast<const char*>(kFakeKeyId), |
| 43 | arraysize(kFakeKeyId)), |
| 44 | std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)), |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 45 | std::vector<SubsampleEntry>()))); |
| 46 | return buffer; |
| 47 | } |
| 48 | |
| 49 | // Use anonymous namespace here to prevent the actions to be defined multiple |
| 50 | // times across multiple test files. Sadly we can't use static for them. |
| 51 | namespace { |
| 52 | |
| 53 | ACTION_P(ReturnBuffer, buffer) { |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 54 | return buffer; |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 55 | } |
| 56 | |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 57 | ACTION_P(RunCallbackIfNotNull, param) { |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 58 | if (!arg0.is_null()) |
| 59 | arg0.Run(param); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 60 | } |
| 61 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 62 | } // namespace |
| 63 | |
| 64 | class DecryptingAudioDecoderTest : public testing::Test { |
| 65 | public: |
| 66 | DecryptingAudioDecoderTest() |
xhwang@chromium.org | 2b454ff8 | 2012-11-15 03:57:58 | [diff] [blame] | 67 | : decoder_(new DecryptingAudioDecoder( |
scherkus@chromium.org | 8c076cb | 2012-11-28 21:06:16 | [diff] [blame] | 68 | message_loop_.message_loop_proxy(), |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 69 | base::Bind( |
| 70 | &DecryptingAudioDecoderTest::RequestDecryptorNotification, |
| 71 | base::Unretained(this)))), |
| 72 | decryptor_(new StrictMock<MockDecryptor>()), |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 73 | num_decrypt_and_decode_calls_(0), |
| 74 | num_frames_in_decryptor_(0), |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 75 | encrypted_buffer_(CreateFakeEncryptedBuffer()), |
xhwang@chromium.org | ecbb976 | 2012-10-24 22:33:54 | [diff] [blame] | 76 | decoded_frame_(NULL), |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 77 | decoded_frame_list_() {} |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 78 | |
rileya@chromium.org | b960fd0 | 2014-01-23 04:48:54 | [diff] [blame] | 79 | virtual ~DecryptingAudioDecoderTest() { |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 80 | EXPECT_CALL(*this, RequestDecryptorNotification(_)) |
| 81 | .Times(testing::AnyNumber()); |
xhwang@chromium.org | 83fb2a9e | 2014-07-16 23:36:41 | [diff] [blame^] | 82 | Destroy(); |
rileya@chromium.org | b960fd0 | 2014-01-23 04:48:54 | [diff] [blame] | 83 | } |
| 84 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 85 | void InitializeAndExpectStatus(const AudioDecoderConfig& config, |
| 86 | PipelineStatus status) { |
dalecurtis@google.com | e81f5f9d | 2014-03-29 01:32:16 | [diff] [blame] | 87 | // Initialize data now that the config is known. Since the code uses |
| 88 | // invalid values (that CreateEmptyBuffer() doesn't support), tweak them |
| 89 | // just for CreateEmptyBuffer(). |
| 90 | int channels = ChannelLayoutToChannelCount(config.channel_layout()); |
| 91 | if (channels < 0) |
| 92 | channels = 0; |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 93 | decoded_frame_ = AudioBuffer::CreateEmptyBuffer(config.channel_layout(), |
dalecurtis@google.com | e81f5f9d | 2014-03-29 01:32:16 | [diff] [blame] | 94 | channels, |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 95 | kSampleRate, |
| 96 | kFakeAudioFrameSize, |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 97 | kNoTimestamp()); |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 98 | decoded_frame_list_.push_back(decoded_frame_); |
| 99 | |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 100 | decoder_->Initialize(config, NewExpectedStatusCB(status), |
| 101 | base::Bind(&DecryptingAudioDecoderTest::FrameReady, |
| 102 | base::Unretained(this))); |
xhwang@chromium.org | 643d544 | 2012-11-14 05:08:32 | [diff] [blame] | 103 | message_loop_.RunUntilIdle(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void Initialize() { |
xhwang@chromium.org | a3e2867 | 2013-03-14 14:54:59 | [diff] [blame] | 107 | EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _)) |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 108 | .Times(AtMost(1)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 109 | .WillOnce(RunCallback<1>(true)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 110 | EXPECT_CALL(*this, RequestDecryptorNotification(_)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 111 | .WillOnce(RunCallbackIfNotNull(decryptor_.get())); |
xhwang@chromium.org | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 112 | EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _)) |
xhwang@chromium.org | 82d7abd | 2012-11-02 23:04:03 | [diff] [blame] | 113 | .WillOnce(SaveArg<1>(&key_added_cb_)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 114 | |
xhwang@chromium.org | 7c39486c | 2013-01-06 15:36:09 | [diff] [blame] | 115 | config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32, |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 116 | CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, true, true, |
dalecurtis@google.com | 6fb8654 | 2014-04-18 19:58:13 | [diff] [blame] | 117 | base::TimeDelta(), 0); |
xhwang@chromium.org | 7c39486c | 2013-01-06 15:36:09 | [diff] [blame] | 118 | InitializeAndExpectStatus(config_, PIPELINE_OK); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 119 | } |
| 120 | |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 121 | void Reinitialize() { |
| 122 | ReinitializeConfigChange(config_); |
| 123 | } |
| 124 | |
dalecurtis@google.com | 6fb8654 | 2014-04-18 19:58:13 | [diff] [blame] | 125 | void ReinitializeConfigChange(const AudioDecoderConfig& new_config) { |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 126 | EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio)); |
| 127 | EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _)) |
| 128 | .WillOnce(RunCallback<1>(true)); |
| 129 | EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _)) |
| 130 | .WillOnce(SaveArg<1>(&key_added_cb_)); |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 131 | decoder_->Initialize(new_config, NewExpectedStatusCB(PIPELINE_OK), |
| 132 | base::Bind(&DecryptingAudioDecoderTest::FrameReady, |
| 133 | base::Unretained(this))); |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 134 | } |
| 135 | |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 136 | // Decode |buffer| and expect DecodeDone to get called with |status|. |
| 137 | void DecodeAndExpect(const scoped_refptr<DecoderBuffer>& buffer, |
| 138 | AudioDecoder::Status status) { |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 139 | EXPECT_CALL(*this, DecodeDone(status)); |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 140 | decoder_->Decode(buffer, |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 141 | base::Bind(&DecryptingAudioDecoderTest::DecodeDone, |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 142 | base::Unretained(this))); |
xhwang@chromium.org | 643d544 | 2012-11-14 05:08:32 | [diff] [blame] | 143 | message_loop_.RunUntilIdle(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 144 | } |
| 145 | |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 146 | // Helper function to simulate the decrypting and decoding process in the |
| 147 | // |decryptor_| with a decoding delay of kDecodingDelay buffers. |
| 148 | void DecryptAndDecodeAudio(const scoped_refptr<DecoderBuffer>& encrypted, |
| 149 | const Decryptor::AudioDecodeCB& audio_decode_cb) { |
| 150 | num_decrypt_and_decode_calls_++; |
| 151 | if (!encrypted->end_of_stream()) |
| 152 | num_frames_in_decryptor_++; |
| 153 | |
| 154 | if (num_decrypt_and_decode_calls_ <= kDecodingDelay || |
| 155 | num_frames_in_decryptor_ == 0) { |
| 156 | audio_decode_cb.Run(Decryptor::kNeedMoreData, Decryptor::AudioBuffers()); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | num_frames_in_decryptor_--; |
| 161 | audio_decode_cb.Run(Decryptor::kSuccess, |
| 162 | Decryptor::AudioBuffers(1, decoded_frame_)); |
| 163 | } |
| 164 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 165 | // Sets up expectations and actions to put DecryptingAudioDecoder in an |
| 166 | // active normal decoding state. |
| 167 | void EnterNormalDecodingState() { |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 168 | EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)).WillRepeatedly( |
| 169 | Invoke(this, &DecryptingAudioDecoderTest::DecryptAndDecodeAudio)); |
| 170 | EXPECT_CALL(*this, FrameReady(decoded_frame_)); |
| 171 | for (int i = 0; i < kDecodingDelay + 1; ++i) |
| 172 | DecodeAndExpect(encrypted_buffer_, AudioDecoder::kOk); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Sets up expectations and actions to put DecryptingAudioDecoder in an end |
| 176 | // of stream state. This function must be called after |
| 177 | // EnterNormalDecodingState() to work. |
| 178 | void EnterEndOfStreamState() { |
xhwang@chromium.org | ff75703 | 2014-06-19 01:42:24 | [diff] [blame] | 179 | // The codec in the |decryptor_| will be flushed. |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 180 | EXPECT_CALL(*this, FrameReady(decoded_frame_)) |
| 181 | .Times(kDecodingDelay); |
| 182 | DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), AudioDecoder::kOk); |
| 183 | EXPECT_EQ(0, num_frames_in_decryptor_); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Make the audio decode callback pending by saving and not firing it. |
| 187 | void EnterPendingDecodeState() { |
| 188 | EXPECT_TRUE(pending_audio_decode_cb_.is_null()); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 189 | EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _)) |
| 190 | .WillOnce(SaveArg<1>(&pending_audio_decode_cb_)); |
| 191 | |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 192 | decoder_->Decode(encrypted_buffer_, |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 193 | base::Bind(&DecryptingAudioDecoderTest::DecodeDone, |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 194 | base::Unretained(this))); |
xhwang@chromium.org | 643d544 | 2012-11-14 05:08:32 | [diff] [blame] | 195 | message_loop_.RunUntilIdle(); |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 196 | // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on |
| 197 | // the decryptor. |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 198 | EXPECT_FALSE(pending_audio_decode_cb_.is_null()); |
| 199 | } |
| 200 | |
| 201 | void EnterWaitingForKeyState() { |
xhwang@chromium.org | 2b454ff8 | 2012-11-15 03:57:58 | [diff] [blame] | 202 | EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 203 | .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey, |
| 204 | Decryptor::AudioBuffers())); |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 205 | decoder_->Decode(encrypted_buffer_, |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 206 | base::Bind(&DecryptingAudioDecoderTest::DecodeDone, |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 207 | base::Unretained(this))); |
xhwang@chromium.org | 643d544 | 2012-11-14 05:08:32 | [diff] [blame] | 208 | message_loop_.RunUntilIdle(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void AbortPendingAudioDecodeCB() { |
| 212 | if (!pending_audio_decode_cb_.is_null()) { |
| 213 | base::ResetAndReturn(&pending_audio_decode_cb_).Run( |
| 214 | Decryptor::kSuccess, Decryptor::AudioBuffers()); |
| 215 | } |
| 216 | } |
| 217 | |
rileya@chromium.org | b960fd0 | 2014-01-23 04:48:54 | [diff] [blame] | 218 | void AbortAllPendingCBs() { |
| 219 | if (!pending_init_cb_.is_null()) { |
| 220 | ASSERT_TRUE(pending_audio_decode_cb_.is_null()); |
| 221 | base::ResetAndReturn(&pending_init_cb_).Run(false); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | AbortPendingAudioDecodeCB(); |
| 226 | } |
| 227 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 228 | void Reset() { |
| 229 | EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio)) |
| 230 | .WillRepeatedly(InvokeWithoutArgs( |
| 231 | this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB)); |
| 232 | |
| 233 | decoder_->Reset(NewExpectedClosure()); |
rileya@chromium.org | 3c0e406 | 2014-01-15 22:23:12 | [diff] [blame] | 234 | message_loop_.RunUntilIdle(); |
| 235 | } |
| 236 | |
xhwang@chromium.org | 83fb2a9e | 2014-07-16 23:36:41 | [diff] [blame^] | 237 | void Destroy() { |
rileya@chromium.org | b960fd0 | 2014-01-23 04:48:54 | [diff] [blame] | 238 | EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio)) |
| 239 | .WillRepeatedly(InvokeWithoutArgs( |
| 240 | this, &DecryptingAudioDecoderTest::AbortAllPendingCBs)); |
| 241 | |
xhwang@chromium.org | 83fb2a9e | 2014-07-16 23:36:41 | [diff] [blame^] | 242 | decoder_.reset(); |
rileya@chromium.org | b960fd0 | 2014-01-23 04:48:54 | [diff] [blame] | 243 | message_loop_.RunUntilIdle(); |
| 244 | } |
| 245 | |
xhwang@chromium.org | daede9b9 | 2012-12-14 00:57:25 | [diff] [blame] | 246 | MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 247 | |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 248 | MOCK_METHOD1(FrameReady, void(const scoped_refptr<AudioBuffer>&)); |
| 249 | MOCK_METHOD1(DecodeDone, void(AudioDecoder::Status)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 250 | |
xhwang@chromium.org | fb5af23 | 2013-04-22 22:40:03 | [diff] [blame] | 251 | base::MessageLoop message_loop_; |
scherkus@chromium.org | 7dd4c74 | 2013-03-21 22:33:47 | [diff] [blame] | 252 | scoped_ptr<DecryptingAudioDecoder> decoder_; |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 253 | scoped_ptr<StrictMock<MockDecryptor> > decryptor_; |
xhwang@chromium.org | 7c39486c | 2013-01-06 15:36:09 | [diff] [blame] | 254 | AudioDecoderConfig config_; |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 255 | |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 256 | // Variables to help the |decryptor_| to simulate decoding delay and flushing. |
| 257 | int num_decrypt_and_decode_calls_; |
| 258 | int num_frames_in_decryptor_; |
| 259 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 260 | Decryptor::DecoderInitCB pending_init_cb_; |
xhwang@chromium.org | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 261 | Decryptor::NewKeyCB key_added_cb_; |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 262 | Decryptor::AudioDecodeCB pending_audio_decode_cb_; |
| 263 | |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 264 | // Constant buffer/frames, to be used/returned by |decoder_| and |decryptor_|. |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 265 | scoped_refptr<DecoderBuffer> encrypted_buffer_; |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 266 | scoped_refptr<AudioBuffer> decoded_frame_; |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 267 | Decryptor::AudioBuffers decoded_frame_list_; |
| 268 | |
| 269 | private: |
| 270 | DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest); |
| 271 | }; |
| 272 | |
| 273 | TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) { |
| 274 | Initialize(); |
| 275 | } |
| 276 | |
| 277 | // Ensure that DecryptingAudioDecoder only accepts encrypted audio. |
| 278 | TEST_F(DecryptingAudioDecoderTest, Initialize_UnencryptedAudioConfig) { |
dalecurtis@google.com | b5eca3c | 2013-01-04 20:20:15 | [diff] [blame] | 279 | AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32, |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 280 | CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, false); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 281 | |
| 282 | InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED); |
| 283 | } |
| 284 | |
| 285 | // Ensure decoder handles invalid audio configs without crashing. |
| 286 | TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) { |
dalecurtis@google.com | b5eca3c | 2013-01-04 20:20:15 | [diff] [blame] | 287 | AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat, |
| 288 | CHANNEL_LAYOUT_STEREO, 0, NULL, 0, true); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 289 | |
| 290 | InitializeAndExpectStatus(config, PIPELINE_ERROR_DECODE); |
| 291 | } |
| 292 | |
| 293 | // Ensure decoder handles unsupported audio configs without crashing. |
| 294 | TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) { |
xhwang@chromium.org | a3e2867 | 2013-03-14 14:54:59 | [diff] [blame] | 295 | EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 296 | .WillOnce(RunCallback<1>(false)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 297 | EXPECT_CALL(*this, RequestDecryptorNotification(_)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 298 | .WillOnce(RunCallbackIfNotNull(decryptor_.get())); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 299 | |
dalecurtis@google.com | b5eca3c | 2013-01-04 20:20:15 | [diff] [blame] | 300 | AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32, |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 301 | CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, true); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 302 | InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED); |
| 303 | } |
| 304 | |
xhwang@chromium.org | 699b64a | 2013-06-19 07:36:05 | [diff] [blame] | 305 | TEST_F(DecryptingAudioDecoderTest, Initialize_NullDecryptor) { |
| 306 | EXPECT_CALL(*this, RequestDecryptorNotification(_)) |
| 307 | .WillRepeatedly(RunCallbackIfNotNull(static_cast<Decryptor*>(NULL))); |
| 308 | |
| 309 | AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32, |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 310 | CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, true); |
xhwang@chromium.org | 699b64a | 2013-06-19 07:36:05 | [diff] [blame] | 311 | InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED); |
| 312 | } |
| 313 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 314 | // Test normal decrypt and decode case. |
| 315 | TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) { |
| 316 | Initialize(); |
| 317 | EnterNormalDecodingState(); |
| 318 | } |
| 319 | |
| 320 | // Test the case where the decryptor returns error when doing decrypt and |
| 321 | // decode. |
| 322 | TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) { |
| 323 | Initialize(); |
| 324 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 325 | EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 326 | .WillRepeatedly(RunCallback<1>(Decryptor::kError, |
| 327 | Decryptor::AudioBuffers())); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 328 | |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 329 | DecodeAndExpect(encrypted_buffer_, AudioDecoder::kDecodeError); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Test the case where the decryptor returns multiple decoded frames. |
| 333 | TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) { |
| 334 | Initialize(); |
| 335 | |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 336 | scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer( |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 337 | config_.channel_layout(), |
dalecurtis@google.com | e81f5f9d | 2014-03-29 01:32:16 | [diff] [blame] | 338 | ChannelLayoutToChannelCount(config_.channel_layout()), |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 339 | kSampleRate, |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 340 | kFakeAudioFrameSize, |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 341 | kNoTimestamp()); |
| 342 | scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer( |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 343 | config_.channel_layout(), |
dalecurtis@google.com | e81f5f9d | 2014-03-29 01:32:16 | [diff] [blame] | 344 | ChannelLayoutToChannelCount(config_.channel_layout()), |
rileya@chromium.org | 16ecd5f | 2014-03-20 05:41:07 | [diff] [blame] | 345 | kSampleRate, |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 346 | kFakeAudioFrameSize, |
jrummell@chromium.org | 47b37a6 | 2013-07-10 03:56:10 | [diff] [blame] | 347 | kNoTimestamp()); |
xhwang@chromium.org | ecbb976 | 2012-10-24 22:33:54 | [diff] [blame] | 348 | decoded_frame_list_.push_back(frame_a); |
| 349 | decoded_frame_list_.push_back(frame_b); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 350 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 351 | EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 352 | .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 353 | |
xhwang@chromium.org | 5c5e9b7 | 2014-06-17 19:21:12 | [diff] [blame] | 354 | EXPECT_CALL(*this, FrameReady(decoded_frame_)); |
| 355 | EXPECT_CALL(*this, FrameReady(frame_a)); |
| 356 | EXPECT_CALL(*this, FrameReady(frame_b)); |
| 357 | DecodeAndExpect(encrypted_buffer_, AudioDecoder::kOk); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Test the case where the decryptor receives end-of-stream buffer. |
| 361 | TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) { |
| 362 | Initialize(); |
| 363 | EnterNormalDecodingState(); |
| 364 | EnterEndOfStreamState(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 365 | } |
| 366 | |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 367 | // Test reinitializing decode with a new config |
| 368 | TEST_F(DecryptingAudioDecoderTest, Reinitialize_ConfigChange) { |
xhwang@chromium.org | 7c39486c | 2013-01-06 15:36:09 | [diff] [blame] | 369 | Initialize(); |
| 370 | |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 371 | EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _)) |
| 372 | .Times(AtMost(1)) |
| 373 | .WillOnce(RunCallback<1>(true)); |
xhwang@chromium.org | 7c39486c | 2013-01-06 15:36:09 | [diff] [blame] | 374 | |
xhwang@chromium.org | ecf1691 | 2013-01-12 16:55:22 | [diff] [blame] | 375 | // The new config is different from the initial config in bits-per-channel, |
| 376 | // channel layout and samples_per_second. |
| 377 | AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16, |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 378 | CHANNEL_LAYOUT_5_1, 88200, NULL, 0, true); |
xhwang@chromium.org | ecf1691 | 2013-01-12 16:55:22 | [diff] [blame] | 379 | EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel()); |
| 380 | EXPECT_NE(new_config.channel_layout(), config_.channel_layout()); |
| 381 | EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second()); |
| 382 | |
rileya@chromium.org | d7015779 | 2014-03-17 21:45:03 | [diff] [blame] | 383 | ReinitializeConfigChange(new_config); |
| 384 | message_loop_.RunUntilIdle(); |
xhwang@chromium.org | 7c39486c | 2013-01-06 15:36:09 | [diff] [blame] | 385 | } |
| 386 | |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 387 | // Test the case where the a key is added when the decryptor is in |
| 388 | // kWaitingForKey state. |
| 389 | TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) { |
| 390 | Initialize(); |
| 391 | EnterWaitingForKeyState(); |
| 392 | |
| 393 | EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 394 | .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_)); |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 395 | EXPECT_CALL(*this, FrameReady(decoded_frame_)); |
| 396 | EXPECT_CALL(*this, DecodeDone(AudioDecoder::kOk)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 397 | key_added_cb_.Run(); |
xhwang@chromium.org | 643d544 | 2012-11-14 05:08:32 | [diff] [blame] | 398 | message_loop_.RunUntilIdle(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Test the case where the a key is added when the decryptor is in |
| 402 | // kPendingDecode state. |
| 403 | TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) { |
| 404 | Initialize(); |
| 405 | EnterPendingDecodeState(); |
| 406 | |
| 407 | EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)) |
xhwang@chromium.org | 8b10f222 | 2012-11-13 05:49:48 | [diff] [blame] | 408 | .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_)); |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 409 | EXPECT_CALL(*this, FrameReady(decoded_frame_)); |
| 410 | EXPECT_CALL(*this, DecodeDone(AudioDecoder::kOk)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 411 | // The audio decode callback is returned after the correct decryption key is |
| 412 | // added. |
| 413 | key_added_cb_.Run(); |
| 414 | base::ResetAndReturn(&pending_audio_decode_cb_).Run( |
| 415 | Decryptor::kNoKey, Decryptor::AudioBuffers()); |
xhwang@chromium.org | 643d544 | 2012-11-14 05:08:32 | [diff] [blame] | 416 | message_loop_.RunUntilIdle(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // Test resetting when the decoder is in kIdle state but has not decoded any |
| 420 | // frame. |
| 421 | TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) { |
| 422 | Initialize(); |
| 423 | Reset(); |
| 424 | } |
| 425 | |
| 426 | // Test resetting when the decoder is in kIdle state after it has decoded one |
| 427 | // frame. |
| 428 | TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) { |
| 429 | Initialize(); |
| 430 | EnterNormalDecodingState(); |
| 431 | Reset(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Test resetting when the decoder is in kPendingDecode state. |
| 435 | TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) { |
| 436 | Initialize(); |
| 437 | EnterPendingDecodeState(); |
| 438 | |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 439 | EXPECT_CALL(*this, DecodeDone(AudioDecoder::kAborted)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 440 | |
| 441 | Reset(); |
| 442 | } |
| 443 | |
| 444 | // Test resetting when the decoder is in kWaitingForKey state. |
| 445 | TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) { |
| 446 | Initialize(); |
| 447 | EnterWaitingForKeyState(); |
| 448 | |
sergeyu@chromium.org | 49cea86 | 2014-06-11 11:11:50 | [diff] [blame] | 449 | EXPECT_CALL(*this, DecodeDone(AudioDecoder::kAborted)); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 450 | |
| 451 | Reset(); |
| 452 | } |
| 453 | |
| 454 | // Test resetting when the decoder has hit end of stream and is in |
| 455 | // kDecodeFinished state. |
| 456 | TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) { |
| 457 | Initialize(); |
| 458 | EnterNormalDecodingState(); |
| 459 | EnterEndOfStreamState(); |
| 460 | Reset(); |
| 461 | } |
| 462 | |
| 463 | // Test resetting after the decoder has been reset. |
| 464 | TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) { |
| 465 | Initialize(); |
| 466 | EnterNormalDecodingState(); |
| 467 | Reset(); |
| 468 | Reset(); |
xhwang@chromium.org | 97a9ce4 | 2012-10-19 10:06:43 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | } // namespace media |