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