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