1 /*
2 * Copyright (c) 2025 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 <cstddef>
17 #include <cstdint>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include "securec.h"
22
23 #include <iostream>
24
25 #include "media_demuxer.h"
26
27 #define FUZZ_PROJECT_NAME "mediademuxerptsfunctions_fuzzer"
28 using namespace std;
29 using namespace OHOS::Media;
30 namespace OHOS {
31 const char *MP4_PATH = "/data/test/fuzz_create.mp4";
32 const int64_t EXPECT_SIZE = 37;
33 const int64_t SELECT_TRACK = 4;
CheckDataValidity(const uint8_t * data,size_t size)34 bool CheckDataValidity(const uint8_t *data, size_t size)
35 {
36 if (size < EXPECT_SIZE) {
37 return false;
38 }
39 int32_t fd = open(MP4_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
40 if (fd < 0) {
41 return false;
42 }
43 int len = write(fd, data, size - 36);
44 if (len <= 0) {
45 close(fd);
46 return false;
47 }
48 close(fd);
49 return true;
50 }
51
MediaDemuxerPtsFunctionsFuzzTest(const uint8_t * data,size_t size)52 bool MediaDemuxerPtsFunctionsFuzzTest(const uint8_t *data, size_t size)
53 {
54 if (!CheckDataValidity(data, size)) {
55 return false;
56 }
57
58 std::shared_ptr<MediaDemuxer> mediaDemuxer =
59 std::make_shared<MediaDemuxer>();
60
61 std::shared_ptr<MediaSource> mediaSource =
62 std::make_shared<MediaSource>(MP4_PATH);
63 mediaDemuxer->SetDataSource(mediaSource);
64 int32_t trackId = size % SELECT_TRACK;
65
66 std::shared_ptr<Media::AVBufferQueue> implBufferQueue_ =
67 Media::AVBufferQueue::Create(size, Media::MemoryType::SHARED_MEMORY, "InnerDemo"); // 4
68 sptr<AVBufferQueueProducer> producer = implBufferQueue_->GetProducer();
69 mediaDemuxer->SetOutputBufferQueue(trackId, producer);
70
71 mediaDemuxer->Start();
72 mediaDemuxer->InitPtsInfo();
73 mediaDemuxer->InitMediaStartPts();
74 mediaDemuxer->TranscoderInitMediaStartPts();
75
76 mediaDemuxer->SetSubtitleSource(std::make_shared<MediaSource>(MP4_PATH));
77 mediaDemuxer->SetTranscoderMode();
78 std::shared_ptr<AVBuffer> sample = AVBuffer::CreateAVBuffer();
79 mediaDemuxer->HandleAutoMaintainPts(trackId, sample);
80 int ret = remove(MP4_PATH);
81 if (ret != 0) {
82 return false;
83 }
84 return true;
85 }
86 } // namespace OHOS
87
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
90 {
91 /* Run your code on data */
92 OHOS::MediaDemuxerPtsFunctionsFuzzTest(data, size);
93 return 0;
94 }