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 #include <cstddef>
16 #include <cstdint>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <iostream>
21 #include <fstream>
22 #include "demuxerinnerfunc_sample.h"
23
24 using namespace OHOS;
25 using namespace OHOS::Media;
26 using namespace OHOS::MediaAVCodec;
27 using namespace std;
28 const char *FILE_PATH = "/data/test/fuzz_create.mp4";
~DemuxerInnerFuncSample()29 DemuxerInnerFuncSample::~DemuxerInnerFuncSample()
30 {
31 if (fd_ > 0) {
32 close(fd_);
33 }
34 fd_ = -1;
35
36 if (source_ != nullptr) {
37 source_ = nullptr;
38 }
39 if (demuxer_ != nullptr) {
40 demuxer_ = nullptr;
41 }
42 }
43
Init(const uint8_t * data,size_t size)44 bool DemuxerInnerFuncSample::Init(const uint8_t *data, size_t size)
45 {
46 std::ofstream file(FILE_PATH, std::ios::binary);
47 if (file.is_open()) {
48 file.write(reinterpret_cast<const char*>(data), size);
49 file.close();
50 }
51 int32_t fd = open(FILE_PATH, O_RDONLY);
52 if (fd < 0) {
53 return false;
54 }
55
56 source_ = AVSourceFactory::CreateWithFD(fd, 0, size);
57 if (source_ == nullptr) {
58 return false;
59 }
60 demuxer_ = AVDemuxerFactory::CreateWithSource(source_);
61 if (demuxer_ == nullptr) {
62 return false;
63 }
64 int32_t ret = demuxer_->SelectTrackByID(0);
65 if (ret != AV_ERR_OK) {
66 return false;
67 }
68 return true;
69 }
70
GetCurrentCacheSize(uint32_t trackIndex)71 void DemuxerInnerFuncSample::GetCurrentCacheSize(uint32_t trackIndex)
72 {
73 uint32_t cacheSize = 0;
74 demuxer_->GetCurrentCacheSize(trackIndex, cacheSize);
75 }
76