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