1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#ifndef HISTREAMER_PLUGIN_TAG_VALUE_MAP_H
17fa7767c5Sopenharmony_ci#define HISTREAMER_PLUGIN_TAG_VALUE_MAP_H
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_ci#include "plugin/common/plugin_audio_tags.h"
20fa7767c5Sopenharmony_ci#include "plugin/common/plugin_source_tags.h"
21fa7767c5Sopenharmony_ci#include "plugin/common/plugin_tags.h"
22fa7767c5Sopenharmony_ci#include "plugin/common/plugin_types.h"
23fa7767c5Sopenharmony_ci#include "plugin/common/plugin_video_tags.h"
24fa7767c5Sopenharmony_ci#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
25fa7767c5Sopenharmony_ci#include "plugin/common/surface_allocator.h"
26fa7767c5Sopenharmony_ci#endif
27fa7767c5Sopenharmony_ci
28fa7767c5Sopenharmony_cinamespace OHOS {
29fa7767c5Sopenharmony_cinamespace Media {
30fa7767c5Sopenharmony_cinamespace Plugin {
31fa7767c5Sopenharmony_ci#define DEFINE_INSERT_GET_FUNC(condition, ValueType)             \
32fa7767c5Sopenharmony_ci        template<Tag tag>                                        \
33fa7767c5Sopenharmony_ci        inline typename std::enable_if<(condition), bool>::type  \
34fa7767c5Sopenharmony_ci        Set(ValueType value)                                     \
35fa7767c5Sopenharmony_ci        {                                                        \
36fa7767c5Sopenharmony_ci            auto iter = map_.find(tag);                          \
37fa7767c5Sopenharmony_ci            if (iter != map_.end()) {                            \
38fa7767c5Sopenharmony_ci                map_.erase(iter++);                              \
39fa7767c5Sopenharmony_ci            }                                                    \
40fa7767c5Sopenharmony_ci            map_.insert(std::make_pair(tag, value));             \
41fa7767c5Sopenharmony_ci            return true;                                         \
42fa7767c5Sopenharmony_ci        }                                                        \
43fa7767c5Sopenharmony_ci        template<Tag tag>                                        \
44fa7767c5Sopenharmony_ci        inline typename std::enable_if<(condition), bool>::type  \
45fa7767c5Sopenharmony_ci        Get(ValueType& value) const                              \
46fa7767c5Sopenharmony_ci        {                                                        \
47fa7767c5Sopenharmony_ci            if (map_.count(tag) == 0) {                          \
48fa7767c5Sopenharmony_ci                return false;                                    \
49fa7767c5Sopenharmony_ci            }                                                    \
50fa7767c5Sopenharmony_ci            return AnyCast<ValueType>(&map_.at(tag), value);     \
51fa7767c5Sopenharmony_ci        }
52fa7767c5Sopenharmony_ci
53fa7767c5Sopenharmony_ci
54fa7767c5Sopenharmony_ciusing MapIt = std::map<Tag, Any>::const_iterator;
55fa7767c5Sopenharmony_ciclass Meta {
56fa7767c5Sopenharmony_cipublic:
57fa7767c5Sopenharmony_ci#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
58fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::BUFFER_ALLOCATOR or tag == Tag::VIDEO_SURFACE,
59fa7767c5Sopenharmony_ci                           std::shared_ptr<SurfaceAllocator>);
60fa7767c5Sopenharmony_ci#endif
61fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::SRC_INPUT_TYPE, SrcInputType);
62fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::MEDIA_CODEC_CONFIG, std::vector<uint8_t>);
63fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(
64fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_CHANNEL_LAYOUT or
65fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_OUTPUT_CHANNEL_LAYOUT, AudioChannelLayout);
66fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::AUDIO_SAMPLE_FORMAT, AudioSampleFormat);
67fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::AUDIO_AAC_PROFILE, AudioAacProfile);
68fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::AUDIO_AAC_STREAM_FORMAT, AudioAacStreamFormat);
69fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::VIDEO_PIXEL_FORMAT, VideoPixelFormat);
70fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::MEDIA_SEEKABLE, Seekable);
71fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::MEDIA_TYPE, MediaType);
72fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::VIDEO_BIT_STREAM_FORMAT, std::vector<VideoBitStreamFormat>);
73fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(tag == Tag::VIDEO_H264_PROFILE, VideoH264Profile);
74fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(
75fa7767c5Sopenharmony_ci        tag == Tag::TRACK_ID or
76fa7767c5Sopenharmony_ci        tag == Tag::REQUIRED_OUT_BUFFER_CNT or
77fa7767c5Sopenharmony_ci        tag == Tag::BUFFERING_SIZE or
78fa7767c5Sopenharmony_ci        tag == Tag::WATERLINE_HIGH or
79fa7767c5Sopenharmony_ci        tag == Tag::WATERLINE_LOW or
80fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_CHANNELS or
81fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_SAMPLE_RATE or
82fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_SAMPLE_PER_FRAME or
83fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_OUTPUT_CHANNELS or
84fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_MPEG_VERSION or
85fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_MPEG_LAYER or
86fa7767c5Sopenharmony_ci        tag == Tag::AUDIO_AAC_LEVEL or
87fa7767c5Sopenharmony_ci        tag == Tag::VIDEO_WIDTH or
88fa7767c5Sopenharmony_ci        tag == Tag::VIDEO_HEIGHT or
89fa7767c5Sopenharmony_ci        tag == Tag::VIDEO_FRAME_RATE or
90fa7767c5Sopenharmony_ci        tag == Tag::VIDEO_MAX_SURFACE_NUM or
91fa7767c5Sopenharmony_ci        tag == Tag::VIDEO_H264_LEVEL or
92fa7767c5Sopenharmony_ci        tag == Tag::BITS_PER_CODED_SAMPLE or
93fa7767c5Sopenharmony_ci        tag == Tag::USER_FRAME_NUMBER, uint32_t);
94fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(
95fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_DURATION or
96fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_BITRATE or
97fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_START_TIME or
98fa7767c5Sopenharmony_ci        tag == Tag::USER_FRAME_PTS or
99fa7767c5Sopenharmony_ci        tag == Tag::USER_PUSH_DATA_TIME or
100fa7767c5Sopenharmony_ci        tag == Tag::USER_PUSH_DATA_TIME or
101fa7767c5Sopenharmony_ci        tag == Tag::SUBTITLE_PTS or
102fa7767c5Sopenharmony_ci        tag == Tag::SUBTITLE_DURATION, int64_t);
103fa7767c5Sopenharmony_ci
104fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(
105fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_FILE_SIZE or
106fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_POSITION, uint64_t);
107fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(
108fa7767c5Sopenharmony_ci        tag == Tag::VIDEO_CAPTURE_RATE, double);
109fa7767c5Sopenharmony_ci    DEFINE_INSERT_GET_FUNC(
110fa7767c5Sopenharmony_ci        tag == Tag::MIME or
111fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_FILE_URI or
112fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_TITLE or
113fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_ARTIST or
114fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_LYRICIST or
115fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_ALBUM or
116fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_ALBUM_ARTIST or
117fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_DATE or
118fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_COMMENT or
119fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_GENRE or
120fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_COPYRIGHT or
121fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_LANGUAGE or
122fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_DESCRIPTION or
123fa7767c5Sopenharmony_ci        tag == Tag::USER_TIME_SYNC_RESULT or
124fa7767c5Sopenharmony_ci        tag == Tag::USER_AV_SYNC_GROUP_INFO or
125fa7767c5Sopenharmony_ci        tag == Tag::USER_SHARED_MEMORY_FD or
126fa7767c5Sopenharmony_ci        tag == Tag::MEDIA_LYRICS or
127fa7767c5Sopenharmony_ci        tag == Tag::SUBTITLE_TEXT, std::string);
128fa7767c5Sopenharmony_ci    Meta& operator=(const Meta& other)
129fa7767c5Sopenharmony_ci    {
130fa7767c5Sopenharmony_ci        map_ = other.map_;
131fa7767c5Sopenharmony_ci        return *this;
132fa7767c5Sopenharmony_ci    }
133fa7767c5Sopenharmony_ci
134fa7767c5Sopenharmony_ci    Meta() {
135fa7767c5Sopenharmony_ci    };
136fa7767c5Sopenharmony_ci
137fa7767c5Sopenharmony_ci    Meta(const Meta& other)
138fa7767c5Sopenharmony_ci    {
139fa7767c5Sopenharmony_ci        map_ = other.map_;
140fa7767c5Sopenharmony_ci    }
141fa7767c5Sopenharmony_ci
142fa7767c5Sopenharmony_ci    ValueType& operator[](const Tag &tag)
143fa7767c5Sopenharmony_ci    {
144fa7767c5Sopenharmony_ci        return map_[tag];
145fa7767c5Sopenharmony_ci    }
146fa7767c5Sopenharmony_ci
147fa7767c5Sopenharmony_ci    MapIt begin() const // to support for (auto e : Meta), must use begin/end name
148fa7767c5Sopenharmony_ci    {
149fa7767c5Sopenharmony_ci        return map_.cbegin();
150fa7767c5Sopenharmony_ci    }
151fa7767c5Sopenharmony_ci
152fa7767c5Sopenharmony_ci    MapIt end() const
153fa7767c5Sopenharmony_ci    {
154fa7767c5Sopenharmony_ci        return map_.cend();
155fa7767c5Sopenharmony_ci    }
156fa7767c5Sopenharmony_ci
157fa7767c5Sopenharmony_ci    void Clear()
158fa7767c5Sopenharmony_ci    {
159fa7767c5Sopenharmony_ci        map_.clear();
160fa7767c5Sopenharmony_ci    }
161fa7767c5Sopenharmony_ci
162fa7767c5Sopenharmony_ci    MapIt Find(Tag tag) const
163fa7767c5Sopenharmony_ci    {
164fa7767c5Sopenharmony_ci        return map_.find(tag);
165fa7767c5Sopenharmony_ci    }
166fa7767c5Sopenharmony_ci
167fa7767c5Sopenharmony_ci    bool Empty() const
168fa7767c5Sopenharmony_ci    {
169fa7767c5Sopenharmony_ci        return map_.empty();
170fa7767c5Sopenharmony_ci    }
171fa7767c5Sopenharmony_ci
172fa7767c5Sopenharmony_ci    template <typename T>
173fa7767c5Sopenharmony_ci    void SetData(Plugin::Tag id, const T& value)
174fa7767c5Sopenharmony_ci    {
175fa7767c5Sopenharmony_ci        map_[id] = value;
176fa7767c5Sopenharmony_ci    }
177fa7767c5Sopenharmony_ci
178fa7767c5Sopenharmony_ci    template <typename T>
179fa7767c5Sopenharmony_ci    bool GetData(Plugin::Tag id, T& value) const
180fa7767c5Sopenharmony_ci    {
181fa7767c5Sopenharmony_ci        auto ite = map_.find(id);
182fa7767c5Sopenharmony_ci        if (ite == map_.end() || !Plugin::Any::IsSameTypeWith<T>(ite->second)) {
183fa7767c5Sopenharmony_ci            return false;
184fa7767c5Sopenharmony_ci        }
185fa7767c5Sopenharmony_ci        value = Plugin::AnyCast<T>(ite->second);
186fa7767c5Sopenharmony_ci        return true;
187fa7767c5Sopenharmony_ci    }
188fa7767c5Sopenharmony_ci
189fa7767c5Sopenharmony_ci    bool GetData(Plugin::Tag id, Plugin::ValueType& value) const
190fa7767c5Sopenharmony_ci    {
191fa7767c5Sopenharmony_ci        auto ite = map_.find(id);
192fa7767c5Sopenharmony_ci        if (ite == map_.end()) {
193fa7767c5Sopenharmony_ci            return false;
194fa7767c5Sopenharmony_ci        }
195fa7767c5Sopenharmony_ci        value = ite->second;
196fa7767c5Sopenharmony_ci        return true;
197fa7767c5Sopenharmony_ci    }
198fa7767c5Sopenharmony_ci
199fa7767c5Sopenharmony_ci    void Remove(Plugin::Tag id)
200fa7767c5Sopenharmony_ci    {
201fa7767c5Sopenharmony_ci        auto ite = map_.find(id);
202fa7767c5Sopenharmony_ci        if (ite != map_.end()) {
203fa7767c5Sopenharmony_ci            map_.erase(ite);
204fa7767c5Sopenharmony_ci        }
205fa7767c5Sopenharmony_ci    }
206fa7767c5Sopenharmony_ci
207fa7767c5Sopenharmony_ci    void GetKeys(std::vector<Tag>& keys) const
208fa7767c5Sopenharmony_ci    {
209fa7767c5Sopenharmony_ci        int cnt = 0;
210fa7767c5Sopenharmony_ci        keys.resize(map_.size());
211fa7767c5Sopenharmony_ci        for (const auto& tmp : map_) {
212fa7767c5Sopenharmony_ci            keys[cnt++] = tmp.first;
213fa7767c5Sopenharmony_ci        }
214fa7767c5Sopenharmony_ci    }
215fa7767c5Sopenharmony_ciprivate:
216fa7767c5Sopenharmony_ci    std::map<Tag, Any> map_;
217fa7767c5Sopenharmony_ci};
218fa7767c5Sopenharmony_ci} // namespace Plugin
219fa7767c5Sopenharmony_ci} // namespace Media
220fa7767c5Sopenharmony_ci} // namespace OHOS
221fa7767c5Sopenharmony_ci#endif // HISTREAMER_PLUGIN_TAG_VALUE_MAP_H
222