• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 namespace OHOS {
26 namespace Media {
27 namespace Plugin {
28 using PointerPair = std::pair<std::shared_ptr<uint8_t>, size_t>;
~Meta()29 Meta::~Meta()
30 {
31     Clear();
32 }
33 
Empty() const34 bool Meta::Empty() const
35 {
36     return items_.empty();
37 }
38 
SetString(Plugin::MetaID id,const std::string & value)39 bool Meta::SetString(Plugin::MetaID id, const std::string& value)
40 {
41     return SetData<std::string>(id, value);
42 }
43 
SetInt32(Plugin::MetaID id,int32_t value)44 bool Meta::SetInt32(Plugin::MetaID id, int32_t value)
45 {
46     return SetData<int32_t>(id, value);
47 }
48 
SetUint32(Plugin::MetaID id,uint32_t value)49 bool Meta::SetUint32(Plugin::MetaID id, uint32_t value)
50 {
51     return SetData<uint32_t>(id, value);
52 }
53 
SetInt64(Plugin::MetaID id,int64_t value)54 bool Meta::SetInt64(Plugin::MetaID id, int64_t value)
55 {
56     return SetData<int64_t>(id, value);
57 }
58 
SetUint64(Plugin::MetaID id,uint64_t value)59 bool Meta::SetUint64(Plugin::MetaID id, uint64_t value)
60 {
61     return SetData<uint64_t>(id, value);
62 }
63 
SetFloat(Plugin::MetaID id,float value)64 bool Meta::SetFloat(Plugin::MetaID id, float value)
65 {
66     return SetData<float>(id, value);
67 }
68 
GetString(Plugin::MetaID id,std::string & value) const69 bool Meta::GetString(Plugin::MetaID id, std::string& value) const
70 {
71     auto ite = items_.find(id);
72     if (ite == items_.end()) {
73         return false;
74     }
75     if (ite->second.SameTypeWith(typeid(const char*))) {
76         value = Plugin::AnyCast<const char*>(ite->second);
77     } else if (ite->second.SameTypeWith(typeid(std::string))) {
78         value = Plugin::AnyCast<std::string>(ite->second);
79     } else if (ite->second.SameTypeWith(typeid(char*))) {
80         value = Plugin::AnyCast<char*>(ite->second);
81     } else {
82         return false;
83     }
84     return true;
85 }
86 
GetInt32(Plugin::MetaID id,int32_t & value) const87 bool Meta::GetInt32(Plugin::MetaID id, int32_t& value) const
88 {
89     return GetData<int32_t>(id, value);
90 }
91 
GetUint32(Plugin::MetaID id,uint32_t & value) const92 bool Meta::GetUint32(Plugin::MetaID id, uint32_t& value) const
93 {
94     return GetData<uint32_t>(id, value);
95 }
96 
GetInt64(Plugin::MetaID id,int64_t & value) const97 bool Meta::GetInt64(Plugin::MetaID id, int64_t& value) const
98 {
99     return GetData<int64_t>(id, value);
100 }
101 
GetUint64(Plugin::MetaID id,uint64_t & value) const102 bool Meta::GetUint64(Plugin::MetaID id, uint64_t& value) const
103 {
104     return GetData<uint64_t>(id, value);
105 }
106 
GetFloat(Plugin::MetaID id,float & value) const107 bool Meta::GetFloat(Plugin::MetaID id, float& value) const
108 {
109     return GetData<float>(id, value);
110 }
111 
Clear()112 void Meta::Clear()
113 {
114     items_.clear();
115 }
116 
Remove(Plugin::MetaID id)117 bool Meta::Remove(Plugin::MetaID id)
118 {
119     auto ite = items_.find(id);
120     if (ite == items_.end()) {
121         return false;
122     }
123     items_.erase(ite);
124     return true;
125 }
126 
Update(const Meta & meta)127 void Meta::Update(const Meta& meta)
128 {
129     for (auto& ptr : meta.items_) {
130         // we need to copy memory for pointers
131         if (ptr.second.SameTypeWith(typeid(PointerPair))) {
132             auto pointerPair = Plugin::AnyCast<PointerPair>(ptr.second);
133             SetPointer(ptr.first, pointerPair.first.get(), pointerPair.second);
134         } else {
135             items_[ptr.first] = ptr.second;
136         }
137     }
138 }
139 
SetPointer(Plugin::MetaID id,const void * ptr,size_t size)140 bool Meta::SetPointer(Plugin::MetaID id, const void* ptr, size_t size) // NOLINT:void*
141 {
142     if (size == 0) {
143         return false;
144     }
145     std::shared_ptr<uint8_t> savePtr(new (std::nothrow) uint8_t[size], std::default_delete<uint8_t[]>());
146     if (!savePtr) {
147         return false;
148     }
149     if (memcpy_s(savePtr.get(), size, ptr, size) != EOK) {
150         return false;
151     }
152     return SetData<std::pair<std::shared_ptr<uint8_t>, size_t>>(id, std::make_pair(savePtr, size));
153 }
154 
GetPointer(Plugin::MetaID id,void ** ptr,size_t & size) const155 bool Meta::GetPointer(Plugin::MetaID id, void** ptr, size_t& size) const // NOLINT: void*
156 {
157     std::pair<std::shared_ptr<uint8_t>, size_t> item;
158     if (GetData<std::pair<std::shared_ptr<uint8_t>, size_t>>(id, item)) {
159         size = item.second;
160         if (size == 0) {
161             return false;
162         }
163         auto tmp = new (std::nothrow) uint8_t[size];
164         if (tmp == nullptr) {
165             return false;
166         }
167         if (memcpy_s(tmp, size, item.first.get(), size) != EOK) {
168             delete[] tmp;
169             return false;
170         }
171         *ptr = tmp;
172         return true;
173     } else {
174         return false;
175     }
176 }
177 
GetMetaIDs() const178 std::vector<MetaID> Meta::GetMetaIDs() const
179 {
180     std::vector<MetaID> ret (items_.size());
181     int cnt = 0;
182     for (const auto& tmp : items_) {
183         ret[cnt++] = tmp.first;
184     }
185     return ret;
186 }
187 } // namespace Plugin
188 } // namespace Media
189 } // namespace OHOS
190