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_MANAGER_H
17 #define HISTREAMER_PLUGIN_MANAGER_H
18 
19 #include "plugin/common/type_cast_ext.h"
20 #include "plugin/core/audio_sink.h"
21 #include "plugin/core/codec.h"
22 #include "plugin/core/demuxer.h"
23 #include "plugin/core/muxer.h"
24 #include "plugin/core/output_sink.h"
25 #include "plugin/core/plugin_info.h"
26 #include "plugin/core/plugin_register.h"
27 #include "plugin/core/source.h"
28 #include "plugin/core/video_sink.h"
29 #include "plugin/interface/generic_plugin.h"
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Plugin {
34 class PluginManager {
35 public:
36     PluginManager(const PluginManager&) = delete;
37     PluginManager operator=(const PluginManager&) = delete;
38     ~PluginManager() = default;
Instance()39     static PluginManager& Instance()
40     {
41         static PluginManager impl;
42         return impl;
43     }
44 
45     std::vector<std::string> ListPlugins(PluginType pluginType, CodecMode preferredCodecMode = CodecMode::HARDWARE);
46 
47     std::shared_ptr<PluginInfo> GetPluginInfo(PluginType type, const std::string& name);
48 
49     std::shared_ptr<Source> CreateSourcePlugin(const std::string& name);
50 
51     std::shared_ptr<Demuxer> CreateDemuxerPlugin(const std::string& name);
52 
53     std::shared_ptr<Muxer> CreateMuxerPlugin(const std::string& name);
54 
55     std::shared_ptr<Codec> CreateCodecPlugin(const std::string& name, PluginType type);
56 
57     std::shared_ptr<AudioSink> CreateAudioSinkPlugin(const std::string& name);
58 
59     std::shared_ptr<VideoSink> CreateVideoSinkPlugin(const std::string& name);
60 
61     std::shared_ptr<OutputSink> CreateOutputSinkPlugin(const std::string& name);
62 
63     template<typename T, typename U>
CreateGenericPlugin(const std::string & name)64     std::shared_ptr<T> CreateGenericPlugin(const std::string& name)
65     {
66         return CreatePlugin<T, U>(name, PluginType::GENERIC_PLUGIN);
67     }
68 
69     int32_t Sniffer(const std::string& name, std::shared_ptr<DataSourceHelper> source);
70 
71     void RegisterGenericPlugin(const GenericPluginDef& pluginDef);
72 
73     void RegisterGenericPlugins(const std::vector<GenericPluginDef>& vecPluginDef);
74 private:
75     PluginManager();
76 
77     void Init();
78 
79     template<typename T, typename U>
CreatePlugin(const std::string & name,PluginType pluginType)80     std::shared_ptr<T> CreatePlugin(const std::string& name, PluginType pluginType)
81     {
82         auto regInfo = pluginRegister_->GetPluginRegInfo(pluginType, name);
83         if (!regInfo) {
84             return {};
85         }
86         auto plugin = ReinterpretPointerCast<U>(regInfo->creator(name));
87         if (!plugin) {
88             return {};
89         }
90         return std::shared_ptr<T>(
91                 new T(regInfo->packageDef->pkgVersion, regInfo->info->apiVersion, plugin));
92     }
93 
94 private:
95     std::shared_ptr<PluginRegister> pluginRegister_;
96 };
97 } // namespace Plugin
98 } // namespace Media
99 } // namespace OHOS
100 #endif // HISTREAMER_PLUGIN_MANAGER_H
101