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