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