Upload upstream chromium 85.0.4183.84
[platform/framework/web/chromium-efl.git] / 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 <stdint.h>
6
7 #include <string>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/run_loop.h"
13 #include "base/stl_util.h"
14 #include "base/test/gmock_callback_support.h"
15 #include "base/test/task_environment.h"
16 #include "media/base/audio_buffer.h"
17 #include "media/base/decoder_buffer.h"
18 #include "media/base/decrypt_config.h"
19 #include "media/base/media_util.h"
20 #include "media/base/mock_filters.h"
21 #include "media/base/test_helpers.h"
22 #include "media/base/timestamp_constants.h"
23 #include "media/filters/decrypting_audio_decoder.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25
26 using ::base::test::RunCallback;
27 using ::base::test::RunOnceCallback;
28 using ::testing::_;
29 using ::testing::AtMost;
30 using ::testing::Return;
31 using ::testing::SaveArg;
32 using ::testing::StrictMock;
33
34 namespace media {
35
36 const int kSampleRate = 44100;
37
38 // Make sure the kFakeAudioFrameSize is a valid frame size for all audio decoder
39 // configs used in this test.
40 const int kFakeAudioFrameSize = 48;
41 const uint8_t kFakeKeyId[] = {0x4b, 0x65, 0x79, 0x20, 0x49, 0x44};
42 const uint8_t kFakeIv[DecryptConfig::kDecryptionKeySize] = {0};
43 const int kDecodingDelay = 3;
44
45 // Create a fake non-empty encrypted buffer.
46 static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() {
47   const int buffer_size = 16;  // Need a non-empty buffer;
48   scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size));
49   buffer->set_decrypt_config(DecryptConfig::CreateCencConfig(
50       std::string(reinterpret_cast<const char*>(kFakeKeyId),
51                   base::size(kFakeKeyId)),
52       std::string(reinterpret_cast<const char*>(kFakeIv), base::size(kFakeIv)),
53       std::vector<SubsampleEntry>()));
54   return buffer;
55 }
56
57 class DecryptingAudioDecoderTest : public testing::Test {
58  public:
59   DecryptingAudioDecoderTest()
60       : decoder_(new DecryptingAudioDecoder(
61             task_environment_.GetMainThreadTaskRunner(),
62             &media_log_)),
63         cdm_context_(new StrictMock<MockCdmContext>()),
64         decryptor_(new StrictMock<MockDecryptor>()),
65         num_decrypt_and_decode_calls_(0),
66         num_frames_in_decryptor_(0),
67         encrypted_buffer_(CreateFakeEncryptedBuffer()),
68         decoded_frame_(nullptr),
69         decoded_frame_list_() {}
70
71   ~DecryptingAudioDecoderTest() override { Destroy(); }
72
73   void InitializeAndExpectResult(const AudioDecoderConfig& config,
74                                  bool success) {
75     // Initialize data now that the config is known. Since the code uses
76     // invalid values (that CreateEmptyBuffer() doesn't support), tweak them
77     // just for CreateEmptyBuffer().
78     int channels = ChannelLayoutToChannelCount(config.channel_layout());
79     if (channels < 0)
80       channels = 0;
81     decoded_frame_ = AudioBuffer::CreateEmptyBuffer(
82         config.channel_layout(), channels, kSampleRate, kFakeAudioFrameSize,
83         kNoTimestamp);
84     decoded_frame_list_.push_back(decoded_frame_);
85
86     decoder_->Initialize(config, cdm_context_.get(),
87                          base::BindOnce(
88                              [](bool success, Status status) {
89                                EXPECT_EQ(status.is_ok(), success);
90                              },
91                              success),
92                          base::Bind(&DecryptingAudioDecoderTest::FrameReady,
93                                     base::Unretained(this)),
94                          base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
95                                     base::Unretained(this)));
96     base::RunLoop().RunUntilIdle();
97   }
98
99   enum CdmType { CDM_WITHOUT_DECRYPTOR, CDM_WITH_DECRYPTOR };
100
101   void SetCdmType(CdmType cdm_type) {
102     const bool has_decryptor = cdm_type == CDM_WITH_DECRYPTOR;
103     EXPECT_CALL(*cdm_context_, GetDecryptor())
104         .WillRepeatedly(Return(has_decryptor ? decryptor_.get() : nullptr));
105   }
106
107   void Initialize() {
108     SetCdmType(CDM_WITH_DECRYPTOR);
109     EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
110         .Times(AtMost(1))
111         .WillOnce(RunOnceCallback<1>(true));
112     EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
113         .WillOnce(SaveArg<1>(&key_added_cb_));
114
115     config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
116                        CHANNEL_LAYOUT_STEREO, kSampleRate, EmptyExtraData(),
117                        EncryptionScheme::kCenc, base::TimeDelta(), 0);
118     InitializeAndExpectResult(config_, true);
119   }
120
121   void Reinitialize() { ReinitializeConfigChange(config_); }
122
123   void ReinitializeConfigChange(const AudioDecoderConfig& new_config) {
124     EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
125     EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
126         .WillOnce(RunOnceCallback<1>(true));
127     EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
128         .WillOnce(SaveArg<1>(&key_added_cb_));
129     decoder_->Initialize(
130         new_config, cdm_context_.get(),
131         base::BindOnce([](Status status) { EXPECT_TRUE(status.is_ok()); }),
132         base::Bind(&DecryptingAudioDecoderTest::FrameReady,
133                    base::Unretained(this)),
134         base::Bind(&DecryptingAudioDecoderTest::OnWaiting,
135                    base::Unretained(this)));
136   }
137
138   // Decode |buffer| and expect DecodeDone to get called with |status|.
139   void DecodeAndExpect(scoped_refptr<DecoderBuffer> buffer,
140                        DecodeStatus status) {
141     EXPECT_CALL(*this, DecodeDone(status));
142     decoder_->Decode(buffer, base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
143                                         base::Unretained(this)));
144     base::RunLoop().RunUntilIdle();
145   }
146
147   // Helper function to simulate the decrypting and decoding process in the
148   // |decryptor_| with a decoding delay of kDecodingDelay buffers.
149   void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
150                              const Decryptor::AudioDecodeCB& audio_decode_cb) {
151     num_decrypt_and_decode_calls_++;
152     if (!encrypted->end_of_stream())
153       num_frames_in_decryptor_++;
154
155     if (num_decrypt_and_decode_calls_ <= kDecodingDelay ||
156         num_frames_in_decryptor_ == 0) {
157       audio_decode_cb.Run(Decryptor::kNeedMoreData, Decryptor::AudioFrames());
158       return;
159     }
160
161     num_frames_in_decryptor_--;
162     audio_decode_cb.Run(Decryptor::kSuccess,
163                         Decryptor::AudioFrames(1, decoded_frame_));
164   }
165
166   // Sets up expectations and actions to put DecryptingAudioDecoder in an
167   // active normal decoding state.
168   void EnterNormalDecodingState() {
169     EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
170         .WillRepeatedly(
171             Invoke(this, &DecryptingAudioDecoderTest::DecryptAndDecodeAudio));
172     EXPECT_CALL(*this, FrameReady(decoded_frame_));
173     for (int i = 0; i < kDecodingDelay + 1; ++i)
174       DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
175   }
176
177   // Sets up expectations and actions to put DecryptingAudioDecoder in an end
178   // of stream state. This function must be called after
179   // EnterNormalDecodingState() to work.
180   void EnterEndOfStreamState() {
181     // The codec in the |decryptor_| will be flushed.
182     EXPECT_CALL(*this, FrameReady(decoded_frame_)).Times(kDecodingDelay);
183     DecodeAndExpect(DecoderBuffer::CreateEOSBuffer(), DecodeStatus::OK);
184     EXPECT_EQ(0, num_frames_in_decryptor_);
185   }
186
187   // Make the audio decode callback pending by saving and not firing it.
188   void EnterPendingDecodeState() {
189     EXPECT_TRUE(!pending_audio_decode_cb_);
190     EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
191         .WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
192
193     decoder_->Decode(encrypted_buffer_,
194                      base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
195                                 base::Unretained(this)));
196     base::RunLoop().RunUntilIdle();
197     // Make sure the Decode() on the decoder triggers a DecryptAndDecode() on
198     // the decryptor.
199     EXPECT_FALSE(!pending_audio_decode_cb_);
200   }
201
202   void EnterWaitingForKeyState() {
203     EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
204         .WillRepeatedly(
205             RunCallback<1>(Decryptor::kNoKey, Decryptor::AudioFrames()));
206     EXPECT_CALL(*this, OnWaiting(WaitingReason::kNoDecryptionKey));
207     decoder_->Decode(encrypted_buffer_,
208                      base::Bind(&DecryptingAudioDecoderTest::DecodeDone,
209                                 base::Unretained(this)));
210
211     base::RunLoop().RunUntilIdle();
212   }
213
214   void AbortPendingAudioDecodeCB() {
215     if (pending_audio_decode_cb_) {
216       std::move(pending_audio_decode_cb_)
217           .Run(Decryptor::kSuccess, Decryptor::AudioFrames());
218     }
219   }
220
221   void AbortAllPendingCBs() {
222     if (pending_init_cb_) {
223       ASSERT_TRUE(!pending_audio_decode_cb_);
224       std::move(pending_init_cb_).Run(false);
225       return;
226     }
227
228     AbortPendingAudioDecodeCB();
229   }
230
231   void Reset() {
232     EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
233         .WillRepeatedly(InvokeWithoutArgs(
234             this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
235
236     decoder_->Reset(NewExpectedClosure());
237     base::RunLoop().RunUntilIdle();
238   }
239
240   void Destroy() {
241     EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
242         .WillRepeatedly(InvokeWithoutArgs(
243             this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
244
245     decoder_.reset();
246     base::RunLoop().RunUntilIdle();
247   }
248
249   MOCK_METHOD1(FrameReady, void(scoped_refptr<AudioBuffer>));
250   MOCK_METHOD1(DecodeDone, void(DecodeStatus));
251
252   MOCK_METHOD1(OnWaiting, void(WaitingReason));
253
254   base::test::SingleThreadTaskEnvironment task_environment_;
255   NullMediaLog media_log_;
256   std::unique_ptr<DecryptingAudioDecoder> decoder_;
257   std::unique_ptr<StrictMock<MockCdmContext>> cdm_context_;
258   std::unique_ptr<StrictMock<MockDecryptor>> decryptor_;
259   AudioDecoderConfig config_;
260
261   // Variables to help the |decryptor_| to simulate decoding delay and flushing.
262   int num_decrypt_and_decode_calls_;
263   int num_frames_in_decryptor_;
264
265   Decryptor::DecoderInitCB pending_init_cb_;
266   Decryptor::NewKeyCB key_added_cb_;
267   Decryptor::AudioDecodeCB pending_audio_decode_cb_;
268
269   // Constant buffer/frames, to be used/returned by |decoder_| and |decryptor_|.
270   scoped_refptr<DecoderBuffer> encrypted_buffer_;
271   scoped_refptr<AudioBuffer> decoded_frame_;
272   Decryptor::AudioFrames decoded_frame_list_;
273
274  private:
275   DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
276 };
277
278 TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
279   Initialize();
280 }
281
282 // Ensure decoder handles invalid audio configs without crashing.
283 TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
284   AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
285                             CHANNEL_LAYOUT_STEREO, 0, EmptyExtraData(),
286                             EncryptionScheme::kCenc);
287
288   InitializeAndExpectResult(config, false);
289 }
290
291 // Ensure decoder handles unsupported audio configs without crashing.
292 TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
293   SetCdmType(CDM_WITH_DECRYPTOR);
294   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
295       .WillOnce(RunOnceCallback<1>(false));
296
297   AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
298                             CHANNEL_LAYOUT_STEREO, kSampleRate,
299                             EmptyExtraData(), EncryptionScheme::kCenc);
300   InitializeAndExpectResult(config, false);
301 }
302
303 TEST_F(DecryptingAudioDecoderTest, Initialize_CdmWithoutDecryptor) {
304   SetCdmType(CDM_WITHOUT_DECRYPTOR);
305   AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
306                             CHANNEL_LAYOUT_STEREO, kSampleRate,
307                             EmptyExtraData(), EncryptionScheme::kCenc);
308   InitializeAndExpectResult(config, false);
309 }
310
311 // Test normal decrypt and decode case.
312 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
313   Initialize();
314   EnterNormalDecodingState();
315 }
316
317 // Test the case where the decryptor returns error when doing decrypt and
318 // decode.
319 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
320   Initialize();
321
322   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
323       .WillRepeatedly(
324           RunCallback<1>(Decryptor::kError, Decryptor::AudioFrames()));
325
326   DecodeAndExpect(encrypted_buffer_, DecodeStatus::DECODE_ERROR);
327 }
328
329 // Test the case where the decryptor returns multiple decoded frames.
330 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
331   Initialize();
332
333   scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
334       config_.channel_layout(),
335       ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
336       kFakeAudioFrameSize, kNoTimestamp);
337   scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer(
338       config_.channel_layout(),
339       ChannelLayoutToChannelCount(config_.channel_layout()), kSampleRate,
340       kFakeAudioFrameSize, kNoTimestamp);
341   decoded_frame_list_.push_back(frame_a);
342   decoded_frame_list_.push_back(frame_b);
343
344   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
345       .WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
346
347   EXPECT_CALL(*this, FrameReady(decoded_frame_));
348   EXPECT_CALL(*this, FrameReady(frame_a));
349   EXPECT_CALL(*this, FrameReady(frame_b));
350   DecodeAndExpect(encrypted_buffer_, DecodeStatus::OK);
351 }
352
353 // Test the case where the decryptor receives end-of-stream buffer.
354 TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
355   Initialize();
356   EnterNormalDecodingState();
357   EnterEndOfStreamState();
358 }
359
360 // Test reinitializing decode with a new encrypted config.
361 TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToEncrypted) {
362   Initialize();
363
364   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
365       .Times(AtMost(1))
366       .WillOnce(RunOnceCallback<1>(true));
367
368   // The new config is different from the initial config in bits-per-channel,
369   // channel layout and samples_per_second.
370   AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
371                                 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
372                                 EncryptionScheme::kCenc);
373   EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
374   EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
375   EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
376   ASSERT_TRUE(new_config.is_encrypted());
377
378   ReinitializeConfigChange(new_config);
379   base::RunLoop().RunUntilIdle();
380 }
381
382 // Test reinitializing decode with a new clear config.
383 TEST_F(DecryptingAudioDecoderTest, Reinitialize_EncryptedToClear) {
384   Initialize();
385
386   EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
387       .Times(AtMost(1))
388       .WillOnce(RunOnceCallback<1>(true));
389
390   // The new config is different from the initial config in bits-per-channel,
391   // channel layout and samples_per_second.
392   AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
393                                 CHANNEL_LAYOUT_5_1, 88200, EmptyExtraData(),
394                                 EncryptionScheme::kUnencrypted);
395   EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
396   EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
397   EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
398   ASSERT_FALSE(new_config.is_encrypted());
399
400   ReinitializeConfigChange(new_config);
401   base::RunLoop().RunUntilIdle();
402 }
403
404 // Test the case where the a key is added when the decryptor is in
405 // kWaitingForKey state.
406 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
407   Initialize();
408   EnterWaitingForKeyState();
409
410   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
411       .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
412   EXPECT_CALL(*this, FrameReady(decoded_frame_));
413   EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
414   key_added_cb_.Run();
415   base::RunLoop().RunUntilIdle();
416 }
417
418 // Test the case where the a key is added when the decryptor is in
419 // kPendingDecode state.
420 TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
421   Initialize();
422   EnterPendingDecodeState();
423
424   EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
425       .WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
426   EXPECT_CALL(*this, FrameReady(decoded_frame_));
427   EXPECT_CALL(*this, DecodeDone(DecodeStatus::OK));
428   // The audio decode callback is returned after the correct decryption key is
429   // added.
430   key_added_cb_.Run();
431   std::move(pending_audio_decode_cb_)
432       .Run(Decryptor::kNoKey, Decryptor::AudioFrames());
433   base::RunLoop().RunUntilIdle();
434 }
435
436 // Test resetting when the decoder is in kIdle state but has not decoded any
437 // frame.
438 TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
439   Initialize();
440   Reset();
441 }
442
443 // Test resetting when the decoder is in kIdle state after it has decoded one
444 // frame.
445 TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
446   Initialize();
447   EnterNormalDecodingState();
448   Reset();
449 }
450
451 // Test resetting when the decoder is in kPendingDecode state.
452 TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
453   Initialize();
454   EnterPendingDecodeState();
455
456   EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
457
458   Reset();
459 }
460
461 // Test resetting when the decoder is in kWaitingForKey state.
462 TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
463   Initialize();
464   EnterWaitingForKeyState();
465
466   EXPECT_CALL(*this, DecodeDone(DecodeStatus::ABORTED));
467
468   Reset();
469 }
470
471 // Test resetting when the decoder has hit end of stream and is in
472 // kDecodeFinished state.
473 TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
474   Initialize();
475   EnterNormalDecodingState();
476   EnterEndOfStreamState();
477   Reset();
478 }
479
480 // Test resetting after the decoder has been reset.
481 TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
482   Initialize();
483   EnterNormalDecodingState();
484   Reset();
485   Reset();
486 }
487
488 }  // namespace media