• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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_TAG_VALUE_MAP_H
17 #define HISTREAMER_PLUGIN_TAG_VALUE_MAP_H
18 
19 #include "plugin/common/plugin_audio_tags.h"
20 #include "plugin/common/plugin_source_tags.h"
21 #include "plugin/common/plugin_tags.h"
22 #include "plugin/common/plugin_types.h"
23 #include "plugin/common/plugin_video_tags.h"
24 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
25 #include "plugin/common/surface_allocator.h"
26 #endif
27 
28 namespace OHOS {
29 namespace Media {
30 namespace Plugin {
31 #define DEFINE_INSERT_GET_FUNC(condition, ValueType)             \
32         template<Tag tag>                                        \
33         inline typename std::enable_if<(condition), bool>::type  \
34         Set(ValueType value)                                     \
35         {                                                        \
36             auto iter = map_.find(tag);                          \
37             if (iter != map_.end()) {                            \
38                 map_.erase(iter++);                              \
39             }                                                    \
40             map_.insert(std::make_pair(tag, value));             \
41             return true;                                         \
42         }                                                        \
43         template<Tag tag>                                        \
44         inline typename std::enable_if<(condition), bool>::type  \
45         Get(ValueType& value) const                              \
46         {                                                        \
47             if (map_.count(tag) == 0) {                          \
48                 return false;                                    \
49             }                                                    \
50             return AnyCast<ValueType>(&map_.at(tag), value);     \
51         }
52 
53 
54 using MapIt = std::map<Tag, Any>::const_iterator;
55 class Meta {
56 public:
57 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
58     DEFINE_INSERT_GET_FUNC(tag == Tag::BUFFER_ALLOCATOR or tag == Tag::VIDEO_SURFACE,
59                            std::shared_ptr<SurfaceAllocator>);
60 #endif
61     DEFINE_INSERT_GET_FUNC(tag == Tag::SRC_INPUT_TYPE, SrcInputType);
62     DEFINE_INSERT_GET_FUNC(tag == Tag::MEDIA_CODEC_CONFIG, std::vector<uint8_t>);
63     DEFINE_INSERT_GET_FUNC(
64         tag == Tag::AUDIO_CHANNEL_LAYOUT or
65         tag == Tag::AUDIO_OUTPUT_CHANNEL_LAYOUT, AudioChannelLayout);
66     DEFINE_INSERT_GET_FUNC(tag == Tag::AUDIO_SAMPLE_FORMAT, AudioSampleFormat);
67     DEFINE_INSERT_GET_FUNC(tag == Tag::AUDIO_AAC_PROFILE, AudioAacProfile);
68     DEFINE_INSERT_GET_FUNC(tag == Tag::AUDIO_AAC_STREAM_FORMAT, AudioAacStreamFormat);
69     DEFINE_INSERT_GET_FUNC(tag == Tag::VIDEO_PIXEL_FORMAT, VideoPixelFormat);
70     DEFINE_INSERT_GET_FUNC(tag == Tag::MEDIA_SEEKABLE, Seekable);
71     DEFINE_INSERT_GET_FUNC(tag == Tag::MEDIA_TYPE, MediaType);
72     DEFINE_INSERT_GET_FUNC(tag == Tag::VIDEO_BIT_STREAM_FORMAT, std::vector<VideoBitStreamFormat>);
73     DEFINE_INSERT_GET_FUNC(tag == Tag::VIDEO_H264_PROFILE, VideoH264Profile);
74     DEFINE_INSERT_GET_FUNC(
75         tag == Tag::TRACK_ID or
76         tag == Tag::REQUIRED_OUT_BUFFER_CNT or
77         tag == Tag::BUFFERING_SIZE or
78         tag == Tag::WATERLINE_HIGH or
79         tag == Tag::WATERLINE_LOW or
80         tag == Tag::AUDIO_CHANNELS or
81         tag == Tag::AUDIO_SAMPLE_RATE or
82         tag == Tag::AUDIO_SAMPLE_PER_FRAME or
83         tag == Tag::AUDIO_OUTPUT_CHANNELS or
84         tag == Tag::AUDIO_MPEG_VERSION or
85         tag == Tag::AUDIO_MPEG_LAYER or
86         tag == Tag::AUDIO_AAC_LEVEL or
87         tag == Tag::VIDEO_WIDTH or
88         tag == Tag::VIDEO_HEIGHT or
89         tag == Tag::VIDEO_FRAME_RATE or
90         tag == Tag::VIDEO_MAX_SURFACE_NUM or
91         tag == Tag::VIDEO_H264_LEVEL or
92         tag == Tag::BITS_PER_CODED_SAMPLE or
93         tag == Tag::USER_FRAME_NUMBER, uint32_t);
94     DEFINE_INSERT_GET_FUNC(
95         tag == Tag::MEDIA_DURATION or
96         tag == Tag::MEDIA_BITRATE or
97         tag == Tag::MEDIA_START_TIME or
98         tag == Tag::USER_FRAME_PTS or
99         tag == Tag::USER_PUSH_DATA_TIME, int64_t);
100 
101     DEFINE_INSERT_GET_FUNC(
102         tag == Tag::MEDIA_FILE_SIZE or
103         tag == Tag::MEDIA_POSITION, uint64_t);
104     DEFINE_INSERT_GET_FUNC(
105         tag == Tag::VIDEO_CAPTURE_RATE, double);
106     DEFINE_INSERT_GET_FUNC(
107         tag == Tag::MIME or
108         tag == Tag::MEDIA_FILE_URI or
109         tag == Tag::MEDIA_TITLE or
110         tag == Tag::MEDIA_ARTIST or
111         tag == Tag::MEDIA_LYRICIST or
112         tag == Tag::MEDIA_ALBUM or
113         tag == Tag::MEDIA_ALBUM_ARTIST or
114         tag == Tag::MEDIA_DATE or
115         tag == Tag::MEDIA_COMMENT or
116         tag == Tag::MEDIA_GENRE or
117         tag == Tag::MEDIA_COPYRIGHT or
118         tag == Tag::MEDIA_LANGUAGE or
119         tag == Tag::MEDIA_DESCRIPTION or
120         tag == Tag::USER_TIME_SYNC_RESULT or
121         tag == Tag::USER_AV_SYNC_GROUP_INFO or
122         tag == Tag::USER_SHARED_MEMORY_FD or
123         tag == Tag::MEDIA_LYRICS, std::string);
124     Meta& operator=(const Meta& other)
125     {
126         map_ = other.map_;
127         return *this;
128     }
129 
Meta()130     Meta() {
131     };
132 
Meta(const Meta & other)133     Meta(const Meta& other)
134     {
135         map_ = other.map_;
136     }
137 
138     ValueType& operator[](const Tag &tag)
139     {
140         return map_[tag];
141     }
142 
begin()143     MapIt begin() const // to support for (auto e : Meta), must use begin/end name
144     {
145         return map_.cbegin();
146     }
147 
end()148     MapIt end() const
149     {
150         return map_.cend();
151     }
152 
Clear()153     void Clear()
154     {
155         map_.clear();
156     }
157 
Find(Tag tag)158     MapIt Find(Tag tag) const
159     {
160         return map_.find(tag);
161     }
162 
Empty()163     bool Empty() const
164     {
165         return map_.empty();
166     }
167 
168     template <typename T>
SetData(Plugin::Tag id,const T & value)169     void SetData(Plugin::Tag id, const T& value)
170     {
171         map_[id] = value;
172     }
173 
174     template <typename T>
GetData(Plugin::Tag id,T & value)175     bool GetData(Plugin::Tag id, T& value) const
176     {
177         auto ite = map_.find(id);
178         if (ite == map_.end() || !Plugin::Any::IsSameTypeWith<T>(ite->second)) {
179             return false;
180         }
181         value = Plugin::AnyCast<T>(ite->second);
182         return true;
183     }
184 
GetData(Plugin::Tag id,Plugin::ValueType & value)185     bool GetData(Plugin::Tag id, Plugin::ValueType& value) const
186     {
187         auto ite = map_.find(id);
188         if (ite == map_.end()) {
189             return false;
190         }
191         value = ite->second;
192         return true;
193     }
194 
Remove(Plugin::Tag id)195     void Remove(Plugin::Tag id)
196     {
197         auto ite = map_.find(id);
198         if (ite != map_.end()) {
199             map_.erase(ite);
200         }
201     }
202 
GetKeys(std::vector<Tag> & keys)203     void GetKeys(std::vector<Tag>& keys) const
204     {
205         int cnt = 0;
206         keys.resize(map_.size());
207         for (const auto& tmp : map_) {
208             keys[cnt++] = tmp.first;
209         }
210     }
211 private:
212     std::map<Tag, Any> map_;
213 };
214 } // namespace Plugin
215 } // namespace Media
216 } // namespace OHOS
217 #endif // HISTREAMER_PLUGIN_TAG_VALUE_MAP_H
218