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 "hdf_audio_server_render.h"
16 #include "hdf_audio_server_common.h"
17 namespace OHOS::HDI::Audio_Bluetooth {
GetInitRenderParaAttrs(struct HdfSBuf * data,struct AudioSampleAttributes * attrs)18 int32_t GetInitRenderParaAttrs(struct HdfSBuf *data, struct AudioSampleAttributes *attrs)
19 {
20 if (data == NULL || attrs == NULL) {
21 return HDF_FAILURE;
22 }
23 uint32_t tempRenderPara = 0;
24 if (!HdfSbufReadUint32(data, &tempRenderPara)) {
25 HDF_LOGE("%{public}s: read tempRenderPara fail", __func__);
26 return HDF_FAILURE;
27 }
28 attrs->type = (AudioCategory)tempRenderPara;
29 if (!HdfSbufReadUint32(data, &attrs->period)) {
30 HDF_LOGE("%{public}s: read period fail", __func__);
31 return HDF_FAILURE;
32 }
33 if (!HdfSbufReadUint32(data, &attrs->frameSize)) {
34 HDF_LOGE("%{public}s: read frameSize fail", __func__);
35 return HDF_FAILURE;
36 }
37 if (!HdfSbufReadUint32(data, &attrs->startThreshold)) {
38 HDF_LOGE("%{public}s: read startThreshold fail", __func__);
39 return HDF_FAILURE;
40 }
41 if (!HdfSbufReadUint32(data, &attrs->stopThreshold)) {
42 HDF_LOGE("%{public}s: read stopThreshold fail", __func__);
43 return HDF_FAILURE;
44 }
45 if (!HdfSbufReadUint32(data, &attrs->silenceThreshold)) {
46 HDF_LOGE("%{public}s: read silenceThreshold fail", __func__);
47 return HDF_FAILURE;
48 }
49 if (!HdfSbufReadUint32(data, &tempRenderPara)) {
50 HDF_LOGE("%{public}s: read bool isBigEndian fail", __func__);
51 return HDF_FAILURE;
52 }
53 attrs->isBigEndian = (bool)tempRenderPara;
54 return HDF_SUCCESS;
55 }
56
GetInitRenderPara(struct HdfSBuf * data,struct AudioDeviceDescriptor * devDesc,struct AudioSampleAttributes * attrs)57 int32_t GetInitRenderPara(struct HdfSBuf *data, struct AudioDeviceDescriptor *devDesc,
58 struct AudioSampleAttributes *attrs)
59 {
60 if (data == NULL || devDesc == NULL || attrs == NULL) {
61 return HDF_FAILURE;
62 }
63 uint32_t tempRenderPara = 0;
64 if (!HdfSbufReadUint32(data, &tempRenderPara)) {
65 HDF_LOGE("%{public}s: read attrs format fail", __func__);
66 return HDF_FAILURE;
67 }
68 attrs->format = (AudioFormat)tempRenderPara;
69 if (!HdfSbufReadUint32(data, &attrs->channelCount)) {
70 HDF_LOGE("%{public}s: read channelCount fail", __func__);
71 return HDF_FAILURE;
72 }
73 if (!HdfSbufReadUint32(data, &attrs->sampleRate)) {
74 HDF_LOGE("%{public}s: read sampleRate fail", __func__);
75 return HDF_FAILURE;
76 }
77 if (!HdfSbufReadUint32(data, &tempRenderPara)) {
78 HDF_LOGE("%{public}s: read attrs interleaved fail", __func__);
79 return HDF_FAILURE;
80 }
81 attrs->interleaved = (bool)tempRenderPara;
82 if (GetInitRenderParaAttrs(data, attrs) < 0) {
83 return HDF_FAILURE;
84 }
85 if (!HdfSbufReadUint32(data, &tempRenderPara)) {
86 HDF_LOGE("%{public}s: read attrs isSignedData fail", __func__);
87 return HDF_FAILURE;
88 }
89 attrs->isSignedData = (bool)tempRenderPara;
90 if (!HdfSbufReadUint32(data, &devDesc->portId)) {
91 HDF_LOGE("%{public}s: read portId fail", __func__);
92 return HDF_FAILURE;
93 }
94 if (!HdfSbufReadUint32(data, &tempRenderPara)) {
95 HDF_LOGE("%{public}s: read tempRenderPara fail", __func__);
96 return HDF_FAILURE;
97 }
98 devDesc->pins = (AudioPortPin)tempRenderPara;
99 devDesc->desc = NULL;
100 return HDF_SUCCESS;
101 }
102
HdiServiceCreatRender(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)103 int32_t HdiServiceCreatRender(const struct HdfDeviceIoClient *client,
104 struct HdfSBuf *data, struct HdfSBuf *reply)
105 {
106 if (client == NULL || data == NULL || reply == NULL) {
107 return AUDIO_HAL_ERR_INVALID_PARAM;
108 }
109 struct AudioAdapter *adapter = NULL;
110 struct AudioDeviceDescriptor devDesc;
111 struct AudioSampleAttributes attrs;
112 struct AudioRender *render = NULL;
113 const char *adapterName = NULL;
114 uint32_t renderPid = 0;
115 if ((adapterName = HdfSbufReadString(data)) == NULL) {
116 HDF_LOGE("%{public}s: adapterNameCase Is NULL", __func__);
117 return AUDIO_HAL_ERR_INVALID_PARAM;
118 }
119 if (!HdfSbufReadUint32(data, &renderPid)) {
120 return AUDIO_HAL_ERR_INTERNAL;
121 }
122 HDF_LOGE("HdiServiceCreatRender: renderPid = %{public}u", renderPid);
123 int32_t ret = GetInitRenderPara(data, &devDesc, &attrs);
124 if (ret < 0) {
125 HDF_LOGE("%{public}s: GetInitRenderPara fail", __func__);
126 return AUDIO_HAL_ERR_INTERNAL;
127 }
128 if (AudioAdapterListGetAdapter(adapterName, &adapter)) {
129 HDF_LOGE("%{public}s: fail", __func__);
130 return AUDIO_HAL_ERR_INTERNAL;
131 }
132 if (adapter == NULL) {
133 HDF_LOGE("%{public}s: adapter is NULL!", __func__);
134 return AUDIO_HAL_ERR_INVALID_PARAM;
135 }
136 const int32_t priority = attrs.type;
137 ret = AudioCreatRenderCheck(adapterName, priority);
138 if (ret < 0) {
139 HDF_LOGE("%{public}s: AudioCreatRenderCheck: Render is working can not replace!", __func__);
140 return ret;
141 }
142 ret = adapter->CreateRender(adapter, &devDesc, &attrs, &render);
143 if (render == NULL || ret < 0) {
144 HDF_LOGE("%{public}s: Failed to CreateRender", __func__);
145 return AUDIO_HAL_ERR_INTERNAL;
146 }
147 if (AudioAddRenderInfoInAdapter(adapterName, render, adapter, priority, renderPid)) {
148 HDF_LOGE("%{public}s: AudioAddRenderInfoInAdapter", __func__);
149 adapter->DestroyRender(adapter, render);
150 return AUDIO_HAL_ERR_INTERNAL;
151 }
152 return AUDIO_HAL_SUCCESS;
153 }
154
HdiServiceRenderDestory(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)155 int32_t HdiServiceRenderDestory(const struct HdfDeviceIoClient *client,
156 struct HdfSBuf *data, struct HdfSBuf *reply)
157 {
158 if (client == NULL || data == NULL || reply == NULL) {
159 return AUDIO_HAL_ERR_INVALID_PARAM;
160 }
161 struct AudioAdapter *adapter = NULL;
162 struct AudioRender *render = NULL;
163 const char *adapterName = NULL;
164 uint32_t pid = 0;
165 if (HdiServiceRenderCaptureReadData(data, &adapterName, &pid) < 0) {
166 return AUDIO_HAL_ERR_INTERNAL;
167 }
168 int32_t ret = AudioAdapterListGetRender(adapterName, &render, pid);
169 if (ret < 0) {
170 return ret;
171 }
172 ret = AudioAdapterListGetAdapterRender(adapterName, &adapter, &render);
173 if (ret < 0) {
174 return ret;
175 }
176 if (adapter == NULL || render == NULL) {
177 return AUDIO_HAL_ERR_INVALID_PARAM;
178 }
179 ret = adapter->DestroyRender(adapter, render);
180 if (ret < 0) {
181 HDF_LOGE("%{public}s: DestroyRender failed!", __func__);
182 return ret;
183 }
184 if (AudioDestroyRenderInfoInAdapter(adapterName)) {
185 return AUDIO_HAL_ERR_INTERNAL;
186 }
187 return AUDIO_HAL_SUCCESS;
188 }
189
HdiServiceRenderStart(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)190 int32_t HdiServiceRenderStart(const struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
191 {
192 if (client == NULL || data == NULL || reply == NULL) {
193 return AUDIO_HAL_ERR_INVALID_PARAM;
194 }
195 struct AudioRender *render = NULL;
196 int ret = AudioAdapterListCheckAndGetRender(&render, data);
197 if (ret < 0) {
198 return ret;
199 }
200 return render->control.Start((AudioHandle)render);
201 }
202
HdiServiceRenderStop(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)203 int32_t HdiServiceRenderStop(const struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
204 {
205 if (client == NULL || data == NULL || reply == NULL) {
206 return AUDIO_HAL_ERR_INVALID_PARAM;
207 }
208 struct AudioRender *render = NULL;
209 int ret = AudioAdapterListCheckAndGetRender(&render, data);
210 if (ret < 0) {
211 return ret;
212 }
213 return render->control.Stop((AudioHandle)render);
214 }
215
HdiServiceRenderPause(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)216 int32_t HdiServiceRenderPause(const struct HdfDeviceIoClient *client,
217 struct HdfSBuf *data, struct HdfSBuf *reply)
218 {
219 if (client == NULL || data == NULL || reply == NULL) {
220 return AUDIO_HAL_ERR_INVALID_PARAM;
221 }
222 HDF_LOGE("%{public}s: enter", __func__);
223 struct AudioRender *render = NULL;
224 int ret = AudioAdapterListCheckAndGetRender(&render, data);
225 if (ret < 0) {
226 return ret;
227 }
228 return render->control.Pause((AudioHandle)render);
229 }
230
HdiServiceRenderResume(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)231 int32_t HdiServiceRenderResume(const struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
232 {
233 if (client == NULL || data == NULL || reply == NULL) {
234 return AUDIO_HAL_ERR_INVALID_PARAM;
235 }
236 struct AudioRender *render = NULL;
237 int ret = AudioAdapterListCheckAndGetRender(&render, data);
238 if (ret < 0) {
239 return ret;
240 }
241 return render->control.Resume((AudioHandle)render);
242 }
243
HdiServiceRenderFlush(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)244 int32_t HdiServiceRenderFlush(const struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
245 {
246 if (client == NULL || data == NULL || reply == NULL) {
247 HDF_LOGI("%{public}s: The parameter is empty", __func__);
248 return AUDIO_HAL_ERR_INVALID_PARAM;
249 }
250 struct AudioRender *render = NULL;
251 int ret = AudioAdapterListCheckAndGetRender(&render, data);
252 if (ret < 0) {
253 return ret;
254 }
255 return render->control.Flush((AudioHandle)render);
256 }
257
HdiServiceRenderGetFrameSize(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)258 int32_t HdiServiceRenderGetFrameSize(const struct HdfDeviceIoClient *client,
259 struct HdfSBuf *data, struct HdfSBuf *reply)
260 {
261 if (client == NULL || data == NULL || reply == NULL) {
262 return AUDIO_HAL_ERR_INVALID_PARAM;
263 }
264 uint64_t size;
265 struct AudioRender *render = NULL;
266 int ret = AudioAdapterListCheckAndGetRender(&render, data);
267 if (ret < 0) {
268 return ret;
269 }
270 if (render->attr.GetFrameSize((AudioHandle)render, &size)) {
271 return AUDIO_HAL_ERR_INTERNAL;
272 }
273 if (!HdfSbufWriteUint64(reply, size)) {
274 return AUDIO_HAL_ERR_INTERNAL;
275 }
276 return AUDIO_HAL_SUCCESS;
277 }
278
HdiServiceRenderGetFrameCount(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)279 int32_t HdiServiceRenderGetFrameCount(const struct HdfDeviceIoClient *client,
280 struct HdfSBuf *data, struct HdfSBuf *reply)
281 {
282 if (client == NULL || data == NULL || reply == NULL) {
283 return AUDIO_HAL_ERR_INVALID_PARAM;
284 }
285 uint64_t count;
286 struct AudioRender *render = NULL;
287 int ret = AudioAdapterListCheckAndGetRender(&render, data);
288 if (ret < 0) {
289 return ret;
290 }
291 if (render->attr.GetFrameCount((AudioHandle)render, &count)) {
292 return AUDIO_HAL_ERR_INTERNAL;
293 }
294 if (!HdfSbufWriteUint64(reply, count)) {
295 return AUDIO_HAL_ERR_INTERNAL;
296 }
297 return AUDIO_HAL_SUCCESS;
298 }
299
HdiServiceRenderSetSampleAttr(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)300 int32_t HdiServiceRenderSetSampleAttr(const struct HdfDeviceIoClient *client,
301 struct HdfSBuf *data, struct HdfSBuf *reply)
302 {
303 if (client == NULL || data == NULL || reply == NULL) {
304 return AUDIO_HAL_ERR_INVALID_PARAM;
305 }
306 int ret;
307 struct AudioSampleAttributes attrs;
308 struct AudioRender *render = NULL;
309 ret = AudioAdapterListCheckAndGetRender(&render, data);
310 if (ret < 0) {
311 return ret;
312 }
313 if (ReadAudioSapmleAttrbutes(data, &attrs) < 0) {
314 return AUDIO_HAL_ERR_INTERNAL;
315 }
316 return render->attr.SetSampleAttributes((AudioHandle)render, &attrs);
317 }
318
HdiServiceRenderGetSampleAttr(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)319 int32_t HdiServiceRenderGetSampleAttr(const struct HdfDeviceIoClient *client,
320 struct HdfSBuf *data, struct HdfSBuf *reply)
321 {
322 if (client == NULL || data == NULL || reply == NULL) {
323 return AUDIO_HAL_ERR_INVALID_PARAM;
324 }
325 struct AudioSampleAttributes attrs;
326 struct AudioRender *render = NULL;
327 int32_t ret = AudioAdapterListCheckAndGetRender(&render, data);
328 if (ret < 0) {
329 return ret;
330 }
331 ret = render->attr.GetSampleAttributes((AudioHandle)render, &attrs);
332 if (ret < 0) {
333 return ret;
334 }
335 if (WriteAudioSampleAttributes(reply, &attrs) < 0) {
336 return AUDIO_HAL_ERR_INTERNAL;
337 }
338 return AUDIO_HAL_SUCCESS;
339 }
340
HdiServiceRenderGetCurChannelId(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)341 int32_t HdiServiceRenderGetCurChannelId(const struct HdfDeviceIoClient *client,
342 struct HdfSBuf *data, struct HdfSBuf *reply)
343 {
344 if (client == NULL || data == NULL || reply == NULL) {
345 return AUDIO_HAL_ERR_INVALID_PARAM;
346 }
347 uint32_t channelId;
348 struct AudioRender *render = NULL;
349 int ret = AudioAdapterListCheckAndGetRender(&render, data);
350 if (ret < 0) {
351 return ret;
352 }
353 ret = render->attr.GetCurrentChannelId((AudioHandle)render, &channelId);
354 if (ret < 0) {
355 return ret;
356 }
357 if (!HdfSbufWriteUint32(reply, channelId)) {
358 return AUDIO_HAL_ERR_INTERNAL;
359 }
360 return AUDIO_HAL_SUCCESS;
361 }
362
HdiServiceRenderCheckSceneCapability(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)363 int32_t HdiServiceRenderCheckSceneCapability(const struct HdfDeviceIoClient *client,
364 struct HdfSBuf *data, struct HdfSBuf *reply)
365 {
366 if (client == NULL || data == NULL || reply == NULL) {
367 return AUDIO_HAL_ERR_INVALID_PARAM;
368 }
369 uint32_t temporaryPins = 0;
370 struct AudioSceneDescriptor scene;
371 bool supported = false;
372 struct AudioRender *render = NULL;
373 int ret = AudioAdapterListCheckAndGetRender(&render, data);
374 if (ret < 0) {
375 return ret;
376 }
377 if (!HdfSbufReadUint32(data, &scene.scene.id)) {
378 return AUDIO_HAL_ERR_INTERNAL;
379 }
380 if (!HdfSbufReadUint32(data, &temporaryPins)) {
381 return AUDIO_HAL_ERR_INTERNAL;
382 }
383 scene.desc.pins = (AudioPortPin)temporaryPins;
384 ret = render->scene.CheckSceneCapability((AudioHandle)render, &scene, &supported);
385 if (ret < 0) {
386 return ret;
387 }
388 uint32_t tempSupported = (uint32_t)supported;
389 if (!HdfSbufWriteUint32(reply, tempSupported)) {
390 return AUDIO_HAL_ERR_INTERNAL;
391 }
392 return AUDIO_HAL_SUCCESS;
393 }
394
HdiServiceRenderSelectScene(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)395 int32_t HdiServiceRenderSelectScene(const struct HdfDeviceIoClient *client,
396 struct HdfSBuf *data, struct HdfSBuf *reply)
397 {
398 if (client == NULL || data == NULL || reply == NULL) {
399 return AUDIO_HAL_ERR_INVALID_PARAM;
400 }
401 uint32_t tempPins = 0;
402 struct AudioSceneDescriptor scene;
403 struct AudioRender *render = NULL;
404 int ret = AudioAdapterListCheckAndGetRender(&render, data);
405 if (ret < 0) {
406 return ret;
407 }
408 if (!HdfSbufReadUint32(data, &scene.scene.id)) {
409 HDF_LOGI("%{public}s: Read id Fail", __func__);
410 return AUDIO_HAL_ERR_INTERNAL;
411 }
412 if (!HdfSbufReadUint32(data, &tempPins)) {
413 HDF_LOGI("%{public}s: Read tempPins Fail", __func__);
414 return AUDIO_HAL_ERR_INTERNAL;
415 }
416 scene.desc.pins = (AudioPortPin)tempPins;
417 return render->scene.SelectScene((AudioHandle)render, &scene);
418 }
419
HdiServiceRenderGetMute(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)420 int32_t HdiServiceRenderGetMute(const struct HdfDeviceIoClient *client,
421 struct HdfSBuf *data, struct HdfSBuf *reply)
422 {
423 if (client == NULL || data == NULL || reply == NULL) {
424 HDF_LOGI("%{public}s: parameter is empty", __func__);
425 return AUDIO_HAL_ERR_INVALID_PARAM;
426 }
427 bool mute = false;
428 struct AudioRender *render = NULL;
429 int ret = AudioAdapterListCheckAndGetRender(&render, data);
430 if (ret < 0) {
431 return ret;
432 }
433 ret = render->volume.GetMute((AudioHandle)render, &mute);
434 if (ret < 0) {
435 return ret;
436 }
437 uint32_t tempMute = (uint32_t)mute;
438 if (!HdfSbufWriteUint32(reply, tempMute)) {
439 return AUDIO_HAL_ERR_INTERNAL;
440 }
441 return AUDIO_HAL_SUCCESS;
442 }
443
HdiServiceRenderSetMute(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)444 int32_t HdiServiceRenderSetMute(const struct HdfDeviceIoClient *client,
445 struct HdfSBuf *data, struct HdfSBuf *reply)
446 {
447 if (client == NULL || data == NULL || reply == NULL) {
448 return AUDIO_HAL_ERR_INVALID_PARAM;
449 }
450 bool mute = false;
451 struct AudioRender *render = NULL;
452 int ret = AudioAdapterListCheckAndGetRender(&render, data);
453 if (ret < 0) {
454 return ret;
455 }
456 uint32_t tempMute = 0;
457 if (!HdfSbufReadUint32(data, &tempMute)) {
458 return AUDIO_HAL_ERR_INTERNAL;
459 }
460 mute = (bool)tempMute;
461 return render->volume.SetMute((AudioHandle)render, mute);
462 }
463
HdiServiceRenderSetVolume(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)464 int32_t HdiServiceRenderSetVolume(const struct HdfDeviceIoClient *client,
465 struct HdfSBuf *data, struct HdfSBuf *reply)
466 {
467 if (client == NULL || data == NULL || reply == NULL) {
468 return AUDIO_HAL_ERR_INVALID_PARAM;
469 }
470 uint32_t volume = 0;
471 struct AudioRender *render = NULL;
472 int ret = AudioAdapterListCheckAndGetRender(&render, data);
473 if (ret < 0) {
474 return ret;
475 }
476 if (!HdfSbufReadUint32(data, &volume)) {
477 return AUDIO_HAL_ERR_INTERNAL;
478 }
479 float setVolume = (float)volume / VOLUME_CHANGE;
480 return render->volume.SetVolume((AudioHandle)render, setVolume);
481 }
482
HdiServiceRenderGetVolume(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)483 int32_t HdiServiceRenderGetVolume(const struct HdfDeviceIoClient *client,
484 struct HdfSBuf *data, struct HdfSBuf *reply)
485 {
486 if (client == NULL || data == NULL || reply == NULL) {
487 return AUDIO_HAL_ERR_INVALID_PARAM;
488 }
489 float volume;
490 struct AudioRender *render = NULL;
491 int ret = AudioAdapterListCheckAndGetRender(&render, data);
492 if (ret < 0) {
493 return ret;
494 }
495 ret = render->volume.GetVolume((AudioHandle)render, &volume);
496 if (ret < 0) {
497 return ret;
498 }
499 uint32_t tempVolume = (uint32_t)(volume * VOLUME_CHANGE);
500 if (!HdfSbufWriteUint32(reply, tempVolume)) {
501 return AUDIO_HAL_ERR_INTERNAL;
502 }
503 return AUDIO_HAL_SUCCESS;
504 }
505
HdiServiceRenderGetGainThreshold(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)506 int32_t HdiServiceRenderGetGainThreshold(const struct HdfDeviceIoClient *client,
507 struct HdfSBuf *data, struct HdfSBuf *reply)
508 {
509 if (client == NULL || data == NULL || reply == NULL) {
510 return AUDIO_HAL_ERR_INVALID_PARAM;
511 }
512 float min, max;
513 struct AudioRender *render = NULL;
514 int ret = AudioAdapterListCheckAndGetRender(&render, data);
515 if (ret < 0) {
516 return ret;
517 }
518 ret = render->volume.GetGainThreshold((AudioHandle)render, &min, &max);
519 if (ret < 0) {
520 return ret;
521 }
522 uint32_t temporaryMin = (uint32_t)min;
523 if (!HdfSbufWriteUint32(reply, temporaryMin)) {
524 return AUDIO_HAL_ERR_INTERNAL;
525 }
526 uint32_t temporaryMax = (uint32_t)max;
527 if (!HdfSbufWriteUint32(reply, temporaryMax)) {
528 return AUDIO_HAL_ERR_INTERNAL;
529 }
530 return AUDIO_HAL_SUCCESS;
531 }
532
HdiServiceRenderGetGain(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)533 int32_t HdiServiceRenderGetGain(const struct HdfDeviceIoClient *client,
534 struct HdfSBuf *data, struct HdfSBuf *reply)
535 {
536 if (client == NULL || data == NULL || reply == NULL) {
537 return AUDIO_HAL_ERR_INVALID_PARAM;
538 }
539 float gain;
540 struct AudioRender *render = NULL;
541 int ret = AudioAdapterListCheckAndGetRender(&render, data);
542 if (ret < 0) {
543 return ret;
544 }
545 ret = render->volume.GetGain((AudioHandle)render, &gain);
546 if (ret < 0) {
547 return ret;
548 }
549 uint32_t tempGain = (uint32_t)gain;
550 if (!HdfSbufWriteUint32(reply, tempGain)) {
551 return AUDIO_HAL_ERR_INTERNAL;
552 }
553 return AUDIO_HAL_SUCCESS;
554 }
555
HdiServiceRenderSetGain(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)556 int32_t HdiServiceRenderSetGain(const struct HdfDeviceIoClient *client,
557 struct HdfSBuf *data, struct HdfSBuf *reply)
558 {
559 if (client == NULL || data == NULL || reply == NULL) {
560 return AUDIO_HAL_ERR_INVALID_PARAM;
561 }
562 uint32_t tempGain = 0;
563 struct AudioRender *render = NULL;
564 int ret = AudioAdapterListCheckAndGetRender(&render, data);
565 if (ret < 0) {
566 return ret;
567 }
568 if (!HdfSbufReadUint32(data, &tempGain)) {
569 return AUDIO_HAL_ERR_INTERNAL;
570 }
571 return render->volume.SetGain((AudioHandle)render, (float)tempGain);
572 }
573
HdiServiceRenderGetLatency(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)574 int32_t HdiServiceRenderGetLatency(const struct HdfDeviceIoClient *client,
575 struct HdfSBuf *data, struct HdfSBuf *reply)
576 {
577 if (client == NULL || data == NULL || reply == NULL) {
578 return AUDIO_HAL_ERR_INVALID_PARAM;
579 }
580 uint32_t ms;
581 struct AudioRender *render = NULL;
582 int ret = AudioAdapterListCheckAndGetRender(&render, data);
583 if (ret < 0) {
584 return ret;
585 }
586 ret = render->GetLatency(render, &ms);
587 if (ret < 0) {
588 return ret;
589 }
590 if (!HdfSbufWriteUint32(reply, ms)) {
591 return AUDIO_HAL_ERR_INTERNAL;
592 }
593 return AUDIO_HAL_SUCCESS;
594 }
595
HdiServiceRenderRenderFrame(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)596 int32_t HdiServiceRenderRenderFrame(const struct HdfDeviceIoClient *client,
597 struct HdfSBuf *data, struct HdfSBuf *reply)
598 {
599 if (client == NULL || data == NULL || reply == NULL) {
600 return AUDIO_HAL_ERR_INVALID_PARAM;
601 }
602 HDF_LOGI("%{public}s: enter", __func__);
603 char *frame = NULL;
604 uint32_t requestBytes;
605 uint64_t replyBytes;
606 struct AudioRender *render = NULL;
607 const char *adapterName = NULL;
608 uint32_t pid;
609 if (HdiServiceRenderCaptureReadData(data, &adapterName, &pid) < 0) {
610 HDF_LOGE("%{public}s: HdiServiceRenderCaptureReadData fail!", __func__);
611 return AUDIO_HAL_ERR_INTERNAL;
612 }
613 int32_t ret = AudioAdapterListGetRender(adapterName, &render, pid);
614 if (ret < 0) {
615 HDF_LOGE("%{public}s: AudioAdapterListGetRender fail", __func__);
616 return ret;
617 }
618 ret = AudioGetRenderStatus(adapterName);
619 if (ret < 0) {
620 HDF_LOGE("%{public}s: AudioGetRenderStatus fail", __func__);
621 return ret;
622 }
623 if (!HdfSbufReadBuffer(data, (const void **)&frame, &requestBytes)) {
624 HDF_LOGE("%{public}s: AudioAdapterListGetRender:HdfSbufReadBuffer fail", __func__);
625 return AUDIO_HAL_ERR_INTERNAL;
626 }
627 AudioSetRenderStatus(adapterName, true);
628 ret = render->RenderFrame(render, static_cast<const void *>(frame),
629 static_cast<uint64_t>(requestBytes), &replyBytes);
630 AudioSetRenderStatus(adapterName, false);
631 HDF_LOGE("%{public}s,%{public}u,%{public}ju", __func__, requestBytes, replyBytes);
632 if (ret < 0) {
633 HDF_LOGE("%{public}s: HdiServiceRenderRenderFrame ", __func__);
634 return ret;
635 }
636 return AUDIO_HAL_SUCCESS;
637 }
638
HdiServiceRenderGetRenderPosition(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)639 int32_t HdiServiceRenderGetRenderPosition(const struct HdfDeviceIoClient *client,
640 struct HdfSBuf *data, struct HdfSBuf *reply)
641 {
642 if (client == NULL || data == NULL || reply == NULL) {
643 return AUDIO_HAL_ERR_INVALID_PARAM;
644 }
645 struct AudioTimeStamp time;
646 struct AudioRender *render = NULL;
647 uint64_t frames;
648 int ret = AudioAdapterListCheckAndGetRender(&render, data);
649 if (ret < 0) {
650 return ret;
651 }
652 ret = render->GetRenderPosition(render, &frames, &time);
653 if (ret < 0) {
654 return ret;
655 }
656 ret = HdiServicePositionWrite(reply, frames, time);
657 if (ret < 0) {
658 return AUDIO_HAL_ERR_INTERNAL;
659 }
660 return AUDIO_HAL_SUCCESS;
661 }
662
HdiServiceRenderGetSpeed(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)663 int32_t HdiServiceRenderGetSpeed(const struct HdfDeviceIoClient *client,
664 struct HdfSBuf *data, struct HdfSBuf *reply)
665 {
666 if (client == NULL || data == NULL || reply == NULL) {
667 return AUDIO_HAL_ERR_INVALID_PARAM;
668 }
669 float speed;
670 struct AudioRender *render = NULL;
671 int ret = AudioAdapterListCheckAndGetRender(&render, data);
672 if (ret < 0) {
673 return ret;
674 }
675 ret = render->GetRenderSpeed(render, &speed);
676 if (ret < 0) {
677 return ret;
678 }
679 uint64_t tempSpeed = (uint64_t)speed;
680 if (!HdfSbufWriteUint64(reply, tempSpeed)) {
681 return AUDIO_HAL_ERR_INTERNAL;
682 }
683 return AUDIO_HAL_SUCCESS;
684 }
685
HdiServiceRenderSetSpeed(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)686 int32_t HdiServiceRenderSetSpeed(const struct HdfDeviceIoClient *client,
687 struct HdfSBuf *data, struct HdfSBuf *reply)
688 {
689 if (client == NULL || data == NULL || reply == NULL) {
690 return AUDIO_HAL_ERR_INVALID_PARAM;
691 }
692 uint64_t speed = 0;
693 struct AudioRender *render = NULL;
694 int ret = AudioAdapterListCheckAndGetRender(&render, data);
695 if (ret < 0) {
696 return ret;
697 }
698 if (!HdfSbufReadUint64(data, &speed)) {
699 return AUDIO_HAL_ERR_INTERNAL;
700 }
701 return render->SetRenderSpeed(render, (float)speed);
702 }
703
HdiServiceRenderSetChannelMode(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)704 int32_t HdiServiceRenderSetChannelMode(const struct HdfDeviceIoClient *client,
705 struct HdfSBuf *data, struct HdfSBuf *reply)
706 {
707 if (client == NULL || data == NULL || reply == NULL) {
708 return AUDIO_HAL_ERR_INVALID_PARAM;
709 }
710 AudioChannelMode mode;
711 struct AudioRender *render = NULL;
712 int ret = AudioAdapterListCheckAndGetRender(&render, data);
713 if (ret < 0) {
714 return ret;
715 }
716 uint32_t tempMode = 0;
717 if (!HdfSbufReadUint32(data, &tempMode)) {
718 return AUDIO_HAL_ERR_INTERNAL;
719 }
720 mode = (AudioChannelMode)tempMode;
721 return render->SetChannelMode(render, mode);
722 }
723
HdiServiceRenderGetChannelMode(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)724 int32_t HdiServiceRenderGetChannelMode(const struct HdfDeviceIoClient *client,
725 struct HdfSBuf *data, struct HdfSBuf *reply)
726 {
727 HDF_LOGE("%{public}s: enter", __func__);
728 if (client == NULL || data == NULL || reply == NULL) {
729 return AUDIO_HAL_ERR_INVALID_PARAM;
730 }
731 AudioChannelMode mode;
732 struct AudioRender *render = NULL;
733 int ret = AudioAdapterListCheckAndGetRender(&render, data);
734 if (ret < 0) {
735 return ret;
736 }
737 ret = render->GetChannelMode(render, &mode);
738 if (ret < 0) {
739 return ret;
740 }
741 uint32_t tempMode = (uint32_t)mode;
742 if (!HdfSbufWriteUint32(reply, tempMode)) {
743 return AUDIO_HAL_ERR_INTERNAL;
744 }
745 HDF_LOGE("%{public}s: out", __func__);
746 return AUDIO_HAL_SUCCESS;
747 }
748
HdiServiceRenderSetExtraParams(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)749 int32_t HdiServiceRenderSetExtraParams(const struct HdfDeviceIoClient *client,
750 struct HdfSBuf *data, struct HdfSBuf *reply)
751 {
752 HDF_LOGE("%{public}s: enter", __func__);
753 if (client == NULL || data == NULL || reply == NULL) {
754 return AUDIO_HAL_ERR_INVALID_PARAM;
755 }
756 struct AudioRender *render = NULL;
757 int32_t ret = AudioAdapterListCheckAndGetRender(&render, data);
758 if (ret < 0) {
759 return ret;
760 }
761 const char *keyValueList = NULL;
762 if ((keyValueList = HdfSbufReadString(data)) == NULL) {
763 HDF_LOGE("%{public}s: keyValueList Is NULL", __func__);
764 return AUDIO_HAL_ERR_INTERNAL;
765 }
766 return render->attr.SetExtraParams((AudioHandle)render, keyValueList);
767 }
768
HdiServiceRenderGetExtraParams(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)769 int32_t HdiServiceRenderGetExtraParams(const struct HdfDeviceIoClient *client,
770 struct HdfSBuf *data, struct HdfSBuf *reply)
771 {
772 HDF_LOGE("%{public}s: enter", __func__);
773 if (client == NULL || data == NULL || reply == NULL) {
774 return AUDIO_HAL_ERR_INVALID_PARAM;
775 }
776 int32_t listLenth;
777 struct AudioRender *render = NULL;
778 int ret = AudioAdapterListCheckAndGetRender(&render, data);
779 if (ret < 0) {
780 return ret;
781 }
782 if (!HdfSbufReadInt32(data, &listLenth)) {
783 return AUDIO_HAL_ERR_INTERNAL;
784 }
785 if (listLenth <= 0 || listLenth > STR_MAX - 1) {
786 return AUDIO_HAL_ERR_INTERNAL;
787 }
788 char keyValueList[STR_MAX] = { 0 };
789 ret = render->attr.GetExtraParams((AudioHandle)render, keyValueList, listLenth);
790 if (ret < 0) {
791 return ret;
792 }
793 if (!HdfSbufWriteString(reply, keyValueList)) {
794 return AUDIO_HAL_ERR_INTERNAL;
795 }
796 HDF_LOGE("%{public}s: out", __func__);
797 return AUDIO_HAL_SUCCESS;
798 }
799
HdiServiceRenderReqMmapBuffer(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)800 int32_t HdiServiceRenderReqMmapBuffer(const struct HdfDeviceIoClient *client,
801 struct HdfSBuf *data, struct HdfSBuf *reply)
802 {
803 HDF_LOGE("%{public}s: enter", __func__);
804 if (client == NULL || data == NULL || reply == NULL) {
805 return AUDIO_HAL_ERR_INVALID_PARAM;
806 }
807 struct AudioMmapBufferDescripter desc;
808 int32_t reqSize = 0;
809 struct AudioRender *render = NULL;
810 int ret = AudioAdapterListCheckAndGetRender(&render, data);
811 if (ret < 0) {
812 return ret;
813 }
814 if (!HdfSbufReadInt32(data, &reqSize)) {
815 return AUDIO_HAL_ERR_INTERNAL;
816 }
817 if (HdiServiceReqMmapBuffer(&desc, data) < 0) {
818 return AUDIO_HAL_ERR_INTERNAL;
819 }
820 return render->attr.ReqMmapBuffer((AudioHandle)render, reqSize, &desc);
821 }
822
HdiServiceRenderGetMmapPosition(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)823 int32_t HdiServiceRenderGetMmapPosition(const struct HdfDeviceIoClient *client,
824 struct HdfSBuf *data, struct HdfSBuf *reply)
825 {
826 HDF_LOGE("%{public}s: enter", __func__);
827 if (client == NULL || data == NULL || reply == NULL) {
828 return AUDIO_HAL_ERR_INVALID_PARAM;
829 }
830 uint64_t frames;
831 struct AudioTimeStamp time;
832 struct AudioRender *render = NULL;
833 int ret = AudioAdapterListCheckAndGetRender(&render, data);
834 if (ret < 0) {
835 return ret;
836 }
837 ret = render->attr.GetMmapPosition((AudioHandle)render, &frames, &time);
838 if (ret < 0) {
839 return ret;
840 }
841 if (HdiServicePositionWrite(reply, frames, time) < 0) {
842 return AUDIO_HAL_ERR_INTERNAL;
843 }
844 HDF_LOGE("%{public}s: out", __func__);
845 return AUDIO_HAL_SUCCESS;
846 }
847
HdiServiceRenderTurnStandbyMode(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)848 int32_t HdiServiceRenderTurnStandbyMode(const struct HdfDeviceIoClient *client,
849 struct HdfSBuf *data, struct HdfSBuf *reply)
850 {
851 if (client == NULL || data == NULL || reply == NULL) {
852 HDF_LOGE("%{public}s: The pointer is null", __func__);
853 return AUDIO_HAL_ERR_INVALID_PARAM;
854 }
855 struct AudioRender *render = NULL;
856 int ret = AudioAdapterListCheckAndGetRender(&render, data);
857 if (ret < 0) {
858 return ret;
859 }
860 return render->control.Stop((AudioHandle)render);
861 }
862
HdiServiceRenderDevDump(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)863 int32_t HdiServiceRenderDevDump(const struct HdfDeviceIoClient *client,
864 struct HdfSBuf *data, struct HdfSBuf *reply)
865 {
866 if (client == NULL || data == NULL || reply == NULL) {
867 return AUDIO_HAL_ERR_INVALID_PARAM;
868 }
869 int32_t range = 0;
870 int32_t fd = 0;
871 struct AudioRender *render = NULL;
872 int ret = AudioAdapterListCheckAndGetRender(&render, data);
873 if (ret < 0) {
874 return ret;
875 }
876 if (!HdfSbufReadInt32(data, &range)) {
877 return AUDIO_HAL_ERR_INTERNAL;
878 }
879 if (!HdfSbufReadInt32(data, &fd)) {
880 return AUDIO_HAL_ERR_INTERNAL;
881 }
882 return render->control.AudioDevDump((AudioHandle)render, range, fd);
883 }
884
HdiServiceRenderRegCallback(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)885 int32_t HdiServiceRenderRegCallback(const struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
886 {
887 if (client == NULL || data == NULL || reply == NULL) {
888 return AUDIO_HAL_ERR_INVALID_PARAM;
889 }
890 struct AudioRender *render = NULL;
891 int ret = AudioAdapterListCheckAndGetRender(&render, data);
892 if (ret < 0) {
893 return ret;
894 }
895 void *cookie;
896 RenderCallback pCallback;
897 uint64_t tempAddr = 0;
898 if (!HdfSbufReadUint64(data, &tempAddr)) {
899 HDF_LOGE("%{public}s: read cookie Is NULL", __func__);
900 return AUDIO_HAL_ERR_INTERNAL;
901 }
902 cookie = reinterpret_cast<void *>((uintptr_t)tempAddr);
903 if (!HdfSbufReadUint64(data, &tempAddr)) {
904 HDF_LOGE("%{public}s: read callback pointer Is NULL", __func__);
905 return AUDIO_HAL_ERR_INTERNAL;
906 }
907 pCallback = (RenderCallback)tempAddr;
908 return render->RegCallback(render, pCallback, cookie);
909 }
910
HdiServiceRenderDrainBuffer(const struct HdfDeviceIoClient * client,struct HdfSBuf * data,struct HdfSBuf * reply)911 int32_t HdiServiceRenderDrainBuffer(const struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply)
912 {
913 if (client == NULL || data == NULL || reply == NULL) {
914 return AUDIO_HAL_ERR_INVALID_PARAM;
915 }
916 struct AudioRender *render = NULL;
917 int ret = AudioAdapterListCheckAndGetRender(&render, data);
918 if (ret < 0) {
919 return ret;
920 }
921 AudioDrainNotifyType type;
922 uint32_t tempType = 0;
923 if (!HdfSbufReadUint32(data, &tempType)) {
924 return AUDIO_HAL_ERR_INTERNAL;
925 }
926 type = (AudioDrainNotifyType)tempType;
927 return render->DrainBuffer(render, &type);
928 }
929 }