decrypting_audio_decoder_unittest.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
1// 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
5#include <string>
6#include <vector>
7
8#include "base/bind.h"
9#include "base/callback_helpers.h"
10#include "base/message_loop.h"
11#include "media/base/buffers.h"
12#include "media/base/data_buffer.h"
13#include "media/base/decoder_buffer.h"
14#include "media/base/decrypt_config.h"
15#include "media/base/gmock_callback_support.h"
16#include "media/base/mock_filters.h"
17#include "media/base/test_helpers.h"
18#include "media/filters/decrypting_audio_decoder.h"
19#include "testing/gmock/include/gmock/gmock.h"
20
21using ::testing::_;
22using ::testing::AtMost;
23using ::testing::IsNull;
24using ::testing::SaveArg;
25using ::testing::StrictMock;
26
27namespace media {
28
29// Make sure the kFakeAudioFrameSize is a valid frame size for all audio decoder
30// configs used in this test.
31static const int kFakeAudioFrameSize = 48;
32static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 };
33static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 };
34
35// Create a fake non-empty encrypted buffer.
36static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() {
37  const int buffer_size = 16;  // Need a non-empty buffer;
38  scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size));
39  buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig(
40      std::string(reinterpret_cast<const char*>(kFakeKeyId),
41                  arraysize(kFakeKeyId)),
42      std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)),
43      0,
44      std::vector<SubsampleEntry>())));
45  return buffer;
46}
47
48// Use anonymous namespace here to prevent the actions to be defined multiple
49// times across multiple test files. Sadly we can't use static for them.
50namespace {
51
52ACTION_P(ReturnBuffer, buffer) {
53  arg0.Run(buffer.get() ? DemuxerStream::kOk : DemuxerStream::kAborted, buffer);
54}
55
56ACTION_P(RunCallbackIfNotNull, param) {
57  if (!arg0.is_null())
58    arg0.Run(param);
59}
60
61ACTION_P2(ResetAndRunCallback, callback, param) {
62  base::ResetAndReturn(callback).Run(param);
63}
64
65MATCHER(IsEndOfStream, "end of stream") {
66  return (arg->IsEndOfStream());
67}
68
69}  // namespace
70
71class DecryptingAudioDecoderTest : public testing::Test {
72 public:
73  DecryptingAudioDecoderTest()
74      : decoder_(new DecryptingAudioDecoder(
75            message_loop_.message_loop_proxy(),
76            base::Bind(
77                &DecryptingAudioDecoderTest::RequestDecryptorNotification,
78                base::Unretained(this)))),
79        decryptor_(new StrictMock<MockDecryptor>()),
80        demuxer_(new StrictMock<MockDemuxerStream>(DemuxerStream::AUDIO)),
81        encrypted_buffer_(CreateFakeEncryptedBuffer()),
82        decoded_frame_(NULL),
83        end_of_stream_frame_(DataBuffer::CreateEOSBuffer()),
84        decoded_frame_list_() {
85    scoped_refptr<DataBuffer> data_buffer = new DataBuffer(kFakeAudioFrameSize);
86    data_buffer->SetDataSize(kFakeAudioFrameSize);
87    // |decoded_frame_| contains random data.
88    decoded_frame_ = data_buffer;
89    decoded_frame_list_.push_back(decoded_frame_);
90  }
91
92  void InitializeAndExpectStatus(const AudioDecoderConfig& config,
93                                 PipelineStatus status) {
94    demuxer_->set_audio_decoder_config(config);
95    decoder_->Initialize(demuxer_.get(), NewExpectedStatusCB(status),
96                         base::Bind(&MockStatisticsCB::OnStatistics,
97                                    base::Unretained(&statistics_cb_)));
98    message_loop_.RunUntilIdle();
99  }
100
101  void Initialize() {
102    EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
103        .Times(AtMost(1))
104        .WillOnce(RunCallback<1>(true));
105    EXPECT_CALL(*this, RequestDecryptorNotification(_))
106        .WillOnce(RunCallbackIfNotNull(decryptor_.get()));
107    EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
108        .WillOnce(SaveArg<1>(&key_added_cb_));
109
110    config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
111                       CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, true, true);
112    InitializeAndExpectStatus(config_, PIPELINE_OK);
113
114    EXPECT_EQ(DecryptingAudioDecoder::kSupportedBitsPerChannel,
115              decoder_->bits_per_channel());
116    EXPECT_EQ(config_.channel_layout(), decoder_->channel_layout());
117    EXPECT_EQ(config_.samples_per_second(), decoder_->samples_per_second());
118  }
119
120  void ReadAndExpectFrameReadyWith(
121      AudioDecoder::Status status,
122      const scoped_refptr<DataBuffer>& audio_frame) {
123    if (status != AudioDecoder::kOk)
124      EXPECT_CALL(*this, FrameReady(status, IsNull()));
125    else if (audio_frame->IsEndOfStream())
126      EXPECT_CALL(*this, FrameReady(status, IsEndOfStream()));
127    else
128      EXPECT_CALL(*this, FrameReady(status, audio_frame));
129
130    decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
131                              base::Unretained(this)));
132    message_loop_.RunUntilIdle();
133  }
134
135  // Sets up expectations and actions to put DecryptingAudioDecoder in an
136  // active normal decoding state.
137  void EnterNormalDecodingState() {
138    Decryptor::AudioBuffers end_of_stream_frames_(1, end_of_stream_frame_);
139
140    EXPECT_CALL(*demuxer_, Read(_))
141        .WillOnce(ReturnBuffer(encrypted_buffer_))
142        .WillRepeatedly(ReturnBuffer(DecoderBuffer::CreateEOSBuffer()));
143    EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
144        .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_))
145        .WillRepeatedly(RunCallback<1>(Decryptor::kNeedMoreData,
146                                       Decryptor::AudioBuffers()));
147    EXPECT_CALL(statistics_cb_, OnStatistics(_));
148
149    ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
150  }
151
152  // Sets up expectations and actions to put DecryptingAudioDecoder in an end
153  // of stream state. This function must be called after
154  // EnterNormalDecodingState() to work.
155  void EnterEndOfStreamState() {
156    ReadAndExpectFrameReadyWith(AudioDecoder::kOk, end_of_stream_frame_);
157  }
158
159  // Make the read callback pending by saving and not firing it.
160  void EnterPendingReadState() {
161    EXPECT_TRUE(pending_demuxer_read_cb_.is_null());
162    EXPECT_CALL(*demuxer_, Read(_))
163        .WillOnce(SaveArg<0>(&pending_demuxer_read_cb_));
164    decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
165                              base::Unretained(this)));
166    message_loop_.RunUntilIdle();
167    // Make sure the Read() on the decoder triggers a Read() on the demuxer.
168    EXPECT_FALSE(pending_demuxer_read_cb_.is_null());
169  }
170
171  // Make the audio decode callback pending by saving and not firing it.
172  void EnterPendingDecodeState() {
173    EXPECT_TRUE(pending_audio_decode_cb_.is_null());
174    EXPECT_CALL(*demuxer_, Read(_))
175        .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
176    EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
177        .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
178
179    decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
180                              base::Unretained(this)));
181    message_loop_.RunUntilIdle();
182    // Make sure the Read() on the decoder triggers a DecryptAndDecode() on the
183    // decryptor.
184    EXPECT_FALSE(pending_audio_decode_cb_.is_null());
185  }
186
187  void EnterWaitingForKeyState() {
188    EXPECT_CALL(*demuxer_, Read(_))
189        .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
190    EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
191        .WillRepeatedly(RunCallback<1>(Decryptor::kNoKey,
192                                       Decryptor::AudioBuffers()));
193    decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
194                              base::Unretained(this)));
195    message_loop_.RunUntilIdle();
196  }
197
198  void AbortPendingAudioDecodeCB() {
199    if (!pending_audio_decode_cb_.is_null()) {
200      base::ResetAndReturn(&pending_audio_decode_cb_).Run(
201          Decryptor::kSuccess, Decryptor::AudioBuffers());
202    }
203  }
204
205  void Reset() {
206    EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
207        .WillRepeatedly(InvokeWithoutArgs(
208            this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
209
210    decoder_->Reset(NewExpectedClosure());
211    message_loop_.RunUntilIdle();
212  }
213
214  MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&));
215
216  MOCK_METHOD2(FrameReady, void(AudioDecoder::Status,
217                                const scoped_refptr<DataBuffer>&));
218
219  base::MessageLoop message_loop_;
220  scoped_ptr<DecryptingAudioDecoder> decoder_;
221  scoped_ptr<StrictMock<MockDecryptor> > decryptor_;
222  scoped_ptr<StrictMock<MockDemuxerStream> > demuxer_;
223  MockStatisticsCB statistics_cb_;
224  AudioDecoderConfig config_;
225
226  DemuxerStream::ReadCB pending_demuxer_read_cb_;
227  Decryptor::DecoderInitCB pending_init_cb_;
228  Decryptor::NewKeyCB key_added_cb_;
229  Decryptor::AudioDecodeCB pending_audio_decode_cb_;
230
231  // Constant buffer/frames to be returned by the |demuxer_| and |decryptor_|.
232  scoped_refptr<DecoderBuffer> encrypted_buffer_;
233  scoped_refptr<DataBuffer> decoded_frame_;
234  scoped_refptr<DataBuffer> end_of_stream_frame_;
235  Decryptor::AudioBuffers decoded_frame_list_;
236
237 private:
238  DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
239};
240
241TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
242  Initialize();
243}
244
245// Ensure that DecryptingAudioDecoder only accepts encrypted audio.
246TEST_F(DecryptingAudioDecoderTest, Initialize_UnencryptedAudioConfig) {
247  AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
248                            CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, false);
249
250  InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
251}
252
253// Ensure decoder handles invalid audio configs without crashing.
254TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
255  AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
256                            CHANNEL_LAYOUT_STEREO, 0, NULL, 0, true);
257
258  InitializeAndExpectStatus(config, PIPELINE_ERROR_DECODE);
259}
260
261// Ensure decoder handles unsupported audio configs without crashing.
262TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
263  EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
264      .WillOnce(RunCallback<1>(false));
265  EXPECT_CALL(*this, RequestDecryptorNotification(_))
266      .WillOnce(RunCallbackIfNotNull(decryptor_.get()));
267
268  AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
269                            CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, true);
270  InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
271}
272
273TEST_F(DecryptingAudioDecoderTest, Initialize_NullDecryptor) {
274  EXPECT_CALL(*this, RequestDecryptorNotification(_))
275      .WillRepeatedly(RunCallbackIfNotNull(static_cast<Decryptor*>(NULL)));
276
277  AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
278                            CHANNEL_LAYOUT_STEREO, 44100, NULL, 0, true);
279  InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
280}
281
282// Test normal decrypt and decode case.
283TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
284  Initialize();
285  EnterNormalDecodingState();
286}
287
288// Test the case where the decryptor returns error when doing decrypt and
289// decode.
290TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
291  Initialize();
292
293  EXPECT_CALL(*demuxer_, Read(_))
294      .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
295  EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
296      .WillRepeatedly(RunCallback<1>(Decryptor::kError,
297                                     Decryptor::AudioBuffers()));
298
299  ReadAndExpectFrameReadyWith(AudioDecoder::kDecodeError, NULL);
300}
301
302// Test the case where the decryptor returns kNeedMoreData to ask for more
303// buffers before it can produce a frame.
304TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_NeedMoreData) {
305  Initialize();
306
307  EXPECT_CALL(*demuxer_, Read(_))
308      .Times(2)
309      .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
310  EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
311      .WillOnce(RunCallback<1>(Decryptor::kNeedMoreData,
312                               Decryptor::AudioBuffers()))
313      .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
314  EXPECT_CALL(statistics_cb_, OnStatistics(_))
315      .Times(2);
316
317  ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
318}
319
320// Test the case where the decryptor returns multiple decoded frames.
321TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
322  Initialize();
323
324  scoped_refptr<DataBuffer> frame_a = new DataBuffer(kFakeAudioFrameSize);
325  frame_a->SetDataSize(kFakeAudioFrameSize);
326  scoped_refptr<DataBuffer> frame_b = new DataBuffer(kFakeAudioFrameSize);
327  frame_b->SetDataSize(kFakeAudioFrameSize);
328  decoded_frame_list_.push_back(frame_a);
329  decoded_frame_list_.push_back(frame_b);
330
331  EXPECT_CALL(*demuxer_, Read(_))
332      .WillOnce(ReturnBuffer(encrypted_buffer_));
333  EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
334      .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
335  EXPECT_CALL(statistics_cb_, OnStatistics(_));
336
337  ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
338  ReadAndExpectFrameReadyWith(AudioDecoder::kOk, frame_a);
339  ReadAndExpectFrameReadyWith(AudioDecoder::kOk, frame_b);
340}
341
342// Test the case where the decryptor receives end-of-stream buffer.
343TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
344  Initialize();
345  EnterNormalDecodingState();
346  EnterEndOfStreamState();
347}
348
349// Test aborted read on the demuxer stream.
350TEST_F(DecryptingAudioDecoderTest, DemuxerRead_Aborted) {
351  Initialize();
352
353  // ReturnBuffer() with NULL triggers aborted demuxer read.
354  EXPECT_CALL(*demuxer_, Read(_))
355      .WillOnce(ReturnBuffer(scoped_refptr<DecoderBuffer>()));
356
357  ReadAndExpectFrameReadyWith(AudioDecoder::kAborted, NULL);
358}
359
360// Test config change on the demuxer stream.
361TEST_F(DecryptingAudioDecoderTest, DemuxerRead_ConfigChange) {
362  Initialize();
363
364  // The new config is different from the initial config in bits-per-channel,
365  // channel layout and samples_per_second.
366  AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
367                                CHANNEL_LAYOUT_5_1, 88200, NULL, 0, false);
368  EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
369  EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
370  EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
371
372  demuxer_->set_audio_decoder_config(new_config);
373  EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
374  EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
375      .WillOnce(RunCallback<1>(true));
376  EXPECT_CALL(*demuxer_, Read(_))
377      .WillOnce(RunCallback<0>(DemuxerStream::kConfigChanged,
378                               scoped_refptr<DecoderBuffer>()))
379      .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
380  EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
381      .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
382  EXPECT_CALL(statistics_cb_, OnStatistics(_));
383
384  ReadAndExpectFrameReadyWith(AudioDecoder::kOk, decoded_frame_);
385
386  EXPECT_EQ(new_config.bits_per_channel(), decoder_->bits_per_channel());
387  EXPECT_EQ(new_config.channel_layout(), decoder_->channel_layout());
388  EXPECT_EQ(new_config.samples_per_second(), decoder_->samples_per_second());
389}
390
391// Test config change failure.
392TEST_F(DecryptingAudioDecoderTest, DemuxerRead_ConfigChangeFailed) {
393  Initialize();
394
395  EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
396  EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
397      .WillOnce(RunCallback<1>(false));
398  EXPECT_CALL(*demuxer_, Read(_))
399      .WillOnce(RunCallback<0>(DemuxerStream::kConfigChanged,
400                               scoped_refptr<DecoderBuffer>()))
401      .WillRepeatedly(ReturnBuffer(encrypted_buffer_));
402
403  ReadAndExpectFrameReadyWith(AudioDecoder::kDecodeError, NULL);
404}
405
406// Test the case where the a key is added when the decryptor is in
407// kWaitingForKey state.
408TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
409  Initialize();
410  EnterWaitingForKeyState();
411
412  EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
413      .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
414  EXPECT_CALL(statistics_cb_, OnStatistics(_));
415  EXPECT_CALL(*this, FrameReady(AudioDecoder::kOk, decoded_frame_));
416  key_added_cb_.Run();
417  message_loop_.RunUntilIdle();
418}
419
420// Test the case where the a key is added when the decryptor is in
421// kPendingDecode state.
422TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
423  Initialize();
424  EnterPendingDecodeState();
425
426  EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
427      .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
428  EXPECT_CALL(statistics_cb_, OnStatistics(_));
429  EXPECT_CALL(*this, FrameReady(AudioDecoder::kOk, decoded_frame_));
430  // The audio decode callback is returned after the correct decryption key is
431  // added.
432  key_added_cb_.Run();
433  base::ResetAndReturn(&pending_audio_decode_cb_).Run(
434      Decryptor::kNoKey, Decryptor::AudioBuffers());
435  message_loop_.RunUntilIdle();
436}
437
438// Test resetting when the decoder is in kIdle state but has not decoded any
439// frame.
440TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
441  Initialize();
442  Reset();
443}
444
445// Test resetting when the decoder is in kIdle state after it has decoded one
446// frame.
447TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
448  Initialize();
449  EnterNormalDecodingState();
450  Reset();
451}
452
453// Test resetting when the decoder is in kPendingDemuxerRead state and the read
454// callback is returned with kOk.
455TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_Ok) {
456  Initialize();
457  EnterPendingReadState();
458
459  EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
460
461  Reset();
462  base::ResetAndReturn(&pending_demuxer_read_cb_).Run(DemuxerStream::kOk,
463                                                      encrypted_buffer_);
464  message_loop_.RunUntilIdle();
465}
466
467// Test resetting when the decoder is in kPendingDemuxerRead state and the read
468// callback is returned with kAborted.
469TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_Aborted) {
470  Initialize();
471  EnterPendingReadState();
472
473  // Make sure we get a NULL audio frame returned.
474  EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
475
476  Reset();
477  base::ResetAndReturn(&pending_demuxer_read_cb_).Run(DemuxerStream::kAborted,
478                                                      NULL);
479  message_loop_.RunUntilIdle();
480}
481
482// Test resetting when the decoder is in kPendingDemuxerRead state and the read
483// callback is returned with kConfigChanged.
484TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_ConfigChange) {
485  Initialize();
486  EnterPendingReadState();
487
488  Reset();
489
490  // The new config is different from the initial config in bits-per-channel,
491  // channel layout and samples_per_second.
492  AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
493                                CHANNEL_LAYOUT_5_1, 88200, NULL, 0, false);
494  EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
495  EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
496  EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
497
498  // Even during pending reset, the decoder still needs to be initialized with
499  // the new config.
500  demuxer_->set_audio_decoder_config(new_config);
501  EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
502  EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
503      .WillOnce(RunCallback<1>(true));
504  EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
505
506  base::ResetAndReturn(&pending_demuxer_read_cb_)
507      .Run(DemuxerStream::kConfigChanged, NULL);
508  message_loop_.RunUntilIdle();
509
510  EXPECT_EQ(new_config.bits_per_channel(), decoder_->bits_per_channel());
511  EXPECT_EQ(new_config.channel_layout(), decoder_->channel_layout());
512  EXPECT_EQ(new_config.samples_per_second(), decoder_->samples_per_second());
513}
514
515// Test resetting when the decoder is in kPendingDemuxerRead state, the read
516// callback is returned with kConfigChanged and the config change fails.
517TEST_F(DecryptingAudioDecoderTest, Reset_DuringDemuxerRead_ConfigChangeFailed) {
518  Initialize();
519  EnterPendingReadState();
520
521  Reset();
522
523  // Even during pending reset, the decoder still needs to be initialized with
524  // the new config.
525  EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
526  EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
527      .WillOnce(RunCallback<1>(false));
528  EXPECT_CALL(*this, FrameReady(AudioDecoder::kDecodeError, IsNull()));
529
530  base::ResetAndReturn(&pending_demuxer_read_cb_)
531      .Run(DemuxerStream::kConfigChanged, NULL);
532  message_loop_.RunUntilIdle();
533}
534
535// Test resetting when the decoder is in kPendingConfigChange state.
536TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingConfigChange) {
537  Initialize();
538  EnterNormalDecodingState();
539
540  EXPECT_CALL(*demuxer_, Read(_))
541      .WillOnce(RunCallback<0>(DemuxerStream::kConfigChanged,
542                               scoped_refptr<DecoderBuffer>()));
543  EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
544  EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
545      .WillOnce(SaveArg<1>(&pending_init_cb_));
546
547  decoder_->Read(base::Bind(&DecryptingAudioDecoderTest::FrameReady,
548                            base::Unretained(this)));
549  message_loop_.RunUntilIdle();
550  EXPECT_FALSE(pending_init_cb_.is_null());
551
552  EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
553
554  Reset();
555  base::ResetAndReturn(&pending_init_cb_).Run(true);
556  message_loop_.RunUntilIdle();
557}
558
559// Test resetting when the decoder is in kPendingDecode state.
560TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
561  Initialize();
562  EnterPendingDecodeState();
563
564  EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
565
566  Reset();
567}
568
569// Test resetting when the decoder is in kWaitingForKey state.
570TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
571  Initialize();
572  EnterWaitingForKeyState();
573
574  EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
575
576  Reset();
577}
578
579// Test resetting when the decoder has hit end of stream and is in
580// kDecodeFinished state.
581TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
582  Initialize();
583  EnterNormalDecodingState();
584  EnterEndOfStreamState();
585  Reset();
586}
587
588// Test resetting after the decoder has been reset.
589TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
590  Initialize();
591  EnterNormalDecodingState();
592  Reset();
593  Reset();
594}
595
596}  // namespace media
597