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 <hdf_remote_service.h>
18 #include <servmgr_hdi.h>
19 #include "audio_proxy_common.h"
20
21 namespace OHOS::HDI::Audio_Bluetooth {
22 constexpr int PROXY_VOLUME_CHANGE = 100;
23
AudioProxyObtainHdfSBuf()24 struct HdfSBuf *AudioProxyObtainHdfSBuf()
25 {
26 HdfSbufType bufType = SBUF_IPC;
27 return HdfSbufTypedObtain(bufType);
28 }
29
AudioProxyDispatchCall(struct HdfRemoteService * self,int32_t id,struct HdfSBuf * data,struct HdfSBuf * reply)30 int32_t AudioProxyDispatchCall(struct HdfRemoteService *self,
31 int32_t id, struct HdfSBuf *data, struct HdfSBuf *reply)
32 {
33 if (data == NULL || reply == NULL || self == NULL) {
34 HDF_LOGE("AudioProxyDispatchCall parameter is null");
35 return AUDIO_HAL_ERR_INVALID_PARAM;
36 }
37 if (self->dispatcher == NULL || self->dispatcher->Dispatch == NULL) {
38 HDF_LOGE("AudioProxyDispatchCall Remote obj is null");
39 return AUDIO_HAL_ERR_INVALID_PARAM;
40 }
41 return self->dispatcher->Dispatch(self, id, data, reply);
42 }
43
AudioProxyBufReplyRecycle(struct HdfSBuf * data,struct HdfSBuf * reply)44 void AudioProxyBufReplyRecycle(struct HdfSBuf *data, struct HdfSBuf *reply)
45 {
46 if (data != NULL) {
47 HdfSbufRecycle(data);
48 }
49 if (reply != NULL) {
50 HdfSbufRecycle(reply);
51 }
52 return;
53 }
54
AudioProxyPreprocessSBuf(struct HdfSBuf ** data,struct HdfSBuf ** reply)55 int32_t AudioProxyPreprocessSBuf(struct HdfSBuf **data, struct HdfSBuf **reply)
56 {
57 if (data == NULL || reply == NULL) {
58 return HDF_FAILURE;
59 }
60 *data = AudioProxyObtainHdfSBuf();
61 if (*data == NULL) {
62 HDF_LOGE("Failed to obtain data");
63 return HDF_FAILURE;
64 }
65 *reply = AudioProxyObtainHdfSBuf();
66 if (*reply == NULL) {
67 HDF_LOGE("Failed to obtain reply");
68 HdfSbufRecycle(*data);
69 return HDF_FAILURE;
70 }
71 return HDF_SUCCESS;
72 }
73
AudioProxyPreprocessRender(struct AudioHwRender * render,struct HdfSBuf ** data,struct HdfSBuf ** reply)74 int32_t AudioProxyPreprocessRender(struct AudioHwRender *render, struct HdfSBuf **data, struct HdfSBuf **reply)
75 {
76 struct AudioHwRender *hwRender = render;
77 if (hwRender == NULL || data == NULL || reply == NULL) {
78 return HDF_FAILURE;
79 }
80 uint32_t renderPid = (uint32_t)getpid();
81 const char *adapterName = hwRender->renderParam.renderMode.hwInfo.adapterName;
82 if (adapterName == NULL) {
83 HDF_LOGE("adapterName is NULL");
84 return HDF_FAILURE;
85 }
86 if (AudioProxyPreprocessSBuf(data, reply) < 0) {
87 return HDF_FAILURE;
88 }
89 if (!HdfRemoteServiceWriteInterfaceToken(render->proxyRemoteHandle, *data)) {
90 AudioProxyBufReplyRecycle(*data, *reply);
91 return HDF_FAILURE;
92 }
93 if (!HdfSbufWriteString(*data, adapterName)) {
94 AudioProxyBufReplyRecycle(*data, *reply);
95 return HDF_FAILURE;
96 }
97 if (!HdfSbufWriteUint32(*data, renderPid)) {
98 AudioProxyBufReplyRecycle(*data, *reply);
99 return HDF_FAILURE;
100 }
101 return HDF_SUCCESS;
102 }
103
AudioProxyWriteSampleAttributes(struct HdfSBuf * data,const struct AudioSampleAttributes * attrs)104 int32_t AudioProxyWriteSampleAttributes(struct HdfSBuf *data, const struct AudioSampleAttributes *attrs)
105 {
106 if (data == NULL || attrs == NULL) {
107 return HDF_FAILURE;
108 }
109 if (!HdfSbufWriteUint32(data, (uint32_t)attrs->type)) {
110 return HDF_FAILURE;
111 }
112 if (!HdfSbufWriteUint32(data, (uint32_t)attrs->interleaved)) {
113 return HDF_FAILURE;
114 }
115 if (!HdfSbufWriteUint32(data, (uint32_t)attrs->format)) {
116 return HDF_FAILURE;
117 }
118 if (!HdfSbufWriteUint32(data, attrs->sampleRate)) {
119 return HDF_FAILURE;
120 }
121 if (!HdfSbufWriteUint32(data, attrs->channelCount)) {
122 return HDF_FAILURE;
123 }
124 if (!HdfSbufWriteUint32(data, attrs->period)) {
125 return HDF_FAILURE;
126 }
127 if (!HdfSbufWriteUint32(data, attrs->frameSize)) {
128 return HDF_FAILURE;
129 }
130 if (!HdfSbufWriteUint32(data, (uint32_t)(attrs->isBigEndian))) {
131 return HDF_FAILURE;
132 }
133 if (!HdfSbufWriteUint32(data, (uint32_t)(attrs->isSignedData))) {
134 return HDF_FAILURE;
135 }
136 if (!HdfSbufWriteUint32(data, attrs->startThreshold)) {
137 return HDF_FAILURE;
138 }
139 if (!HdfSbufWriteUint32(data, attrs->stopThreshold)) {
140 return HDF_FAILURE;
141 }
142 if (!HdfSbufWriteUint32(data, attrs->silenceThreshold)) {
143 return HDF_FAILURE;
144 }
145 return HDF_SUCCESS;
146 }
147
AudioProxyReadSapmleAttrbutes(struct HdfSBuf * reply,struct AudioSampleAttributes * attrs)148 int32_t AudioProxyReadSapmleAttrbutes(struct HdfSBuf *reply, struct AudioSampleAttributes *attrs)
149 {
150 if (reply == NULL || attrs == NULL) {
151 return HDF_FAILURE;
152 }
153 uint32_t tempType = 0;
154 if (!HdfSbufReadUint32(reply, &tempType)) {
155 return HDF_FAILURE;
156 }
157 attrs->type = (AudioCategory)tempType;
158 uint32_t tempInterleaved = 0;
159 if (!HdfSbufReadUint32(reply, &tempInterleaved)) {
160 return HDF_FAILURE;
161 }
162 attrs->interleaved = (bool)tempInterleaved;
163 uint32_t tempFormat = 0;
164 if (!HdfSbufReadUint32(reply, &tempFormat)) {
165 return HDF_FAILURE;
166 }
167 attrs->format = (AudioFormat)tempFormat;
168 if (!HdfSbufReadUint32(reply, &attrs->sampleRate)) {
169 return HDF_FAILURE;
170 }
171 if (!HdfSbufReadUint32(reply, &attrs->channelCount)) {
172 return HDF_FAILURE;
173 }
174 if (!HdfSbufReadUint32(reply, &attrs->period)) {
175 return HDF_FAILURE;
176 }
177 if (!HdfSbufReadUint32(reply, &attrs->frameSize)) {
178 return HDF_FAILURE;
179 }
180 if (!HdfSbufReadUint32(reply, &tempInterleaved)) {
181 return HDF_FAILURE;
182 }
183 attrs->isBigEndian = (bool)tempInterleaved;
184 if (!HdfSbufReadUint32(reply, &tempInterleaved)) {
185 return HDF_FAILURE;
186 }
187 attrs->isSignedData = (bool)tempInterleaved;
188 if (!HdfSbufReadUint32(reply, &attrs->startThreshold)) {
189 return HDF_FAILURE;
190 }
191 if (!HdfSbufReadUint32(reply, &attrs->stopThreshold)) {
192 return HDF_FAILURE;
193 }
194 if (!HdfSbufReadUint32(reply, &attrs->silenceThreshold)) {
195 return HDF_FAILURE;
196 }
197 return HDF_SUCCESS;
198 }
199
AudioProxyCommonSetRenderCtrlParam(int cmId,AudioHandle handle,float param)200 int32_t AudioProxyCommonSetRenderCtrlParam(int cmId, AudioHandle handle, float param)
201 {
202 HDF_LOGI("%{public}s, ", __func__);
203 struct HdfSBuf *data = NULL;
204 struct HdfSBuf *reply = NULL;
205 struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
206 if (hwRender == NULL || hwRender->proxyRemoteHandle == NULL) {
207 HDF_LOGE("The hwRender parameter is null");
208 return AUDIO_HAL_ERR_INVALID_PARAM;
209 }
210 if (param < 0) {
211 HDF_LOGE("Set param is invalid, Please check param!");
212 return AUDIO_HAL_ERR_INVALID_PARAM;
213 }
214 if (cmId == AUDIO_HDI_RENDER_SET_VOLUME) {
215 if (param > 1.0) {
216 HDF_LOGE("volume param Is error!");
217 return AUDIO_HAL_ERR_INVALID_PARAM;
218 }
219 param = param * PROXY_VOLUME_CHANGE;
220 }
221 if (AudioProxyPreprocessRender(hwRender, &data, &reply) < 0) {
222 return AUDIO_HAL_ERR_INTERNAL;
223 }
224 uint32_t tempParam = (uint32_t)param;
225 if (!HdfSbufWriteUint32(data, tempParam)) {
226 AudioProxyBufReplyRecycle(data, reply);
227 return AUDIO_HAL_ERR_INTERNAL;
228 }
229 int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, cmId, data, reply);
230 AudioProxyBufReplyRecycle(data, reply);
231 return ret;
232 }
233
AudioProxyCommonGetRenderCtrlParam(int cmId,AudioHandle handle,float * param)234 int32_t AudioProxyCommonGetRenderCtrlParam(int cmId, AudioHandle handle, float *param)
235 {
236 HDF_LOGI("%{public}s, ", __func__);
237 if (param == NULL) {
238 return AUDIO_HAL_ERR_INVALID_PARAM;
239 }
240 struct HdfSBuf *data = NULL;
241 struct HdfSBuf *reply = NULL;
242 struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
243 if (hwRender == NULL || hwRender->proxyRemoteHandle == NULL) {
244 HDF_LOGE("The pointer is empty");
245 return AUDIO_HAL_ERR_INVALID_PARAM;
246 }
247 if (AudioProxyPreprocessRender(hwRender, &data, &reply) < 0) {
248 return AUDIO_HAL_ERR_INTERNAL;
249 }
250 int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, cmId, data, reply);
251 if (ret < 0) {
252 AudioProxyBufReplyRecycle(data, reply);
253 return ret;
254 }
255 uint32_t tempParam = 0;
256 if (!HdfSbufReadUint32(reply, &tempParam)) {
257 AudioProxyBufReplyRecycle(data, reply);
258 return AUDIO_HAL_ERR_INTERNAL;
259 }
260 if (cmId == AUDIO_HDI_RENDER_GET_VOLUME) {
261 *param = (float)tempParam / PROXY_VOLUME_CHANGE;
262 } else {
263 *param = (float)tempParam;
264 }
265 AudioProxyBufReplyRecycle(data, reply);
266 return AUDIO_HAL_SUCCESS;
267 }
268
AudioProxyGetMmapPositionRead(struct HdfSBuf * reply,uint64_t * frames,struct AudioTimeStamp * time)269 int32_t AudioProxyGetMmapPositionRead(struct HdfSBuf *reply, uint64_t *frames, struct AudioTimeStamp *time)
270 {
271 if (reply == NULL || frames == NULL || time == NULL) {
272 return HDF_FAILURE;
273 }
274 if (!HdfSbufReadUint64(reply, frames)) {
275 return HDF_FAILURE;
276 }
277 if (!HdfSbufReadInt64(reply, &time->tvSec)) {
278 return HDF_FAILURE;
279 }
280 if (!HdfSbufReadInt64(reply, &time->tvNSec)) {
281 return HDF_FAILURE;
282 }
283 return HDF_SUCCESS;
284 }
285
AudioProxyReqMmapBufferWrite(struct HdfSBuf * data,int32_t reqSize,const struct AudioMmapBufferDescriptor * desc)286 int32_t AudioProxyReqMmapBufferWrite(struct HdfSBuf *data, int32_t reqSize,
287 const struct AudioMmapBufferDescriptor *desc)
288 {
289 if (data == NULL || desc == NULL) {
290 return HDF_FAILURE;
291 }
292 if (!HdfSbufWriteInt32(data, reqSize)) {
293 return HDF_FAILURE;
294 }
295 uint64_t memAddr = (uint64_t)(uintptr_t)desc->memoryAddress;
296 if (!HdfSbufWriteUint64(data, memAddr)) {
297 return HDF_FAILURE;
298 }
299 if (!HdfSbufWriteFileDescriptor(data, desc->memoryFd)) {
300 return HDF_FAILURE;
301 }
302 if (!HdfSbufWriteInt32(data, desc->totalBufferFrames)) {
303 return HDF_FAILURE;
304 }
305 if (!HdfSbufWriteInt32(data, desc->transferFrameSize)) {
306 return HDF_FAILURE;
307 }
308 if (!HdfSbufWriteInt32(data, desc->isShareable)) {
309 return HDF_FAILURE;
310 }
311 if (!HdfSbufWriteUint32(data, desc->offset)) {
312 return HDF_FAILURE;
313 }
314 return HDF_SUCCESS;
315 }
316 }