1 /*
2 * Copyright (C) 2021 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 "format.h"
17 #include "securec.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "Format"};
23 }
24
25 namespace OHOS {
26 namespace Media {
CopyFormatDataMap(const Format::FormatDataMap & from,Format::FormatDataMap & to)27 void CopyFormatDataMap(const Format::FormatDataMap &from, Format::FormatDataMap &to)
28 {
29 for (auto it = to.begin(); it != to.end(); ++it) {
30 if (it->second.type == FORMAT_TYPE_ADDR && it->second.addr != nullptr) {
31 free(it->second.addr);
32 it->second.addr = nullptr;
33 }
34 }
35
36 to = from;
37
38 for (auto it = to.begin(); it != to.end();) {
39 if (it->second.type != FORMAT_TYPE_ADDR || it->second.addr == nullptr) {
40 ++it;
41 continue;
42 }
43
44 it->second.addr = reinterpret_cast<uint8_t *>(malloc(it->second.size));
45 if (it->second.addr == nullptr) {
46 MEDIA_LOGE("malloc addr failed. Key: %{public}s", it->first.c_str());
47 it = to.erase(it);
48 continue;
49 }
50
51 errno_t err = memcpy_s(reinterpret_cast<void *>(it->second.addr),
52 it->second.size, reinterpret_cast<const void *>(from.at(it->first).addr), it->second.size);
53 if (err != EOK) {
54 MEDIA_LOGE("memcpy addr failed. Key: %{public}s", it->first.c_str());
55 free(it->second.addr);
56 it->second.addr = nullptr;
57 it = to.erase(it);
58 continue;
59 }
60 ++it;
61 }
62 }
63
~Format()64 Format::~Format()
65 {
66 for (auto it = formatMap_.begin(); it != formatMap_.end(); ++it) {
67 if (it->second.type == FORMAT_TYPE_ADDR && it->second.addr != nullptr) {
68 free(it->second.addr);
69 it->second.addr = nullptr;
70 }
71 }
72 }
73
Format(const Format & rhs)74 Format::Format(const Format &rhs)
75 {
76 if (&rhs == this) {
77 return;
78 }
79
80 CopyFormatDataMap(rhs.formatMap_, formatMap_);
81 }
82
Format(Format && rhs)83 Format::Format(Format &&rhs) noexcept
84 {
85 std::swap(formatMap_, rhs.formatMap_);
86 }
87
operator =(const Format & rhs)88 Format &Format::operator=(const Format &rhs)
89 {
90 if (&rhs == this) {
91 return *this;
92 }
93
94 CopyFormatDataMap(rhs.formatMap_, this->formatMap_);
95 return *this;
96 }
97
operator =(Format && rhs)98 Format &Format::operator=(Format &&rhs) noexcept
99 {
100 if (&rhs == this) {
101 return *this;
102 }
103
104 std::swap(this->formatMap_, rhs.formatMap_);
105 return *this;
106 }
107
PutIntValue(const std::string_view & key,int32_t value)108 bool Format::PutIntValue(const std::string_view &key, int32_t value)
109 {
110 FormatData data;
111 data.type = FORMAT_TYPE_INT32;
112 data.val.int32Val = value;
113 RemoveKey(key);
114 auto ret = formatMap_.insert(std::make_pair(key, data));
115 return ret.second;
116 }
117
PutLongValue(const std::string_view & key,int64_t value)118 bool Format::PutLongValue(const std::string_view &key, int64_t value)
119 {
120 FormatData data;
121 data.type = FORMAT_TYPE_INT64;
122 data.val.int64Val = value;
123 RemoveKey(key);
124 auto ret = formatMap_.insert(std::make_pair(key, data));
125 return ret.second;
126 }
127
PutFloatValue(const std::string_view & key,float value)128 bool Format::PutFloatValue(const std::string_view &key, float value)
129 {
130 FormatData data;
131 data.type = FORMAT_TYPE_FLOAT;
132 data.val.floatVal = value;
133 RemoveKey(key);
134 auto ret = formatMap_.insert(std::make_pair(key, data));
135 return ret.second;
136 }
137
PutDoubleValue(const std::string_view & key,double value)138 bool Format::PutDoubleValue(const std::string_view &key, double value)
139 {
140 FormatData data;
141 data.type = FORMAT_TYPE_DOUBLE;
142 data.val.doubleVal = value;
143 RemoveKey(key);
144 auto ret = formatMap_.insert(std::make_pair(key, data));
145 return ret.second;
146 }
147
PutStringValue(const std::string_view & key,const std::string_view & value)148 bool Format::PutStringValue(const std::string_view &key, const std::string_view &value)
149 {
150 FormatData data;
151 data.type = FORMAT_TYPE_STRING;
152 data.stringVal = value;
153 RemoveKey(key);
154 auto ret = formatMap_.insert(std::make_pair(key, data));
155 return ret.second;
156 }
157
GetStringValue(const std::string_view & key,std::string & value) const158 bool Format::GetStringValue(const std::string_view &key, std::string &value) const
159 {
160 auto iter = formatMap_.find(key);
161 if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_STRING) {
162 MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
163 return false;
164 }
165 value = iter->second.stringVal;
166 return true;
167 }
168
GetIntValue(const std::string_view & key,int32_t & value) const169 bool Format::GetIntValue(const std::string_view &key, int32_t &value) const
170 {
171 auto iter = formatMap_.find(key);
172 if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_INT32) {
173 MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
174 return false;
175 }
176 value = iter->second.val.int32Val;
177 return true;
178 }
179
GetLongValue(const std::string_view & key,int64_t & value) const180 bool Format::GetLongValue(const std::string_view &key, int64_t &value) const
181 {
182 auto iter = formatMap_.find(key);
183 if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_INT64) {
184 MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
185 return false;
186 }
187 value = iter->second.val.int64Val;
188 return true;
189 }
190
GetFloatValue(const std::string_view & key,float & value) const191 bool Format::GetFloatValue(const std::string_view &key, float &value) const
192 {
193 auto iter = formatMap_.find(key);
194 if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_FLOAT) {
195 MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
196 return false;
197 }
198 value = iter->second.val.floatVal;
199 return true;
200 }
201
GetDoubleValue(const std::string_view & key,double & value) const202 bool Format::GetDoubleValue(const std::string_view &key, double &value) const
203 {
204 auto iter = formatMap_.find(key);
205 if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_DOUBLE) {
206 MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
207 return false;
208 }
209 value = iter->second.val.doubleVal;
210 return true;
211 }
212
PutBuffer(const std::string_view & key,const uint8_t * addr,size_t size)213 bool Format::PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size)
214 {
215 if (addr == nullptr) {
216 MEDIA_LOGE("put buffer error, addr is nullptr");
217 return false;
218 }
219
220 constexpr size_t sizeMax = 1 * 1024 * 1024;
221 if (size > sizeMax) {
222 MEDIA_LOGE("PutBuffer input size failed. Key: %{public}s", key.data());
223 return false;
224 }
225
226 FormatData data;
227 data.type = FORMAT_TYPE_ADDR;
228 data.addr = reinterpret_cast<uint8_t *>(malloc(size));
229 if (data.addr == nullptr) {
230 MEDIA_LOGE("malloc addr failed. Key: %{public}s", key.data());
231 return false;
232 }
233
234 errno_t err = memcpy_s(reinterpret_cast<void *>(data.addr), size, reinterpret_cast<const void *>(addr), size);
235 if (err != EOK) {
236 MEDIA_LOGE("PutBuffer memcpy addr failed. Key: %{public}s", key.data());
237 free(data.addr);
238 return false;
239 }
240
241 RemoveKey(key);
242
243 data.size = size;
244 auto ret = formatMap_.insert(std::make_pair(key, data));
245 return ret.second;
246 }
247
GetBuffer(const std::string_view & key,uint8_t ** addr,size_t & size) const248 bool Format::GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size) const
249 {
250 auto iter = formatMap_.find(key);
251 if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_ADDR) {
252 MEDIA_LOGE("Format::GetBuffer failed. Key: %{public}s", key.data());
253 return false;
254 }
255 *addr = iter->second.addr;
256 size = iter->second.size;
257 return true;
258 }
259
ContainKey(const std::string_view & key) const260 bool Format::ContainKey(const std::string_view &key) const
261 {
262 auto iter = formatMap_.find(key);
263 if (iter != formatMap_.end()) {
264 return true;
265 }
266
267 return false;
268 }
269
GetValueType(const std::string_view & key) const270 FormatDataType Format::GetValueType(const std::string_view &key) const
271 {
272 auto iter = formatMap_.find(key);
273 if (iter == formatMap_.end()) {
274 return FORMAT_TYPE_NONE;
275 }
276
277 return iter->second.type;
278 }
279
RemoveKey(const std::string_view & key)280 void Format::RemoveKey(const std::string_view &key)
281 {
282 auto iter = formatMap_.find(key);
283 if (iter != formatMap_.end()) {
284 if (iter->second.type == FORMAT_TYPE_ADDR && iter->second.addr != nullptr) {
285 free(iter->second.addr);
286 iter->second.addr = nullptr;
287 }
288 formatMap_.erase(iter);
289 }
290 }
291
GetFormatMap() const292 const Format::FormatDataMap &Format::GetFormatMap() const
293 {
294 return formatMap_;
295 }
296
Stringify() const297 std::string Format::Stringify() const
298 {
299 std::string outString;
300 for (auto iter = formatMap_.begin(); iter != formatMap_.end(); iter++) {
301 switch (GetValueType(iter->first)) {
302 case FORMAT_TYPE_INT32:
303 outString += iter->first + " = " + std::to_string(iter->second.val.int32Val) + " | ";
304 break;
305 case FORMAT_TYPE_INT64:
306 outString += iter->first + " = " + std::to_string(iter->second.val.int64Val) + " | ";
307 break;
308 case FORMAT_TYPE_FLOAT:
309 outString += iter->first + " = " + std::to_string(iter->second.val.floatVal) + " | ";
310 break;
311 case FORMAT_TYPE_DOUBLE:
312 outString += iter->first + " = " + std::to_string(iter->second.val.doubleVal) + " | ";
313 break;
314 case FORMAT_TYPE_STRING:
315 outString += iter->first + " = " + iter->second.stringVal + " | ";
316 break;
317 case FORMAT_TYPE_ADDR:
318 break;
319 default:
320 MEDIA_LOGE("Format::Stringify failed. Key: %{public}s", iter->first.c_str());
321 }
322 }
323 return outString;
324 }
325 } // namespace Media
326 } // namespace OHOS