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 #ifndef HISTREAMER_PLUGIN_META_H
17 #define HISTREAMER_PLUGIN_META_H
18
19 #include <vector>
20
21 #include "common/plugin_tags.h"
22 #include "common/plugin_types.h"
23
24 namespace OHOS {
25 namespace Media {
26 namespace Plugin {
27 template <typename E>
to_underlying(E e)28 constexpr typename std::underlying_type<E>::type to_underlying(E e) noexcept
29 {
30 return static_cast<typename std::underlying_type<E>::type>(e);
31 }
32
33 /**
34 * Meta ID is used to describe the metadata of media files or data streams.
35 * All Meta ID must come from Tag.
36 *
37 * For details about the definition and usage, to see enum Tag in file plugin_tags.h.
38 */
39 enum struct MetaID : uint32_t {
40 MIME = to_underlying(Tag::MIME),
41 TRACK_ID = to_underlying(Tag::TRACK_ID),
42 MEDIA_CODEC_CONFIG = to_underlying(Tag::MEDIA_CODEC_CONFIG),
43
44 AUDIO_CHANNELS = to_underlying(Tag::AUDIO_CHANNELS),
45 AUDIO_SAMPLE_RATE = to_underlying(Tag::AUDIO_SAMPLE_RATE),
46 AUDIO_SAMPLE_FORMAT = to_underlying(Tag::AUDIO_SAMPLE_FORMAT),
47 AUDIO_SAMPLE_PER_FRAME = to_underlying(Tag::AUDIO_SAMPLE_PER_FRAME),
48 AUDIO_CHANNEL_LAYOUT = to_underlying(Tag::AUDIO_CHANNEL_LAYOUT),
49
50 MEDIA_TITLE = to_underlying(Tag::MEDIA_TITLE),
51 MEDIA_ARTIST = to_underlying(Tag::MEDIA_ARTIST),
52 MEDIA_LYRICIST = to_underlying(Tag::MEDIA_LYRICIST),
53 MEDIA_ALBUM = to_underlying(Tag::MEDIA_ALBUM),
54 MEDIA_ALBUM_ARTIST = to_underlying(Tag::MEDIA_ALBUM_ARTIST),
55 MEDIA_DATE = to_underlying(Tag::MEDIA_DATE),
56 MEDIA_COMMENT = to_underlying(Tag::MEDIA_COMMENT),
57 MEDIA_GENRE = to_underlying(Tag::MEDIA_GENRE),
58 MEDIA_DESCRIPTION = to_underlying(Tag::MEDIA_DESCRIPTION),
59 MEDIA_COPYRIGHT = to_underlying(Tag::MEDIA_COPYRIGHT),
60 MEDIA_LANGUAGE = to_underlying(Tag::MEDIA_LANGUAGE),
61 MEDIA_LYRICS = to_underlying(Tag::MEDIA_LYRICS),
62 MEDIA_DURATION = to_underlying(Tag::MEDIA_DURATION),
63 MEDIA_BITRATE = to_underlying(Tag::MEDIA_BITRATE),
64 MEDIA_FILE_EXTENSION = to_underlying(Tag::MEDIA_FILE_EXTENSION),
65 MEDIA_FILE_SIZE = to_underlying(Tag::MEDIA_FILE_SIZE),
66
67 AUDIO_MPEG_VERSION = to_underlying(Tag::AUDIO_MPEG_VERSION),
68 AUDIO_MPEG_LAYER = to_underlying(Tag::AUDIO_MPEG_LAYER),
69 AUDIO_AAC_PROFILE = to_underlying(Tag::AUDIO_AAC_PROFILE),
70 AUDIO_AAC_LEVEL = to_underlying(Tag::AUDIO_AAC_LEVEL),
71 AUDIO_AAC_STREAM_FORMAT = to_underlying(Tag::AUDIO_AAC_STREAM_FORMAT),
72
73 VIDEO_WIDTH = to_underlying(Tag::VIDEO_WIDTH),
74 VIDEO_HEIGHT = to_underlying(Tag::VIDEO_HEIGHT),
75 VIDEO_PIXEL_FORMAT = to_underlying(Tag::VIDEO_PIXEL_FORMAT),
76 };
77
78 class Meta {
79 public:
80 explicit Meta() = default;
81 ~Meta();
82 bool Empty() const;
83 bool SetString(Plugin::MetaID id, const std::string& value);
84 bool SetInt32(Plugin::MetaID id, int32_t value);
85 bool SetUint32(Plugin::MetaID id, uint32_t value);
86 bool SetInt64(Plugin::MetaID id, int64_t value);
87 bool SetUint64(Plugin::MetaID id, uint64_t value);
88 bool SetFloat(Plugin::MetaID id, float value);
89
90 /**
91 * this function will copy from ptr with size.
92 * @param ptr pointer
93 * @param size size
94 * @return
95 */
96 bool SetPointer(Plugin::MetaID, const void* ptr, size_t size); // NOLINT: void*
97
98 bool GetString(Plugin::MetaID id, std::string& value) const;
99 bool GetInt32(Plugin::MetaID id, int32_t& value) const;
100 bool GetUint32(Plugin::MetaID id, uint32_t& value) const;
101 bool GetInt64(Plugin::MetaID id, int64_t& value) const;
102 bool GetUint64(Plugin::MetaID id, uint64_t& value) const;
103 bool GetFloat(Plugin::MetaID id, float& value) const;
104
105 /**
106 * this function will copy from inner storage with size if exists. The user should delete the memory when it's not
107 * needed.
108 * @param ptr pointer
109 * @param size size
110 * @return
111 */
112 bool GetPointer(Plugin::MetaID, void** ptr, size_t& size) const; // NOLINT: void*
113
114 template <typename T>
GetData(Plugin::MetaID id,T & value)115 bool GetData(Plugin::MetaID id, T& value) const
116 {
117 auto ite = items_.find(id);
118 if (ite == items_.end() || !ite->second.SameTypeWith(typeid(T))) {
119 return false;
120 }
121 value = Plugin::AnyCast<T>(ite->second);
122 return true;
123 }
124
GetData(Plugin::MetaID id,Plugin::ValueType & value)125 bool GetData(Plugin::MetaID id, Plugin::ValueType& value) const
126 {
127 auto ite = items_.find(id);
128 if (ite == items_.end()) {
129 return false;
130 }
131 value = ite->second;
132 return true;
133 }
134
GetData(MetaID id)135 const Plugin::ValueType* GetData(MetaID id) const
136 {
137 auto ite = items_.find(id);
138 if (ite == items_.end()) {
139 return nullptr;
140 }
141 return &(ite->second);
142 }
143
144 void Clear();
145
146 /**
147 * remove item with the input name
148 * @param name target name
149 * @return true if the target exists; or false.
150 */
151 bool Remove(Plugin::MetaID id);
152
153 void Update(const Meta& meta);
154
155 /**
156 * user should always use like SetData<T> to set data, otherwise it may be wrong to GetData.
157 * e.g. for string if use SetData(id, "abc") then GetData<std::string>(id, value) cannot work.
158 * for uint64 if use SetData(id, 1), then GetData<uint64>(id, value) cannot work.
159 *
160 * @tparam T
161 * @param id
162 * @param value
163 * @return
164 */
165 template <typename T>
SetData(Plugin::MetaID id,const T & value)166 bool SetData(Plugin::MetaID id, const T& value)
167 {
168 items_[id] = value;
169 return true;
170 }
171
SetData(Plugin::MetaID id,const Plugin::ValueType & value)172 bool SetData(Plugin::MetaID id, const Plugin::ValueType& value)
173 {
174 items_[id] = value;
175 return true;
176 }
177
178 std::vector<MetaID> GetMetaIDs() const;
179
180 private:
181 std::map<MetaID, Plugin::ValueType> items_ {};
182 };
183 } // namespace Plugin
184 } // namespace Media
185 } // namespace OHOS
186 #endif // HISTREAMER_PLUGIN_META_H
187