• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "avcodec_info.h"
22 #include "avcodec_errors.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::MediaAVCodec;
28 
29 
30 namespace {
31     class InnerAVMuxerFuzzTest : public testing::Test {
32     public:
33         static void SetUpTestCase();
34         static void TearDownTestCase();
35         void SetUp() override;
36         void TearDown() override;
37     };
SetUpTestCase()38     void InnerAVMuxerFuzzTest::SetUpTestCase() {}
TearDownTestCase()39     void InnerAVMuxerFuzzTest::TearDownTestCase() {}
SetUp()40     void InnerAVMuxerFuzzTest::SetUp() {}
TearDown()41     void InnerAVMuxerFuzzTest::TearDown() {}
42 
43     constexpr int FUZZ_TEST_NUM = 1000000;
getIntRand()44     int32_t getIntRand()
45     {
46         int32_t data = -10000 + rand() % 20001;
47         return data;
48     }
49 }
50 
51 /**
52  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_001
53  * @tc.name      : Create
54  * @tc.desc      : Fuzz test
55  */
56 HWTEST_F(InnerAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_001, TestSize.Level2)
57 {
58     srand(time(nullptr) * 10);
59     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
60     OutputFormat format = OUTPUT_FORMAT_MPEG_4;
61     int32_t fd = -1;
62 
63     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
64         cout << "current run time is: " << i << endl;
65         fd = rand();
66 
67         muxerDemo->InnerCreate(fd, format);
68         muxerDemo->InnerDestroy();
69     }
70 
71     delete muxerDemo;
72 }
73 
74 /**
75  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_002
76  * @tc.name      : SetRotation
77  * @tc.desc      : Fuzz test
78  */
79 HWTEST_F(InnerAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_002, TestSize.Level2)
80 {
81     srand(time(nullptr) * 10);
82     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
83 
84     OutputFormat format = OUTPUT_FORMAT_MPEG_4;
85     int32_t fd = -1;
86     fd = muxerDemo->InnergetFdByMode(format);
87     muxerDemo->InnerCreate(fd, format);
88 
89 
90     int32_t rotation;
91     int32_t ret;
92 
93     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
94         cout << "current run time is: " << i << endl;
95         rotation = getIntRand();
96         cout << "rotation is: " << rotation << endl;
97         ret = muxerDemo->InnerSetRotation(rotation);
98         cout << "ret code is: " << ret << endl;
99     }
100 
101     delete muxerDemo;
102 }
103 
104 /**
105  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_003
106  * @tc.name      : AddTrack
107  * @tc.desc      : Fuzz test
108  */
109 HWTEST_F(InnerAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_003, TestSize.Level2)
110 {
111     srand(time(nullptr) * 10);
112     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
113 
114     OutputFormat format = OUTPUT_FORMAT_MPEG_4;
115     int32_t fd = -1;
116     fd = muxerDemo->InnergetFdByMode(format);
117     muxerDemo->InnerCreate(fd, format);
118 
119 
120     string mimeType[] = {"audio/mp4a-latm", "audio/mpeg", "video/avc", "video/mp4v-es"};
121     MediaDescription mediaParams;
122 
123     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
124         cout << "current run time is: " << i << endl;
125         int typeIndex = rand() % 4;
126         int bitRate = getIntRand();
127         int dataLen = rand() % 65536;
128         uint8_t data[dataLen];
129         int audioSampleFormat = getIntRand();
130         int audioChannels = getIntRand();
131         int audioSampleRate = getIntRand();
132 
133         int videoWidth = getIntRand();
134         int videoHeight = getIntRand();
135         int videoFrameRate = getIntRand();
136 
137         cout << "OH_AV_KEY_MIME is: " << mimeType[typeIndex] << endl;
138         cout << "OH_AV_KEY_BIT_RATE is: " << bitRate << ", OH_AV_KEY_CODEC_CONFIG len is: " << dataLen << endl;
139         cout << "OH_AV_KEY_AUDIO_SAMPLE_FORMAT is: " << audioSampleFormat <<
140         ", OH_AV_KEY_AUDIO_CHANNELS len is: " << audioChannels << endl;
141         cout << "OH_AV_KEY_VIDEO_HEIGHT is: " << videoHeight <<
142         ", OH_AV_KEY_VIDEO_FRAME_RATE len is: " << videoFrameRate << endl;
143 
144         mediaParams.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, mimeType[typeIndex].c_str());
145         mediaParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, bitRate);
146         mediaParams.PutBuffer(MediaDescriptionKey::MD_KEY_CODEC_CONFIG, data, dataLen);
147         mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, audioChannels);
148         mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, audioSampleRate);
149 
150         // video config
151         mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, videoWidth);
152         mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, videoHeight);
153         mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, videoFrameRate);
154 
155         int trackIndex = 0;
156         muxerDemo->InnerAddTrack(trackIndex, mediaParams);
157     }
158 
159     muxerDemo->InnerDestroy();
160     delete muxerDemo;
161 }
162 
163 
164 /**
165  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_004
166  * @tc.name      : WriteSampleBuffer
167  * @tc.desc      : Fuzz test
168  */
169 HWTEST_F(InnerAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_004, TestSize.Level2)
170 {
171     srand(time(nullptr) * 10);
172     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
173 
174     OutputFormat format = OUTPUT_FORMAT_M4A;
175     int32_t fd = -1;
176     fd = muxerDemo->InnergetFdByMode(format);
177     muxerDemo->InnerCreate(fd, format);
178 
179     uint8_t a[100];
180     MediaDescription mediaParams;
181     mediaParams.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, CodecMimeType::AUDIO_AAC);
182     mediaParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, 320000);
183     mediaParams.PutBuffer(MediaDescriptionKey::MD_KEY_CODEC_CONFIG, a, 100);
184     mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, 1);
185     mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, 48000);
186 
187     int32_t trackId;
188     int32_t ret;
189     int trackIndex = 0;
190 
191     trackId = muxerDemo->InnerAddTrack(trackIndex, mediaParams);
192 
193 
194     ret = muxerDemo->InnerStart();
195     ASSERT_EQ(AVCS_ERR_OK, ret);
196 
197     AVCodecBufferInfo info;
198     AVCodecBufferFlag flag = AVCODEC_BUFFER_FLAG_NONE;
199     info.presentationTimeUs = 0;
200 
201     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
202         cout << "current run time is: " << i << endl;
203         int dataLen = rand() % 65536;
204         uint8_t data[dataLen];
205         cout << "data len is:" << dataLen << endl;
206 
207         info.presentationTimeUs += 21;
208         info.size = dataLen;
209         info.offset = getIntRand();
210         trackIndex = trackId;
211 
212         cout << "info.presentationTimeUs is:" << info.presentationTimeUs << endl;
213         cout << "info.size is:" << info.size << endl;
214         cout << "info.offset is:" << info.offset << endl;
215         cout << "flag is:" << flag << endl;
216         cout << "trackIndex is:" << trackIndex << endl;
217         std::shared_ptr<AVSharedMemoryBase> avMemBuffer = std::make_shared<AVSharedMemoryBase>
218         (info.size, AVSharedMemory::FLAGS_READ_ONLY, "sampleData");
219         avMemBuffer->Init();
220         (void)memcpy_s(avMemBuffer->GetBase(), avMemBuffer->GetSize(), data, info.size);
221         ret = muxerDemo->InnerWriteSample(trackIndex, avMemBuffer, info, flag);
222         cout << "ret code is: " << ret << endl;
223     }
224 
225     muxerDemo->InnerDestroy();
226     delete muxerDemo;
227 }
228 
HwTest_AddTrack(MediaDescription * mediaParams,AVCodecBufferInfo * info,AVMuxerDemo * muxerDemo)229 static int HwTest_AddTrack(MediaDescription *mediaParams, AVCodecBufferInfo *info, AVMuxerDemo *muxerDemo)
230 {
231     string mimeType[] = { "audio/mp4a-latm", "audio/mpeg", "video/avc", "video/mp4v-es" };
232     // AddTrack
233     int typeIndex = rand() % 4;
234     int bitRate = getIntRand();
235     int configLen = rand() % 65536;
236     uint8_t config[configLen];
237     int audioSampleFormat = getIntRand();
238     int audioChannels = getIntRand();
239     int audioSampleRate = getIntRand();
240 
241     int videoWidth = getIntRand();
242     int videoHeight = getIntRand();
243     int videoFrameRate = getIntRand();
244 
245     cout << "OH_AV_KEY_MIME is: " << mimeType[typeIndex] << endl;
246     cout << "OH_AV_KEY_BIT_RATE is: " << bitRate << ", OH_AV_KEY_CODEC_CONFIG len is: " << configLen << endl;
247     cout << "OH_AV_KEY_AUDIO_SAMPLE_FORMAT is: " << audioSampleFormat
248         << ", OH_AV_KEY_AUDIO_CHANNELS len is: " << audioChannels << endl;
249     cout << "OH_AV_KEY_VIDEO_HEIGHT is: " << videoHeight <<
250         ", OH_AV_KEY_VIDEO_FRAME_RATE len is: " << videoFrameRate << endl;
251 
252     // audio config
253     mediaParams->PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, mimeType[typeIndex].c_str());
254     mediaParams->PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, bitRate);
255     mediaParams->PutBuffer(MediaDescriptionKey::MD_KEY_CODEC_CONFIG, config, configLen);
256     mediaParams->PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, audioChannels);
257     mediaParams->PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, audioSampleRate);
258 
259     // video config
260     mediaParams->PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, videoWidth);
261     mediaParams->PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, videoHeight);
262     mediaParams->PutIntValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, videoFrameRate);
263 
264     int trackIndex = 0;
265     int32_t trackId;
266     int32_t ret;
267     trackId = muxerDemo->InnerAddTrack(trackIndex, *mediaParams);
268     cout << "trackId is: " << trackId << endl;
269 
270     ret = muxerDemo->InnerStart();
271     cout << "Start ret is:" << ret << endl;
272 
273     int dataLen = rand() % 0x10000;
274 
275     constexpr int64_t PTS = 21;
276     info->presentationTimeUs += PTS;
277     info->size = dataLen;
278     trackIndex = trackId;
279 
280     cout << "info.presentationTimeUs is:" << info->presentationTimeUs << endl;
281     cout << "info.size is:" << info->size << endl;
282 
283     return trackIndex;
284 }
285 /**
286  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_005
287  * @tc.name      : WriteSample
288  * @tc.desc      : Fuzz test
289  */
290 HWTEST_F(InnerAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_005, TestSize.Level2)
291 {
292     srand(time(nullptr) * 10);
293     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
294 
295     OutputFormat format = OUTPUT_FORMAT_MPEG_4;
296     int32_t fd = -1;
297 
298 
299     string test_key = "";
300     string test_value = "";
301 
302     MediaDescription mediaParams;
303     string mimeType[] = { "audio/mp4a-latm", "audio/mpeg", "video/avc", "video/mp4v-es" };
304 
305     int32_t ret;
306     AVCodecBufferInfo info;
307     AVCodecBufferFlag flag = AVCODEC_BUFFER_FLAG_NONE;
308     info.presentationTimeUs = 0;
309 
310     for (int i = 0; i < FUZZ_TEST_NUM; i++) {
311         cout << "current run time is: " << i << endl;
312 
313         // Create
314         fd = rand();
315         format = OutputFormat(rand() % 3);
316         cout << "fd is: " << fd << ", format is: " << format << endl;
317         muxerDemo->InnerCreate(fd, format);
318         cout << "Create ret code is: " << ret << endl;
319 
320         // SetRotation
321         float rotation = getIntRand();
322         cout << "rotation is: " << rotation << endl;
323         ret = muxerDemo->InnerSetRotation(rotation);
324         cout << "SetRotation ret code is: " << ret << endl;
325 
326         // AddTrack
327         int trackIndex = HwTest_AddTrack(&mediaParams, &info, muxerDemo);
328         cout << "flag is:" << flag << endl;
329 
330         std::shared_ptr<AVSharedMemoryBase> avMemBuffer =
331          std::make_shared<AVSharedMemoryBase>(info.size, AVSharedMemory::FLAGS_READ_ONLY, "sampleData");
332 
333         avMemBuffer->Init();
334 
335         ret = muxerDemo->InnerWriteSample(trackIndex, avMemBuffer, info, flag);
336         cout << "WriteSample ret code is: " << ret << endl;
337 
338         ret = muxerDemo->InnerStop();
339         cout << "Stop ret is:" << ret << endl;
340 
341         ret = muxerDemo->InnerDestroy();
342         cout << "Destroy ret is:" << ret << endl;
343     }
344 
345     delete muxerDemo;
346 }