[go: nahoru, domu]

blob: 39d394a4faafd0995521124eb0c8f85511ad25cf [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));
John Rummell9044a762018-04-19 01:24:0747 buffer->set_decrypt_config(DecryptConfig::CreateCencConfig(
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)),
John Rummell9044a762018-04-19 01:24:0751 std::vector<SubsampleEntry>()));
xhwang@chromium.org97a9ce42012-10-19 10:06:4352 return buffer;
53}
54
xhwang@chromium.org97a9ce42012-10-19 10:06:4355class DecryptingAudioDecoderTest : public testing::Test {
56 public:
57 DecryptingAudioDecoderTest()
Dale Curtis71d130e52018-03-09 06:05:1558 : decoder_(new DecryptingAudioDecoder(message_loop_.task_runner(),
59 &media_log_)),
xhwang00f69db72015-11-12 07:32:3760 cdm_context_(new StrictMock<MockCdmContext>()),
xhwang@chromium.org97a9ce42012-10-19 10:06:4361 decryptor_(new StrictMock<MockDecryptor>()),
xhwang@chromium.org5c5e9b72014-06-17 19:21:1262 num_decrypt_and_decode_calls_(0),
63 num_frames_in_decryptor_(0),
xhwang@chromium.org97a9ce42012-10-19 10:06:4364 encrypted_buffer_(CreateFakeEncryptedBuffer()),
xhwang@chromium.orgecbb9762012-10-24 22:33:5465 decoded_frame_(NULL),
rileya@chromium.orgd70157792014-03-17 21:45:0366 decoded_frame_list_() {}
xhwang@chromium.org97a9ce42012-10-19 10:06:4367
Daniel Cheng9f0b7012018-04-26 21:09:0568 ~DecryptingAudioDecoderTest() override { Destroy(); }
rileya@chromium.orgb960fd02014-01-23 04:48:5469
xhwang59c321f2015-06-05 23:46:5570 void InitializeAndExpectResult(const AudioDecoderConfig& config,
71 bool success) {
dalecurtis@google.come81f5f9d2014-03-29 01:32:1672 // Initialize data now that the config is known. Since the code uses
73 // invalid values (that CreateEmptyBuffer() doesn't support), tweak them
74 // just for CreateEmptyBuffer().
75 int channels = ChannelLayoutToChannelCount(config.channel_layout());
76 if (channels < 0)
77 channels = 0;
dalecurtis39a7f932016-07-19 18:34:5978 decoded_frame_ = AudioBuffer::CreateEmptyBuffer(
79 config.channel_layout(), channels, kSampleRate, kFakeAudioFrameSize,
80 kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:1081 decoded_frame_list_.push_back(decoded_frame_);
82
Dale Curtis71d130e52018-03-09 06:05:1583 decoder_->Initialize(
84 config, cdm_context_.get(), NewExpectedBoolCB(success),
85 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
86 base::Unretained(this)),
87 base::Bind(&DecryptingAudioDecoderTest::OnWaitingForDecryptionKey,
88 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:5989 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:4390 }
91
xhwanga935e442016-02-11 02:22:4592 enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR };
xhwang00f69db72015-11-12 07:32:3793
94 void SetCdmType(CdmType cdm_type) {
xhwang00f69db72015-11-12 07:32:3795 const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR;
xhwanga935e442016-02-11 02:22:4596 EXPECT_CALL(*cdm_context_, GetDecryptor())
97 .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr));
jrummell@chromium.org9ebc3b032014-08-13 04:01:2398 }
99
xhwang@chromium.org97a9ce42012-10-19 10:06:43100 void Initialize() {
xhwang00f69db72015-11-12 07:32:37101 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59102 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org97a9ce42012-10-19 10:06:43103 .Times(AtMost(1))
xhwang@chromium.org8b10f2222012-11-13 05:49:48104 .WillOnce(RunCallback<1>(true));
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02105 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
xhwang@chromium.org82d7abd2012-11-02 23:04:03106 .WillOnce(SaveArg<1>(&key_added_cb_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43107
xhwang@chromium.org7c39486c2013-01-06 15:36:09108 config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09109 CHANNEL_LAYOUT_STEREO, kSampleRate, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30110 AesCtrEncryptionScheme(), base::TimeDelta(), 0);
xhwang59c321f2015-06-05 23:46:55111 InitializeAndExpectResult(config_, true);
xhwang@chromium.org97a9ce42012-10-19 10:06:43112 }
113
Xiaohan Wangc03ce232018-05-30 21:07:58114 void Reinitialize() { ReinitializeConfigChange(config_); }
rileya@chromium.orgd70157792014-03-17 21:45:03115
dalecurtis@google.com6fb86542014-04-18 19:58:13116 void ReinitializeConfigChange(const AudioDecoderConfig& new_config) {
rileya@chromium.orgd70157792014-03-17 21:45:03117 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
118 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
119 .WillOnce(RunCallback<1>(true));
120 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
Xiaohan Wangc03ce232018-05-30 21:07:58121 .WillOnce(SaveArg<1>(&key_added_cb_));
Dale Curtis71d130e52018-03-09 06:05:15122 decoder_->Initialize(
123 new_config, cdm_context_.get(), NewExpectedBoolCB(true),
124 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
125 base::Unretained(this)),
126 base::Bind(&DecryptingAudioDecoderTest::OnWaitingForDecryptionKey,
127 base::Unretained(this)));
rileya@chromium.orgd70157792014-03-17 21:45:03128 }
129
xhwang@chromium.org5c5e9b72014-06-17 19:21:12130 // Decode |buffer| and expect DecodeDone to get called with |status|.
Dale Curtisca8ad982018-04-09 20:23:14131 void DecodeAndExpect(scoped_refptr<DecoderBuffer> buffer,
dalecurtis8f75b992016-03-30 17:54:59132 DecodeStatus status) {
sergeyu@chromium.org49cea862014-06-11 11:11:50133 EXPECT_CALL(*this, DecodeDone(status));
Xiaohan Wangc03ce232018-05-30 21:07:58134 decoder_->Decode(buffer, base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
135 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59136 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43137 }
138
xhwang@chromium.org5c5e9b72014-06-17 19:21:12139 // Helper function to simulate the decrypting and decoding process in the
140 // |decryptor_| with a decoding delay of kDecodingDelay buffers.
Dale Curtisca8ad982018-04-09 20:23:14141 void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
xhwang@chromium.org5c5e9b72014-06-17 19:21:12142 const Decryptor::AudioDecodeCB& audio_decode_cb) {
143 num_decrypt_and_decode_calls_++;
144 if (!encrypted->end_of_stream())
145 num_frames_in_decryptor_++;
146
147 if (num_decrypt_and_decode_calls_ <= kDecodingDelay ||
148 num_frames_in_decryptor_ == 0) {
anujk.sharmaf1cb600a2015-01-07 19:11:13149 audio_decode_cb.Run(Decryptor::kNeedMoreData, Decryptor::AudioFrames());
xhwang@chromium.org5c5e9b72014-06-17 19:21:12150 return;
151 }
152
153 num_frames_in_decryptor_--;
154 audio_decode_cb.Run(Decryptor::kSuccess,
anujk.sharmaf1cb600a2015-01-07 19:11:13155 Decryptor::AudioFrames(1, decoded_frame_));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12156 }
157
xhwang@chromium.org97a9ce42012-10-19 10:06:43158 // Sets up expectations and actions to put DecryptingAudioDecoder in an
159 // active normal decoding state.
160 void EnterNormalDecodingState() {
Xiaohan Wangc03ce232018-05-30 21:07:58161 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
162 .WillRepeatedly(
163 Invoke(this, &DecryptingAudioDecoderTest::DecryptAndDecodeAudio));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12164 EXPECT_CALL(*this, FrameReady(decoded_frame_));
165 for (int i = 0; i < kDecodingDelay + 1; ++i)
dalecurtis8f75b992016-03-30 17:54:59166 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43167 }
168
169 // Sets up expectations and actions to put DecryptingAudioDecoder in an end
170 // of stream state. This function must be called after
171 // EnterNormalDecodingState() to work.
172 void EnterEndOfStreamState() {
xhwang@chromium.orgff757032014-06-19 01:42:24173 // The codec in the |decryptor_| will be flushed.
Xiaohan Wangc03ce232018-05-30 21:07:58174 EXPECT_CALL(*this, FrameReady(decoded_frame_)).Times(kDecodingDelay);
dalecurtis8f75b992016-03-30 17:54:59175 DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), DecodeStatus::OK);
xhwang@chromium.org5c5e9b72014-06-17 19:21:12176 EXPECT_EQ(0, num_frames_in_decryptor_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43177 }
178
179 // Make the audio decode callback pending by saving and not firing it.
180 void EnterPendingDecodeState() {
Dale Curtise25163812018-09-21 22:13:39181 EXPECT_TRUE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43182 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
183 .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
184
rileya@chromium.orgd70157792014-03-17 21:45:03185 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50186 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03187 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59188 base::RunLoop().RunUntilIdle();
rileya@chromium.orgd70157792014-03-17 21:45:03189 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on
190 // the decryptor.
Dale Curtise25163812018-09-21 22:13:39191 EXPECT_FALSE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43192 }
193
194 void EnterWaitingForKeyState() {
xhwang@chromium.org2b454ff82012-11-15 03:57:58195 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58196 .WillRepeatedly(
197 RunCallback<1>(Decryptor::kNoKey, Decryptor::AudioFrames()));
jrummell74fc4f942015-03-02 22:48:27198 EXPECT_CALL(*this, OnWaitingForDecryptionKey());
rileya@chromium.orgd70157792014-03-17 21:45:03199 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50200 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03201 base::Unretained(this)));
Xiaohan Wang689cd4282017-08-04 07:54:55202
fdorayf920dcf2016-06-27 17:16:59203 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43204 }
205
206 void AbortPendingAudioDecodeCB() {
Dale Curtise25163812018-09-21 22:13:39207 if (pending_audio_decode_cb_) {
208 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58209 .Run(Decryptor::kSuccess, Decryptor::AudioFrames());
xhwang@chromium.org97a9ce42012-10-19 10:06:43210 }
211 }
212
rileya@chromium.orgb960fd02014-01-23 04:48:54213 void AbortAllPendingCBs() {
Dale Curtise25163812018-09-21 22:13:39214 if (pending_init_cb_) {
215 ASSERT_TRUE(!pending_audio_decode_cb_);
216 std::move(pending_init_cb_).Run(false);
rileya@chromium.orgb960fd02014-01-23 04:48:54217 return;
218 }
219
220 AbortPendingAudioDecodeCB();
221 }
222
xhwang@chromium.org97a9ce42012-10-19 10:06:43223 void Reset() {
224 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
225 .WillRepeatedly(InvokeWithoutArgs(
226 this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
227
228 decoder_->Reset(NewExpectedClosure());
fdorayf920dcf2016-06-27 17:16:59229 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43230 }
231
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41232 void Destroy() {
rileya@chromium.orgb960fd02014-01-23 04:48:54233 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
234 .WillRepeatedly(InvokeWithoutArgs(
235 this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
236
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41237 decoder_.reset();
fdorayf920dcf2016-06-27 17:16:59238 base::RunLoop().RunUntilIdle();
rileya@chromium.orgb960fd02014-01-23 04:48:54239 }
240
sergeyu@chromium.org49cea862014-06-11 11:11:50241 MOCK_METHOD1(FrameReady, void(const scoped_refptr<AudioBuffer>&));
dalecurtis8f75b992016-03-30 17:54:59242 MOCK_METHOD1(DecodeDone, void(DecodeStatus));
xhwang@chromium.org97a9ce42012-10-19 10:06:43243
jrummell74fc4f942015-03-02 22:48:27244 MOCK_METHOD0(OnWaitingForDecryptionKey, void(void));
245
xhwang@chromium.orgfb5af232013-04-22 22:40:03246 base::MessageLoop message_loop_;
dalecurtis9cddc0b2017-04-19 21:23:38247 MediaLog media_log_;
dcheng254c5362016-04-22 01:12:48248 std::unique_ptr<DecryptingAudioDecoder> decoder_;
249 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
250 std::unique_ptr<StrictMock<MockDecryptor>> decryptor_;
xhwang@chromium.org7c39486c2013-01-06 15:36:09251 AudioDecoderConfig config_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43252
xhwang@chromium.org5c5e9b72014-06-17 19:21:12253 // Variables to help the |decryptor_| to simulate decoding delay and flushing.
254 int num_decrypt_and_decode_calls_;
255 int num_frames_in_decryptor_;
256
xhwang@chromium.org97a9ce42012-10-19 10:06:43257 Decryptor::DecoderInitCB pending_init_cb_;
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02258 Decryptor::NewKeyCB key_added_cb_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43259 Decryptor::AudioDecodeCB pending_audio_decode_cb_;
260
rileya@chromium.orgd70157792014-03-17 21:45:03261 // Constant buffer/frames, to be used/returned by |decoder_| and |decryptor_|.
xhwang@chromium.org97a9ce42012-10-19 10:06:43262 scoped_refptr<DecoderBuffer> encrypted_buffer_;
jrummell@chromium.org47b37a62013-07-10 03:56:10263 scoped_refptr<AudioBuffer> decoded_frame_;
anujk.sharmaf1cb600a2015-01-07 19:11:13264 Decryptor::AudioFrames decoded_frame_list_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43265
266 private:
267 DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
268};
269
270TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
271 Initialize();
272}
273
xhwang@chromium.org97a9ce42012-10-19 10:06:43274// Ensure decoder handles invalid audio configs without crashing.
275TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
dalecurtis@google.comb5eca3c2013-01-04 20:20:15276 AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
dougsteed8d5275f2016-03-12 00:04:30277 CHANNEL_LAYOUT_STEREO, 0, EmptyExtraData(),
278 AesCtrEncryptionScheme());
xhwang@chromium.org97a9ce42012-10-19 10:06:43279
xhwang59c321f2015-06-05 23:46:55280 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43281}
282
283// Ensure decoder handles unsupported audio configs without crashing.
284TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
xhwang00f69db72015-11-12 07:32:37285 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59286 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48287 .WillOnce(RunCallback<1>(false));
xhwang@chromium.org97a9ce42012-10-19 10:06:43288
dalecurtis@google.comb5eca3c2013-01-04 20:20:15289 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09290 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30291 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55292 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43293}
294
xhwang00f69db72015-11-12 07:32:37295TEST_F(DecryptingAudioDecoderTest, Initialize_CdmWithoutDecryptor) {
296 SetCdmType(CDM_WITHOUT_DECRYPTOR);
xhwang@chromium.org699b64a2013-06-19 07:36:05297 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09298 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30299 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55300 InitializeAndExpectResult(config, false);
xhwang@chromium.org699b64a2013-06-19 07:36:05301}
302
xhwang@chromium.org97a9ce42012-10-19 10:06:43303// Test normal decrypt and decode case.
304TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
305 Initialize();
306 EnterNormalDecodingState();
307}
308
309// Test the case where the decryptor returns error when doing decrypt and
310// decode.
311TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
312 Initialize();
313
xhwang@chromium.org97a9ce42012-10-19 10:06:43314 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58315 .WillRepeatedly(
316 RunCallback<1>(Decryptor::kError, Decryptor::AudioFrames()));
xhwang@chromium.org97a9ce42012-10-19 10:06:43317
dalecurtis8f75b992016-03-30 17:54:59318 DecodeAndExpect(encrypted_buffer_, DecodeStatus::DECODE_ERROR);
xhwang@chromium.org97a9ce42012-10-19 10:06:43319}
320
321// Test the case where the decryptor returns multiple decoded frames.
322TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
323 Initialize();
324
jrummell@chromium.org47b37a62013-07-10 03:56:10325 scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07326 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59327 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
328 kFakeAudioFrameSize, kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:10329 scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07330 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59331 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
332 kFakeAudioFrameSize, kNoTimestamp);
xhwang@chromium.orgecbb9762012-10-24 22:33:54333 decoded_frame_list_.push_back(frame_a);
334 decoded_frame_list_.push_back(frame_b);
xhwang@chromium.org97a9ce42012-10-19 10:06:43335
xhwang@chromium.org97a9ce42012-10-19 10:06:43336 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48337 .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43338
xhwang@chromium.org5c5e9b72014-06-17 19:21:12339 EXPECT_CALL(*this, FrameReady(decoded_frame_));
340 EXPECT_CALL(*this, FrameReady(frame_a));
341 EXPECT_CALL(*this, FrameReady(frame_b));
dalecurtis8f75b992016-03-30 17:54:59342 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43343}
344
345// Test the case where the decryptor receives end-of-stream buffer.
346TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
347 Initialize();
348 EnterNormalDecodingState();
349 EnterEndOfStreamState();
350}
351
xhwang65c23032017-02-17 04:37:42352// Test reinitializing decode with a new encrypted config.
353TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToEncrypted) {
xhwang@chromium.org7c39486c2013-01-06 15:36:09354 Initialize();
355
rileya@chromium.orgd70157792014-03-17 21:45:03356 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
357 .Times(AtMost(1))
358 .WillOnce(RunCallback<1>(true));
xhwang@chromium.org7c39486c2013-01-06 15:36:09359
xhwang@chromium.orgecf16912013-01-12 16:55:22360 // The new config is different from the initial config in bits-per-channel,
361 // channel layout and samples_per_second.
362 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
chcunningham9812dd82015-10-20 01:42:09363 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30364 AesCtrEncryptionScheme());
xhwang@chromium.orgecf16912013-01-12 16:55:22365 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
366 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
367 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
xhwang65c23032017-02-17 04:37:42368 ASSERT_TRUE(new_config.is_encrypted());
369
370 ReinitializeConfigChange(new_config);
371 base::RunLoop().RunUntilIdle();
372}
373
374// Test reinitializing decode with a new clear config.
375TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToClear) {
376 Initialize();
377
378 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
379 .Times(AtMost(1))
380 .WillOnce(RunCallback<1>(true));
381
382 // The new config is different from the initial config in bits-per-channel,
383 // channel layout and samples_per_second.
384 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
385 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
386 Unencrypted());
387 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
388 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
389 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
390 ASSERT_FALSE(new_config.is_encrypted());
xhwang@chromium.orgecf16912013-01-12 16:55:22391
rileya@chromium.orgd70157792014-03-17 21:45:03392 ReinitializeConfigChange(new_config);
fdorayf920dcf2016-06-27 17:16:59393 base::RunLoop().RunUntilIdle();
xhwang@chromium.org7c39486c2013-01-06 15:36:09394}
395
xhwang@chromium.org97a9ce42012-10-19 10:06:43396// Test the case where the a key is added when the decryptor is in
397// kWaitingForKey state.
398TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
399 Initialize();
400 EnterWaitingForKeyState();
401
402 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48403 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50404 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59405 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43406 key_added_cb_.Run();
fdorayf920dcf2016-06-27 17:16:59407 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43408}
409
410// Test the case where the a key is added when the decryptor is in
411// kPendingDecode state.
412TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
413 Initialize();
414 EnterPendingDecodeState();
415
416 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48417 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50418 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59419 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43420 // The audio decode callback is returned after the correct decryption key is
421 // added.
422 key_added_cb_.Run();
Dale Curtise25163812018-09-21 22:13:39423 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58424 .Run(Decryptor::kNoKey, Decryptor::AudioFrames());
fdorayf920dcf2016-06-27 17:16:59425 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43426}
427
428// Test resetting when the decoder is in kIdle state but has not decoded any
429// frame.
430TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
431 Initialize();
432 Reset();
433}
434
435// Test resetting when the decoder is in kIdle state after it has decoded one
436// frame.
437TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
438 Initialize();
439 EnterNormalDecodingState();
440 Reset();
441}
442
xhwang@chromium.org97a9ce42012-10-19 10:06:43443// Test resetting when the decoder is in kPendingDecode state.
444TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
445 Initialize();
446 EnterPendingDecodeState();
447
dalecurtis8f75b992016-03-30 17:54:59448 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43449
450 Reset();
451}
452
453// Test resetting when the decoder is in kWaitingForKey state.
454TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
455 Initialize();
456 EnterWaitingForKeyState();
457
dalecurtis8f75b992016-03-30 17:54:59458 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43459
460 Reset();
461}
462
463// Test resetting when the decoder has hit end of stream and is in
464// kDecodeFinished state.
465TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
466 Initialize();
467 EnterNormalDecodingState();
468 EnterEndOfStreamState();
469 Reset();
470}
471
472// Test resetting after the decoder has been reset.
473TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
474 Initialize();
475 EnterNormalDecodingState();
476 Reset();
477 Reset();
478}
479
xhwang@chromium.org97a9ce42012-10-19 10:06:43480} // namespace media