1 /*
2 * Copyright (c) 2021 Huawei Device 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 "proxy_msgproc.h"
17 #include <hdf_log.h>
18 #include <osal_mem.h>
19 #include <servmgr_hdi.h>
20 #include "icodec.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif /* __cplusplus */
25
CodecProxyCall(struct ICodec * self,int32_t id,struct HdfSBuf * data,struct HdfSBuf * reply)26 static int32_t CodecProxyCall(struct ICodec *self,
27 int32_t id, struct HdfSBuf *data, struct HdfSBuf *reply)
28 {
29 if (self->remote == NULL || self->remote->dispatcher == NULL ||
30 self->remote->dispatcher->Dispatch == NULL) {
31 HDF_LOGE("%{public}s: obj is null", __func__);
32 return HDF_ERR_INVALID_OBJECT;
33 }
34 return self->remote->dispatcher->Dispatch(self->remote, id, data, reply);
35 }
36
CodecProxyReqSBuf(struct HdfSBuf ** data,struct HdfSBuf ** reply)37 int32_t CodecProxyReqSBuf(struct HdfSBuf **data, struct HdfSBuf **reply)
38 {
39 *data = HdfSbufTypedObtain(SBUF_IPC);
40 if (*data == NULL) {
41 HDF_LOGE("%{public}s: Failed to obtain", __func__);
42 return HDF_FAILURE;
43 }
44 *reply = HdfSbufTypedObtain(SBUF_IPC);
45 if (*reply == NULL) {
46 HDF_LOGE("%{public}s: Failed to obtain reply", __func__);
47 HdfSbufRecycle(*data);
48 return HDF_FAILURE;
49 }
50 return HDF_SUCCESS;
51 }
CodecProxySBufRecycle(struct HdfSBuf * data,struct HdfSBuf * reply)52 void CodecProxySBufRecycle(struct HdfSBuf *data, struct HdfSBuf *reply)
53 {
54 if (data != NULL) {
55 HdfSbufRecycle(data);
56 }
57 if (reply != NULL) {
58 HdfSbufRecycle(reply);
59 }
60 return;
61 }
62
CodecPorxyInit(struct ICodec * self)63 int32_t CodecPorxyInit(struct ICodec *self)
64 {
65 int32_t ret;
66 struct HdfSBuf *data = NULL;
67 struct HdfSBuf *reply = NULL;
68 if (self == NULL) {
69 return HDF_ERR_INVALID_PARAM;
70 }
71 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
72 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
73 return HDF_FAILURE;
74 }
75 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
76 HDF_LOGE("write interface token failed");
77 CodecProxySBufRecycle(data, reply);
78 return HDF_ERR_INVALID_PARAM;
79 }
80 ret = CodecProxyCall(self, CMD_CODEC_INIT, data, reply);
81 if (ret != HDF_SUCCESS) {
82 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
83 }
84 CodecProxySBufRecycle(data, reply);
85 return ret;
86 }
87
CodecProxyDeinit(struct ICodec * self)88 int32_t CodecProxyDeinit(struct ICodec *self)
89 {
90 int32_t ret;
91 struct HdfSBuf *data = NULL;
92 struct HdfSBuf *reply = NULL;
93 if (self == NULL) {
94 return HDF_ERR_INVALID_PARAM;
95 }
96 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
97 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
98 return HDF_FAILURE;
99 }
100 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
101 HDF_LOGE("write interface token failed");
102 CodecProxySBufRecycle(data, reply);
103 return HDF_ERR_INVALID_PARAM;
104 }
105 ret = CodecProxyCall(self, CMD_CODEC_DEINIT, data, reply);
106 if (ret != HDF_SUCCESS) {
107 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
108 CodecProxySBufRecycle(data, reply);
109 return ret;
110 }
111 CodecProxySBufRecycle(data, reply);
112 return ret;
113 }
114
CodecProxyEnumerateCapbility(struct ICodec * self,uint32_t index,CodecCapbility * cap)115 int32_t CodecProxyEnumerateCapbility(struct ICodec *self, uint32_t index, CodecCapbility *cap)
116 {
117 int32_t ret;
118 struct HdfSBuf *data = NULL;
119 struct HdfSBuf *reply = NULL;
120 if (self == NULL || cap == NULL) {
121 return HDF_ERR_INVALID_PARAM;
122 }
123 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
124 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
125 return HDF_FAILURE;
126 }
127 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
128 HDF_LOGE("write interface token failed");
129 CodecProxySBufRecycle(data, reply);
130 return HDF_ERR_INVALID_PARAM;
131 }
132 if (!HdfSbufWriteUint32(data, index)) {
133 HDF_LOGE("%{public}s: write input index failed!", __func__);
134 CodecProxySBufRecycle(data, reply);
135 return HDF_ERR_INVALID_PARAM;
136 }
137 ret = CodecProxyCall(self, CMD_CODEC_ENUM_CAP, data, reply);
138 if (ret != HDF_SUCCESS) {
139 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
140 CodecProxySBufRecycle(data, reply);
141 return ret;
142 }
143 if (CodecProxyParseGottenCapbility(reply, cap) != HDF_SUCCESS) {
144 HDF_LOGE("%{public}s: CodecProxyParseGottenCapbility failed!", __func__);
145 CodecProxySBufRecycle(data, reply);
146 return HDF_ERR_INVALID_PARAM;
147 }
148 CodecProxySBufRecycle(data, reply);
149 return ret;
150 }
151
CodecProxyGetCapbility(struct ICodec * self,AvCodecMime mime,CodecType type,uint32_t flags,CodecCapbility * cap)152 int32_t CodecProxyGetCapbility(struct ICodec *self, AvCodecMime mime, CodecType type,
153 uint32_t flags, CodecCapbility *cap)
154 {
155 int32_t ret;
156 struct HdfSBuf *data = NULL;
157 struct HdfSBuf *reply = NULL;
158 if (self == NULL || cap == NULL) {
159 return HDF_ERR_INVALID_PARAM;
160 }
161 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
162 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
163 return HDF_FAILURE;
164 }
165 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
166 HDF_LOGE("write interface token failed");
167 CodecProxySBufRecycle(data, reply);
168 return HDF_ERR_INVALID_PARAM;
169 }
170 if (!HdfSbufWriteUint32(data, (uint32_t)mime)) {
171 HDF_LOGE("%{public}s: write input mime failed!", __func__);
172 CodecProxySBufRecycle(data, reply);
173 return HDF_ERR_INVALID_PARAM;
174 }
175 if (!HdfSbufWriteUint32(data, (uint32_t)type)) {
176 HDF_LOGE("%{public}s: write input type failed!", __func__);
177 CodecProxySBufRecycle(data, reply);
178 return HDF_ERR_INVALID_PARAM;
179 }
180 if (!HdfSbufWriteUint32(data, flags)) {
181 HDF_LOGE("%{public}s: write input flags failed!", __func__);
182 CodecProxySBufRecycle(data, reply);
183 return HDF_ERR_INVALID_PARAM;
184 }
185 ret = CodecProxyCall(self, CMD_CODEC_GET_CAP, data, reply);
186 if (ret != HDF_SUCCESS) {
187 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
188 CodecProxySBufRecycle(data, reply);
189 return ret;
190 }
191 if (CodecProxyParseGottenCapbility(reply, cap) != HDF_SUCCESS) {
192 HDF_LOGE("%{public}s: CodecProxyParseGottenCapbility failed!", __func__);
193 CodecProxySBufRecycle(data, reply);
194 return HDF_ERR_INVALID_PARAM;
195 }
196 CodecProxySBufRecycle(data, reply);
197 return ret;
198 }
199
CodecProxyCreate(struct ICodec * self,const char * name,const Param * attr,int len,CODEC_HANDLETYPE * handle)200 int32_t CodecProxyCreate(struct ICodec *self, const char* name, const Param *attr, int len, CODEC_HANDLETYPE *handle)
201 {
202 struct HdfSBuf *data = NULL;
203 struct HdfSBuf *reply = NULL;
204 if (self == NULL || name == NULL || attr == NULL || handle == NULL) {
205 return HDF_ERR_INVALID_PARAM;
206 }
207 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
208 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
209 return HDF_FAILURE;
210 }
211 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data) || !HdfSbufWriteString(data, name)) {
212 CodecProxySBufRecycle(data, reply);
213 return HDF_ERR_INVALID_PARAM;
214 }
215 if (!HdfSbufWriteUint32(data, attr->key)) {
216 CodecProxySBufRecycle(data, reply);
217 return HDF_ERR_INVALID_PARAM;
218 }
219 if (!HdfSbufWriteInt32(data, attr->size)) {
220 CodecProxySBufRecycle(data, reply);
221 return HDF_ERR_INVALID_PARAM;
222 }
223 if (!HdfSbufWriteBuffer(data, attr->val, attr->size)) {
224 CodecProxySBufRecycle(data, reply);
225 return HDF_ERR_INVALID_PARAM;
226 }
227 if (!HdfSbufWriteInt32(data, len)) {
228 CodecProxySBufRecycle(data, reply);
229 return HDF_ERR_INVALID_PARAM;
230 }
231 if (!HdfSbufWriteUint32(data, (uint32_t)handle)) {
232 CodecProxySBufRecycle(data, reply);
233 return HDF_ERR_INVALID_PARAM;
234 }
235 int32_t ret = CodecProxyCall(self, CMD_CODEC_CREATE, data, reply);
236 if (ret != HDF_SUCCESS) {
237 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
238 CodecProxySBufRecycle(data, reply);
239 return ret;
240 }
241 if (!HdfSbufReadUint32(reply, (uint32_t *)handle)) {
242 ret = HDF_ERR_INVALID_PARAM;
243 }
244 CodecProxySBufRecycle(data, reply);
245 return ret;
246 }
247
CodecProxyDestroy(struct ICodec * self,CODEC_HANDLETYPE handle)248 int32_t CodecProxyDestroy(struct ICodec *self, CODEC_HANDLETYPE handle)
249 {
250 int32_t ret;
251 struct HdfSBuf *data = NULL;
252 struct HdfSBuf *reply = NULL;
253
254 if (self == NULL || handle == NULL) {
255 return HDF_ERR_INVALID_PARAM;
256 }
257 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
258 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
259 return HDF_ERR_INVALID_PARAM;
260 }
261 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
262 HDF_LOGE("write interface token failed");
263 CodecProxySBufRecycle(data, reply);
264 return HDF_ERR_INVALID_PARAM;
265 }
266 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
267 HDF_LOGE("%{public}s: Write handle failed!", __func__);
268 CodecProxySBufRecycle(data, reply);
269 return HDF_ERR_INVALID_PARAM;
270 }
271 ret = CodecProxyCall(self, CMD_CODEC_DESTROY, data, reply);
272 if (ret != HDF_SUCCESS) {
273 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
274 }
275 CodecProxySBufRecycle(data, reply);
276 return ret;
277 }
278
CodecProxySetPortMode(struct ICodec * self,CODEC_HANDLETYPE handle,DirectionType type,BufferMode mode)279 int32_t CodecProxySetPortMode(struct ICodec *self, CODEC_HANDLETYPE handle, DirectionType type, BufferMode mode)
280 {
281 int32_t ret;
282 struct HdfSBuf *data = NULL;
283 struct HdfSBuf *reply = NULL;
284 if (self == NULL || handle == NULL) {
285 return HDF_ERR_INVALID_PARAM;
286 }
287 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
288 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
289 return HDF_FAILURE;
290 }
291 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
292 HDF_LOGE("write interface token failed");
293 CodecProxySBufRecycle(data, reply);
294 return HDF_ERR_INVALID_PARAM;
295 }
296 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
297 HDF_LOGE("%{public}s: Read size failed!", __func__);
298 CodecProxySBufRecycle(data, reply);
299 return HDF_ERR_INVALID_PARAM;
300 }
301 if (!HdfSbufWriteUint32(data, (uint32_t)type)) {
302 HDF_LOGE("%{public}s: Read size failed!", __func__);
303 CodecProxySBufRecycle(data, reply);
304 return HDF_ERR_INVALID_PARAM;
305 }
306 if (!HdfSbufWriteUint32(data, (uint32_t)mode)) {
307 HDF_LOGE("%{public}s: Read size failed!", __func__);
308 CodecProxySBufRecycle(data, reply);
309 return HDF_ERR_INVALID_PARAM;
310 }
311 ret = CodecProxyCall(self, CMD_CODEC_SET_MODE, data, reply);
312 if (ret != HDF_SUCCESS) {
313 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
314 }
315 CodecProxySBufRecycle(data, reply);
316 return ret;
317 }
318
CodecProxySetParameter(struct ICodec * self,CODEC_HANDLETYPE handle,const Param * params,int paramCnt)319 int32_t CodecProxySetParameter(struct ICodec *self, CODEC_HANDLETYPE handle, const Param *params, int paramCnt)
320 {
321 int32_t ret;
322 struct HdfSBuf *data = NULL;
323 struct HdfSBuf *reply = NULL;
324 if (self == NULL || params == NULL || paramCnt < 0) {
325 HDF_LOGE("%{public}s: params null!", __func__);
326 return HDF_ERR_INVALID_PARAM;
327 }
328 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
329 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
330 return HDF_FAILURE;
331 }
332 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
333 HDF_LOGE("write interface token failed");
334 CodecProxySBufRecycle(data, reply);
335 return HDF_ERR_INVALID_PARAM;
336 }
337 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
338 HDF_LOGE("%{public}s: write size failed!", __func__);
339 CodecProxySBufRecycle(data, reply);
340 return HDF_ERR_INVALID_PARAM;
341 }
342 if (!HdfSbufWriteInt32(data, paramCnt)) {
343 HDF_LOGE("%{public}s: write paramCnt failed!", __func__);
344 CodecProxySBufRecycle(data, reply);
345 return HDF_ERR_INVALID_PARAM;
346 }
347 for (int32_t i = 0; i < paramCnt; i++) {
348 if (CodecProxyPackParam(data, ¶ms[i]) != HDF_SUCCESS) {
349 HDF_LOGE("%{public}s: write params failed!", __func__);
350 CodecProxySBufRecycle(data, reply);
351 return HDF_FAILURE;
352 }
353 }
354 ret = CodecProxyCall(self, CMD_CODEC_SET_PARAMS, data, reply);
355 if (ret != HDF_SUCCESS) {
356 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
357 }
358 CodecProxySBufRecycle(data, reply);
359 return ret;
360 }
361
362 // params需客户端调用点释放
CodecProxyGetParameter(struct ICodec * self,CODEC_HANDLETYPE handle,Param * params,int paramCnt)363 int32_t CodecProxyGetParameter(struct ICodec *self, CODEC_HANDLETYPE handle, Param *params, int paramCnt)
364 {
365 int32_t ret;
366 struct HdfSBuf *data = NULL;
367 struct HdfSBuf *reply = NULL;
368 if (self == NULL || params == NULL) {
369 HDF_LOGE("%{public}s: params null!", __func__);
370 return HDF_ERR_INVALID_PARAM;
371 }
372 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
373 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
374 return HDF_FAILURE;
375 }
376 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data) ||
377 !HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
378 HDF_LOGE("%{public}s: write interface token or size failed!", __func__);
379 CodecProxySBufRecycle(data, reply);
380 return HDF_ERR_INVALID_PARAM;
381 }
382 if (!HdfSbufWriteInt32(data, paramCnt)) {
383 HDF_LOGE("%{public}s: write paramCnt failed!", __func__);
384 CodecProxySBufRecycle(data, reply);
385 return HDF_ERR_INVALID_PARAM;
386 }
387 for (int32_t i = 0; i < paramCnt; i++) {
388 if (CodecProxyPackParam(data, ¶ms[i]) != HDF_SUCCESS) {
389 HDF_LOGE("%{public}s: CodecProxyPackParam!", __func__);
390 CodecProxySBufRecycle(data, reply);
391 return HDF_FAILURE;
392 }
393 }
394 ret = CodecProxyCall(self, CMD_CODEC_GET_PARAMS, data, reply);
395 if (ret != HDF_SUCCESS) {
396 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
397 CodecProxySBufRecycle(data, reply);
398 return ret;
399 }
400 for (int32_t i = 0; i < paramCnt; i++) {
401 if (CodecProxyParseParam(data, ¶ms[i]) != HDF_SUCCESS) {
402 HDF_LOGE("%{public}s: read params failed!", __func__);
403 CodecProxySBufRecycle(data, reply);
404 return HDF_ERR_INVALID_PARAM;
405 }
406 }
407 CodecProxySBufRecycle(data, reply);
408 return ret;
409 }
CodecProxyStart(struct ICodec * self,CODEC_HANDLETYPE handle)410 int32_t CodecProxyStart(struct ICodec *self, CODEC_HANDLETYPE handle)
411 {
412 int32_t ret;
413 struct HdfSBuf *data = NULL;
414 struct HdfSBuf *reply = NULL;
415 if (self == NULL || handle == NULL) {
416 HDF_LOGE("%{public}s: params null!", __func__);
417 return HDF_ERR_INVALID_PARAM;
418 }
419 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
420 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
421 return HDF_FAILURE;
422 }
423 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
424 HDF_LOGE("write interface token failed");
425 CodecProxySBufRecycle(data, reply);
426 return HDF_ERR_INVALID_PARAM;
427 }
428 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
429 HDF_LOGE("%{public}s: write handle failed!", __func__);
430 CodecProxySBufRecycle(data, reply);
431 return HDF_ERR_INVALID_PARAM;
432 }
433 ret = CodecProxyCall(self, CMD_CODEC_START, data, reply);
434 if (ret != HDF_SUCCESS) {
435 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
436 }
437 CodecProxySBufRecycle(data, reply);
438 return ret;
439 }
CodecProxyStop(struct ICodec * self,CODEC_HANDLETYPE handle)440 int32_t CodecProxyStop(struct ICodec *self, CODEC_HANDLETYPE handle)
441 {
442 int32_t ret;
443 struct HdfSBuf *data = NULL;
444 struct HdfSBuf *reply = NULL;
445 if (self == NULL || handle == NULL) {
446 HDF_LOGE("%{public}s: params null!", __func__);
447 return HDF_ERR_INVALID_PARAM;
448 }
449 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
450 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
451 return HDF_FAILURE;
452 }
453 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
454 HDF_LOGE("write interface token failed");
455 CodecProxySBufRecycle(data, reply);
456 return HDF_ERR_INVALID_PARAM;
457 }
458 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
459 HDF_LOGE("%{public}s: write input handle failed!", __func__);
460 CodecProxySBufRecycle(data, reply);
461 return HDF_ERR_INVALID_PARAM;
462 }
463 ret = CodecProxyCall(self, CMD_CODEC_STOP, data, reply);
464 if (ret != HDF_SUCCESS) {
465 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
466 }
467 CodecProxySBufRecycle(data, reply);
468 return ret;
469 }
CodecProxyFlush(struct ICodec * self,CODEC_HANDLETYPE handle,DirectionType directType)470 int32_t CodecProxyFlush(struct ICodec *self, CODEC_HANDLETYPE handle, DirectionType directType)
471 {
472 int32_t ret;
473 struct HdfSBuf *data = NULL;
474 struct HdfSBuf *reply = NULL;
475 if (self == NULL || handle == NULL) {
476 HDF_LOGE("%{public}s: params null!", __func__);
477 return HDF_ERR_INVALID_PARAM;
478 }
479 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
480 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
481 return HDF_FAILURE;
482 }
483 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
484 HDF_LOGE("write interface token failed");
485 CodecProxySBufRecycle(data, reply);
486 return HDF_ERR_INVALID_PARAM;
487 }
488 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
489 HDF_LOGE("%{public}s: write input handle failed!", __func__);
490 CodecProxySBufRecycle(data, reply);
491 return HDF_ERR_INVALID_PARAM;
492 }
493 if (!HdfSbufWriteUint32(data, (uint32_t)directType)) {
494 CodecProxySBufRecycle(data, reply);
495 HDF_LOGE("%{public}s: write input directType failed!", __func__);
496 return HDF_ERR_INVALID_PARAM;
497 }
498 ret = CodecProxyCall(self, CMD_CODEC_FLUSH, data, reply);
499 if (ret != HDF_SUCCESS) {
500 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
501 }
502 CodecProxySBufRecycle(data, reply);
503 return ret;
504 }
CodecPorxyQueueInput(struct ICodec * self,CODEC_HANDLETYPE handle,const InputInfo * inputData,uint32_t timeoutMs)505 int32_t CodecPorxyQueueInput(struct ICodec *self, CODEC_HANDLETYPE handle,
506 const InputInfo *inputData, uint32_t timeoutMs)
507 {
508 int32_t ret;
509 struct HdfSBuf *data = NULL;
510 struct HdfSBuf *reply = NULL;
511 if (self == NULL || handle == NULL || inputData == NULL) {
512 HDF_LOGE("%{public}s: params null!", __func__);
513 return HDF_ERR_INVALID_PARAM;
514 }
515 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
516 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
517 return HDF_FAILURE;
518 }
519 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
520 HDF_LOGE("write interface token failed");
521 CodecProxySBufRecycle(data, reply);
522 return HDF_ERR_INVALID_PARAM;
523 }
524 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
525 HDF_LOGE("%{public}s: write input handle failed!", __func__);
526 CodecProxySBufRecycle(data, reply);
527 return HDF_ERR_INVALID_PARAM;
528 }
529 if (CodecProxyPackInputInfo(data, inputData)) {
530 HDF_LOGE("%{public}s: write input buffer failed!", __func__);
531 CodecProxySBufRecycle(data, reply);
532 return HDF_ERR_INVALID_PARAM;
533 }
534 if (!HdfSbufWriteUint32(data, timeoutMs)) {
535 HDF_LOGE("%{public}s: write input timeoutMs failed!", __func__);
536 CodecProxySBufRecycle(data, reply);
537 return HDF_ERR_INVALID_PARAM;
538 }
539 ret = CodecProxyCall(self, CMD_CODEC_QUEQUE_INPUT, data, reply);
540 if (ret != HDF_SUCCESS) {
541 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
542 }
543 CodecProxySBufRecycle(data, reply);
544 return ret;
545 }
CodecProxyDequeInput(struct ICodec * self,CODEC_HANDLETYPE handle,uint32_t timeoutMs,InputInfo * inputData)546 int32_t CodecProxyDequeInput(struct ICodec *self, CODEC_HANDLETYPE handle, uint32_t timeoutMs, InputInfo *inputData)
547 {
548 int32_t ret;
549 struct HdfSBuf *data = NULL;
550 struct HdfSBuf *reply = NULL;
551 if (self == NULL || handle == NULL || inputData == NULL || inputData->bufferCnt == 0) {
552 HDF_LOGE("%{public}s: params null!", __func__);
553 return HDF_ERR_INVALID_PARAM;
554 }
555 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
556 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
557 return HDF_FAILURE;
558 }
559 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
560 HDF_LOGE("write interface token failed");
561 CodecProxySBufRecycle(data, reply);
562 return HDF_ERR_INVALID_PARAM;
563 }
564 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
565 HDF_LOGE("%{public}s: write input handle failed!", __func__);
566 CodecProxySBufRecycle(data, reply);
567 return HDF_ERR_INVALID_PARAM;
568 }
569 if (!HdfSbufWriteUint32(data, timeoutMs)) {
570 HDF_LOGE("%{public}s: write input timeoutMs failed!", __func__);
571 CodecProxySBufRecycle(data, reply);
572 return HDF_ERR_INVALID_PARAM;
573 }
574 if (!HdfSbufWriteUint32(data, inputData->bufferCnt)) {
575 HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
576 CodecProxySBufRecycle(data, reply);
577 return HDF_ERR_INVALID_PARAM;
578 }
579 ret = CodecProxyCall(self, CMD_CODEC_DEQUEQUE_INPUT, data, reply);
580 if (ret != HDF_SUCCESS) {
581 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
582 CodecProxySBufRecycle(data, reply);
583 return ret;
584 }
585 if (CodecProxyParseInputInfo(reply, inputData)) {
586 HDF_LOGE("%{public}s: read struct reply failed!", __func__);
587 CodecProxySBufRecycle(data, reply);
588 return HDF_ERR_INVALID_PARAM;
589 }
590 CodecProxySBufRecycle(data, reply);
591 return ret;
592 }
CodecProxyQueueOutput(struct ICodec * self,CODEC_HANDLETYPE handle,OutputInfo * outInfo,uint32_t timeoutMs,int releaseFenceFd)593 int32_t CodecProxyQueueOutput(struct ICodec *self, CODEC_HANDLETYPE handle, OutputInfo *outInfo,
594 uint32_t timeoutMs, int releaseFenceFd)
595 {
596 int32_t ret;
597 struct HdfSBuf *data = NULL;
598 struct HdfSBuf *reply = NULL;
599 if (self == NULL || handle == NULL || outInfo == NULL) {
600 HDF_LOGE("%{public}s: params null!", __func__);
601 return HDF_ERR_INVALID_PARAM;
602 }
603 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
604 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
605 return HDF_FAILURE;
606 }
607 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
608 HDF_LOGE("write interface token failed");
609 CodecProxySBufRecycle(data, reply);
610 return HDF_ERR_INVALID_PARAM;
611 }
612 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
613 HDF_LOGE("%{public}s: write input handle failed!", __func__);
614 CodecProxySBufRecycle(data, reply);
615 return HDF_ERR_INVALID_PARAM;
616 }
617 if (CodecProxyPackOutputInfo(data, outInfo)) {
618 HDF_LOGE("%{public}s: write output buffer failed!", __func__);
619 CodecProxySBufRecycle(data, reply);
620 return HDF_ERR_INVALID_PARAM;
621 }
622 if (!HdfSbufWriteUint32(data, timeoutMs)) {
623 HDF_LOGE("%{public}s: write input timeoutMs failed!", __func__);
624 CodecProxySBufRecycle(data, reply);
625 return HDF_ERR_INVALID_PARAM;
626 }
627 if (!HdfSbufWriteFileDescriptor(data, releaseFenceFd)) {
628 HDF_LOGE("%{public}s: write input releaseFenceFd failed!", __func__);
629 CodecProxySBufRecycle(data, reply);
630 return HDF_ERR_INVALID_PARAM;
631 }
632 ret = CodecProxyCall(self, CMD_CODEC_QUEQUE_OUTPUT, data, reply);
633 if (ret != HDF_SUCCESS) {
634 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
635 }
636 CodecProxySBufRecycle(data, reply);
637 return ret;
638 }
CodecProxyDequeueOutput(struct ICodec * self,CODEC_HANDLETYPE handle,uint32_t timeoutMs,int * acquireFd,OutputInfo * outInfo)639 int32_t CodecProxyDequeueOutput(struct ICodec *self, CODEC_HANDLETYPE handle, uint32_t timeoutMs,
640 int *acquireFd, OutputInfo *outInfo)
641 {
642 int32_t ret;
643 struct HdfSBuf *data = NULL;
644 struct HdfSBuf *reply = NULL;
645 if (self == NULL || handle == NULL || acquireFd == NULL || outInfo == NULL || outInfo->bufferCnt == 0) {
646 HDF_LOGE("%{public}s: params null!", __func__);
647 return HDF_ERR_INVALID_PARAM;
648 }
649 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
650 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
651 return HDF_FAILURE;
652 }
653 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data) ||
654 !HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
655 HDF_LOGE("%{public}s: write interface token or input handle failed!", __func__);
656 CodecProxySBufRecycle(data, reply);
657 return HDF_ERR_INVALID_PARAM;
658 }
659 if (!HdfSbufWriteUint32(data, timeoutMs)) {
660 HDF_LOGE("%{public}s: write input timeoutMs failed!", __func__);
661 CodecProxySBufRecycle(data, reply);
662 return HDF_ERR_INVALID_PARAM;
663 }
664 if (!HdfSbufWriteUint32(data, outInfo->bufferCnt)) {
665 HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
666 CodecProxySBufRecycle(data, reply);
667 return HDF_ERR_INVALID_PARAM;
668 }
669 ret = CodecProxyCall(self, CMD_CODEC_DEQUEQUE_OUTPUT, data, reply);
670 if (ret != HDF_SUCCESS) {
671 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
672 CodecProxySBufRecycle(data, reply);
673 return ret;
674 }
675 *acquireFd = HdfSbufReadFileDescriptor(reply);
676 if (*acquireFd < 0) {
677 HDF_LOGE("%{public}s: read acquireFd failed!", __func__);
678 CodecProxySBufRecycle(data, reply);
679 return HDF_ERR_INVALID_PARAM;
680 }
681 if (CodecProxyParseOutputInfo(reply, outInfo)) {
682 HDF_LOGE("%{public}s: read reply failed!", __func__);
683 CodecProxySBufRecycle(data, reply);
684 return HDF_ERR_INVALID_PARAM;
685 }
686 CodecProxySBufRecycle(data, reply);
687 return ret;
688 }
689
CodecProxySetCallback(struct ICodec * self,CODEC_HANDLETYPE handle,struct ICodecCallback * cb,UINTPTR instance)690 int32_t CodecProxySetCallback(struct ICodec *self, CODEC_HANDLETYPE handle, struct ICodecCallback *cb, UINTPTR instance)
691 {
692 int32_t ret;
693 struct HdfSBuf *data = NULL;
694 struct HdfSBuf *reply = NULL;
695 if (self == NULL || cb == NULL) {
696 return HDF_ERR_INVALID_PARAM;
697 }
698 if (CodecProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
699 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
700 return HDF_FAILURE;
701 }
702 if (!HdfRemoteServiceWriteInterfaceToken(self->remote, data)) {
703 HDF_LOGE("write interface token failed");
704 CodecProxySBufRecycle(data, reply);
705 return HDF_ERR_INVALID_PARAM;
706 }
707 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
708 HDF_LOGE("%{public}s: write input handle failed!", __func__);
709 CodecProxySBufRecycle(data, reply);
710 return HDF_ERR_INVALID_PARAM;
711 }
712 if (HdfSbufWriteRemoteService(data, cb->remote) != HDF_SUCCESS) {
713 HDF_LOGE("%{public}s: write cb failed!", __func__);
714 CodecProxySBufRecycle(data, reply);
715 return HDF_ERR_INVALID_PARAM;
716 }
717 if (!HdfSbufWriteUint32(data, instance)) {
718 HDF_LOGE("%{public}s: write input instance failed!", __func__);
719 CodecProxySBufRecycle(data, reply);
720 return HDF_ERR_INVALID_PARAM;
721 }
722 ret = CodecProxyCall(self, CMD_CODEC_SET_CBK, data, reply);
723 if (ret != HDF_SUCCESS) {
724 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
725 CodecProxySBufRecycle(data, reply);
726 return ret;
727 }
728 CodecProxySBufRecycle(data, reply);
729 return ret;
730 }
731
CodecIpmlConstruct(struct ICodec * instance)732 static void CodecIpmlConstruct(struct ICodec *instance)
733 {
734 instance->CodecInit = CodecPorxyInit;
735 instance->CodecDeinit = CodecProxyDeinit;
736 instance->CodecEnumerateCapbility = CodecProxyEnumerateCapbility;
737 instance->CodecGetCapbility = CodecProxyGetCapbility;
738 instance->CodecCreate = CodecProxyCreate;
739 instance->CodecDestroy = CodecProxyDestroy;
740 instance->CodecSetPortMode = CodecProxySetPortMode;
741 instance->CodecSetParameter = CodecProxySetParameter;
742 instance->CodecGetParameter = CodecProxyGetParameter;
743 instance->CodecStart = CodecProxyStart;
744 instance->CodecStop = CodecProxyStop;
745 instance->CodecFlush = CodecProxyFlush;
746 instance->CodecQueueInput = CodecPorxyQueueInput;
747 instance->CodecDequeInput = CodecProxyDequeInput;
748 instance->CodecQueueOutput = CodecProxyQueueOutput;
749 instance->CodecDequeueOutput = CodecProxyDequeueOutput;
750 instance->CodecSetCallback = CodecProxySetCallback;
751 return;
752 }
753
HdiCodecGet(const char * serviceName)754 struct ICodec *HdiCodecGet(const char *serviceName)
755 {
756 struct HDIServiceManager *serviceMgr = HDIServiceManagerGet();
757 if (serviceMgr == NULL) {
758 HDF_LOGE("%{public}s: HDIServiceManager not found!", __func__);
759 return NULL;
760 }
761
762 struct HdfRemoteService *remote = serviceMgr->GetService(serviceMgr, serviceName);
763 if (remote == NULL) {
764 HDF_LOGE("%{public}s: HdfRemoteService not found!", __func__);
765 return NULL;
766 }
767
768 if (!HdfRemoteServiceSetInterfaceDesc(remote, "ohos.hdi.codec_service")) {
769 HDF_LOGE("%{public}s: failed to init interface desc", __func__);
770 HdfRemoteServiceRecycle(remote);
771 return NULL;
772 }
773
774 struct ICodec *codecClient = (struct ICodec *)OsalMemAlloc(sizeof(struct ICodec));
775 if (codecClient == NULL) {
776 HDF_LOGE("%{public}s: malloc codec instance failed!", __func__);
777 HdfRemoteServiceRecycle(remote);
778 return NULL;
779 }
780
781 codecClient->remote = remote;
782 CodecIpmlConstruct(codecClient);
783 return codecClient;
784 }
785
HdiCodecRelease(struct ICodec * instance)786 void HdiCodecRelease(struct ICodec *instance)
787 {
788 if (instance == NULL) {
789 return;
790 }
791 HdfRemoteServiceRecycle(instance->remote);
792 OsalMemFree(instance);
793 }
794
795 #ifdef __cplusplus
796 }
797 #endif /* __cplusplus */