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_PLUGIN_REGISTER_H 17 #define HISTREAMER_PLUGIN_REGISTER_H 18 19 #include <functional> 20 #include <map> 21 #include <set> 22 #include <utility> 23 #include "meta/any.h" 24 #include "plugin/plugin_loader.h" 25 #include "plugin/plugin_info.h" 26 #include "plugin/generic_plugin.h" 27 28 namespace OHOS { 29 namespace Media { 30 namespace Plugins { 31 struct DataSource; 32 using DemuxerPluginSnifferFunc = int (*)(const std::string& name, std::shared_ptr<DataSource> dataSource); 33 struct PluginRegInfo { 34 std::shared_ptr<PackageDef> packageDef; 35 std::shared_ptr<PluginInfo> info; 36 PluginCreatorFunc<PluginBase> creator; 37 DemuxerPluginSnifferFunc sniffer; 38 std::shared_ptr<PluginLoader> loader; 39 }; 40 41 class PluginRegister { 42 public: 43 PluginRegister() = default; 44 PluginRegister(const PluginRegister&) = delete; 45 PluginRegister operator=(const PluginRegister&) = delete; 46 ~PluginRegister(); 47 48 std::vector<std::string> ListPlugins(PluginType type, CodecMode preferredCodecMode = CodecMode::HARDWARE); 49 int GetAllRegisteredPluginCount(); 50 51 std::shared_ptr<PluginRegInfo> GetPluginRegInfo(PluginType type, const std::string& name); 52 53 void RegisterPlugins(); 54 55 void RegisterGenericPlugin(const GenericPluginDef& pluginDef); 56 57 void RegisterGenericPlugins(const std::vector<GenericPluginDef>& vecPluginDef); 58 private: 59 void RegisterStaticPlugins(); 60 void RegisterDynamicPlugins(); 61 __attribute__((no_sanitize("cfi"))) void RegisterPluginsFromPath(const char* libDirPath); 62 void UnregisterAllPlugins(); 63 void EraseRegisteredPluginsByLoader(const std::shared_ptr<PluginLoader>& loader); 64 65 private: 66 using REGISTERED_TABLE = std::map<PluginType, std::map<std::string, std::shared_ptr<PluginRegInfo>>>; 67 68 struct RegisterData { 69 std::map<PluginType, std::vector<std::string>> registerNames; 70 REGISTERED_TABLE registerTable; 71 bool IsPluginExist(PluginType type, const std::string& name); 72 }; 73 74 struct RegisterImpl : PackageRegister { 75 explicit RegisterImpl(std::shared_ptr<RegisterData> data, std::shared_ptr<PluginLoader> loader = nullptr) pluginLoaderRegisterImpl76 : pluginLoader(std::move(loader)), registerData(std::move(data)) {} 77 78 ~RegisterImpl() override = default; 79 80 Status AddPlugin(const PluginDefBase& def) override; 81 82 Status AddPackage(const PackageDef& def) override; 83 84 Status SetPackageDef(const PackageDef& def); 85 86 void UpdateRegisterTableAndRegisterNames(const PluginDefBase& def); 87 88 void SetPluginInfo(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def); 89 90 Status InitSourceInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def); 91 92 Status InitDemuxerInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def); 93 94 Status InitMuxerInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def); 95 96 __attribute__((no_sanitize("cfi"))) Status InitCodecInfo(std::shared_ptr<PluginRegInfo>& reg, 97 const PluginDefBase& def); 98 99 Status InitAudioSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def); 100 101 Status InitVideoSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def); 102 103 Status InitOutputSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def); 104 105 Status InitGenericPlugin(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def); 106 107 bool Verification(const PluginDefBase& definition); 108 109 bool VersionMatched(const PluginDefBase& definition); 110 111 bool MoreAcceptable(std::shared_ptr<PluginRegInfo>& regInfo, const PluginDefBase& def); 112 113 std::shared_ptr<PluginLoader> pluginLoader; 114 std::shared_ptr<RegisterData> registerData; 115 std::shared_ptr<PackageDef> packageDef {nullptr}; 116 }; 117 void DeletePlugin(std::map<std::string, std::shared_ptr<PluginRegInfo>>& plugins, 118 std::map<std::string, std::shared_ptr<PluginRegInfo>>::iterator& info); 119 std::shared_ptr<RegisterData> registerData_ = std::make_shared<RegisterData>(); 120 std::vector<std::shared_ptr<PluginLoader>> registeredLoaders_; 121 std::shared_ptr<RegisterImpl> staticPluginRegister_ = std::make_shared<RegisterImpl>(registerData_); 122 public: GetStaticPluginRegister()123 std::shared_ptr<RegisterImpl> GetStaticPluginRegister() 124 { 125 return staticPluginRegister_; 126 } 127 }; 128 } // namespace Plugins 129 } // namespace Media 130 } // namespace OHOS 131 #endif // HISTREAMER_PLUGIN_REGISTER_H 132