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 <osal_mem.h>
17 #include <servmgr_hdi.h>
18 #include "codec_component_manager.h"
19 #include "codec_internal.h"
20 #include "codec_log_wrapper.h"
21 #include "codec_util.h"
22 #include "codec_types.h"
23
24 struct CodecComponentManagerProxy {
25 struct CodecComponentManager instance;
26 struct HdfRemoteService *remoteOmx;
27 };
28
29 static struct CodecComponentManagerProxy g_codecComponentManagerProxy = {
30 .instance = {
31 .GetComponentNum = NULL,
32 .GetComponentCapabilityList = NULL,
33 .CreateComponent = NULL,
34 .DestroyComponent = NULL,
35 .AsObject = NULL,
36 },
37 .remoteOmx = NULL,
38 };
39
ReleaseSbuf(struct HdfSBuf * data,struct HdfSBuf * reply)40 static void ReleaseSbuf(struct HdfSBuf *data, struct HdfSBuf *reply)
41 {
42 if (data != NULL) {
43 HdfSbufRecycle(data);
44 }
45 if (reply != NULL) {
46 HdfSbufRecycle(reply);
47 }
48 }
49
GetComponentNum()50 static int32_t GetComponentNum()
51 {
52 int32_t num = 0;
53 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
54 if (data == NULL) {
55 CODEC_LOGE("Failed to obtain");
56 return HDF_FAILURE;
57 }
58 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
59 if (reply == NULL) {
60 CODEC_LOGE("Failed to obtain reply");
61 HdfSbufRecycle(data);
62 return HDF_FAILURE;
63 }
64
65 if (!HdfRemoteServiceWriteInterfaceToken(g_codecComponentManagerProxy.remoteOmx, data)) {
66 CODEC_LOGE("write interface token failed");
67 ReleaseSbuf(data, reply);
68 return HDF_FAILURE;
69 }
70
71 if (g_codecComponentManagerProxy.remoteOmx->dispatcher->Dispatch(
72 g_codecComponentManagerProxy.remoteOmx, CMD_CODEC_GET_COMPONENT_NUM, data, reply) != HDF_SUCCESS) {
73 CODEC_LOGE("dispatch request failed!");
74 ReleaseSbuf(data, reply);
75 return HDF_FAILURE;
76 }
77
78 if (!HdfSbufReadInt32(reply, &num)) {
79 CODEC_LOGE("read dataBlock->role failed!");
80 ReleaseSbuf(data, reply);
81 return HDF_FAILURE;
82 }
83
84 ReleaseSbuf(data, reply);
85 return num;
86 }
87
GetComponentCapabilityList(CodecCompCapability * capList,int32_t count)88 static int32_t GetComponentCapabilityList(CodecCompCapability *capList, int32_t count)
89 {
90 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
91 int32_t num = GetComponentNum();
92 if (data == NULL || count <= 0 || count > num) {
93 CODEC_LOGE("Failed to obtain");
94 return HDF_FAILURE;
95 }
96 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
97 if (reply == NULL) {
98 CODEC_LOGE("Failed to obtain reply");
99 HdfSbufRecycle(data);
100 return HDF_FAILURE;
101 }
102
103 if (!HdfRemoteServiceWriteInterfaceToken(g_codecComponentManagerProxy.remoteOmx, data)) {
104 CODEC_LOGE("write interface token failed");
105 return HDF_FAILURE;
106 }
107
108 if (!HdfSbufWriteInt32(data, count)) {
109 CODEC_LOGE("write count failed!");
110 ReleaseSbuf(data, reply);
111 return HDF_ERR_INVALID_PARAM;
112 }
113
114 if (g_codecComponentManagerProxy.remoteOmx->dispatcher->Dispatch(g_codecComponentManagerProxy.remoteOmx,
115 CMD_CODEC_GET_COMPONENT_CAPABILITY_LIST, data,
116 reply) != HDF_SUCCESS) {
117 CODEC_LOGE("dispatch request failed!");
118 ReleaseSbuf(data, reply);
119 return HDF_FAILURE;
120 }
121
122 for (int32_t i = 0; i < count; i++) {
123 if (!CodecCompCapabilityBlockUnmarshalling(reply, &(capList)[i])) {
124 CODEC_LOGE("read capbility %{public}d from sbuf failed!", i);
125 ReleaseSbuf(data, reply);
126 return HDF_FAILURE;
127 }
128 }
129
130 ReleaseSbuf(data, reply);
131 return HDF_SUCCESS;
132 }
133
CreateComponentPreWriteData(struct HdfSBuf * data,struct HdfSBuf * reply,char * compName,int64_t appData,struct CodecCallbackType * callback)134 static int32_t CreateComponentPreWriteData(struct HdfSBuf *data, struct HdfSBuf *reply, char *compName,
135 int64_t appData, struct CodecCallbackType *callback)
136 {
137 if (!HdfRemoteServiceWriteInterfaceToken(g_codecComponentManagerProxy.remoteOmx, data)) {
138 CODEC_LOGE("write interface token failed");
139 ReleaseSbuf(data, reply);
140 return HDF_FAILURE;
141 }
142 if (!HdfSbufWriteString(data, compName)) {
143 CODEC_LOGE("write paramName failed!");
144 ReleaseSbuf(data, reply);
145 return HDF_ERR_INVALID_PARAM;
146 }
147 if (!HdfSbufWriteInt64(data, appData)) {
148 CODEC_LOGE("write appData failed!");
149 ReleaseSbuf(data, reply);
150 return HDF_ERR_INVALID_PARAM;
151 }
152 if (HdfSbufWriteRemoteService(data, callback->remote) != 0) {
153 CODEC_LOGE("write callback failed!");
154 ReleaseSbuf(data, reply);
155 return HDF_ERR_INVALID_PARAM;
156 }
157 return HDF_SUCCESS;
158 }
159
CreateComponent(struct CodecComponentType ** component,uint32_t * componentId,char * compName,int64_t appData,struct CodecCallbackType * callback)160 static int32_t CreateComponent(struct CodecComponentType **component, uint32_t *componentId, char *compName,
161 int64_t appData, struct CodecCallbackType *callback)
162 {
163 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
164 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
165 if (data == NULL || reply == NULL || componentId == NULL) {
166 CODEC_LOGE("HdfSubf malloc failed!");
167 ReleaseSbuf(data, reply);
168 return HDF_ERR_MALLOC_FAIL;
169 }
170 int32_t ret = CreateComponentPreWriteData(data, reply, compName, appData, callback);
171 if (ret != HDF_SUCCESS) {
172 return ret;
173 }
174
175 ret = g_codecComponentManagerProxy.remoteOmx->dispatcher->Dispatch(g_codecComponentManagerProxy.remoteOmx,
176 CMD_CREATE_COMPONENT, data, reply);
177 if (ret != HDF_SUCCESS) {
178 CODEC_LOGE("call failed! error code is %{public}d", ret);
179 ReleaseSbuf(data, reply);
180 return ret;
181 }
182
183 struct HdfRemoteService *componentRemote = HdfSbufReadRemoteService(reply);
184 if (componentRemote == NULL) {
185 CODEC_LOGE("read componentRemote failed!");
186 ReleaseSbuf(data, reply);
187 return HDF_ERR_INVALID_PARAM;
188 }
189 if (!HdfSbufReadUint32(reply, componentId)) {
190 CODEC_LOGE("read componentId failed!");
191 ReleaseSbuf(data, reply);
192 return HDF_ERR_INVALID_PARAM;
193 }
194 *component = CodecComponentTypeGet(componentRemote);
195 ReleaseSbuf(data, reply);
196 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
197 ReleaseCodecCache();
198 #endif
199 return ret;
200 }
201
DestroyComponent(uint32_t componentId)202 static int32_t DestroyComponent(uint32_t componentId)
203 {
204 int32_t ret;
205
206 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
207 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
208 if (data == NULL || reply == NULL) {
209 CODEC_LOGE("HdfSubf malloc failed!");
210 ReleaseSbuf(data, reply);
211 return HDF_ERR_MALLOC_FAIL;
212 }
213
214 if (!HdfRemoteServiceWriteInterfaceToken(g_codecComponentManagerProxy.remoteOmx, data)) {
215 CODEC_LOGE("write interface token failed");
216 ReleaseSbuf(data, reply);
217 return HDF_FAILURE;
218 }
219
220 if (!HdfSbufWriteUint32(data, componentId)) {
221 CODEC_LOGE("write componentId failed!");
222 ReleaseSbuf(data, reply);
223 return HDF_ERR_INVALID_PARAM;
224 }
225
226 ret = g_codecComponentManagerProxy.remoteOmx->dispatcher->Dispatch(g_codecComponentManagerProxy.remoteOmx,
227 CMD_DESTROY_COMPONENT, data, reply);
228 if (ret != HDF_SUCCESS) {
229 CODEC_LOGE("call failed! error code is %{public}d", ret);
230 ReleaseSbuf(data, reply);
231 return ret;
232 }
233 ReleaseSbuf(data, reply);
234 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
235 ReleaseCodecCache();
236 #endif
237 return ret;
238 }
239
InitCodecComponentManagerProxy(void)240 static int32_t InitCodecComponentManagerProxy(void)
241 {
242 if (g_codecComponentManagerProxy.remoteOmx != NULL) {
243 return HDF_SUCCESS;
244 }
245
246 struct HDIServiceManager *serviceMgr = HDIServiceManagerGet();
247 if (serviceMgr == NULL) {
248 CODEC_LOGE("HDIServiceManager not found!");
249 return HDF_FAILURE;
250 }
251
252 struct HdfRemoteService *remoteOmx = serviceMgr->GetService(serviceMgr, CODEC_HDI_OMX_SERVICE_NAME);
253 HDIServiceManagerRelease(serviceMgr);
254 if (remoteOmx == NULL) {
255 CODEC_LOGE("CodecComponentTypeService not found!");
256 return HDF_FAILURE;
257 }
258 if (!HdfRemoteServiceSetInterfaceDesc(remoteOmx, "ohos.hdi.codec_service")) {
259 CODEC_LOGE("failed to init interface desc");
260 HdfRemoteServiceRecycle(remoteOmx);
261 return HDF_FAILURE;
262 }
263
264 g_codecComponentManagerProxy.remoteOmx = remoteOmx;
265 g_codecComponentManagerProxy.instance.GetComponentNum = GetComponentNum;
266 g_codecComponentManagerProxy.instance.GetComponentCapabilityList = GetComponentCapabilityList;
267 g_codecComponentManagerProxy.instance.CreateComponent = CreateComponent;
268 g_codecComponentManagerProxy.instance.DestroyComponent = DestroyComponent;
269
270 return HDF_SUCCESS;
271 }
272
GetCodecComponentManager(void)273 struct CodecComponentManager *GetCodecComponentManager(void)
274 {
275 if (InitCodecComponentManagerProxy() != HDF_SUCCESS) {
276 return NULL;
277 }
278 return &g_codecComponentManagerProxy.instance;
279 }
280
CodecComponentManagerRelease(void)281 void CodecComponentManagerRelease(void)
282 {
283 if (g_codecComponentManagerProxy.remoteOmx != NULL) {
284 HdfRemoteServiceRecycle(g_codecComponentManagerProxy.remoteOmx);
285 g_codecComponentManagerProxy.remoteOmx = NULL;
286 }
287 }