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