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