1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define private public
17 #include "audio_codec_decoder_adapter_impl.h"
18 #include "audio_cenc_info_adapter_impl.h"
19 #undef private
20 #include "native_drm_common.h"
21 #include "native_avcodec_base.h"
22
23 #include "nweb_log.h"
24 #include "gtest/gtest.h"
25 #include <gmock/gmock.h>
26 #include <thread>
27 #include <chrono>
28
29 using namespace testing;
30 using namespace testing::ext;
31 using testing::ext::TestSize;
32
33 namespace OHOS {
34 namespace NWeb {
35 const char *OH_AVCODEC_MIMETYPE_AUDIO_MPEG = "audio/mpeg";
36 const char *OH_AVCODEC_NAME_AUDIO_MPEG = "OH.Media.Codec.Decoder.Audio.Mpeg";
37 const int64_t WAIT_FOR_BUFFER_TIMEOUT = 500;
38
39 class AudioDecoderCallbackImplTest : public testing::Test {};
40
41 class AudioDecoderCallbackAdapterMock : public AudioDecoderCallbackAdapter {
42 public:
43 AudioDecoderCallbackAdapterMock() = default;
44
45 ~AudioDecoderCallbackAdapterMock() override = default;
46
OnError(int32_t errorCode)47 void OnError(int32_t errorCode) override {}
48
OnOutputFormatChanged()49 void OnOutputFormatChanged() override {}
50
OnInputBufferAvailable(uint32_t index)51 void OnInputBufferAvailable(uint32_t index) override {}
52
OnOutputBufferAvailable(uint32_t index,uint8_t * bufferData,int32_t size,int64_t pts,int32_t offset,uint32_t flags)53 void OnOutputBufferAvailable(
54 uint32_t index, uint8_t *bufferData, int32_t size, int64_t pts, int32_t offset, uint32_t flags) override {}
55 };
56
57 class AudioDecoderFormatAdapterMock : public AudioDecoderFormatAdapter {
58 public:
59 AudioDecoderFormatAdapterMock() = default;
60
61 ~AudioDecoderFormatAdapterMock() override = default;
62
GetSampleRate()63 int32_t GetSampleRate() override
64 {
65 return sampleRate_;
66 }
67
GetChannelCount()68 int32_t GetChannelCount() override
69 {
70 return channelCount_;
71 }
72
GetBitRate()73 int64_t GetBitRate() override
74 {
75 return bitRate_;
76 }
77
GetMaxInputSize()78 int32_t GetMaxInputSize() override
79 {
80 return maxInputSize_;
81 }
82
GetAACIsAdts()83 bool GetAACIsAdts() override
84 {
85 return aacIsAdts_;
86 }
87
GetAudioSampleFormat()88 int32_t GetAudioSampleFormat() override
89 {
90 return audioSampleFormat_;
91 }
92
GetIdentificationHeader()93 int32_t GetIdentificationHeader() override
94 {
95 return idHeader_;
96 }
97
GetSetupHeader()98 int32_t GetSetupHeader() override
99 {
100 return setupHeader_;
101 }
102
GetCodecConfig()103 uint8_t* GetCodecConfig() override
104 {
105 return codecConfig_;
106 }
107
GetCodecConfigSize()108 uint32_t GetCodecConfigSize() override
109 {
110 return codecConfigSize_;
111 }
112
SetSampleRate(int32_t sampleRate)113 void SetSampleRate(int32_t sampleRate) override
114 {
115 sampleRate_ = sampleRate;
116 }
117
SetChannelCount(int32_t channelCount)118 void SetChannelCount(int32_t channelCount) override
119 {
120 channelCount_ = channelCount;
121 }
122
SetBitRate(int64_t bitRate)123 void SetBitRate(int64_t bitRate) override
124 {
125 bitRate_ = bitRate;
126 }
127
SetMaxInputSize(int32_t maxInputSize)128 void SetMaxInputSize(int32_t maxInputSize) override
129 {
130 maxInputSize_ = maxInputSize;
131 }
132
SetAudioSampleFormat(int32_t audioSampleFormat)133 void SetAudioSampleFormat(int32_t audioSampleFormat) override
134 {
135 audioSampleFormat_ = audioSampleFormat;
136 }
137
SetAACIsAdts(bool isAdts)138 void SetAACIsAdts(bool isAdts) override
139 {
140 aacIsAdts_ = isAdts;
141 }
142
SetIdentificationHeader(int32_t idHeader)143 void SetIdentificationHeader(int32_t idHeader) override
144 {
145 idHeader_ = idHeader;
146 }
147
SetSetupHeader(int32_t setupHeader)148 void SetSetupHeader(int32_t setupHeader) override
149 {
150 setupHeader_ = setupHeader;
151 }
152
SetCodecConfig(uint8_t * codecConfig)153 void SetCodecConfig(uint8_t* codecConfig) override
154 {
155 codecConfig_ = codecConfig;
156 }
157
SetCodecConfigSize(uint32_t size)158 void SetCodecConfigSize(uint32_t size) override
159 {
160 codecConfigSize_ = size;
161 }
162 private:
163 int32_t sampleRate_ = 0;
164 int32_t channelCount_ = 0;
165 int64_t bitRate_ = 0;
166 int32_t maxInputSize_ = 0;
167 bool aacIsAdts_ = false;
168 int32_t audioSampleFormat_ = 0;
169 int32_t idHeader_ = 0;
170 int32_t setupHeader_ = 0;
171 uint8_t* codecConfig_ = nullptr;
172 uint32_t codecConfigSize_ = 0;
173 };
174
175 /**
176 * @tc.name: AudioDecoderCallbackImpl_NormalTest_001.
177 * @tc.desc: test of AudioDecoderCallbackManager::OnError() OnOutputFormatChanged() OnInputBufferAvailable()
178 OnOutputBufferAvailable()
179 * @tc.type: FUNC.
180 * @tc.require:
181 */
182 HWTEST_F(AudioDecoderCallbackImplTest, AudioDecoderCallbackImpl_NormalTest_001, TestSize.Level1)
183 {
184 std::shared_ptr<AudioCodecDecoderAdapterImpl> decoder = std::make_shared<AudioCodecDecoderAdapterImpl>();
185 EXPECT_NE(decoder, nullptr);
186 EXPECT_EQ(decoder->CreateAudioDecoderByName(std::string(OH_AVCODEC_NAME_AUDIO_MPEG)),
187 AudioDecoderAdapterCode::DECODER_OK);
188 AudioDecoderCallbackManager::OnError(nullptr, 0, nullptr);
189 AudioDecoderCallbackManager::OnError(decoder->GetAVCodec(), 0, nullptr);
190 AudioDecoderCallbackManager::OnOutputFormatChanged(nullptr, 0, nullptr);
191 AudioDecoderCallbackManager::OnOutputFormatChanged(decoder->GetAVCodec(), 0, nullptr);
192 constexpr int32_t MEMSIZE = 1024 * 1024;
193 OH_AVBuffer* buffer = OH_AVBuffer_Create(MEMSIZE);
194 AudioDecoderCallbackManager::OnInputBufferAvailable(nullptr, 0, nullptr, nullptr);
195 AudioDecoderCallbackManager::OnInputBufferAvailable(decoder->GetAVCodec(), 0, nullptr, nullptr);
196 AudioDecoderCallbackManager::OnInputBufferAvailable(decoder->GetAVCodec(), 0, buffer, nullptr);
197 AudioDecoderCallbackManager::OnInputBufferAvailable(
198 decoder->GetAVCodec(), 0, buffer, nullptr);
199 AudioDecoderCallbackManager::OnOutputBufferAvailable(nullptr, 0, nullptr, nullptr);
200 AudioDecoderCallbackManager::OnOutputBufferAvailable(decoder->GetAVCodec(), 0, nullptr, nullptr);
201 AudioDecoderCallbackManager::OnOutputBufferAvailable(decoder->GetAVCodec(), 0, buffer, nullptr);
202 AudioDecoderCallbackManager::OnOutputBufferAvailable(
203 decoder->GetAVCodec(), 0, buffer, nullptr);
204 EXPECT_EQ(decoder->SetCallbackDec(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
205 std::shared_ptr<AudioDecoderCallbackAdapter> callback = std::make_shared<AudioDecoderCallbackAdapterMock>();
206 EXPECT_EQ(decoder->SetCallbackDec(callback), AudioDecoderAdapterCode::DECODER_OK);
207 AudioDecoderCallbackManager::OnError(decoder->GetAVCodec(), 0, nullptr);
208 AudioDecoderCallbackManager::OnOutputFormatChanged(decoder->GetAVCodec(), 0, nullptr);
209 AudioDecoderCallbackManager::OnInputBufferAvailable(
210 decoder->GetAVCodec(), 0, buffer, nullptr);
211 AudioDecoderCallbackManager::OnOutputBufferAvailable(
212 decoder->GetAVCodec(), 0, buffer, nullptr);
213 OH_AVBuffer_Destroy(buffer);
214 buffer = nullptr;
215 std::shared_ptr<AudioDecoderCallbackAdapterImpl> errCallbackImpl =
216 std::make_shared<AudioDecoderCallbackAdapterImpl>(nullptr);
217 errCallbackImpl->OnError(0);
218 errCallbackImpl->OnOutputFormatChanged();
219 errCallbackImpl->OnInputBufferAvailable(0);
220 errCallbackImpl->OnOutputBufferAvailable(0, nullptr, 0, 0, 0, 0);
221 std::shared_ptr<AudioDecoderCallbackAdapterImpl> callbackImpl =
222 std::make_shared<AudioDecoderCallbackAdapterImpl>(callback);
223 callbackImpl->OnError(0);
224 callbackImpl->OnOutputFormatChanged();
225 callbackImpl->OnInputBufferAvailable(0);
226 callbackImpl->OnOutputBufferAvailable(0, nullptr, 0, 0, 0, 0);
227 }
228
229 /**
230 * @tc.name: AudioDecoderCallbackImpl_NormalTest_002.
231 * @tc.desc: test of AudioDecoderCallbackManager
232 * @tc.type: FUNC.
233 * @tc.require:
234 */
235 HWTEST_F(AudioDecoderCallbackImplTest, AudioDecoderCallbackImpl_NormalTest_002, TestSize.Level1)
236 {
237 std::shared_ptr<AudioCodecDecoderAdapterImpl> decoder = std::make_shared<AudioCodecDecoderAdapterImpl>();
238 EXPECT_NE(decoder, nullptr);
239 EXPECT_EQ(decoder->CreateAudioDecoderByName(std::string(OH_AVCODEC_NAME_AUDIO_MPEG)),
240 AudioDecoderAdapterCode::DECODER_OK);
241 AudioDecoderCallbackManager::OnError(nullptr, 0, nullptr);
242 AudioDecoderCallbackManager::OnError(decoder->GetAVCodec(), 0, nullptr);
243 AudioDecoderCallbackManager::OnOutputFormatChanged(nullptr, 0, nullptr);
244 AudioDecoderCallbackManager::OnOutputFormatChanged(decoder->GetAVCodec(), 0, nullptr);
245
246 constexpr int32_t MEMSIZE = 1024 * 1024;
247 OH_AVBuffer* buffer = OH_AVBuffer_Create(MEMSIZE);
248 AudioDecoderCallbackManager::OnInputBufferAvailable(nullptr, 0, nullptr, nullptr);
249 AudioDecoderCallbackManager::OnInputBufferAvailable(decoder->GetAVCodec(), 0, nullptr, nullptr);
250 AudioDecoderCallbackManager::OnInputBufferAvailable(decoder->GetAVCodec(), 0, buffer, nullptr);
251 AudioDecoderCallbackManager::OnInputBufferAvailable(
252 decoder->GetAVCodec(), 0, buffer, nullptr);
253
254 AudioDecoderCallbackManager::OnOutputBufferAvailable(nullptr, 0, nullptr, nullptr);
255 AudioDecoderCallbackManager::OnOutputBufferAvailable(decoder->GetAVCodec(), 0, nullptr, nullptr);
256 AudioDecoderCallbackManager::OnOutputBufferAvailable(decoder->GetAVCodec(), 0, buffer, nullptr);
257 AudioDecoderCallbackManager::OnOutputBufferAvailable(
258 decoder->GetAVCodec(), 0, buffer, nullptr);
259 std::shared_ptr<AudioCodecDecoderAdapterImpl> decoder_1 = std::make_shared<AudioCodecDecoderAdapterImpl>();
260
261 decoder_1->CreateAudioDecoderByName(std::string(OH_AVCODEC_NAME_AUDIO_MPEG));
262 AudioDecoderCallbackManager::AddAudioDecoder(decoder_1.get());
263 AudioDecoderCallbackManager::DeleteAudioDecoder(decoder->GetAVCodec());
264 AudioDecoderCallbackManager::OnInputBufferAvailable(decoder->GetAVCodec(), 0, nullptr, nullptr);
265 AudioDecoderCallbackManager::OnOutputBufferAvailable(decoder->GetAVCodec(), 0, nullptr, nullptr);
266 AudioDecoderCallbackManager::OnError(decoder->GetAVCodec(), 0, nullptr);
267 AudioDecoderCallbackManager::OnOutputFormatChanged(decoder->GetAVCodec(), 0, nullptr);
268
269 AudioDecoderCallbackManager::AddAudioDecoder(nullptr);
270 AudioDecoderCallbackManager::DeleteAudioDecoder(nullptr);
271 EXPECT_EQ(decoder->SetCallbackDec(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
272
273 std::shared_ptr<AudioDecoderCallbackAdapter> callback = std::make_shared<AudioDecoderCallbackAdapterMock>();
274 EXPECT_EQ(decoder->SetCallbackDec(callback), AudioDecoderAdapterCode::DECODER_OK);
275 AudioDecoderCallbackManager::OnError(decoder->GetAVCodec(), 0, nullptr);
276 AudioDecoderCallbackManager::OnOutputFormatChanged(decoder->GetAVCodec(), 0, nullptr);
277 AudioDecoderCallbackManager::OnInputBufferAvailable(
278 decoder->GetAVCodec(), 0, buffer, nullptr);
279 AudioDecoderCallbackManager::OnOutputBufferAvailable(
280 decoder->GetAVCodec(), 0, buffer, nullptr);
281 OH_AVBuffer_Destroy(buffer);
282 buffer = nullptr;
283 }
284
285 /**
286 * @tc.name: AudioDecoderCallbackImpl_NormalTest_003.
287 * @tc.desc: test of AudioDecoderCallbackManager
288 * @tc.type: FUNC.
289 * @tc.require:
290 */
291 HWTEST_F(AudioDecoderCallbackImplTest, AudioDecoderCallbackImpl_NormalTest_003, TestSize.Level1)
292 {
293 std::shared_ptr<AudioCodecDecoderAdapterImpl> decoder = std::make_shared<AudioCodecDecoderAdapterImpl>();
294 EXPECT_NE(decoder, nullptr);
295 EXPECT_EQ(decoder->CreateAudioDecoderByName(std::string(OH_AVCODEC_NAME_AUDIO_MPEG)),
296 AudioDecoderAdapterCode::DECODER_OK);
297
298 std::shared_ptr<AudioDecoderCallbackAdapter> callback = std::make_shared<AudioDecoderCallbackAdapterMock>();
299 EXPECT_EQ(decoder->SetCallbackDec(callback), AudioDecoderAdapterCode::DECODER_OK);
300
301 constexpr int32_t MEMSIZE = 1024 * 1024;
302 OH_AVBuffer* buffer = OH_AVBuffer_Create(MEMSIZE);
303 AudioDecoderCallbackManager::FindAudioDecoder(nullptr);
304 AudioDecoderCallbackManager::AddAudioDecoder(nullptr);
305 AudioDecoderCallbackManager::decoders_.clear();
306 AudioDecoderCallbackManager::AddAudioDecoder(decoder.get());
307 AudioDecoderCallbackManager::OnError(decoder->GetAVCodec(), 0, nullptr);
308 AudioDecoderCallbackManager::OnOutputFormatChanged(decoder->GetAVCodec(), 0, nullptr);
309 AudioDecoderCallbackManager::OnInputBufferAvailable(decoder->GetAVCodec(), 0, buffer, nullptr);
310 AudioDecoderCallbackManager::OnOutputBufferAvailable(decoder->GetAVCodec(), 0, buffer, nullptr);
311 AudioDecoderCallbackManager::DeleteAudioDecoder(nullptr);
312 OH_AVBuffer_Destroy(buffer);
313 buffer = nullptr;
314 }
315
316 class AudioCodecDecoderAdapterImplTest : public testing::Test {
317 public:
318 static void SetUpTestCase(void);
319 static void TearDownTestCase(void);
320 void SetUp();
321 void TearDown();
322 void SetCencInfoAboutKeyIdIvAlgo(std::shared_ptr<AudioCencInfoAdapter> cencInfo);
323 void SetCencInfoAboutClearHeaderAndPayLoadLens(std::shared_ptr<AudioCencInfoAdapter> cencInfo);
324
325 protected:
326 std::shared_ptr<AudioDecoderFormatAdapterMock> format_ = nullptr;
327 std::shared_ptr<AudioCodecDecoderAdapterImpl> AudioCodecDecoderAdapterImpl_ = nullptr;
328 };
329
SetUpTestCase(void)330 void AudioCodecDecoderAdapterImplTest::SetUpTestCase(void) {}
331
TearDownTestCase(void)332 void AudioCodecDecoderAdapterImplTest::TearDownTestCase(void) {}
333
SetUp()334 void AudioCodecDecoderAdapterImplTest::SetUp()
335 {
336 EXPECT_EQ(AudioCodecDecoderAdapterImpl_, nullptr);
337 AudioCodecDecoderAdapterImpl_ = std::make_shared<AudioCodecDecoderAdapterImpl>();
338 EXPECT_NE(AudioCodecDecoderAdapterImpl_, nullptr);
339 format_ = std::make_unique<AudioDecoderFormatAdapterMock>();
340 EXPECT_NE(format_, nullptr);
341 }
342
TearDown(void)343 void AudioCodecDecoderAdapterImplTest::TearDown(void)
344 {
345 format_ = nullptr;
346 AudioCodecDecoderAdapterImpl_ = nullptr;
347 }
348
SetCencInfoAboutKeyIdIvAlgo(std::shared_ptr<AudioCencInfoAdapter> cencInfo)349 void AudioCodecDecoderAdapterImplTest::SetCencInfoAboutKeyIdIvAlgo(
350 std::shared_ptr<AudioCencInfoAdapter> cencInfo)
351 {
352 uint8_t keyId[] = {
353 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
354 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
355 uint8_t iv[] = {
356 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
357 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
358 cencInfo->SetAlgo(0);
359 cencInfo->SetKeyId(keyId);
360 cencInfo->SetKeyIdLen(DRM_KEY_ID_SIZE);
361 cencInfo->SetIv(iv);
362 cencInfo->SetIvLen(DRM_KEY_ID_SIZE);
363 }
364
SetCencInfoAboutClearHeaderAndPayLoadLens(std::shared_ptr<AudioCencInfoAdapter> cencInfo)365 void AudioCodecDecoderAdapterImplTest::SetCencInfoAboutClearHeaderAndPayLoadLens(
366 std::shared_ptr<AudioCencInfoAdapter> cencInfo)
367 {
368 const std::vector<uint32_t> clearHeaderLens = {1, 2, 3};
369 const std::vector<uint32_t> payLoadLens = {4, 5, 6};
370 const uint32_t ERR_BLOCK_COUNT = 10000;
371 cencInfo->SetClearHeaderLens(clearHeaderLens);
372 cencInfo->SetPayLoadLens(payLoadLens);
373 cencInfo->SetEncryptedBlockCount(ERR_BLOCK_COUNT);
374 cencInfo->SetSkippedBlockCount(0);
375 cencInfo->SetFirstEncryptedOffset(0);
376 }
377
378 /**
379 * @tc.name: AudioCodecDecoderAdapterImpl_CreateAudioDecoderByName_001.
380 * @tc.desc: test of AudioCodecDecoderAdapterImpl::CreateAudioDecoderByName() CreateAudioDecoderByMime()
381 * @tc.type: FUNC.
382 * @tc.require:
383 */
384 HWTEST_F(AudioCodecDecoderAdapterImplTest, AudioCodecDecoderAdapterImpl_CreateAudioDecoderByName_001, TestSize.Level1)
385 {
386 EXPECT_NE(AudioCodecDecoderAdapterImpl_, nullptr);
387
388 // create decoder by invalid name or mimetype.
389 std::string name = "testname";
390 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByName(name), AudioDecoderAdapterCode::DECODER_ERROR);
391
392 std::string mimetype = "testmimeType";
393 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByMime(mimetype),
394 AudioDecoderAdapterCode::DECODER_ERROR);
395
396 // create decoder by normal name.
397 name = std::string(OH_AVCODEC_NAME_AUDIO_MPEG);
398 AudioDecoderAdapterCode ret = AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByName(name);
399 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
400
401 // repeat create
402 ret = AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByName(name);
403 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
404
405 // release decoder.
406 ret = AudioCodecDecoderAdapterImpl_->ReleaseDecoder();
407 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
408
409 // create decoder by normal mimetype.
410 mimetype = std::string(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
411 ret = AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByMime(mimetype);
412 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
413
414 // repeat create
415 ret = AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByMime(mimetype);
416 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
417
418 // release decoder.
419 ret = AudioCodecDecoderAdapterImpl_->ReleaseDecoder();
420 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
421
422 // repeat release decoder.
423 ret = AudioCodecDecoderAdapterImpl_->ReleaseDecoder();
424 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
425 }
426
427 /**
428 * @tc.name: AudioCodecDecoderAdapterImpl_InvalidValueTest_002.
429 * @tc.desc: test of InvalidValueScene in AudioCodecDecoderAdapterImpl
430 * @tc.type: FUNC.
431 * @tc.require:
432 */
433 HWTEST_F(AudioCodecDecoderAdapterImplTest, AudioCodecDecoderAdapterImpl_InvalidValueTest_002, TestSize.Level1)
434 {
435 // not create decoder
436 format_->SetSampleRate(0);
437 format_->SetChannelCount(0);
438 format_->SetBitRate(0);
439 format_->SetMaxInputSize(0);
440 format_->SetAudioSampleFormat(0);
441 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ConfigureDecoder(format_), AudioDecoderAdapterCode::DECODER_ERROR);
442 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ConfigureDecoder(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
443 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetParameterDecoder(format_), AudioDecoderAdapterCode::DECODER_ERROR);
444 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetParameterDecoder(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
445
446 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetCallbackDec(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
447 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->GetAudioDecoderCallBack(), nullptr);
448
449 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, nullptr, false,
450 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
451 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->GetOutputFormatDec(format_), AudioDecoderAdapterCode::DECODER_ERROR);
452 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(0), AudioDecoderAdapterCode::DECODER_ERROR);
453 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->PrepareDecoder(), AudioDecoderAdapterCode::DECODER_ERROR);
454 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->StartDecoder(), AudioDecoderAdapterCode::DECODER_ERROR);
455 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->StopDecoder(), AudioDecoderAdapterCode::DECODER_ERROR);
456 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->FlushDecoder(), AudioDecoderAdapterCode::DECODER_ERROR);
457 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ResetDecoder(), AudioDecoderAdapterCode::DECODER_ERROR);
458 AudioCodecDecoderAdapterImpl_->ResetDecoder();
459 AudioCodecDecoderAdapterImpl_->FlushDecoder();
460 AudioCodecDecoderAdapterImpl_->StopDecoder();
461 AudioCodecDecoderAdapterImpl_->StartDecoder();
462 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetDecryptionConfig(nullptr, false),
463 AudioDecoderAdapterCode::DECODER_ERROR);
464
465 int32_t tmp = 100;
466 void *session = reinterpret_cast<void*>(&tmp);
467 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetDecryptionConfig(session, false),
468 AudioDecoderAdapterCode::DECODER_ERROR);
469 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->GetBufferFlag(static_cast<OHOS::MediaAVCodec::AVCodecBufferFlag>(tmp)),
470 BufferFlag::CODEC_BUFFER_FLAG_NONE);
471 }
472
473 /**
474 * @tc.name: AudioCodecDecoderAdapterImpl_QueueInputBufferDec_003.
475 * @tc.desc: test of InvalidValueScene in AudioCodecDecoderAdapterImpl
476 * @tc.type: FUNC.
477 * @tc.require:
478 */
479 HWTEST_F(AudioCodecDecoderAdapterImplTest, AudioCodecDecoderAdapterImpl_QueueInputBufferDec_003, TestSize.Level1)
480 {
481 std::string mimetype = std::string(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
482 AudioDecoderAdapterCode ret = AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByMime(mimetype);
483 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
484 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, nullptr, true,
485 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
486 // test QueueInputBufferDec with decrypt data
487 std::shared_ptr<AudioCencInfoAdapterImpl> cencInfo = std::make_shared<AudioCencInfoAdapterImpl>();
488 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
489 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
490 const uint32_t ERR_ALGO = 10000;
491 cencInfo->SetAlgo(ERR_ALGO);
492 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
493 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
494 SetCencInfoAboutKeyIdIvAlgo(cencInfo);
495 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
496 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
497 SetCencInfoAboutClearHeaderAndPayLoadLens(cencInfo);
498 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
499 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
500 cencInfo->SetEncryptedBlockCount(0);
501 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
502 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
503 const uint32_t ERR_MODE = 10000;
504 cencInfo->SetMode(ERR_MODE);
505 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
506 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
507 cencInfo->SetMode(0);
508 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
509 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
510 // test QueueInputBufferDec with input buffer and output buffer
511 constexpr int32_t MEMSIZE = 1024 * 1024;
512 OH_AVBuffer* buffer = OH_AVBuffer_Create(MEMSIZE);
513 AudioCodecDecoderAdapterImpl_->SetInputBuffer(0, buffer);
514 AudioCodecDecoderAdapterImpl_->SetOutputBuffer(0, buffer);
515 AudioCodecDecoderAdapterImpl_->SetInputBuffer(0, nullptr);
516 AudioCodecDecoderAdapterImpl_->SetOutputBuffer(0, nullptr);
517 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(1, 0, nullptr, 0, cencInfo, true,
518 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
519 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
520 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
521 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, cencInfo, true,
522 BufferFlag::CODEC_BUFFER_FLAG_EOS), AudioDecoderAdapterCode::DECODER_ERROR);
523 AudioCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(0);
524 AudioCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(100);
525 OH_AVBuffer_Destroy(buffer);
526 buffer = nullptr;
527 }
528
529 /**
530 * @tc.name: AudioCodecDecoderAdapterImpl_NormalValueTest_004.
531 * @tc.desc: test of NormalScene in AudioCodecDecoderAdapterImpl
532 * @tc.type: FUNC.
533 * @tc.require:
534 */
535 HWTEST_F(AudioCodecDecoderAdapterImplTest, AudioCodecDecoderAdapterImpl_NormalValueTest_004, TestSize.Level1)
536 {
537 // create decoder by normal mimetype.
538 std::string mimetype = std::string(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
539 AudioDecoderAdapterCode ret = AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByMime(mimetype);
540 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
541
542 // config decoder.
543 constexpr uint32_t DEFAULT_SAMPLERATE = 44100;
544 constexpr uint32_t DEFAULT_BITRATE = 32000;
545 constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2;
546 constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = 1152;
547 format_->SetSampleRate(DEFAULT_SAMPLERATE);
548 format_->SetChannelCount(DEFAULT_CHANNEL_COUNT);
549 format_->SetBitRate(DEFAULT_BITRATE);
550 format_->SetMaxInputSize(DEFAULT_MAX_INPUT_SIZE);
551 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ConfigureDecoder(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
552 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ConfigureDecoder(format_), AudioDecoderAdapterCode::DECODER_OK);
553 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->GetOutputFormatDec(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
554 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->GetOutputFormatDec(format_), AudioDecoderAdapterCode::DECODER_OK);
555
556 // set callback for decoding.
557 std::shared_ptr<AudioDecoderCallbackAdapter> callback = std::make_shared<AudioDecoderCallbackAdapterMock>();
558 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetCallbackDec(callback), AudioDecoderAdapterCode::DECODER_OK);
559
560 // prepare decoder.
561 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->PrepareDecoder(), AudioDecoderAdapterCode::DECODER_OK);
562 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->StartDecoder(), AudioDecoderAdapterCode::DECODER_OK);
563 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetParameterDecoder(nullptr), AudioDecoderAdapterCode::DECODER_ERROR);
564 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetParameterDecoder(format_), AudioDecoderAdapterCode::DECODER_OK);
565 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->FlushDecoder(), AudioDecoderAdapterCode::DECODER_OK);
566 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->StopDecoder(), AudioDecoderAdapterCode::DECODER_OK);
567 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ResetDecoder(), AudioDecoderAdapterCode::DECODER_OK);
568 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->SetCallbackDec(callback), AudioDecoderAdapterCode::DECODER_OK);
569
570 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->GetBufferFlag(
571 OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS), BufferFlag::CODEC_BUFFER_FLAG_EOS);
572 AudioCodecDecoderAdapterImpl_->ResetDecoder();
573 AudioCodecDecoderAdapterImpl_->FlushDecoder();
574 AudioCodecDecoderAdapterImpl_->StopDecoder();
575 AudioCodecDecoderAdapterImpl_->StartDecoder();
576 (void)AudioCodecDecoderAdapterImpl_->GetDecoderMutex();
577 }
578
579 /**
580 * @tc.name: AudioCodecDecoderAdapterImpl_NormalValueTest_005.
581 * @tc.desc: test of NormalScene in AudioCodecDecoderAdapterImpl
582 * @tc.type: FUNC.
583 * @tc.require:
584 */
585 HWTEST_F(AudioCodecDecoderAdapterImplTest, AudioCodecDecoderAdapterImpl_NormalValueTest_005, TestSize.Level1)
586 {
587 constexpr int32_t DEFAULT_SAMPLERATE = 44100;
588 constexpr int64_t DEFAULT_BITRATE = 32000;
589 constexpr int32_t DEFAULT_CHANNEL_COUNT = 2;
590 constexpr int32_t DEFAULT_MAX_INPUT_SIZE = 1152;
591 constexpr bool DEFAUL_AAC_IS_ADTS = false;
592 constexpr int32_t DEFAULT_AUDIO_SAMPLE_FORMAT = 100;
593 constexpr int32_t DEFAUL_ID_HEARDER = 100;
594 constexpr int32_t DEFAULT_SETUP_HEADER = 100;
595 uint8_t codecConfig[10] = {0};
596 constexpr uint32_t DEFAULT_CODEC_Config_SIZE = 10;
597 std::shared_ptr<AudioDecoderFormatAdapterImpl> format = std::make_shared<AudioDecoderFormatAdapterImpl>();
598 format->SetSampleRate(DEFAULT_SAMPLERATE);
599 format->SetChannelCount(DEFAULT_CHANNEL_COUNT);
600 format->SetBitRate(DEFAULT_BITRATE);
601 format->SetMaxInputSize(DEFAULT_MAX_INPUT_SIZE);
602 format->SetAACIsAdts(DEFAUL_AAC_IS_ADTS);
603 format->SetAudioSampleFormat(DEFAULT_AUDIO_SAMPLE_FORMAT);
604 format->SetIdentificationHeader(DEFAUL_ID_HEARDER);
605 format->SetSetupHeader(DEFAULT_SETUP_HEADER);
606 format->SetCodecConfig(codecConfig);
607 format->SetCodecConfigSize(DEFAULT_CODEC_Config_SIZE);
608 format->PrintFormatData(format);
609 EXPECT_EQ(format->GetSampleRate(), DEFAULT_SAMPLERATE);
610 EXPECT_EQ(format->GetChannelCount(), DEFAULT_CHANNEL_COUNT);
611 EXPECT_EQ(format->GetBitRate(), DEFAULT_BITRATE);
612 EXPECT_EQ(format->GetMaxInputSize(), DEFAULT_MAX_INPUT_SIZE);
613 EXPECT_EQ(format->GetAACIsAdts(), DEFAUL_AAC_IS_ADTS);
614 EXPECT_EQ(format->GetAudioSampleFormat(), DEFAULT_AUDIO_SAMPLE_FORMAT);
615 EXPECT_EQ(format->GetIdentificationHeader(), DEFAUL_ID_HEARDER);
616 EXPECT_EQ(format->GetSetupHeader(), DEFAULT_SETUP_HEADER);
617 EXPECT_EQ(format->GetCodecConfig(), codecConfig);
618 EXPECT_EQ(format->GetCodecConfigSize(), DEFAULT_CODEC_Config_SIZE);
619 }
620
621 /**
622 * @tc.name: AudioCodecDecoderAdapterImpl_NormalValueTest_006.
623 * @tc.desc: test of NormalScene in AudioCodecDecoderAdapterImpl
624 * @tc.type: FUNC.
625 * @tc.require:
626 */
627 HWTEST_F(AudioCodecDecoderAdapterImplTest, AudioCodecDecoderAdapterImpl_NormalValueTest_006, TestSize.Level1)
628 {
629 // create decoder by normal mimetype.
630 std::string mimetype = std::string(OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
631 AudioDecoderAdapterCode ret = AudioCodecDecoderAdapterImpl_->CreateAudioDecoderByMime(mimetype);
632 EXPECT_EQ(ret, AudioDecoderAdapterCode::DECODER_OK);
633 EXPECT_NE(AudioCodecDecoderAdapterImpl_->GetAVCodec(), nullptr);
634
635 AudioCodecDecoderAdapterImpl_->inputBuffers_.clear();
636 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->GetOutputBuffer(1000), nullptr);
637 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(0), AudioDecoderAdapterCode::DECODER_ERROR);
638
639 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(0, 0, nullptr, 0, nullptr, false,
640 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
641 std::shared_ptr<AudioCencInfoAdapterImpl> cencInfo = std::make_shared<AudioCencInfoAdapterImpl>();
642 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->StartDecoder(), AudioDecoderAdapterCode::DECODER_ERROR);
643 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_BUFFER_TIMEOUT));
644 if (!AudioCodecDecoderAdapterImpl_->inputBuffers_.empty()) {
645 int32_t index = AudioCodecDecoderAdapterImpl_->inputBuffers_.begin()->first;
646 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(index, 0, nullptr, 0, cencInfo, true,
647 BufferFlag::CODEC_BUFFER_FLAG_NONE), AudioDecoderAdapterCode::DECODER_ERROR);
648 EXPECT_EQ(AudioCodecDecoderAdapterImpl_->QueueInputBufferDec(index, 0, nullptr, 0, cencInfo, true,
649 BufferFlag::CODEC_BUFFER_FLAG_EOS), AudioDecoderAdapterCode::DECODER_ERROR);
650 AudioCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(index);
651 }
652 }
653 }
654 } // namespace OHOS::NWeb