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 <ctime>
19 #include "gtest/gtest.h"
20 #include "AVMuxerDemo.h"
21 
22 using namespace std;
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::MediaAVCodec;
26 
27 
28 namespace {
29     class NativeAVMuxerFuzzTest : public testing::Test {
30     public:
31         static void SetUpTestCase();
32         static void TearDownTestCase();
33         void SetUp() override;
34         void TearDown() override;
35     };
36 
SetUpTestCase()37     void NativeAVMuxerFuzzTest::SetUpTestCase() {}
TearDownTestCase()38     void NativeAVMuxerFuzzTest::TearDownTestCase() {}
SetUp()39     void NativeAVMuxerFuzzTest::SetUp() {}
TearDown()40     void NativeAVMuxerFuzzTest::TearDown() {}
41 
42     constexpr int FUZZ_TEST_NUM = 1000000;
43     constexpr int AUDIO_PTS_ADD = 21;
44 
getIntRand()45     int32_t getIntRand()
46     {
47         int32_t data = -10000 + rand() % 20001;
48         return data;
49     }
50 }
51 
52 /**
53  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_001
54  * @tc.name      : OH_AVMuxer_Create
55  * @tc.desc      : Fuzz test
56  */
57 HWTEST_F(NativeAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_001, TestSize.Level2)
58 {
59     srand(time(nullptr) * 10);
60     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
61 
62     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
63     int32_t fd = -1;
64 
65     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
66 
67     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
68         cout << "current run time is: " << i << endl;
69         fd = rand();
70         format = OH_AVOutputFormat(rand() % 3);
71         handle = muxerDemo->NativeCreate(fd, format);
72         if (handle != nullptr) {
73             muxerDemo->NativeDestroy(handle);
74         }
75     }
76 
77     delete muxerDemo;
78 }
79 
80 
81 /**
82  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_002
83  * @tc.name      : OH_AVMuxer_SetRotation
84  * @tc.desc      : Fuzz test
85  */
86 HWTEST_F(NativeAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_002, TestSize.Level2)
87 {
88     srand(time(nullptr) * 10);
89     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
90 
91     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
92     int32_t fd = -1;
93     fd = muxerDemo->GetFdByMode(format);
94     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
95     ASSERT_NE(nullptr, handle);
96 
97     int32_t rotation;
98     OH_AVErrCode ret;
99 
100     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
101         cout << "current run time is: " << i << endl;
102         rotation = getIntRand();
103         cout << "rotation is: " << rotation << endl;
104         ret = muxerDemo->NativeSetRotation(handle, rotation);
105         cout << "ret code is: " << ret << endl;
106     }
107 
108     delete muxerDemo;
109 }
110 
111 
112 /**
113  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_003
114  * @tc.name      : OH_AVMuxer_AddTrack
115  * @tc.desc      : Fuzz test
116  */
117 HWTEST_F(NativeAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_003, TestSize.Level2)
118 {
119     srand(time(nullptr) * 10);
120     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
121 
122     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
123     int32_t fd = -1;
124     fd = muxerDemo->GetFdByMode(format);
125     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
126     ASSERT_NE(nullptr, handle);
127 
128     string mimeType[] = {"audio/mp4a-latm", "audio/mpeg", "video/avc", "video/mp4v-es"};
129     OH_AVFormat* trackFormat = OH_AVFormat_Create();
130     int32_t trackId;
131     OH_AVErrCode ret;
132 
133     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
134         cout << "current run time is: " << i << endl;
135         int typeIndex = rand() % 4;
136         int audioChannels = getIntRand();
137         int audioSampleRate = getIntRand();
138 
139         int videoWidth = getIntRand();
140         int videoHeight = getIntRand();
141 
142         // audio config
143         OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, mimeType[typeIndex].c_str());
144         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, audioChannels);
145         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, audioSampleRate);
146 
147         // video config
148         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_WIDTH, videoWidth);
149         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_HEIGHT, videoHeight);
150 
151         ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
152         cout << "trackId is: " << trackId << ", ret is: " << ret << endl;
153     }
154 
155     OH_AVFormat_Destroy(trackFormat);
156     muxerDemo->NativeDestroy(handle);
157     delete muxerDemo;
158 }
159 
160 
161 /**
162  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_004
163  * @tc.name      : WriteSampleBuffer
164  * @tc.desc      : Fuzz test
165  */
166 HWTEST_F(NativeAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_004, TestSize.Level2)
167 {
168     srand(time(nullptr) * 10);
169     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
170 
171     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_M4A;
172     int32_t fd = -1;
173     fd = muxerDemo->GetFdByMode(format);
174     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
175     ASSERT_NE(nullptr, handle);
176 
177     uint8_t a[100];
178     OH_AVFormat* trackFormat = OH_AVFormat_Create();
179     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
180     OH_AVFormat_SetLongValue(trackFormat, OH_MD_KEY_BITRATE, 320000);
181     OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, a, 100);
182     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, 1);
183     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, 48000);
184     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_PROFILE, 0);
185 
186     int32_t trackId;
187     OH_AVErrCode ret;
188 
189     ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
190     ASSERT_EQ(0, trackId);
191 
192     ret = muxerDemo->NativeStart(handle);
193     ASSERT_EQ(AV_ERR_OK, ret);
194 
195     OH_AVCodecBufferAttr info;
196     info.pts = 0;
197 
198     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
199         cout << "current run time is: " << i << endl;
200         int dataLen = rand() % 65536;
201         OH_AVMemory* avMemBuffer = OH_AVMemory_Create(dataLen);
202 
203         cout << "data len is:" << dataLen << endl;
204 
205         info.pts += AUDIO_PTS_ADD;
206         info.size = dataLen;
207         info.offset = getIntRand();
208         info.flags = getIntRand();
209 
210         cout << "info.pts is:" << info.pts << endl;
211         cout << "info.size is:" << info.size << endl;
212         cout << "info.offset is:" << info.offset << endl;
213         cout << "info.flags is:" << info.flags << endl;
214 
215         ret = muxerDemo->NativeWriteSampleBuffer(handle, trackId, avMemBuffer, info);
216         cout << "ret code is: " << ret << endl;
217         OH_AVMemory_Destroy(avMemBuffer);
218     }
219 
220     muxerDemo->NativeDestroy(handle);
221     delete muxerDemo;
222 }
223 
224 
225 /**
226  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_005
227  * @tc.name      : WriteSampleBuffer
228  * @tc.desc      : Fuzz test
229  */
230 HWTEST_F(NativeAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_005, TestSize.Level2)
231 {
232     srand(time(nullptr) * 10);
233     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
234 
235     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
236     int32_t fd = -1;
237     OH_AVMuxer* handle;
238 
239     string test_key = "";
240     string test_value = "";
241 
242     OH_AVFormat* trackFormat = OH_AVFormat_Create();
243     string mimeType[] = {
244         "audio/mp4a-latm", "audio/mpeg", "video/avc", "video/mp4v-es","image/jpeg", "image/png", "image/bmp"
245     };
246 
247     int32_t trackId;
248     OH_AVErrCode ret;
249 
250     OH_AVCodecBufferAttr info;
251     info.pts = 0;
252 
253     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
254         cout << "current run time is: " << i << endl;
255         // Create
256         fd = rand();
257         format = OH_AVOutputFormat(rand() % 3);
258         handle = muxerDemo->NativeCreate(fd, format);
259 
260         // SetRotation
261         float rotation = getIntRand();
262         ret = muxerDemo->NativeSetRotation(handle, rotation);
263 
264         // AddTrack
265         int typeIndex = rand() % 4;
266         int audioChannels = getIntRand();
267         int audioSampleRate = getIntRand();
268 
269         int videoWidth = getIntRand();
270         int videoHeight = getIntRand();
271 
272         // audio config
273         OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, mimeType[typeIndex].c_str());
274         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, audioChannels);
275         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, audioSampleRate);
276 
277         // video config
278         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_WIDTH, videoWidth);
279         OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_HEIGHT, videoHeight);
280 
281         ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
282         ret = muxerDemo->NativeStart(handle);
283 
284         int dataLen = rand() % 65536;
285         OH_AVMemory* avMemBuffer = OH_AVMemory_Create(dataLen);
286 
287         info.pts += AUDIO_PTS_ADD;
288         info.size = dataLen;
289         info.offset = getIntRand();
290         info.flags = getIntRand();
291 
292         ret = muxerDemo->NativeWriteSampleBuffer(handle, trackId, avMemBuffer, info);
293 
294         ret = muxerDemo->NativeStop(handle);
295 
296         ret = muxerDemo->NativeDestroy(handle);
297         cout << "Destroy ret is:" << ret << endl;
298         OH_AVMemory_Destroy(avMemBuffer);
299     }
300 
301     delete muxerDemo;
302 }