• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2025 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 "meta/format.h"
17 #include <sstream>
18 #include "common/log.h"
19 #include "common/status.h"
20 #include "meta/meta.h"
21 #include "securec.h"
22 
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "Format" };
25 }
26 
27 namespace {
28 using namespace OHOS::Media;
29 using FormatDataMap = Format::FormatDataMap;
30 constexpr size_t BUFFER_SIZE_MAX = 1 * 1024 * 1024;
31 
CopyFormatVectorMap(const Format::FormatVectorMap & from,Format::FormatVectorMap & to)32 void CopyFormatVectorMap(const Format::FormatVectorMap &from, Format::FormatVectorMap &to)
33 {
34     to = from;
35 }
36 
37 #ifdef MEDIA_OHOS
PutIntValueToFormatMap(FormatDataMap & formatMap,const std::string_view & key,int32_t value)38 bool PutIntValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, int32_t value)
39 {
40     FormatData data;
41     data.type = FORMAT_TYPE_INT32;
42     data.val.int32Val = value;
43     auto ret = formatMap.insert(std::make_pair(std::string(key), data));
44     return ret.second;
45 }
46 
PutLongValueToFormatMap(FormatDataMap & formatMap,const std::string_view & key,int64_t value)47 bool PutLongValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, int64_t value)
48 {
49     FormatData data;
50     data.type = FORMAT_TYPE_INT64;
51     data.val.int64Val = value;
52     auto ret = formatMap.insert(std::make_pair(std::string(key), data));
53     return ret.second;
54 }
55 
PutFloatValueToFormatMap(FormatDataMap & formatMap,const std::string_view & key,float value)56 bool PutFloatValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, float value)
57 {
58     FormatData data;
59     data.type = FORMAT_TYPE_FLOAT;
60     data.val.floatVal = value;
61     auto ret = formatMap.insert(std::make_pair(std::string(key), data));
62     return ret.second;
63 }
64 
PutDoubleValueToFormatMap(FormatDataMap & formatMap,const std::string_view & key,double value)65 bool PutDoubleValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, double value)
66 {
67     FormatData data;
68     data.type = FORMAT_TYPE_DOUBLE;
69     data.val.doubleVal = value;
70     auto ret = formatMap.insert(std::make_pair(std::string(key), data));
71     return ret.second;
72 }
73 
PutStringValueToFormatMap(FormatDataMap & formatMap,const std::string_view & key,const std::string_view & value)74 bool PutStringValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, const std::string_view &value)
75 {
76     FormatData data;
77     data.type = FORMAT_TYPE_STRING;
78     data.stringVal = value;
79     auto ret = formatMap.insert(std::make_pair(std::string(key), data));
80     return ret.second;
81 }
82 
PutBufferToFormatMap(FormatDataMap & formatMap,const std::string_view & key,uint8_t * addr,size_t size)83 bool PutBufferToFormatMap(FormatDataMap &formatMap, const std::string_view &key, uint8_t *addr, size_t size)
84 {
85     FormatData data;
86     FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr");
87     data.type = FORMAT_TYPE_ADDR;
88     data.addr = addr;
89     data.size = size;
90     auto ret = formatMap.insert(std::make_pair(std::string(key), data));
91     return ret.second;
92 }
93 
PutIntBufferToFormatMap(FormatDataMap & formatMap,const std::string_view & key,int32_t * addr,size_t size)94 bool PutIntBufferToFormatMap(FormatDataMap &formatMap, const std::string_view &key, int32_t *addr, size_t size)
95 {
96     FormatData data;
97     FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr");
98     data.type = FORMAT_TYPE_ADDR;
99     data.addr = reinterpret_cast<uint8_t*>(addr);
100     data.size = size * sizeof(int32_t);
101     auto ret = formatMap.insert(std::make_pair(std::string(key), data));
102     return ret.second;
103 }
104 #endif
105 } // namespace
106 
107 namespace OHOS {
108 namespace Media {
~Format()109 Format::~Format()
110 {
111     this->meta_ = nullptr;
112 }
113 
Format()114 Format::Format()
115 {
116     this->meta_ = std::make_shared<Meta>();
117 }
118 
Format(const Format & rhs)119 Format::Format(const Format &rhs)
120 {
121     FALSE_RETURN_W(&rhs != this);
122     this->meta_ = std::make_shared<Meta>();
123 
124     FALSE_RETURN_W(this->meta_ != nullptr);
125     FALSE_RETURN_W(rhs.meta_ != nullptr);
126     *(this->meta_) = *(rhs.meta_);
127     CopyFormatVectorMap(rhs.formatVecMap_, formatVecMap_);
128 }
129 
Format(Format && rhs)130 Format::Format(Format &&rhs) noexcept
131 {
132     this->meta_ = rhs.meta_;
133     std::swap(formatVecMap_, rhs.formatVecMap_);
134 }
135 
operator =(const Format & rhs)136 Format &Format::operator=(const Format &rhs)
137 {
138     FALSE_RETURN_V_W(&rhs != this, *this);
139 
140     FALSE_RETURN_V_W(this->meta_ != nullptr, *this);
141     FALSE_RETURN_V_W(rhs.meta_ != nullptr, *this);
142     *(this->meta_) = *(rhs.meta_);
143     CopyFormatVectorMap(rhs.formatVecMap_, this->formatVecMap_);
144     return *this;
145 }
146 
operator =(Format && rhs)147 Format &Format::operator=(Format &&rhs) noexcept
148 {
149     FALSE_RETURN_V(&rhs != this, *this);
150     this->meta_ = rhs.meta_;
151     std::swap(this->formatVecMap_, rhs.formatVecMap_);
152     return *this;
153 }
154 
PutIntValue(const std::string_view & key,int32_t value)155 bool Format::PutIntValue(const std::string_view &key, int32_t value)
156 {
157     auto defaultValue = GetDefaultAnyValueOpt(key.data());
158     if (defaultValue != std::nullopt) {
159         auto isSameType =
160             Any::IsSameTypeWith<int32_t>(defaultValue.value()) ||
161             Any::IsSameTypeWith<bool>(defaultValue.value()) || IsIntEnum(key.data());
162         FALSE_RETURN_V_MSG_E(isSameType, false, "Key's value type does not match int32, key: %{public}s", key.data());
163     }
164 
165     return SetMetaData(*meta_, std::string(key), value);
166 }
167 
PutLongValue(const std::string_view & key,int64_t value)168 bool Format::PutLongValue(const std::string_view &key, int64_t value)
169 {
170     auto defaultValue = GetDefaultAnyValueOpt(key.data());
171     if (defaultValue != std::nullopt) {
172         auto isSameType =
173             Any::IsSameTypeWith<int64_t>(defaultValue.value()) || IsLongEnum(key.data());
174         FALSE_RETURN_V_MSG_E(isSameType, false, "Key's value type does not match int64, key: %{public}s", key.data());
175     }
176     return SetMetaData(*meta_, std::string(key), value);
177 }
178 
PutFloatValue(const std::string_view & key,float value)179 bool Format::PutFloatValue(const std::string_view &key, float value)
180 {
181     auto defaultValue = GetDefaultAnyValueOpt(key.data());
182     if (defaultValue != std::nullopt) {
183         auto isSameType = Any::IsSameTypeWith<float>(defaultValue.value());
184         FALSE_RETURN_V_MSG_E(isSameType, false, "Key's value type does not match float, key: %{public}s", key.data());
185     }
186 
187     meta_->SetData(std::string(key), value);
188     return true;
189 }
190 
PutDoubleValue(const std::string_view & key,double value)191 bool Format::PutDoubleValue(const std::string_view &key, double value)
192 {
193     auto defaultValue = GetDefaultAnyValueOpt(key.data());
194     if (defaultValue != std::nullopt) {
195         auto isSameType = Any::IsSameTypeWith<double>(defaultValue.value());
196         FALSE_RETURN_V_MSG_E(isSameType, false, "Key's value type does not match double, key: %{public}s", key.data());
197     }
198 
199     meta_->SetData(std::string(key), value);
200     return true;
201 }
202 
PutStringValue(const std::string_view & key,const std::string_view & value)203 bool Format::PutStringValue(const std::string_view &key, const std::string_view &value)
204 {
205     auto defaultValue = GetDefaultAnyValueOpt(key.data());
206     if (defaultValue != std::nullopt) {
207         auto isSameType = Any::IsSameTypeWith<std::string>(defaultValue.value());
208         FALSE_RETURN_V_MSG_E(isSameType, false, "Key's value type does not match string, key: %{public}s", key.data());
209     }
210 
211     meta_->SetData(std::string(key), std::string(value));
212     return true;
213 }
214 
PutBuffer(const std::string_view & key,const uint8_t * addr,size_t size)215 bool Format::PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size)
216 {
217     auto defaultValue = GetDefaultAnyValueOpt(key.data());
218     if (defaultValue != std::nullopt) {
219         auto isSameType = Any::IsSameTypeWith<std::vector<uint8_t>>(defaultValue.value());
220         FALSE_RETURN_V_MSG_E(isSameType, false, "Key's value type does not match buffer, key: %{public}s", key.data());
221     }
222     FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr");
223     FALSE_RETURN_V_MSG_E(size <= BUFFER_SIZE_MAX, false, "PutBuffer input size failed. Key: " PUBLIC_LOG_S, key.data());
224 
225     auto iter = meta_->Find(std::string(key));
226     if (iter == meta_->end()) {
227         std::vector<uint8_t> value(addr, addr + size);
228         meta_->SetData(std::string(key), std::move(value));
229         return true;
230     }
231     Any *value = const_cast<Any *>(&(iter->second));
232     auto tmpVector = AnyCast<std::vector<uint8_t>>(value);
233     FALSE_RETURN_V_MSG_E(tmpVector != nullptr, false, "Any value is invalid. Key: " PUBLIC_LOG_S, key.data());
234 
235     tmpVector->resize(size);
236     uint8_t *anyAddr = tmpVector->data();
237     auto error = memcpy_s(anyAddr, size, addr, size);
238     FALSE_RETURN_V_MSG_E(error == EOK, false, "PutBuffer memcpy_s failed, error: %{public}s", strerror(error));
239 
240     auto formatMapIter = formatMap_.find(key);
241     if (formatMapIter != formatMap_.end()) {
242         formatMap_.erase(formatMapIter);
243         PutBufferToFormatMap(formatMap_, key, anyAddr, size);
244     }
245     return true;
246 }
247 
PutIntBuffer(const std::string_view & key,const int32_t * addr,size_t size)248 bool Format::PutIntBuffer(const std::string_view &key, const int32_t *addr, size_t size)
249 {
250     auto defaultValue = GetDefaultAnyValueOpt(key.data());
251     if (defaultValue != std::nullopt) {
252         auto isSameType = Any::IsSameTypeWith<std::vector<int32_t>>(defaultValue.value());
253         FALSE_RETURN_V_MSG_E(isSameType, false, "Key's value type does not match buffer, key: %{public}s", key.data());
254     }
255     FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr");
256     FALSE_RETURN_V_MSG_E(size <= BUFFER_SIZE_MAX, false, "PutBuffer input size failed. Key: " PUBLIC_LOG_S, key.data());
257 
258     auto iter = meta_->Find(std::string(key));
259     if (iter == meta_->end()) {
260         std::vector<int32_t> value(addr, addr + size);
261         meta_->SetData(std::string(key), std::move(value));
262         return true;
263     }
264     Any *value = const_cast<Any *>(&(iter->second));
265     auto tmpVector = AnyCast<std::vector<int32_t>>(value);
266     FALSE_RETURN_V_MSG_E(tmpVector != nullptr, false, "Any value is invalid. Key: " PUBLIC_LOG_S, key.data());
267 
268     tmpVector->resize(size);
269     int32_t *anyAddr = tmpVector->data();
270     auto error = memcpy_s(anyAddr, size * sizeof(int32_t), addr, size * sizeof(int32_t));
271     FALSE_RETURN_V_MSG_E(error == EOK, false, "PutBuffer memcpy_s failed, error: %{public}s", strerror(error));
272 
273     auto formatMapIter = formatMap_.find(key);
274     if (formatMapIter != formatMap_.end()) {
275         formatMap_.erase(formatMapIter);
276         PutIntBufferToFormatMap(formatMap_, key, anyAddr, size);
277     }
278     return true;
279 }
280 
GetIntValue(const std::string_view & key,int32_t & value) const281 bool Format::GetIntValue(const std::string_view &key, int32_t &value) const
282 {
283     return GetMetaData(*meta_, std::string(key), value);
284 }
285 
GetLongValue(const std::string_view & key,int64_t & value) const286 bool Format::GetLongValue(const std::string_view &key, int64_t &value) const
287 {
288     return GetMetaData(*meta_, std::string(key), value);
289 }
290 
GetFloatValue(const std::string_view & key,float & value) const291 bool Format::GetFloatValue(const std::string_view &key, float &value) const
292 {
293     return meta_->GetData(std::string(key), value);
294 }
295 
GetDoubleValue(const std::string_view & key,double & value) const296 bool Format::GetDoubleValue(const std::string_view &key, double &value) const
297 {
298     return meta_->GetData(std::string(key), value);
299 }
300 
GetStringValue(const std::string_view & key,std::string & value) const301 bool Format::GetStringValue(const std::string_view &key, std::string &value) const
302 {
303     return meta_->GetData(std::string(key), value);
304 }
305 
GetBuffer(const std::string_view & key,uint8_t ** addr,size_t & size) const306 bool Format::GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size) const
307 {
308     using Buf = std::vector<uint8_t>;
309     auto iter = meta_->Find(std::string(key));
310     if ((iter != meta_->end()) && Any::IsSameTypeWith<Buf>(iter->second)) {
311         Any *value = const_cast<Any *>(&(iter->second));
312         if (AnyCast<Buf>(value) != nullptr) {
313             *addr = (AnyCast<Buf>(value))->data();
314             size = (AnyCast<Buf>(value))->size();
315             return true;
316         }
317     }
318     return false;
319 }
320 
GetIntBuffer(const std::string_view & key,int32_t ** addr,size_t & size) const321 bool Format::GetIntBuffer(const std::string_view &key, int32_t **addr, size_t &size) const
322 {
323     using Buf = std::vector<int32_t>;
324     auto iter = meta_->Find(std::string(key));
325     if ((iter != meta_->end()) && Any::IsSameTypeWith<Buf>(iter->second)) {
326         Any *value = const_cast<Any *>(&(iter->second));
327         if (AnyCast<Buf>(value) != nullptr) {
328             *addr = (AnyCast<Buf>(value))->data();
329             size = (AnyCast<Buf>(value))->size();
330             return true;
331         }
332     }
333     return false;
334 }
335 
PutFormatVector(const std::string_view & key,std::vector<Format> & value)336 bool Format::PutFormatVector(const std::string_view &key, std::vector<Format> &value)
337 {
338     RemoveKey(key);
339     auto ret = formatVecMap_.insert(std::make_pair(std::string(key), value));
340     return ret.second;
341 }
342 
GetFormatVector(const std::string_view & key,std::vector<Format> & value) const343 bool Format::GetFormatVector(const std::string_view &key, std::vector<Format> &value) const
344 {
345     auto iter = formatVecMap_.find(key);
346     FALSE_RETURN_V_MSG_E(iter != formatVecMap_.end(),
347         false, "GetFormatVector failed. Key: %{public}s", key.data());
348     value.assign(iter->second.begin(), iter->second.end());
349     return true;
350 }
351 
ContainKey(const std::string_view & key) const352 bool Format::ContainKey(const std::string_view &key) const
353 {
354     auto iter = meta_->Find(std::string(key));
355     if (iter != meta_->end()) {
356         return true;
357     }
358     auto vecMapIter = formatVecMap_.find(key);
359     return vecMapIter != formatVecMap_.end();
360 }
361 
GetValueType(const std::string_view & key) const362 FormatDataType Format::GetValueType(const std::string_view &key) const
363 {
364     auto iter = meta_->Find(std::string(key));
365     if (iter != meta_->end()) {
366         if (Any::IsSameTypeWith<int32_t>(iter->second)) {
367             return FORMAT_TYPE_INT32;
368         } else if (Any::IsSameTypeWith<int64_t>(iter->second)) {
369             return FORMAT_TYPE_INT64;
370         } else if (Any::IsSameTypeWith<float>(iter->second)) {
371             return FORMAT_TYPE_FLOAT;
372         } else if (Any::IsSameTypeWith<double>(iter->second)) {
373             return FORMAT_TYPE_DOUBLE;
374         } else if (Any::IsSameTypeWith<std::string>(iter->second)) {
375             return FORMAT_TYPE_STRING;
376         } else if (Any::IsSameTypeWith<std::vector<uint8_t>>(iter->second)) {
377             return FORMAT_TYPE_ADDR;
378         } else {
379             int64_t valueTemp;
380             bool isLongValue = GetMetaData(*meta_, std::string(key), valueTemp);
381             return isLongValue ? FORMAT_TYPE_INT64 : FORMAT_TYPE_INT32;
382         }
383     }
384     return FORMAT_TYPE_NONE;
385 }
386 
RemoveKey(const std::string_view & key)387 void Format::RemoveKey(const std::string_view &key)
388 {
389     meta_->Remove(std::string(key));
390 
391     auto vecMapIter = formatVecMap_.find(key);
392     if (vecMapIter != formatVecMap_.end()) {
393         formatVecMap_.erase(vecMapIter);
394     }
395 }
396 
GetFormatMap() const397 const Format::FormatDataMap &Format::GetFormatMap() const
398 {
399 #ifdef MEDIA_OHOS
400     FormatDataMap formatTemp;
401     bool ret = true;
402     for (auto iter = meta_->begin(); iter != meta_->end(); ++iter) {
403         switch (GetValueType(iter->first)) {
404             case FORMAT_TYPE_INT32:
405                 ret = PutIntValueToFormatMap(formatTemp, iter->first, AnyCast<int32_t>(iter->second));
406                 break;
407             case FORMAT_TYPE_INT64:
408                 ret = PutLongValueToFormatMap(formatTemp, iter->first, AnyCast<int64_t>(iter->second));
409                 break;
410             case FORMAT_TYPE_FLOAT:
411                 ret = PutFloatValueToFormatMap(formatTemp, iter->first, AnyCast<float>(iter->second));
412                 break;
413             case FORMAT_TYPE_DOUBLE:
414                 ret = PutDoubleValueToFormatMap(formatTemp, iter->first, AnyCast<double>(iter->second));
415                 break;
416             case FORMAT_TYPE_STRING:
417                 ret = PutStringValueToFormatMap(formatTemp, iter->first, AnyCast<std::string>(iter->second));
418                 break;
419             case FORMAT_TYPE_ADDR: {
420                 Any *value = const_cast<Any *>(&(iter->second));
421                 uint8_t *addr = (AnyCast<std::vector<uint8_t>>(value))->data();
422                 size_t size = (AnyCast<std::vector<uint8_t>>(value))->size();
423                 ret = PutBufferToFormatMap(formatTemp, iter->first, addr, size);
424                 break;
425             }
426             default:
427                 MEDIA_LOG_E("Format::Stringify failed. Key: %{public}s", iter->first.c_str());
428         }
429         FALSE_LOG_MSG(ret, "Put value to formatMap failed, key = %{public}s", iter->first.c_str());
430     }
431     FormatDataMap *formatMapRef = const_cast<FormatDataMap *>(&formatMap_);
432     swap(formatTemp, *formatMapRef);
433 #endif
434     return formatMap_;
435 }
436 
GetFormatVectorMap() const437 const Format::FormatVectorMap &Format::GetFormatVectorMap() const
438 {
439     return formatVecMap_;
440 }
441 
Stringify() const442 std::string Format::Stringify() const
443 {
444     std::stringstream dumpStream;
445     for (auto iter = meta_->begin(); iter != meta_->end(); ++iter) {
446         switch (GetValueType(iter->first)) {
447             case FORMAT_TYPE_INT32:
448                 dumpStream << iter->first << " = " << std::to_string(AnyCast<int32_t>(iter->second)) << " | ";
449                 break;
450             case FORMAT_TYPE_INT64:
451                 dumpStream << iter->first << " = " << std::to_string(AnyCast<int64_t>(iter->second)) << " | ";
452                 break;
453             case FORMAT_TYPE_FLOAT:
454                 dumpStream << iter->first << " = " << std::to_string(AnyCast<float>(iter->second)) << " | ";
455                 break;
456             case FORMAT_TYPE_DOUBLE:
457                 dumpStream << iter->first << " = " << std::to_string(AnyCast<double>(iter->second)) << " | ";
458                 break;
459             case FORMAT_TYPE_STRING:
460                 dumpStream << iter->first << " = " << AnyCast<std::string>(iter->second) << " | ";
461                 break;
462             case FORMAT_TYPE_ADDR: {
463                 Any *value = const_cast<Any *>(&(iter->second));
464                 if (AnyCast<std::vector<uint8_t>>(value) != nullptr) {
465                     dumpStream << iter->first << ", bufferSize = " << (AnyCast<std::vector<uint8_t>>(value))->size()
466                             << " | ";
467                 }
468                 break;
469             }
470             default:
471                 MEDIA_LOG_E("Format::Stringify failed. Key: %{public}s", iter->first.c_str());
472         }
473     }
474     return dumpStream.str();
475 }
476 
GetMeta()477 std::shared_ptr<Meta> Format::GetMeta()
478 {
479     return meta_;
480 }
481 
SetMetaPtr(std::shared_ptr<Meta> meta)482 bool Format::SetMetaPtr(std::shared_ptr<Meta> meta)
483 {
484     FALSE_RETURN_V(meta != nullptr, false);
485     meta_ = meta;
486     return true;
487 }
488 
SetMeta(std::shared_ptr<Meta> meta)489 bool Format::SetMeta(std::shared_ptr<Meta> meta)
490 {
491     FALSE_RETURN_V(meta != nullptr, false);
492     if (meta.use_count() > 1) {
493         *meta_ = *meta;
494     } else {
495         meta_ = meta;
496     }
497     return true;
498 }
499 } // namespace Media
500 } // namespace OHOS