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_callback_proxy.h"
16 #include <hdf_log.h>
17 #include <osal_mem.h>
18 #include <servmgr_hdi.h>
19 #include "proxy_msgproc.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24
25 struct ICodecCallback *g_callback = NULL;
26
CodecCallbackProxyCall(int32_t id,struct HdfSBuf * data,struct HdfSBuf * reply)27 static int32_t CodecCallbackProxyCall(int32_t id, struct HdfSBuf *data, struct HdfSBuf *reply)
28 {
29 if (g_callback->remote == NULL || g_callback->remote->dispatcher == NULL ||
30 g_callback->remote->dispatcher->Dispatch == NULL) {
31 HDF_LOGE("%{public}s: obj is null", __func__);
32 return HDF_ERR_INVALID_OBJECT;
33 }
34 return g_callback->remote->dispatcher->Dispatch(g_callback->remote, id, data, reply);
35 }
36
CodecCallbackProxyReqSBuf(struct HdfSBuf ** data,struct HdfSBuf ** reply)37 int32_t CodecCallbackProxyReqSBuf(struct HdfSBuf **data, struct HdfSBuf **reply)
38 {
39 *data = HdfSBufTypedObtain(SBUF_IPC);
40 if (*data == NULL) {
41 HDF_LOGE("%{public}s: Failed to obtain", __func__);
42 return HDF_ERR_MALLOC_FAIL;
43 }
44 *reply = HdfSBufTypedObtain(SBUF_IPC);
45 if (*reply == NULL) {
46 HDF_LOGE("%{public}s: Failed to obtain reply", __func__);
47 HdfSBufRecycle(*data);
48 return HDF_ERR_MALLOC_FAIL;
49 }
50 return HDF_SUCCESS;
51 }
52
CodecCallbackProxySBufRecycle(struct HdfSBuf * data,struct HdfSBuf * reply)53 void CodecCallbackProxySBufRecycle(struct HdfSBuf *data, struct HdfSBuf *reply)
54 {
55 if (data != NULL) {
56 HdfSBufRecycle(data);
57 }
58 if (reply != NULL) {
59 HdfSBufRecycle(reply);
60 }
61 return;
62 }
63
CodecCallbackProxyOnEvent(UINTPTR comp,UINTPTR appData,EventType event,uint32_t data1,uint32_t data2,UINTPTR eventData)64 static int CodecCallbackProxyOnEvent(UINTPTR comp, UINTPTR appData, EventType event,
65 uint32_t data1, uint32_t data2, UINTPTR eventData)
66 {
67 int32_t ret;
68 struct HdfSBuf *data = NULL;
69 struct HdfSBuf *reply = NULL;
70 if (CodecCallbackProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
71 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
72 return HDF_ERR_INVALID_PARAM;
73 }
74 if (!HdfSbufWriteUint32(data, (uint32_t)comp)) {
75 HDF_LOGE("%{public}s: write input comp failed!", __func__);
76 CodecCallbackProxySBufRecycle(data, reply);
77 return HDF_ERR_INVALID_PARAM;
78 }
79 if (!HdfSbufWriteUint32(data, (uint32_t)appData)) {
80 HDF_LOGE("%{public}s: write input appData failed!", __func__);
81 CodecCallbackProxySBufRecycle(data, reply);
82 return HDF_ERR_INVALID_PARAM;
83 }
84 if (!HdfSbufWriteUint32(data, (uint32_t)event)) {
85 HDF_LOGE("%{public}s: write input event failed!", __func__);
86 CodecCallbackProxySBufRecycle(data, reply);
87 return HDF_ERR_INVALID_PARAM;
88 }
89 if (!HdfSbufWriteUint32(data, (uint32_t)data1)) {
90 HDF_LOGE("%{public}s: write input data1 failed!", __func__);
91 CodecCallbackProxySBufRecycle(data, reply);
92 return HDF_ERR_INVALID_PARAM;
93 }
94 if (!HdfSbufWriteUint32(data, (uint32_t)data2)) {
95 HDF_LOGE("%{public}s: write input data2 failed!", __func__);
96 CodecCallbackProxySBufRecycle(data, reply);
97 return HDF_ERR_INVALID_PARAM;
98 }
99 if (!HdfSbufWriteUint32(data, (uint32_t)eventData)) {
100 HDF_LOGE("%{public}s: write input eventData failed!", __func__);
101 CodecCallbackProxySBufRecycle(data, reply);
102 return HDF_ERR_INVALID_PARAM;
103 }
104 ret = CodecCallbackProxyCall(CMD_CODEC_ON_EVENT, data, reply);
105 if (ret != HDF_SUCCESS) {
106 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
107 CodecCallbackProxySBufRecycle(data, reply);
108 return ret;
109 }
110 CodecCallbackProxySBufRecycle(data, reply);
111 return ret;
112 }
113
CodecCallbackProxyInputBufferAvailable(UINTPTR comp,UINTPTR appData,InputInfo * inBuf)114 static int CodecCallbackProxyInputBufferAvailable(UINTPTR comp, UINTPTR appData, InputInfo *inBuf)
115 {
116 int32_t ret;
117 struct HdfSBuf *data = NULL;
118 struct HdfSBuf *reply = NULL;
119 if (inBuf == NULL) {
120 return HDF_ERR_INVALID_PARAM;
121 }
122 if (CodecCallbackProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
123 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
124 return HDF_ERR_INVALID_PARAM;
125 }
126 if (!HdfSbufWriteUint32(data, (uint32_t)comp)) {
127 HDF_LOGE("%{public}s: write input comp failed!", __func__);
128 CodecCallbackProxySBufRecycle(data, reply);
129 return HDF_ERR_INVALID_PARAM;
130 }
131 if (!HdfSbufWriteUint32(data, (uint32_t)appData)) {
132 HDF_LOGE("%{public}s: write input appData failed!", __func__);
133 CodecCallbackProxySBufRecycle(data, reply);
134 return HDF_ERR_INVALID_PARAM;
135 }
136 if (CodecProxyPackInputInfo(data, inBuf)) {
137 HDF_LOGE("%{public}s: write input buffer failed!", __func__);
138 CodecCallbackProxySBufRecycle(data, reply);
139 return HDF_ERR_INVALID_PARAM;
140 }
141 ret = CodecCallbackProxyCall(CMD_CODEC_INPUT_BUFFER_AVAILABLE, data, reply);
142 if (ret != HDF_SUCCESS) {
143 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
144 CodecCallbackProxySBufRecycle(data, reply);
145 return ret;
146 }
147 CodecCallbackProxySBufRecycle(data, reply);
148 return ret;
149 }
150
CodecCallbackProxyOutputBufferAvailable(UINTPTR comp,UINTPTR appData,OutputInfo * outBuf)151 static int CodecCallbackProxyOutputBufferAvailable(UINTPTR comp, UINTPTR appData, OutputInfo *outBuf)
152 {
153 int32_t ret;
154 struct HdfSBuf *data = NULL;
155 struct HdfSBuf *reply = NULL;
156 if (outBuf == NULL) {
157 return HDF_ERR_INVALID_PARAM;
158 }
159 if (CodecCallbackProxyReqSBuf(&data, &reply) != HDF_SUCCESS) {
160 HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__);
161 return HDF_ERR_INVALID_PARAM;
162 }
163 if (!HdfSbufWriteUint32(data, (uint32_t)comp)) {
164 HDF_LOGE("%{public}s: write input comp failed!", __func__);
165 CodecCallbackProxySBufRecycle(data, reply);
166 return HDF_ERR_INVALID_PARAM;
167 }
168 if (!HdfSbufWriteUint32(data, (uint32_t)appData)) {
169 HDF_LOGE("%{public}s: write input appData failed!", __func__);
170 CodecCallbackProxySBufRecycle(data, reply);
171 return HDF_ERR_INVALID_PARAM;
172 }
173 if (CodecProxyPackOutputInfo(data, outBuf)) {
174 HDF_LOGE("%{public}s: write input buffer failed!", __func__);
175 CodecCallbackProxySBufRecycle(data, reply);
176 return HDF_ERR_INVALID_PARAM;
177 }
178 ret = CodecCallbackProxyCall(CMD_CODEC_OUTPUT_BUFFER_AVAILABLE, data, reply);
179 if (ret != HDF_SUCCESS) {
180 HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ret);
181 CodecCallbackProxySBufRecycle(data, reply);
182 return ret;
183 }
184 CodecCallbackProxySBufRecycle(data, reply);
185 return ret;
186 }
187
CodecCallbackProxyConstruct(struct ICodecCallback * callback)188 static void CodecCallbackProxyConstruct(struct ICodecCallback *callback)
189 {
190 callback->callback.OnEvent = CodecCallbackProxyOnEvent;
191 callback->callback.InputBufferAvailable = CodecCallbackProxyInputBufferAvailable;
192 callback->callback.OutputBufferAvailable = CodecCallbackProxyOutputBufferAvailable;
193 }
194
CodecProxyCallbackObtain(struct HdfRemoteService * remote)195 struct ICodecCallback *CodecProxyCallbackObtain(struct HdfRemoteService *remote)
196 {
197 g_callback = (struct ICodecCallback *)OsalMemAlloc(sizeof(struct ICodecCallback));
198 if (g_callback == NULL) {
199 HDF_LOGE("%{public}s: OsalMemAlloc failed!", __func__);
200 return NULL;
201 }
202 g_callback->remote = remote;
203 CodecCallbackProxyConstruct(g_callback);
204 return g_callback;
205 }
206
CodecProxyCallbackRelease(struct ICodecCallback * callback)207 void CodecProxyCallbackRelease(struct ICodecCallback *callback)
208 {
209 if (callback == NULL) {
210 return;
211 }
212 OsalMemFree(callback);
213 }
214
215 #ifdef __cplusplus
216 }
217 #endif /* __cplusplus */