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