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 "plugin/common/any.h"
24 #include "plugin/core/plugin_loader.h"
25 #include "plugin/core/plugin_info.h"
26 #include "plugin/interface/generic_plugin.h"
27 
28 namespace OHOS {
29 namespace Media {
30 namespace Plugin {
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     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)
76             : 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         static Status SourceCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
93 
94         Status InitDemuxerInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
95 
96         static Status DemuxerCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
97 
98         Status InitMuxerInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
99 
100         Status InitCodecInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
101 
102         static Status CodecCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
103 
104         Status InitAudioSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
105 
106         static Status AudioSinkCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
107 
108         Status InitVideoSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
109 
110         static Status VideoSinkCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
111 
112         Status InitOutputSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
113 
114         Status InitGenericPlugin(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
115 
116         bool Verification(const PluginDefBase& definition);
117 
118         bool VersionMatched(const PluginDefBase& definition);
119 
120         bool MoreAcceptable(std::shared_ptr<PluginRegInfo>& regInfo, const PluginDefBase& def);
121 
122         std::shared_ptr<PluginLoader> pluginLoader;
123         std::shared_ptr<RegisterData> registerData;
124         std::shared_ptr<PackageDef> packageDef {nullptr};
125     };
126     void DeletePlugin(std::map<std::string, std::shared_ptr<PluginRegInfo>>& plugins,
127         std::map<std::string, std::shared_ptr<PluginRegInfo>>::iterator& info);
128     std::shared_ptr<RegisterData> registerData_ = std::make_shared<RegisterData>();
129     std::vector<std::shared_ptr<PluginLoader>> registeredLoaders_;
130     std::shared_ptr<RegisterImpl> staticPluginRegister_ = std::make_shared<RegisterImpl>(registerData_);
131 };
132 } // namespace Plugin
133 } // namespace Media
134 } // namespace OHOS
135 #endif // HISTREAMER_PLUGIN_REGISTER_H
136