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 "AudioEncoderDemoCommon.h"
19
20using namespace std;
21using namespace testing::ext;
22using namespace OHOS;
23using namespace OHOS::MediaAVCodec;
24
25
26namespace {
27    class NativeFunctionTest : public testing::Test {
28    public:
29        static void SetUpTestCase();
30        static void TearDownTestCase();
31        void SetUp() override;
32        void TearDown() override;
33    };
34
35    void NativeFunctionTest::SetUpTestCase() {}
36    void NativeFunctionTest::TearDownTestCase() {}
37    void NativeFunctionTest::SetUp() {}
38    void NativeFunctionTest::TearDown() {}
39
40    constexpr uint32_t DEFAULT_AAC_TYPE = 1;
41    constexpr int32_t CHANNEL_COUNT_AAC = 2;
42    constexpr int32_t SAMPLE_RATE_AAC = 16000;
43    constexpr int64_t BITS_RATE_AAC = 129000;
44    constexpr int32_t CHANNEL_COUNT_FLAC = 2;
45    constexpr int32_t SAMPLE_RATE_FLAC = 48000;
46    constexpr int64_t BITS_RATE_FLAC = 128000;
47    constexpr int32_t COMPLIANCE_LEVEL = -2;
48
49    constexpr int32_t CHANNEL_COUNT_MONO = 1;
50    constexpr int32_t CHANNEL_COUNT_STEREO = 2;
51    constexpr int32_t CHANNEL_COUNT_7POINT1 = 8;
52
53    int32_t testResult[16] = { -1 };
54
55    vector<string> SplitStringFully(const string& str, const string& separator)
56    {
57        vector<string> dest;
58        string substring;
59        string::size_type start = 0;
60        string::size_type index = str.find_first_of(separator, start);
61
62        while (index != string::npos) {
63            substring = str.substr(start, index - start);
64            dest.push_back(substring);
65            start = str.find_first_not_of(separator, index);
66            if (start == string::npos) {
67                return dest;
68            }
69
70            index = str.find_first_of(separator, start);
71        }
72        substring = str.substr(start);
73        dest.push_back(substring);
74
75        return dest;
76    }
77
78    bool compareFile(string file1, string file2)
79    {
80        string ans1, ans2;
81        int i;
82        (void)freopen(file1.c_str(), "r", stdin);
83        char c;
84        while (scanf_s("%c", &c, 1) != EOF) {
85            ans1 += c;
86        }
87        (void)fclose(stdin);
88
89        (void)freopen(file2.c_str(), "r", stdin);
90        while (scanf_s("%c", &c, 1) != EOF) {
91            ans2 += c;
92        }
93        (void)fclose(stdin);
94        if (ans1.size() != ans2.size()) {
95            return false;
96        }
97        for (i = 0; i < ans1.size(); i++) {
98            if (ans1[i] != ans2[i]) {
99                return false;
100            }
101        }
102        return true;
103    }
104
105    OH_AVFormat* getAVFormatByEncoder(string encoderName)
106    {
107        OH_AVFormat* format = OH_AVFormat_Create();
108        if (encoderName == "OH.Media.Codec.Encoder.Audio.AAC") {
109            OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT_AAC);
110            OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE_AAC);
111            OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_F32LE);
112            OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_F32LE);
113            OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
114            OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
115            OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE_AAC);
116        } else {
117            OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT_FLAC);
118            OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE_FLAC);
119            OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_S16LE);
120            OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_S16LE);
121            OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
122            OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE_FLAC);
123        }
124        return format;
125    }
126
127    void runEncode(string encoderName, string inputFile, string outputFile, int32_t threadId)
128    {
129        AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
130
131        OH_AVFormat* format = getAVFormatByEncoder(encoderName);
132
133        encoderDemo->NativeRunCase(inputFile, outputFile, encoderName.c_str(), format);
134
135        OH_AVFormat_Destroy(format);
136        delete encoderDemo;
137
138        testResult[threadId] = AV_ERR_OK;
139    }
140
141    OH_AVFormat* getAVFormatAAC(int32_t channelCount, int32_t sampleRate, int64_t bitrate)
142    {
143        OH_AVFormat* format = OH_AVFormat_Create();
144        OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, channelCount);
145        OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, sampleRate);
146        OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_F32LE);
147        OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_F32LE);
148
149        switch (channelCount) {
150            case CHANNEL_COUNT_MONO:
151                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, MONO);
152                break;
153            case CHANNEL_COUNT_STEREO:
154                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
155                break;
156            case CHANNEL_COUNT_7POINT1:
157                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CH_7POINT1);
158                break;
159            default:
160                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, UNKNOWN_CHANNEL_LAYOUT);
161                break;
162        }
163
164        OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
165        OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, bitrate);
166
167        return format;
168    }
169
170
171    OH_AVFormat* getAVFormatFlac(int32_t channelCount, int32_t sampleRate, int32_t sampleFormat, int64_t bitrate)
172    {
173        OH_AVFormat* format = OH_AVFormat_Create();
174        OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, channelCount);
175        OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, sampleRate);
176        OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, sampleFormat);
177        OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, sampleFormat);
178
179        int32_t bitsPerSample;
180        if (sampleFormat == OH_BitsPerSample::SAMPLE_S16LE) {
181            bitsPerSample = S16_BITS_PER_SAMPLE;
182        } else {
183            bitsPerSample = S32_BITS_PER_SAMPLE;
184        }
185
186        int32_t inputBufSize;
187        switch (channelCount) {
188            case CHANNEL_COUNT_MONO:
189                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, MONO);
190                inputBufSize = COMMON_FLAC_NUM * channelCount * bitsPerSample;
191                break;
192            case CHANNEL_COUNT_STEREO:
193                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
194                inputBufSize = COMMON_FLAC_NUM * channelCount * bitsPerSample;
195                break;
196            case CHANNEL_COUNT_7POINT1:
197                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CH_7POINT1);
198                inputBufSize = COMMON_FLAC_NUM * channelCount * bitsPerSample;
199                break;
200            default:
201                OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, UNKNOWN_CHANNEL_LAYOUT);
202                inputBufSize = COMMON_FLAC_NUM * UNKNOWN_CHANNEL * bitsPerSample;
203                break;
204        }
205
206        OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, bitrate);
207        OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, COMPLIANCE_LEVEL);
208        OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, inputBufSize);
209
210        return format;
211    }
212}
213
214/**
215 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_001
216 * @tc.name      : aac(different sample rate)
217 * @tc.desc      : function check
218 */
219HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_001, TestSize.Level2)
220{
221    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
222    string encoderName = "OH.Media.Codec.Encoder.Audio.AAC";
223
224    string fileList[] = {
225        "f32le_7350_2_dayuhaitang.pcm",
226        "f32le_16000_2_dayuhaitang.pcm",
227        "f32le_44100_2_dayuhaitang.pcm",
228        "f32le_48000_2_dayuhaitang.pcm",
229        "f32le_96000_2_dayuhaitang.pcm"
230    };
231
232    for (int i = 0; i < 5; i++)
233    {
234        vector<string> dest = SplitStringFully(fileList[i], "_");
235        if (dest.size() < 4) {
236            cout << "split error !!!" << endl;
237            return;
238        }
239        int32_t channelCount = stoi(dest[2]);
240        int32_t sampleRate = stoi(dest[1]);
241
242        cout << "channel count is " << channelCount << ", sample rate is " << sampleRate << endl;
243
244        OH_AVFormat* format = getAVFormatAAC(channelCount, sampleRate, BITS_RATE_AAC);
245        ASSERT_NE(nullptr, format);
246
247        string inputFile = fileList[i];
248        string outputFile = "FUNCTION_001_" + to_string(sampleRate) + ".aac";
249
250        encoderDemo->NativeRunCase(inputFile, outputFile, encoderName.c_str(), format);
251
252        OH_AVFormat_Destroy(format);
253    }
254    delete encoderDemo;
255}
256
257/**
258 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_002
259 * @tc.name      : aac(different channel num)
260 * @tc.desc      : function check
261 */
262HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_002, TestSize.Level2)
263{
264    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
265    string encoderName = "OH.Media.Codec.Encoder.Audio.AAC";
266
267    string fileList[] = {
268        "f32le_7350_1_dayuhaitang.pcm",
269        "f32le_16000_2_dayuhaitang.pcm",
270        "f32le_96000_8_dayuhaitang.pcm"
271    };
272
273    for (int i = 0; i < 3; i++)
274    {
275        vector<string> dest = SplitStringFully(fileList[i], "_");
276        if (dest.size() < 4)
277        {
278            cout << "split error !!!" << endl;
279            return;
280        }
281        int32_t channelCount = stoi(dest[2]);
282        int32_t sampleRate = stoi(dest[1]);
283
284        cout << "channel count is " << channelCount << ", sample rate is " << sampleRate << endl;
285
286        OH_AVFormat* format = getAVFormatAAC(channelCount, sampleRate, BITS_RATE_AAC);
287        ASSERT_NE(nullptr, format);
288
289        string inputFile = fileList[i];
290        string outputFile = "FUNCTION_002_" + to_string(channelCount) + ".aac";
291
292        encoderDemo->NativeRunCase(inputFile, outputFile, encoderName.c_str(), format);
293
294        OH_AVFormat_Destroy(format);
295    }
296    delete encoderDemo;
297}
298
299
300/**
301 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_003
302 * @tc.name      : aac(different bitrate)
303 * @tc.desc      : function check
304 */
305HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_003, TestSize.Level2)
306{
307    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
308    string encoderName = "OH.Media.Codec.Encoder.Audio.AAC";
309
310    long bitrateList[] = {32000L, 96000L, 128000L, 256000L, 300000L, 500000L};
311    string inputFile = "f32le_96000_2_dayuhaitang.pcm";
312
313    for (int i = 0; i < 6; i++)
314    {
315        vector<string> dest = SplitStringFully(inputFile, "_");
316        if (dest.size() < 4)
317        {
318            cout << "split error !!!" << endl;
319            return;
320        }
321        int32_t channelCount = stoi(dest[2]);
322        int32_t sampleRate = stoi(dest[1]);
323
324        cout << "channel count is " << channelCount << ", sample rate is " << sampleRate << endl;
325
326        OH_AVFormat* format = getAVFormatAAC(channelCount, sampleRate, bitrateList[i]);
327        ASSERT_NE(nullptr, format);
328
329        string outputFile = "FUNCTION_003_" + to_string(bitrateList[i]) + ".aac";
330
331        encoderDemo->NativeRunCase(inputFile, outputFile, encoderName.c_str(), format);
332
333        OH_AVFormat_Destroy(format);
334    }
335    delete encoderDemo;
336}
337
338
339/**
340 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_004
341 * @tc.name      : flac(different sample rate)
342 * @tc.desc      : function check
343 */
344HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_004, TestSize.Level2)
345{
346    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
347    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
348
349    string fileList[] = {
350        "s16_8000_2_dayuhaitang.pcm",
351        "s16_16000_2_dayuhaitang.pcm",
352        "s16_44100_2_dayuhaitang.pcm",
353        "s16_48000_2_dayuhaitang.pcm",
354        "s16_96000_2_dayuhaitang.pcm"
355    };
356
357    for (int i = 0; i < 5; i++)
358    {
359        vector<string> dest = SplitStringFully(fileList[i], "_");
360        if (dest.size() < 4)
361        {
362            cout << "split error !!!" << endl;
363            return;
364        }
365        int32_t channelCount = stoi(dest[2]);
366        int32_t sampleRate = stoi(dest[1]);
367
368        OH_AVFormat* format = getAVFormatFlac(channelCount, sampleRate, OH_BitsPerSample::SAMPLE_S16LE, BITS_RATE_FLAC);
369        ASSERT_NE(nullptr, format);
370
371        string inputFile = fileList[i];
372        string outputFile = "FUNCTION_004_" + to_string(sampleRate) + ".flac";
373
374        encoderDemo->NativeRunCase(inputFile, outputFile, encoderName.c_str(), format);
375
376        OH_AVFormat_Destroy(format);
377    }
378    delete encoderDemo;
379}
380
381
382/**
383 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_005
384 * @tc.name      : flac(different sample format)
385 * @tc.desc      : function check
386 */
387HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_005, TestSize.Level2)
388{
389    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
390    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
391
392    string fileList[] = {
393        "s32_8000_2_dayuhaitang.pcm",
394        "s32_16000_2_dayuhaitang.pcm",
395        "s32_44100_2_dayuhaitang.pcm",
396        "s32_48000_2_dayuhaitang.pcm",
397        "s32_96000_2_dayuhaitang.pcm"
398    };
399
400    long bitrate = 500000;
401
402    for (int i = 0; i < 5; i++)
403    {
404        vector<string> dest = SplitStringFully(fileList[i], "_");
405        if (dest.size() < 4) {
406            cout << "split error !!!" << endl;
407            return;
408        }
409        int32_t channelCount = stoi(dest[2]);
410        int32_t sampleRate = stoi(dest[1]);
411
412        int32_t sampleFormat;
413        if (dest[0] == "s16") {
414            sampleFormat = OH_BitsPerSample::SAMPLE_S16LE;
415        } else {
416            sampleFormat = OH_BitsPerSample::SAMPLE_S32LE;
417        }
418
419        OH_AVFormat* format = getAVFormatFlac(channelCount, sampleRate, sampleFormat, bitrate);
420        ASSERT_NE(nullptr, format);
421
422        string inputFile = fileList[i];
423        string outputFile = "FUNCTION_005_" + dest[0] + "_" + to_string(sampleRate) + ".flac";
424
425        encoderDemo->NativeRunCase(inputFile, outputFile, encoderName.c_str(), format);
426
427        OH_AVFormat_Destroy(format);
428    }
429    delete encoderDemo;
430}
431
432
433/**
434 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_006
435 * @tc.name      : flac(different channel num)
436 * @tc.desc      : function check
437 */
438HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_006, TestSize.Level2)
439{
440    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
441    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
442
443    string fileList[] = {
444        "s16_8000_1_dayuhaitang.pcm",
445        "s16_48000_1_dayuhaitang.pcm",
446        "s16_48000_2_dayuhaitang.pcm",
447        "s16_48000_8_dayuhaitang.pcm"
448    };
449
450    long bitrate = 500000;
451
452    for (int i = 0; i < 4; i++)
453    {
454        vector<string> dest = SplitStringFully(fileList[i], "_");
455        if (dest.size() < 4)
456        {
457            cout << "split error !!!" << endl;
458            return;
459        }
460        int32_t channelCount = stoi(dest[2]);
461        int32_t sampleRate = stoi(dest[1]);
462
463        int32_t sampleFormat;
464        if (dest[0] == "s16") {
465            sampleFormat = OH_BitsPerSample::SAMPLE_S16LE;
466        } else {
467            sampleFormat = OH_BitsPerSample::SAMPLE_S32LE;
468        }
469
470        OH_AVFormat* format = getAVFormatFlac(channelCount, sampleRate, sampleFormat, bitrate);
471        ASSERT_NE(nullptr, format);
472
473        string inputFile = fileList[i];
474        string outputFile = "FUNCTION_006_" + dest[0] + "_" + to_string(channelCount) + ".flac";
475
476        encoderDemo->NativeRunCase(inputFile, outputFile, encoderName.c_str(), format);
477
478        OH_AVFormat_Destroy(format);
479    }
480    delete encoderDemo;
481}
482
483/**
484 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_007
485 * @tc.name      : Flush(AAC)
486 * @tc.desc      : function check
487 */
488HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_007, TestSize.Level2)
489{
490    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
491    string decoderName = "OH.Media.Codec.Encoder.Audio.AAC";
492
493    string inputFile = "f32le_16000_2_dayuhaitang.pcm";
494    string firstOutputFile = "FUNCTION_007_1.aac";
495    string secondOutputFile = "FUNCTION_007_2.aac";
496
497    OH_AVFormat* format = OH_AVFormat_Create();
498    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 2);
499    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 16000);
500    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_F32LE);
501    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_F32LE);
502    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
503    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
504    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 129000);
505
506    encoderDemo->NativeRunCaseFlush(inputFile, firstOutputFile, secondOutputFile, decoderName.c_str(), format);
507
508    bool isSame = compareFile(firstOutputFile, secondOutputFile);
509    ASSERT_EQ(true, isSame);
510
511    OH_AVFormat_Destroy(format);
512    delete encoderDemo;
513}
514
515/**
516 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_008
517 * @tc.name      : Flush(flac)
518 * @tc.desc      : function check
519 */
520HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_008, TestSize.Level2)
521{
522    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
523    string decoderName = "OH.Media.Codec.Encoder.Audio.Flac";
524
525    string inputFile = "s16_48000_2_dayuhaitang.pcm";
526    string firstOutputFile = "FUNCTION_008_1.flac";
527    string secondOutputFile = "FUNCTION_008_2.flac";
528
529    OH_AVFormat* format = OH_AVFormat_Create();
530    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 2);
531    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 48000);
532    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_S16LE);
533    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_S16LE);
534    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
535    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
536    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 129000);
537    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE,
538        COMMON_FLAC_NUM * CHANNEL_COUNT_STEREO * S16_BITS_PER_SAMPLE);
539
540    encoderDemo->NativeRunCaseFlush(inputFile, firstOutputFile, secondOutputFile, decoderName.c_str(), format);
541
542    bool isSame = compareFile(firstOutputFile, secondOutputFile);
543    ASSERT_EQ(true, isSame);
544
545    OH_AVFormat_Destroy(format);
546    delete encoderDemo;
547}
548
549/**
550 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_009
551 * @tc.name      : Reset(AAC)
552 * @tc.desc      : function check
553 */
554HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_009, TestSize.Level2)
555{
556    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
557    string decoderName = "OH.Media.Codec.Encoder.Audio.AAC";
558
559    string inputFile = "f32le_16000_2_dayuhaitang.pcm";
560    string firstOutputFile = "FUNCTION_009_1.aac";
561    string secondOutputFile = "FUNCTION_009_2.aac";
562
563    OH_AVFormat* format = OH_AVFormat_Create();
564    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 2);
565    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 16000);
566    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_F32LE);
567    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_F32LE);
568    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
569    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
570    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 129000);
571
572    encoderDemo->NativeRunCaseReset(inputFile, firstOutputFile, secondOutputFile, decoderName.c_str(), format);
573
574    bool isSame = compareFile(firstOutputFile, secondOutputFile);
575    ASSERT_EQ(true, isSame);
576
577    OH_AVFormat_Destroy(format);
578    delete encoderDemo;
579}
580
581/**
582 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_010
583 * @tc.name      : Reset(flac)
584 * @tc.desc      : function check
585 */
586HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_010, TestSize.Level2)
587{
588    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
589    string decoderName = "OH.Media.Codec.Encoder.Audio.Flac";
590
591    string inputFile = "s16_48000_2_dayuhaitang.pcm";
592    string firstOutputFile = "FUNCTION_010_1.flac";
593    string secondOutputFile = "FUNCTION_010_2.flac";
594
595    OH_AVFormat* format = OH_AVFormat_Create();
596    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 2);
597    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 48000);
598    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_S16LE);
599    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_S16LE);
600    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
601    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
602    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 129000);
603    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE,
604        COMMON_FLAC_NUM * CHANNEL_COUNT_STEREO * S16_BITS_PER_SAMPLE);
605
606    encoderDemo->NativeRunCaseReset(inputFile, firstOutputFile, secondOutputFile, decoderName.c_str(), format);
607
608    bool isSame = compareFile(firstOutputFile, secondOutputFile);
609    ASSERT_EQ(true, isSame);
610
611    OH_AVFormat_Destroy(format);
612    delete encoderDemo;
613}
614
615
616/**
617 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_011
618 * @tc.name      : OH_AudioEncoder_GetOutputDescription
619 * @tc.desc      : function check
620 */
621HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_011, TestSize.Level2)
622{
623    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
624    string encoderName = "OH.Media.Codec.Encoder.Audio.AAC";
625
626    string inputFile = "f32le_16000_2_dayuhaitang.pcm";
627    string outputFile = "FUNCTION_011.aac";
628
629    OH_AVFormat* format = OH_AVFormat_Create();
630    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 2);
631    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 16000);
632    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_F32LE);
633    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_F32LE);
634    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, STEREO);
635    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
636    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 129000);
637
638    OH_AVFormat* formatGet = encoderDemo->NativeRunCaseGetOutputDescription(inputFile, outputFile, encoderName.c_str(),
639        format);
640    ASSERT_NE(nullptr, formatGet);
641
642    int32_t channelNum_Get;
643    int32_t sampleRate_Get;
644    int64_t bitrate_Get;
645    OH_AVFormat_GetIntValue(formatGet, OH_MD_KEY_AUD_CHANNEL_COUNT, &channelNum_Get);
646    OH_AVFormat_GetIntValue(formatGet, OH_MD_KEY_AUD_SAMPLE_RATE, &sampleRate_Get);
647    OH_AVFormat_GetLongValue(formatGet, OH_MD_KEY_BITRATE, &bitrate_Get);
648
649    const char* testStr = nullptr;
650    OH_AVFormat_GetStringValue(formatGet, OH_MD_KEY_CODEC_MIME, &testStr);
651
652    cout << "channel num is " << channelNum_Get << ", sample rate is " <<
653    sampleRate_Get << ", bitrate is " << bitrate_Get << endl;
654
655    cout << "OH_MD_KEY_CODEC_MIME is " << testStr << endl;
656
657    ASSERT_EQ(2, channelNum_Get);
658    ASSERT_EQ(16000, sampleRate_Get);
659    ASSERT_EQ(129000, bitrate_Get);
660
661    OH_AVFormat_Destroy(format);
662    OH_AVFormat_Destroy(formatGet);
663    delete encoderDemo;
664}
665
666/**
667 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_012
668 * @tc.name      : AAC(thread)
669 * @tc.desc      : Function test
670 */
671HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_012, TestSize.Level2)
672{
673    vector<thread> threadVec;
674    string encoderName = "OH.Media.Codec.Encoder.Audio.AAC";
675
676    string inputFile = "f32le_16000_2_dayuhaitang.pcm";
677
678    for (int32_t i = 0; i < 16; i++)
679    {
680        string outputFile = "FUNCTION_012_" + to_string(i) + ".aac";
681        threadVec.push_back(thread(runEncode, encoderName, inputFile, outputFile, i));
682    }
683    for (uint32_t i = 0; i < threadVec.size(); i++)
684    {
685        threadVec[i].join();
686    }
687    for (int32_t i = 0; i < 16; i++)
688    {
689        ASSERT_EQ(AV_ERR_OK, testResult[i]);
690    }
691}
692
693/**
694 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_013
695 * @tc.name      : flac(thread)
696 * @tc.desc      : Function test
697 */
698HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_013, TestSize.Level2)
699{
700    vector<thread> threadVec;
701    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
702
703    string inputFile = "s16_48000_2_dayuhaitang.pcm";
704
705    for (int32_t i = 0; i < 16; i++)
706    {
707        string outputFile = "FUNCTION_013_" + to_string(i) + ".flac";
708        threadVec.push_back(thread(runEncode, encoderName, inputFile, outputFile, i));
709    }
710    for (uint32_t i = 0; i < threadVec.size(); i++)
711    {
712        threadVec[i].join();
713    }
714    for (int32_t i = 0; i < 16; i++)
715    {
716        ASSERT_EQ(AV_ERR_OK, testResult[i]);
717    }
718}
719
720/**
721 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_100
722 * @tc.name      : AAC
723 * @tc.desc      : function check
724 */
725HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_100, TestSize.Level2)
726{
727    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
728    string encoderName = "OH.Media.Codec.Encoder.Audio.AAC";
729
730    string inputFile = "B_Bird_on_a_wire_1_f32.pcm";
731    string outputFile = "FUNCTION_100.aac";
732
733    OH_AVFormat* format = OH_AVFormat_Create();
734    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 1);
735    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 48000);
736    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_F32LE);
737
738    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_F32LE);
739    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, MONO);
740    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE);
741    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 128000);
742    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, 48000);
743    ASSERT_NE(nullptr, format);
744
745    encoderDemo->NativeRunCasePerformance(inputFile, outputFile, encoderName.c_str(), format);
746
747    OH_AVFormat_Destroy(format);
748    delete encoderDemo;
749}
750
751/**
752 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_200
753 * @tc.name      : FLAC
754 * @tc.desc      : function check
755 */
756HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_200, TestSize.Level2)
757{
758    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
759    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
760
761    string inputFile = "B_Bird_on_a_wire_1_1.pcm";
762    string outputFile = "FUNCTION_200.flac";
763
764    OH_AVFormat* format = OH_AVFormat_Create();
765    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 1);
766    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 48000);
767    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_S16LE);
768    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_S16LE);
769    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, MONO);
770    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 429000);
771    OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, -2);
772    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, 48000);
773    ASSERT_NE(nullptr, format);
774
775    encoderDemo->NativeRunCasePerformance(inputFile, outputFile, encoderName.c_str(), format);
776
777    OH_AVFormat_Destroy(format);
778    delete encoderDemo;
779}
780
781/**
782 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_201
783 * @tc.name      : FLAC
784 * @tc.desc      : function check
785 */
786HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_201, TestSize.Level2)
787{
788    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
789    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
790
791    string inputFile = "free_loss.pcm";
792    string outputFile = "FUNCTION_201.flac";
793
794    OH_AVFormat* format = OH_AVFormat_Create();
795    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 1);
796    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 48000);
797    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_S16LE);
798    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_S16LE);
799    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, MONO);
800    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 613000);
801    OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, -2);
802    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, 48000);
803    ASSERT_NE(nullptr, format);
804
805    encoderDemo->NativeRunCasePerformance(inputFile, outputFile, encoderName.c_str(), format);
806
807    OH_AVFormat_Destroy(format);
808    delete encoderDemo;
809}
810
811/**
812 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_202
813 * @tc.name      : FLAC
814 * @tc.desc      : function check
815 */
816HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_202, TestSize.Level2)
817{
818    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
819    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
820
821    string inputFile = "B_Bird_on_a_wire_1_1.pcm";
822    string outputFile = "FUNCTION_202.dat";
823
824    OH_AVFormat* format = OH_AVFormat_Create();
825    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 1);
826    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 48000);
827    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_S16LE);
828    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_S16LE);
829    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, MONO);
830    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 429000);
831    OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, -2);
832    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, 48000);
833    ASSERT_NE(nullptr, format);
834
835    encoderDemo->TestRunCase(inputFile, outputFile, encoderName.c_str(), format);
836
837    OH_AVFormat_Destroy(format);
838    delete encoderDemo;
839}
840
841/**
842 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_203
843 * @tc.name      : FLAC
844 * @tc.desc      : function check
845 */
846HWTEST_F(NativeFunctionTest, SUB_MULTIMEDIA_AUDIO_ENCODER_FUNCTION_203, TestSize.Level2)
847{
848    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
849    string encoderName = "OH.Media.Codec.Encoder.Audio.Flac";
850
851    string inputFile = "free_loss.pcm";
852    string outputFile = "FUNCTION_203.dat";
853
854    OH_AVFormat* format = OH_AVFormat_Create();
855    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, 1);
856    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, 96000);
857    OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, OH_BitsPerSample::SAMPLE_S16LE);
858    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, OH_BitsPerSample::SAMPLE_S16LE);
859    OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, MONO);
860    OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, 613000);
861    OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, -2);
862    OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, 16384);
863    ASSERT_NE(nullptr, format);
864
865    encoderDemo->TestRunCase(inputFile, outputFile, encoderName.c_str(), format);
866
867    OH_AVFormat_Destroy(format);
868    delete encoderDemo;
869}