• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "native_avformat.h"
17 
18 #include "securec.h"
19 #include "native_avmagic.h"
20 #include "media_log.h"
21 #include "media_errors.h"
22 
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "OH_AVFormat"};
25 constexpr uint32_t MAX_STRING_LENGTH = 256;
26 constexpr uint32_t MAX_DUMP_LENGTH = 1024;
27 }
28 
29 using namespace OHOS::Media;
30 
OH_AVFormat()31 OH_AVFormat::OH_AVFormat()
32     : AVObjectMagic(AVMagic::MEDIA_MAGIC_FORMAT)
33 {
34 }
35 
OH_AVFormat(const Format & fmt)36 OH_AVFormat::OH_AVFormat(const Format &fmt)
37     : AVObjectMagic(AVMagic::MEDIA_MAGIC_FORMAT), format_(fmt)
38 {
39 }
40 
~OH_AVFormat()41 OH_AVFormat::~OH_AVFormat()
42 {
43     if (outString_ != nullptr) {
44         free(outString_);
45         outString_ = nullptr;
46     }
47     if (dumpInfo_ != nullptr) {
48         free(dumpInfo_);
49         dumpInfo_ = nullptr;
50     }
51 }
52 
OH_AVFormat_Create(void)53 struct OH_AVFormat *OH_AVFormat_Create(void)
54 {
55     return new(std::nothrow) OH_AVFormat();
56 }
57 
OH_AVFormat_Destroy(struct OH_AVFormat * format)58 void OH_AVFormat_Destroy(struct OH_AVFormat *format)
59 {
60     delete format;
61 }
62 
OH_AVFormat_Copy(struct OH_AVFormat * to,struct OH_AVFormat * from)63 bool OH_AVFormat_Copy(struct OH_AVFormat *to, struct OH_AVFormat *from)
64 {
65     CHECK_AND_RETURN_RET_LOG(to != nullptr, false, "to format is nullptr!");
66     CHECK_AND_RETURN_RET_LOG(to->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
67     CHECK_AND_RETURN_RET_LOG(from != nullptr, false, "from format is nullptr!");
68     CHECK_AND_RETURN_RET_LOG(from->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
69 
70     to->format_ = from->format_;
71     return true;
72 }
73 
OH_AVFormat_SetIntValue(struct OH_AVFormat * format,const char * key,int32_t value)74 bool OH_AVFormat_SetIntValue(struct OH_AVFormat *format, const char *key, int32_t value)
75 {
76     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
77     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
78     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
79 
80     return format->format_.PutIntValue(key, value);
81 }
82 
OH_AVFormat_SetLongValue(struct OH_AVFormat * format,const char * key,int64_t value)83 bool OH_AVFormat_SetLongValue(struct OH_AVFormat *format, const char *key, int64_t value)
84 {
85     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
86     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
87     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
88 
89     return format->format_.PutLongValue(key, value);
90 }
91 
OH_AVFormat_SetFloatValue(struct OH_AVFormat * format,const char * key,float value)92 bool OH_AVFormat_SetFloatValue(struct OH_AVFormat *format, const char *key, float value)
93 {
94     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
95     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
96     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
97 
98     return format->format_.PutFloatValue(key, value);
99 }
100 
OH_AVFormat_SetDoubleValue(struct OH_AVFormat * format,const char * key,double value)101 bool OH_AVFormat_SetDoubleValue(struct OH_AVFormat *format, const char *key, double value)
102 {
103     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
104     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
105     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
106 
107     return format->format_.PutDoubleValue(key, value);
108 }
109 
OH_AVFormat_SetStringValue(struct OH_AVFormat * format,const char * key,const char * value)110 bool OH_AVFormat_SetStringValue(struct OH_AVFormat *format, const char *key, const char *value)
111 {
112     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
113     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
114     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
115     CHECK_AND_RETURN_RET_LOG(value != nullptr, false, "value is nullptr!");
116 
117     return format->format_.PutStringValue(key, value);
118 }
119 
OH_AVFormat_SetBuffer(struct OH_AVFormat * format,const char * key,const uint8_t * addr,size_t size)120 bool OH_AVFormat_SetBuffer(struct OH_AVFormat *format, const char *key, const uint8_t *addr, size_t size)
121 {
122     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
123     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
124     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
125     CHECK_AND_RETURN_RET_LOG(addr != nullptr, false, "addr is nullptr!");
126     CHECK_AND_RETURN_RET_LOG(size != 0, false, "size is zero!");
127 
128     return format->format_.PutBuffer(key, addr, size);
129 }
130 
OH_AVFormat_GetIntValue(struct OH_AVFormat * format,const char * key,int32_t * out)131 bool OH_AVFormat_GetIntValue(struct OH_AVFormat *format, const char *key, int32_t *out)
132 {
133     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
134     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
135     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
136     CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
137 
138     return format->format_.GetIntValue(key, *out);
139 }
140 
OH_AVFormat_GetLongValue(struct OH_AVFormat * format,const char * key,int64_t * out)141 bool OH_AVFormat_GetLongValue(struct OH_AVFormat *format, const char *key, int64_t *out)
142 {
143     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
144     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
145     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
146     CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
147 
148     return format->format_.GetLongValue(key, *out);
149 }
150 
OH_AVFormat_GetFloatValue(struct OH_AVFormat * format,const char * key,float * out)151 bool OH_AVFormat_GetFloatValue(struct OH_AVFormat *format, const char *key, float *out)
152 {
153     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
154     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
155     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
156     CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
157 
158     return format->format_.GetFloatValue(key, *out);
159 }
160 
OH_AVFormat_GetDoubleValue(struct OH_AVFormat * format,const char * key,double * out)161 bool OH_AVFormat_GetDoubleValue(struct OH_AVFormat *format, const char *key, double *out)
162 {
163     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
164     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
165     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
166     CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
167 
168     return format->format_.GetDoubleValue(key, *out);
169 }
170 
OH_AVFormat_GetStringValue(struct OH_AVFormat * format,const char * key,const char ** out)171 bool OH_AVFormat_GetStringValue(struct OH_AVFormat *format, const char *key, const char **out)
172 {
173     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
174     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
175     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
176     CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
177 
178     if (format->outString_ != nullptr) {
179         free(format->outString_);
180         format->outString_ = nullptr;
181     }
182 
183     std::string str;
184     bool ret = format->format_.GetStringValue(key, str);
185     if (!ret) {
186         return false;
187     }
188     uint32_t bufLength = str.size() > MAX_STRING_LENGTH ? MAX_STRING_LENGTH : str.size();
189 
190     format->outString_ = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
191     CHECK_AND_RETURN_RET_LOG(format->outString_ != nullptr, false, "malloc out string nullptr!");
192 
193     if (strcpy_s(format->outString_, bufLength + 1, str.c_str()) != EOK) {
194         MEDIA_LOGE("Failed to strcpy_s");
195         free(format->outString_);
196         format->outString_ = nullptr;
197         return false;
198     }
199 
200     *out = format->outString_;
201     return true;
202 }
203 
OH_AVFormat_GetBuffer(struct OH_AVFormat * format,const char * key,uint8_t ** addr,size_t * size)204 bool OH_AVFormat_GetBuffer(struct OH_AVFormat *format, const char *key, uint8_t **addr, size_t *size)
205 {
206     CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
207     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, false, "magic error!");
208     CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
209     CHECK_AND_RETURN_RET_LOG(addr != nullptr, false, "addr is nullptr!");
210     CHECK_AND_RETURN_RET_LOG(size != nullptr, false, "size is nullptr!");
211 
212     return format->format_.GetBuffer(key, addr, *size);
213 }
214 
OH_AVFormat_DumpInfo(struct OH_AVFormat * format)215 const char *OH_AVFormat_DumpInfo(struct OH_AVFormat *format)
216 {
217     CHECK_AND_RETURN_RET_LOG(format != nullptr, nullptr, "input format is nullptr!");
218     if (format->dumpInfo_ != nullptr) {
219         free(format->dumpInfo_);
220         format->dumpInfo_ = nullptr;
221     }
222     std::string info = format->format_.Stringify();
223     if (info.empty()) {
224         return nullptr;
225     }
226     uint32_t bufLength = info.size() > MAX_DUMP_LENGTH ? MAX_DUMP_LENGTH : info.size();
227     format->dumpInfo_ = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
228     CHECK_AND_RETURN_RET_LOG(format->dumpInfo_ != nullptr, nullptr, "malloc dump info nullptr!");
229     if (strcpy_s(format->dumpInfo_, bufLength + 1, info.c_str()) != EOK) {
230         MEDIA_LOGE("Failed to strcpy_s");
231         free(format->dumpInfo_);
232         format->dumpInfo_ = nullptr;
233     }
234     return format->dumpInfo_;
235 }
236