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 "AudioDecoderDemoCommon.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
30using namespace std;
31using namespace testing::ext;
32using namespace OHOS;
33using namespace OHOS::MediaAVCodec;
34
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
50    constexpr int RUN_TIMES = 100000;
51    constexpr int RUN_TIME = 12 * 3600;
52}
53
54/**
55 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_001
56 * @tc.name      : InnerCreateByMime(1000 times)
57 * @tc.desc      : Stability test
58 */
59HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_001, TestSize.Level2)
60{
61    srand(time(nullptr) * 10);
62    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
63
64    string mimeType[] = { "audio/mp4a-latm", "audio/mpeg", "audio/vorbis", "audio/flac", "audio/amr-wb",
65                          "audio/3gpp", "audio/g711mu" };
66
67    for (int i = 0; i < RUN_TIMES; i++)
68    {
69        int typeIndex = rand() % 4;
70        decoderDemo->InnerCreateByMime(mimeType[typeIndex].c_str());
71        cout << "run time is: " << i << endl;
72        int32_t ret = decoderDemo->InnerDestroy();
73        ASSERT_EQ(AVCS_ERR_OK, ret);
74    }
75
76    delete decoderDemo;
77}
78
79
80/**
81 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_002
82 * @tc.name      : InnerCreateByName(1000 times)
83 * @tc.desc      : Stability test
84 */
85HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_002, TestSize.Level2)
86{
87    srand(time(nullptr) * 10);
88    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
89
90    for (int i = 0; i < RUN_TIMES; i++)
91    {
92        decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
93        cout << "run time is: " << i << endl;
94        int32_t ret = decoderDemo->InnerDestroy();
95        ASSERT_EQ(AVCS_ERR_OK, ret);
96    }
97    delete decoderDemo;
98}
99
100
101/**
102 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_003
103 * @tc.name      : InnerPrepare(1000 times)
104 * @tc.desc      : Stability test
105 */
106HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_003, TestSize.Level2)
107{
108    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
109    int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
110    ASSERT_EQ(AVCS_ERR_OK, ret);
111    std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
112    std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
113    decoderDemo->InnerSetCallback(cb_);
114
115    Format format;
116
117    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
118    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
119    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
120
121    decoderDemo->InnerConfigure(format);
122
123    for (int i = 0; i < RUN_TIMES; i++)
124    {
125        ret = decoderDemo->InnerPrepare();
126        cout << "run time is: " << i << ", ret is:" << ret << endl;
127    }
128
129    decoderDemo->InnerDestroy();
130
131    delete decoderDemo;
132}
133
134
135/**
136 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_004
137 * @tc.name      : InnerStart(1000 times)
138 * @tc.desc      : Stability test
139 */
140HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_004, TestSize.Level2)
141{
142    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
143    int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
144    ASSERT_EQ(AVCS_ERR_OK, ret);
145    std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
146    std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
147    decoderDemo->InnerSetCallback(cb_);
148    Format format;
149
150    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
151    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
152    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
153
154    decoderDemo->InnerConfigure(format);
155    decoderDemo->InnerPrepare();
156
157    for (int i = 0; i < RUN_TIMES; i++)
158    {
159        ret = decoderDemo->InnerStart();
160        cout << "run time is: " << i << ", ret is:" << ret << endl;
161    }
162
163    decoderDemo->InnerDestroy();
164
165    delete decoderDemo;
166}
167
168
169/**
170 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_005
171 * @tc.name      : InnerStop(1000 times)
172 * @tc.desc      : Stability test
173 */
174HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_005, TestSize.Level2)
175{
176    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
177    int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
178    ASSERT_EQ(AVCS_ERR_OK, ret);
179    std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
180    std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
181    decoderDemo->InnerSetCallback(cb_);
182    Format format;
183
184    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
185    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
186    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
187
188    decoderDemo->InnerConfigure(format);
189    decoderDemo->InnerPrepare();
190    decoderDemo->InnerStart();
191
192    for (int i = 0; i < RUN_TIMES; i++)
193    {
194        ret = decoderDemo->InnerStop();
195        cout << "run time is: " << i << ", ret is:" << ret << endl;
196    }
197
198    decoderDemo->InnerDestroy();
199
200    delete decoderDemo;
201}
202
203/**
204 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_006
205 * @tc.name      : InnerDestroy(1000 times)
206 * @tc.desc      : Stability test
207 */
208HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_006, TestSize.Level2)
209{
210    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
211    int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
212    ASSERT_EQ(AVCS_ERR_OK, ret);
213    std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
214    std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
215    decoderDemo->InnerSetCallback(cb_);
216    Format format;
217
218    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
219    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
220    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
221
222    decoderDemo->InnerConfigure(format);
223    decoderDemo->InnerPrepare();
224    decoderDemo->InnerStart();
225    decoderDemo->InnerStop();
226
227    for (int i = 0; i < RUN_TIMES; i++)
228    {
229        ret = decoderDemo->InnerDestroy();
230        cout << "run time is: " << i << ", ret is:" << ret << endl;
231    }
232    delete decoderDemo;
233}
234
235
236/**
237 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_007
238 * @tc.name      : InnerReset(1000 times)
239 * @tc.desc      : Stability test
240 */
241HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_007, TestSize.Level2)
242{
243    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
244    int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
245    ASSERT_EQ(AVCS_ERR_OK, ret);
246    std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
247    std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
248    decoderDemo->InnerSetCallback(cb_);
249    Format format;
250
251    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
252    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
253    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
254
255    decoderDemo->InnerConfigure(format);
256    decoderDemo->InnerPrepare();
257    decoderDemo->InnerStart();
258
259    for (int i = 0; i < RUN_TIMES; i++)
260    {
261        ret = decoderDemo->InnerReset();
262        cout << "run time is: " << i << ", ret is:" << ret << endl;
263    }
264    decoderDemo->InnerDestroy();
265    delete decoderDemo;
266}
267
268
269/**
270 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_008
271 * @tc.name      : InnerFlush(1000 times)
272 * @tc.desc      : Stability test
273 */
274HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_008, TestSize.Level2)
275{
276    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
277    int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
278    ASSERT_EQ(AVCS_ERR_OK, ret);
279    std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
280    std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
281    decoderDemo->InnerSetCallback(cb_);
282    Format format;
283
284    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
285    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
286    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
287
288    decoderDemo->InnerConfigure(format);
289    decoderDemo->InnerPrepare();
290    decoderDemo->InnerStart();
291
292    for (int i = 0; i < RUN_TIMES; i++)
293    {
294        ret = decoderDemo->InnerFlush();
295        cout << "run time is: " << i << ", ret is:" << ret << endl;
296    }
297    decoderDemo->InnerDestroy();
298    delete decoderDemo;
299}
300
301
302/**
303 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_009
304 * @tc.name      : InnerRelease(1000 times)
305 * @tc.desc      : Stability test
306 */
307HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_009, TestSize.Level2)
308{
309    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
310    int32_t ret = decoderDemo->InnerCreateByName("OH.Media.Codec.Decoder.Audio.Mpeg");
311    ASSERT_EQ(AVCS_ERR_OK, ret);
312    std::shared_ptr<ADecSignal> signal_ = decoderDemo->getSignal();
313    std::shared_ptr<InnerADecDemoCallback> cb_ = make_unique<InnerADecDemoCallback>(signal_);
314    decoderDemo->InnerSetCallback(cb_);
315    Format format;
316
317    format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
318    format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
319    format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
320
321    decoderDemo->InnerConfigure(format);
322    decoderDemo->InnerPrepare();
323    decoderDemo->InnerStart();
324
325    for (int i = 0; i < RUN_TIMES; i++)
326    {
327        ret = decoderDemo->InnerRelease();
328        cout << "run time is: " << i << ", ret is:" << ret << endl;
329    }
330    decoderDemo->InnerDestroy();
331    delete decoderDemo;
332}
333
334
335/**
336 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_010
337 * @tc.name      : MP3(long time)
338 * @tc.desc      : Stability test
339 */
340HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_010, TestSize.Level2)
341{
342    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
343    time_t startTime = time(nullptr);
344    ASSERT_NE(startTime, -1);
345    time_t curTime = startTime;
346
347    while (difftime(curTime, startTime) < RUN_TIME)
348    {
349        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
350        std::string inputFilePath = "s16p_128k_16000_1_dayuhaitang.mp3";
351        std::string outputFilePath = "SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_010.pcm";
352
353        Format format;
354        format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 128000);
355        format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
356        format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 16000);
357
358        decoderDemo->InnerRunCase(inputFilePath, outputFilePath, "OH.Media.Codec.Decoder.Audio.Mpeg", format);
359        curTime = time(nullptr);
360        ASSERT_NE(curTime, -1);
361    }
362    delete decoderDemo;
363}
364
365
366/**
367 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_011
368 * @tc.name      : aac(long time)
369 * @tc.desc      : Stability test
370 */
371HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_011, TestSize.Level2)
372{
373    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
374    time_t startTime = time(nullptr);
375    ASSERT_NE(startTime, -1);
376    time_t curTime = startTime;
377
378    while (difftime(curTime, startTime) < RUN_TIME)
379    {
380        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
381        std::string inputFilePath = "fltp_aac_low_128k_16000_2_dayuhaitang.aac";
382        std::string outputFilePath = "SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_011.pcm";
383
384        Format format;
385        format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 2);
386        format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 16000);
387        format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 128000);
388        format.PutIntValue(MediaDescriptionKey::MD_KEY_AAC_IS_ADTS, 1);
389
390        decoderDemo->InnerRunCase(inputFilePath, outputFilePath, "avdec_aac", format);
391        curTime = time(nullptr);
392        ASSERT_NE(curTime, -1);
393    }
394    delete decoderDemo;
395}
396
397
398/**
399 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_012
400 * @tc.name      : avdec_vorbis(long time)
401 * @tc.desc      : Stability test
402 */
403HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_012, TestSize.Level2)
404{
405    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
406    time_t startTime = time(nullptr);
407    ASSERT_NE(startTime, -1);
408    time_t curTime = startTime;
409
410    while (difftime(curTime, startTime) < RUN_TIME)
411    {
412        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
413        std::string inputFilePath = "fltp_128k_16000_2_dayuhaitang.ogg";
414        std::string outputFilePath = "SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_012.pcm";
415
416        Format format;
417        format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 128000);
418        format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 2);
419        format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 16000);
420
421        decoderDemo->InnerRunCase(inputFilePath, outputFilePath, "avdec_vorbis", format);
422        curTime = time(nullptr);
423        ASSERT_NE(curTime, -1);
424    }
425    delete decoderDemo;
426}
427
428
429/**
430 * @tc.number    : SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_013
431 * @tc.name      : avdec_flac(long time)
432 * @tc.desc      : Stability test
433 */
434HWTEST_F(InnerStablityTest, SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_013, TestSize.Level2)
435{
436    AudioDecoderDemo* decoderDemo = new AudioDecoderDemo();
437    time_t startTime = time(nullptr);
438    ASSERT_NE(startTime, -1);
439    time_t curTime = startTime;
440
441    while (difftime(curTime, startTime) < RUN_TIME)
442    {
443        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
444        std::string inputFilePath = "s32_16000_2_dayuhaitang.flac";
445        std::string outputFilePath = "SUB_MULTIMEDIA_AUDIO_DECODER_STABILITY_013.pcm";
446
447        Format format;
448        format.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 357000);
449        format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 2);
450        format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 16000);
451        format.PutIntValue("bits_per_coded_sample", 24);
452
453        decoderDemo->InnerRunCase(inputFilePath, outputFilePath, "avdec_flac", format);
454        curTime = time(nullptr);
455        ASSERT_NE(curTime, -1);
456    }
457    delete decoderDemo;
458}