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