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