1 /* 2 * Copyright (C) 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 PLUGIN_SERVER_H 17 #define PLUGIN_SERVER_H 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 #include <cstdint> 23 #include "singleton.h" 24 #include "attr_data.h" 25 #include "plugin_class_base.h" 26 #include "plugin_common_type.h" 27 #include "plugin_errors.h" 28 #include "plugin_service.h" 29 #include "priority_scheme.h" 30 31 namespace OHOS { 32 namespace MultimediaPlugin { 33 class PlatformAdp; 34 class PluginFw; 35 class GstPluginFw; 36 37 enum class PluginFWType : int32_t { 38 PLUGIN_FW_GENERAL = 0, 39 PLUGIN_FW_GSTREAMER 40 }; 41 42 class PluginServer final : public NoCopyable { 43 public: 44 uint32_t Register(std::vector<std::string> &&pluginPaths); 45 46 template<typename T> CreateObject(const std::string & className,uint32_t & errorCode)47 inline T *CreateObject(const std::string &className, uint32_t &errorCode) 48 { 49 uint16_t interfaceID = GetInterfaceId<T>(); 50 return ConvertToServiceInterface<T>(CreateObject(interfaceID, className, errorCode)); 51 } 52 53 template<typename T> CreateObject(const std::string & className)54 inline T *CreateObject(const std::string &className) 55 { 56 uint16_t interfaceID = GetInterfaceId<T>(); 57 uint32_t errorCode = 0; 58 return ConvertToServiceInterface<T>(CreateObject(interfaceID, className, errorCode)); 59 } 60 61 template<typename T> CreateObject(uint16_t serviceType,uint32_t & errorCode)62 inline T *CreateObject(uint16_t serviceType, uint32_t &errorCode) 63 { 64 uint16_t interfaceID = GetInterfaceId<T>(); 65 std::map<std::string, AttrData> emptyCapabilities; 66 PriorityScheme emptyPriScheme; 67 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, emptyCapabilities, 68 emptyPriScheme, errorCode)); 69 } 70 71 template<typename T> CreateObject(uint16_t serviceType)72 inline T *CreateObject(uint16_t serviceType) 73 { 74 uint16_t interfaceID = GetInterfaceId<T>(); 75 std::map<std::string, AttrData> emptyCapabilities; 76 PriorityScheme emptyPriScheme; 77 uint32_t errorCode = 0; 78 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, emptyCapabilities, 79 emptyPriScheme, errorCode)); 80 } 81 82 template<typename T> CreateObject(uint16_t serviceType,const PriorityScheme & priorityScheme,uint32_t & errorCode)83 inline T *CreateObject(uint16_t serviceType, const PriorityScheme &priorityScheme, uint32_t &errorCode) 84 { 85 uint16_t interfaceID = GetInterfaceId<T>(); 86 std::map<std::string, AttrData> emptyCapabilities; 87 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, emptyCapabilities, 88 priorityScheme, errorCode)); 89 } 90 91 template<typename T> CreateObject(uint16_t serviceType,const PriorityScheme & priorityScheme)92 inline T *CreateObject(uint16_t serviceType, const PriorityScheme &priorityScheme) 93 { 94 uint16_t interfaceID = GetInterfaceId<T>(); 95 std::map<std::string, AttrData> emptyCapabilities; 96 uint32_t errorCode = 0; 97 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, emptyCapabilities, 98 priorityScheme, errorCode)); 99 } 100 101 template<typename T> CreateObject(uint16_t serviceType,const std::map<std::string,AttrData> & capabilities,uint32_t & errorCode)102 inline T *CreateObject(uint16_t serviceType, const std::map<std::string, AttrData> &capabilities, 103 uint32_t &errorCode) 104 { 105 uint16_t interfaceID = GetInterfaceId<T>(); 106 PriorityScheme emptyPriScheme; 107 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, capabilities, 108 emptyPriScheme, errorCode)); 109 } 110 111 template<typename T> CreateObject(uint16_t serviceType,const std::map<std::string,AttrData> & capabilities)112 inline T *CreateObject(uint16_t serviceType, const std::map<std::string, AttrData> &capabilities) 113 { 114 uint16_t interfaceID = GetInterfaceId<T>(); 115 PriorityScheme emptyPriScheme; 116 uint32_t errorCode = 0; 117 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, capabilities, 118 emptyPriScheme, errorCode)); 119 } 120 121 template<typename T> CreateObject(uint16_t serviceType,const std::map<std::string,AttrData> & capabilities,const PriorityScheme & priorityScheme,uint32_t & errorCode)122 inline T *CreateObject(uint16_t serviceType, const std::map<std::string, AttrData> &capabilities, 123 const PriorityScheme &priorityScheme, uint32_t &errorCode) 124 { 125 uint16_t interfaceID = GetInterfaceId<T>(); 126 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, capabilities, 127 priorityScheme, errorCode)); 128 } 129 130 template<typename T> CreateObject(uint16_t serviceType,const std::map<std::string,AttrData> & capabilities,const PriorityScheme & priorityScheme)131 inline T *CreateObject(uint16_t serviceType, const std::map<std::string, AttrData> &capabilities, 132 const PriorityScheme &priorityScheme) 133 { 134 uint16_t interfaceID = GetInterfaceId<T>(); 135 uint32_t errorCode = 0; 136 return ConvertToServiceInterface<T>(CreateObject(interfaceID, serviceType, capabilities, 137 priorityScheme, errorCode)); 138 } 139 140 template<typename T> PluginServerGetClassInfo(uint16_t serviceType,std::vector<ClassInfo> & classesInfo)141 inline uint32_t PluginServerGetClassInfo(uint16_t serviceType, std::vector<ClassInfo> &classesInfo) 142 { 143 uint16_t interfaceID = GetInterfaceId<T>(); 144 std::map<std::string, AttrData> emptyCapabilities; 145 return PluginServerGetClassInfo(interfaceID, serviceType, emptyCapabilities, classesInfo); 146 } 147 148 template<typename T> PluginServerGetClassInfo(uint16_t serviceType,const std::map<std::string,AttrData> & capabilities,std::vector<ClassInfo> & classesInfo)149 inline uint32_t PluginServerGetClassInfo(uint16_t serviceType, const std::map<std::string, AttrData> &capabilities, 150 std::vector<ClassInfo> &classesInfo) 151 { 152 uint16_t interfaceID = GetInterfaceId<T>(); 153 return PluginServerGetClassInfo(interfaceID, serviceType, capabilities, classesInfo); 154 } 155 156 DECLARE_DELAYED_REF_SINGLETON(PluginServer); 157 158 private: 159 template<typename T> ConvertToServiceInterface(PluginClassBase * pluginBase)160 inline T *ConvertToServiceInterface(PluginClassBase *pluginBase) 161 { 162 #ifdef PLUGIN_FLAG_RTTI_ENABLE 163 // when -frtti is enable, we use dynamic cast directly 164 // to achieve the correct base class side-to-side conversion. 165 T *serviceObj = dynamic_cast<T *>(pluginBase); 166 if (serviceObj == nullptr && pluginBase != nullptr) { 167 // type mismatch. 168 delete pluginBase; 169 } 170 #else 171 // adjust pointer position when multiple inheritance. 172 void *obj = dynamic_cast<void *>(pluginBase); 173 // when -frtti is not enable, we use static cast. 174 // static cast is not safe enough, but we have checked before we get here. 175 T *serviceObj = static_cast<T *>(obj); 176 #endif 177 return serviceObj; 178 } 179 180 PluginClassBase *CreateObject(uint16_t interfaceID, const std::string &className, uint32_t &errorCode); 181 PluginClassBase *CreateObject(uint16_t interfaceID, uint16_t serviceType, 182 const std::map<std::string, AttrData> &capabilities, 183 const PriorityScheme &priorityScheme, uint32_t &errorCode); 184 uint32_t PluginServerGetClassInfo(uint16_t interfaceID, uint16_t serviceType, 185 const std::map<std::string, AttrData> &capabilities, 186 std::vector<ClassInfo> &classesInfo); 187 PluginFWType AnalyzeFWType(const std::string &canonicalPath); 188 189 PlatformAdp &platformAdp_; 190 PluginFw &pluginFw_; 191 GstPluginFw &gstPluginFw_; 192 }; 193 } // namespace MultimediaPlugin 194 } // namespace OHOS 195 196 #endif // PLUGIN_SERVER_H 197