[go: nahoru, domu]

blob: b8bca93b1b1d129cb5abb5e607427586d08afafc [file] [log] [blame]
xhwang@chromium.org97a9ce42012-10-19 10:06:431// 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
avi1323b9c22015-12-23 06:22:365#include <stdint.h>
6
xhwang@chromium.org97a9ce42012-10-19 10:06:437#include <string>
8#include <vector>
9
10#include "base/bind.h"
11#include "base/callback_helpers.h"
avi1323b9c22015-12-23 06:22:3612#include "base/macros.h"
avi@chromium.org5821fa0e2013-07-18 04:32:3513#include "base/message_loop/message_loop.h"
fdorayf920dcf2016-06-27 17:16:5914#include "base/run_loop.h"
jrummell@chromium.org47b37a62013-07-10 03:56:1015#include "media/base/audio_buffer.h"
xhwang@chromium.org97a9ce42012-10-19 10:06:4316#include "media/base/decoder_buffer.h"
17#include "media/base/decrypt_config.h"
xhwang@chromium.org8b10f2222012-11-13 05:49:4818#include "media/base/gmock_callback_support.h"
chcunningham9812dd82015-10-20 01:42:0919#include "media/base/media_util.h"
xhwang@chromium.org97a9ce42012-10-19 10:06:4320#include "media/base/mock_filters.h"
scherkus@chromium.org1848da02012-12-06 06:58:3821#include "media/base/test_helpers.h"
watk9f9dfdc92015-09-04 21:33:2922#include "media/base/timestamp_constants.h"
xhwang@chromium.org97a9ce42012-10-19 10:06:4323#include "media/filters/decrypting_audio_decoder.h"
xhwang@chromium.org97a9ce42012-10-19 10:06:4324#include "testing/gmock/include/gmock/gmock.h"
25
26using ::testing::_;
27using ::testing::AtMost;
xhwang00f69db72015-11-12 07:32:3728using ::testing::Return;
xhwang@chromium.org97a9ce42012-10-19 10:06:4329using ::testing::SaveArg;
30using ::testing::StrictMock;
31
32namespace media {
33
xhwang@chromium.org5c5e9b72014-06-17 19:21:1234const int kSampleRate = 44100;
rileya@chromium.org16ecd5f2014-03-20 05:41:0735
xhwang@chromium.orgecf16912013-01-12 16:55:2236// Make sure the kFakeAudioFrameSize is a valid frame size for all audio decoder
37// configs used in this test.
xhwang@chromium.org5c5e9b72014-06-17 19:21:1238const int kFakeAudioFrameSize = 48;
Avi Drissman97785ea2015-12-19 01:11:3139const uint8_t kFakeKeyId[] = {0x4b, 0x65, 0x79, 0x20, 0x49, 0x44};
40const uint8_t kFakeIv[DecryptConfig::kDecryptionKeySize] = {0};
xhwang@chromium.org5c5e9b72014-06-17 19:21:1241const int kDecodingDelay = 3;
xhwang@chromium.org97a9ce42012-10-19 10:06:4342
43// Create a fake non-empty encrypted buffer.
44static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() {
45 const int buffer_size = 16; // Need a non-empty buffer;
46 scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size));
dcheng254c5362016-04-22 01:12:4847 buffer->set_decrypt_config(std::unique_ptr<DecryptConfig>(new DecryptConfig(
xhwang@chromium.org97a9ce42012-10-19 10:06:4348 std::string(reinterpret_cast<const char*>(kFakeKeyId),
49 arraysize(kFakeKeyId)),
50 std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)),
xhwang@chromium.org97a9ce42012-10-19 10:06:4351 std::vector<SubsampleEntry>())));
52 return buffer;
53}
54
xhwang@chromium.org97a9ce42012-10-19 10:06:4355class DecryptingAudioDecoderTest : public testing::Test {
56 public:
57 DecryptingAudioDecoderTest()
xhwang@chromium.org2b454ff82012-11-15 03:57:5858 : decoder_(new DecryptingAudioDecoder(
skyostil6f5f1402015-06-17 15:49:0959 message_loop_.task_runner(),
dalecurtis9cddc0b2017-04-19 21:23:3860 &media_log_,
jrummell74fc4f942015-03-02 22:48:2761 base::Bind(&DecryptingAudioDecoderTest::OnWaitingForDecryptionKey,
62 base::Unretained(this)))),
xhwang00f69db72015-11-12 07:32:3763 cdm_context_(new StrictMock<MockCdmContext>()),
xhwang@chromium.org97a9ce42012-10-19 10:06:4364 decryptor_(new StrictMock<MockDecryptor>()),
xhwang@chromium.org5c5e9b72014-06-17 19:21:1265 num_decrypt_and_decode_calls_(0),
66 num_frames_in_decryptor_(0),
xhwang@chromium.org97a9ce42012-10-19 10:06:4367 encrypted_buffer_(CreateFakeEncryptedBuffer()),
xhwang@chromium.orgecbb9762012-10-24 22:33:5468 decoded_frame_(NULL),
rileya@chromium.orgd70157792014-03-17 21:45:0369 decoded_frame_list_() {}
xhwang@chromium.org97a9ce42012-10-19 10:06:4370
rileya@chromium.orgb960fd02014-01-23 04:48:5471 virtual ~DecryptingAudioDecoderTest() {
xhwang@chromium.org83fb2a9e2014-07-16 23:36:4172 Destroy();
rileya@chromium.orgb960fd02014-01-23 04:48:5473 }
74
xhwang59c321f2015-06-05 23:46:5575 void InitializeAndExpectResult(const AudioDecoderConfig& config,
76 bool success) {
dalecurtis@google.come81f5f9d2014-03-29 01:32:1677 // Initialize data now that the config is known. Since the code uses
78 // invalid values (that CreateEmptyBuffer() doesn't support), tweak them
79 // just for CreateEmptyBuffer().
80 int channels = ChannelLayoutToChannelCount(config.channel_layout());
81 if (channels < 0)
82 channels = 0;
dalecurtis39a7f932016-07-19 18:34:5983 decoded_frame_ = AudioBuffer::CreateEmptyBuffer(
84 config.channel_layout(), channels, kSampleRate, kFakeAudioFrameSize,
85 kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:1086 decoded_frame_list_.push_back(decoded_frame_);
87
xhwanga935e442016-02-11 02:22:4588 decoder_->Initialize(config, cdm_context_.get(), NewExpectedBoolCB(success),
89 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
90 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:5991 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:4392 }
93
xhwanga935e442016-02-11 02:22:4594 enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR };
xhwang00f69db72015-11-12 07:32:3795
96 void SetCdmType(CdmType cdm_type) {
xhwang00f69db72015-11-12 07:32:3797 const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR;
xhwanga935e442016-02-11 02:22:4598 EXPECT_CALL(*cdm_context_, GetDecryptor())
99 .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr));
jrummell@chromium.org9ebc3b032014-08-13 04:01:23100 }
101
xhwang@chromium.org97a9ce42012-10-19 10:06:43102 void Initialize() {
xhwang00f69db72015-11-12 07:32:37103 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59104 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org97a9ce42012-10-19 10:06:43105 .Times(AtMost(1))
xhwang@chromium.org8b10f2222012-11-13 05:49:48106 .WillOnce(RunCallback<1>(true));
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02107 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
xhwang@chromium.org82d7abd2012-11-02 23:04:03108 .WillOnce(SaveArg<1>(&key_added_cb_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43109
xhwang@chromium.org7c39486c2013-01-06 15:36:09110 config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09111 CHANNEL_LAYOUT_STEREO, kSampleRate, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30112 AesCtrEncryptionScheme(), base::TimeDelta(), 0);
xhwang59c321f2015-06-05 23:46:55113 InitializeAndExpectResult(config_, true);
xhwang@chromium.org97a9ce42012-10-19 10:06:43114 }
115
rileya@chromium.orgd70157792014-03-17 21:45:03116 void Reinitialize() {
117 ReinitializeConfigChange(config_);
118 }
119
dalecurtis@google.com6fb86542014-04-18 19:58:13120 void ReinitializeConfigChange(const AudioDecoderConfig& new_config) {
rileya@chromium.orgd70157792014-03-17 21:45:03121 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
122 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
123 .WillOnce(RunCallback<1>(true));
124 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
125 .WillOnce(SaveArg<1>(&key_added_cb_));
xhwangb2827d22017-02-24 22:57:33126 decoder_->Initialize(new_config, cdm_context_.get(),
127 NewExpectedBoolCB(true),
sergeyu@chromium.org49cea862014-06-11 11:11:50128 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
129 base::Unretained(this)));
rileya@chromium.orgd70157792014-03-17 21:45:03130 }
131
xhwang@chromium.org5c5e9b72014-06-17 19:21:12132 // Decode |buffer| and expect DecodeDone to get called with |status|.
133 void DecodeAndExpect(const scoped_refptr<DecoderBuffer>& buffer,
dalecurtis8f75b992016-03-30 17:54:59134 DecodeStatus status) {
sergeyu@chromium.org49cea862014-06-11 11:11:50135 EXPECT_CALL(*this, DecodeDone(status));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12136 decoder_->Decode(buffer,
sergeyu@chromium.org49cea862014-06-11 11:11:50137 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03138 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59139 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43140 }
141
xhwang@chromium.org5c5e9b72014-06-17 19:21:12142 // Helper function to simulate the decrypting and decoding process in the
143 // |decryptor_| with a decoding delay of kDecodingDelay buffers.
144 void DecryptAndDecodeAudio(const scoped_refptr<DecoderBuffer>& encrypted,
145 const Decryptor::AudioDecodeCB& audio_decode_cb) {
146 num_decrypt_and_decode_calls_++;
147 if (!encrypted->end_of_stream())
148 num_frames_in_decryptor_++;
149
150 if (num_decrypt_and_decode_calls_ <= kDecodingDelay ||
151 num_frames_in_decryptor_ == 0) {
anujk.sharmaf1cb600a2015-01-07 19:11:13152 audio_decode_cb.Run(Decryptor::kNeedMoreData, Decryptor::AudioFrames());
xhwang@chromium.org5c5e9b72014-06-17 19:21:12153 return;
154 }
155
156 num_frames_in_decryptor_--;
157 audio_decode_cb.Run(Decryptor::kSuccess,
anujk.sharmaf1cb600a2015-01-07 19:11:13158 Decryptor::AudioFrames(1, decoded_frame_));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12159 }
160
xhwang@chromium.org97a9ce42012-10-19 10:06:43161 // Sets up expectations and actions to put DecryptingAudioDecoder in an
162 // active normal decoding state.
163 void EnterNormalDecodingState() {
xhwang@chromium.org5c5e9b72014-06-17 19:21:12164 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _)).WillRepeatedly(
165 Invoke(this, &DecryptingAudioDecoderTest::DecryptAndDecodeAudio));
166 EXPECT_CALL(*this, FrameReady(decoded_frame_));
167 for (int i = 0; i < kDecodingDelay + 1; ++i)
dalecurtis8f75b992016-03-30 17:54:59168 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43169 }
170
171 // Sets up expectations and actions to put DecryptingAudioDecoder in an end
172 // of stream state. This function must be called after
173 // EnterNormalDecodingState() to work.
174 void EnterEndOfStreamState() {
xhwang@chromium.orgff757032014-06-19 01:42:24175 // The codec in the |decryptor_| will be flushed.
xhwang@chromium.org5c5e9b72014-06-17 19:21:12176 EXPECT_CALL(*this, FrameReady(decoded_frame_))
177 .Times(kDecodingDelay);
dalecurtis8f75b992016-03-30 17:54:59178 DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), DecodeStatus::OK);
xhwang@chromium.org5c5e9b72014-06-17 19:21:12179 EXPECT_EQ(0, num_frames_in_decryptor_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43180 }
181
182 // Make the audio decode callback pending by saving and not firing it.
183 void EnterPendingDecodeState() {
184 EXPECT_TRUE(pending_audio_decode_cb_.is_null());
xhwang@chromium.org97a9ce42012-10-19 10:06:43185 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
186 .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
187
rileya@chromium.orgd70157792014-03-17 21:45:03188 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50189 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03190 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59191 base::RunLoop().RunUntilIdle();
rileya@chromium.orgd70157792014-03-17 21:45:03192 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on
193 // the decryptor.
xhwang@chromium.org97a9ce42012-10-19 10:06:43194 EXPECT_FALSE(pending_audio_decode_cb_.is_null());
195 }
196
197 void EnterWaitingForKeyState() {
xhwang@chromium.org2b454ff82012-11-15 03:57:58198 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48199 .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey,
anujk.sharmaf1cb600a2015-01-07 19:11:13200 Decryptor::AudioFrames()));
jrummell74fc4f942015-03-02 22:48:27201 EXPECT_CALL(*this, OnWaitingForDecryptionKey());
rileya@chromium.orgd70157792014-03-17 21:45:03202 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50203 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03204 base::Unretained(this)));
Xiaohan Wang689cd4282017-08-04 07:54:55205
fdorayf920dcf2016-06-27 17:16:59206 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43207 }
208
209 void AbortPendingAudioDecodeCB() {
210 if (!pending_audio_decode_cb_.is_null()) {
211 base::ResetAndReturn(&pending_audio_decode_cb_).Run(
anujk.sharmaf1cb600a2015-01-07 19:11:13212 Decryptor::kSuccess, Decryptor::AudioFrames());
xhwang@chromium.org97a9ce42012-10-19 10:06:43213 }
214 }
215
rileya@chromium.orgb960fd02014-01-23 04:48:54216 void AbortAllPendingCBs() {
217 if (!pending_init_cb_.is_null()) {
218 ASSERT_TRUE(pending_audio_decode_cb_.is_null());
219 base::ResetAndReturn(&pending_init_cb_).Run(false);
220 return;
221 }
222
223 AbortPendingAudioDecodeCB();
224 }
225
xhwang@chromium.org97a9ce42012-10-19 10:06:43226 void Reset() {
227 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
228 .WillRepeatedly(InvokeWithoutArgs(
229 this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
230
231 decoder_->Reset(NewExpectedClosure());
fdorayf920dcf2016-06-27 17:16:59232 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43233 }
234
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41235 void Destroy() {
rileya@chromium.orgb960fd02014-01-23 04:48:54236 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
237 .WillRepeatedly(InvokeWithoutArgs(
238 this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
239
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41240 decoder_.reset();
fdorayf920dcf2016-06-27 17:16:59241 base::RunLoop().RunUntilIdle();
rileya@chromium.orgb960fd02014-01-23 04:48:54242 }
243
sergeyu@chromium.org49cea862014-06-11 11:11:50244 MOCK_METHOD1(FrameReady, void(const scoped_refptr<AudioBuffer>&));
dalecurtis8f75b992016-03-30 17:54:59245 MOCK_METHOD1(DecodeDone, void(DecodeStatus));
xhwang@chromium.org97a9ce42012-10-19 10:06:43246
jrummell74fc4f942015-03-02 22:48:27247 MOCK_METHOD0(OnWaitingForDecryptionKey, void(void));
248
xhwang@chromium.orgfb5af232013-04-22 22:40:03249 base::MessageLoop message_loop_;
dalecurtis9cddc0b2017-04-19 21:23:38250 MediaLog media_log_;
dcheng254c5362016-04-22 01:12:48251 std::unique_ptr<DecryptingAudioDecoder> decoder_;
252 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
253 std::unique_ptr<StrictMock<MockDecryptor>> decryptor_;
xhwang@chromium.org7c39486c2013-01-06 15:36:09254 AudioDecoderConfig config_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43255
xhwang@chromium.org5c5e9b72014-06-17 19:21:12256 // 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.org97a9ce42012-10-19 10:06:43260 Decryptor::DecoderInitCB pending_init_cb_;
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02261 Decryptor::NewKeyCB key_added_cb_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43262 Decryptor::AudioDecodeCB pending_audio_decode_cb_;
263
rileya@chromium.orgd70157792014-03-17 21:45:03264 // Constant buffer/frames, to be used/returned by |decoder_| and |decryptor_|.
xhwang@chromium.org97a9ce42012-10-19 10:06:43265 scoped_refptr<DecoderBuffer> encrypted_buffer_;
jrummell@chromium.org47b37a62013-07-10 03:56:10266 scoped_refptr<AudioBuffer> decoded_frame_;
anujk.sharmaf1cb600a2015-01-07 19:11:13267 Decryptor::AudioFrames decoded_frame_list_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43268
269 private:
270 DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
271};
272
273TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
274 Initialize();
275}
276
xhwang@chromium.org97a9ce42012-10-19 10:06:43277// Ensure decoder handles invalid audio configs without crashing.
278TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
dalecurtis@google.comb5eca3c2013-01-04 20:20:15279 AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
dougsteed8d5275f2016-03-12 00:04:30280 CHANNEL_LAYOUT_STEREO, 0, EmptyExtraData(),
281 AesCtrEncryptionScheme());
xhwang@chromium.org97a9ce42012-10-19 10:06:43282
xhwang59c321f2015-06-05 23:46:55283 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43284}
285
286// Ensure decoder handles unsupported audio configs without crashing.
287TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
xhwang00f69db72015-11-12 07:32:37288 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59289 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48290 .WillOnce(RunCallback<1>(false));
xhwang@chromium.org97a9ce42012-10-19 10:06:43291
dalecurtis@google.comb5eca3c2013-01-04 20:20:15292 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09293 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30294 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55295 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43296}
297
xhwang00f69db72015-11-12 07:32:37298TEST_F(DecryptingAudioDecoderTest, Initialize_CdmWithoutDecryptor) {
299 SetCdmType(CDM_WITHOUT_DECRYPTOR);
xhwang@chromium.org699b64a2013-06-19 07:36:05300 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09301 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30302 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55303 InitializeAndExpectResult(config, false);
xhwang@chromium.org699b64a2013-06-19 07:36:05304}
305
xhwang@chromium.org97a9ce42012-10-19 10:06:43306// Test normal decrypt and decode case.
307TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
308 Initialize();
309 EnterNormalDecodingState();
310}
311
312// Test the case where the decryptor returns error when doing decrypt and
313// decode.
314TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
315 Initialize();
316
xhwang@chromium.org97a9ce42012-10-19 10:06:43317 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48318 .WillRepeatedly(RunCallback<1>(Decryptor::kError,
anujk.sharmaf1cb600a2015-01-07 19:11:13319 Decryptor::AudioFrames()));
xhwang@chromium.org97a9ce42012-10-19 10:06:43320
dalecurtis8f75b992016-03-30 17:54:59321 DecodeAndExpect(encrypted_buffer_, DecodeStatus::DECODE_ERROR);
xhwang@chromium.org97a9ce42012-10-19 10:06:43322}
323
324// Test the case where the decryptor returns multiple decoded frames.
325TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
326 Initialize();
327
jrummell@chromium.org47b37a62013-07-10 03:56:10328 scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07329 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59330 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
331 kFakeAudioFrameSize, kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:10332 scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07333 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59334 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
335 kFakeAudioFrameSize, kNoTimestamp);
xhwang@chromium.orgecbb9762012-10-24 22:33:54336 decoded_frame_list_.push_back(frame_a);
337 decoded_frame_list_.push_back(frame_b);
xhwang@chromium.org97a9ce42012-10-19 10:06:43338
xhwang@chromium.org97a9ce42012-10-19 10:06:43339 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48340 .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43341
xhwang@chromium.org5c5e9b72014-06-17 19:21:12342 EXPECT_CALL(*this, FrameReady(decoded_frame_));
343 EXPECT_CALL(*this, FrameReady(frame_a));
344 EXPECT_CALL(*this, FrameReady(frame_b));
dalecurtis8f75b992016-03-30 17:54:59345 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43346}
347
348// Test the case where the decryptor receives end-of-stream buffer.
349TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
350 Initialize();
351 EnterNormalDecodingState();
352 EnterEndOfStreamState();
353}
354
xhwang65c23032017-02-17 04:37:42355// Test reinitializing decode with a new encrypted config.
356TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToEncrypted) {
xhwang@chromium.org7c39486c2013-01-06 15:36:09357 Initialize();
358
rileya@chromium.orgd70157792014-03-17 21:45:03359 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
360 .Times(AtMost(1))
361 .WillOnce(RunCallback<1>(true));
xhwang@chromium.org7c39486c2013-01-06 15:36:09362
xhwang@chromium.orgecf16912013-01-12 16:55:22363 // The new config is different from the initial config in bits-per-channel,
364 // channel layout and samples_per_second.
365 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
chcunningham9812dd82015-10-20 01:42:09366 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30367 AesCtrEncryptionScheme());
xhwang@chromium.orgecf16912013-01-12 16:55:22368 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
369 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
370 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
xhwang65c23032017-02-17 04:37:42371 ASSERT_TRUE(new_config.is_encrypted());
372
373 ReinitializeConfigChange(new_config);
374 base::RunLoop().RunUntilIdle();
375}
376
377// Test reinitializing decode with a new clear config.
378TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToClear) {
379 Initialize();
380
381 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
382 .Times(AtMost(1))
383 .WillOnce(RunCallback<1>(true));
384
385 // The new config is different from the initial config in bits-per-channel,
386 // channel layout and samples_per_second.
387 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
388 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
389 Unencrypted());
390 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
391 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
392 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
393 ASSERT_FALSE(new_config.is_encrypted());
xhwang@chromium.orgecf16912013-01-12 16:55:22394
rileya@chromium.orgd70157792014-03-17 21:45:03395 ReinitializeConfigChange(new_config);
fdorayf920dcf2016-06-27 17:16:59396 base::RunLoop().RunUntilIdle();
xhwang@chromium.org7c39486c2013-01-06 15:36:09397}
398
xhwang@chromium.org97a9ce42012-10-19 10:06:43399// Test the case where the a key is added when the decryptor is in
400// kWaitingForKey state.
401TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
402 Initialize();
403 EnterWaitingForKeyState();
404
405 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48406 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50407 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59408 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43409 key_added_cb_.Run();
fdorayf920dcf2016-06-27 17:16:59410 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43411}
412
413// Test the case where the a key is added when the decryptor is in
414// kPendingDecode state.
415TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
416 Initialize();
417 EnterPendingDecodeState();
418
419 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48420 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50421 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59422 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43423 // The audio decode callback is returned after the correct decryption key is
424 // added.
425 key_added_cb_.Run();
426 base::ResetAndReturn(&pending_audio_decode_cb_).Run(
anujk.sharmaf1cb600a2015-01-07 19:11:13427 Decryptor::kNoKey, Decryptor::AudioFrames());
fdorayf920dcf2016-06-27 17:16:59428 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43429}
430
431// Test resetting when the decoder is in kIdle state but has not decoded any
432// frame.
433TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
434 Initialize();
435 Reset();
436}
437
438// Test resetting when the decoder is in kIdle state after it has decoded one
439// frame.
440TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
441 Initialize();
442 EnterNormalDecodingState();
443 Reset();
444}
445
xhwang@chromium.org97a9ce42012-10-19 10:06:43446// Test resetting when the decoder is in kPendingDecode state.
447TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
448 Initialize();
449 EnterPendingDecodeState();
450
dalecurtis8f75b992016-03-30 17:54:59451 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43452
453 Reset();
454}
455
456// Test resetting when the decoder is in kWaitingForKey state.
457TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
458 Initialize();
459 EnterWaitingForKeyState();
460
dalecurtis8f75b992016-03-30 17:54:59461 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43462
463 Reset();
464}
465
466// Test resetting when the decoder has hit end of stream and is in
467// kDecodeFinished state.
468TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
469 Initialize();
470 EnterNormalDecodingState();
471 EnterEndOfStreamState();
472 Reset();
473}
474
475// Test resetting after the decoder has been reset.
476TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
477 Initialize();
478 EnterNormalDecodingState();
479 Reset();
480 Reset();
481}
482
xhwang@chromium.org97a9ce42012-10-19 10:06:43483} // namespace media