1 /*
2 * Copyright (C) 2022 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 #include <string>
17 #include "gtest/gtest.h"
18 #include "AudioDecoderDemoCommon.h"
19 #include "avcodec_info.h"
20 #include "avcodec_errors.h"
21 #include "media_description.h"
22 #include "av_common.h"
23 #include "format.h"
24
25 using namespace std;
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::MediaAVCodec;
29
30 namespace {
31 class InnerParamCheckTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37 };
38
SetUpTestCase()39 void InnerParamCheckTest::SetUpTestCase() {}
TearDownTestCase()40 void InnerParamCheckTest::TearDownTestCase() {}
SetUp()41 void InnerParamCheckTest::SetUp() {}
TearDown()42 void InnerParamCheckTest::TearDown() {}
43 } // namespace
44
45 /**
46 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_001
47 * @tc.name : InnerCreateByMime
48 * @tc.desc : param check test
49 */
50 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_001, TestSize.Level2)
51 {
52 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
53
54 int32_t ret = decoderDemo->InnerCreateByMime("audio/mpeg");
55 ASSERT_EQ(AVCS_ERR_OK, ret);
56 decoderDemo->InnerDestroy();
57
58 ret = decoderDemo->InnerCreateByMime("audio/mp4a-latm");
59 ASSERT_EQ(AVCS_ERR_OK, ret);
60 decoderDemo->InnerDestroy();
61
62 ret = decoderDemo->InnerCreateByMime("audio/vorbis");
63 ASSERT_EQ(AVCS_ERR_OK, ret);
64 decoderDemo->InnerDestroy();
65
66 ret = decoderDemo->InnerCreateByMime("audio/flac");
67 ASSERT_EQ(AVCS_ERR_OK, ret);
68 decoderDemo->InnerDestroy();
69
70 ret = decoderDemo->InnerCreateByMime("aaa");
71 ASSERT_EQ(AVCS_ERR_INVALID_OPERATION, ret);
72
73 decoderDemo->InnerDestroy();
74 delete decoderDemo;
75 }
76
77 /**
78 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_002
79 * @tc.name : InnerCreateByName
80 * @tc.desc : param check test
81 */
82 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_002, TestSize.Level2)
83 {
84 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
85
86 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
87 ASSERT_EQ(AVCS_ERR_OK, ret);
88 decoderDemo->InnerDestroy();
89
90 ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.AAC");
91 ASSERT_EQ(AVCS_ERR_OK, ret);
92 decoderDemo->InnerDestroy();
93
94 ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Flac");
95 ASSERT_EQ(AVCS_ERR_OK, ret);
96 decoderDemo->InnerDestroy();
97
98 ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Vorbis");
99 ASSERT_EQ(AVCS_ERR_OK, ret);
100 decoderDemo->InnerDestroy();
101
102 ret = decoderDemo->InnerCreateByName("aaa");
103 ASSERT_EQ(AVCS_ERR_INVALID_OPERATION, ret);
104
105 decoderDemo->InnerDestroy();
106 delete decoderDemo;
107 }
108
109 /**
110 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_003
111 * @tc.name : InnerConfigure - MD_KEY_BITRATE
112 * @tc.desc : param check test
113 */
114 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_003, TestSize.Level2)
115 {
116 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
117
118 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
119 ASSERT_EQ(AVCS_ERR_OK, ret);
120 Format audioParams;
121
122 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
123 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
124 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
125
126 ret = decoderDemo->InnerConfigure(audioParams);
127 ASSERT_EQ(AVCS_ERR_OK, ret);
128
129 decoderDemo->InnerReset();
130 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, -1);
131 ret = decoderDemo->InnerConfigure(audioParams);
132 ASSERT_EQ(AVCS_ERR_OK, ret);
133
134 decoderDemo->InnerReset();
135 audioParams.PutStringValue(MediaDescriptionKey::MD_KEY_BITRATE, "aaaaaa");
136 ret = decoderDemo->InnerConfigure(audioParams);
137 ASSERT_EQ(AVCS_ERR_OK, ret);
138
139 decoderDemo->InnerReset();
140 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_BITRATE, 0);
141 ret = decoderDemo->InnerConfigure(audioParams);
142 ASSERT_EQ(AVCS_ERR_OK, ret);
143
144 decoderDemo->InnerReset();
145 audioParams.PutFloatValue(MediaDescriptionKey::MD_KEY_BITRATE, 0.1);
146 ret = decoderDemo->InnerConfigure(audioParams);
147 ASSERT_EQ(AVCS_ERR_OK, ret);
148
149 decoderDemo->InnerReset();
150 audioParams.PutDoubleValue(MediaDescriptionKey::MD_KEY_BITRATE, 0.1);
151 ret = decoderDemo->InnerConfigure(audioParams);
152 ASSERT_EQ(AVCS_ERR_OK, ret);
153
154 uint8_t b[100];
155 decoderDemo->InnerReset();
156 audioParams.PutBuffer(MediaDescriptionKey::MD_KEY_BITRATE, b, 100);
157 ret = decoderDemo->InnerConfigure(audioParams);
158 ASSERT_EQ(AVCS_ERR_OK, ret);
159
160 decoderDemo->InnerDestroy();
161 delete decoderDemo;
162 }
163
164 /**
165 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_004
166 * @tc.name : InnerConfigure - MD_KEY_CHANNEL_COUNT check
167 * @tc.desc : param check test
168 */
169 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_004, TestSize.Level2)
170 {
171 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
172 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
173 ASSERT_EQ(AVCS_ERR_OK, ret);
174 Format audioParams;
175
176 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
177 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
178 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
179
180 ret = decoderDemo->InnerConfigure(audioParams);
181 ASSERT_EQ(AVCS_ERR_OK, ret);
182
183 decoderDemo->InnerReset();
184 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, -1);
185 ret = decoderDemo->InnerConfigure(audioParams);
186 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
187
188 decoderDemo->InnerReset();
189 audioParams.PutStringValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, "aaaaaa");
190 ret = decoderDemo->InnerConfigure(audioParams);
191 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
192
193 decoderDemo->InnerReset();
194 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 0);
195 ret = decoderDemo->InnerConfigure(audioParams);
196 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
197
198 decoderDemo->InnerReset();
199 audioParams.PutFloatValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 0.1);
200 ret = decoderDemo->InnerConfigure(audioParams);
201 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
202
203 decoderDemo->InnerReset();
204 audioParams.PutDoubleValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 0.1);
205 ret = decoderDemo->InnerConfigure(audioParams);
206 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
207
208 uint8_t b[100];
209 decoderDemo->InnerReset();
210 audioParams.PutBuffer(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, b, 100);
211 ret = decoderDemo->InnerConfigure(audioParams);
212 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
213
214 decoderDemo->InnerDestroy();
215 delete decoderDemo;
216 }
217
218 /**
219 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_005
220 * @tc.name : InnerConfigure - MD_KEY_SAMPLE_RATE check
221 * @tc.desc : param check test
222 */
223 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_005, TestSize.Level2)
224 {
225 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
226 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
227 ASSERT_EQ(AVCS_ERR_OK, ret);
228 Format audioParams;
229
230 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
231 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
232 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
233
234 ret = decoderDemo->InnerConfigure(audioParams);
235 ASSERT_EQ(AVCS_ERR_OK, ret);
236
237 decoderDemo->InnerReset();
238 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, -1);
239 ret = decoderDemo->InnerConfigure(audioParams);
240 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
241
242 decoderDemo->InnerReset();
243 audioParams.PutStringValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, "aaaaaa");
244 ret = decoderDemo->InnerConfigure(audioParams);
245 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
246
247 decoderDemo->InnerReset();
248 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 0);
249 ret = decoderDemo->InnerConfigure(audioParams);
250 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
251
252 decoderDemo->InnerReset();
253 audioParams.PutFloatValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 0.1);
254 ret = decoderDemo->InnerConfigure(audioParams);
255 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
256
257 decoderDemo->InnerReset();
258 audioParams.PutDoubleValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 0.1);
259 ret = decoderDemo->InnerConfigure(audioParams);
260 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
261
262 uint8_t b[100];
263 decoderDemo->InnerReset();
264 audioParams.PutBuffer(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, b, 100);
265 ret = decoderDemo->InnerConfigure(audioParams);
266 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
267
268 decoderDemo->InnerDestroy();
269 delete decoderDemo;
270 }
271
272 /**
273 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_006
274 * @tc.name : InnerQueueInputBuffer - index check
275 * @tc.desc : param check test
276 */
277 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_006, TestSize.Level2)
278 {
279 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
280 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
281 ASSERT_EQ(AVCS_ERR_OK, ret);
282 Format audioParams;
283
284 std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
285 std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
286 decoderDemo->InnerSetCallback(cb_);
287
288 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
289 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
290 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
291
292 ret = decoderDemo->InnerConfigure(audioParams);
293 ASSERT_EQ(AVCS_ERR_OK, ret);
294
295 decoderDemo->InnerPrepare();
296 decoderDemo->InnerStart();
297
298 uint32_t index = decoderDemo->NativeGetInputIndex();
299 AVCodecBufferInfo info;
300 AVCodecBufferFlag flag;
301
302 info.presentationTimeUs = 0;
303 info.size = 100;
304 info.offset = 0;
305 flag = AVCODEC_BUFFER_FLAG_NONE;
306
307 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
308 ASSERT_EQ(AVCS_ERR_OK, ret);
309
310 index = -1;
311 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
312 ASSERT_EQ(AVCS_ERR_NO_MEMORY, ret);
313
314 decoderDemo->InnerDestroy();
315 delete decoderDemo;
316 }
317
318 /**
319 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_007
320 * @tc.name : InnerQueueInputBuffer - info.presentationTimeUs check
321 * @tc.desc : param check test
322 */
323 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_007, TestSize.Level2)
324 {
325 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
326 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
327 ASSERT_EQ(AVCS_ERR_OK, ret);
328 Format audioParams;
329
330 std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
331 std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
332 decoderDemo->InnerSetCallback(cb_);
333
334 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
335 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
336 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
337 ret = decoderDemo->InnerConfigure(audioParams);
338 ASSERT_EQ(AVCS_ERR_OK, ret);
339
340 decoderDemo->InnerPrepare();
341 decoderDemo->InnerStart();
342
343 uint32_t index = decoderDemo->NativeGetInputIndex();
344 std::shared_ptr<AVSharedMemory> buffer = signal_->inInnerBufQueue_.front();
345 ASSERT_NE(nullptr, buffer);
346
347 AVCodecBufferInfo info;
348 AVCodecBufferFlag flag;
349
350 info.presentationTimeUs = 0;
351 info.size = 100;
352 info.offset = 0;
353 flag = AVCODEC_BUFFER_FLAG_NONE;
354
355 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
356 ASSERT_EQ(AVCS_ERR_OK, ret);
357
358 index = decoderDemo->NativeGetInputIndex();
359 info.presentationTimeUs = -1;
360 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
361 ASSERT_EQ(AVCS_ERR_OK, ret);
362
363 decoderDemo->InnerDestroy();
364 delete decoderDemo;
365 }
366
367 /**
368 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_008
369 * @tc.name : InnerQueueInputBuffer - info.size check
370 * @tc.desc : param check test
371 */
372 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_008, TestSize.Level2)
373 {
374 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
375 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
376 ASSERT_EQ(AVCS_ERR_OK, ret);
377 Format audioParams;
378
379 std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
380 std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
381 decoderDemo->InnerSetCallback(cb_);
382
383 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
384 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
385 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
386 ret = decoderDemo->InnerConfigure(audioParams);
387 ASSERT_EQ(AVCS_ERR_OK, ret);
388
389 decoderDemo->InnerPrepare();
390 decoderDemo->InnerStart();
391
392 uint32_t index = decoderDemo->NativeGetInputIndex();
393
394 AVCodecBufferInfo info;
395 AVCodecBufferFlag flag;
396
397 info.presentationTimeUs = 0;
398 info.size = 100;
399 info.offset = 0;
400 flag = AVCODEC_BUFFER_FLAG_NONE;
401
402 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
403 ASSERT_EQ(AVCS_ERR_OK, ret);
404
405 index = decoderDemo->NativeGetInputIndex();
406 info.size = -1;
407 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
408 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
409
410 decoderDemo->InnerDestroy();
411
412 delete decoderDemo;
413 }
414
415 /**
416 * @tc.number : SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_009
417 * @tc.name : InnerQueueInputBuffer - offset
418 * @tc.desc : param check test
419 */
420 HWTEST_F(InnerParamCheckTest, SUB_MULTIMEDIA_AUDIO_DECODER_PARAM_CHECK_009, TestSize.Level2)
421 {
422 AudioDecoderDemo *decoderDemo = new AudioDecoderDemo();
423 int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
424 ASSERT_EQ(AVCS_ERR_OK, ret);
425 Format audioParams;
426
427 std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
428 std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
429 decoderDemo->InnerSetCallback(cb_);
430
431 audioParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
432 audioParams.PutIntValue("bits_per_coded_sample", 4);
433
434 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
435 audioParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
436 ret = decoderDemo->InnerConfigure(audioParams);
437 ASSERT_EQ(AVCS_ERR_OK, ret);
438
439 decoderDemo->InnerPrepare();
440 decoderDemo->InnerStart();
441
442 uint32_t index = decoderDemo->NativeGetInputIndex();
443
444 AVCodecBufferInfo info;
445 AVCodecBufferFlag flag;
446
447 info.presentationTimeUs = 0;
448 info.size = 100;
449 info.offset = 0;
450 flag = AVCODEC_BUFFER_FLAG_NONE;
451 index = 0;
452
453 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
454 ASSERT_EQ(AVCS_ERR_OK, ret);
455
456 index = decoderDemo->NativeGetInputIndex();
457 info.offset = -1;
458 ret = decoderDemo->InnerQueueInputBuffer(index, info, flag);
459 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
460
461 decoderDemo->InnerDestroy();
462 delete decoderDemo;
463 }
464