• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CopyFormatVectorMap(const Format::FormatVectorMap & from,Format::FormatVectorMap & to)64 void CopyFormatVectorMap(const Format::FormatVectorMap &from, Format::FormatVectorMap &to)
65 {
66     to = from;
67 }
68 
~Format()69 Format::~Format()
70 {
71     for (auto it = formatMap_.begin(); it != formatMap_.end(); ++it) {
72         if (it->second.type == FORMAT_TYPE_ADDR && it->second.addr != nullptr) {
73             free(it->second.addr);
74             it->second.addr = nullptr;
75         }
76     }
77 }
78 
Format(const Format & rhs)79 Format::Format(const Format &rhs)
80 {
81     CHECK_AND_RETURN_LOG(&rhs != this, "Copying oneself is not supported");
82     CopyFormatDataMap(rhs.formatMap_, formatMap_);
83     CopyFormatVectorMap(rhs.formatVecMap_, formatVecMap_);
84 }
85 
Format(Format && rhs)86 Format::Format(Format &&rhs) noexcept
87 {
88     std::swap(formatMap_, rhs.formatMap_);
89     std::swap(formatVecMap_, rhs.formatVecMap_);
90 }
91 
operator =(const Format & rhs)92 Format &Format::operator=(const Format &rhs)
93 {
94     CHECK_AND_RETURN_RET_LOG(&rhs != this, *this, "Copying oneself is not supported");
95     CopyFormatDataMap(rhs.formatMap_, this->formatMap_);
96     CopyFormatVectorMap(rhs.formatVecMap_, this->formatVecMap_);
97     return *this;
98 }
99 
operator =(Format && rhs)100 Format &Format::operator=(Format &&rhs) noexcept
101 {
102     CHECK_AND_RETURN_RET_LOG(&rhs != this, *this, "Copying oneself is not supported");
103     std::swap(this->formatMap_, rhs.formatMap_);
104     std::swap(this->formatVecMap_, rhs.formatVecMap_);
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     CHECK_AND_RETURN_RET_LOG(iter != formatMap_.end(), false,
162         "Format::GetFormat iter failed. Key: %{public}s", key.data());
163     CHECK_AND_RETURN_RET_LOG(iter->second.type == FORMAT_TYPE_STRING, false,
164         "Format::GetFormat type failed. Key: %{public}s", key.data());
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     CHECK_AND_RETURN_RET_LOG(iter != formatMap_.end(), false,
173         "Format::GetFormat iter failed. Key: %{public}s", key.data());
174     CHECK_AND_RETURN_RET_LOG(iter->second.type == FORMAT_TYPE_INT32, false,
175         "Format::GetFormat type failed. Key: %{public}s", key.data());
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     CHECK_AND_RETURN_RET_LOG(iter != formatMap_.end(), false,
184         "Format::GetFormat iter failed. Key: %{public}s", key.data());
185     CHECK_AND_RETURN_RET_LOG(iter->second.type == FORMAT_TYPE_INT64, false,
186         "Format::GetFormat type failed. Key: %{public}s", key.data());
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     CHECK_AND_RETURN_RET_LOG(iter != formatMap_.end(), false,
195         "Format::GetFormat iter failed. Key: %{public}s", key.data());
196     CHECK_AND_RETURN_RET_LOG(iter->second.type == FORMAT_TYPE_FLOAT, false,
197         "Format::GetFormat type failed. Key: %{public}s", key.data());
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     CHECK_AND_RETURN_RET_LOG(iter != formatMap_.end(), false,
206         "Format::GetFormat iter failed. Key: %{public}s", key.data());
207     CHECK_AND_RETURN_RET_LOG(iter->second.type == FORMAT_TYPE_DOUBLE, false,
208         "Format::GetFormat type failed. Key: %{public}s", key.data());
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     CHECK_AND_RETURN_RET_LOG(addr != nullptr, false, "put buffer error, addr is nullptr");
216     constexpr size_t sizeMax = 1 * 1024 * 1024;
217     CHECK_AND_RETURN_RET_LOG(size <= sizeMax, false,
218         "PutBuffer input size too large. Key: %{public}s", key.data());
219 
220     FormatData data;
221     data.type = FORMAT_TYPE_ADDR;
222     data.addr = reinterpret_cast<uint8_t *>(malloc(size));
223     CHECK_AND_RETURN_RET_LOG(data.addr != nullptr, false,
224         "malloc addr failed. Key: %{public}s", key.data());
225 
226     errno_t err = memcpy_s(reinterpret_cast<void *>(data.addr), size, reinterpret_cast<const void *>(addr), size);
227     if (err != EOK) {
228         MEDIA_LOGE("PutBuffer memcpy addr failed. Key: %{public}s", key.data());
229         free(data.addr);
230         return false;
231     }
232 
233     RemoveKey(key);
234 
235     data.size = size;
236     auto ret = formatMap_.insert(std::make_pair(key, data));
237     return ret.second;
238 }
239 
GetBuffer(const std::string_view & key,uint8_t ** addr,size_t & size) const240 bool Format::GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size) const
241 {
242     auto iter = formatMap_.find(key);
243     CHECK_AND_RETURN_RET_LOG(iter != formatMap_.end(), false,
244         "Format::GetFormat iter failed. Key: %{public}s", key.data());
245     CHECK_AND_RETURN_RET_LOG(iter->second.type == FORMAT_TYPE_ADDR, false,
246         "Format::GetFormat type failed. Key: %{public}s", key.data());
247     *addr = iter->second.addr;
248     size = iter->second.size;
249     return true;
250 }
251 
PutFormatVector(const std::string_view & key,std::vector<Format> & value)252 bool Format::PutFormatVector(const std::string_view &key, std::vector<Format> &value)
253 {
254     RemoveKey(key);
255     auto ret = formatVecMap_.insert(std::make_pair(key, value));
256     return ret.second;
257 }
258 
GetFormatVector(const std::string_view & key,std::vector<Format> & value) const259 bool Format::GetFormatVector(const std::string_view &key, std::vector<Format> &value) const
260 {
261     auto iter = formatVecMap_.find(key);
262     if (iter == formatVecMap_.end()) {
263         MEDIA_LOGE("Format::GetFormatVector failed. Key: %{public}s", key.data());
264         return false;
265     }
266     value.assign(iter->second.begin(), iter->second.end());
267     return true;
268 }
269 
ContainKey(const std::string_view & key) const270 bool Format::ContainKey(const std::string_view &key) const
271 {
272     auto iter = formatMap_.find(key);
273     if (iter != formatMap_.end()) {
274         return true;
275     }
276 
277     return false;
278 }
279 
GetValueType(const std::string_view & key) const280 FormatDataType Format::GetValueType(const std::string_view &key) const
281 {
282     auto iter = formatMap_.find(key);
283     if (iter == formatMap_.end()) {
284         return FORMAT_TYPE_NONE;
285     }
286 
287     return iter->second.type;
288 }
289 
RemoveKey(const std::string_view & key)290 void Format::RemoveKey(const std::string_view &key)
291 {
292     auto iter = formatMap_.find(key);
293     if (iter != formatMap_.end()) {
294         if (iter->second.type == FORMAT_TYPE_ADDR && iter->second.addr != nullptr) {
295             free(iter->second.addr);
296             iter->second.addr = nullptr;
297         }
298         formatMap_.erase(iter);
299     }
300 
301     auto vecMapIter = formatVecMap_.find(key);
302     if (vecMapIter != formatVecMap_.end()) {
303         formatVecMap_.erase(vecMapIter);
304     }
305 }
306 
GetFormatMap() const307 const Format::FormatDataMap &Format::GetFormatMap() const
308 {
309     return formatMap_;
310 }
311 
GetFormatVectorMap() const312 const Format::FormatVectorMap &Format::GetFormatVectorMap() const
313 {
314     return formatVecMap_;
315 }
316 
Stringify() const317 std::string Format::Stringify() const
318 {
319     std::string outString;
320     for (auto iter = formatMap_.begin(); iter != formatMap_.end(); iter++) {
321         switch (GetValueType(iter->first)) {
322             case FORMAT_TYPE_INT32:
323                 outString += iter->first + " = " + std::to_string(iter->second.val.int32Val) + " | ";
324                 break;
325             case FORMAT_TYPE_INT64:
326                 outString += iter->first + " = " + std::to_string(iter->second.val.int64Val) + " | ";
327                 break;
328             case FORMAT_TYPE_FLOAT:
329                 outString += iter->first + " = " + std::to_string(iter->second.val.floatVal) + " | ";
330                 break;
331             case FORMAT_TYPE_DOUBLE:
332                 outString += iter->first + " = " + std::to_string(iter->second.val.doubleVal) + " | ";
333                 break;
334             case FORMAT_TYPE_STRING:
335                 outString += iter->first + " = " + iter->second.stringVal + " | ";
336                 break;
337             case FORMAT_TYPE_ADDR:
338                 break;
339             default:
340                 MEDIA_LOGE("Format::Stringify failed. Key: %{public}s", iter->first.c_str());
341         }
342     }
343     return outString;
344 }
345 } // namespace Media
346 } // namespace OHOS