[go: nahoru, domu]

blob: 3364873d4f0e88c3118efdc27f95dacb10e7f594 [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"
Alexander Timin4f9c35c2018-11-01 20:15:2012#include "base/message_loop/message_loop.h"
fdorayf920dcf2016-06-27 17:16:5913#include "base/run_loop.h"
Avi Drissmanfa9bed42018-12-25 20:26:3914#include "base/stl_util.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),
Avi Drissmanfa9bed42018-12-25 20:26:3949 base::size(kFakeKeyId)),
50 std::string(reinterpret_cast<const char*>(kFakeIv), base::size(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
Xiaohan Wang640b41d2018-12-18 19:00:4683 decoder_->Initialize(config, cdm_context_.get(), NewExpectedBoolCB(success),
84 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
85 base::Unretained(this)),
86 base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
87 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:5988 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:4389 }
90
xhwanga935e442016-02-11 02:22:4591 enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR };
xhwang00f69db72015-11-12 07:32:3792
93 void SetCdmType(CdmType cdm_type) {
xhwang00f69db72015-11-12 07:32:3794 const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR;
xhwanga935e442016-02-11 02:22:4595 EXPECT_CALL(*cdm_context_, GetDecryptor())
96 .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr));
jrummell@chromium.org9ebc3b032014-08-13 04:01:2397 }
98
xhwang@chromium.org97a9ce42012-10-19 10:06:4399 void Initialize() {
xhwang00f69db72015-11-12 07:32:37100 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59101 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org97a9ce42012-10-19 10:06:43102 .Times(AtMost(1))
xhwang@chromium.org8b10f2222012-11-13 05:49:48103 .WillOnce(RunCallback<1>(true));
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02104 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
xhwang@chromium.org82d7abd2012-11-02 23:04:03105 .WillOnce(SaveArg<1>(&key_added_cb_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43106
xhwang@chromium.org7c39486c2013-01-06 15:36:09107 config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09108 CHANNEL_LAYOUT_STEREO, kSampleRate, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30109 AesCtrEncryptionScheme(), base::TimeDelta(), 0);
xhwang59c321f2015-06-05 23:46:55110 InitializeAndExpectResult(config_, true);
xhwang@chromium.org97a9ce42012-10-19 10:06:43111 }
112
Xiaohan Wangc03ce232018-05-30 21:07:58113 void Reinitialize() { ReinitializeConfigChange(config_); }
rileya@chromium.orgd70157792014-03-17 21:45:03114
dalecurtis@google.com6fb86542014-04-18 19:58:13115 void ReinitializeConfigChange(const AudioDecoderConfig& new_config) {
rileya@chromium.orgd70157792014-03-17 21:45:03116 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
117 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
118 .WillOnce(RunCallback<1>(true));
119 EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
Xiaohan Wangc03ce232018-05-30 21:07:58120 .WillOnce(SaveArg<1>(&key_added_cb_));
Xiaohan Wang640b41d2018-12-18 19:00:46121 decoder_->Initialize(new_config, cdm_context_.get(),
122 NewExpectedBoolCB(true),
123 base::Bind(&DecryptingAudioDecoderTest::FrameReady,
124 base::Unretained(this)),
125 base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
126 base::Unretained(this)));
rileya@chromium.orgd70157792014-03-17 21:45:03127 }
128
xhwang@chromium.org5c5e9b72014-06-17 19:21:12129 // Decode |buffer| and expect DecodeDone to get called with |status|.
Dale Curtisca8ad982018-04-09 20:23:14130 void DecodeAndExpect(scoped_refptr<DecoderBuffer> buffer,
dalecurtis8f75b992016-03-30 17:54:59131 DecodeStatus status) {
sergeyu@chromium.org49cea862014-06-11 11:11:50132 EXPECT_CALL(*this, DecodeDone(status));
Xiaohan Wangc03ce232018-05-30 21:07:58133 decoder_->Decode(buffer, base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
134 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59135 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43136 }
137
xhwang@chromium.org5c5e9b72014-06-17 19:21:12138 // Helper function to simulate the decrypting and decoding process in the
139 // |decryptor_| with a decoding delay of kDecodingDelay buffers.
Dale Curtisca8ad982018-04-09 20:23:14140 void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
xhwang@chromium.org5c5e9b72014-06-17 19:21:12141 const Decryptor::AudioDecodeCB& audio_decode_cb) {
142 num_decrypt_and_decode_calls_++;
143 if (!encrypted->end_of_stream())
144 num_frames_in_decryptor_++;
145
146 if (num_decrypt_and_decode_calls_ <= kDecodingDelay ||
147 num_frames_in_decryptor_ == 0) {
anujk.sharmaf1cb600a2015-01-07 19:11:13148 audio_decode_cb.Run(Decryptor::kNeedMoreData, Decryptor::AudioFrames());
xhwang@chromium.org5c5e9b72014-06-17 19:21:12149 return;
150 }
151
152 num_frames_in_decryptor_--;
153 audio_decode_cb.Run(Decryptor::kSuccess,
anujk.sharmaf1cb600a2015-01-07 19:11:13154 Decryptor::AudioFrames(1, decoded_frame_));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12155 }
156
xhwang@chromium.org97a9ce42012-10-19 10:06:43157 // Sets up expectations and actions to put DecryptingAudioDecoder in an
158 // active normal decoding state.
159 void EnterNormalDecodingState() {
Xiaohan Wangc03ce232018-05-30 21:07:58160 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
161 .WillRepeatedly(
162 Invoke(this, &DecryptingAudioDecoderTest::DecryptAndDecodeAudio));
xhwang@chromium.org5c5e9b72014-06-17 19:21:12163 EXPECT_CALL(*this, FrameReady(decoded_frame_));
164 for (int i = 0; i < kDecodingDelay + 1; ++i)
dalecurtis8f75b992016-03-30 17:54:59165 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43166 }
167
168 // Sets up expectations and actions to put DecryptingAudioDecoder in an end
169 // of stream state. This function must be called after
170 // EnterNormalDecodingState() to work.
171 void EnterEndOfStreamState() {
xhwang@chromium.orgff757032014-06-19 01:42:24172 // The codec in the |decryptor_| will be flushed.
Xiaohan Wangc03ce232018-05-30 21:07:58173 EXPECT_CALL(*this, FrameReady(decoded_frame_)).Times(kDecodingDelay);
dalecurtis8f75b992016-03-30 17:54:59174 DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), DecodeStatus::OK);
xhwang@chromium.org5c5e9b72014-06-17 19:21:12175 EXPECT_EQ(0, num_frames_in_decryptor_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43176 }
177
178 // Make the audio decode callback pending by saving and not firing it.
179 void EnterPendingDecodeState() {
Dale Curtise25163812018-09-21 22:13:39180 EXPECT_TRUE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43181 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
182 .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
183
rileya@chromium.orgd70157792014-03-17 21:45:03184 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50185 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03186 base::Unretained(this)));
fdorayf920dcf2016-06-27 17:16:59187 base::RunLoop().RunUntilIdle();
rileya@chromium.orgd70157792014-03-17 21:45:03188 // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on
189 // the decryptor.
Dale Curtise25163812018-09-21 22:13:39190 EXPECT_FALSE(!pending_audio_decode_cb_);
xhwang@chromium.org97a9ce42012-10-19 10:06:43191 }
192
193 void EnterWaitingForKeyState() {
xhwang@chromium.org2b454ff82012-11-15 03:57:58194 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58195 .WillRepeatedly(
196 RunCallback<1>(Decryptor::kNoKey, Decryptor::AudioFrames()));
Xiaohan Wang640b41d2018-12-18 19:00:46197 EXPECT_CALL(*this, OnWaiting(WaitingReason::kNoDecryptionKey));
rileya@chromium.orgd70157792014-03-17 21:45:03198 decoder_->Decode(encrypted_buffer_,
sergeyu@chromium.org49cea862014-06-11 11:11:50199 base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
rileya@chromium.orgd70157792014-03-17 21:45:03200 base::Unretained(this)));
Xiaohan Wang689cd4282017-08-04 07:54:55201
fdorayf920dcf2016-06-27 17:16:59202 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43203 }
204
205 void AbortPendingAudioDecodeCB() {
Dale Curtise25163812018-09-21 22:13:39206 if (pending_audio_decode_cb_) {
207 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58208 .Run(Decryptor::kSuccess, Decryptor::AudioFrames());
xhwang@chromium.org97a9ce42012-10-19 10:06:43209 }
210 }
211
rileya@chromium.orgb960fd02014-01-23 04:48:54212 void AbortAllPendingCBs() {
Dale Curtise25163812018-09-21 22:13:39213 if (pending_init_cb_) {
214 ASSERT_TRUE(!pending_audio_decode_cb_);
215 std::move(pending_init_cb_).Run(false);
rileya@chromium.orgb960fd02014-01-23 04:48:54216 return;
217 }
218
219 AbortPendingAudioDecodeCB();
220 }
221
xhwang@chromium.org97a9ce42012-10-19 10:06:43222 void Reset() {
223 EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
224 .WillRepeatedly(InvokeWithoutArgs(
225 this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
226
227 decoder_->Reset(NewExpectedClosure());
fdorayf920dcf2016-06-27 17:16:59228 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43229 }
230
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41231 void Destroy() {
rileya@chromium.orgb960fd02014-01-23 04:48:54232 EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
233 .WillRepeatedly(InvokeWithoutArgs(
234 this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
235
xhwang@chromium.org83fb2a9e2014-07-16 23:36:41236 decoder_.reset();
fdorayf920dcf2016-06-27 17:16:59237 base::RunLoop().RunUntilIdle();
rileya@chromium.orgb960fd02014-01-23 04:48:54238 }
239
John Rummell1b2b63d2019-04-26 20:45:57240 MOCK_METHOD1(FrameReady, void(scoped_refptr<AudioBuffer>));
dalecurtis8f75b992016-03-30 17:54:59241 MOCK_METHOD1(DecodeDone, void(DecodeStatus));
xhwang@chromium.org97a9ce42012-10-19 10:06:43242
Xiaohan Wang640b41d2018-12-18 19:00:46243 MOCK_METHOD1(OnWaiting, void(WaitingReason));
jrummell74fc4f942015-03-02 22:48:27244
xhwang@chromium.orgfb5af232013-04-22 22:40:03245 base::MessageLoop message_loop_;
Xiaohan Wang14b3166d2018-12-28 21:05:46246 NullMediaLog media_log_;
dcheng254c5362016-04-22 01:12:48247 std::unique_ptr<DecryptingAudioDecoder> decoder_;
248 std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
249 std::unique_ptr<StrictMock<MockDecryptor>> decryptor_;
xhwang@chromium.org7c39486c2013-01-06 15:36:09250 AudioDecoderConfig config_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43251
xhwang@chromium.org5c5e9b72014-06-17 19:21:12252 // Variables to help the |decryptor_| to simulate decoding delay and flushing.
253 int num_decrypt_and_decode_calls_;
254 int num_frames_in_decryptor_;
255
xhwang@chromium.org97a9ce42012-10-19 10:06:43256 Decryptor::DecoderInitCB pending_init_cb_;
xhwang@chromium.orgabd36b3af2012-12-22 03:42:02257 Decryptor::NewKeyCB key_added_cb_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43258 Decryptor::AudioDecodeCB pending_audio_decode_cb_;
259
rileya@chromium.orgd70157792014-03-17 21:45:03260 // Constant buffer/frames, to be used/returned by |decoder_| and |decryptor_|.
xhwang@chromium.org97a9ce42012-10-19 10:06:43261 scoped_refptr<DecoderBuffer> encrypted_buffer_;
jrummell@chromium.org47b37a62013-07-10 03:56:10262 scoped_refptr<AudioBuffer> decoded_frame_;
anujk.sharmaf1cb600a2015-01-07 19:11:13263 Decryptor::AudioFrames decoded_frame_list_;
xhwang@chromium.org97a9ce42012-10-19 10:06:43264
265 private:
266 DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
267};
268
269TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
270 Initialize();
271}
272
xhwang@chromium.org97a9ce42012-10-19 10:06:43273// Ensure decoder handles invalid audio configs without crashing.
274TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
dalecurtis@google.comb5eca3c2013-01-04 20:20:15275 AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
dougsteed8d5275f2016-03-12 00:04:30276 CHANNEL_LAYOUT_STEREO, 0, EmptyExtraData(),
277 AesCtrEncryptionScheme());
xhwang@chromium.org97a9ce42012-10-19 10:06:43278
xhwang59c321f2015-06-05 23:46:55279 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43280}
281
282// Ensure decoder handles unsupported audio configs without crashing.
283TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
xhwang00f69db72015-11-12 07:32:37284 SetCdmType(CDM_WITH_DECRYPTOR);
xhwang@chromium.orga3e28672013-03-14 14:54:59285 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48286 .WillOnce(RunCallback<1>(false));
xhwang@chromium.org97a9ce42012-10-19 10:06:43287
dalecurtis@google.comb5eca3c2013-01-04 20:20:15288 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09289 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30290 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55291 InitializeAndExpectResult(config, false);
xhwang@chromium.org97a9ce42012-10-19 10:06:43292}
293
xhwang00f69db72015-11-12 07:32:37294TEST_F(DecryptingAudioDecoderTest, Initialize_CdmWithoutDecryptor) {
295 SetCdmType(CDM_WITHOUT_DECRYPTOR);
xhwang@chromium.org699b64a2013-06-19 07:36:05296 AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
chcunningham9812dd82015-10-20 01:42:09297 CHANNEL_LAYOUT_STEREO, kSampleRate,
dougsteed8d5275f2016-03-12 00:04:30298 EmptyExtraData(), AesCtrEncryptionScheme());
xhwang59c321f2015-06-05 23:46:55299 InitializeAndExpectResult(config, false);
xhwang@chromium.org699b64a2013-06-19 07:36:05300}
301
xhwang@chromium.org97a9ce42012-10-19 10:06:43302// Test normal decrypt and decode case.
303TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
304 Initialize();
305 EnterNormalDecodingState();
306}
307
308// Test the case where the decryptor returns error when doing decrypt and
309// decode.
310TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
311 Initialize();
312
xhwang@chromium.org97a9ce42012-10-19 10:06:43313 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
Xiaohan Wangc03ce232018-05-30 21:07:58314 .WillRepeatedly(
315 RunCallback<1>(Decryptor::kError, Decryptor::AudioFrames()));
xhwang@chromium.org97a9ce42012-10-19 10:06:43316
dalecurtis8f75b992016-03-30 17:54:59317 DecodeAndExpect(encrypted_buffer_, DecodeStatus::DECODE_ERROR);
xhwang@chromium.org97a9ce42012-10-19 10:06:43318}
319
320// Test the case where the decryptor returns multiple decoded frames.
321TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
322 Initialize();
323
jrummell@chromium.org47b37a62013-07-10 03:56:10324 scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
rileya@chromium.org16ecd5f2014-03-20 05:41:07325 config_.channel_layout(),
dalecurtis39a7f932016-07-19 18:34:59326 ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
327 kFakeAudioFrameSize, kNoTimestamp);
jrummell@chromium.org47b37a62013-07-10 03:56:10328 scoped_refptr<AudioBuffer> frame_b = 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);
xhwang@chromium.orgecbb9762012-10-24 22:33:54332 decoded_frame_list_.push_back(frame_a);
333 decoded_frame_list_.push_back(frame_b);
xhwang@chromium.org97a9ce42012-10-19 10:06:43334
xhwang@chromium.org97a9ce42012-10-19 10:06:43335 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48336 .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
xhwang@chromium.org97a9ce42012-10-19 10:06:43337
xhwang@chromium.org5c5e9b72014-06-17 19:21:12338 EXPECT_CALL(*this, FrameReady(decoded_frame_));
339 EXPECT_CALL(*this, FrameReady(frame_a));
340 EXPECT_CALL(*this, FrameReady(frame_b));
dalecurtis8f75b992016-03-30 17:54:59341 DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
xhwang@chromium.org97a9ce42012-10-19 10:06:43342}
343
344// Test the case where the decryptor receives end-of-stream buffer.
345TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
346 Initialize();
347 EnterNormalDecodingState();
348 EnterEndOfStreamState();
349}
350
xhwang65c23032017-02-17 04:37:42351// Test reinitializing decode with a new encrypted config.
352TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToEncrypted) {
xhwang@chromium.org7c39486c2013-01-06 15:36:09353 Initialize();
354
rileya@chromium.orgd70157792014-03-17 21:45:03355 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
356 .Times(AtMost(1))
357 .WillOnce(RunCallback<1>(true));
xhwang@chromium.org7c39486c2013-01-06 15:36:09358
xhwang@chromium.orgecf16912013-01-12 16:55:22359 // The new config is different from the initial config in bits-per-channel,
360 // channel layout and samples_per_second.
361 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
chcunningham9812dd82015-10-20 01:42:09362 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
dougsteed8d5275f2016-03-12 00:04:30363 AesCtrEncryptionScheme());
xhwang@chromium.orgecf16912013-01-12 16:55:22364 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
365 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
366 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
xhwang65c23032017-02-17 04:37:42367 ASSERT_TRUE(new_config.is_encrypted());
368
369 ReinitializeConfigChange(new_config);
370 base::RunLoop().RunUntilIdle();
371}
372
373// Test reinitializing decode with a new clear config.
374TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToClear) {
375 Initialize();
376
377 EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
378 .Times(AtMost(1))
379 .WillOnce(RunCallback<1>(true));
380
381 // The new config is different from the initial config in bits-per-channel,
382 // channel layout and samples_per_second.
383 AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
384 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
385 Unencrypted());
386 EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
387 EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
388 EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
389 ASSERT_FALSE(new_config.is_encrypted());
xhwang@chromium.orgecf16912013-01-12 16:55:22390
rileya@chromium.orgd70157792014-03-17 21:45:03391 ReinitializeConfigChange(new_config);
fdorayf920dcf2016-06-27 17:16:59392 base::RunLoop().RunUntilIdle();
xhwang@chromium.org7c39486c2013-01-06 15:36:09393}
394
xhwang@chromium.org97a9ce42012-10-19 10:06:43395// Test the case where the a key is added when the decryptor is in
396// kWaitingForKey state.
397TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
398 Initialize();
399 EnterWaitingForKeyState();
400
401 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48402 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50403 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59404 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43405 key_added_cb_.Run();
fdorayf920dcf2016-06-27 17:16:59406 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43407}
408
409// Test the case where the a key is added when the decryptor is in
410// kPendingDecode state.
411TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
412 Initialize();
413 EnterPendingDecodeState();
414
415 EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
xhwang@chromium.org8b10f2222012-11-13 05:49:48416 .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
sergeyu@chromium.org49cea862014-06-11 11:11:50417 EXPECT_CALL(*this, FrameReady(decoded_frame_));
dalecurtis8f75b992016-03-30 17:54:59418 EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
xhwang@chromium.org97a9ce42012-10-19 10:06:43419 // The audio decode callback is returned after the correct decryption key is
420 // added.
421 key_added_cb_.Run();
Dale Curtise25163812018-09-21 22:13:39422 std::move(pending_audio_decode_cb_)
Xiaohan Wangc03ce232018-05-30 21:07:58423 .Run(Decryptor::kNoKey, Decryptor::AudioFrames());
fdorayf920dcf2016-06-27 17:16:59424 base::RunLoop().RunUntilIdle();
xhwang@chromium.org97a9ce42012-10-19 10:06:43425}
426
427// Test resetting when the decoder is in kIdle state but has not decoded any
428// frame.
429TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
430 Initialize();
431 Reset();
432}
433
434// Test resetting when the decoder is in kIdle state after it has decoded one
435// frame.
436TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
437 Initialize();
438 EnterNormalDecodingState();
439 Reset();
440}
441
xhwang@chromium.org97a9ce42012-10-19 10:06:43442// Test resetting when the decoder is in kPendingDecode state.
443TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
444 Initialize();
445 EnterPendingDecodeState();
446
dalecurtis8f75b992016-03-30 17:54:59447 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43448
449 Reset();
450}
451
452// Test resetting when the decoder is in kWaitingForKey state.
453TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
454 Initialize();
455 EnterWaitingForKeyState();
456
dalecurtis8f75b992016-03-30 17:54:59457 EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
xhwang@chromium.org97a9ce42012-10-19 10:06:43458
459 Reset();
460}
461
462// Test resetting when the decoder has hit end of stream and is in
463// kDecodeFinished state.
464TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
465 Initialize();
466 EnterNormalDecodingState();
467 EnterEndOfStreamState();
468 Reset();
469}
470
471// Test resetting after the decoder has been reset.
472TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
473 Initialize();
474 EnterNormalDecodingState();
475 Reset();
476 Reset();
477}
478
xhwang@chromium.org97a9ce42012-10-19 10:06:43479} // namespace media