1 /*
2 * Copyright (c) 2021-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 "plugin_meta.h"
17
18 #include <cinttypes>
19 #include <cstring>
20 #include <memory>
21 #include <vector>
22
23 #include "plugin/common/plugin_audio_tags.h"
24 #include "utils/utils.h"
25
26 namespace OHOS {
27 namespace Media {
28 namespace Plugin {
29 using PointerPair = std::pair<std::shared_ptr<uint8_t>, size_t>;
~Meta()30 Meta::~Meta()
31 {
32 Clear();
33 }
34
Empty() const35 bool Meta::Empty() const
36 {
37 return items_.empty();
38 }
39
SetString(Plugin::MetaID id,const std::string & value)40 bool Meta::SetString(Plugin::MetaID id, const std::string& value)
41 {
42 return SetData<std::string>(id, value);
43 }
44
SetInt32(Plugin::MetaID id,int32_t value)45 bool Meta::SetInt32(Plugin::MetaID id, int32_t value)
46 {
47 return SetData<int32_t>(id, value);
48 }
49
SetUint32(Plugin::MetaID id,uint32_t value)50 bool Meta::SetUint32(Plugin::MetaID id, uint32_t value)
51 {
52 return SetData<uint32_t>(id, value);
53 }
54
SetInt64(Plugin::MetaID id,int64_t value)55 bool Meta::SetInt64(Plugin::MetaID id, int64_t value)
56 {
57 return SetData<int64_t>(id, value);
58 }
59
SetUint64(Plugin::MetaID id,uint64_t value)60 bool Meta::SetUint64(Plugin::MetaID id, uint64_t value)
61 {
62 return SetData<uint64_t>(id, value);
63 }
64
SetFloat(Plugin::MetaID id,float value)65 bool Meta::SetFloat(Plugin::MetaID id, float value)
66 {
67 return SetData<float>(id, value);
68 }
69
GetString(Plugin::MetaID id,std::string & value) const70 bool Meta::GetString(Plugin::MetaID id, std::string& value) const
71 {
72 auto ite = items_.find(id);
73 if (ite == items_.end()) {
74 return false;
75 }
76 if (ite->second.SameTypeWith(typeid(const char*))) {
77 value = Plugin::AnyCast<const char*>(ite->second);
78 } else if (ite->second.SameTypeWith(typeid(std::string))) {
79 value = Plugin::AnyCast<std::string>(ite->second);
80 } else if (ite->second.SameTypeWith(typeid(char*))) {
81 value = Plugin::AnyCast<char*>(ite->second);
82 } else {
83 return false;
84 }
85 return true;
86 }
87
GetInt32(Plugin::MetaID id,int32_t & value) const88 bool Meta::GetInt32(Plugin::MetaID id, int32_t& value) const
89 {
90 return GetData<int32_t>(id, value);
91 }
92
GetUint32(Plugin::MetaID id,uint32_t & value) const93 bool Meta::GetUint32(Plugin::MetaID id, uint32_t& value) const
94 {
95 return GetData<uint32_t>(id, value);
96 }
97
GetInt64(Plugin::MetaID id,int64_t & value) const98 bool Meta::GetInt64(Plugin::MetaID id, int64_t& value) const
99 {
100 return GetData<int64_t>(id, value);
101 }
102
GetUint64(Plugin::MetaID id,uint64_t & value) const103 bool Meta::GetUint64(Plugin::MetaID id, uint64_t& value) const
104 {
105 return GetData<uint64_t>(id, value);
106 }
107
GetFloat(Plugin::MetaID id,float & value) const108 bool Meta::GetFloat(Plugin::MetaID id, float& value) const
109 {
110 return GetData<float>(id, value);
111 }
112
Clear()113 void Meta::Clear()
114 {
115 items_.clear();
116 }
117
Remove(Plugin::MetaID id)118 bool Meta::Remove(Plugin::MetaID id)
119 {
120 auto ite = items_.find(id);
121 if (ite == items_.end()) {
122 return false;
123 }
124 items_.erase(ite);
125 return true;
126 }
127
Update(const Meta & meta)128 void Meta::Update(const Meta& meta)
129 {
130 for (auto& ptr : meta.items_) {
131 // we need to copy memory for pointers
132 if (ptr.second.SameTypeWith(typeid(PointerPair))) {
133 auto pointerPair = Plugin::AnyCast<PointerPair>(ptr.second);
134 SetPointer(ptr.first, pointerPair.first.get(), pointerPair.second);
135 } else {
136 items_[ptr.first] = ptr.second;
137 }
138 }
139 }
140
SetPointer(Plugin::MetaID id,const void * ptr,size_t size)141 bool Meta::SetPointer(Plugin::MetaID id, const void* ptr, size_t size) // NOLINT:void*
142 {
143 if (size == 0) {
144 return false;
145 }
146 auto tmp = new (std::nothrow) uint8_t[size];
147 if (tmp == nullptr) {
148 return false;
149 }
150 std::shared_ptr<uint8_t> savePtr(tmp, std::default_delete<uint8_t[]>());
151 if (memcpy_s(savePtr.get(), size, ptr, size) != EOK) {
152 delete[] tmp;
153 return false;
154 }
155 return SetData<std::pair<std::shared_ptr<uint8_t>, size_t>>(id, std::make_pair(savePtr, size));
156 }
157
GetPointer(Plugin::MetaID id,void ** ptr,size_t & size) const158 bool Meta::GetPointer(Plugin::MetaID id, void** ptr, size_t& size) const // NOLINT: void*
159 {
160 std::pair<std::shared_ptr<uint8_t>, size_t> item;
161 if (GetData<std::pair<std::shared_ptr<uint8_t>, size_t>>(id, item)) {
162 size = item.second;
163 if (size == 0) {
164 return false;
165 }
166 auto tmp = new (std::nothrow) uint8_t[size];
167 if (tmp == nullptr) {
168 return false;
169 }
170 if (memcpy_s(tmp, size, item.first.get(), size) != EOK) {
171 delete[] tmp;
172 return false;
173 }
174 *ptr = tmp;
175 return true;
176 } else {
177 return false;
178 }
179 }
180
GetMetaIDs() const181 std::vector<MetaID> Meta::GetMetaIDs() const
182 {
183 std::vector<MetaID> ret (items_.size());
184 int cnt = 0;
185 for (const auto& tmp : items_) {
186 ret[cnt++] = tmp.first;
187 }
188 return ret;
189 }
190 } // namespace Plugin
191 } // namespace Media
192 } // namespace OHOS
193