• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Shenzhen Kaihong DID 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 #include <hdf_log.h>
17 #include <memory.h>
18 #include <malloc.h>
19 #include <securec.h>
20 #include "codec_adapter_interface.h"
21 #include "component_mgr.h"
22 #include "component_node.h"
23 #define HDF_LOG_TAG codec_hdi_server
24 
25 using namespace OHOS::Codec::Omx;
26 
27 static ComponentMgr g_mgr;
28 struct CodecComponentNode {
29     std::shared_ptr<ComponentNode> node;
30 };
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
OMXAdapterCreateComponent(struct CodecComponentNode ** codecNode,char * compName,int64_t appData,struct CodecCallbackType * callbacks)35 int32_t OMXAdapterCreateComponent(struct CodecComponentNode **codecNode, char *compName, int64_t appData,
36                                   struct CodecCallbackType *callbacks)
37 {
38     OMX_COMPONENTTYPE *comp = nullptr;
39     CodecComponentNode *tempNode = new CodecComponentNode;
40     if (tempNode == nullptr) {
41         HDF_LOGE("%{public}s create CodecComponentNode error", __func__);
42         return HDF_ERR_MALLOC_FAIL;
43     }
44     tempNode->node = std::make_shared<ComponentNode>(callbacks, appData);
45     auto err = g_mgr.CreateComponentInstance(compName, &ComponentNode::callbacks_, tempNode->node.get(), &comp);
46     if (err != OMX_ErrorNone) {
47         HDF_LOGE("%{public}s create component instance err[%{public}d]", __func__, err);
48         delete tempNode;
49         tempNode = nullptr;
50         return err;
51     }
52     tempNode->node->SetHandle(static_cast<OMX_HANDLETYPE>(comp));
53 
54     *codecNode = tempNode;
55     return HDF_SUCCESS;
56 }
57 
OmxAdapterDestroyComponent(struct CodecComponentNode * codecNode)58 int32_t OmxAdapterDestroyComponent(struct CodecComponentNode *codecNode)
59 {
60     if (codecNode == nullptr || codecNode->node == nullptr) {
61         HDF_LOGE("%{public}s codecNode is null", __func__);
62         return HDF_ERR_INVALID_PARAM;
63     }
64     auto err = g_mgr.DeleteComponentInstance(static_cast<OMX_COMPONENTTYPE*>(codecNode->node->GetHandle()));
65     if (err != OMX_ErrorNone) {
66         HDF_LOGE("%{public}s DeleteComponentInstance err[%{public}d]", __func__, err);
67         return err;
68     }
69     codecNode->node = nullptr;
70     delete codecNode;
71     codecNode = nullptr;
72 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
73     err = mallopt(M_FLUSH_THREAD_CACHE, 0);
74     if (err != HDF_SUCCESS) {
75         HDF_LOGE("%{public}s :release cache error, m_purge = %{public}d", __func__, err);
76     }
77 #endif
78     return HDF_SUCCESS;
79 }
80 
OmxAdapterComponentVersion(struct CodecComponentNode * codecNode,struct CompVerInfo * verInfo)81 int32_t OmxAdapterComponentVersion(struct CodecComponentNode *codecNode, struct CompVerInfo *verInfo)
82 {
83     if (codecNode == nullptr || codecNode->node == nullptr || verInfo == nullptr) {
84         HDF_LOGE("%{public}s codecNode, node or verInfois is null", __func__);
85         return HDF_ERR_INVALID_PARAM;
86     }
87     return codecNode->node->GetComponentVersion(*verInfo);
88 }
89 
OmxAdapterSendCommand(struct CodecComponentNode * codecNode,OMX_COMMANDTYPE cmd,uint32_t param,int8_t * cmdData,uint32_t cmdDataLen)90 int32_t OmxAdapterSendCommand(struct CodecComponentNode *codecNode, OMX_COMMANDTYPE cmd, uint32_t param,
91                               int8_t *cmdData, uint32_t cmdDataLen)
92 {
93     if (codecNode == nullptr || codecNode->node == nullptr) {
94         HDF_LOGE("%{public}s codecNode or node is null", __func__);
95         return HDF_ERR_INVALID_PARAM;
96     }
97     return codecNode->node->SendCommand(cmd, param, cmdData, cmdDataLen);
98 }
99 
OmxAdapterGetParameter(struct CodecComponentNode * codecNode,OMX_INDEXTYPE paramIndex,int8_t * param,uint32_t paramLen)100 int32_t OmxAdapterGetParameter(struct CodecComponentNode *codecNode, OMX_INDEXTYPE paramIndex, int8_t *param,
101                                uint32_t paramLen)
102 {
103     if (codecNode == nullptr || codecNode->node == nullptr || param == nullptr) {
104         HDF_LOGE("%{public}s codecNode, node or param is null", __func__);
105         return HDF_ERR_INVALID_PARAM;
106     }
107 
108     return codecNode->node->GetParameter(paramIndex, param, paramLen);
109 }
110 
OmxAdapterSetParameter(struct CodecComponentNode * codecNode,OMX_INDEXTYPE index,int8_t * param,uint32_t paramLen)111 int32_t OmxAdapterSetParameter(struct CodecComponentNode *codecNode, OMX_INDEXTYPE index, int8_t *param,
112                                uint32_t paramLen)
113 {
114     if (codecNode == nullptr || codecNode->node == nullptr || param == nullptr) {
115         HDF_LOGE("%{public}s codecNode, node or param is null", __func__);
116         return HDF_ERR_INVALID_PARAM;
117     }
118     return codecNode->node->SetParameter(index, param, paramLen);
119 }
120 
OmxAdapterGetConfig(struct CodecComponentNode * codecNode,OMX_INDEXTYPE index,int8_t * config,uint32_t configLen)121 int32_t OmxAdapterGetConfig(struct CodecComponentNode *codecNode, OMX_INDEXTYPE index, int8_t *config,
122                             uint32_t configLen)
123 {
124     if (codecNode == nullptr || codecNode->node == nullptr || config == nullptr) {
125         HDF_LOGE("%{public}s codecNode, node or config is null", __func__);
126         return HDF_ERR_INVALID_PARAM;
127     }
128     return codecNode->node->GetConfig(index, config, configLen);
129 }
130 
OmxAdapterSetConfig(struct CodecComponentNode * codecNode,OMX_INDEXTYPE index,int8_t * config,uint32_t configLen)131 int32_t OmxAdapterSetConfig(struct CodecComponentNode *codecNode, OMX_INDEXTYPE index, int8_t *config,
132                             uint32_t configLen)
133 {
134     if (codecNode == nullptr || codecNode->node == nullptr || config == nullptr) {
135         HDF_LOGE("%{public}s codecNode, node or config is null", __func__);
136         return HDF_ERR_INVALID_PARAM;
137     }
138     return codecNode->node->SetConfig(index, config, configLen);
139 }
140 
OmxAdapterGetExtensionIndex(struct CodecComponentNode * codecNode,const char * parameterName,OMX_INDEXTYPE * indexType)141 int32_t OmxAdapterGetExtensionIndex(struct CodecComponentNode *codecNode, const char *parameterName,
142                                     OMX_INDEXTYPE *indexType)
143 {
144     if (codecNode == nullptr || codecNode->node == nullptr || parameterName == nullptr || indexType == nullptr) {
145         HDF_LOGE("%{public}s codecNode, node , parameterName or indexType is null", __func__);
146         return HDF_ERR_INVALID_PARAM;
147     }
148     return codecNode->node->GetExtensionIndex(parameterName, indexType);
149 }
150 
OmxAdapterGetState(struct CodecComponentNode * codecNode,OMX_STATETYPE * state)151 int32_t OmxAdapterGetState(struct CodecComponentNode *codecNode, OMX_STATETYPE *state)
152 {
153     if (codecNode == nullptr || codecNode->node == nullptr || state == nullptr) {
154         HDF_LOGE("%{public}s codecNode, node or state is null", __func__);
155         return HDF_ERR_INVALID_PARAM;
156     }
157     return codecNode->node->GetState(state);
158 }
159 
OmxAdapterComponentTunnelRequest(struct CodecComponentNode * codecNode,uint32_t port,int32_t omxHandleTypeTunneledComp,uint32_t tunneledPort,struct OMX_TUNNELSETUPTYPE * tunnelSetup)160 int32_t OmxAdapterComponentTunnelRequest(struct CodecComponentNode *codecNode, uint32_t port,
161                                          int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort,
162                                          struct OMX_TUNNELSETUPTYPE *tunnelSetup)
163 {
164     if (codecNode == nullptr || codecNode->node == nullptr || tunnelSetup == nullptr) {
165         HDF_LOGE("%{public}s codecNode, node or tunnelSetup is null", __func__);
166         return HDF_ERR_INVALID_PARAM;
167     }
168     return codecNode->node->ComponentTunnelRequest(port, omxHandleTypeTunneledComp, tunneledPort, tunnelSetup);
169 }
170 
OmxAdapterUseBuffer(struct CodecComponentNode * codecNode,uint32_t portIndex,struct OmxCodecBuffer * omxBuffer)171 int32_t OmxAdapterUseBuffer(struct CodecComponentNode *codecNode, uint32_t portIndex, struct OmxCodecBuffer *omxBuffer)
172 {
173     if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
174         HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
175         return HDF_ERR_INVALID_PARAM;
176     }
177     return codecNode->node->UseBuffer(portIndex, *omxBuffer);
178 }
179 
OmxAdapterAllocateBuffer(struct CodecComponentNode * codecNode,uint32_t portIndex,struct OmxCodecBuffer * omxBuffer)180 int32_t OmxAdapterAllocateBuffer(struct CodecComponentNode *codecNode, uint32_t portIndex,
181                                  struct OmxCodecBuffer *omxBuffer)
182 {
183     if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
184         HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
185         return HDF_ERR_INVALID_PARAM;
186     }
187     return codecNode->node->AllocateBuffer(portIndex, *omxBuffer);
188 }
189 
OmxAdapterFreeBuffer(struct CodecComponentNode * codecNode,uint32_t portIndex,struct OmxCodecBuffer * omxBuffer)190 int32_t OmxAdapterFreeBuffer(struct CodecComponentNode *codecNode, uint32_t portIndex, struct OmxCodecBuffer *omxBuffer)
191 {
192     if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
193         HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
194         return HDF_ERR_INVALID_PARAM;
195     }
196     int32_t ret = codecNode->node->FreeBuffer(portIndex, *omxBuffer);
197 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
198     int32_t err = mallopt(M_FLUSH_THREAD_CACHE, 0);
199     if (err != HDF_SUCCESS) {
200         HDF_LOGE("%{public}s :release cache error, m_purge = %{public}d", __func__, err);
201     }
202 #endif
203     return ret;
204 }
205 
OmxAdapterEmptyThisBuffer(struct CodecComponentNode * codecNode,struct OmxCodecBuffer * omxBuffer)206 int32_t OmxAdapterEmptyThisBuffer(struct CodecComponentNode *codecNode, struct OmxCodecBuffer *omxBuffer)
207 {
208     if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
209         HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
210         return HDF_ERR_INVALID_PARAM;
211     }
212     return codecNode->node->EmptyThisBuffer(*omxBuffer);
213 }
214 
OmxAdapterFillThisBuffer(struct CodecComponentNode * codecNode,struct OmxCodecBuffer * omxBuffer)215 int32_t OmxAdapterFillThisBuffer(struct CodecComponentNode *codecNode, struct OmxCodecBuffer *omxBuffer)
216 {
217     if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
218         HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
219         return HDF_ERR_INVALID_PARAM;
220     }
221     return codecNode->node->FillThisBuffer(*omxBuffer);
222 }
223 
OmxAdapterSetCallbacks(struct CodecComponentNode * codecNode,struct CodecCallbackType * omxCallback,int64_t appData)224 int32_t OmxAdapterSetCallbacks(struct CodecComponentNode *codecNode, struct CodecCallbackType *omxCallback,
225                                int64_t appData)
226 {
227     if (codecNode == nullptr || codecNode->node == nullptr || omxCallback == nullptr) {
228         HDF_LOGE("%{public}s codecNode, node or omxCallback is null", __func__);
229         return HDF_ERR_INVALID_PARAM;
230     }
231     return codecNode->node->SetCallbacks(omxCallback, appData);
232 }
233 
OmxAdapterDeInit(struct CodecComponentNode * codecNode)234 int32_t OmxAdapterDeInit(struct CodecComponentNode *codecNode)
235 {
236     if (codecNode == nullptr || codecNode->node == nullptr) {
237         HDF_LOGE("%{public}s codecNode or node is null", __func__);
238         return HDF_ERR_INVALID_PARAM;
239     }
240     return codecNode->node->DeInit();
241 }
242 
OmxAdapterUseEglImage(struct CodecComponentNode * codecNode,struct OmxCodecBuffer * buffer,uint32_t portIndex,int8_t * eglImage,uint32_t eglImageLen)243 int32_t OmxAdapterUseEglImage(struct CodecComponentNode *codecNode, struct OmxCodecBuffer *buffer, uint32_t portIndex,
244                               int8_t *eglImage, uint32_t eglImageLen)
245 {
246     if (codecNode == nullptr || codecNode->node == nullptr || buffer == nullptr || eglImage == nullptr) {
247         HDF_LOGE("%{public}s codecNode, node, buffer or eglImage is null", __func__);
248         return HDF_ERR_INVALID_PARAM;
249     }
250     return codecNode->node->UseEglImage(*buffer, portIndex, eglImage, eglImageLen);
251 }
252 
OmxAdapterComponentRoleEnum(struct CodecComponentNode * codecNode,uint8_t * role,uint32_t roleLen,uint32_t index)253 int32_t OmxAdapterComponentRoleEnum(struct CodecComponentNode *codecNode, uint8_t *role, uint32_t roleLen,
254                                     uint32_t index)
255 {
256     if (codecNode == nullptr || codecNode->node == nullptr || role == nullptr) {
257         HDF_LOGE("%{public}s codecNode, node or role is null", __func__);
258         return HDF_ERR_INVALID_PARAM;
259     }
260     return codecNode->node->ComponentRoleEnum(role, roleLen, index);
261 }
262 
OmxAdapterSetComponentRole(struct CodecComponentNode * codecNode,char * compName)263 int32_t OmxAdapterSetComponentRole(struct CodecComponentNode *codecNode, char *compName)
264 {
265     if (codecNode == nullptr || codecNode->node == nullptr || compName == nullptr) {
266         HDF_LOGE("%{public}s codecNode, codecNode->node or compName is null", __func__);
267         return HDF_ERR_INVALID_PARAM;
268     }
269     CodecOMXCore *core;
270     auto err = g_mgr.GetCoreOfComponent(core, compName);
271     if (err != HDF_SUCCESS) {
272         HDF_LOGE("%{public}s core is null", __func__);
273         return err;
274     }
275 
276     std::vector<std::string> roles;
277     std::string name = compName;
278     int32_t ret = core->GetRolesOfComponent(name, roles);
279     if (ret != HDF_SUCCESS) {
280         HDF_LOGE("%{public}s: GetRoleOfComponent return err [%{public}d]", __func__, ret);
281         return ret;
282     }
283     uint32_t roleIndex = 0;
284     HDF_LOGI("%{public}s: RoleName = [%{public}s]", __func__, roles[roleIndex].c_str());
285 
286     OMX_PARAM_COMPONENTROLETYPE role;
287     errno_t res = strncpy_s(reinterpret_cast<char *>(role.cRole), OMX_MAX_STRINGNAME_SIZE,
288                             roles[roleIndex].c_str(), roles[roleIndex].length());
289     if (res != EOK) {
290         HDF_LOGE("%{public}s: strncpy_s return err [%{public}d]", __func__, err);
291         return HDF_FAILURE;
292     }
293     role.nSize = sizeof(role);
294     ret = codecNode->node->SetParameter(OMX_IndexParamStandardComponentRole,
295                                         reinterpret_cast<int8_t *>(&role), sizeof(role));
296     if (ret != HDF_SUCCESS) {
297         HDF_LOGE("%{public}s: OMX_IndexParamStandardComponentRole err [%{public}d]", __func__, ret);
298     }
299 
300     return ret;
301 }
302 #ifdef __cplusplus
303 };
304 #endif
305