• 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 #include "codec_stub.h"
16 #include <hdf_device_object.h>
17 #include <hdf_log.h>
18 #include <hdf_remote_service.h>
19 #include <osal_mem.h>
20 #include "codec_callback_proxy.h"
21 #include "codec_config_parser.h"
22 #include "codec_interface.h"
23 #include "codec_service.h"
24 #include "icodec.h"
25 #include "stub_msgproc.h"
26 
27 #define HDF_LOG_TAG codec_hdi_stub
28 #define HDF_CODEC_NAME_LEN 50
29 
SerCodecInit(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)30 static int32_t SerCodecInit(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
31 {
32     int32_t errNum = CodecInit();
33     if (errNum != HDF_SUCCESS) {
34         HDF_LOGE("%{public}s: call CodecInit fuc failed!", __func__);
35         return errNum;
36     }
37     if (!HdfSbufWriteUint32(reply, errNum)) {
38         HDF_LOGE("%{public}s: write errNum failed!", __func__);
39         return HDF_ERR_INVALID_PARAM;
40     }
41     return errNum;
42 }
43 
SerCodecDeinit(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)44 static int32_t SerCodecDeinit(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
45 {
46     int32_t errNum = CodecDeinit();
47     if (errNum != HDF_SUCCESS) {
48         HDF_LOGE("%{public}s: call CodecDeinit fuc failed!", __func__);
49         return errNum;
50     }
51     if (!HdfSbufWriteUint32(reply, errNum)) {
52         HDF_LOGE("%{public}s: write errNum failed!", __func__);
53         return HDF_ERR_INVALID_PARAM;
54     }
55     return errNum;
56 }
57 
SerCodecEnumerateCapability(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)58 static int32_t SerCodecEnumerateCapability(struct HdfDeviceIoClient *client, struct HdfSBuf *data,
59     struct HdfSBuf *reply)
60 {
61     uint32_t index;
62     CodecCapability capability;
63 
64     if (!HdfSbufReadUint32(data, &index)) {
65         HDF_LOGE("%{public}s: read index failed!", __func__);
66         return HDF_FAILURE;
67     }
68     if (!CodecCapablitesInited()) {
69         HDF_LOGE("%{public}s: codec capabilities not inited!", __func__);
70         return HDF_FAILURE;
71     }
72     if (CodecEnumerateCapability(index, &capability) != HDF_SUCCESS) {
73         HDF_LOGE("%{public}s: EnumrateCapablity - no more capability to Enumrate!", __func__);
74         return HDF_FAILURE;
75     }
76     if (capability.mime == MEDIA_MIMETYPE_INVALID) {
77         HDF_LOGE("%{public}s: Capability invalid, discard!", __func__);
78         return HDF_FAILURE;
79     }
80     if (CodecSerPackCapability(reply, &capability) != HDF_SUCCESS) {
81         HDF_LOGE("%{public}s: write capability to sbuf failed!", __func__);
82         return HDF_FAILURE;
83     }
84     return HDF_SUCCESS;
85 }
86 
SerCodecGetCapability(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)87 static int32_t SerCodecGetCapability(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
88 {
89     uint32_t flags;
90     AvCodecMime mime;
91     CodecType type;
92     CodecCapability capability;
93 
94     if (!HdfSbufReadUint32(data, (uint32_t*)&mime)) {
95         HDF_LOGE("%{public}s: read input mime failed!", __func__);
96         return HDF_ERR_INVALID_PARAM;
97     }
98     if (!HdfSbufReadUint32(data, (uint32_t*)&type)) {
99         HDF_LOGE("%{public}s: read input type failed!", __func__);
100         return HDF_ERR_INVALID_PARAM;
101     }
102     if (!HdfSbufReadUint32(data, &flags)) {
103         HDF_LOGE("%{public}s: read input flags failed!", __func__);
104         return HDF_ERR_INVALID_PARAM;
105     }
106     if (CodecGetCapability(mime, type, flags, &capability) != HDF_SUCCESS) {
107         HDF_LOGE("%{public}s: GetCapability - got nothing!", __func__);
108         return HDF_FAILURE;
109     }
110     if (CodecSerPackCapability(reply, &capability) != HDF_SUCCESS) {
111         HDF_LOGE("%{public}s: write capability to sbuf failed!", __func__);
112         return HDF_FAILURE;
113     }
114     return HDF_SUCCESS;
115 }
116 
SerCodecCreate(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)117 static int32_t SerCodecCreate(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
118 {
119     int32_t errNum;
120     uint64_t handle = 0;
121     const char *name = NULL;
122 
123     name = HdfSbufReadString(data);
124     if (name == NULL) {
125         HDF_LOGE("%{public}s: Read name failed!", __func__);
126         return HDF_ERR_INVALID_PARAM;
127     }
128     errNum = CodecCreate(name, (CODEC_HANDLETYPE *)&handle);
129     if (errNum != HDF_SUCCESS) {
130         HDF_LOGE("%{public}s: call CodecCreate fuc failed! errNum:%{public}d", __func__, errNum);
131         return errNum;
132     }
133     if (!HdfSbufWriteUint64(reply, handle)) {
134         HDF_LOGE("%{public}s: write handle failed!", __func__);
135         return HDF_ERR_INVALID_PARAM;
136     }
137     return errNum;
138 }
139 
SerCodecCreateByType(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)140 static int32_t SerCodecCreateByType(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
141 {
142     (void)client;
143     int32_t errNum;
144     CodecType type;
145     AvCodecMime mime;
146     uint64_t handle = 0;
147 
148     if (!HdfSbufReadUint32(data, (uint32_t*)&type)) {
149         HDF_LOGE("%{public}s: read input type failed!", __func__);
150         return HDF_ERR_INVALID_PARAM;
151     }
152     if (!HdfSbufReadUint32(data, (uint32_t*)&mime)) {
153         HDF_LOGE("%{public}s: read input mime failed!", __func__);
154         return HDF_ERR_INVALID_PARAM;
155     }
156     errNum = CodecCreateByType(type, mime, (CODEC_HANDLETYPE *)&handle);
157     if (errNum != HDF_SUCCESS) {
158         HDF_LOGE("%{public}s: call CodecCreateByType fuc failed! errNum:%{public}d", __func__, errNum);
159         return errNum;
160     }
161     if (!HdfSbufWriteUint64(reply, handle)) {
162         HDF_LOGE("%{public}s: write handle failed!", __func__);
163         return HDF_ERR_INVALID_PARAM;
164     }
165     return errNum;
166 }
167 
SerCodecDestroy(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)168 static int32_t SerCodecDestroy(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
169 {
170     int32_t errNum;
171     uint64_t handle = 0;
172     if (!HdfSbufReadUint64(data, &handle)) {
173         HDF_LOGE("%{public}s: Read size failed!", __func__);
174         return HDF_ERR_INVALID_PARAM;
175     }
176     errNum = CodecDestroy((CODEC_HANDLETYPE)(uintptr_t)handle);
177     if (errNum != HDF_SUCCESS) {
178         HDF_LOGE("%{public}s: call CodecDestroy fuc failed!", __func__);
179         return errNum;
180     }
181     return errNum;
182 }
183 
SerCodecSetPortMode(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)184 static int32_t SerCodecSetPortMode(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
185 {
186     int32_t errNum;
187     uint64_t handle = 0;
188     DirectionType direct;
189     AllocateBufferMode mode;
190     BufferType type;
191 
192     if (!HdfSbufReadUint64(data, &handle)) {
193         HDF_LOGE("%{public}s: Read handle failed!", __func__);
194         return HDF_ERR_INVALID_PARAM;
195     }
196     if (!HdfSbufReadUint32(data, (uint32_t*)&direct)) {
197         HDF_LOGE("%{public}s: Read DirectionType failed!", __func__);
198         return HDF_ERR_INVALID_PARAM;
199     }
200     if (!HdfSbufReadUint32(data, (uint32_t*)&mode)) {
201         HDF_LOGE("%{public}s: Read AllocateBufferMode failed!", __func__);
202         return HDF_ERR_INVALID_PARAM;
203     }
204     if (!HdfSbufReadUint32(data, (uint32_t*)&type)) {
205         HDF_LOGE("%{public}s: Read BufferType failed!", __func__);
206         return HDF_ERR_INVALID_PARAM;
207     }
208     errNum = CodecSetPortMode((CODEC_HANDLETYPE)(uintptr_t)handle, direct, mode, type);
209     if (errNum != HDF_SUCCESS) {
210         HDF_LOGE("%{public}s: call CodecSetPortMode fuc failed!", __func__);
211         return errNum;
212     }
213     return errNum;
214 }
215 
SerCodecGetPortMode(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)216 static int32_t SerCodecGetPortMode(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
217 {
218     int32_t errNum;
219     uint64_t handle = 0;
220     DirectionType direct;
221     AllocateBufferMode mode;
222     BufferType type;
223 
224     if (!HdfSbufReadUint64(data, &handle)) {
225         HDF_LOGE("%{public}s: Read handle failed!", __func__);
226         return HDF_ERR_INVALID_PARAM;
227     }
228     if (!HdfSbufReadUint32(data, (uint32_t*)&direct)) {
229         HDF_LOGE("%{public}s: Read DirectionType failed!", __func__);
230         return HDF_ERR_INVALID_PARAM;
231     }
232     errNum = CodecGetPortMode((CODEC_HANDLETYPE)(uintptr_t)handle, direct, &mode, &type);
233     if (errNum != HDF_SUCCESS) {
234         HDF_LOGE("%{public}s: call CodecSetPortMode fuc failed!", __func__);
235         return errNum;
236     }
237     if (!HdfSbufWriteUint32(reply, (uint32_t)mode)) {
238         HDF_LOGE("%{public}s: write AllocateBufferMode failed!", __func__);
239         return HDF_ERR_INVALID_PARAM;
240     }
241     if (!HdfSbufWriteUint32(reply, (uint32_t)type)) {
242         HDF_LOGE("%{public}s: write BufferType failed!", __func__);
243         return HDF_ERR_INVALID_PARAM;
244     }
245     return HDF_SUCCESS;
246 }
247 
FreeParams(Param * params,int32_t paramCnt)248 static void FreeParams(Param *params, int32_t paramCnt)
249 {
250     if (params == NULL || paramCnt <= 0) {
251         HDF_LOGE("%{public}s: params is null or invalid count!", __func__);
252         return;
253     }
254     for (int32_t j = 0; j < paramCnt; j++) {
255         if (params[j].val != NULL && params[j].size > 0) {
256             OsalMemFree(params[j].val);
257             params[j].val = NULL;
258         }
259     }
260     OsalMemFree(params);
261 }
262 
SerCodecSetParameter(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)263 static int32_t SerCodecSetParameter(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
264 {
265     int32_t errNum;
266     int32_t paramCnt = 0;
267     uint64_t handle = 0;
268     Param *params = NULL;
269 
270     if (!HdfSbufReadUint64(data, &handle)) {
271         HDF_LOGE("%{public}s: Read handle failed!", __func__);
272         return HDF_ERR_INVALID_PARAM;
273     }
274     if (!HdfSbufReadInt32(data, &paramCnt)) {
275         HDF_LOGE("%{public}s: Read paramCnt failed!", __func__);
276         return HDF_ERR_INVALID_PARAM;
277     }
278     if (paramCnt <= 0) {
279         HDF_LOGE("%{public}s: Param paramCnt err!", __func__);
280         return HDF_ERR_INVALID_PARAM;
281     }
282     params = (Param *)OsalMemAlloc(sizeof(Param)*paramCnt);
283     if (params == NULL) {
284         HDF_LOGE("%{public}s: OsalMemAlloc failed!", __func__);
285         return HDF_ERR_INVALID_PARAM;
286     }
287     for (int32_t i = 0; i < paramCnt; i++) {
288         if (CodecSerParseParam(data, &params[i]) != HDF_SUCCESS) {
289             HDF_LOGE("%{public}s: Read params failed!", __func__);
290             FreeParams(params, paramCnt);
291             return HDF_FAILURE;
292         }
293     }
294     errNum = CodecSetParameter((CODEC_HANDLETYPE)(uintptr_t)handle, params, paramCnt);
295     if (errNum != HDF_SUCCESS) {
296         HDF_LOGE("%{public}s: call CodecSetParameter fuc failed!", __func__);
297     }
298 
299     FreeParams(params, paramCnt);
300     return errNum;
301 }
302 
SerCodecGetParameter(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)303 static int32_t SerCodecGetParameter(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
304 {
305     int32_t errNum;
306     int32_t paramCnt = 0;
307     uint64_t handle = 0;
308     Param *params = NULL;
309 
310     if (!HdfSbufReadUint64(data, &handle)) {
311         HDF_LOGE("%{public}s: Read handle failed!", __func__);
312         return HDF_ERR_INVALID_PARAM;
313     }
314     if (!HdfSbufReadInt32(data, &paramCnt)) {
315         HDF_LOGE("%{public}s: Read paramCnt failed!", __func__);
316         return HDF_ERR_INVALID_PARAM;
317     }
318     if (paramCnt <= 0) {
319         HDF_LOGE("%{public}s: Param paramCnt err!", __func__);
320         return HDF_ERR_INVALID_PARAM;
321     }
322     params = (Param *)OsalMemAlloc(sizeof(Param)*paramCnt);
323     if (params == NULL) {
324         HDF_LOGE("%{public}s: OsalMemAlloc failed!", __func__);
325         return HDF_ERR_INVALID_PARAM;
326     }
327     for (int32_t i = 0; i < paramCnt; i++) {
328         if (CodecSerParseParam(data, &params[i]) != HDF_SUCCESS) {
329             HDF_LOGE("%{public}s: Read params failed!", __func__);
330             FreeParams(params, paramCnt);
331             return HDF_FAILURE;
332         }
333     }
334     errNum = CodecGetParameter((CODEC_HANDLETYPE)(uintptr_t)handle, params, paramCnt);
335     if (errNum != HDF_SUCCESS) {
336         HDF_LOGE("%{public}s: call CodecGetParameter fuc failed!", __func__);
337         FreeParams(params, paramCnt);
338         return errNum;
339     }
340     for (int32_t i = 0; i < paramCnt; i++) {
341         if (CodecSerPackParam(reply, &params[i]) != HDF_SUCCESS) {
342             HDF_LOGE("%{public}s: CodecSerPackParam err!", __func__);
343             FreeParams(params, paramCnt);
344             return HDF_FAILURE;
345         }
346     }
347 
348     FreeParams(params, paramCnt);
349     return errNum;
350 }
351 
SerCodecStart(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)352 static int32_t SerCodecStart(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
353 {
354     int32_t errNum;
355     uint64_t handle = 0;
356 
357     if (!HdfSbufReadUint64(data, &handle)) {
358         HDF_LOGE("%{public}s: Read handle failed!", __func__);
359         return HDF_ERR_INVALID_PARAM;
360     }
361     errNum = CodecStart((CODEC_HANDLETYPE)(uintptr_t)handle);
362     if (errNum != HDF_SUCCESS) {
363         HDF_LOGE("%{public}s: call SerCodecStart fuc failed!", __func__);
364         return errNum;
365     }
366     return errNum;
367 }
368 
SerCodecStop(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)369 static int32_t SerCodecStop(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
370 {
371     uint64_t handle = 0;
372     if (!HdfSbufReadUint64(data, &handle)) {
373         HDF_LOGE("%{public}s: read handle data failed!", __func__);
374         return HDF_ERR_INVALID_PARAM;
375     }
376     int32_t errNum = CodecStop((CODEC_HANDLETYPE)(uintptr_t)handle);
377     if (errNum != HDF_SUCCESS) {
378         HDF_LOGE("%{public}s: call CodecStop fuc failed!", __func__);
379         return errNum;
380     }
381     return errNum;
382 }
383 
SerCodecReset(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)384 static int32_t SerCodecReset(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
385 {
386     (void)client;
387     (void)reply;
388     uint64_t handle = 0;
389     if (!HdfSbufReadUint64(data, &handle)) {
390         HDF_LOGE("%{public}s: read handle data failed!", __func__);
391         return HDF_ERR_INVALID_PARAM;
392     }
393     int32_t errNum = CodecReset((CODEC_HANDLETYPE)(uintptr_t)handle);
394     if (errNum != HDF_SUCCESS) {
395         HDF_LOGE("%{public}s: call CodecStop fuc failed!", __func__);
396         return errNum;
397     }
398     return errNum;
399 }
400 
SerCodecFlush(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)401 static int32_t SerCodecFlush(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
402 {
403     uint64_t handle = 0;
404     uint32_t directType = 0;
405     if (!HdfSbufReadUint64(data, &handle)) {
406         HDF_LOGE("%{public}s: read handle data failed!", __func__);
407         return HDF_ERR_INVALID_PARAM;
408     }
409     if (!HdfSbufReadUint32(data, &directType)) {
410         HDF_LOGE("%{public}s: read directType data failed!", __func__);
411         return HDF_ERR_INVALID_PARAM;
412     }
413     int32_t errNum = CodecFlush((CODEC_HANDLETYPE)(uintptr_t)handle, (DirectionType)directType);
414     if (errNum != HDF_SUCCESS) {
415         HDF_LOGE("%{public}s: call CodecFlush fuc failed!", __func__);
416         return errNum;
417     }
418     return errNum;
419 }
420 
SerCodecQueueInput(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)421 static int32_t SerCodecQueueInput(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
422 {
423     uint32_t timeoutMs = 0;
424     int releaseFenceFd = -1;
425     uint64_t handle = 0;
426     uint32_t bufCnt = 0;
427     CodecBuffer *inputData = NULL;
428     if (!HdfSbufReadUint64(data, &handle)) {
429         HDF_LOGE("%{public}s: read handle data failed!", __func__);
430         return HDF_ERR_INVALID_PARAM;
431     }
432     if (!HdfSbufReadUint32(data, &bufCnt)) {
433         HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
434         return HDF_ERR_INVALID_PARAM;
435     }
436     if (bufCnt == 0) {
437         HDF_LOGE("%{public}s: Param bufCnt err!", __func__);
438         return HDF_ERR_INVALID_PARAM;
439     }
440     inputData = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * bufCnt);
441     if (inputData == NULL) {
442         HDF_LOGE("%{public}s: OsalMemAlloc failed!", __func__);
443         return HDF_ERR_MALLOC_FAIL;
444     }
445     inputData->bufferCnt = bufCnt;
446     if (CodecSerParseCodecBuffer(data, inputData)) {
447         HDF_LOGE("%{public}s: read inputData failed!", __func__);
448         OsalMemFree(inputData);
449         return HDF_ERR_INVALID_PARAM;
450     }
451     if (!HdfSbufReadUint32(data, &timeoutMs)) {
452         HDF_LOGE("%{public}s: read timeoutMs data failed!", __func__);
453         OsalMemFree(inputData);
454         return HDF_ERR_INVALID_PARAM;
455     }
456     if (CodecSerParseFenceFd(data, &releaseFenceFd) != HDF_SUCCESS) {
457         HDF_LOGE("%{public}s: read releaseFenceFd failed!", __func__);
458         OsalMemFree(inputData);
459         return HDF_ERR_INVALID_PARAM;
460     }
461     int32_t errNum = CodecQueueInput((CODEC_HANDLETYPE)(uintptr_t)handle, inputData, timeoutMs, releaseFenceFd);
462     if (errNum != HDF_SUCCESS) {
463         HDF_LOGE("%{public}s: call CodecQueueInput fuc failed!", __func__);
464         OsalMemFree(inputData);
465         return errNum;
466     }
467 
468     OsalMemFree(inputData);
469     return HDF_SUCCESS;
470 }
471 
SerCodecDequeueInput(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)472 static int32_t SerCodecDequeueInput(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
473 {
474     uint32_t timeoutMs = 0;
475     uint64_t handle = 0;
476     uint32_t bufCnt = 0;
477     int32_t acquireFd = 0;
478     CodecBuffer *inputData = NULL;
479     if (!HdfSbufReadUint64(data, &handle)) {
480         HDF_LOGE("%{public}s: read handle data failed!", __func__);
481         return HDF_ERR_INVALID_PARAM;
482     }
483     if (!HdfSbufReadUint32(data, &timeoutMs)) {
484         HDF_LOGE("%{public}s: read timeoutMs data failed!", __func__);
485         return HDF_ERR_INVALID_PARAM;
486     }
487     if (!HdfSbufReadUint32(data, &bufCnt)) {
488         HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
489         return HDF_ERR_INVALID_PARAM;
490     }
491     if (bufCnt == 0) {
492         HDF_LOGE("%{public}s: Param bufCnt err!", __func__);
493         return HDF_ERR_INVALID_PARAM;
494     }
495     inputData = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * bufCnt);
496     if (inputData == NULL) {
497         HDF_LOGE("%{public}s: OsalMemAlloc failed!", __func__);
498         return HDF_ERR_MALLOC_FAIL;
499     }
500     inputData->bufferCnt = bufCnt;
501     int32_t errNum = CodecDequeueInput((CODEC_HANDLETYPE)(uintptr_t)handle, timeoutMs, &acquireFd, inputData);
502     if (errNum != HDF_SUCCESS) {
503         if (errNum != HDF_ERR_TIMEOUT) {
504             HDF_LOGE("%{public}s: call CodecDequeInput fuc failed!", __func__);
505         }
506         OsalMemFree(inputData);
507         return errNum;
508     }
509     if (CodecSerPackFenceFd(reply, acquireFd) != HDF_SUCCESS) {
510         HDF_LOGE("%{public}s: write acquireFd failed!", __func__);
511         OsalMemFree(inputData);
512         return HDF_ERR_INVALID_PARAM;
513     }
514     if (CodecSerPackCodecBuffer(reply, inputData)) {
515         HDF_LOGE("%{public}s: struct inputData write failed!", __func__);
516         OsalMemFree(inputData);
517         return HDF_ERR_INVALID_PARAM;
518     }
519     OsalMemFree(inputData);
520     return HDF_SUCCESS;
521 }
522 
SerCodecQueueOutput(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)523 static int32_t SerCodecQueueOutput(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
524 {
525     uint32_t timeoutMs = 0;
526     int releaseFenceFd = -1;
527     uint64_t handle = 0;
528     uint32_t bufCnt = 0;
529     CodecBuffer *outInfo = NULL;
530     if (!HdfSbufReadUint64(data, &handle)) {
531         HDF_LOGE("%{public}s: read handle data failed!", __func__);
532         return HDF_ERR_INVALID_PARAM;
533     }
534     if (!HdfSbufReadUint32(data, &bufCnt)) {
535         HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
536         return HDF_ERR_INVALID_PARAM;
537     }
538     if (bufCnt == 0) {
539         HDF_LOGE("%{public}s: Param bufCnt err!", __func__);
540         return HDF_ERR_INVALID_PARAM;
541     }
542     outInfo = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * bufCnt);
543     if (outInfo == NULL) {
544         HDF_LOGE("%{public}s: OsalMemAlloc failed!", __func__);
545         return HDF_ERR_MALLOC_FAIL;
546     }
547     outInfo->bufferCnt = bufCnt;
548     if (CodecSerParseCodecBuffer(data, outInfo)) {
549         HDF_LOGE("%{public}s: read struct data failed!", __func__);
550         OsalMemFree(outInfo);
551         return HDF_ERR_INVALID_PARAM;
552     }
553     if (!HdfSbufReadUint32(data, &timeoutMs)) {
554         HDF_LOGE("%{public}s: read timeoutMs data failed!", __func__);
555         OsalMemFree(outInfo);
556         return HDF_ERR_INVALID_PARAM;
557     }
558     if (CodecSerParseFenceFd(data, &releaseFenceFd) != HDF_SUCCESS) {
559         HDF_LOGE("%{public}s: read releaseFenceFd failed!", __func__);
560         OsalMemFree(outInfo);
561         return HDF_ERR_INVALID_PARAM;
562     }
563     int32_t errNum = CodecQueueOutput((CODEC_HANDLETYPE)(uintptr_t)handle, outInfo, timeoutMs, releaseFenceFd);
564     if (errNum != HDF_SUCCESS) {
565         HDF_LOGE("%{public}s: call CodecQueueOutput fuc failed!", __func__);
566         OsalMemFree(outInfo);
567         return errNum;
568     }
569 
570     OsalMemFree(outInfo);
571     return HDF_SUCCESS;
572 }
573 
SerCodecDequeueOutput(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)574 static int32_t SerCodecDequeueOutput(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
575 {
576     uint32_t timeoutMs = 0;
577     int32_t acquireFd = 0;
578     uint64_t handle = 0;
579     uint32_t bufCnt = 0;
580     CodecBuffer *outInfo = NULL;
581     if (!HdfSbufReadUint64(data, &handle)) {
582         HDF_LOGE("%{public}s: read handle data failed!", __func__);
583         return HDF_ERR_INVALID_PARAM;
584     }
585     if (!HdfSbufReadUint32(data, &timeoutMs)) {
586         HDF_LOGE("%{public}s: read timeoutMs data failed!", __func__);
587         return HDF_ERR_INVALID_PARAM;
588     }
589     if (!HdfSbufReadUint32(data, &bufCnt)) {
590         HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
591         return HDF_ERR_INVALID_PARAM;
592     }
593     if (bufCnt == 0) {
594         HDF_LOGE("%{public}s: Param bufCnt err!", __func__);
595         return HDF_ERR_INVALID_PARAM;
596     }
597     outInfo = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * bufCnt);
598     if (outInfo == NULL) {
599         HDF_LOGE("%{public}s: OsalMemAlloc failed!", __func__);
600         return HDF_ERR_MALLOC_FAIL;
601     }
602     outInfo->bufferCnt = bufCnt;
603 
604     int32_t errNum = CodecDequeueOutput((CODEC_HANDLETYPE)(uintptr_t)handle, timeoutMs, &acquireFd, outInfo);
605     if (errNum != HDF_SUCCESS) {
606         if (errNum != HDF_ERR_TIMEOUT) {
607             HDF_LOGE("%{public}s: call CodecDequeueOutput fuc failed!", __func__);
608         }
609         OsalMemFree(outInfo);
610         return errNum;
611     }
612     if (CodecSerPackFenceFd(reply, acquireFd) != HDF_SUCCESS) {
613         HDF_LOGE("%{public}s: write acquireFd failed!", __func__);
614         OsalMemFree(outInfo);
615         return HDF_ERR_INVALID_PARAM;
616     }
617     if (CodecSerPackCodecBuffer(reply, outInfo)) {
618         HDF_LOGE("%{public}s: write outInfo buffer failed!", __func__);
619         OsalMemFree(outInfo);
620         return HDF_ERR_INVALID_PARAM;
621     }
622     OsalMemFree(outInfo);
623     return HDF_SUCCESS;
624 }
625 
SerCodecSetCallback(struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)626 static int32_t SerCodecSetCallback(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
627 {
628     uint64_t handle = 0;
629     uint64_t instance = 0;
630     struct ICodecCallbackProxy *cb = NULL;
631 
632     if (!HdfSbufReadUint64(data, &handle)) {
633         HDF_LOGE("%{public}s: read handle data failed!", __func__);
634         return HDF_ERR_INVALID_PARAM;
635     }
636     struct HdfRemoteService *cbRemote = HdfSbufReadRemoteService(data);
637     if (cbRemote == NULL) {
638         HDF_LOGE("%{public}s: read cbRemote failed!", __func__);
639         return HDF_ERR_INVALID_PARAM;
640     }
641     cb = CodecCallbackProxyObtain(cbRemote);
642     if (cb == NULL) {
643         HDF_LOGE("%{public}s: CodecCallbackProxyObtain failed!", __func__);
644         HdfRemoteServiceRecycle(cbRemote);
645         return HDF_FAILURE;
646     }
647     if (!HdfSbufReadUint64(data, &instance)) {
648         HDF_LOGE("%{public}s: read instance data failed!", __func__);
649         HdfRemoteServiceRecycle(cbRemote);
650         CodecProxyCallbackRelease(cb);
651         return HDF_ERR_INVALID_PARAM;
652     }
653     int32_t errNum = CodecSetCallbackProxy((CODEC_HANDLETYPE)(uintptr_t)handle, cb, (UINTPTR)instance);
654     if (errNum != HDF_SUCCESS) {
655         HDF_LOGE("%{public}s: call CodecSetCallback fuc failed!", __func__);
656         HdfRemoteServiceRecycle(cbRemote);
657         CodecProxyCallbackRelease(cb);
658         return errNum;
659     }
660     return errNum;
661 }
662 
HandleRequestCmdExt(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)663 static int32_t HandleRequestCmdExt(struct HdfDeviceIoClient *client, int cmdId,
664     struct HdfSBuf *data, struct HdfSBuf *reply)
665 {
666     switch (cmdId) {
667         case CMD_CODEC_QUEQUE_INPUT:
668             return SerCodecQueueInput(client, data, reply);
669         case CMD_CODEC_DEQUEQUE_INPUT:
670             return SerCodecDequeueInput(client, data, reply);
671         case CMD_CODEC_QUEQUE_OUTPUT:
672             return SerCodecQueueOutput(client, data, reply);
673         case CMD_CODEC_DEQUEQUE_OUTPUT:
674             return SerCodecDequeueOutput(client, data, reply);
675         case CMD_CODEC_SET_CBK:
676             return SerCodecSetCallback(client, data, reply);
677         default: {
678             HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, cmdId);
679             return HDF_ERR_INVALID_PARAM;
680         }
681     }
682 }
683 
HandleRequestCmd(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)684 static int32_t HandleRequestCmd(struct HdfDeviceIoClient *client, int cmdId,
685     struct HdfSBuf *data, struct HdfSBuf *reply)
686 {
687     switch (cmdId) {
688         case CMD_CODEC_INIT:
689             return SerCodecInit(client, data, reply);
690         case CMD_CODEC_DEINIT:
691             return SerCodecDeinit(client, data, reply);
692         case CMD_CODEC_ENUM_CAP:
693             return SerCodecEnumerateCapability(client, data, reply);
694         case CMD_CODEC_GET_CAP:
695             return SerCodecGetCapability(client, data, reply);
696         case CMD_CODEC_CREATE:
697             return SerCodecCreate(client, data, reply);
698         case CMD_CODEC_CREATE_BY_TYPE:
699             return SerCodecCreateByType(client, data, reply);
700         case CMD_CODEC_DESTROY:
701             return SerCodecDestroy(client, data, reply);
702         case CMD_CODEC_SET_MODE:
703             return SerCodecSetPortMode(client, data, reply);
704         case CMD_CODEC_GET_MODE:
705             return SerCodecGetPortMode(client, data, reply);
706         case CMD_CODEC_SET_PARAMS:
707             return SerCodecSetParameter(client, data, reply);
708         case CMD_CODEC_GET_PARAMS:
709             return SerCodecGetParameter(client, data, reply);
710         case CMD_CODEC_START:
711             return SerCodecStart(client, data, reply);
712         case CMD_CODEC_STOP:
713             return SerCodecStop(client, data, reply);
714         case CMD_CODEC_RESET:
715             return SerCodecReset(client, data, reply);
716         case CMD_CODEC_FLUSH:
717             return SerCodecFlush(client, data, reply);
718         default: {
719             return HandleRequestCmdExt(client, cmdId, data, reply);
720         }
721     }
722 }
723 
CodecServiceOnRemoteRequest(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)724 int32_t CodecServiceOnRemoteRequest(struct HdfDeviceIoClient *client, int cmdId,
725     struct HdfSBuf *data, struct HdfSBuf *reply)
726 {
727     if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) {
728         HDF_LOGE("check interface token failed");
729         return HDF_ERR_INVALID_PARAM;
730     }
731     if ((cmdId == CMD_CODEC_ENUM_CAP) || (cmdId == CMD_CODEC_GET_CAP)) {
732         if (!CodecCapablitesInited()) {
733             ReloadCapabilities();
734         }
735     }
736     return HandleRequestCmd(client, cmdId, data, reply);
737 }
738