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 using namespace OHOS::MediaAVCodec::Plugin; 29 using namespace Ffmpeg; 30 31 namespace { 32 class FFmpegAVMuxerFuzzTest : public testing::Test { 33 public: 34 static void SetUpTestCase(); 35 static void TearDownTestCase(); 36 void SetUp() override; 37 void TearDown() override; 38 }; 39 SetUpTestCase()40 void FFmpegAVMuxerFuzzTest::SetUpTestCase() {} TearDownTestCase()41 void FFmpegAVMuxerFuzzTest::TearDownTestCase() {} SetUp()42 void FFmpegAVMuxerFuzzTest::SetUp() {} TearDown()43 void FFmpegAVMuxerFuzzTest::TearDown() {} 44 45 constexpr int FUZZ_TEST_NUM = 1000000; 46 getIntRand()47 int32_t getIntRand() 48 { 49 int32_t data = -10000 + rand() % 20001; 50 return data; 51 } 52 } 53 54 /** 55 * @tc.number : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_001 56 * @tc.name : FFmpegCreate 57 * @tc.desc : Fuzz test 58 */ 59 HWTEST_F(FFmpegAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_001, TestSize.Level2) 60 { 61 srand(time(nullptr) * 10); 62 AVMuxerDemo* muxerDemo = new AVMuxerDemo(); 63 64 int32_t fd = -1; 65 66 for (int i = 0; i < FUZZ_TEST_NUM; i++) { 67 cout << "current run time is: " << i << endl; 68 fd = rand(); 69 70 muxerDemo->FFmpegCreate(fd); 71 Status ret = muxerDemo->FFmpegDestroy(); 72 ASSERT_NE(Status::ERROR_INVALID_DATA, ret); 73 } 74 75 delete muxerDemo; 76 } 77 78 /** 79 * @tc.number : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_002 80 * @tc.name : FFmpegSetRotation 81 * @tc.desc : Fuzz test 82 */ 83 HWTEST_F(FFmpegAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_002, TestSize.Level2) 84 { 85 srand(time(nullptr) * 10); 86 AVMuxerDemo* muxerDemo = new AVMuxerDemo(); 87 88 OutputFormat format = OUTPUT_FORMAT_MPEG_4; 89 int32_t fd = -1; 90 fd = muxerDemo->FFmpeggetFdByMode(format); 91 muxerDemo->FFmpegCreate(fd); 92 93 int32_t rotation; 94 Status ret; 95 96 for (int i = 0; i < FUZZ_TEST_NUM; i++) { 97 cout << "current run time is: " << i << endl; 98 rotation = getIntRand(); 99 cout << "rotation is: " << rotation << endl; 100 ret = muxerDemo->FFmpegSetRotation(rotation); 101 cout << "ret code is: " << (int)ret << endl; 102 } 103 delete muxerDemo; 104 } 105 106 /** 107 * @tc.number : SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_003 108 * @tc.name : FFmpegAddTrack 109 * @tc.desc : Fuzz test 110 */ 111 HWTEST_F(FFmpegAVMuxerFuzzTest, SUB_MULTIMEDIA_MEDIA_MUXER_FUZZ_003, TestSize.Level2) 112 { 113 srand(time(nullptr) * 10); 114 AVMuxerDemo* muxerDemo = new AVMuxerDemo(); 115 116 OutputFormat format = OUTPUT_FORMAT_MPEG_4; 117 int32_t fd = -1; 118 fd = muxerDemo->FFmpeggetFdByMode(format); 119 muxerDemo->FFmpegCreate(fd); 120 121 string mimeType[] = {"audio/mp4a-latm", "audio/mpeg", "video/avc", "video/mp4v-es"}; 122 MediaDescription mediaParams; 123 124 for (int i = 0; i < FUZZ_TEST_NUM; i++) { 125 cout << "current run time is: " << i << endl; 126 int typeIndex = rand() % 4; 127 int bitRate = getIntRand(); 128 int dataLen = rand() % 65536; 129 uint8_t data[dataLen]; 130 int audioSampleFormat = getIntRand(); 131 int audioChannels = getIntRand(); 132 int audioSampleRate = getIntRand(); 133 int videoWidth = getIntRand(); 134 int videoHeight = getIntRand(); 135 int videoFrameRate = getIntRand(); 136 137 cout << "MediaDescriptionKey::MD_KEY_CODEC_MIME is: " << mimeType[typeIndex] << endl; 138 cout << "OH_AV_KEY_BIT_RATE is: " << bitRate << endl; 139 cout << "MediaDescriptionKey::MD_KEY_CHANNEL_COUNT len is: " << dataLen << endl; 140 cout << "OH_AV_KEY_AUDIO_SAMPLE_FORMAT is: " << audioSampleFormat << endl; 141 cout << "OH_AV_KEY_AUDIO_CHANNELS len is: " << audioChannels << endl; 142 cout << "OH_AV_KEY_VIDEO_HEIGHT is: " << videoHeight << endl; 143 cout << "OH_AV_KEY_VIDEO_FRAME_RATE len is: " << videoFrameRate << endl; 144 145 mediaParams.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, mimeType[typeIndex].c_str()); 146 mediaParams.PutLongValue(MediaDescriptionKey::MD_KEY_BITRATE, bitRate); 147 mediaParams.PutBuffer(MediaDescriptionKey::MD_KEY_CODEC_CONFIG, data, dataLen); 148 mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, audioChannels); 149 mediaParams.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, audioSampleRate); 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 trackId = 0; 156 muxerDemo->FFmpegAddTrack(trackId, mediaParams); 157 cout << "trackId is: " << trackId << endl; 158 } 159 160 muxerDemo->FFmpegDestroy(); 161 delete muxerDemo; 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(FFmpegAVMuxerFuzzTest, 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->FFmpeggetFdByMode(format); 177 muxerDemo->FFmpegCreate(fd); 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 Status ret; 188 int trackIndex = 0; 189 190 ret = muxerDemo->FFmpegAddTrack(trackIndex, mediaParams); 191 ASSERT_EQ(Status::OK, ret); 192 193 ret = muxerDemo->FFmpegStart(); 194 ASSERT_EQ(Status::OK, ret); 195 196 AVCodecBufferInfo info; 197 AVCodecBufferFlag flag = AVCODEC_BUFFER_FLAG_NONE; 198 info.presentationTimeUs = 0; 199 200 for (int i = 0; i < FUZZ_TEST_NUM; i++) { 201 cout << "current run time is: " << i << endl; 202 int dataLen = rand() % 65536; 203 uint8_t data[dataLen]; 204 cout << "data len is:" << dataLen << endl; 205 206 info.presentationTimeUs += 21; 207 info.size = dataLen; 208 info.offset = getIntRand(); 209 210 cout << "info.presentationTimeUs is:" << info.presentationTimeUs << endl; 211 cout << "info.size is:" << info.size << endl; 212 cout << "info.offset is:" << info.offset << endl; 213 cout << "flag is:" << flag << endl; 214 cout << "info.trackIndex is:" << trackIndex << endl; 215 216 217 ret = muxerDemo->FFmpegWriteSample(trackIndex, data, info, flag); 218 cout << "ret code is: " << (int)ret << endl; 219 } 220 221 muxerDemo->FFmpegDestroy(); 222 delete muxerDemo; 223 } 224 225