• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int GetAllRegisteredPluginCount();
54 
55     std::shared_ptr<PluginRegInfo> GetPluginRegInfo(PluginType type, const std::string& name);
56 
57     void EnablePackage(PluginType type, const std::string& name);
58 
59     void DisablePackage(PluginType type, const std::string& name);
60 
61     int GetRegisteredPluginCountByPackageName(std::string& name);
62 
63     bool IsPackageExist(PluginType type, const std::string& name);
64     void PrintRegisteredPluginInfo();
65 
66     int GetPackageCounts(std::string& name);
67 
68     void RegisterPlugins();
69 
70 private:
71     void RegisterStaticPlugins();
72     void RegisterDynamicPlugins();
73     void RegisterPluginsFromPath(const char* libDirPath);
74     void UnregisterAllPlugins();
75     void EraseRegisteredPluginsByPackageName(const std::string& name);
76     void EraseRegisteredPluginsByLoader(const std::shared_ptr<PluginLoader>& loader);
77     void SaveDisabledPackage(std::pair<std::string, std::shared_ptr<PluginRegInfo>> info);
78     void RecoverDisabledPackage(PluginType type, const std::string& name);
79 
80 private:
81     using REGISTERED_TABLE = std::map<PluginType, std::map<std::string, std::shared_ptr<PluginRegInfo>>>;
82 
83     struct RegisterData {
84         std::map<PluginType, std::set<std::string>> registerNames;
85         REGISTERED_TABLE registerTable;
86         std::vector<std::pair<std::string, std::shared_ptr<PluginRegInfo>>> disabledPackage;
87         bool IsPluginExist(PluginType type, const std::string& name);
88     };
89 
90     struct RegisterImpl : PackageRegister {
91         explicit RegisterImpl(std::shared_ptr<RegisterData> data, std::shared_ptr<PluginLoader> loader = nullptr)
pluginLoaderRegisterImpl92             : pluginLoader(std::move(loader)), registerData(std::move(data)) {}
93 
94         ~RegisterImpl() override = default;
95 
96         Status AddPlugin(const PluginDefBase& def) override;
97 
98         Status AddPackage(const PackageDef& def) override;
99 
100         Status SetPackageDef(const PackageDef& def);
101 
102         std::shared_ptr<PluginRegInfo> BuildRegInfo(const PluginDefBase& def);
103 
104         void SetPluginInfo(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
105 
106         Status InitSourceInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
107 
108         static Status SourceCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
109 
110         Status InitDemuxerInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
111 
112         static Status DemuxerCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
113 
114         Status InitMuxerInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
115 
116         Status InitCodecInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
117 
118         static Status CodecCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
119 
120         Status InitAudioSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
121 
122         static Status AudioSinkCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
123 
124         Status InitVideoSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
125 
126         static Status VideoSinkCapabilityConvert(std::shared_ptr<PluginInfo>& info, const PluginDefBase& def);
127 
128         Status InitOutputSinkInfo(std::shared_ptr<PluginRegInfo>& reg, const PluginDefBase& def);
129 
130         bool Verification(const PluginDefBase& definition);
131 
132         bool VersionMatched(const PluginDefBase& definition);
133 
134         bool MoreAcceptable(std::shared_ptr<PluginRegInfo>& regInfo, const PluginDefBase& def);
135 
136         std::shared_ptr<PluginLoader> pluginLoader;
137         std::shared_ptr<RegisterData> registerData;
138         std::shared_ptr<PackageDef> packageDef {nullptr};
139     };
140 
141     std::shared_ptr<RegisterData> registerData = std::make_shared<RegisterData>();
142     std::vector<std::shared_ptr<PluginLoader>> registeredLoaders;
143 };
144 } // namespace Plugin
145 } // namespace Media
146 } // namespace OHOS
147 #endif // HISTREAMER_PLUGIN_REGISTER_H
148