1 /*
2 * Copyright (C) 2023 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 <memory>
17 #include <vector>
18 #include <map>
19 #include <sys/stat.h>
20 #include <string>
21 #include "avsource_demo.h"
22
23 namespace OHOS {
24 namespace MediaAVCodec {
AVSourceDemo()25 AVSourceDemo::AVSourceDemo()
26 {
27 printf("AVSourceDemo construtor\n");
28 }
29
~AVSourceDemo()30 AVSourceDemo::~AVSourceDemo()
31 {
32 printf("AVSourceDemo deconstrutor\n");
33 }
34
CreateWithURI(char * uri)35 int32_t AVSourceDemo::CreateWithURI(char* uri)
36 {
37 this->avsource_ = OH_AVSource_CreateWithURI(uri);
38 if (!avsource_) {
39 printf("OH_AVSource_CreateWithURI is failed\n");
40 return -1;
41 }
42 return 0;
43 }
44
GetFileSize(const std::string & fileName)45 size_t AVSourceDemo::GetFileSize(const std::string& fileName)
46 {
47 size_t fileSize = 0;
48 if (!fileName.empty()) {
49 struct stat fileStatus {};
50 if (stat(fileName.c_str(), &fileStatus) == 0) {
51 fileSize = static_cast<size_t>(fileStatus.st_size);
52 }
53 }
54 return fileSize;
55 }
56
CreateWithFD(int32_t fd,int64_t offset,int64_t size)57 int32_t AVSourceDemo::CreateWithFD(int32_t fd, int64_t offset, int64_t size)
58 {
59 this->avsource_ = OH_AVSource_CreateWithFD(fd, offset, size);
60 if (!avsource_) {
61 printf("OH_AVSource_CreateWithFD is failed\n");
62 return -1;
63 }
64 return 0;
65 }
66
CreateWithDataSource(OH_AVDataSource * dataSource)67 int32_t AVSourceDemo::CreateWithDataSource(OH_AVDataSource *dataSource)
68 {
69 this->avsource_ = OH_AVSource_CreateWithDataSource(dataSource);
70 if (!avsource_) {
71 printf("CreateWithDataSource is failed\n");
72 return -1;
73 }
74 return 0;
75 }
76
GetAVSource()77 OH_AVSource* AVSourceDemo::GetAVSource()
78 {
79 return this->avsource_;
80 }
81
Destroy()82 int32_t AVSourceDemo::Destroy()
83 {
84 int32_t ret = static_cast<int32_t>(OH_AVSource_Destroy(this->avsource_));
85 if (ret != 0) {
86 printf("destroy failed\n");
87 return ret;
88 }
89 return ret;
90 }
91
GetSourceFormat()92 OH_AVFormat* AVSourceDemo::GetSourceFormat()
93 {
94 this->avformat_ = OH_AVSource_GetSourceFormat(this->avsource_);
95 if (!this->avformat_) {
96 printf("OH_AVSource_GetSourceFormat is failed\n");
97 return nullptr;
98 }
99 return this->avformat_;
100 }
101
GetTrackFormat(uint32_t trackIndex)102 OH_AVFormat* AVSourceDemo::GetTrackFormat(uint32_t trackIndex)
103 {
104 this->trackFormat_ = OH_AVSource_GetTrackFormat(this->avsource_, trackIndex);
105 if (!this->trackFormat_) {
106 printf("OH_AVSourceTrack_GetTrackFormat is failed\n");
107 return nullptr;
108 }
109 return this->trackFormat_;
110 }
111 } // namespace MediaAVCodec
112 } // namespace OHOS
113