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_PIPELINE_FILTER_FACTORY_H
17 #define HISTREAMER_PIPELINE_FILTER_FACTORY_H
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "pipeline/core/filter.h"
25 #include "plugin/common/type_cast_ext.h"
26 
27 namespace OHOS {
28 namespace Media {
29 using InstanceGenerator = std::function<std::shared_ptr<Pipeline::Filter>(const std::string&)>;
30 
31 class FilterFactory {
32 public:
33     ~FilterFactory() = default;
34 
35     FilterFactory(const FilterFactory&) = delete;
36 
37     FilterFactory operator=(const FilterFactory&) = delete;
38 
39     static FilterFactory& Instance();
40 
41     void Init();
42 
43     void RegisterGenerator(const std::string& name, const InstanceGenerator& generator);
44 
45     std::shared_ptr<Pipeline::Filter> CreateFilter(const std::string& filterName, const std::string& aliasName);
46 
47     template <typename T>
CreateFilterWithType(const std::string & filterName,const std::string & aliasName)48     std::shared_ptr<T> CreateFilterWithType(const std::string& filterName, const std::string& aliasName)
49     {
50         auto filter = CreateFilter(filterName, aliasName);
51         auto typedFilter = Plugin::ReinterpretPointerCast<T>(filter);
52         return typedFilter;
53     }
54 
55     template <typename T>
RegisterFilter(const std::string & name)56     void RegisterFilter(const std::string& name)
57     {
58         RegisterGenerator(name, [](const std::string& aliaName) { return std::make_shared<T>(aliaName); });
59     }
60 
61 private:
62     FilterFactory() = default;
63 
64     std::unordered_map<std::string, InstanceGenerator> generators;
65 };
66 
67 template <typename T>
68 class AutoRegisterFilter {
69 public:
AutoRegisterFilter(const std::string & name)70     explicit AutoRegisterFilter(const std::string& name)
71     {
72         FilterFactory::Instance().RegisterFilter<T>(name);
73     }
74 
AutoRegisterFilter(const std::string & name,const InstanceGenerator & generator)75     AutoRegisterFilter(const std::string& name, const InstanceGenerator& generator)
76     {
77         FilterFactory::Instance().RegisterGenerator(name, generator);
78     }
79 
80     ~AutoRegisterFilter() = default;
81 };
82 } // namespace Media
83 } // namespace OHOS
84 #endif // HISTREAMER_PIPELINE_FILTER_FACTORY_H
85