[go: nahoru, domu]

blob: 25717caa4b37763e14bef750b8d72a2eed994043 [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"
fdorayf920dcf2016-06-27 17:16:5912#include "base/run_loop.h"
Avi Drissmanfa9bed42018-12-25 20:26:3913#include "base/stl_util.h"
Marijn Kruisselbrink52a35242019-06-05 22:58:2914#include "base/test/gmock_callback_support.h"
Gabriel Charettec7108742019-08-23 03:31:4015#include "base/test/task_environment.h"
jrummell@chromium.org47b37a62013-07-10 03:56:1016#include "media/base/audio_buffer.h"
xhwang@chromium.org97a9ce42012-10-19 10:06:4317#include "media/base/decoder_buffer.h"
18#include "media/base/decrypt_config.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
Marijn Kruisselbrink52a35242019-06-05 22:58:2926using ::base::test::RunCallback;
Jose Lopes109230ec2020-02-24 10:28:1227using ::base::test::RunOnceCallback;
xhwang@chromium.org97a9ce42012-10-19 10:06:4328using ::testing::_;
29using ::testing::AtMost;
xhwang00f69db72015-11-12 07:32:3730using ::testing::Return;
xhwang@chromium.org97a9ce42012-10-19 10:06:4331using ::testing::SaveArg;
32using ::testing::StrictMock;
33
34namespace media {
35
xhwang@chromium.org5c5e9b72014-06-17 19:21:1236const int kSampleRate = 44100;
rileya@chromium.org16ecd5f2014-03-20 05:41:0737
xhwang@chromium.orgecf16912013-01-12 16:55:2238// Make sure the kFakeAudioFrameSize is a valid frame size for all audio decoder
39// configs used in this test.
xhwang@chromium.org5c5e9b72014-06-17 19:21:1240const int kFakeAudioFrameSize = 48;
Avi Drissman97785ea2015-12-19 01:11:3141const uint8_t kFakeKeyId[] = {0x4b, 0x65, 0x79, 0x20, 0x49, 0x44};
42const uint8_t kFakeIv[DecryptConfig::kDecryptionKeySize] = {0};
xhwang@chromium.org5c5e9b72014-06-17 19:21:1243const int kDecodingDelay = 3;
xhwang@chromium.org97a9ce42012-10-19 10:06:4344
45// Create a fake non-empty encrypted buffer.
46static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() {
47 const int buffer_size = 16; // Need a non-empty buffer;
48 scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size));
John Rummell9044a762018-04-19 01:24:0749 buffer->set_decrypt_config(DecryptConfig::CreateCencConfig(
xhwang@chromium.org97a9ce42012-10-19 10:06:4350 std::string(reinterpret_cast<const char*>(kFakeKeyId),
Avi Drissmanfa9bed42018-12-25 20:26:3951 base::size(kFakeKeyId)),
52 std::string(reinterpret_cast<const char*>(kFakeIv), base::size(kFakeIv)),
John Rummell9044a762018-04-19 01:24:0753 std::vector<SubsampleEntry>()));
xhwang@chromium.org97a9ce42012-10-19 10:06:4354 return buffer;
55}
56
xhwang@chromium.org97a9ce42012-10-19 10:06:4357class DecryptingAudioDecoderTest : public testing::Test {
58 public:
59 DecryptingAudioDecoderTest()
Carlos Caballero9de7c722019-05-31 21:25:0760 : decoder_(new DecryptingAudioDecoder(
Gabriel Charettedfa36042019-08-19 17:30:1161 task_environment_.GetMainThreadTaskRunner(),
Carlos Caballero9de7c722019-05-31 21:25:0762 &media_log_)),
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()),
kylechar938dc442019-10-29 14:40:2468 decoded_frame_(nullptr),
rileya@chromium.orgd70157792014-03-17 21:45:0369 decoded_frame_list_() {}
xhwang@chromium.org97a9ce42012-10-19 10:06:4370
Daniel Cheng9f0b7012018-04-26 21:09:0571 ~DecryptingAudioDecoderTest() override { Destroy(); }
rileya@chromium.orgb960fd02014-01-23 04:48:5472
xhwang59c321f2015-06-05 23:46:5573 void InitializeAndExpectResult(const AudioDecoderConfig& config,
74 bool success) {
dalecurtis@google.come81f5f9d2014-03-29 01:32:1675 // Initialize data now that the config is known. Since the code uses
76 // invalid values (that CreateEmptyBuffer() doesn't support), tweak them
77 // just for CreateEmptyBuffer().
78 int channels = ChannelLayoutToChannelCount(config.channel_layout());
79 if (channels < 0)
80 channels = 0;
dalecurtis39a7f932016-07-19 18:34:5981 decoded_frame_ = AudioBuffer::CreateEmptyBuffer(
82 config.channel_layout(), channels, kSampleRate, kFakeAudioFrameSize,
83 kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:1084 decoded_frame_list_.push_back(decoded_frame_);
85
Ted Meyer2a41e242020-03-20 08:01:2786 decoder_->Initialize(config, cdm_context_.get(),
87 base::BindOnce(
88 [](bool success, Status status) {
89 EXPECT_EQ(status.is_ok(), success);
90 },
91 success),
Xiaohan Wang640b41d2018-12-18 19:00:4692 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
93 base::Unretained(this)),
94 base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
95 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:5996 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:4397 }
98
xhwanga935e442016-02-11 02:22:4599 enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR };
xhwang00f69db72015-11-12 07:32:37100
101 void SetCdmType(CdmType cdm_type) {
xhwang00f69db72015-11-12 07:32:37102 const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR;
xhwanga935e442016-02-11 02:22:45103 EXPECT_CALL(*cdm_context_, GetDecryptor())
104 .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr));
jrummell@chromium.org9ebc3b032014-08-13 04:01:23105 }
106
xhwang@chromium.org97a9ce42012-10-19 10:06:43107 void Initialize() {
xhwang00f69db72015-11-12 07:32:37108 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59109 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org97a9ce42012-10-19 10:06:43110 .Times(AtMost(1))
Jose Lopes109230ec2020-02-24 10:28:12111 .WillOnce(RunOnceCallback<1>(true));
Xiaohan Wangd72ce5d2020-07-14 01:34:43112 EXPECT_CALL(*cdm_context_, RegisterEventCB(_)).WillOnce([&](auto cb) {
113 event_cb_ = cb;
114 return std::make_unique<CallbackRegistration>();
115 });
xhwang@chromium.org97a9ce42012-10-19 10:06:43116
xhwang@chromium.org7c39486c2013-01-06 15:36:09117 config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09118 CHANNEL_LAYOUT_STEREO, kSampleRate, EmptyExtraData(),
Yuchen Liub33bfc12019-11-08 20:16:12119 EncryptionScheme::kCenc, base::TimeDelta(), 0);
xhwang59c321f2015-06-05 23:46:55120 InitializeAndExpectResult(config_, true);
xhwang@chromium.org97a9ce42012-10-19 10:06:43121 }
122
Xiaohan Wangc03ce232018-05-30 21:07:58123 void Reinitialize() { ReinitializeConfigChange(config_); }
rileya@chromium.orgd70157792014-03-17 21:45:03124
dalecurtis@google.com6fb86542014-04-18 19:58:13125 void ReinitializeConfigChange(const AudioDecoderConfig& new_config) {
rileya@chromium.orgd70157792014-03-17 21:45:03126 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
127 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
Jose Lopes109230ec2020-02-24 10:28:12128 .WillOnce(RunOnceCallback<1>(true));
Ted Meyer2a41e242020-03-20 08:01:27129 decoder_->Initialize(
130 new_config, cdm_context_.get(),
131 base::BindOnce([](Status status) { EXPECT_TRUE(status.is_ok()); }),
132 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
133 base::Unretained(this)),
134 base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
135 base::Unretained(this)));
rileya@chromium.orgd70157792014-03-17 21:45:03136 }
137
xhwang@chromium.org5c5e9b72014-06-17 19:21:12138 // Decode |buffer| and expect DecodeDone to get called with |status|.
Dale Curtisca8ad982018-04-09 20:23:14139 void DecodeAndExpect(scoped_refptr<DecoderBuffer> buffer,
dalecurtis8f75b992016-03-30 17:54:59140 DecodeStatus status) {
sergeyu@chromium.org49cea862014-06-11 11:11:50141 EXPECT_CALL(*this, DecodeDone(status));
Xiaohan Wangc03ce232018-05-30 21:07:58142 decoder_->Decode(buffer, base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
143 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59144 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43145 }
146
xhwang@chromium.org5c5e9b72014-06-17 19:21:12147 // Helper function to simulate the decrypting and decoding process in the
148 // |decryptor_| with a decoding delay of kDecodingDelay buffers.
Dale Curtisca8ad982018-04-09 20:23:14149 void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
xhwang@chromium.org5c5e9b72014-06-17 19:21:12150 const Decryptor::AudioDecodeCB& audio_decode_cb) {
151 num_decrypt_and_decode_calls_++;
152 if (!encrypted->end_of_stream())
153 num_frames_in_decryptor_++;
154
155 if (num_decrypt_and_decode_calls_ <= kDecodingDelay ||
156 num_frames_in_decryptor_ == 0) {
anujk.sharmaf1cb600a2015-01-07 19:11:13157 audio_decode_cb.Run(Decryptor::kNeedMoreData, Decryptor::AudioFrames());
xhwang@chromium.org5c5e9b72014-06-17 19:21:12158 return;
159 }
160
161 num_frames_in_decryptor_--;
162 audio_decode_cb.Run(Decryptor::kSuccess,
anujk.sharmaf1cb600a2015-01-07 19:11:13163 Decryptor::AudioFrames(1, decoded_frame_));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12164 }
165
xhwang@chromium.org97a9ce42012-10-19 10:06:43166 // Sets up expectations and actions to put DecryptingAudioDecoder in an
167 // active normal decoding state.
168 void EnterNormalDecodingState() {
Xiaohan Wangc03ce232018-05-30 21:07:58169 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
170 .WillRepeatedly(
171 Invoke(this, &DecryptingAudioDecoderTest::DecryptAndDecodeAudio));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12172 EXPECT_CALL(*this, FrameReady(decoded_frame_));
173 for (int i = 0; i < kDecodingDelay + 1; ++i)
dalecurtis8f75b992016-03-30 17:54:59174 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43175 }
176
177 // Sets up expectations and actions to put DecryptingAudioDecoder in an end
178 // of stream state. This function must be called after
179 // EnterNormalDecodingState() to work.
180 void EnterEndOfStreamState() {
xhwang@chromium.orgff757032014-06-19 01:42:24181 // The codec in the |decryptor_| will be flushed.
Xiaohan Wangc03ce232018-05-30 21:07:58182 EXPECT_CALL(*this, FrameReady(decoded_frame_)).Times(kDecodingDelay);
dalecurtis8f75b992016-03-30 17:54:59183 DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), DecodeStatus::OK);
xhwang@chromium.org5c5e9b72014-06-17 19:21:12184 EXPECT_EQ(0, num_frames_in_decryptor_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43185 }
186
187 // Make the audio decode callback pending by saving and not firing it.
188 void EnterPendingDecodeState() {
Dale Curtise25163812018-09-21 22:13:39189 EXPECT_TRUE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43190 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
191 .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
192
rileya@chromium.orgd70157792014-03-17 21:45:03193 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50194 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03195 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59196 base::RunLoop().RunUntilIdle();
rileya@chromium.orgd70157792014-03-17 21:45:03197 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on
198 // the decryptor.
Dale Curtise25163812018-09-21 22:13:39199 EXPECT_FALSE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43200 }
201
202 void EnterWaitingForKeyState() {
xhwang@chromium.org2b454ff82012-11-15 03:57:58203 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58204 .WillRepeatedly(
205 RunCallback<1>(Decryptor::kNoKey, Decryptor::AudioFrames()));
Xiaohan Wang640b41d2018-12-18 19:00:46206 EXPECT_CALL(*this, OnWaiting(WaitingReason::kNoDecryptionKey));
rileya@chromium.orgd70157792014-03-17 21:45:03207 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50208 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03209 base::Unretained(this)));
Xiaohan Wang689cd4282017-08-04 07:54:55210
fdorayf920dcf2016-06-27 17:16:59211 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43212 }
213
214 void AbortPendingAudioDecodeCB() {
Dale Curtise25163812018-09-21 22:13:39215 if (pending_audio_decode_cb_) {
216 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58217 .Run(Decryptor::kSuccess, Decryptor::AudioFrames());
xhwang@chromium.org97a9ce42012-10-19 10:06:43218 }
219 }
220
rileya@chromium.orgb960fd02014-01-23 04:48:54221 void AbortAllPendingCBs() {
Dale Curtise25163812018-09-21 22:13:39222 if (pending_init_cb_) {
223 ASSERT_TRUE(!pending_audio_decode_cb_);
224 std::move(pending_init_cb_).Run(false);
rileya@chromium.orgb960fd02014-01-23 04:48:54225 return;
226 }
227
228 AbortPendingAudioDecodeCB();
229 }
230
xhwang@chromium.org97a9ce42012-10-19 10:06:43231 void Reset() {
232 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
233 .WillRepeatedly(InvokeWithoutArgs(
234 this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
235
236 decoder_->Reset(NewExpectedClosure());
fdorayf920dcf2016-06-27 17:16:59237 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43238 }
239
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41240 void Destroy() {
rileya@chromium.orgb960fd02014-01-23 04:48:54241 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
242 .WillRepeatedly(InvokeWithoutArgs(
243 this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
244
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41245 decoder_.reset();
fdorayf920dcf2016-06-27 17:16:59246 base::RunLoop().RunUntilIdle();
rileya@chromium.orgb960fd02014-01-23 04:48:54247 }
248
John Rummell1b2b63d2019-04-26 20:45:57249 MOCK_METHOD1(FrameReady, void(scoped_refptr<AudioBuffer>));
dalecurtis8f75b992016-03-30 17:54:59250 MOCK_METHOD1(DecodeDone, void(DecodeStatus));
xhwang@chromium.org97a9ce42012-10-19 10:06:43251
Xiaohan Wang640b41d2018-12-18 19:00:46252 MOCK_METHOD1(OnWaiting, void(WaitingReason));
jrummell74fc4f942015-03-02 22:48:27253
Gabriel Charette311b0e92019-09-09 20:01:25254 base::test::SingleThreadTaskEnvironment task_environment_;
Xiaohan Wang14b3166d2018-12-28 21:05:46255 NullMediaLog media_log_;
dcheng254c5362016-04-22 01:12:48256 std::unique_ptr<DecryptingAudioDecoder> decoder_;
257 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
258 std::unique_ptr<StrictMock<MockDecryptor>> decryptor_;
xhwang@chromium.org7c39486c2013-01-06 15:36:09259 AudioDecoderConfig config_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43260
xhwang@chromium.org5c5e9b72014-06-17 19:21:12261 // Variables to help the |decryptor_| to simulate decoding delay and flushing.
262 int num_decrypt_and_decode_calls_;
263 int num_frames_in_decryptor_;
264
xhwang@chromium.org97a9ce42012-10-19 10:06:43265 Decryptor::DecoderInitCB pending_init_cb_;
Xiaohan Wangd72ce5d2020-07-14 01:34:43266 CdmContext::EventCB event_cb_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43267 Decryptor::AudioDecodeCB pending_audio_decode_cb_;
268
rileya@chromium.orgd70157792014-03-17 21:45:03269 // Constant buffer/frames, to be used/returned by |decoder_| and |decryptor_|.
xhwang@chromium.org97a9ce42012-10-19 10:06:43270 scoped_refptr<DecoderBuffer> encrypted_buffer_;
jrummell@chromium.org47b37a62013-07-10 03:56:10271 scoped_refptr<AudioBuffer> decoded_frame_;
anujk.sharmaf1cb600a2015-01-07 19:11:13272 Decryptor::AudioFrames decoded_frame_list_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43273
274 private:
275 DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
276};
277
278TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
279 Initialize();
280}
281
xhwang@chromium.org97a9ce42012-10-19 10:06:43282// Ensure decoder handles invalid audio configs without crashing.
283TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
dalecurtis@google.comb5eca3c2013-01-04 20:20:15284 AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
dougsteed8d5275f2016-03-12 00:04:30285 CHANNEL_LAYOUT_STEREO, 0, EmptyExtraData(),
Yuchen Liub33bfc12019-11-08 20:16:12286 EncryptionScheme::kCenc);
xhwang@chromium.org97a9ce42012-10-19 10:06:43287
xhwang59c321f2015-06-05 23:46:55288 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43289}
290
291// Ensure decoder handles unsupported audio configs without crashing.
292TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
xhwang00f69db72015-11-12 07:32:37293 SetCdmType(CDM_WITH_DECRYPTOR);
Xiaohan Wangd72ce5d2020-07-14 01:34:43294 EXPECT_CALL(*cdm_context_, RegisterEventCB(_)).WillOnce([&](auto cb) {
295 event_cb_ = cb;
296 return std::make_unique<CallbackRegistration>();
297 });
xhwang@chromium.orga3e28672013-03-14 14:54:59298 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
Jose Lopes109230ec2020-02-24 10:28:12299 .WillOnce(RunOnceCallback<1>(false));
xhwang@chromium.org97a9ce42012-10-19 10:06:43300
dalecurtis@google.comb5eca3c2013-01-04 20:20:15301 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09302 CHANNEL_LAYOUT_STEREO, kSampleRate,
Yuchen Liub33bfc12019-11-08 20:16:12303 EmptyExtraData(), EncryptionScheme::kCenc);
xhwang59c321f2015-06-05 23:46:55304 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43305}
306
xhwang00f69db72015-11-12 07:32:37307TEST_F(DecryptingAudioDecoderTest, Initialize_CdmWithoutDecryptor) {
308 SetCdmType(CDM_WITHOUT_DECRYPTOR);
xhwang@chromium.org699b64a2013-06-19 07:36:05309 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09310 CHANNEL_LAYOUT_STEREO, kSampleRate,
Yuchen Liub33bfc12019-11-08 20:16:12311 EmptyExtraData(), EncryptionScheme::kCenc);
xhwang59c321f2015-06-05 23:46:55312 InitializeAndExpectResult(config, false);
xhwang@chromium.org699b64a2013-06-19 07:36:05313}
314
xhwang@chromium.org97a9ce42012-10-19 10:06:43315// Test normal decrypt and decode case.
316TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
317 Initialize();
318 EnterNormalDecodingState();
319}
320
321// Test the case where the decryptor returns error when doing decrypt and
322// decode.
323TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
324 Initialize();
325
xhwang@chromium.org97a9ce42012-10-19 10:06:43326 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58327 .WillRepeatedly(
328 RunCallback<1>(Decryptor::kError, Decryptor::AudioFrames()));
xhwang@chromium.org97a9ce42012-10-19 10:06:43329
dalecurtis8f75b992016-03-30 17:54:59330 DecodeAndExpect(encrypted_buffer_, DecodeStatus::DECODE_ERROR);
xhwang@chromium.org97a9ce42012-10-19 10:06:43331}
332
333// Test the case where the decryptor returns multiple decoded frames.
334TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
335 Initialize();
336
jrummell@chromium.org47b37a62013-07-10 03:56:10337 scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07338 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59339 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
340 kFakeAudioFrameSize, kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:10341 scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07342 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59343 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
344 kFakeAudioFrameSize, kNoTimestamp);
xhwang@chromium.orgecbb9762012-10-24 22:33:54345 decoded_frame_list_.push_back(frame_a);
346 decoded_frame_list_.push_back(frame_b);
xhwang@chromium.org97a9ce42012-10-19 10:06:43347
xhwang@chromium.org97a9ce42012-10-19 10:06:43348 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48349 .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43350
xhwang@chromium.org5c5e9b72014-06-17 19:21:12351 EXPECT_CALL(*this, FrameReady(decoded_frame_));
352 EXPECT_CALL(*this, FrameReady(frame_a));
353 EXPECT_CALL(*this, FrameReady(frame_b));
dalecurtis8f75b992016-03-30 17:54:59354 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43355}
356
357// Test the case where the decryptor receives end-of-stream buffer.
358TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
359 Initialize();
360 EnterNormalDecodingState();
361 EnterEndOfStreamState();
362}
363
xhwang65c23032017-02-17 04:37:42364// Test reinitializing decode with a new encrypted config.
365TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToEncrypted) {
xhwang@chromium.org7c39486c2013-01-06 15:36:09366 Initialize();
367
rileya@chromium.orgd70157792014-03-17 21:45:03368 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
369 .Times(AtMost(1))
Jose Lopes109230ec2020-02-24 10:28:12370 .WillOnce(RunOnceCallback<1>(true));
xhwang@chromium.org7c39486c2013-01-06 15:36:09371
xhwang@chromium.orgecf16912013-01-12 16:55:22372 // The new config is different from the initial config in bits-per-channel,
373 // channel layout and samples_per_second.
374 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
chcunningham9812dd82015-10-20 01:42:09375 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
Yuchen Liub33bfc12019-11-08 20:16:12376 EncryptionScheme::kCenc);
xhwang@chromium.orgecf16912013-01-12 16:55:22377 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
378 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
379 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
xhwang65c23032017-02-17 04:37:42380 ASSERT_TRUE(new_config.is_encrypted());
381
382 ReinitializeConfigChange(new_config);
383 base::RunLoop().RunUntilIdle();
384}
385
386// Test reinitializing decode with a new clear config.
387TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToClear) {
388 Initialize();
389
390 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
391 .Times(AtMost(1))
Jose Lopes109230ec2020-02-24 10:28:12392 .WillOnce(RunOnceCallback<1>(true));
xhwang65c23032017-02-17 04:37:42393
394 // The new config is different from the initial config in bits-per-channel,
395 // channel layout and samples_per_second.
396 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
397 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
Yuchen Liub33bfc12019-11-08 20:16:12398 EncryptionScheme::kUnencrypted);
xhwang65c23032017-02-17 04:37:42399 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
400 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
401 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
402 ASSERT_FALSE(new_config.is_encrypted());
xhwang@chromium.orgecf16912013-01-12 16:55:22403
rileya@chromium.orgd70157792014-03-17 21:45:03404 ReinitializeConfigChange(new_config);
fdorayf920dcf2016-06-27 17:16:59405 base::RunLoop().RunUntilIdle();
xhwang@chromium.org7c39486c2013-01-06 15:36:09406}
407
xhwang@chromium.org97a9ce42012-10-19 10:06:43408// Test the case where the a key is added when the decryptor is in
409// kWaitingForKey state.
410TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
411 Initialize();
412 EnterWaitingForKeyState();
413
414 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48415 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50416 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59417 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
Xiaohan Wangd72ce5d2020-07-14 01:34:43418 event_cb_.Run(CdmContext::Event::kHasAdditionalUsableKey);
fdorayf920dcf2016-06-27 17:16:59419 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43420}
421
422// Test the case where the a key is added when the decryptor is in
423// kPendingDecode state.
424TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
425 Initialize();
426 EnterPendingDecodeState();
427
428 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48429 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50430 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59431 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43432 // The audio decode callback is returned after the correct decryption key is
433 // added.
Xiaohan Wangd72ce5d2020-07-14 01:34:43434 event_cb_.Run(CdmContext::Event::kHasAdditionalUsableKey);
Dale Curtise25163812018-09-21 22:13:39435 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58436 .Run(Decryptor::kNoKey, Decryptor::AudioFrames());
fdorayf920dcf2016-06-27 17:16:59437 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43438}
439
440// Test resetting when the decoder is in kIdle state but has not decoded any
441// frame.
442TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
443 Initialize();
444 Reset();
445}
446
447// Test resetting when the decoder is in kIdle state after it has decoded one
448// frame.
449TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
450 Initialize();
451 EnterNormalDecodingState();
452 Reset();
453}
454
xhwang@chromium.org97a9ce42012-10-19 10:06:43455// Test resetting when the decoder is in kPendingDecode state.
456TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
457 Initialize();
458 EnterPendingDecodeState();
459
dalecurtis8f75b992016-03-30 17:54:59460 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43461
462 Reset();
463}
464
465// Test resetting when the decoder is in kWaitingForKey state.
466TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
467 Initialize();
468 EnterWaitingForKeyState();
469
dalecurtis8f75b992016-03-30 17:54:59470 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43471
472 Reset();
473}
474
475// Test resetting when the decoder has hit end of stream and is in
476// kDecodeFinished state.
477TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
478 Initialize();
479 EnterNormalDecodingState();
480 EnterEndOfStreamState();
481 Reset();
482}
483
484// Test resetting after the decoder has been reset.
485TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
486 Initialize();
487 EnterNormalDecodingState();
488 Reset();
489 Reset();
490}
491
xhwang@chromium.org97a9ce42012-10-19 10:06:43492} // namespace media