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