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