• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "proxy_msgproc.h"
16 #include <hdf_log.h>
17 #include <osal_mem.h>
18 #include <securec.h>
19 #include "common_msgproc.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24 
25 #define HDF_LOG_TAG codec_hdi_proxy
26 
CodecCapabilityBaseUnmarshalling(struct HdfSBuf * reply,CodecCapability * cap)27 static bool CodecCapabilityBaseUnmarshalling(struct HdfSBuf *reply, CodecCapability *cap)
28 {
29     if (!HdfSbufReadUint32(reply, (uint32_t*)&cap->mime)) {
30         HDF_LOGE("%{public}s: read cap->mime failed!", __func__);
31         return false;
32     }
33     if (!HdfSbufReadUint32(reply, (uint32_t*)&cap->type)) {
34         HDF_LOGE("%{public}s: read cap->type failed!", __func__);
35         return false;
36     }
37     for (uint32_t i = 0; i < NAME_LENGTH; i++) {
38         if (!HdfSbufReadUint8(reply, (uint8_t *)&(cap->name)[i])) {
39             HDF_LOGE("%{public}s: read name[i] failed!", __func__);
40             return false;
41         }
42     }
43     for (uint32_t j = 0; j < PROFILE_NUM; j++) {
44         if (!HdfSbufReadInt32(reply, &(cap->supportProfiles)[j])) {
45             HDF_LOGE("%{public}s: read supportProfiles[i] failed!", __func__);
46             return false;
47         }
48     }
49     if (!HdfSbufReadInt8(reply, (int8_t *)&cap->isSoftwareCodec)) {
50         HDF_LOGE("%{public}s: read cap->isSoftwareCodec failed!", __func__);
51         return false;
52     }
53     if (!HdfSbufReadInt32(reply, &cap->processModeMask)) {
54         HDF_LOGE("%{public}s: read cap->processModeMask failed!", __func__);
55         return false;
56     }
57     if (!HdfSbufReadUint32(reply, &cap->capsMask)) {
58         HDF_LOGE("%{public}s: read cap->capsMask failed!", __func__);
59         return false;
60     }
61     if (!HdfSbufReadUint32(reply, &cap->allocateMask)) {
62         HDF_LOGE("%{public}s: read cap->allocateMask failed!", __func__);
63         return false;
64     }
65     return true;
66 }
67 
CodecCapabilityRangeValueUnmarshalling(struct HdfSBuf * reply,RangeValue * dataBlock)68 static bool CodecCapabilityRangeValueUnmarshalling(struct HdfSBuf *reply, RangeValue *dataBlock)
69 {
70     if (dataBlock == NULL) {
71         return false;
72     }
73     if (!HdfSbufReadInt32(reply, &dataBlock->min)) {
74         HDF_LOGE("%{public}s: read dataBlock->min failed!", __func__);
75         return false;
76     }
77     if (!HdfSbufReadInt32(reply, &dataBlock->max)) {
78         HDF_LOGE("%{public}s: read dataBlock->max failed!", __func__);
79         return false;
80     }
81     return true;
82 }
83 
CodecCapabilityPortUnmarshalling(struct HdfSBuf * reply,CodecCapability * cap)84 static bool CodecCapabilityPortUnmarshalling(struct HdfSBuf *reply, CodecCapability *cap)
85 {
86     int32_t ret;
87     if (cap->type < AUDIO_DECODER) {
88         const VideoPortCap *video = (const VideoPortCap *)HdfSbufReadUnpadBuffer(reply, sizeof(VideoPortCap));
89         if (video == NULL) {
90             HDF_LOGE("%{public}s: read video failed!", __func__);
91             return false;
92         }
93         ret = memcpy_s(&cap->port, sizeof(VideoPortCap), video, sizeof(VideoPortCap));
94         if (ret != EOK) {
95             HDF_LOGE("%{public}s: memcpy_s video failed, error code: %{public}d", __func__, ret);
96             return false;
97         }
98     } else if (cap->type < INVALID_TYPE) {
99         const AudioPortCap *audio = (const AudioPortCap *)HdfSbufReadUnpadBuffer(reply, sizeof(AudioPortCap));
100         if (audio == NULL) {
101             HDF_LOGE("%{public}s: read audio failed!", __func__);
102             return false;
103         }
104         ret = memcpy_s(&cap->port, sizeof(AudioPortCap), audio, sizeof(AudioPortCap));
105         if (ret != EOK) {
106             HDF_LOGE("%{public}s: memcpy_s audio failed, error code: %{public}d", __func__, ret);
107             return false;
108         }
109     } else {
110         ret = memset_s(&cap->port, sizeof(cap->port), 0, sizeof(cap->port));
111         if (ret != EOK) {
112             HDF_LOGE("%{public}s: memset_s cap->port failed, error code: %{public}d", __func__, ret);
113             return false;
114         }
115     }
116     return true;
117 }
118 
CodecProxyParseGottenCapability(struct HdfSBuf * reply,CodecCapability * cap)119 int32_t CodecProxyParseGottenCapability(struct HdfSBuf *reply, CodecCapability *cap)
120 {
121     if (reply == NULL || cap == NULL) {
122         return HDF_ERR_INVALID_PARAM;
123     }
124     if (!CodecCapabilityBaseUnmarshalling(reply, cap)) {
125         return HDF_FAILURE;
126     }
127     if (!CodecCapabilityRangeValueUnmarshalling(reply, &cap->inputBufferNum)) {
128         HDF_LOGE("%{public}s: read &cap->inputBufferNum failed!", __func__);
129         return HDF_FAILURE;
130     }
131     if (!CodecCapabilityRangeValueUnmarshalling(reply, &cap->outputBufferNum)) {
132         HDF_LOGE("%{public}s: read &cap->outputBufferNum failed!", __func__);
133         return HDF_FAILURE;
134     }
135     if (!CodecCapabilityRangeValueUnmarshalling(reply, &cap->bitRate)) {
136         HDF_LOGE("%{public}s: read &cap->bitRate failed!", __func__);
137         return HDF_FAILURE;
138     }
139     if (!HdfSbufReadInt32(reply, &cap->inputBufferSize)) {
140         HDF_LOGE("%{public}s: read cap->inputBufferSize failed!", __func__);
141         return HDF_FAILURE;
142     }
143     if (!HdfSbufReadInt32(reply, &cap->outputBufferSize)) {
144         HDF_LOGE("%{public}s: read cap->outputBufferSize failed!", __func__);
145         return HDF_FAILURE;
146     }
147     if (!CodecCapabilityPortUnmarshalling(reply, cap)) {
148         HDF_LOGE("%{public}s: read cap->port failed!", __func__);
149         return HDF_FAILURE;
150     }
151 
152     return HDF_SUCCESS;
153 }
154 
CodecProxyPackParam(struct HdfSBuf * data,const Param * param)155 int32_t CodecProxyPackParam(struct HdfSBuf *data, const Param *param)
156 {
157     if (data == NULL || param == NULL) {
158         HDF_LOGE("%{public}s: params NULL!", __func__);
159         return HDF_ERR_INVALID_PARAM;
160     }
161 
162     if (!HdfSbufWriteInt32(data, (int32_t)param->key)) {
163         HDF_LOGE("%{public}s: write param->key failed!", __func__);
164         return false;
165     }
166 
167     if (!HdfSbufWriteInt32(data, param->size)) {
168         HDF_LOGE("%{public}s: write param->size failed!", __func__);
169         return false;
170     }
171     for (int32_t i = 0; i < param->size; i++) {
172         if (!HdfSbufWriteInt8(data, ((int8_t*)(param->val))[i])) {
173             HDF_LOGE("%{public}s: write (param->val)[i] failed!", __func__);
174             return false;
175         }
176     }
177 
178     return HDF_SUCCESS;
179 }
180 
CodecProxyParseParam(struct HdfSBuf * reply,Param * param)181 int32_t CodecProxyParseParam(struct HdfSBuf *reply, Param *param)
182 {
183     if (reply == NULL || param == NULL) {
184         HDF_LOGE("%{public}s: params NULL!", __func__);
185         return HDF_ERR_INVALID_PARAM;
186     }
187 
188     if (!HdfSbufReadInt32(reply, (int32_t*)&param->key)) {
189         HDF_LOGE("%{public}s: read param->key failed!", __func__);
190         return HDF_FAILURE;
191     }
192 
193     int8_t* valCp = NULL;
194     int32_t valCpLen = 0;
195     if (!HdfSbufReadInt32(reply, &valCpLen)) {
196         HDF_LOGE("%{public}s: read size failed!", __func__);
197         return HDF_FAILURE;
198     }
199     if (valCpLen > 0) {
200         valCp = (int8_t*)OsalMemCalloc(sizeof(int8_t) * valCpLen);
201         if (valCp == NULL) {
202             HDF_LOGE("%{public}s: alloc mem failed!", __func__);
203             return HDF_FAILURE;
204         }
205         for (int32_t i = 0; i < valCpLen; i++) {
206             if (!HdfSbufReadInt8(reply, &valCp[i])) {
207                 HDF_LOGE("%{public}s: read valCp[i] failed!", __func__);
208                 OsalMemFree(valCp);
209                 return HDF_FAILURE;
210             }
211         }
212     }
213     param->val = (void*)valCp;
214     param->size = valCpLen;
215 
216     return HDF_SUCCESS;
217 }
218 
CodecProxyPackBufOfBufferInfo(struct HdfSBuf * data,const CodecBufferInfo * buffer)219 static int32_t CodecProxyPackBufOfBufferInfo(struct HdfSBuf *data, const CodecBufferInfo *buffer)
220 {
221     if (data == NULL || buffer == NULL) {
222         HDF_LOGE("%{public}s: params NULL!", __func__);
223         return HDF_ERR_INVALID_PARAM;
224     }
225     if (buffer->type == BUFFER_TYPE_VIRTUAL) {
226         if (!HdfSbufWriteBuffer(data, (void *)buffer->buf, buffer->length)) {
227             HDF_LOGE("%{public}s: Write virtual buffer failed!", __func__);
228             return HDF_FAILURE;
229         }
230     } else if (buffer->type == BUFFER_TYPE_FD) {
231         uint8_t validFd = buffer->buf >= 0 ? 1 : 0;
232         if (!HdfSbufWriteUint8(data, validFd)) {
233             HDF_LOGE("%{public}s: write validFd failed!", __func__);
234             return HDF_FAILURE;
235         }
236         if (validFd && !HdfSbufWriteFileDescriptor(data, (int32_t)buffer->buf)) {
237             HDF_LOGE("%{public}s: Write fd failed!", __func__);
238             return HDF_FAILURE;
239         }
240     } else if (buffer->type == BUFFER_TYPE_HANDLE) {
241         uint8_t validHandle = buffer->buf != 0 ? 1 : 0;
242         if (!HdfSbufWriteUint8(data, validHandle)) {
243             HDF_LOGE("%{public}s: write validHandle failed!", __func__);
244             return HDF_FAILURE;
245         }
246         if (validHandle && !PackBufferHandle(data, (BufferHandle *)buffer->buf)) {
247             return HDF_FAILURE;
248         }
249     } else {
250         HDF_LOGE("%{public}s: buffer->type incorrect!", __func__);
251         return HDF_FAILURE;
252     }
253 
254     return HDF_SUCCESS;
255 }
256 
CodecProxyPackBufferInfo(struct HdfSBuf * data,const CodecBufferInfo * buffer)257 static int32_t CodecProxyPackBufferInfo(struct HdfSBuf *data, const CodecBufferInfo *buffer)
258 {
259     if (data == NULL || buffer == NULL) {
260         HDF_LOGE("%{public}s: params NULL!", __func__);
261         return HDF_ERR_INVALID_PARAM;
262     }
263     if (!HdfSbufWriteUint32(data, (uint32_t)buffer->type)) {
264         HDF_LOGE("%{public}s: Write BufferType failed!", __func__);
265         return HDF_FAILURE;
266     }
267     int32_t ret = CodecProxyPackBufOfBufferInfo(data, buffer);
268     if (ret != HDF_SUCCESS) {
269         return ret;
270     }
271     if (!HdfSbufWriteUint32(data, buffer->offset)) {
272         HDF_LOGE("%{public}s: Write offset failed!", __func__);
273         return HDF_FAILURE;
274     }
275     if (!HdfSbufWriteUint32(data, buffer->length)) {
276         HDF_LOGE("%{public}s: Write length failed!", __func__);
277         return HDF_FAILURE;
278     }
279     if (!HdfSbufWriteUint32(data, buffer->capacity)) {
280         HDF_LOGE("%{public}s: Write capacity failed!", __func__);
281         return HDF_FAILURE;
282     }
283     return HDF_SUCCESS;
284 }
285 
CodecProxyParseBufOfBufferInfo(struct HdfSBuf * reply,CodecBufferInfo * buffer)286 static int32_t CodecProxyParseBufOfBufferInfo(struct HdfSBuf *reply, CodecBufferInfo *buffer)
287 {
288     if (reply == NULL || buffer == NULL) {
289         HDF_LOGE("%{public}s: buffer null!", __func__);
290         return HDF_ERR_INVALID_PARAM;
291     }
292     uint32_t readLen = 0;
293     if (buffer->type == BUFFER_TYPE_VIRTUAL) {
294         void *buf = (void *)buffer->buf;
295         if (!HdfSbufReadBuffer(reply, (const void **)&buf, &readLen)) {
296             HDF_LOGE("%{public}s: read addr failed!", __func__);
297             return HDF_FAILURE;
298         }
299     } else if (buffer->type == BUFFER_TYPE_FD) {
300         uint8_t validFd = 0;
301         if (!HdfSbufReadUint8(reply, &validFd)) {
302             HDF_LOGE("%{public}s: read validFd failed!", __func__);
303             return HDF_FAILURE;
304         }
305         if (validFd != 0) {
306             buffer->buf = (intptr_t)HdfSbufReadFileDescriptor(reply);
307             if (buffer->buf < 0) {
308                 HDF_LOGE("%{public}s: read fd failed!", __func__);
309                 return HDF_FAILURE;
310             }
311         } else {
312             buffer->buf = (intptr_t)(-1);
313         }
314     } else if (buffer->type == BUFFER_TYPE_HANDLE) {
315         uint8_t validHandle = 0;
316         if (!HdfSbufReadUint8(reply, &validHandle)) {
317             HDF_LOGE("%{public}s: read validHandle failed!", __func__);
318             return HDF_FAILURE;
319         }
320         if (validHandle != 0) {
321             if (!ParseBufferHandle(reply, (BufferHandle **)&buffer->buf)) {
322                 return HDF_FAILURE;
323             }
324         } else {
325             buffer->buf = (intptr_t)(0);
326         }
327     } else {
328         HDF_LOGE("%{public}s: buffer->type incorrect!", __func__);
329         return HDF_FAILURE;
330     }
331 
332     return HDF_SUCCESS;
333 }
334 
CodecProxyParseBufferInfo(struct HdfSBuf * reply,CodecBufferInfo * buffer)335 static int32_t CodecProxyParseBufferInfo(struct HdfSBuf *reply, CodecBufferInfo *buffer)
336 {
337     if (reply == NULL || buffer == NULL) {
338         HDF_LOGE("%{public}s: buffer null!", __func__);
339         return HDF_ERR_INVALID_PARAM;
340     }
341     if (!HdfSbufReadUint32(reply, (uint32_t *)&buffer->type)) {
342         HDF_LOGE("%{public}s: read type failed!", __func__);
343         return HDF_FAILURE;
344     }
345     int32_t ret = CodecProxyParseBufOfBufferInfo(reply, buffer);
346     if (ret != HDF_SUCCESS) {
347         return ret;
348     }
349     if (!HdfSbufReadUint32(reply, &buffer->offset)) {
350         HDF_LOGE("%{public}s: read offset failed!", __func__);
351         return HDF_FAILURE;
352     }
353     if (!HdfSbufReadUint32(reply, &buffer->length)) {
354         HDF_LOGE("%{public}s: read length failed!", __func__);
355         return HDF_FAILURE;
356     }
357     if (!HdfSbufReadUint32(reply, &buffer->capacity)) {
358         HDF_LOGE("%{public}s: read capacity failed!", __func__);
359         return HDF_FAILURE;
360     }
361     return HDF_SUCCESS;
362 }
363 
CodecProxyParseCodecBuffer(struct HdfSBuf * reply,CodecBuffer * codecBuffer)364 int32_t CodecProxyParseCodecBuffer(struct HdfSBuf *reply, CodecBuffer *codecBuffer)
365 {
366     if (reply == NULL || codecBuffer == NULL) {
367         HDF_LOGE("%{public}s: params error!", __func__);
368         return HDF_ERR_INVALID_PARAM;
369     }
370     if (!HdfSbufReadUint32(reply, &codecBuffer->bufferId)) {
371         HDF_LOGE("%{public}s: read sequence failed!", __func__);
372         return HDF_FAILURE;
373     }
374     if (!HdfSbufReadInt64(reply, &codecBuffer->timeStamp)) {
375         HDF_LOGE("%{public}s: read timeStamp failed!", __func__);
376         return HDF_FAILURE;
377     }
378     if (!HdfSbufReadUint32(reply, &codecBuffer->flag)) {
379         HDF_LOGE("%{public}s: read flag failed!", __func__);
380         return HDF_FAILURE;
381     }
382     for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
383         if (CodecProxyParseBufferInfo(reply, &codecBuffer->buffer[i])) {
384             HDF_LOGE("%{public}s: read buffers failed!", __func__);
385             return HDF_FAILURE;
386         }
387     }
388     return HDF_SUCCESS;
389 }
390 
CodecProxyPackCodecBuffer(struct HdfSBuf * data,const CodecBuffer * codecBuffer)391 int32_t CodecProxyPackCodecBuffer(struct HdfSBuf *data, const CodecBuffer *codecBuffer)
392 {
393     if (data == NULL || codecBuffer == NULL) {
394         HDF_LOGE("%{public}s: params error!", __func__);
395         return HDF_ERR_INVALID_PARAM;
396     }
397     if (!HdfSbufWriteUint32(data, codecBuffer->bufferCnt)) {
398         HDF_LOGE("%{public}s: write bufferCnt failed!", __func__);
399         return HDF_FAILURE;
400     }
401     if (!HdfSbufWriteUint32(data, codecBuffer->bufferId)) {
402         HDF_LOGE("%{public}s: write bufferId failed!", __func__);
403         return HDF_FAILURE;
404     }
405     if (!HdfSbufWriteInt64(data, codecBuffer->timeStamp)) {
406         HDF_LOGE("%{public}s: write timeStamp failed!", __func__);
407         return HDF_FAILURE;
408     }
409     if (!HdfSbufWriteUint32(data, codecBuffer->flag)) {
410         HDF_LOGE("%{public}s: write flag failed!", __func__);
411         return HDF_FAILURE;
412     }
413     for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
414         if (CodecProxyPackBufferInfo(data, &codecBuffer->buffer[i])) {
415             HDF_LOGE("%{public}s: write buffers failed!", __func__);
416             return HDF_FAILURE;
417         }
418     }
419     return HDF_SUCCESS;
420 }
421 
CodecProxyParseFenceFd(struct HdfSBuf * reply,int32_t * fenceFd)422 int32_t CodecProxyParseFenceFd(struct HdfSBuf *reply, int32_t *fenceFd)
423 {
424     uint8_t validFd = 0;
425     if (!HdfSbufReadUint8(reply, &validFd)) {
426         HDF_LOGE("%{public}s: read validFd failed!", __func__);
427         return HDF_FAILURE;
428     }
429     if (validFd != 0) {
430         *fenceFd = HdfSbufReadFileDescriptor(reply);
431     } else {
432         *fenceFd = -1;
433     }
434     return HDF_SUCCESS;
435 }
436 
CodecProxyPackFenceFd(struct HdfSBuf * data,int32_t fenceFd)437 int32_t CodecProxyPackFenceFd(struct HdfSBuf *data, int32_t fenceFd)
438 {
439     uint8_t validFd = fenceFd >= 0;
440     if (!HdfSbufWriteUint8(data, validFd)) {
441         HDF_LOGE("%{public}s: write validFd flag failed!", __func__);
442         return HDF_ERR_INVALID_PARAM;
443     }
444     if (validFd != 0 && !HdfSbufWriteFileDescriptor(data, fenceFd)) {
445         HDF_LOGE("%{public}s: write fenceFd failed!", __func__);
446         return HDF_ERR_INVALID_PARAM;
447     }
448     return HDF_SUCCESS;
449 }
450 
451 #ifdef __cplusplus
452 }
453 #endif /* __cplusplus */
454