1 /*
2  * Copyright (C) 2023 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 "avmuxer_demo_runner.h"
17 #include <iostream>
18 #include <thread>
19 #include "avmuxer_demo.h"
20 #include "avmuxer_ffmpeg_demo.h"
21 #include "avmuxer_engine_demo.h"
22 #include "native_avmuxer_demo.h"
23 
24 using namespace std;
25 using namespace OHOS::MediaAVCodec;
26 
27 constexpr int RUN_TIME = 60000;
28 constexpr int DEMO_THREAD_COUNT = 10;
29 
RunLoopNativeMuxer(const string & out)30 static int RunLoopNativeMuxer(const string &out)
31 {
32     time_t startTime = 0;
33     time_t curTime = 0;
34     (void)time(&startTime);
35     (void)time(&curTime);
36     while (difftime(curTime, startTime) < RUN_TIME) {
37         RunNativeMuxer(out.c_str());
38         (void)time(&curTime);
39     }
40     return 0;
41 }
42 
RunAVMuxer()43 static int RunAVMuxer()
44 {
45     auto avmuxer = std::make_unique<AVMuxerDemo>();
46     avmuxer->RunCase();
47     cout << "demo avmuxer end" << endl;
48     return 0;
49 }
50 
RunAVMuxerWithMultithread()51 static int RunAVMuxerWithMultithread()
52 {
53     auto avmuxer = std::make_unique<AVMuxerDemo>();
54     avmuxer->RunMultiThreadCase();
55     cout << "demo multi thread avmuxer end" << endl;
56     return 0;
57 }
58 
RunFfmpegMuxer()59 static int RunFfmpegMuxer()
60 {
61     std::unique_ptr<AVMuxerDemoBase> ffmpegMuxer = std::make_unique<AVMuxerFFmpegDemo>();
62     ffmpegMuxer->RunCase();
63     cout << "demo ffmpegMuxer end" << endl;
64     return 0;
65 }
66 
RunEngineMuxer()67 static int RunEngineMuxer()
68 {
69     std::unique_ptr<AVMuxerDemoBase> muxer = std::make_unique<AVMuxerEngineDemo>();
70     muxer->RunCase();
71     cout << "demo engine demo end" << endl;
72     return 0;
73 }
74 
RunLoopEngineMuxer()75 static int RunLoopEngineMuxer()
76 {
77     time_t startTime = 0;
78     time_t curTime = 0;
79     (void)time(&startTime);
80     (void)time(&curTime);
81     while (difftime(curTime, startTime) < RUN_TIME) {
82         RunEngineMuxer();
83         (void)time(&curTime);
84     }
85     return 0;
86 }
87 
AVMuxerDemoCase(void)88 void AVMuxerDemoCase(void)
89 {
90     cout << "Please select a muxer demo(default native muxer demo): " << endl;
91     cout << "0:native_muxer" << endl;
92     cout << "1:native_muxer loop" << endl;
93     cout << "2:native_muxer multithread" << endl;
94     cout << "3:inner_muxer" << endl;
95     cout << "4:inner_muxer with multithread write" << endl;
96     cout << "5:ffmpeg_muxer" << endl;
97     cout << "6:engine_muxer" << endl;
98     cout << "7:engine_muxer loop" << endl;
99 
100     string mode;
101     (void)getline(cin, mode);
102 
103     if (mode == "0" || mode == "") {
104         (void)NativeSelectMode();
105         (void)RunNativeMuxer("native_mux");
106     } else if (mode == "1") {
107         (void)NativeSelectMode();
108         (void)RunLoopNativeMuxer("loop_native_mux");
109     } else if (mode == "2") {
110         (void)NativeSelectMode();
111         vector<thread> vecThread;
112         for (int i = 0; i < DEMO_THREAD_COUNT; ++i) {
113             string out = to_string(i + 1);
114             out += "_native_mux";
115             vecThread.push_back(thread(RunLoopNativeMuxer, out));
116         }
117         for (int i = 0; i < DEMO_THREAD_COUNT; ++i) {
118             vecThread[i].join();
119         }
120     } else if (mode == "3") {
121         (void)RunAVMuxer();
122     } else if (mode == "4") {
123         (void)RunAVMuxerWithMultithread();
124     } else if (mode == "5") {
125         (void)RunFfmpegMuxer();
126     } else if (mode == "6") {
127         (void)RunEngineMuxer();
128     } else if (mode == "7") {
129         (void)RunLoopEngineMuxer();
130     }
131 }