[go: nahoru, domu]

blob: cd936d3dcef0b480a0ac740a58aee4bcd1a06f5b [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;
xhwang@chromium.org97a9ce42012-10-19 10:06:4327using ::testing::_;
28using ::testing::AtMost;
xhwang00f69db72015-11-12 07:32:3729using ::testing::Return;
xhwang@chromium.org97a9ce42012-10-19 10:06:4330using ::testing::SaveArg;
31using ::testing::StrictMock;
32
33namespace media {
34
xhwang@chromium.org5c5e9b72014-06-17 19:21:1235const int kSampleRate = 44100;
rileya@chromium.org16ecd5f2014-03-20 05:41:0736
xhwang@chromium.orgecf16912013-01-12 16:55:2237// Make sure the kFakeAudioFrameSize is a valid frame size for all audio decoder
38// configs used in this test.
xhwang@chromium.org5c5e9b72014-06-17 19:21:1239const int kFakeAudioFrameSize = 48;
Avi Drissman97785ea2015-12-19 01:11:3140const uint8_t kFakeKeyId[] = {0x4b, 0x65, 0x79, 0x20, 0x49, 0x44};
41const uint8_t kFakeIv[DecryptConfig::kDecryptionKeySize] = {0};
xhwang@chromium.org5c5e9b72014-06-17 19:21:1242const int kDecodingDelay = 3;
xhwang@chromium.org97a9ce42012-10-19 10:06:4343
44// Create a fake non-empty encrypted buffer.
45static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() {
46 const int buffer_size = 16; // Need a non-empty buffer;
47 scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size));
John Rummell9044a762018-04-19 01:24:0748 buffer->set_decrypt_config(DecryptConfig::CreateCencConfig(
xhwang@chromium.org97a9ce42012-10-19 10:06:4349 std::string(reinterpret_cast<const char*>(kFakeKeyId),
Avi Drissmanfa9bed42018-12-25 20:26:3950 base::size(kFakeKeyId)),
51 std::string(reinterpret_cast<const char*>(kFakeIv), base::size(kFakeIv)),
John Rummell9044a762018-04-19 01:24:0752 std::vector<SubsampleEntry>()));
xhwang@chromium.org97a9ce42012-10-19 10:06:4353 return buffer;
54}
55
xhwang@chromium.org97a9ce42012-10-19 10:06:4356class DecryptingAudioDecoderTest : public testing::Test {
57 public:
58 DecryptingAudioDecoderTest()
Carlos Caballero9de7c722019-05-31 21:25:0759 : decoder_(new DecryptingAudioDecoder(
Gabriel Charettedfa36042019-08-19 17:30:1160 task_environment_.GetMainThreadTaskRunner(),
Carlos Caballero9de7c722019-05-31 21:25:0761 &media_log_)),
xhwang00f69db72015-11-12 07:32:3762 cdm_context_(new StrictMock<MockCdmContext>()),
xhwang@chromium.org97a9ce42012-10-19 10:06:4363 decryptor_(new StrictMock<MockDecryptor>()),
xhwang@chromium.org5c5e9b72014-06-17 19:21:1264 num_decrypt_and_decode_calls_(0),
65 num_frames_in_decryptor_(0),
xhwang@chromium.org97a9ce42012-10-19 10:06:4366 encrypted_buffer_(CreateFakeEncryptedBuffer()),
xhwang@chromium.orgecbb9762012-10-24 22:33:5467 decoded_frame_(NULL),
rileya@chromium.orgd70157792014-03-17 21:45:0368 decoded_frame_list_() {}
xhwang@chromium.org97a9ce42012-10-19 10:06:4369
Daniel Cheng9f0b7012018-04-26 21:09:0570 ~DecryptingAudioDecoderTest() override { Destroy(); }
rileya@chromium.orgb960fd02014-01-23 04:48:5471
xhwang59c321f2015-06-05 23:46:5572 void InitializeAndExpectResult(const AudioDecoderConfig& config,
73 bool success) {
dalecurtis@google.come81f5f9d2014-03-29 01:32:1674 // Initialize data now that the config is known. Since the code uses
75 // invalid values (that CreateEmptyBuffer() doesn't support), tweak them
76 // just for CreateEmptyBuffer().
77 int channels = ChannelLayoutToChannelCount(config.channel_layout());
78 if (channels < 0)
79 channels = 0;
dalecurtis39a7f932016-07-19 18:34:5980 decoded_frame_ = AudioBuffer::CreateEmptyBuffer(
81 config.channel_layout(), channels, kSampleRate, kFakeAudioFrameSize,
82 kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:1083 decoded_frame_list_.push_back(decoded_frame_);
84
Xiaohan Wang640b41d2018-12-18 19:00:4685 decoder_->Initialize(config, cdm_context_.get(), NewExpectedBoolCB(success),
86 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
87 base::Unretained(this)),
88 base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
89 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:5990 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:4391 }
92
xhwanga935e442016-02-11 02:22:4593 enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR };
xhwang00f69db72015-11-12 07:32:3794
95 void SetCdmType(CdmType cdm_type) {
xhwang00f69db72015-11-12 07:32:3796 const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR;
xhwanga935e442016-02-11 02:22:4597 EXPECT_CALL(*cdm_context_, GetDecryptor())
98 .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr));
jrummell@chromium.org9ebc3b032014-08-13 04:01:2399 }
100
xhwang@chromium.org97a9ce42012-10-19 10:06:43101 void Initialize() {
xhwang00f69db72015-11-12 07:32:37102 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59103 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org97a9ce42012-10-19 10:06:43104 .Times(AtMost(1))
xhwang@chromium.org8b10f2222012-11-13 05:49:48105 .WillOnce(RunCallback<1>(true));
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02106 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
xhwang@chromium.org82d7abd2012-11-02 23:04:03107 .WillOnce(SaveArg<1>(&key_added_cb_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43108
xhwang@chromium.org7c39486c2013-01-06 15:36:09109 config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09110 CHANNEL_LAYOUT_STEREO, kSampleRate, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30111 AesCtrEncryptionScheme(), base::TimeDelta(), 0);
xhwang59c321f2015-06-05 23:46:55112 InitializeAndExpectResult(config_, true);
xhwang@chromium.org97a9ce42012-10-19 10:06:43113 }
114
Xiaohan Wangc03ce232018-05-30 21:07:58115 void Reinitialize() { ReinitializeConfigChange(config_); }
rileya@chromium.orgd70157792014-03-17 21:45:03116
dalecurtis@google.com6fb86542014-04-18 19:58:13117 void ReinitializeConfigChange(const AudioDecoderConfig& new_config) {
rileya@chromium.orgd70157792014-03-17 21:45:03118 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
119 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
120 .WillOnce(RunCallback<1>(true));
121 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
Xiaohan Wangc03ce232018-05-30 21:07:58122 .WillOnce(SaveArg<1>(&key_added_cb_));
Xiaohan Wang640b41d2018-12-18 19:00:46123 decoder_->Initialize(new_config, cdm_context_.get(),
124 NewExpectedBoolCB(true),
125 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
126 base::Unretained(this)),
127 base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
128 base::Unretained(this)));
rileya@chromium.orgd70157792014-03-17 21:45:03129 }
130
xhwang@chromium.org5c5e9b72014-06-17 19:21:12131 // Decode |buffer| and expect DecodeDone to get called with |status|.
Dale Curtisca8ad982018-04-09 20:23:14132 void DecodeAndExpect(scoped_refptr<DecoderBuffer> buffer,
dalecurtis8f75b992016-03-30 17:54:59133 DecodeStatus status) {
sergeyu@chromium.org49cea862014-06-11 11:11:50134 EXPECT_CALL(*this, DecodeDone(status));
Xiaohan Wangc03ce232018-05-30 21:07:58135 decoder_->Decode(buffer, base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
136 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59137 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43138 }
139
xhwang@chromium.org5c5e9b72014-06-17 19:21:12140 // Helper function to simulate the decrypting and decoding process in the
141 // |decryptor_| with a decoding delay of kDecodingDelay buffers.
Dale Curtisca8ad982018-04-09 20:23:14142 void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
xhwang@chromium.org5c5e9b72014-06-17 19:21:12143 const Decryptor::AudioDecodeCB& audio_decode_cb) {
144 num_decrypt_and_decode_calls_++;
145 if (!encrypted->end_of_stream())
146 num_frames_in_decryptor_++;
147
148 if (num_decrypt_and_decode_calls_ <= kDecodingDelay ||
149 num_frames_in_decryptor_ == 0) {
anujk.sharmaf1cb600a2015-01-07 19:11:13150 audio_decode_cb.Run(Decryptor::kNeedMoreData, Decryptor::AudioFrames());
xhwang@chromium.org5c5e9b72014-06-17 19:21:12151 return;
152 }
153
154 num_frames_in_decryptor_--;
155 audio_decode_cb.Run(Decryptor::kSuccess,
anujk.sharmaf1cb600a2015-01-07 19:11:13156 Decryptor::AudioFrames(1, decoded_frame_));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12157 }
158
xhwang@chromium.org97a9ce42012-10-19 10:06:43159 // Sets up expectations and actions to put DecryptingAudioDecoder in an
160 // active normal decoding state.
161 void EnterNormalDecodingState() {
Xiaohan Wangc03ce232018-05-30 21:07:58162 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
163 .WillRepeatedly(
164 Invoke(this, &DecryptingAudioDecoderTest::DecryptAndDecodeAudio));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12165 EXPECT_CALL(*this, FrameReady(decoded_frame_));
166 for (int i = 0; i < kDecodingDelay + 1; ++i)
dalecurtis8f75b992016-03-30 17:54:59167 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43168 }
169
170 // Sets up expectations and actions to put DecryptingAudioDecoder in an end
171 // of stream state. This function must be called after
172 // EnterNormalDecodingState() to work.
173 void EnterEndOfStreamState() {
xhwang@chromium.orgff757032014-06-19 01:42:24174 // The codec in the |decryptor_| will be flushed.
Xiaohan Wangc03ce232018-05-30 21:07:58175 EXPECT_CALL(*this, FrameReady(decoded_frame_)).Times(kDecodingDelay);
dalecurtis8f75b992016-03-30 17:54:59176 DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), DecodeStatus::OK);
xhwang@chromium.org5c5e9b72014-06-17 19:21:12177 EXPECT_EQ(0, num_frames_in_decryptor_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43178 }
179
180 // Make the audio decode callback pending by saving and not firing it.
181 void EnterPendingDecodeState() {
Dale Curtise25163812018-09-21 22:13:39182 EXPECT_TRUE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43183 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
184 .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
185
rileya@chromium.orgd70157792014-03-17 21:45:03186 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50187 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03188 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59189 base::RunLoop().RunUntilIdle();
rileya@chromium.orgd70157792014-03-17 21:45:03190 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on
191 // the decryptor.
Dale Curtise25163812018-09-21 22:13:39192 EXPECT_FALSE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43193 }
194
195 void EnterWaitingForKeyState() {
xhwang@chromium.org2b454ff82012-11-15 03:57:58196 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58197 .WillRepeatedly(
198 RunCallback<1>(Decryptor::kNoKey, Decryptor::AudioFrames()));
Xiaohan Wang640b41d2018-12-18 19:00:46199 EXPECT_CALL(*this, OnWaiting(WaitingReason::kNoDecryptionKey));
rileya@chromium.orgd70157792014-03-17 21:45:03200 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50201 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03202 base::Unretained(this)));
Xiaohan Wang689cd4282017-08-04 07:54:55203
fdorayf920dcf2016-06-27 17:16:59204 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43205 }
206
207 void AbortPendingAudioDecodeCB() {
Dale Curtise25163812018-09-21 22:13:39208 if (pending_audio_decode_cb_) {
209 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58210 .Run(Decryptor::kSuccess, Decryptor::AudioFrames());
xhwang@chromium.org97a9ce42012-10-19 10:06:43211 }
212 }
213
rileya@chromium.orgb960fd02014-01-23 04:48:54214 void AbortAllPendingCBs() {
Dale Curtise25163812018-09-21 22:13:39215 if (pending_init_cb_) {
216 ASSERT_TRUE(!pending_audio_decode_cb_);
217 std::move(pending_init_cb_).Run(false);
rileya@chromium.orgb960fd02014-01-23 04:48:54218 return;
219 }
220
221 AbortPendingAudioDecodeCB();
222 }
223
xhwang@chromium.org97a9ce42012-10-19 10:06:43224 void Reset() {
225 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
226 .WillRepeatedly(InvokeWithoutArgs(
227 this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
228
229 decoder_->Reset(NewExpectedClosure());
fdorayf920dcf2016-06-27 17:16:59230 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43231 }
232
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41233 void Destroy() {
rileya@chromium.orgb960fd02014-01-23 04:48:54234 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
235 .WillRepeatedly(InvokeWithoutArgs(
236 this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
237
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41238 decoder_.reset();
fdorayf920dcf2016-06-27 17:16:59239 base::RunLoop().RunUntilIdle();
rileya@chromium.orgb960fd02014-01-23 04:48:54240 }
241
John Rummell1b2b63d2019-04-26 20:45:57242 MOCK_METHOD1(FrameReady, void(scoped_refptr<AudioBuffer>));
dalecurtis8f75b992016-03-30 17:54:59243 MOCK_METHOD1(DecodeDone, void(DecodeStatus));
xhwang@chromium.org97a9ce42012-10-19 10:06:43244
Xiaohan Wang640b41d2018-12-18 19:00:46245 MOCK_METHOD1(OnWaiting, void(WaitingReason));
jrummell74fc4f942015-03-02 22:48:27246
Gabriel Charette311b0e92019-09-09 20:01:25247 base::test::SingleThreadTaskEnvironment task_environment_;
Xiaohan Wang14b3166d2018-12-28 21:05:46248 NullMediaLog media_log_;
dcheng254c5362016-04-22 01:12:48249 std::unique_ptr<DecryptingAudioDecoder> decoder_;
250 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
251 std::unique_ptr<StrictMock<MockDecryptor>> decryptor_;
xhwang@chromium.org7c39486c2013-01-06 15:36:09252 AudioDecoderConfig config_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43253
xhwang@chromium.org5c5e9b72014-06-17 19:21:12254 // Variables to help the |decryptor_| to simulate decoding delay and flushing.
255 int num_decrypt_and_decode_calls_;
256 int num_frames_in_decryptor_;
257
xhwang@chromium.org97a9ce42012-10-19 10:06:43258 Decryptor::DecoderInitCB pending_init_cb_;
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02259 Decryptor::NewKeyCB key_added_cb_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43260 Decryptor::AudioDecodeCB pending_audio_decode_cb_;
261
rileya@chromium.orgd70157792014-03-17 21:45:03262 // Constant buffer/frames, to be used/returned by |decoder_| and |decryptor_|.
xhwang@chromium.org97a9ce42012-10-19 10:06:43263 scoped_refptr<DecoderBuffer> encrypted_buffer_;
jrummell@chromium.org47b37a62013-07-10 03:56:10264 scoped_refptr<AudioBuffer> decoded_frame_;
anujk.sharmaf1cb600a2015-01-07 19:11:13265 Decryptor::AudioFrames decoded_frame_list_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43266
267 private:
268 DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
269};
270
271TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
272 Initialize();
273}
274
xhwang@chromium.org97a9ce42012-10-19 10:06:43275// Ensure decoder handles invalid audio configs without crashing.
276TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
dalecurtis@google.comb5eca3c2013-01-04 20:20:15277 AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
dougsteed8d5275f2016-03-12 00:04:30278 CHANNEL_LAYOUT_STEREO, 0, EmptyExtraData(),
279 AesCtrEncryptionScheme());
xhwang@chromium.org97a9ce42012-10-19 10:06:43280
xhwang59c321f2015-06-05 23:46:55281 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43282}
283
284// Ensure decoder handles unsupported audio configs without crashing.
285TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
xhwang00f69db72015-11-12 07:32:37286 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59287 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48288 .WillOnce(RunCallback<1>(false));
xhwang@chromium.org97a9ce42012-10-19 10:06:43289
dalecurtis@google.comb5eca3c2013-01-04 20:20:15290 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09291 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30292 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55293 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43294}
295
xhwang00f69db72015-11-12 07:32:37296TEST_F(DecryptingAudioDecoderTest, Initialize_CdmWithoutDecryptor) {
297 SetCdmType(CDM_WITHOUT_DECRYPTOR);
xhwang@chromium.org699b64a2013-06-19 07:36:05298 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09299 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30300 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55301 InitializeAndExpectResult(config, false);
xhwang@chromium.org699b64a2013-06-19 07:36:05302}
303
xhwang@chromium.org97a9ce42012-10-19 10:06:43304// Test normal decrypt and decode case.
305TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
306 Initialize();
307 EnterNormalDecodingState();
308}
309
310// Test the case where the decryptor returns error when doing decrypt and
311// decode.
312TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
313 Initialize();
314
xhwang@chromium.org97a9ce42012-10-19 10:06:43315 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58316 .WillRepeatedly(
317 RunCallback<1>(Decryptor::kError, Decryptor::AudioFrames()));
xhwang@chromium.org97a9ce42012-10-19 10:06:43318
dalecurtis8f75b992016-03-30 17:54:59319 DecodeAndExpect(encrypted_buffer_, DecodeStatus::DECODE_ERROR);
xhwang@chromium.org97a9ce42012-10-19 10:06:43320}
321
322// Test the case where the decryptor returns multiple decoded frames.
323TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
324 Initialize();
325
jrummell@chromium.org47b37a62013-07-10 03:56:10326 scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07327 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59328 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
329 kFakeAudioFrameSize, kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:10330 scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07331 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59332 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
333 kFakeAudioFrameSize, kNoTimestamp);
xhwang@chromium.orgecbb9762012-10-24 22:33:54334 decoded_frame_list_.push_back(frame_a);
335 decoded_frame_list_.push_back(frame_b);
xhwang@chromium.org97a9ce42012-10-19 10:06:43336
xhwang@chromium.org97a9ce42012-10-19 10:06:43337 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48338 .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43339
xhwang@chromium.org5c5e9b72014-06-17 19:21:12340 EXPECT_CALL(*this, FrameReady(decoded_frame_));
341 EXPECT_CALL(*this, FrameReady(frame_a));
342 EXPECT_CALL(*this, FrameReady(frame_b));
dalecurtis8f75b992016-03-30 17:54:59343 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43344}
345
346// Test the case where the decryptor receives end-of-stream buffer.
347TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
348 Initialize();
349 EnterNormalDecodingState();
350 EnterEndOfStreamState();
351}
352
xhwang65c23032017-02-17 04:37:42353// Test reinitializing decode with a new encrypted config.
354TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToEncrypted) {
xhwang@chromium.org7c39486c2013-01-06 15:36:09355 Initialize();
356
rileya@chromium.orgd70157792014-03-17 21:45:03357 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
358 .Times(AtMost(1))
359 .WillOnce(RunCallback<1>(true));
xhwang@chromium.org7c39486c2013-01-06 15:36:09360
xhwang@chromium.orgecf16912013-01-12 16:55:22361 // The new config is different from the initial config in bits-per-channel,
362 // channel layout and samples_per_second.
363 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
chcunningham9812dd82015-10-20 01:42:09364 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30365 AesCtrEncryptionScheme());
xhwang@chromium.orgecf16912013-01-12 16:55:22366 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
367 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
368 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
xhwang65c23032017-02-17 04:37:42369 ASSERT_TRUE(new_config.is_encrypted());
370
371 ReinitializeConfigChange(new_config);
372 base::RunLoop().RunUntilIdle();
373}
374
375// Test reinitializing decode with a new clear config.
376TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToClear) {
377 Initialize();
378
379 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
380 .Times(AtMost(1))
381 .WillOnce(RunCallback<1>(true));
382
383 // The new config is different from the initial config in bits-per-channel,
384 // channel layout and samples_per_second.
385 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
386 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
387 Unencrypted());
388 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
389 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
390 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
391 ASSERT_FALSE(new_config.is_encrypted());
xhwang@chromium.orgecf16912013-01-12 16:55:22392
rileya@chromium.orgd70157792014-03-17 21:45:03393 ReinitializeConfigChange(new_config);
fdorayf920dcf2016-06-27 17:16:59394 base::RunLoop().RunUntilIdle();
xhwang@chromium.org7c39486c2013-01-06 15:36:09395}
396
xhwang@chromium.org97a9ce42012-10-19 10:06:43397// Test the case where the a key is added when the decryptor is in
398// kWaitingForKey state.
399TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
400 Initialize();
401 EnterWaitingForKeyState();
402
403 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48404 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50405 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59406 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43407 key_added_cb_.Run();
fdorayf920dcf2016-06-27 17:16:59408 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43409}
410
411// Test the case where the a key is added when the decryptor is in
412// kPendingDecode state.
413TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
414 Initialize();
415 EnterPendingDecodeState();
416
417 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48418 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50419 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59420 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43421 // The audio decode callback is returned after the correct decryption key is
422 // added.
423 key_added_cb_.Run();
Dale Curtise25163812018-09-21 22:13:39424 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58425 .Run(Decryptor::kNoKey, Decryptor::AudioFrames());
fdorayf920dcf2016-06-27 17:16:59426 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43427}
428
429// Test resetting when the decoder is in kIdle state but has not decoded any
430// frame.
431TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
432 Initialize();
433 Reset();
434}
435
436// Test resetting when the decoder is in kIdle state after it has decoded one
437// frame.
438TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
439 Initialize();
440 EnterNormalDecodingState();
441 Reset();
442}
443
xhwang@chromium.org97a9ce42012-10-19 10:06:43444// Test resetting when the decoder is in kPendingDecode state.
445TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
446 Initialize();
447 EnterPendingDecodeState();
448
dalecurtis8f75b992016-03-30 17:54:59449 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43450
451 Reset();
452}
453
454// Test resetting when the decoder is in kWaitingForKey state.
455TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
456 Initialize();
457 EnterWaitingForKeyState();
458
dalecurtis8f75b992016-03-30 17:54:59459 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43460
461 Reset();
462}
463
464// Test resetting when the decoder has hit end of stream and is in
465// kDecodeFinished state.
466TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
467 Initialize();
468 EnterNormalDecodingState();
469 EnterEndOfStreamState();
470 Reset();
471}
472
473// Test resetting after the decoder has been reset.
474TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
475 Initialize();
476 EnterNormalDecodingState();
477 Reset();
478 Reset();
479}
480
xhwang@chromium.org97a9ce42012-10-19 10:06:43481} // namespace media