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