1 /* 2 * Copyright (c) 2023-2023 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 "filter/filter.h" 25 #include "cpp_ext/type_cast_ext.h" 26 27 namespace OHOS { 28 namespace Media { 29 namespace Pipeline { 30 using InstanceGenerator = std::function<std::shared_ptr<Filter>(const std::string&, const FilterType type)>; 31 32 class FilterFactory { 33 public: 34 ~FilterFactory() = default; 35 36 FilterFactory(const FilterFactory&) = delete; 37 38 FilterFactory operator=(const FilterFactory&) = delete; 39 40 static FilterFactory& Instance(); 41 42 template <typename T> CreateFilter(const std::string & filterName,const FilterType type)43 std::shared_ptr<T> CreateFilter(const std::string& filterName, const FilterType type) 44 { 45 auto filter = CreateFilterPriv(filterName, type); 46 auto typedFilter = ReinterpretPointerCast<T>(filter); 47 return typedFilter; 48 } 49 50 template <typename T> 51 void RegisterFilter(const std::string& name, const FilterType type, const InstanceGenerator& generator = nullptr) 52 { 53 RegisterFilterPriv<T>(name, type, generator); 54 } 55 56 private: 57 FilterFactory() = default; 58 59 std::shared_ptr<Filter> CreateFilterPriv(const std::string& filterName, const FilterType type); 60 61 template <typename T> RegisterFilterPriv(const std::string & name,const FilterType type,const InstanceGenerator & generator)62 void RegisterFilterPriv(const std::string& name, const FilterType type, const InstanceGenerator& generator) 63 { 64 if (generator == nullptr) { 65 auto result = generators.emplace( 66 type, [](const std::string &aliaName, const FilterType type) { 67 return std::make_shared<T>(aliaName, type); 68 }); 69 if (!result.second) { 70 result.first->second = generator; 71 } 72 } else { 73 auto result = generators.emplace(type, generator); 74 if (!result.second) { 75 result.first->second = generator; 76 } 77 } 78 } 79 80 std::unordered_map<FilterType, InstanceGenerator> generators; 81 }; 82 83 template <typename T> 84 class AutoRegisterFilter { 85 public: AutoRegisterFilter(const std::string & name,const FilterType type)86 explicit AutoRegisterFilter(const std::string& name, const FilterType type) 87 { 88 FilterFactory::Instance().RegisterFilter<T>(name, type); 89 } 90 AutoRegisterFilter(const std::string & name,const FilterType type,const InstanceGenerator & generator)91 AutoRegisterFilter(const std::string& name, const FilterType type, const InstanceGenerator& generator) 92 { 93 FilterFactory::Instance().RegisterFilter<T>(name, type, generator); 94 } 95 96 ~AutoRegisterFilter() = default; 97 }; 98 } // namespace Pipeline 99 } // namespace Media 100 } // namespace OHOS 101 #endif // HISTREAMER_PIPELINE_FILTER_FACTORY_H 102