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 <iostream>
18#include <thread>
19#include <vector>
20#include <ctime>
21#include "gtest/gtest.h"
22#include "AudioEncoderDemoCommon.h"
23#include "fcntl.h"
24#include "media_description.h"
25#include "avcodec_info.h"
26#include "avcodec_errors.h"
27#include "av_common.h"
28#include "meta/format.h"
29#include "avcodec_audio_common.h"
30
31using namespace std;
32using namespace testing::ext;
33using namespace OHOS;
34using namespace OHOS::MediaAVCodec;
35
36namespace {
37    class InnerStablityTest : public testing::Test {
38    public:
39        static void SetUpTestCase();
40        static void TearDownTestCase();
41        void SetUp() override;
42        void TearDown() override;
43    };
44
45    void InnerStablityTest::SetUpTestCase() {}
46    void InnerStablityTest::TearDownTestCase() {}
47    void InnerStablityTest::SetUp() {}
48    void InnerStablityTest::TearDown() {}
49    constexpr int RUN_TIMES = 100000;
50    constexpr int RUN_TIME = 12 * 3600;
51    string inputFilePath = "/data/audioIn.mp3";
52    string outputFilePath = "/data/audioOut.pcm";
53}
54
55/**
56 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_001
57 * @tc.name      : InnerCreateByMime(1000 times)
58 * @tc.desc      : Stability test
59 */
60HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_001, TestSize.Level2)
61{
62    srand(time(nullptr) * 10);
63    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
64    ASSERT_NE(nullptr, encoderDemo);
65    string mimeType[] = { "audio/mp4a-latm", "audio/flac" };
66
67    for (int i = 0; i < RUN_TIMES; i++)
68    {
69        int typeIndex = rand() % 4;
70        encoderDemo->InnerCreateByMime(mimeType[typeIndex].c_str());
71        cout << "run time is: " << i << endl;
72        encoderDemo->InnerDestroy();
73    }
74
75    delete encoderDemo;
76}
77
78/**
79 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_002
80 * @tc.name      : InnerCreateByName(1000 times)
81 * @tc.desc      : Stability test
82 */
83HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_002, TestSize.Level2)
84{
85    srand(time(nullptr) * 10);
86    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
87    ASSERT_NE(nullptr, encoderDemo);
88    for (int i = 0; i < RUN_TIMES; i++)
89    {
90        encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
91        cout << "run time is: " << i << endl;
92        encoderDemo->InnerDestroy();
93    }
94    delete encoderDemo;
95}
96
97/**
98 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_003
99 * @tc.name      : InnerPrepare(1000 times)
100 * @tc.desc      : Stability test
101 */
102HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_003, TestSize.Level2)
103{
104    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
105    encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
106    ASSERT_NE(nullptr, encoderDemo);
107    Format format;
108
109    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
110    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
111    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
112    format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
113    format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
114    format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
115
116    encoderDemo->InnerConfigure(format);
117
118    for (int i = 0; i < RUN_TIMES; i++)
119    {
120        int32_t ret = encoderDemo->InnerPrepare();
121        cout << "run time is: " << i << ", ret is:" << ret << endl;
122    }
123
124    encoderDemo->InnerDestroy();
125
126    delete encoderDemo;
127}
128
129
130/**
131 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_004
132 * @tc.name      : InnerStart(1000 times)
133 * @tc.desc      : Stability test
134 */
135HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_004, TestSize.Level2)
136{
137    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
138    encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
139    ASSERT_NE(nullptr, encoderDemo);
140    Format format;
141
142    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
143    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
144    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
145    format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
146    format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
147    format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
148
149    std::shared_ptr<AEncSignal> signal_ = encoderDemo->getSignal();
150    std::shared_ptr<InnerAEnDemoCallback> cb_ = make_unique<InnerAEnDemoCallback>(signal_);
151    int32_t ret = encoderDemo->InnerSetCallback(cb_);
152
153    encoderDemo->InnerConfigure(format);
154    encoderDemo->InnerPrepare();
155
156    for (int i = 0; i < RUN_TIMES; i++)
157    {
158        ret = encoderDemo->InnerStart();
159        cout << "run time is: " << i << ", ret is:" << ret << endl;
160    }
161
162    encoderDemo->InnerDestroy();
163
164    delete encoderDemo;
165}
166
167/**
168 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_005
169 * @tc.name      : InnerStop(1000 times)
170 * @tc.desc      : Stability test
171 */
172HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_005, TestSize.Level2)
173{
174    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
175    encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
176    ASSERT_NE(nullptr, encoderDemo);
177    Format format;
178
179    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
180    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
181    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
182    format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
183    format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
184    format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
185
186    std::shared_ptr<AEncSignal> signal_ = encoderDemo->getSignal();
187    std::shared_ptr<InnerAEnDemoCallback> cb_ = make_unique<InnerAEnDemoCallback>(signal_);
188    int32_t ret = encoderDemo->InnerSetCallback(cb_);
189
190    encoderDemo->InnerConfigure(format);
191    encoderDemo->InnerPrepare();
192    encoderDemo->InnerStart();
193
194    for (int i = 0; i < RUN_TIMES; i++)
195    {
196        ret = encoderDemo->InnerStop();
197        cout << "run time is: " << i << ", ret is:" << ret << endl;
198    }
199
200    encoderDemo->InnerDestroy();
201
202    delete encoderDemo;
203}
204
205
206/**
207 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_006
208 * @tc.name      : InnerDestroy(1000 times)
209 * @tc.desc      : Stability test
210 */
211HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_006, TestSize.Level2)
212{
213    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
214    encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
215    ASSERT_NE(nullptr, encoderDemo);
216    Format format;
217
218    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
219    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
220    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
221    format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
222    format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
223    format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
224
225    std::shared_ptr<AEncSignal> signal_ = encoderDemo->getSignal();
226    std::shared_ptr<InnerAEnDemoCallback> cb_ = make_unique<InnerAEnDemoCallback>(signal_);
227    int32_t ret = encoderDemo->InnerSetCallback(cb_);
228
229    encoderDemo->InnerConfigure(format);
230    encoderDemo->InnerPrepare();
231    encoderDemo->InnerStart();
232    encoderDemo->InnerStop();
233
234    for (int i = 0; i < RUN_TIMES; i++)
235    {
236        ret = encoderDemo->InnerDestroy();
237        cout << "run time is: " << i << ", ret is:" << ret << endl;
238    }
239
240    delete encoderDemo;
241}
242
243/**
244 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_007
245 * @tc.name      : InnerReset(1000 times)
246 * @tc.desc      : Stability test
247 */
248HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_007, TestSize.Level2)
249{
250    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
251    encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
252    ASSERT_NE(nullptr, encoderDemo);
253    Format format;
254
255    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
256    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
257    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
258    format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
259    format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
260    format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
261
262    std::shared_ptr<AEncSignal> signal_ = encoderDemo->getSignal();
263    std::shared_ptr<InnerAEnDemoCallback> cb_ = make_unique<InnerAEnDemoCallback>(signal_);
264    int32_t ret = encoderDemo->InnerSetCallback(cb_);
265
266    encoderDemo->InnerConfigure(format);
267    encoderDemo->InnerPrepare();
268    encoderDemo->InnerStart();
269
270    for (int i = 0; i < RUN_TIMES; i++)
271    {
272        ret = encoderDemo->InnerReset();
273        cout << "run time is: " << i << ", ret is:" << ret << endl;
274    }
275    encoderDemo->InnerDestroy();
276    delete encoderDemo;
277}
278
279/**
280 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_008
281 * @tc.name      : InnerFlush(1000 times)
282 * @tc.desc      : Stability test
283 */
284HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_008, TestSize.Level2)
285{
286    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
287    encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
288    ASSERT_NE(nullptr, encoderDemo);
289    Format format;
290
291    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
292    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
293    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
294    format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
295    format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
296    format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
297
298    std::shared_ptr<AEncSignal> signal_ = encoderDemo->getSignal();
299    std::shared_ptr<InnerAEnDemoCallback> cb_ = make_unique<InnerAEnDemoCallback>(signal_);
300    int32_t ret = encoderDemo->InnerSetCallback(cb_);
301
302    encoderDemo->InnerConfigure(format);
303    encoderDemo->InnerPrepare();
304    encoderDemo->InnerStart();
305
306    for (int i = 0; i < RUN_TIMES; i++)
307    {
308        ret = encoderDemo->InnerFlush();
309        cout << "run time is: " << i << ", ret is:" << ret << endl;
310    }
311    encoderDemo->InnerDestroy();
312    delete encoderDemo;
313}
314
315/**
316 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_009
317 * @tc.name      : InnerRelease(1000 times)
318 * @tc.desc      : Stability test
319 */
320HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_009, TestSize.Level2)
321{
322    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
323    encoderDemo->InnerCreateByName("OH.Media.Codec.Encoder.Audio.AAC");
324    ASSERT_NE(nullptr, encoderDemo);
325    Format format;
326
327    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
328    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
329    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
330    format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
331    format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
332    format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
333
334    std::shared_ptr<AEncSignal> signal_ = encoderDemo->getSignal();
335    std::shared_ptr<InnerAEnDemoCallback> cb_ = make_unique<InnerAEnDemoCallback>(signal_);
336    int32_t ret = encoderDemo->InnerSetCallback(cb_);
337
338    encoderDemo->InnerConfigure(format);
339    encoderDemo->InnerPrepare();
340    encoderDemo->InnerStart();
341
342    for (int i = 0; i < RUN_TIMES; i++)
343    {
344        ret = encoderDemo->InnerRelease();
345        cout << "run time is: " << i << ", ret is:" << ret << endl;
346    }
347    encoderDemo->InnerDestroy();
348    delete encoderDemo;
349}
350
351/**
352 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_010
353 * @tc.name      : aac(long time)
354 * @tc.desc      : Function test
355 */
356HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_010, TestSize.Level2)
357{
358    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
359    time_t startTime = time(nullptr);
360    ASSERT_NE(startTime, -1);
361    time_t curTime = startTime;
362
363    while (difftime(curTime, startTime) < RUN_TIME)
364    {
365        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
366        inputFilePath = "f32le_44100_1_dayuhaitang.pcm";
367        outputFilePath = "FUNCTION_010_stability.aac";
368
369        Format format;
370        format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
371        format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
372        format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
373        format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_F32LE);
374        format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_F32LE);
375        format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, MONO);
376        format.PutIntValue(MediaDescriptionKey::MD_KEY_AAC_IS_ADTS, 1);
377
378        encoderDemo->InnerRunCase(inputFilePath, outputFilePath, "OH.Media.Codec.Encoder.Audio.AAC", format);
379        curTime = time(nullptr);
380        ASSERT_NE(curTime, -1);
381    }
382    delete encoderDemo;
383}
384
385/**
386 * @tc.number    : SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_011
387 * @tc.name      : flac(long time)
388 * @tc.desc      : Function test
389 */
390HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_ENCODER_STABILITY_011, TestSize.Level2)
391{
392    AudioEncoderDemo* encoderDemo = new AudioEncoderDemo();
393    time_t startTime = time(nullptr);
394    ASSERT_NE(startTime, -1);
395    time_t curTime = startTime;
396
397    while (difftime(curTime, startTime) < RUN_TIME)
398    {
399        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
400        inputFilePath = "s16_44100_2_dayuhaitang.pcm";
401        outputFilePath = "FUNCTION_011_stability.flac";
402
403        Format format;
404        format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 2);
405        format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 44100);
406        format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 112000);
407        format.PutIntValue(MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE, AudioSampleFormat::SAMPLE_S32LE);
408        format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S32LE);
409        format.PutLongValue(MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT, STEREO);
410        format.PutIntValue(MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL, -2);
411        encoderDemo->InnerRunCase(inputFilePath, outputFilePath, "OH.Media.Codec.Encoder.Audio.Flac", format);
412        curTime = time(nullptr);
413        ASSERT_NE(curTime, -1);
414    }
415    delete encoderDemo;
416}
417