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