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 "AVMuxerDemo.h"
23#include "fcntl.h"
24#include "avcodec_errors.h"
25#include "securec.h"
26
27using namespace std;
28using namespace testing::ext;
29using namespace OHOS;
30using namespace OHOS::MediaAVCodec;
31using namespace OHOS::Media;
32constexpr int32_t SAMPLE_RATE_44100 = 44100;
33constexpr int32_t CHANNEL_COUNT = 2;
34constexpr int32_t BUFFER_SIZE = 100;
35constexpr int32_t SAMPLE_RATE_352 = 352;
36constexpr int32_t SAMPLE_RATE_288 = 288;
37constexpr int32_t BUFFER_SIZE_NUM = 1024 * 1024;
38
39namespace {
40class InnerAVMuxerStablityTest : public testing::Test {
41public:
42    static void SetUpTestCase();
43    static void TearDownTestCase();
44    void SetUp() override;
45    void TearDown() override;
46};
47
48void InnerAVMuxerStablityTest::SetUpTestCase() {}
49void InnerAVMuxerStablityTest::TearDownTestCase() {}
50void InnerAVMuxerStablityTest::SetUp() {}
51void InnerAVMuxerStablityTest::TearDown() {}
52
53static int g_inputFile = -1;
54static const int DATA_AUDIO_ID = 0;
55static const int DATA_VIDEO_ID = 1;
56
57constexpr int RUN_TIMES = 1000;
58constexpr int RUN_TIME = 8 * 3600;
59
60int32_t g_testResult[10] = { -1 };
61
62int32_t SetRotation(AVMuxerDemo *muxerDemo)
63{
64    int32_t rotation = 0;
65    int32_t ret = muxerDemo->InnerSetRotation(rotation);
66    return ret;
67}
68
69int32_t AddTrack(AVMuxerDemo *muxerDemo, int32_t &trackIndex)
70{
71    std::shared_ptr<Meta> audioParams = std::make_shared<Meta>();
72    int extraSize = 0;
73    read(g_inputFile, static_cast<void *>(&extraSize), sizeof(extraSize));
74    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
75        std::vector<uint8_t> buffer(extraSize);
76        read(g_inputFile, buffer.data(), extraSize);
77        audioParams->Set<Tag::MEDIA_CODEC_CONFIG>(buffer);
78    }
79    audioParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::AUDIO_AAC);
80    audioParams->Set<Tag::AUDIO_CHANNEL_COUNT>(CHANNEL_COUNT);
81    audioParams->Set<Tag::AUDIO_SAMPLE_RATE>(SAMPLE_RATE_44100);
82
83    int32_t trackId = muxerDemo->InnerAddTrack(trackIndex, audioParams);
84    return trackId;
85}
86
87int32_t WriteSample(AVMuxerDemo *muxerDemo)
88{
89    uint32_t trackIndex = 0;
90    uint8_t data[100];
91    std::shared_ptr<AVBuffer> avMemBuffer = AVBuffer::CreateAVBuffer(data, sizeof(data), sizeof(data));
92    avMemBuffer->flag_ = static_cast<uint32_t>(Plugins::AVBufferFlag::NONE);
93
94    int32_t ret = muxerDemo->InnerWriteSample(trackIndex, avMemBuffer);
95
96    return ret;
97}
98
99int32_t AddAudioTrack(AVMuxerDemo *muxerDemo, int32_t &trackIndex)
100{
101    std::shared_ptr<Meta> audioParams = std::make_shared<Meta>();
102    int extraSize = 0;
103    read(g_inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
104    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
105        std::vector<uint8_t> buffer(extraSize);
106        read(g_inputFile, buffer.data(), extraSize);
107        audioParams->Set<Tag::MEDIA_CODEC_CONFIG>(buffer);
108    }
109    audioParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::AUDIO_MPEG);
110    audioParams->Set<Tag::AUDIO_CHANNEL_COUNT>(CHANNEL_COUNT);
111    audioParams->Set<Tag::AUDIO_SAMPLE_RATE>(SAMPLE_RATE_44100);
112
113    int32_t trackId = muxerDemo->InnerAddTrack(trackIndex, audioParams);
114    return trackId;
115}
116
117int32_t AddAudioTrackAAC(AVMuxerDemo *muxerDemo, int32_t &trackIndex)
118{
119    std::shared_ptr<Meta> audioParams = std::make_shared<Meta>();
120    int extraSize = 0;
121    read(g_inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
122    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
123        std::vector<uint8_t> buffer(extraSize);
124        read(g_inputFile, buffer.data(), extraSize);
125        audioParams->Set<Tag::MEDIA_CODEC_CONFIG>(buffer);
126    }
127    audioParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::AUDIO_AAC);
128    audioParams->Set<Tag::AUDIO_CHANNEL_COUNT>(CHANNEL_COUNT);
129    audioParams->Set<Tag::AUDIO_SAMPLE_RATE>(SAMPLE_RATE_44100);
130    audioParams->Set<Tag::MEDIA_PROFILE>(AAC_PROFILE_LC);
131
132    int32_t trackId = muxerDemo->InnerAddTrack(trackIndex, audioParams);
133    return trackId;
134}
135
136int32_t AddVideoTrack(AVMuxerDemo *muxerDemo, int32_t &trackIndex)
137{
138    std::shared_ptr<Meta> videoParams = std::make_shared<Meta>();
139    int extraSize = 0;
140    read(g_inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
141    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
142        std::vector<uint8_t> buffer(extraSize);
143        read(g_inputFile, buffer.data(), extraSize);
144        videoParams->Set<Tag::MEDIA_CODEC_CONFIG>(buffer);
145    }
146    videoParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::VIDEO_MPEG4);
147    videoParams->Set<Tag::VIDEO_WIDTH>(SAMPLE_RATE_352);
148    videoParams->Set<Tag::VIDEO_HEIGHT>(SAMPLE_RATE_288);
149
150    int32_t trackId = muxerDemo->InnerAddTrack(trackIndex, videoParams);
151    return trackId;
152}
153
154void RemoveHeader()
155{
156    int extraSize = 0;
157    unsigned char buffer[100] = {0};
158    read(g_inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
159    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
160        read(g_inputFile, buffer, extraSize);
161    }
162}
163
164void WriteTrackSample(AVMuxerDemo *muxerDemo, int audioTrackIndex, int videoTrackIndex)
165{
166    int dataTrackId = 0;
167    int dataSize = 0;
168    int ret = 0;
169    int trackId = 0;
170    uint32_t trackIndex;
171
172    auto alloc = AVAllocatorFactory::CreateSharedAllocator(MemoryFlag::MEMORY_READ_WRITE);
173    std::shared_ptr<AVBuffer> avMemBuffer = AVBuffer::CreateAVBuffer(alloc, BUFFER_SIZE_NUM);
174    do {
175        ret = read(g_inputFile, static_cast<void*>(&dataTrackId), sizeof(dataTrackId));
176        if (ret <= 0) {
177            return;
178        }
179        ret = read(g_inputFile, static_cast<void*>(&avMemBuffer->pts_), sizeof(avMemBuffer->pts_));
180        if (ret <= 0) {
181            return;
182        }
183        ret = read(g_inputFile, static_cast<void*>(&dataSize), sizeof(dataSize));
184        if (ret <= 0) {
185            return;
186        }
187        ret = read(g_inputFile, static_cast<void*>(avMemBuffer->memory_->GetAddr()), dataSize);
188        if (ret <= 0) {
189            return;
190        }
191
192        avMemBuffer->memory_->SetSize(dataSize);
193        if (dataTrackId == DATA_AUDIO_ID) {
194            trackId = audioTrackIndex;
195        } else if (dataTrackId == DATA_VIDEO_ID) {
196            trackId = videoTrackIndex;
197        } else {
198            cout << "error dataTrackId : " << trackId << endl;
199        }
200        if (trackId >= 0) {
201            trackIndex = trackId;
202            int32_t result = muxerDemo->InnerWriteSample(trackIndex, avMemBuffer);
203            if (result != AVCS_ERR_OK) {
204                return;
205            }
206        }
207    } while (ret > 0)
208}
209
210int32_t AddAudioTrackByFd(AVMuxerDemo *muxerDemo, int32_t inputFile, int32_t &trackIndex)
211{
212    std::shared_ptr<Meta> audioParams = std::make_shared<Meta>();
213    int extraSize = 0;
214    read(inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
215    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
216        std::vector<uint8_t> buffer(extraSize);
217        read(inputFile, buffer.data(), extraSize);
218        audioParams->Set<Tag::MEDIA_CODEC_CONFIG>(buffer);
219    }
220    audioParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::AUDIO_MPEG);
221    audioParams->Set<Tag::AUDIO_CHANNEL_COUNT>(CHANNEL_COUNT);
222    audioParams->Set<Tag::AUDIO_SAMPLE_RATE>(SAMPLE_RATE_44100);
223
224    int32_t trackId = muxerDemo->InnerAddTrack(trackIndex, audioParams);
225    return trackId;
226}
227
228int32_t AddAudioTrackAACByFd(AVMuxerDemo *muxerDemo, int32_t inputFile, int32_t &trackIndex)
229{
230    std::shared_ptr<Meta> audioParams = std::make_shared<Meta>();
231    int extraSize = 0;
232    read(inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
233    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
234        std::vector<uint8_t> buffer(extraSize);
235        read(inputFile, buffer.data(), extraSize);
236        audioParams->Set<Tag::MEDIA_CODEC_CONFIG>(buffer);
237    }
238    audioParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::AUDIO_AAC);
239    audioParams->Set<Tag::AUDIO_CHANNEL_COUNT>(CHANNEL_COUNT);
240    audioParams->Set<Tag::AUDIO_SAMPLE_RATE>(SAMPLE_RATE_44100);
241
242    int32_t ret = muxerDemo->InnerAddTrack(trackIndex, audioParams);
243    return ret;
244}
245
246int32_t AddVideoTrackByFd(AVMuxerDemo *muxerDemo, int32_t inputFile, int32_t &trackIndex)
247{
248    std::shared_ptr<Meta> videoParams = std::make_shared<Meta>();
249    int extraSize = 0;
250    read(inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
251    if (extraSize <= BUFFER_SIZE && extraSize > 0) {
252        std::vector<uint8_t> buffer(extraSize);
253        read(inputFile, buffer.data(), extraSize);
254        videoParams->Set<Tag::MEDIA_CODEC_CONFIG>(buffer);
255    }
256    videoParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::VIDEO_MPEG4);
257    videoParams->Set<Tag::VIDEO_WIDTH>(SAMPLE_RATE_352);
258    videoParams->Set<Tag::VIDEO_HEIGHT>(SAMPLE_RATE_288);
259
260    int32_t trackId = muxerDemo->InnerAddTrack(trackIndex, videoParams);
261    return trackId;
262}
263
264int WriteTrackSampleByFdRead(int *inputFile, int64_t *pts, int *dataSize, int *dataTrackId)
265{
266    int ret = read(*inputFile, static_cast<void*>(dataTrackId), sizeof(*dataTrackId));
267    if (ret <= 0) {
268        cout << "read dataTrackId error, ret is: " << ret << endl;
269        return -1;
270    }
271    ret = read(*inputFile, static_cast<void*>(pts), sizeof(*pts));
272    if (ret <= 0) {
273        cout << "read info.presentationTimeUs error, ret is: " << ret << endl;
274        return -1;
275    }
276    ret = read(*inputFile, static_cast<void*>(dataSize), sizeof(*dataSize));
277    if (ret <= 0) {
278        cout << "read dataSize error, ret is: " << ret << endl;
279        return -1;
280    }
281    return 0;
282}
283
284int WriteTrackSampleByFdMem(int dataSize, std::shared_ptr<AVBuffer> &avMuxerDemoBuffer)
285{
286    if (avMuxerDemoBuffer != nullptr && dataSize > avMuxerDemoBuffer->memory_->GetCapacity()) {
287        avMuxerDemoBuffer = nullptr;
288    }
289    if (avMuxerDemoBuffer == nullptr) {
290        auto alloc = AVAllocatorFactory::CreateSharedAllocator(MemoryFlag::MEMORY_READ_ONLY);
291        avMuxerDemoBuffer = AVBuffer::CreateAVBuffer(alloc, dataSize);
292        if (avMuxerDemoBuffer == nullptr) {
293            printf("error malloc memory!\n");
294            return -1;
295        }
296    }
297    return 0;
298}
299
300int WriteTrackSampleByFdGetIndex(const int *dataTrackId, const int *audioTrackIndex,
301                                 int *videoTrackIndex)
302{
303    int trackId = 0;
304    if (*dataTrackId == DATA_AUDIO_ID) {
305        trackId = *audioTrackIndex;
306    } else if (*dataTrackId == DATA_VIDEO_ID) {
307        trackId = *videoTrackIndex;
308    } else {
309        cout << "error dataTrackId : " << *dataTrackId << endl;
310    }
311
312    return trackId;
313}
314
315void WriteTrackSampleByFd(AVMuxerDemo *muxerDemo, int audioTrackIndex, int videoTrackIndex, int32_t inputFile)
316{
317    int dataTrackId = 0;
318    int dataSize = 0;
319    int trackId = 0;
320    int64_t pts = 0;
321    uint32_t trackIndex;
322    std::shared_ptr<AVBuffer> avMuxerDemoBuffer = nullptr;
323    string resultStr = "";
324    do {
325        int ret = WriteTrackSampleByFdRead(&inputFile, &pts, &dataSize, &dataTrackId);
326        if (ret != 0) {
327            return;
328        }
329
330        ret = WriteTrackSampleByFdMem(dataSize, avMuxerDemoBuffer);
331        if (ret != 0) {
332            break;
333        }
334
335        resultStr =
336            "inputFile is: " + to_string(inputFile) + ", avMuxerDemoBufferSize is " + to_string(dataSize);
337        cout << resultStr << endl;
338
339        ret = read(inputFile, static_cast<void*>(avMuxerDemoBuffer->memory_->GetAddr()), dataSize);
340        if (ret <= 0) {
341            cout << "read data error, ret is: " << ret << endl;
342            continue;
343        }
344        avMuxerDemoBuffer->pts_ = pts;
345        avMuxerDemoBuffer->memory_->SetSize(dataSize);
346        trackId = WriteTrackSampleByFdGetIndex(&dataTrackId, &audioTrackIndex, &videoTrackIndex);
347        if (trackId >= 0) {
348            trackIndex = trackId;
349            int32_t result = muxerDemo->InnerWriteSample(trackIndex, avMuxerDemoBuffer);
350            if (result != AVCS_ERR_OK) {
351                cout << "InnerWriteSample error! ret is: " << result << endl;
352                break;
353            }
354        }
355    } while (ret >0)
356}
357
358void RunMuxer(string testcaseName, int threadId, Plugins::OutputFormat format)
359{
360    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
361    time_t startTime = time(nullptr);
362    ASSERT_NE(startTime, -1);
363    time_t curTime = startTime;
364
365    while (difftime(curTime, startTime) < RUN_TIME) {
366        cout << "thread id is: " << threadId << ", run time : " << difftime(curTime, startTime) << " seconds" << endl;
367        string fileName = testcaseName + "_" + to_string(threadId);
368
369        cout << "thread id is: " << threadId << ", cur file name is: " << fileName << endl;
370        int32_t fd = muxerDemo->InnerGetFdByName(format, fileName);
371
372        int32_t inputFile;
373        int32_t audioTrackId;
374        int32_t videoTrackId;
375
376        cout << "thread id is: " << threadId << ", fd is: " << fd << endl;
377        muxerDemo->InnerCreate(fd, format);
378
379        int32_t ret;
380
381        if (format == Plugins::OutputFormat::MPEG_4) {
382            cout << "thread id is: " << threadId << ", format is: " << static_cast<int32_t>(format) << endl;
383            inputFile = open("avDataMpegMpeg4.bin", O_RDONLY);
384            AddAudioTrackByFd(muxerDemo, inputFile, audioTrackId);
385            AddVideoTrackByFd(muxerDemo, inputFile, videoTrackId);
386        } else {
387            cout << "thread id is: " << threadId << ", format is: " << static_cast<int32_t>(format) << endl;
388            inputFile = open("avData_mpeg4_aac_2.bin", O_RDONLY);
389            AddAudioTrackAACByFd(muxerDemo, inputFile, audioTrackId);
390            videoTrackId = -1;
391            int extraSize = 0;
392            unsigned char buffer[100] = {0};
393            read(inputFile, static_cast<void*>(&extraSize), sizeof(extraSize));
394            if (extraSize <= BUFFER_SIZE && extraSize > 0) {
395                read(inputFile, buffer, extraSize);
396            }
397        }
398
399        cout << "thread id is: " << threadId << ", audio track id is: " << audioTrackId
400             << ", video track id is: " << videoTrackId << endl;
401
402        ret = muxerDemo->InnerStart();
403        cout << "thread id is: " << threadId << ", Start ret is:" << ret << endl;
404
405        WriteTrackSampleByFd(muxerDemo, audioTrackId, videoTrackId, inputFile);
406
407        ret = muxerDemo->InnerStop();
408        cout << "thread id is: " << threadId << ", Stop ret is:" << ret << endl;
409
410        ret = muxerDemo->InnerDestroy();
411        cout << "thread id is: " << threadId << ", Destroy ret is:" << ret << endl;
412
413        close(inputFile);
414        close(fd);
415        curTime = time(nullptr);
416        ASSERT_NE(curTime, -1);
417    }
418    g_testResult[threadId] = AVCS_ERR_OK;
419    delete muxerDemo;
420}
421} // namespace
422
423/**
424 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_001
425 * @tc.name      : Create(1000 times)
426 * @tc.desc      : Stability test
427 */
428HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_001, TestSize.Level2)
429{
430    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
431
432    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
433    int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_001");
434
435    g_inputFile = open("avData_mpeg4_aac_2.bin", O_RDONLY);
436    struct timeval start, end;
437    double totalTime = 0;
438    for (int i = 0; i < RUN_TIMES; i++) {
439        gettimeofday(&start, nullptr);
440        muxerDemo->InnerCreate(fd, format);
441        gettimeofday(&end, nullptr);
442        totalTime += (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
443        cout << "run time is: " << i << endl;
444        int32_t ret = muxerDemo->InnerDestroy();
445        ASSERT_EQ(AVCS_ERR_OK, ret);
446    }
447    cout << "1000 times finish, run time is " << totalTime << endl;
448    close(fd);
449    delete muxerDemo;
450}
451
452/**
453 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_002
454 * @tc.name      : SetRotation(1000 times)
455 * @tc.desc      : Stability test
456 */
457HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_002, TestSize.Level2)
458{
459    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
460
461    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
462    int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_002");
463
464    muxerDemo->InnerCreate(fd, format);
465    double totalTime = 0;
466    struct timeval start, end;
467
468    for (int i = 0; i < RUN_TIMES; i++) {
469        gettimeofday(&start, nullptr);
470        int32_t ret = SetRotation(muxerDemo);
471        gettimeofday(&end, nullptr);
472        ASSERT_EQ(AVCS_ERR_OK, ret);
473        totalTime += (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
474        cout << "run time is: " << i << ", ret is:" << ret << endl;
475    }
476    cout << "1000 times finish, run time is " << totalTime << endl;
477    muxerDemo->InnerDestroy();
478
479    close(fd);
480    delete muxerDemo;
481}
482
483/**
484 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_003
485 * @tc.name      : AddTrack(1000 times)
486 * @tc.desc      : Stability test
487 */
488HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_003, TestSize.Level2)
489{
490    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
491
492    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
493    int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_003");
494
495    muxerDemo->InnerCreate(fd, format);
496
497    double totalTime = 0;
498    struct timeval start, end;
499    for (int i = 0; i < RUN_TIMES; i++) {
500        int32_t trackId = -1;
501        gettimeofday(&start, nullptr);
502        AddTrack(muxerDemo, trackId);
503        gettimeofday(&end, nullptr);
504        ASSERT_EQ(-1, trackId);
505        cout << "run time is: " << i << ", track id is:" << trackId << endl;
506    }
507    cout << "1000 times finish, run time is " << totalTime << endl;
508    muxerDemo->InnerDestroy();
509
510    close(fd);
511    delete muxerDemo;
512}
513
514/**
515 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_004
516 * @tc.name      : Start(1000 times)
517 * @tc.desc      : Stability test
518 */
519HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_004, TestSize.Level2)
520{
521    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
522
523    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
524    int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_004");
525
526    muxerDemo->InnerCreate(fd, format);
527    int32_t audioTrackId;
528    int32_t trackId = AddTrack(muxerDemo, audioTrackId);
529    ASSERT_EQ(0, trackId);
530
531    double totalTime = 0;
532    struct timeval start, end;
533    for (int i = 0; i < RUN_TIMES; i++) {
534        gettimeofday(&start, nullptr);
535        int32_t ret = muxerDemo->InnerStart();
536        gettimeofday(&end, nullptr);
537        ASSERT_EQ(AVCS_ERR_OK, ret);
538        cout << "run time is: " << i << ", ret is:" << ret << endl;
539    }
540    cout << "1000 times finish, run time is " << totalTime << endl;
541    muxerDemo->InnerDestroy();
542
543    close(fd);
544    delete muxerDemo;
545}
546
547/**
548 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_005
549 * @tc.name      : WriteSample(1000 times)
550 * @tc.desc      : Stability test
551 */
552HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_005, TestSize.Level2)
553{
554    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
555
556    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
557    int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_005");
558
559    muxerDemo->InnerCreate(fd, format);
560
561    int32_t audioTrackId;
562    int32_t trackId = AddTrack(muxerDemo, audioTrackId);
563    ASSERT_EQ(0, trackId);
564
565    int32_t ret = muxerDemo->InnerStart();
566    ASSERT_EQ(AVCS_ERR_OK, ret);
567
568    double totalTime = 0;
569    struct timeval start, end;
570    for (int i = 0; i < RUN_TIMES; i++) {
571        gettimeofday(&start, nullptr);
572        ret = WriteSample(muxerDemo);
573        gettimeofday(&end, nullptr);
574        cout << "run time is: " << i << ", ret is:" << ret << endl;
575    }
576    cout << "1000 times finish, run time is " << totalTime << endl;
577    muxerDemo->InnerDestroy();
578
579    close(fd);
580    delete muxerDemo;
581}
582
583/**
584 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_006
585 * @tc.name      : Stop(1000 times)
586 * @tc.desc      : Stability test
587 */
588HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_006, TestSize.Level2)
589{
590    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
591
592    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
593    int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_006");
594
595    muxerDemo->InnerCreate(fd, format);
596
597    int32_t audioTrackId;
598    int32_t trackId = AddTrack(muxerDemo, audioTrackId);
599    ASSERT_EQ(0, trackId);
600
601    int32_t ret = muxerDemo->InnerStart();
602    ASSERT_EQ(AVCS_ERR_OK, ret);
603
604    ret = WriteSample(muxerDemo);
605    ASSERT_EQ(AVCS_ERR_OK, ret);
606
607    double totalTime = 0;
608    struct timeval start, end;
609    for (int i = 0; i < RUN_TIMES; i++) {
610        gettimeofday(&start, nullptr);
611        ret = muxerDemo->InnerStop();
612        gettimeofday(&end, nullptr);
613        cout << "run time is: " << i << ", ret is:" << ret << endl;
614    }
615    cout << "1000 times finish, run time is " << totalTime << endl;
616    muxerDemo->InnerDestroy();
617
618    close(fd);
619    delete muxerDemo;
620}
621
622/**
623 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_007
624 * @tc.name      : Destroy(1000 times)
625 * @tc.desc      : Stability test
626 */
627HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_007, TestSize.Level2)
628{
629    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
630
631    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
632    int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_007");
633
634    double totalTime = 0;
635    struct timeval start, end;
636    for (int i = 0; i < RUN_TIMES; i++) {
637        muxerDemo->InnerCreate(fd, format);
638
639        gettimeofday(&start, nullptr);
640        int32_t ret = muxerDemo->InnerDestroy();
641        gettimeofday(&end, nullptr);
642        ASSERT_EQ(AVCS_ERR_OK, ret);
643        cout << "run time is: " << i << ", ret is:" << ret << endl;
644    }
645    cout << "1000 times finish, run time is " << totalTime << endl;
646    close(fd);
647    delete muxerDemo;
648}
649
650/**
651 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_008
652 * @tc.name      : m4a(long time)
653 * @tc.desc      : Function test
654 */
655HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_008, TestSize.Level2)
656{
657    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
658    time_t startTime = time(nullptr);
659    ASSERT_NE(startTime, -1);
660    time_t curTime = startTime;
661
662    while (difftime(curTime, startTime) < RUN_TIME) {
663        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
664        Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
665        int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_008");
666
667        g_inputFile = open("avData_mpeg4_aac_2.bin", O_RDONLY);
668
669        muxerDemo->InnerCreate(fd, format);
670
671        int32_t audioTrackId;
672        AddAudioTrackAAC(muxerDemo, audioTrackId);
673        int32_t videoTrackId = -1;
674        RemoveHeader();
675
676        cout << "audio track id is: " << audioTrackId << ", video track id is: " << videoTrackId << endl;
677
678        int32_t ret;
679
680        ret = muxerDemo->InnerStart();
681        cout << "Start ret is:" << ret << endl;
682
683        WriteTrackSample(muxerDemo, audioTrackId, videoTrackId);
684
685        ret = muxerDemo->InnerStop();
686        cout << "Stop ret is:" << ret << endl;
687
688        ret = muxerDemo->InnerDestroy();
689        cout << "Destroy ret is:" << ret << endl;
690
691        close(g_inputFile);
692        close(fd);
693        curTime = time(nullptr);
694        ASSERT_NE(curTime, -1);
695    }
696    delete muxerDemo;
697}
698
699/**
700 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_009
701 * @tc.name      : mp4(long time)
702 * @tc.desc      : Function test
703 */
704HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_009, TestSize.Level2)
705{
706    AVMuxerDemo *muxerDemo = new AVMuxerDemo();
707    time_t startTime = time(nullptr);
708    ASSERT_NE(startTime, -1);
709    time_t curTime = startTime;
710
711    while (difftime(curTime, startTime) < RUN_TIME) {
712        cout << "run time: " << difftime(curTime, startTime) << " seconds" << endl;
713
714        Plugins::OutputFormat format = Plugins::OutputFormat::MPEG_4;
715        int32_t fd = muxerDemo->InnerGetFdByName(format, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_009");
716
717        g_inputFile = open("avDataMpegMpeg4.bin", O_RDONLY);
718
719        muxerDemo->InnerCreate(fd, format);
720
721        int32_t audioTrackId;
722        AddAudioTrack(muxerDemo, audioTrackId);
723        int32_t videoTrackId;
724        AddVideoTrack(muxerDemo, videoTrackId);
725
726        cout << "audio track id is: " << audioTrackId << ", video track id is: " << videoTrackId << endl;
727
728        int32_t ret;
729
730        ret = muxerDemo->InnerStart();
731        cout << "Start ret is:" << ret << endl;
732
733        WriteTrackSample(muxerDemo, audioTrackId, videoTrackId);
734
735        ret = muxerDemo->InnerStop();
736        cout << "Stop ret is:" << ret << endl;
737
738        ret = muxerDemo->InnerDestroy();
739        cout << "Destroy ret is:" << ret << endl;
740
741        close(g_inputFile);
742        close(fd);
743        curTime = time(nullptr);
744        ASSERT_NE(curTime, -1);
745    }
746    delete muxerDemo;
747}
748
749/**
750 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_010
751 * @tc.name      : m4a(thread long time)
752 * @tc.desc      : Function test
753 */
754HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_010, TestSize.Level2)
755{
756    vector<thread> threadVec;
757    Plugins::OutputFormat format = Plugins::OutputFormat::M4A;
758    for (int i = 0; i < 10; i++) {
759        threadVec.push_back(thread(RunMuxer, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_010", i, format));
760    }
761    for (uint32_t i = 0; i < threadVec.size(); i++) {
762        threadVec[i].join();
763    }
764    for (int32_t i = 0; i < 10; i++)
765    {
766        ASSERT_EQ(AV_ERR_OK, g_testResult[i]);
767    }
768}
769
770/**
771 * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_011
772 * @tc.name      : mp4(thread long time)
773 * @tc.desc      : Function test
774 */
775HWTEST_F(InnerAVMuxerStablityTest, SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_011, TestSize.Level2)
776{
777    vector<thread> threadVec;
778    Plugins::OutputFormat format = Plugins::OutputFormat::MPEG_4;
779    for (int i = 0; i < 10; i++) {
780        threadVec.push_back(thread(RunMuxer, "SUB_MULTIMEDIA_MEDIA_MUXER_STABILITY_011", i, format));
781    }
782    for (uint32_t i = 0; i < threadVec.size(); i++) {
783        threadVec[i].join();
784    }
785    for (int32_t i = 0; i < 10; i++)
786    {
787        ASSERT_EQ(AV_ERR_OK, g_testResult[i]);
788    }
789}