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_COMMON_CAPS_BUILDER_H
17#define HISTREAMER_PLUGIN_COMMON_CAPS_BUILDER_H
18#include <string>
19#include <vector>
20#include "plugin_audio_tags.h"
21#include "inner_api/plugin/plugin_caps.h"
22#include "plugin_video_tags.h"
23namespace OHOS {
24namespace Media {
25namespace Plugins {
26/**
27 * @brief CapabilityBuilder is used for easily building of Capability.
28 *
29 * CapabilityBuilder offers several setter functions for Capability::Key, including bit rate, sample format, audio
30 * channels etc. Utilize this builder, user do not need to explicitly specify detail types of keys. After setting the
31 * keys, call Build() to receive an available Capability. Calling Reset() will clear the keys and mime which have been
32 * set before.
33 *
34 * @since 1.0
35 * @version 1.0
36 */
37class CapabilityBuilder {
38#define DECL_SET_FIX_CAP_FUNC(key, keyName, type) \
39CapabilityBuilder& Set##keyName(type val) \
40{ \
41    cap_.keys[key] = val; \
42    return *this; \
43}
44
45#define DECL_SET_RNG_CAP_FUNC(key, keyName, type) \
46CapabilityBuilder& Set##keyName##Range(type val1, type val2) \
47{ \
48    auto min = std::min(val1, val2); \
49    auto max = std::max(val1, val2); \
50    cap_.keys[key] = std::make_pair(min, max); \
51    return *this; \
52}
53
54#define DECL_SET_LIST_CAP_FUNC(key, keyName, type) \
55CapabilityBuilder& Set##keyName##List(std::vector<type> val) \
56{ \
57    cap_.keys[key] = val; \
58    return *this; \
59}
60
61#define DECL_SET_FRL_CAP_FUNCS(key, keyName, type) \
62DECL_SET_FIX_CAP_FUNC(key, keyName, type); \
63DECL_SET_RNG_CAP_FUNC(key, keyName, type); \
64DECL_SET_LIST_CAP_FUNC(key, keyName, type)
65
66#define DECL_SET_FL_CAP_FUNCS(key, keyName, type) \
67DECL_SET_FIX_CAP_FUNC(key, keyName, type); \
68DECL_SET_LIST_CAP_FUNC(key, keyName, type)
69
70public:
71    CapabilityBuilder() = default;
72
73    /**
74     * set mime
75     *
76     * @param mime mime of this builder.
77     * @return CapabilityBuilder it self.
78     */
79    CapabilityBuilder& SetMime(std::string mime)
80    {
81        cap_.mime = std::move(mime);
82        return *this;
83    }
84
85    DECL_SET_FRL_CAP_FUNCS(Capability::Key::MEDIA_BITRATE, BitRate, uint32_t);
86
87    DECL_SET_FRL_CAP_FUNCS(Capability::Key::AUDIO_SAMPLE_RATE, AudioSampleRate, uint32_t);
88
89    DECL_SET_FRL_CAP_FUNCS(Capability::Key::AUDIO_CHANNELS, AudioChannel, uint32_t);
90
91    DECL_SET_FL_CAP_FUNCS(Capability::Key::AUDIO_CHANNEL_LAYOUT, AudioChannelLayout, AudioChannelLayout);
92
93    DECL_SET_FL_CAP_FUNCS(Capability::Key::AUDIO_SAMPLE_FORMAT, AudioSampleFormat, AudioSampleFormat);
94
95    DECL_SET_FRL_CAP_FUNCS(Capability::Key::AUDIO_MPEG_VERSION, AudioMpegVersion, uint32_t);
96    DECL_SET_FRL_CAP_FUNCS(Capability::Key::AUDIO_MPEG_LAYER, AudioMpegLayer, uint32_t);
97
98    DECL_SET_FL_CAP_FUNCS(Capability::Key::AUDIO_AAC_PROFILE, AudioAacProfile, AudioAacProfile);
99
100    DECL_SET_FRL_CAP_FUNCS(Capability::Key::AUDIO_AAC_LEVEL, AudioAacLevel, uint32_t);
101
102    DECL_SET_FL_CAP_FUNCS(Capability::Key::AUDIO_AAC_STREAM_FORMAT, AudioAacStreamFormat, AudioAacStreamFormat);
103
104    DECL_SET_FL_CAP_FUNCS(Capability::Key::VIDEO_PIXEL_FORMAT, VideoPixelFormat, VideoPixelFormat);
105
106    DECL_SET_FL_CAP_FUNCS(Capability::Key::VIDEO_BIT_STREAM_FORMAT, VideoBitStreamFormat, VideoBitStreamFormat);
107
108    /**
109     *  @brief Build one Capability.
110     *
111     * @return capability
112     */
113    const Capability& Build() const
114    {
115        return cap_;
116    }
117
118    /**
119     * Reset the keys and mime which have been set already.
120     */
121    void Reset()
122    {
123        cap_.mime.clear();
124        cap_.keys.clear();
125    }
126private:
127    Capability cap_;
128#undef DECL_SET_FRL_CAP_FUNCS
129#undef DECL_SET_FL_CAP_FUNCS
130#undef DECL_SET_FIX_CAP_FUNC
131#undef DECL_SET_RNG_CAP_FUNC
132#undef DECL_SET_LIST_CAP_FUNC
133};
134} // Plugin
135} // Media
136} // OHOS
137#endif // HISTREAMER_PLUGIN_COMMON_CAPS_BUILDER_H
138