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
CreateComponent(struct CodecComponentType ** component,uint32_t * componentId,char * compName,int64_t appData,struct CodecCallbackType * callback)134 static int32_t CreateComponent(struct CodecComponentType **component, uint32_t *componentId, char *compName,
135 int64_t appData, struct CodecCallbackType *callback)
136 {
137 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
138 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
139 if (data == NULL || reply == NULL || componentId == NULL) {
140 CODEC_LOGE("HdfSubf malloc failed!");
141 ReleaseSbuf(data, reply);
142 return HDF_ERR_MALLOC_FAIL;
143 }
144
145 if (!HdfRemoteServiceWriteInterfaceToken(g_codecComponentManagerProxy.remoteOmx, data)) {
146 CODEC_LOGE("write interface token failed");
147 ReleaseSbuf(data, reply);
148 return HDF_FAILURE;
149 }
150 if (!HdfSbufWriteString(data, compName)) {
151 CODEC_LOGE("write paramName failed!");
152 ReleaseSbuf(data, reply);
153 return HDF_ERR_INVALID_PARAM;
154 }
155 if (!HdfSbufWriteInt64(data, appData)) {
156 CODEC_LOGE("write appData failed!");
157 ReleaseSbuf(data, reply);
158 return HDF_ERR_INVALID_PARAM;
159 }
160 if (HdfSbufWriteRemoteService(data, callback->remote) != 0) {
161 CODEC_LOGE("write callback failed!");
162 ReleaseSbuf(data, reply);
163 return HDF_ERR_INVALID_PARAM;
164 }
165 int32_t ret = g_codecComponentManagerProxy.remoteOmx->dispatcher->Dispatch(g_codecComponentManagerProxy.remoteOmx,
166 CMD_CREATE_COMPONENT, data, reply);
167 if (ret != HDF_SUCCESS) {
168 CODEC_LOGE("call failed! error code is %{public}d", ret);
169 ReleaseSbuf(data, reply);
170 return ret;
171 }
172
173 struct HdfRemoteService *componentRemote = HdfSbufReadRemoteService(reply);
174 if (componentRemote == NULL) {
175 CODEC_LOGE("read componentRemote failed!");
176 ReleaseSbuf(data, reply);
177 return HDF_ERR_INVALID_PARAM;
178 }
179 if (!HdfSbufReadUint32(reply, componentId)) {
180 CODEC_LOGE("read componentId failed!");
181 ReleaseSbuf(data, reply);
182 return HDF_ERR_INVALID_PARAM;
183 }
184 *component = CodecComponentTypeGet(componentRemote);
185 ReleaseSbuf(data, reply);
186 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
187 ReleaseCodecCache();
188 #endif
189 return ret;
190 }
191
DestroyComponent(uint32_t componentId)192 static int32_t DestroyComponent(uint32_t componentId)
193 {
194 int32_t ret;
195
196 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
197 struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
198 if (data == NULL || reply == NULL) {
199 CODEC_LOGE("HdfSubf malloc failed!");
200 ReleaseSbuf(data, reply);
201 return HDF_ERR_MALLOC_FAIL;
202 }
203
204 if (!HdfRemoteServiceWriteInterfaceToken(g_codecComponentManagerProxy.remoteOmx, data)) {
205 CODEC_LOGE("write interface token failed");
206 ReleaseSbuf(data, reply);
207 return HDF_FAILURE;
208 }
209
210 if (!HdfSbufWriteUint32(data, componentId)) {
211 CODEC_LOGE("write componentId failed!");
212 ReleaseSbuf(data, reply);
213 return HDF_ERR_INVALID_PARAM;
214 }
215
216 ret = g_codecComponentManagerProxy.remoteOmx->dispatcher->Dispatch(g_codecComponentManagerProxy.remoteOmx,
217 CMD_DESTROY_COMPONENT, data, reply);
218 if (ret != HDF_SUCCESS) {
219 CODEC_LOGE("call failed! error code is %{public}d", ret);
220 ReleaseSbuf(data, reply);
221 return ret;
222 }
223 ReleaseSbuf(data, reply);
224 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
225 ReleaseCodecCache();
226 #endif
227 return ret;
228 }
229
InitCodecComponentManagerProxy(void)230 static int32_t InitCodecComponentManagerProxy(void)
231 {
232 if (g_codecComponentManagerProxy.remoteOmx != NULL) {
233 return HDF_SUCCESS;
234 }
235
236 struct HDIServiceManager *serviceMgr = HDIServiceManagerGet();
237 if (serviceMgr == NULL) {
238 CODEC_LOGE("HDIServiceManager not found!");
239 return HDF_FAILURE;
240 }
241
242 struct HdfRemoteService *remoteOmx = serviceMgr->GetService(serviceMgr, CODEC_HDI_OMX_SERVICE_NAME);
243 HDIServiceManagerRelease(serviceMgr);
244 if (remoteOmx == NULL) {
245 CODEC_LOGE("CodecComponentTypeService not found!");
246 return HDF_FAILURE;
247 }
248 if (!HdfRemoteServiceSetInterfaceDesc(remoteOmx, "ohos.hdi.codec_service")) {
249 CODEC_LOGE("failed to init interface desc");
250 HdfRemoteServiceRecycle(remoteOmx);
251 return HDF_FAILURE;
252 }
253
254 g_codecComponentManagerProxy.remoteOmx = remoteOmx;
255 g_codecComponentManagerProxy.instance.GetComponentNum = GetComponentNum;
256 g_codecComponentManagerProxy.instance.GetComponentCapabilityList = GetComponentCapabilityList;
257 g_codecComponentManagerProxy.instance.CreateComponent = CreateComponent;
258 g_codecComponentManagerProxy.instance.DestroyComponent = DestroyComponent;
259
260 return HDF_SUCCESS;
261 }
262
GetCodecComponentManager(void)263 struct CodecComponentManager *GetCodecComponentManager(void)
264 {
265 if (InitCodecComponentManagerProxy() != HDF_SUCCESS) {
266 return NULL;
267 }
268 return &g_codecComponentManagerProxy.instance;
269 }
270
CodecComponentManagerRelease(void)271 void CodecComponentManagerRelease(void)
272 {
273 if (g_codecComponentManagerProxy.remoteOmx != NULL) {
274 HdfRemoteServiceRecycle(g_codecComponentManagerProxy.remoteOmx);
275 g_codecComponentManagerProxy.remoteOmx = NULL;
276 }
277 }