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