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 #include "codec_component_manager_stub.h"
16 #include <dlfcn.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_device_object.h>
19 #include <osal_mem.h>
20 #include <securec.h>
21 #include "codec_component_capability_config.h"
22 #include "codec_component_manager_service.h"
23 #include "codec_util.h"
24 #include "codec_log_wrapper.h"
25
26 #define CODEC_SERVICE_IMPL "libcodec_hdi_omx_service_impl"
27 typedef void (*SERVICE_CONSTRUCT_FUNC)(struct OmxComponentManager *);
SerStubGetComponentNum(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)28 static int32_t SerStubGetComponentNum(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
29 struct HdfSBuf *reply)
30 {
31 if (serviceImpl == NULL) {
32 CODEC_LOGE("invalid paramter");
33 return HDF_ERR_INVALID_PARAM;
34 }
35 int32_t num = serviceImpl->GetComponentNum();
36 if (!HdfSbufWriteInt32(reply, num)) {
37 CODEC_LOGE("write num failed!");
38 return HDF_ERR_INVALID_PARAM;
39 }
40 return HDF_SUCCESS;
41 }
42
SerStubGetComponentCapablityList(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)43 static int32_t SerStubGetComponentCapablityList(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
44 struct HdfSBuf *reply)
45 {
46 if (serviceImpl == NULL) {
47 CODEC_LOGE("invalid paramter");
48 return HDF_ERR_INVALID_PARAM;
49 }
50 int32_t count = 0;
51 int32_t err = HDF_SUCCESS;
52 CodecCompCapability *caps = NULL;
53 if (!HdfSbufReadInt32(data, &count) || (count <= 0)) {
54 CODEC_LOGE("read count failed!");
55 return HDF_ERR_INVALID_PARAM;
56 }
57 caps = (CodecCompCapability *)OsalMemCalloc(sizeof(CodecCompCapability) * (count));
58 if (caps == NULL) {
59 CODEC_LOGE("alloc caps failed!");
60 return HDF_ERR_INVALID_PARAM;
61 }
62 err = serviceImpl->GetComponentCapabilityList(caps, count);
63 if (err != HDF_SUCCESS) {
64 OsalMemFree(caps);
65 CODEC_LOGE("call GetComponentCapabilityList function failed!");
66 return err;
67 }
68
69 for (int32_t i = 0; i < count; i++) {
70 if (!CodecCompCapabilityBlockMarshalling(reply, &caps[i])) {
71 CODEC_LOGE("call CodecCompCapabilityBlockMarshalling function failed!");
72 err = HDF_ERR_INVALID_PARAM;
73 break;
74 }
75 }
76 OsalMemFree(caps);
77 return err;
78 }
79
ReadParamsForCreateComponent(struct HdfSBuf * data,char ** compName,int64_t * appData,struct CodecCallbackType ** callback)80 static int32_t ReadParamsForCreateComponent(struct HdfSBuf *data, char **compName, int64_t *appData,
81 struct CodecCallbackType **callback)
82 {
83 const char *compNameCp = HdfSbufReadString(data);
84 if (compNameCp == NULL) {
85 CODEC_LOGE("read compNameCp failed!");
86 return HDF_ERR_INVALID_PARAM;
87 }
88
89 if (!HdfSbufReadInt64(data, appData)) {
90 CODEC_LOGE("read appData failed!");
91 return HDF_ERR_INVALID_PARAM;
92 }
93 *compName = strdup(compNameCp);
94
95 struct HdfRemoteService *callbackRemote = HdfSbufReadRemoteService(data);
96 if (callbackRemote == NULL) {
97 CODEC_LOGE("read callbackRemote failed!");
98 return HDF_ERR_INVALID_PARAM;
99 }
100 *callback = CodecCallbackTypeGet(callbackRemote);
101
102 return HDF_SUCCESS;
103 }
104
SerStubCreateComponent(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)105 static int32_t SerStubCreateComponent(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
106 struct HdfSBuf *reply)
107 {
108 if (serviceImpl == NULL) {
109 CODEC_LOGE("invalid paramter");
110 return HDF_ERR_INVALID_PARAM;
111 }
112 int32_t ret = HDF_SUCCESS;
113 struct CodecComponentType *component = NULL;
114 uint32_t componentId = 0;
115 int64_t appData = 0;
116 struct CodecCallbackType *callback = NULL;
117 char *compName = NULL;
118
119 ret = ReadParamsForCreateComponent(data, &compName, &appData, &callback);
120 if (ret != HDF_SUCCESS) {
121 if (compName != NULL) {
122 OsalMemFree(compName);
123 compName = NULL;
124 }
125 return ret;
126 }
127 ret = serviceImpl->CreateComponent(&component, &componentId, compName, appData, callback);
128 if (component == NULL) {
129 CODEC_LOGE("fail to create component");
130 return ret;
131 }
132 if (compName != NULL) {
133 OsalMemFree(compName);
134 compName = NULL;
135 }
136 if (ret != HDF_SUCCESS) {
137 CODEC_LOGE("call CreateComponent function failed!");
138 return ret;
139 }
140
141 if (HdfSbufWriteRemoteService(reply, component->AsObject(component)) != 0) {
142 CODEC_LOGE("write component failed!");
143 return HDF_ERR_INVALID_PARAM;
144 }
145 if (!HdfSbufWriteUint32(reply, componentId)) {
146 CODEC_LOGE("write componentId failed!");
147 return HDF_ERR_INVALID_PARAM;
148 }
149 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
150 ReleaseCodecCache();
151 #endif
152 return ret;
153 }
154
SerStubDestroyComponent(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)155 static int32_t SerStubDestroyComponent(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
156 struct HdfSBuf *reply)
157 {
158 if (serviceImpl == NULL) {
159 CODEC_LOGE("invalid paramter");
160 return HDF_ERR_INVALID_PARAM;
161 }
162 uint32_t componentId = 0;
163 if (!HdfSbufReadUint32(data, &componentId)) {
164 CODEC_LOGE("read componentId failed!");
165 return HDF_ERR_INVALID_PARAM;
166 }
167 int32_t ret = serviceImpl->DestroyComponent(componentId);
168 if (ret != HDF_SUCCESS) {
169 CODEC_LOGE("call DestroyComponent function failed!");
170 }
171 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
172 ReleaseCodecCache();
173 #endif
174 return ret;
175 }
176
CodecComponentManagerServiceOnRemoteRequest(struct CodecComponentManager * serviceImpl,int32_t cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)177 static int32_t CodecComponentManagerServiceOnRemoteRequest(struct CodecComponentManager *serviceImpl, int32_t cmdId,
178 struct HdfSBuf *data, struct HdfSBuf *reply)
179 {
180 switch (cmdId) {
181 case CMD_CODEC_GET_COMPONENT_NUM:
182 return SerStubGetComponentNum(serviceImpl, data, reply);
183 case CMD_CODEC_GET_COMPONENT_CAPABILITY_LIST:
184 return SerStubGetComponentCapablityList(serviceImpl, data, reply);
185 case CMD_CREATE_COMPONENT:
186 return SerStubCreateComponent(serviceImpl, data, reply);
187 case CMD_DESTROY_COMPONENT:
188 return SerStubDestroyComponent(serviceImpl, data, reply);
189 default:
190 CODEC_LOGE("not support cmd %{public}d", cmdId);
191 return HDF_ERR_INVALID_PARAM;
192 }
193 }
194
CodecComponentManagerStubAsObject(struct CodecComponentManager * self)195 static struct HdfRemoteService *CodecComponentManagerStubAsObject(struct CodecComponentManager *self)
196 {
197 return NULL;
198 }
199
CodecComponentManagerStubConstruct(struct CodecComponentManagerStub * stub)200 bool CodecComponentManagerStubConstruct(struct CodecComponentManagerStub *stub)
201 {
202 if (stub == NULL) {
203 CODEC_LOGE("stub is null!");
204 return false;
205 }
206
207 stub->OnRemoteRequest = CodecComponentManagerServiceOnRemoteRequest;
208 stub->interface.AsObject = CodecComponentManagerStubAsObject;
209 return true;
210 }