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 "hdf_remote_service.h"
19 #include "codec_callback_service.h"
20 #include "stub_msgproc.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif /* __cplusplus */
25
SerCodecOnEvent(struct ICodecCallback * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)26 static int32_t SerCodecOnEvent(struct ICodecCallback *serviceImpl, struct HdfSBuf *data, struct HdfSBuf *reply)
27 {
28 int32_t ret;
29 UINTPTR userData = 0;
30 EventType event = 0;
31 uint32_t length = 0;
32 int32_t *eventData = NULL;
33
34 if (!HdfSbufReadUint32(data, (uint32_t *)&userData)) {
35 HDF_LOGE("%{public}s: read comp data failed!", __func__);
36 return HDF_ERR_INVALID_PARAM;
37 }
38 if (!HdfSbufReadUint32(data, (uint32_t *)&event)) {
39 HDF_LOGE("%{public}s: read event data failed!", __func__);
40 return HDF_ERR_INVALID_PARAM;
41 }
42 if (!HdfSbufReadUint32(data, &length)) {
43 HDF_LOGE("%{public}s: read data1 data failed!", __func__);
44 return HDF_ERR_INVALID_PARAM;
45 }
46 if (length > 0) {
47 eventData = (int32_t *)OsalMemCalloc(length);
48 if (eventData == NULL) {
49 HDF_LOGE("%{public}s: OsalMemAlloc eventData failed!", __func__);
50 return HDF_ERR_INVALID_PARAM;
51 }
52 for (uint32_t i = 0; i < length; i++) {
53 if (!HdfSbufReadInt32(data, &eventData[i])) {
54 HDF_LOGE("%{public}s: read eventData failed!", __func__);
55 OsalMemFree(eventData);
56 return HDF_ERR_INVALID_PARAM;
57 }
58 }
59 }
60 ret = serviceImpl->callback.OnEvent(userData, event, length, eventData);
61 if (ret != HDF_SUCCESS) {
62 HDF_LOGE("%{public}s: call OnEvent fuc failed!", __func__);
63 OsalMemFree(eventData);
64 return ret;
65 }
66 OsalMemFree(eventData);
67 return ret;
68 }
69
SerCodecInputBufferAvailable(struct ICodecCallback * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)70 static int32_t SerCodecInputBufferAvailable(struct ICodecCallback *serviceImpl,
71 struct HdfSBuf *data, struct HdfSBuf *reply)
72 {
73 int32_t ret = HDF_FAILURE;
74 uint32_t bufCnt = 0;
75 UINTPTR userData = 0;
76 CodecBuffer *inBuf = NULL;
77 int32_t acquireFd;
78
79 if (!HdfSbufReadUint32(data, (uint32_t *)&userData)) {
80 HDF_LOGE("%{public}s: read userData failed!", __func__);
81 return HDF_ERR_INVALID_PARAM;
82 }
83 if (!HdfSbufReadUint32(data, &bufCnt)) {
84 HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
85 return HDF_FAILURE;
86 }
87 if (bufCnt == 0) {
88 HDF_LOGE("%{public}s: invalid bufferCnt!", __func__);
89 return HDF_ERR_INVALID_PARAM;
90 }
91 inBuf = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * bufCnt);
92 if (inBuf == NULL) {
93 HDF_LOGE("%{public}s: OsalMemAlloc inBuf failed!", __func__);
94 return HDF_ERR_MALLOC_FAIL;
95 }
96 inBuf->bufferCnt = bufCnt;
97 if (CodecSerParseCodecBuffer(data, inBuf)) {
98 HDF_LOGE("%{public}s: read inBuf failed!", __func__);
99 OsalMemFree(inBuf);
100 return HDF_ERR_INVALID_PARAM;
101 }
102 ret = serviceImpl->callback.InputBufferAvailable(userData, inBuf, &acquireFd);
103 if (ret != HDF_SUCCESS) {
104 HDF_LOGE("%{public}s: call InputBufferAvailable fuc failed!", __func__);
105 OsalMemFree(inBuf);
106 return ret;
107 }
108 OsalMemFree(inBuf);
109 return ret;
110 }
111
SerCodecOutputBufferAvailable(struct ICodecCallback * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)112 static int32_t SerCodecOutputBufferAvailable(struct ICodecCallback *serviceImpl,
113 struct HdfSBuf *data, struct HdfSBuf *reply)
114 {
115 int32_t ret = HDF_FAILURE;
116 uint32_t bufCnt = 0;
117 UINTPTR userData = 0;
118 CodecBuffer *outBuf = NULL;
119 int32_t acquireFd;
120
121 if (!HdfSbufReadUint32(data, (uint32_t *)&userData)) {
122 HDF_LOGE("%{public}s: read userData failed!", __func__);
123 return HDF_ERR_INVALID_PARAM;
124 }
125 if (!HdfSbufReadUint32(data, (uint32_t *)&bufCnt)) {
126 HDF_LOGE("%{public}s: read bufferCnt failed!", __func__);
127 return HDF_ERR_INVALID_PARAM;
128 }
129 if (bufCnt == 0) {
130 HDF_LOGE("%{public}s: invalid bufferCnt!", __func__);
131 return HDF_ERR_INVALID_PARAM;
132 }
133 outBuf = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * bufCnt);
134 if (outBuf == NULL) {
135 HDF_LOGE("%{public}s: OsalMemAlloc outBuf failed!", __func__);
136 return HDF_ERR_MALLOC_FAIL;
137 }
138 outBuf->bufferCnt = bufCnt;
139 if (CodecSerParseCodecBuffer(data, outBuf)) {
140 HDF_LOGE("%{public}s: read outBuf failed!", __func__);
141 OsalMemFree(outBuf);
142 return HDF_ERR_INVALID_PARAM;
143 }
144 ret = serviceImpl->callback.OutputBufferAvailable(userData, outBuf, &acquireFd);
145 if (ret != HDF_SUCCESS) {
146 HDF_LOGE("%{public}s: call OutputBufferAvailable fuc failed!", __func__);
147 OsalMemFree(outBuf);
148 return ret;
149 }
150 OsalMemFree(outBuf);
151 return ret;
152 }
153
CodecCallbackServiceOnRemoteRequest(struct HdfRemoteService * service,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)154 static int32_t CodecCallbackServiceOnRemoteRequest(struct HdfRemoteService *service, int cmdId,
155 struct HdfSBuf *data, struct HdfSBuf *reply)
156 {
157 struct ICodecCallback *serviceImpl = (struct ICodecCallback *)service;
158 switch (cmdId) {
159 case CMD_CODEC_ON_EVENT:
160 return SerCodecOnEvent(serviceImpl, data, reply);
161 case CMD_CODEC_INPUT_BUFFER_AVAILABLE:
162 return SerCodecInputBufferAvailable(serviceImpl, data, reply);
163 case CMD_CODEC_OUTPUT_BUFFER_AVAILABLE:
164 return SerCodecOutputBufferAvailable(serviceImpl, data, reply);
165 default: {
166 HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, cmdId);
167 return HDF_ERR_INVALID_PARAM;
168 }
169 }
170 }
171
172 struct CodecCallbackStub {
173 struct ICodecCallback service;
174 struct HdfRemoteDispatcher dispatcher;
175 };
176
CodecCallbackStubObtain(void)177 struct ICodecCallback *CodecCallbackStubObtain(void)
178 {
179 struct CodecCallbackStub *stub = (struct CodecCallbackStub *)OsalMemAlloc(sizeof(struct CodecCallbackStub));
180 if (stub == NULL) {
181 HDF_LOGE("%{public}s: OsalMemAlloc CodecCallbackStub obj failed!", __func__);
182 return NULL;
183 }
184 stub->dispatcher.Dispatch = CodecCallbackServiceOnRemoteRequest;
185 stub->service.remote = HdfRemoteServiceObtain((struct HdfObject *)stub, &(stub->dispatcher));
186 if (stub->service.remote == NULL) {
187 HDF_LOGE("%{public}s: stub->service.remote is null", __func__);
188 return NULL;
189 }
190 CodecCallbackServiceConstruct(&stub->service);
191 return &stub->service;
192 }
193
CodecCallbackStubRelease(struct ICodecCallback * stub)194 void CodecCallbackStubRelease(struct ICodecCallback *stub)
195 {
196 if (stub == NULL) {
197 return;
198 }
199 OsalMemFree(stub);
200 }
201
202 #ifdef __cplusplus
203 }
204 #endif /* __cplusplus */