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 "codec_component_type_service.h"
17 #include <hdf_base.h>
18 #include <osal_mem.h>
19 #include <securec.h>
20 #include <unistd.h>
21 #include "codec_adapter_interface.h"
22 #include "codec_component_type_stub.h"
23 #include "codec_log_wrapper.h"
24 
25 struct CodecComponentTypeService {
26     struct CodecComponentTypeStub stub;
27     struct CodecComponentNode *codecNode;
28 };
29 
CodecComponentTypeGetComponentVersion(struct CodecComponentType * self,struct CompVerInfo * verInfo)30 static int32_t CodecComponentTypeGetComponentVersion(struct CodecComponentType *self, struct CompVerInfo *verInfo)
31 {
32     CODEC_LOGI("service impl!");
33     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
34     if (service == NULL) {
35         CODEC_LOGE("invalid parameter");
36         return HDF_ERR_INVALID_PARAM;
37     }
38     return OmxAdapterComponentVersion(service->codecNode, verInfo);
39 }
40 
CodecComponentTypeSendCommand(struct CodecComponentType * self,enum OMX_COMMANDTYPE cmd,uint32_t param,int8_t * cmdData,uint32_t cmdDataLen)41 static int32_t CodecComponentTypeSendCommand(struct CodecComponentType *self,
42     enum OMX_COMMANDTYPE cmd, uint32_t param, int8_t *cmdData, uint32_t cmdDataLen)
43 {
44     CODEC_LOGI("service impl!, type [%{public}d], cmd [%{public}d]", (uint32_t)cmd, param);
45 
46     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
47     if (service == NULL) {
48         CODEC_LOGE("invalid parameter");
49         return HDF_ERR_INVALID_PARAM;
50     }
51     return OmxAdapterSendCommand(service->codecNode, cmd, param, cmdData, cmdDataLen);
52 }
53 
CodecComponentTypeGetParameter(struct CodecComponentType * self,uint32_t paramIndex,int8_t * paramStruct,uint32_t paramStructLen)54 static int32_t CodecComponentTypeGetParameter(struct CodecComponentType *self,
55     uint32_t paramIndex, int8_t *paramStruct, uint32_t paramStructLen)
56 {
57     CODEC_LOGI("service impl, index [%{public}x]!", paramIndex);
58 
59     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
60     if (service == NULL) {
61         CODEC_LOGE("invalid parameter");
62         return HDF_ERR_INVALID_PARAM;
63     }
64     int32_t err = OmxAdapterGetParameter(service->codecNode, paramIndex, paramStruct, paramStructLen);
65     if (err != HDF_SUCCESS) {
66         CODEC_LOGE("index [%{public}u], ret value [%{public}x]!", paramIndex, err);
67     }
68 
69     return err;
70 }
71 
CodecComponentTypeSetParameter(struct CodecComponentType * self,uint32_t index,int8_t * paramStruct,uint32_t paramStructLen)72 static int32_t CodecComponentTypeSetParameter(struct CodecComponentType *self,
73     uint32_t index, int8_t *paramStruct, uint32_t paramStructLen)
74 {
75     CODEC_LOGI("service impl, index [%{public}x]!", index);
76 
77     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
78     if (service == NULL) {
79         CODEC_LOGE("invalid parameter");
80         return HDF_ERR_INVALID_PARAM;
81     }
82     int32_t err = OmxAdapterSetParameter(service->codecNode, index, paramStruct, paramStructLen);
83     if (err != HDF_SUCCESS) {
84         CODEC_LOGE("index [%{public}u], ret value [%{public}x]!", index, err);
85     }
86     return err;
87 }
88 
CodecComponentTypeGetConfig(struct CodecComponentType * self,uint32_t index,int8_t * cfgStruct,uint32_t cfgStructLen)89 static int32_t CodecComponentTypeGetConfig(struct CodecComponentType *self,
90     uint32_t index, int8_t *cfgStruct, uint32_t cfgStructLen)
91 {
92     CODEC_LOGI("service impl, index [%{public}x]!", index);
93 
94     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
95     if (service == NULL) {
96         CODEC_LOGE("invalid parameter");
97         return HDF_ERR_INVALID_PARAM;
98     }
99     int32_t err = OmxAdapterGetConfig(service->codecNode, index, cfgStruct, cfgStructLen);
100     if (err != HDF_SUCCESS) {
101         CODEC_LOGE("index [%{public}u], ret value [%{public}x]!", index, err);
102     }
103     return err;
104 }
105 
CodecComponentTypeSetConfig(struct CodecComponentType * self,uint32_t index,int8_t * cfgStruct,uint32_t cfgStructLen)106 static int32_t CodecComponentTypeSetConfig(struct CodecComponentType *self,
107     uint32_t index, int8_t *cfgStruct, uint32_t cfgStructLen)
108 {
109     CODEC_LOGI("service impl, index [%{public}x]!", index);
110 
111     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
112     if (service == NULL) {
113         CODEC_LOGE("invalid parameter");
114         return HDF_ERR_INVALID_PARAM;
115     }
116     int32_t err = OmxAdapterSetConfig(service->codecNode, index, cfgStruct, cfgStructLen);
117     if (err != HDF_SUCCESS) {
118         CODEC_LOGE("index [%{public}u], ret value [%{public}x]!", index, err);
119     }
120     return err;
121 }
122 
CodecComponentTypeGetExtensionIndex(struct CodecComponentType * self,const char * paramName,uint32_t * indexType)123 static int32_t CodecComponentTypeGetExtensionIndex(struct CodecComponentType *self,
124     const char *paramName, uint32_t *indexType)
125 {
126     CODEC_LOGI("service impl!");
127     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
128     if (service == NULL) {
129         CODEC_LOGE("invalid parameter");
130         return HDF_ERR_INVALID_PARAM;
131     }
132     return OmxAdapterGetExtensionIndex(service->codecNode, paramName, (enum OMX_INDEXTYPE *)indexType);
133 }
134 
CodecComponentTypeGetState(struct CodecComponentType * self,enum OMX_STATETYPE * state)135 static int32_t CodecComponentTypeGetState(struct CodecComponentType *self, enum OMX_STATETYPE *state)
136 {
137     CODEC_LOGI("service impl!");
138 
139     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
140     if (service == NULL) {
141         CODEC_LOGE("invalid parameter");
142         return HDF_ERR_INVALID_PARAM;
143     }
144     return OmxAdapterGetState(service->codecNode, state);
145 }
146 
CodecComponentTypeComponentTunnelRequest(struct CodecComponentType * self,uint32_t port,int32_t tunneledComp,uint32_t tunneledPort,struct OMX_TUNNELSETUPTYPE * tunnelSetup)147 static int32_t CodecComponentTypeComponentTunnelRequest(struct CodecComponentType *self,
148     uint32_t port, int32_t tunneledComp, uint32_t tunneledPort,
149     struct OMX_TUNNELSETUPTYPE *tunnelSetup)
150 {
151     CODEC_LOGI("service impl!");
152 
153     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
154     if (service == NULL) {
155         CODEC_LOGE("invalid parameter");
156         return HDF_ERR_INVALID_PARAM;
157     }
158     return OmxAdapterComponentTunnelRequest(service->codecNode, port, tunneledComp, tunneledPort, tunnelSetup);
159 }
160 
CodecComponentTypeUseBuffer(struct CodecComponentType * self,uint32_t portIndex,struct OmxCodecBuffer * buffer)161 static int32_t CodecComponentTypeUseBuffer(struct CodecComponentType *self,
162     uint32_t portIndex, struct OmxCodecBuffer *buffer)
163 {
164     CODEC_LOGI("service impl, portIndex: [%{public}d]!", portIndex);
165 
166     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
167     if (service == NULL) {
168         CODEC_LOGE("invalid parameter");
169         return HDF_ERR_INVALID_PARAM;
170     }
171     return OmxAdapterUseBuffer(service->codecNode, portIndex, buffer);
172 }
173 
CodecComponentTypeAllocateBuffer(struct CodecComponentType * self,uint32_t portIndex,struct OmxCodecBuffer * buffer)174 static int32_t CodecComponentTypeAllocateBuffer(struct CodecComponentType *self,
175     uint32_t portIndex, struct OmxCodecBuffer *buffer)
176 {
177     CODEC_LOGI("service impl, portIndex: [%{public}d]!", portIndex);
178 
179     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
180     if (service == NULL) {
181         CODEC_LOGE("invalid parameter");
182         return HDF_ERR_INVALID_PARAM;
183     }
184     return OmxAdapterAllocateBuffer(service->codecNode, portIndex, buffer);
185 }
186 
CodecComponentTypeFreeBuffer(struct CodecComponentType * self,uint32_t portIndex,const struct OmxCodecBuffer * buffer)187 static int32_t CodecComponentTypeFreeBuffer(struct CodecComponentType *self, uint32_t portIndex,
188     const struct OmxCodecBuffer *buffer)
189 {
190     CODEC_LOGI("service impl, portIndex: [%{public}d], bufferId: [%{public}d]!",
191         portIndex, buffer->bufferId);
192 
193     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
194     if (service == NULL) {
195         CODEC_LOGE("invalid parameter");
196         return HDF_ERR_INVALID_PARAM;
197     }
198     return OmxAdapterFreeBuffer(service->codecNode, portIndex, (struct OmxCodecBuffer *)buffer);
199 }
200 
CodecComponentTypeEmptyThisBuffer(struct CodecComponentType * self,const struct OmxCodecBuffer * buffer)201 static int32_t CodecComponentTypeEmptyThisBuffer(struct CodecComponentType *self,
202     const struct OmxCodecBuffer *buffer)
203 {
204     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
205     if (service == NULL) {
206         CODEC_LOGE("invalid parameter");
207         return HDF_ERR_INVALID_PARAM;
208     }
209     return OmxAdapterEmptyThisBuffer(service->codecNode, (struct OmxCodecBuffer *)buffer);
210 }
211 
CodecComponentTypeFillThisBuffer(struct CodecComponentType * self,const struct OmxCodecBuffer * buffer)212 static int32_t CodecComponentTypeFillThisBuffer(struct CodecComponentType *self,
213     const struct OmxCodecBuffer *buffer)
214 {
215     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
216     if (service == NULL) {
217         CODEC_LOGE("invalid parameter");
218         return HDF_ERR_INVALID_PARAM;
219     }
220     return OmxAdapterFillThisBuffer(service->codecNode, (struct OmxCodecBuffer *)buffer);
221 }
222 
CodecComponentTypeSetCallbacks(struct CodecComponentType * self,struct CodecCallbackType * callback,int64_t appData)223 static int32_t CodecComponentTypeSetCallbacks(struct CodecComponentType *self, struct CodecCallbackType *callback,
224                                               int64_t appData)
225 {
226     CODEC_LOGI("service impl!");
227 
228     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
229     if (service == NULL) {
230         CODEC_LOGE("invalid parameter");
231         return HDF_ERR_INVALID_PARAM;
232     }
233     return OmxAdapterSetCallbacks(service->codecNode, callback, appData);
234 }
235 
CodecComponentTypeComponentDeInit(struct CodecComponentType * self)236 static int32_t CodecComponentTypeComponentDeInit(struct CodecComponentType *self)
237 {
238     CODEC_LOGI("service impl!");
239 
240     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
241     if (service == NULL) {
242         CODEC_LOGE("invalid parameter");
243         return HDF_ERR_INVALID_PARAM;
244     }
245     return OmxAdapterDeInit(service->codecNode);
246 }
247 
CodecComponentTypeUseEglImage(struct CodecComponentType * self,struct OmxCodecBuffer * buffer,uint32_t portIndex,int8_t * eglImage,uint32_t eglImageLen)248 static int32_t CodecComponentTypeUseEglImage(struct CodecComponentType *self,
249     struct OmxCodecBuffer *buffer, uint32_t portIndex, int8_t *eglImage, uint32_t eglImageLen)
250 {
251     CODEC_LOGI("service impl!");
252 
253     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
254     if (service == NULL) {
255         CODEC_LOGE("invalid parameter");
256         return HDF_ERR_INVALID_PARAM;
257     }
258     return OmxAdapterUseEglImage(service->codecNode, buffer, portIndex, eglImage, eglImageLen);
259 }
260 
CodecComponentTypeComponentRoleEnum(struct CodecComponentType * self,uint8_t * role,uint32_t roleLen,uint32_t index)261 static int32_t CodecComponentTypeComponentRoleEnum(struct CodecComponentType *self,
262     uint8_t *role, uint32_t roleLen, uint32_t index)
263 {
264     CODEC_LOGI("service impl!");
265 
266     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
267     if (service == NULL) {
268         CODEC_LOGE("invalid parameter");
269         return HDF_ERR_INVALID_PARAM;
270     }
271     return OmxAdapterComponentRoleEnum(service->codecNode, role, roleLen, index);
272 }
273 
CodecComponentTypeServiceConstruct(struct CodecComponentType * instance)274 void CodecComponentTypeServiceConstruct(struct CodecComponentType *instance)
275 {
276     if (instance == NULL) {
277         CODEC_LOGE("invalid parameter");
278         return;
279     }
280     instance->GetComponentVersion = CodecComponentTypeGetComponentVersion;
281     instance->SendCommand = CodecComponentTypeSendCommand;
282     instance->GetParameter = CodecComponentTypeGetParameter;
283     instance->SetParameter = CodecComponentTypeSetParameter;
284     instance->GetConfig = CodecComponentTypeGetConfig;
285     instance->SetConfig = CodecComponentTypeSetConfig;
286     instance->GetExtensionIndex = CodecComponentTypeGetExtensionIndex;
287     instance->GetState = CodecComponentTypeGetState;
288     instance->ComponentTunnelRequest = CodecComponentTypeComponentTunnelRequest;
289     instance->UseBuffer = CodecComponentTypeUseBuffer;
290     instance->AllocateBuffer = CodecComponentTypeAllocateBuffer;
291     instance->FreeBuffer = CodecComponentTypeFreeBuffer;
292     instance->EmptyThisBuffer = CodecComponentTypeEmptyThisBuffer;
293     instance->FillThisBuffer = CodecComponentTypeFillThisBuffer;
294     instance->SetCallbacks = CodecComponentTypeSetCallbacks;
295     instance->ComponentDeInit = CodecComponentTypeComponentDeInit;
296     instance->UseEglImage = CodecComponentTypeUseEglImage;
297     instance->ComponentRoleEnum = CodecComponentTypeComponentRoleEnum;
298 }
CodecComponentTypeServiceGet(void)299 struct CodecComponentType *CodecComponentTypeServiceGet(void)
300 {
301     struct CodecComponentTypeService *service =
302         (struct CodecComponentTypeService *)OsalMemCalloc(sizeof(struct CodecComponentTypeService));
303     if (service == NULL) {
304         CODEC_LOGE("malloc CodecComponentTypeService obj failed!");
305         return NULL;
306     }
307 
308     if (!CodecComponentTypeStubConstruct(&service->stub)) {
309         CODEC_LOGE("construct CodecComponentTypeStub obj failed!");
310         OsalMemFree(service);
311         return NULL;
312     }
313     CodecComponentTypeServiceConstruct(&service->stub.interface);
314     return &service->stub.interface;
315 }
316 
CodecComponentTypeServiceRelease(struct CodecComponentType * self)317 void CodecComponentTypeServiceRelease(struct CodecComponentType *self)
318 {
319     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
320     if (service == NULL) {
321         return;
322     }
323 
324     CodecComponentTypeStubRelease(&service->stub);
325     service->codecNode = NULL;
326     OsalMemFree(service);
327 }
328 
CodecComponentTypeServiceSetCodecNode(struct CodecComponentType * self,struct CodecComponentNode * codecNode)329 void CodecComponentTypeServiceSetCodecNode(struct CodecComponentType *self, struct CodecComponentNode *codecNode)
330 {
331     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
332     if (service == NULL) {
333         return;
334     }
335     service->codecNode = codecNode;
336 }
337 
CodecComponentTypeServiceGetCodecNode(struct CodecComponentType * self)338 struct CodecComponentNode *CodecComponentTypeServiceGetCodecNode(struct CodecComponentType *self)
339 {
340     struct CodecComponentTypeService *service = (struct CodecComponentTypeService *)self;
341     if (service == NULL) {
342         return NULL;
343     }
344     return service->codecNode;
345 }