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