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
AudioProxyPreprocessCapture(struct AudioHwCapture * capture,struct HdfSBuf ** data,struct HdfSBuf ** reply)104 int32_t AudioProxyPreprocessCapture(struct AudioHwCapture *capture, struct HdfSBuf **data, struct HdfSBuf **reply)
105 {
106 struct AudioHwCapture *hwCapture = capture;
107 if (hwCapture == nullptr || data == nullptr || reply == nullptr) {
108 return HDF_FAILURE;
109 }
110 auto capturePid = static_cast<uint32_t>(getpid());
111 const char *adapterName = hwCapture->captureParam.captureMode.hwInfo.adapterName;
112 if (adapterName == nullptr) {
113 HDF_LOGE("adapterName is NULL");
114 return HDF_FAILURE;
115 }
116 if (AudioProxyPreprocessSBuf(data, reply) < 0) {
117 return HDF_FAILURE;
118 }
119 if (!HdfRemoteServiceWriteInterfaceToken(capture->proxyRemoteHandle, *data)) {
120 AudioProxyBufReplyRecycle(*data, *reply);
121 return HDF_FAILURE;
122 }
123 if (!HdfSbufWriteString(*data, adapterName)) {
124 AudioProxyBufReplyRecycle(*data, *reply);
125 return HDF_FAILURE;
126 }
127 if (!HdfSbufWriteUint32(*data, capturePid)) {
128 AudioProxyBufReplyRecycle(*data, *reply);
129 return HDF_FAILURE;
130 }
131 return HDF_SUCCESS;
132 }
133
AudioProxyWriteSampleAttributes(struct HdfSBuf * data,const struct AudioSampleAttributes * attrs)134 int32_t AudioProxyWriteSampleAttributes(struct HdfSBuf *data, const struct AudioSampleAttributes *attrs)
135 {
136 if (data == NULL || attrs == NULL) {
137 return HDF_FAILURE;
138 }
139 if (!HdfSbufWriteUint32(data, (uint32_t)attrs->type)) {
140 return HDF_FAILURE;
141 }
142 if (!HdfSbufWriteUint32(data, (uint32_t)attrs->interleaved)) {
143 return HDF_FAILURE;
144 }
145 if (!HdfSbufWriteUint32(data, (uint32_t)attrs->format)) {
146 return HDF_FAILURE;
147 }
148 if (!HdfSbufWriteUint32(data, attrs->sampleRate)) {
149 return HDF_FAILURE;
150 }
151 if (!HdfSbufWriteUint32(data, attrs->channelCount)) {
152 return HDF_FAILURE;
153 }
154 if (!HdfSbufWriteUint32(data, attrs->period)) {
155 return HDF_FAILURE;
156 }
157 if (!HdfSbufWriteUint32(data, attrs->frameSize)) {
158 return HDF_FAILURE;
159 }
160 if (!HdfSbufWriteUint32(data, (uint32_t)(attrs->isBigEndian))) {
161 return HDF_FAILURE;
162 }
163 if (!HdfSbufWriteUint32(data, (uint32_t)(attrs->isSignedData))) {
164 return HDF_FAILURE;
165 }
166 if (!HdfSbufWriteUint32(data, attrs->startThreshold)) {
167 return HDF_FAILURE;
168 }
169 if (!HdfSbufWriteUint32(data, attrs->stopThreshold)) {
170 return HDF_FAILURE;
171 }
172 if (!HdfSbufWriteUint32(data, attrs->silenceThreshold)) {
173 return HDF_FAILURE;
174 }
175 return HDF_SUCCESS;
176 }
177
AudioProxyReadSapmleAttrbutes(struct HdfSBuf * reply,struct AudioSampleAttributes * attrs)178 int32_t AudioProxyReadSapmleAttrbutes(struct HdfSBuf *reply, struct AudioSampleAttributes *attrs)
179 {
180 if (reply == NULL || attrs == NULL) {
181 return HDF_FAILURE;
182 }
183 uint32_t tempType = 0;
184 if (!HdfSbufReadUint32(reply, &tempType)) {
185 return HDF_FAILURE;
186 }
187 attrs->type = (AudioCategory)tempType;
188 uint32_t tempInterleaved = 0;
189 if (!HdfSbufReadUint32(reply, &tempInterleaved)) {
190 return HDF_FAILURE;
191 }
192 attrs->interleaved = (bool)tempInterleaved;
193 uint32_t tempFormat = 0;
194 if (!HdfSbufReadUint32(reply, &tempFormat)) {
195 return HDF_FAILURE;
196 }
197 attrs->format = (AudioFormat)tempFormat;
198 if (!HdfSbufReadUint32(reply, &attrs->sampleRate)) {
199 return HDF_FAILURE;
200 }
201 if (!HdfSbufReadUint32(reply, &attrs->channelCount)) {
202 return HDF_FAILURE;
203 }
204 if (!HdfSbufReadUint32(reply, &attrs->period)) {
205 return HDF_FAILURE;
206 }
207 if (!HdfSbufReadUint32(reply, &attrs->frameSize)) {
208 return HDF_FAILURE;
209 }
210 if (!HdfSbufReadUint32(reply, &tempInterleaved)) {
211 return HDF_FAILURE;
212 }
213 attrs->isBigEndian = (bool)tempInterleaved;
214 if (!HdfSbufReadUint32(reply, &tempInterleaved)) {
215 return HDF_FAILURE;
216 }
217 attrs->isSignedData = (bool)tempInterleaved;
218 if (!HdfSbufReadUint32(reply, &attrs->startThreshold)) {
219 return HDF_FAILURE;
220 }
221 if (!HdfSbufReadUint32(reply, &attrs->stopThreshold)) {
222 return HDF_FAILURE;
223 }
224 if (!HdfSbufReadUint32(reply, &attrs->silenceThreshold)) {
225 return HDF_FAILURE;
226 }
227 return HDF_SUCCESS;
228 }
229
AudioProxyCommonSetRenderCtrlParam(int cmId,AudioHandle handle,float param)230 int32_t AudioProxyCommonSetRenderCtrlParam(int cmId, AudioHandle handle, float param)
231 {
232 HDF_LOGI("%{public}s, ", __func__);
233 struct HdfSBuf *data = NULL;
234 struct HdfSBuf *reply = NULL;
235 struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
236 if (hwRender == NULL || hwRender->proxyRemoteHandle == NULL) {
237 HDF_LOGE("The hwRender parameter is null");
238 return AUDIO_HAL_ERR_INVALID_PARAM;
239 }
240 if (param < 0) {
241 HDF_LOGE("Set param is invalid, Please check param!");
242 return AUDIO_HAL_ERR_INVALID_PARAM;
243 }
244 if (cmId == AUDIO_HDI_RENDER_SET_VOLUME) {
245 if (param > 1.0) {
246 HDF_LOGE("volume param Is error!");
247 return AUDIO_HAL_ERR_INVALID_PARAM;
248 }
249 param = param * PROXY_VOLUME_CHANGE;
250 }
251 if (AudioProxyPreprocessRender(hwRender, &data, &reply) < 0) {
252 return AUDIO_HAL_ERR_INTERNAL;
253 }
254 uint32_t tempParam = (uint32_t)param;
255 if (!HdfSbufWriteUint32(data, tempParam)) {
256 AudioProxyBufReplyRecycle(data, reply);
257 return AUDIO_HAL_ERR_INTERNAL;
258 }
259 int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, cmId, data, reply);
260 AudioProxyBufReplyRecycle(data, reply);
261 return ret;
262 }
263
AudioProxyCommonGetRenderCtrlParam(int cmId,AudioHandle handle,float * param)264 int32_t AudioProxyCommonGetRenderCtrlParam(int cmId, AudioHandle handle, float *param)
265 {
266 HDF_LOGI("%{public}s, ", __func__);
267 if (param == NULL) {
268 return AUDIO_HAL_ERR_INVALID_PARAM;
269 }
270 struct HdfSBuf *data = NULL;
271 struct HdfSBuf *reply = NULL;
272 struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
273 if (hwRender == NULL || hwRender->proxyRemoteHandle == NULL) {
274 HDF_LOGE("The pointer is empty");
275 return AUDIO_HAL_ERR_INVALID_PARAM;
276 }
277 if (AudioProxyPreprocessRender(hwRender, &data, &reply) < 0) {
278 return AUDIO_HAL_ERR_INTERNAL;
279 }
280 int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, cmId, data, reply);
281 if (ret < 0) {
282 AudioProxyBufReplyRecycle(data, reply);
283 return ret;
284 }
285 uint32_t tempParam = 0;
286 if (!HdfSbufReadUint32(reply, &tempParam)) {
287 AudioProxyBufReplyRecycle(data, reply);
288 return AUDIO_HAL_ERR_INTERNAL;
289 }
290 if (cmId == AUDIO_HDI_RENDER_GET_VOLUME) {
291 *param = (float)tempParam / PROXY_VOLUME_CHANGE;
292 } else {
293 *param = (float)tempParam;
294 }
295 AudioProxyBufReplyRecycle(data, reply);
296 return AUDIO_HAL_SUCCESS;
297 }
298
AudioProxyGetMmapPositionRead(struct HdfSBuf * reply,uint64_t * frames,struct AudioTimeStamp * time)299 int32_t AudioProxyGetMmapPositionRead(struct HdfSBuf *reply, uint64_t *frames, struct AudioTimeStamp *time)
300 {
301 if (reply == NULL || frames == NULL || time == NULL) {
302 return HDF_FAILURE;
303 }
304 if (!HdfSbufReadUint64(reply, frames)) {
305 return HDF_FAILURE;
306 }
307 if (!HdfSbufReadInt64(reply, &time->tvSec)) {
308 return HDF_FAILURE;
309 }
310 if (!HdfSbufReadInt64(reply, &time->tvNSec)) {
311 return HDF_FAILURE;
312 }
313 return HDF_SUCCESS;
314 }
315
AudioProxyReqMmapBufferWrite(struct HdfSBuf * data,int32_t reqSize,const struct AudioMmapBufferDescriptor * desc)316 int32_t AudioProxyReqMmapBufferWrite(struct HdfSBuf *data, int32_t reqSize,
317 const struct AudioMmapBufferDescriptor *desc)
318 {
319 if (data == NULL || desc == NULL) {
320 return HDF_FAILURE;
321 }
322 if (!HdfSbufWriteInt32(data, reqSize)) {
323 return HDF_FAILURE;
324 }
325 uint64_t memAddr = (uint64_t)(uintptr_t)desc->memoryAddress;
326 if (!HdfSbufWriteUint64(data, memAddr)) {
327 return HDF_FAILURE;
328 }
329 if (!HdfSbufWriteFileDescriptor(data, desc->memoryFd)) {
330 return HDF_FAILURE;
331 }
332 if (!HdfSbufWriteInt32(data, desc->totalBufferFrames)) {
333 return HDF_FAILURE;
334 }
335 if (!HdfSbufWriteInt32(data, desc->transferFrameSize)) {
336 return HDF_FAILURE;
337 }
338 if (!HdfSbufWriteInt32(data, desc->isShareable)) {
339 return HDF_FAILURE;
340 }
341 if (!HdfSbufWriteUint32(data, desc->offset)) {
342 return HDF_FAILURE;
343 }
344 return HDF_SUCCESS;
345 }
346 }