• 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 = to.erase(it);
57             continue;
58         }
59         ++it;
60     }
61 }
62 
~Format()63 Format::~Format()
64 {
65     for (auto it = formatMap_.begin(); it != formatMap_.end(); ++it) {
66         if (it->second.type == FORMAT_TYPE_ADDR && it->second.addr != nullptr) {
67             free(it->second.addr);
68             it->second.addr = nullptr;
69         }
70     }
71 }
72 
Format(const Format & rhs)73 Format::Format(const Format &rhs)
74 {
75     if (&rhs == this) {
76         return;
77     }
78 
79     CopyFormatDataMap(rhs.formatMap_, formatMap_);
80 }
81 
Format(Format && rhs)82 Format::Format(Format &&rhs) noexcept
83 {
84     std::swap(formatMap_, rhs.formatMap_);
85 }
86 
operator =(const Format & rhs)87 Format &Format::operator=(const Format &rhs)
88 {
89     if (&rhs == this) {
90         return *this;
91     }
92 
93     CopyFormatDataMap(rhs.formatMap_, this->formatMap_);
94     return *this;
95 }
96 
operator =(Format && rhs)97 Format &Format::operator=(Format &&rhs) noexcept
98 {
99     if (&rhs == this) {
100         return *this;
101     }
102 
103     std::swap(this->formatMap_, rhs.formatMap_);
104     return *this;
105 }
106 
PutIntValue(const std::string_view & key,int32_t value)107 bool Format::PutIntValue(const std::string_view &key, int32_t value)
108 {
109     FormatData data;
110     data.type = FORMAT_TYPE_INT32;
111     data.val.int32Val = value;
112     auto ret = formatMap_.insert(std::make_pair(key, data));
113     return ret.second;
114 }
115 
PutLongValue(const std::string_view & key,int64_t value)116 bool Format::PutLongValue(const std::string_view &key, int64_t value)
117 {
118     FormatData data;
119     data.type = FORMAT_TYPE_INT64;
120     data.val.int64Val = value;
121     auto ret = formatMap_.insert(std::make_pair(key, data));
122     return ret.second;
123 }
124 
PutFloatValue(const std::string_view & key,float value)125 bool Format::PutFloatValue(const std::string_view &key, float value)
126 {
127     FormatData data;
128     data.type = FORMAT_TYPE_FLOAT;
129     data.val.floatVal = value;
130     auto ret = formatMap_.insert(std::make_pair(key, data));
131     return ret.second;
132 }
133 
PutDoubleValue(const std::string_view & key,double value)134 bool Format::PutDoubleValue(const std::string_view &key, double value)
135 {
136     FormatData data;
137     data.type = FORMAT_TYPE_DOUBLE;
138     data.val.doubleVal = value;
139     auto ret = formatMap_.insert(std::make_pair(key, data));
140     return ret.second;
141 }
142 
PutStringValue(const std::string_view & key,const std::string_view & value)143 bool Format::PutStringValue(const std::string_view &key, const std::string_view &value)
144 {
145     FormatData data;
146     data.type = FORMAT_TYPE_STRING;
147     data.stringVal = value;
148     auto ret = formatMap_.insert(std::make_pair(key, data));
149     return ret.second;
150 }
151 
GetStringValue(const std::string_view & key,std::string & value) const152 bool Format::GetStringValue(const std::string_view &key, std::string &value) const
153 {
154     auto iter = formatMap_.find(key);
155     if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_STRING) {
156         MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
157         return false;
158     }
159     value = iter->second.stringVal;
160     return true;
161 }
162 
GetIntValue(const std::string_view & key,int32_t & value) const163 bool Format::GetIntValue(const std::string_view &key, int32_t &value) const
164 {
165     auto iter = formatMap_.find(key);
166     if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_INT32) {
167         MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
168         return false;
169     }
170     value = iter->second.val.int32Val;
171     return true;
172 }
173 
GetLongValue(const std::string_view & key,int64_t & value) const174 bool Format::GetLongValue(const std::string_view &key, int64_t &value) const
175 {
176     auto iter = formatMap_.find(key);
177     if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_INT64) {
178         MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
179         return false;
180     }
181     value = iter->second.val.int64Val;
182     return true;
183 }
184 
GetFloatValue(const std::string_view & key,float & value) const185 bool Format::GetFloatValue(const std::string_view &key, float &value) const
186 {
187     auto iter = formatMap_.find(key);
188     if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_FLOAT) {
189         MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
190         return false;
191     }
192     value = iter->second.val.floatVal;
193     return true;
194 }
195 
GetDoubleValue(const std::string_view & key,double & value) const196 bool Format::GetDoubleValue(const std::string_view &key, double &value) const
197 {
198     auto iter = formatMap_.find(key);
199     if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_DOUBLE) {
200         MEDIA_LOGE("Format::GetFormat failed. Key: %{public}s", key.data());
201         return false;
202     }
203     value = iter->second.val.doubleVal;
204     return true;
205 }
206 
PutBuffer(const std::string_view & key,const uint8_t * addr,size_t size)207 bool Format::PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size)
208 {
209     constexpr size_t sizeMax = 1 * 1024 * 1024;
210     if (size > sizeMax) {
211         MEDIA_LOGE("PutBuffer input size failed. Key: %{public}s", key.data());
212         return false;
213     }
214 
215     FormatData data;
216     data.type = FORMAT_TYPE_ADDR;
217     data.addr = reinterpret_cast<uint8_t *>(malloc(size));
218     errno_t err = memcpy_s(reinterpret_cast<void *>(data.addr), size, reinterpret_cast<const void *>(addr), size);
219     if (err != EOK) {
220         MEDIA_LOGE("PutBuffer memcpy addr failed. Key: %{public}s", key.data());
221         return false;
222     }
223 
224     RemoveKey(key);
225 
226     data.size = size;
227     auto ret = formatMap_.insert(std::make_pair(key, data));
228     return ret.second;
229 }
230 
GetBuffer(const std::string_view & key,uint8_t ** addr,size_t & size) const231 bool Format::GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size) const
232 {
233     auto iter = formatMap_.find(key);
234     if (iter == formatMap_.end() || iter->second.type != FORMAT_TYPE_ADDR) {
235         MEDIA_LOGE("Format::GetBuffer failed. Key: %{public}s", key.data());
236         return false;
237     }
238     *addr = iter->second.addr;
239     size = iter->second.size;
240     return true;
241 }
242 
ContainKey(const std::string_view & key) const243 bool Format::ContainKey(const std::string_view &key) const
244 {
245     auto iter = formatMap_.find(key);
246     if (iter != formatMap_.end()) {
247         return true;
248     }
249 
250     return false;
251 }
252 
GetValueType(const std::string_view & key) const253 FormatDataType Format::GetValueType(const std::string_view &key) const
254 {
255     auto iter = formatMap_.find(key);
256     if (iter == formatMap_.end()) {
257         return FORMAT_TYPE_NONE;
258     }
259 
260     return iter->second.type;
261 }
262 
RemoveKey(const std::string_view & key)263 void Format::RemoveKey(const std::string_view &key)
264 {
265     auto iter = formatMap_.find(key);
266     if (iter != formatMap_.end()) {
267         if (iter->second.type == FORMAT_TYPE_ADDR && iter->second.addr != nullptr) {
268             free(iter->second.addr);
269             iter->second.addr = nullptr;
270         }
271         formatMap_.erase(iter);
272     }
273 }
274 
GetFormatMap() const275 const Format::FormatDataMap &Format::GetFormatMap() const
276 {
277     return formatMap_;
278 }
279 } // namespace Media
280 } // namespace OHOS