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 "avformat_capi_mock.h"
17 #include "native_avformat.h"
18 
19 namespace OHOS {
20 namespace MediaAVCodec {
AVFormatCapiMock()21 AVFormatCapiMock::AVFormatCapiMock() : format_(nullptr) {}
22 
~AVFormatCapiMock()23 AVFormatCapiMock::~AVFormatCapiMock()
24 {
25     format_ = nullptr;
26 }
27 
PutIntValue(const std::string_view & key,int32_t value)28 bool AVFormatCapiMock::PutIntValue(const std::string_view &key, int32_t value)
29 {
30     if (format_ != nullptr) {
31         return OH_AVFormat_SetIntValue(format_, key.data(), value);
32     }
33     return false;
34 }
35 
GetIntValue(const std::string_view & key,int32_t & value)36 bool AVFormatCapiMock::GetIntValue(const std::string_view &key, int32_t &value)
37 {
38     if (format_ != nullptr) {
39         return OH_AVFormat_GetIntValue(format_, key.data(), &value);
40     }
41     return false;
42 }
43 
PutStringValue(const std::string_view & key,const std::string_view & value)44 bool AVFormatCapiMock::PutStringValue(const std::string_view &key, const std::string_view &value)
45 {
46     if (format_ != nullptr) {
47         return OH_AVFormat_SetStringValue(format_, key.data(), std::string(value).c_str());
48     }
49     return false;
50 }
51 
GetStringValue(const std::string_view & key,std::string & value)52 bool AVFormatCapiMock::GetStringValue(const std::string_view &key, std::string &value)
53 {
54     if (format_ != nullptr) {
55         const char *out = nullptr;
56         if (OH_AVFormat_GetStringValue(format_, key.data(), &out)) {
57             value = out;
58             return true;
59         }
60     }
61     return false;
62 }
63 
Destroy()64 void AVFormatCapiMock::Destroy()
65 {
66     if (format_ != nullptr) {
67         OH_AVFormat_Destroy(format_);
68         format_ = nullptr;
69     }
70 }
71 
GetFormat()72 OH_AVFormat *AVFormatCapiMock::GetFormat()
73 {
74     return format_;
75 }
76 
InitTrackFormat()77 void AVFormatCapiMock::InitTrackFormat()
78 {
79     Destroy();
80     format_ = OH_AVFormat_Create();
81 }
82 
InitAudioTrackFormat(const std::string_view & mimeType,int32_t sampleRate,int32_t channelCount)83 void AVFormatCapiMock::InitAudioTrackFormat(const std::string_view &mimeType, int32_t sampleRate, int32_t channelCount)
84 {
85     Destroy();
86     format_ = OH_AVFormat_CreateAudioFormat(mimeType.data(), sampleRate, channelCount);
87 }
88 
InitVideoTrackFormat(const std::string_view & mimeType,int32_t width,int32_t height)89 void AVFormatCapiMock::InitVideoTrackFormat(const std::string_view &mimeType, int32_t width, int32_t height)
90 {
91     Destroy();
92     format_ = OH_AVFormat_CreateVideoFormat(mimeType.data(), width, height);
93 }
94 
InitMetadataTrackFormat(const std::string_view & mimeType,const std::string_view & key,int32_t srcTrackID)95 void AVFormatCapiMock::InitMetadataTrackFormat(
96     const std::string_view &mimeType, const std::string_view &key, int32_t srcTrackID)
97 {
98     static_cast<void>(mimeType);
99     static_cast<void>(key);
100     static_cast<void>(srcTrackID);
101     return;
102 }
103 
AVFormat_Copy(struct OH_AVFormat * to,struct OH_AVFormat * from)104 bool AVFormatCapiMock::AVFormat_Copy(struct OH_AVFormat *to, struct OH_AVFormat *from)
105 {
106     return OH_AVFormat_Copy(to, from);
107 }
108 
PutLongValue(const std::string_view & key,int64_t value)109 bool AVFormatCapiMock::PutLongValue(const std::string_view &key, int64_t value)
110 {
111     if (format_ != nullptr) {
112         return OH_AVFormat_SetLongValue(format_, key.data(), value);
113     }
114     return false;
115 }
116 
GetLongValue(const std::string_view & key,int64_t & value)117 bool AVFormatCapiMock::GetLongValue(const std::string_view &key, int64_t &value)
118 {
119     if (format_ != nullptr) {
120         return OH_AVFormat_GetLongValue(format_, key.data(), &value);
121     }
122     return false;
123 }
124 
PutFloatValue(const std::string_view & key,float value)125 bool AVFormatCapiMock::PutFloatValue(const std::string_view &key, float value)
126 {
127     if (format_ != nullptr) {
128         return OH_AVFormat_SetFloatValue(format_, key.data(), value);
129     }
130     return false;
131 }
132 
GetFloatValue(const std::string_view & key,float & value)133 bool AVFormatCapiMock::GetFloatValue(const std::string_view &key, float &value)
134 {
135     if (format_ != nullptr) {
136         return OH_AVFormat_GetFloatValue(format_, key.data(), &value);
137     }
138     return false;
139 }
140 
PutDoubleValue(const std::string_view & key,double value)141 bool AVFormatCapiMock::PutDoubleValue(const std::string_view &key, double value)
142 {
143     if (format_ != nullptr) {
144         return OH_AVFormat_SetDoubleValue(format_, key.data(), value);
145     }
146     return false;
147 }
148 
GetDoubleValue(const std::string_view & key,double & value)149 bool AVFormatCapiMock::GetDoubleValue(const std::string_view &key, double &value)
150 {
151     if (format_ != nullptr) {
152         return OH_AVFormat_GetDoubleValue(format_, key.data(), &value);
153     }
154     return false;
155 }
156 
GetBuffer(const std::string_view & key,uint8_t ** addr,size_t & size)157 bool AVFormatCapiMock::GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size)
158 {
159     if (format_ != nullptr) {
160         return OH_AVFormat_GetBuffer(format_, key.data(), addr, &size);
161     }
162     return false;
163 }
164 
PutBuffer(const std::string_view & key,const uint8_t * addr,size_t size)165 bool AVFormatCapiMock::PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size)
166 {
167     if (format_ != nullptr) {
168         return OH_AVFormat_SetBuffer(format_, key.data(), addr, size);
169     }
170     return false;
171 }
172 
DumpInfo()173 const char *AVFormatCapiMock::DumpInfo()
174 {
175     if (format_ != nullptr) {
176         return OH_AVFormat_DumpInfo(format_);
177     }
178     return nullptr;
179 }
180 }  // namespace MediaAVCodec
181 }  // namespace OHOS