1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci#include <hdf_log.h>
16094332d3Sopenharmony_ci#include "audio_internal.h"
17094332d3Sopenharmony_ci#include "audio_adapter_info_common.h"
18094332d3Sopenharmony_ci#include "audio_bluetooth_manager.h"
19094332d3Sopenharmony_ci#include "audio_render.h"
20094332d3Sopenharmony_ci#include "hitrace_meter.h"
21094332d3Sopenharmony_cinamespace OHOS::HDI::Audio_Bluetooth {
22094332d3Sopenharmony_ci/* 1 buffer: 8000(8kHz sample rate) * 2(bytes, PCM_16_BIT) * 1(channel) */
23094332d3Sopenharmony_ci/* 1 frame: 1024(sample) * 2(bytes, PCM_16_BIT) * 1(channel) */
24094332d3Sopenharmony_ciconstexpr int FRAME_SIZE = 1024;
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_ciint32_t PcmBytesToFrames(const struct AudioFrameRenderMode *frameRenderMode, uint64_t bytes, uint32_t *frameCount)
27094332d3Sopenharmony_ci{
28094332d3Sopenharmony_ci    if (frameRenderMode == NULL || frameCount == NULL) {
29094332d3Sopenharmony_ci        return HDF_FAILURE;
30094332d3Sopenharmony_ci    }
31094332d3Sopenharmony_ci    uint32_t formatBits = 0;
32094332d3Sopenharmony_ci    int32_t ret = FormatToBits(frameRenderMode->attrs.format, &formatBits);
33094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
34094332d3Sopenharmony_ci        return ret;
35094332d3Sopenharmony_ci    }
36094332d3Sopenharmony_ci    uint32_t frameSize = frameRenderMode->attrs.channelCount * (formatBits >> 3); // Bit to byte >> 3
37094332d3Sopenharmony_ci    if (frameSize == 0) {
38094332d3Sopenharmony_ci        return HDF_FAILURE;
39094332d3Sopenharmony_ci    }
40094332d3Sopenharmony_ci    *frameCount = (uint32_t)bytes / frameSize;
41094332d3Sopenharmony_ci    return HDF_SUCCESS;
42094332d3Sopenharmony_ci}
43094332d3Sopenharmony_ci
44094332d3Sopenharmony_ciint32_t AudioRenderStart(AudioHandle handle)
45094332d3Sopenharmony_ci{
46094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
47094332d3Sopenharmony_ci    if (hwRender == NULL) {
48094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
49094332d3Sopenharmony_ci    }
50094332d3Sopenharmony_ci    if (hwRender->renderParam.frameRenderMode.buffer != NULL) {
51094332d3Sopenharmony_ci        HDF_LOGE("AudioRender already start!");
52094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_AO_BUSY; // render is busy now
53094332d3Sopenharmony_ci    }
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_ci    HDF_LOGI("%s, StartPlaying", __func__);
56094332d3Sopenharmony_ci#ifndef A2DP_HDI_SERVICE
57094332d3Sopenharmony_ci    if (OHOS::Bluetooth::GetPlayingState() == false) {
58094332d3Sopenharmony_ci        OHOS::Bluetooth::StartPlaying();
59094332d3Sopenharmony_ci    }
60094332d3Sopenharmony_ci#endif
61094332d3Sopenharmony_ci
62094332d3Sopenharmony_ci    char *buffer = static_cast<char *>(calloc(1, FRAME_DATA));
63094332d3Sopenharmony_ci    if (buffer == NULL) {
64094332d3Sopenharmony_ci        HDF_LOGE("Calloc Render buffer Fail!");
65094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_MALLOC_FAIL;
66094332d3Sopenharmony_ci    }
67094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.buffer = buffer;
68094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
69094332d3Sopenharmony_ci}
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ciint32_t AudioRenderStop(AudioHandle handle)
72094332d3Sopenharmony_ci{
73094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderStop");
74094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
75094332d3Sopenharmony_ci    if (hwRender == NULL) {
76094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
77094332d3Sopenharmony_ci    }
78094332d3Sopenharmony_ci    if (hwRender->renderParam.frameRenderMode.buffer != NULL) {
79094332d3Sopenharmony_ci        AudioMemFree(reinterpret_cast<void **>(&hwRender->renderParam.frameRenderMode.buffer));
80094332d3Sopenharmony_ci    } else {
81094332d3Sopenharmony_ci        HDF_LOGE("Repeat invalid stop operation!");
82094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_NOT_SUPPORT;
83094332d3Sopenharmony_ci    }
84094332d3Sopenharmony_ci
85094332d3Sopenharmony_ci    HDF_LOGI("%s, StopPlaying", __func__);
86094332d3Sopenharmony_ci#ifndef A2DP_HDI_SERVICE
87094332d3Sopenharmony_ci    if (OHOS::Bluetooth::GetPlayingState() == true) {
88094332d3Sopenharmony_ci        OHOS::Bluetooth::StopPlaying();
89094332d3Sopenharmony_ci    }
90094332d3Sopenharmony_ci#else
91094332d3Sopenharmony_ci    OHOS::Bluetooth::StopPlaying();
92094332d3Sopenharmony_ci#endif
93094332d3Sopenharmony_ci
94094332d3Sopenharmony_ci    hwRender->renderParam.renderMode.ctlParam.pause = false;
95094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
96094332d3Sopenharmony_ci}
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_ciint32_t AudioRenderPause(AudioHandle handle)
99094332d3Sopenharmony_ci{
100094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
101094332d3Sopenharmony_ci    if (hwRender == NULL) {
102094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
103094332d3Sopenharmony_ci    }
104094332d3Sopenharmony_ci    if (hwRender->renderParam.frameRenderMode.buffer == NULL) {
105094332d3Sopenharmony_ci        HDF_LOGE("AudioRender already stop!");
106094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
107094332d3Sopenharmony_ci    }
108094332d3Sopenharmony_ci    if (hwRender->renderParam.renderMode.ctlParam.pause) {
109094332d3Sopenharmony_ci        HDF_LOGE("Audio is already pause!");
110094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_NOT_SUPPORT;
111094332d3Sopenharmony_ci    }
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_ci    HDF_LOGI("%s, SuspendPlaying", __func__);
114094332d3Sopenharmony_ci#ifndef A2DP_HDI_SERVICE
115094332d3Sopenharmony_ci    if (OHOS::Bluetooth::GetPlayingState() == true) {
116094332d3Sopenharmony_ci        OHOS::Bluetooth::SuspendPlaying();
117094332d3Sopenharmony_ci    }
118094332d3Sopenharmony_ci#else
119094332d3Sopenharmony_ci    OHOS::Bluetooth::SuspendPlaying();
120094332d3Sopenharmony_ci#endif
121094332d3Sopenharmony_ci
122094332d3Sopenharmony_ci    hwRender->renderParam.renderMode.ctlParam.pause = true;
123094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
124094332d3Sopenharmony_ci}
125094332d3Sopenharmony_ci
126094332d3Sopenharmony_ciint32_t AudioRenderResume(AudioHandle handle)
127094332d3Sopenharmony_ci{
128094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderResume");
129094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
130094332d3Sopenharmony_ci    if (hwRender == NULL) {
131094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
132094332d3Sopenharmony_ci    }
133094332d3Sopenharmony_ci    if (!hwRender->renderParam.renderMode.ctlParam.pause) {
134094332d3Sopenharmony_ci        HDF_LOGE("Audio is already Resume !");
135094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_NOT_SUPPORT;
136094332d3Sopenharmony_ci    }
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ci    HDF_LOGI("%s, StartPlaying", __func__);
139094332d3Sopenharmony_ci#ifndef A2DP_HDI_SERVICE
140094332d3Sopenharmony_ci    if (OHOS::Bluetooth::GetPlayingState() == false) {
141094332d3Sopenharmony_ci        OHOS::Bluetooth::StartPlaying();
142094332d3Sopenharmony_ci    }
143094332d3Sopenharmony_ci#endif
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_ci    hwRender->renderParam.renderMode.ctlParam.pause = false;
146094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
147094332d3Sopenharmony_ci}
148094332d3Sopenharmony_ci
149094332d3Sopenharmony_ciint32_t AudioRenderFlush(AudioHandle handle)
150094332d3Sopenharmony_ci{
151094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderFlush");
152094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
153094332d3Sopenharmony_ci    if (hwRender == NULL) {
154094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
155094332d3Sopenharmony_ci    }
156094332d3Sopenharmony_ci    return AUDIO_HAL_ERR_NOT_SUPPORT;
157094332d3Sopenharmony_ci}
158094332d3Sopenharmony_ci
159094332d3Sopenharmony_ciint32_t AudioRenderGetFrameSize(AudioHandle handle, uint64_t *size)
160094332d3Sopenharmony_ci{
161094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
162094332d3Sopenharmony_ci    const int shift = 3;
163094332d3Sopenharmony_ci    if (hwRender == NULL || size == NULL) {
164094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
165094332d3Sopenharmony_ci    }
166094332d3Sopenharmony_ci    uint32_t channelCount = hwRender->renderParam.frameRenderMode.attrs.channelCount;
167094332d3Sopenharmony_ci    AudioFormat format = hwRender->renderParam.frameRenderMode.attrs.format;
168094332d3Sopenharmony_ci    uint32_t formatBits = 0;
169094332d3Sopenharmony_ci    int32_t ret = FormatToBits(format, &formatBits);
170094332d3Sopenharmony_ci    if (ret != AUDIO_HAL_SUCCESS) {
171094332d3Sopenharmony_ci        return ret;
172094332d3Sopenharmony_ci    }
173094332d3Sopenharmony_ci    *size = FRAME_SIZE * channelCount * (formatBits >> shift);
174094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
175094332d3Sopenharmony_ci}
176094332d3Sopenharmony_ci
177094332d3Sopenharmony_ciint32_t AudioRenderGetFrameCount(AudioHandle handle, uint64_t *count)
178094332d3Sopenharmony_ci{
179094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
180094332d3Sopenharmony_ci    if (hwRender == NULL || count == NULL) {
181094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
182094332d3Sopenharmony_ci    }
183094332d3Sopenharmony_ci    *count = hwRender->renderParam.frameRenderMode.frames;
184094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
185094332d3Sopenharmony_ci}
186094332d3Sopenharmony_ci
187094332d3Sopenharmony_ciint32_t AudioRenderSetSampleAttributes(AudioHandle handle, const struct AudioSampleAttributes *attrs)
188094332d3Sopenharmony_ci{
189094332d3Sopenharmony_ci    (void)attrs;
190094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
191094332d3Sopenharmony_ci}
192094332d3Sopenharmony_ci
193094332d3Sopenharmony_ciint32_t AudioRenderGetSampleAttributes(AudioHandle handle, struct AudioSampleAttributes *attrs)
194094332d3Sopenharmony_ci{
195094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
196094332d3Sopenharmony_ci    if (hwRender == NULL || attrs == NULL) {
197094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
198094332d3Sopenharmony_ci    }
199094332d3Sopenharmony_ci    attrs->format = hwRender->renderParam.frameRenderMode.attrs.format;
200094332d3Sopenharmony_ci    attrs->sampleRate = hwRender->renderParam.frameRenderMode.attrs.sampleRate;
201094332d3Sopenharmony_ci    attrs->channelCount = hwRender->renderParam.frameRenderMode.attrs.channelCount;
202094332d3Sopenharmony_ci    attrs->type = hwRender->renderParam.frameRenderMode.attrs.type;
203094332d3Sopenharmony_ci    attrs->interleaved = hwRender->renderParam.frameRenderMode.attrs.interleaved;
204094332d3Sopenharmony_ci    attrs->period = hwRender->renderParam.frameRenderMode.attrs.period;
205094332d3Sopenharmony_ci    attrs->frameSize = hwRender->renderParam.frameRenderMode.attrs.frameSize;
206094332d3Sopenharmony_ci    attrs->isBigEndian = hwRender->renderParam.frameRenderMode.attrs.isBigEndian;
207094332d3Sopenharmony_ci    attrs->isSignedData = hwRender->renderParam.frameRenderMode.attrs.isSignedData;
208094332d3Sopenharmony_ci    attrs->startThreshold = hwRender->renderParam.frameRenderMode.attrs.startThreshold;
209094332d3Sopenharmony_ci    attrs->stopThreshold = hwRender->renderParam.frameRenderMode.attrs.stopThreshold;
210094332d3Sopenharmony_ci    attrs->silenceThreshold = hwRender->renderParam.frameRenderMode.attrs.silenceThreshold;
211094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
212094332d3Sopenharmony_ci}
213094332d3Sopenharmony_ci
214094332d3Sopenharmony_ciint32_t AudioRenderGetCurrentChannelId(AudioHandle handle, uint32_t *channelId)
215094332d3Sopenharmony_ci{
216094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderGetCurrentChannelId");
217094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(handle);
218094332d3Sopenharmony_ci    if (hwRender == NULL || channelId == NULL) {
219094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
220094332d3Sopenharmony_ci    }
221094332d3Sopenharmony_ci    *channelId = hwRender->renderParam.frameRenderMode.attrs.channelCount;
222094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
223094332d3Sopenharmony_ci}
224094332d3Sopenharmony_ci
225094332d3Sopenharmony_ciint32_t AudioRenderCheckSceneCapability(AudioHandle handle, const struct AudioSceneDescriptor *scene,
226094332d3Sopenharmony_ci                                        bool *supported)
227094332d3Sopenharmony_ci{
228094332d3Sopenharmony_ci    (void)scene;
229094332d3Sopenharmony_ci    (void)supported;
230094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderCheckSceneCapability");
231094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
232094332d3Sopenharmony_ci}
233094332d3Sopenharmony_ci
234094332d3Sopenharmony_ciint32_t AudioRenderSelectScene(AudioHandle handle, const struct AudioSceneDescriptor *scene)
235094332d3Sopenharmony_ci{
236094332d3Sopenharmony_ci    (void)scene;
237094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
238094332d3Sopenharmony_ci}
239094332d3Sopenharmony_ci
240094332d3Sopenharmony_ciint32_t AudioRenderSetMute(AudioHandle handle, bool mute)
241094332d3Sopenharmony_ci{
242094332d3Sopenharmony_ci    (void)mute;
243094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
244094332d3Sopenharmony_ci}
245094332d3Sopenharmony_ci
246094332d3Sopenharmony_ciint32_t AudioRenderGetMute(AudioHandle handle, bool *mute)
247094332d3Sopenharmony_ci{
248094332d3Sopenharmony_ci    (void)mute;
249094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
250094332d3Sopenharmony_ci}
251094332d3Sopenharmony_ci
252094332d3Sopenharmony_ciint32_t AudioRenderSetVolume(AudioHandle handle, float volume)
253094332d3Sopenharmony_ci{
254094332d3Sopenharmony_ci    (void)volume;
255094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
256094332d3Sopenharmony_ci}
257094332d3Sopenharmony_ci
258094332d3Sopenharmony_ciint32_t AudioRenderGetVolume(AudioHandle handle, float *volume)
259094332d3Sopenharmony_ci{
260094332d3Sopenharmony_ci    (void)volume;
261094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
262094332d3Sopenharmony_ci}
263094332d3Sopenharmony_ci
264094332d3Sopenharmony_ciint32_t AudioRenderGetGainThreshold(AudioHandle handle, float *min, float *max)
265094332d3Sopenharmony_ci{
266094332d3Sopenharmony_ci    (void)min;
267094332d3Sopenharmony_ci    (void)max;
268094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderGetGainThreshold");
269094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
270094332d3Sopenharmony_ci}
271094332d3Sopenharmony_ci
272094332d3Sopenharmony_ciint32_t AudioRenderGetGain(AudioHandle handle, float *gain)
273094332d3Sopenharmony_ci{
274094332d3Sopenharmony_ci    (void)gain;
275094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderGetGain");
276094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
277094332d3Sopenharmony_ci}
278094332d3Sopenharmony_ci
279094332d3Sopenharmony_ciint32_t AudioRenderSetGain(AudioHandle handle, float gain)
280094332d3Sopenharmony_ci{
281094332d3Sopenharmony_ci    (void)gain;
282094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderSetGain");
283094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
284094332d3Sopenharmony_ci}
285094332d3Sopenharmony_ci
286094332d3Sopenharmony_ciint32_t AudioRenderGetLatency(struct AudioRender *render, uint32_t *ms)
287094332d3Sopenharmony_ci{
288094332d3Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_BLUETOOTH, "BtAudioRenderGetLatency");
289094332d3Sopenharmony_ci    struct AudioHwRender *impl = reinterpret_cast<struct AudioHwRender *>(render);
290094332d3Sopenharmony_ci    if (impl == NULL || ms == NULL) {
291094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
292094332d3Sopenharmony_ci    }
293094332d3Sopenharmony_ci    uint32_t byteRate = impl->renderParam.frameRenderMode.byteRate;
294094332d3Sopenharmony_ci    uint32_t periodSize = impl->renderParam.frameRenderMode.periodSize;
295094332d3Sopenharmony_ci    uint32_t periodCount = impl->renderParam.frameRenderMode.periodCount;
296094332d3Sopenharmony_ci    if (byteRate == 0) {
297094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
298094332d3Sopenharmony_ci    }
299094332d3Sopenharmony_ci    uint32_t periodMs = (periodCount * periodSize * 1000) / byteRate;
300094332d3Sopenharmony_ci    *ms = periodMs;
301094332d3Sopenharmony_ci#ifdef A2DP_HDI_SERVICE
302094332d3Sopenharmony_ci    uint32_t latency = 0;
303094332d3Sopenharmony_ci    OHOS::Bluetooth::GetLatency(latency);
304094332d3Sopenharmony_ci    *ms = latency;
305094332d3Sopenharmony_ci#endif
306094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
307094332d3Sopenharmony_ci}
308094332d3Sopenharmony_ci
309094332d3Sopenharmony_ciint32_t AudioRenderRenderFramSplit(struct AudioHwRender *hwRender)
310094332d3Sopenharmony_ci{
311094332d3Sopenharmony_ci    return HDF_SUCCESS;
312094332d3Sopenharmony_ci}
313094332d3Sopenharmony_ci
314094332d3Sopenharmony_ciint32_t AudioRenderRenderFrame(struct AudioRender *render, const void *frame,
315094332d3Sopenharmony_ci                               uint64_t requestBytes, uint64_t *replyBytes)
316094332d3Sopenharmony_ci{
317094332d3Sopenharmony_ci    HITRACE_METER_FMT(HITRACE_TAG_BLUETOOTH, "renderFrame:%d", requestBytes);
318094332d3Sopenharmony_ci    HDF_LOGD("AudioRenderRenderFrame");
319094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(render);
320094332d3Sopenharmony_ci    if (hwRender == NULL || frame == NULL || replyBytes == NULL ||
321094332d3Sopenharmony_ci        hwRender->renderParam.frameRenderMode.buffer == NULL) {
322094332d3Sopenharmony_ci        HDF_LOGE("Render Frame Paras is NULL!");
323094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
324094332d3Sopenharmony_ci    }
325094332d3Sopenharmony_ci    if (FRAME_DATA < requestBytes) {
326094332d3Sopenharmony_ci        HDF_LOGE("Out of FRAME_DATA size!");
327094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
328094332d3Sopenharmony_ci    }
329094332d3Sopenharmony_ci    int32_t ret = memcpy_s(hwRender->renderParam.frameRenderMode.buffer, FRAME_DATA, frame, (uint32_t)requestBytes);
330094332d3Sopenharmony_ci    if (ret != EOK) {
331094332d3Sopenharmony_ci        HDF_LOGE("memcpy_s fail");
332094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
333094332d3Sopenharmony_ci    }
334094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.bufferSize = requestBytes;
335094332d3Sopenharmony_ci    uint32_t frameCount = 0;
336094332d3Sopenharmony_ci    ret = PcmBytesToFrames(&hwRender->renderParam.frameRenderMode, requestBytes, &frameCount);
337094332d3Sopenharmony_ci    if (ret != AUDIO_HAL_SUCCESS) {
338094332d3Sopenharmony_ci        return ret;
339094332d3Sopenharmony_ci    }
340094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.bufferFrameSize = (uint64_t)frameCount;
341094332d3Sopenharmony_ci    if (AudioRenderRenderFramSplit(hwRender) < 0) {
342094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
343094332d3Sopenharmony_ci    }
344094332d3Sopenharmony_ci    *replyBytes = requestBytes;
345094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.frames += hwRender->renderParam.frameRenderMode.bufferFrameSize;
346094332d3Sopenharmony_ci    if (hwRender->renderParam.frameRenderMode.attrs.sampleRate == 0) {
347094332d3Sopenharmony_ci        HDF_LOGE("Divisor cannot be zero!");
348094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
349094332d3Sopenharmony_ci    }
350094332d3Sopenharmony_ci    if (TimeToAudioTimeStamp(hwRender->renderParam.frameRenderMode.bufferFrameSize,
351094332d3Sopenharmony_ci        &hwRender->renderParam.frameRenderMode.time,
352094332d3Sopenharmony_ci        hwRender->renderParam.frameRenderMode.attrs.sampleRate) == HDF_FAILURE) {
353094332d3Sopenharmony_ci        HDF_LOGE("Frame is NULL");
354094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
355094332d3Sopenharmony_ci    }
356094332d3Sopenharmony_ci
357094332d3Sopenharmony_ci    LOGV("%s, WriteFrame", __func__);
358094332d3Sopenharmony_ci    const uint8_t *data = reinterpret_cast<const uint8_t *>(frame);
359094332d3Sopenharmony_ci    AudioSampleAttributes *attrs = &hwRender->renderParam.frameRenderMode.attrs;
360094332d3Sopenharmony_ci    return OHOS::Bluetooth::WriteFrame(data, static_cast<uint32_t>(requestBytes), attrs);
361094332d3Sopenharmony_ci}
362094332d3Sopenharmony_ci
363094332d3Sopenharmony_ciint32_t AudioRenderGetRenderPosition(struct AudioRender *render, uint64_t *frames, struct AudioTimeStamp *time)
364094332d3Sopenharmony_ci{
365094332d3Sopenharmony_ci    struct AudioHwRender *impl = reinterpret_cast<struct AudioHwRender *>(render);
366094332d3Sopenharmony_ci    if (impl == NULL || frames == NULL || time == NULL) {
367094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
368094332d3Sopenharmony_ci    }
369094332d3Sopenharmony_ci    *frames = impl->renderParam.frameRenderMode.frames;
370094332d3Sopenharmony_ci    *time = impl->renderParam.frameRenderMode.time;
371094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
372094332d3Sopenharmony_ci}
373094332d3Sopenharmony_ci
374094332d3Sopenharmony_ciint32_t AudioRenderSetRenderSpeed(struct AudioRender *render, float speed)
375094332d3Sopenharmony_ci{
376094332d3Sopenharmony_ci    (void)speed;
377094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderSetRenderSpeed");
378094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(render);
379094332d3Sopenharmony_ci    if (hwRender == NULL) {
380094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
381094332d3Sopenharmony_ci    }
382094332d3Sopenharmony_ci    return AUDIO_HAL_ERR_NOT_SUPPORT;
383094332d3Sopenharmony_ci}
384094332d3Sopenharmony_ci
385094332d3Sopenharmony_ciint32_t AudioRenderGetRenderSpeed(struct AudioRender *render, float *speed)
386094332d3Sopenharmony_ci{
387094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderGetRenderSpeed");
388094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(render);
389094332d3Sopenharmony_ci    if (hwRender == NULL || speed == NULL) {
390094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
391094332d3Sopenharmony_ci    }
392094332d3Sopenharmony_ci    return AUDIO_HAL_ERR_NOT_SUPPORT;
393094332d3Sopenharmony_ci}
394094332d3Sopenharmony_ci
395094332d3Sopenharmony_ciint32_t AudioRenderSetChannelMode(struct AudioRender *render, AudioChannelMode mode)
396094332d3Sopenharmony_ci{
397094332d3Sopenharmony_ci    (void)render;
398094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderSetChannelMode");
399094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
400094332d3Sopenharmony_ci}
401094332d3Sopenharmony_ci
402094332d3Sopenharmony_ciint32_t AudioRenderGetChannelMode(struct AudioRender *render, AudioChannelMode *mode)
403094332d3Sopenharmony_ci{
404094332d3Sopenharmony_ci    (void)render;
405094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderGetChannelMode");
406094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
407094332d3Sopenharmony_ci}
408094332d3Sopenharmony_ci
409094332d3Sopenharmony_ciint32_t SetValue(struct ExtraParams mExtraParams, struct AudioHwRender *render)
410094332d3Sopenharmony_ci{
411094332d3Sopenharmony_ci    if (render == NULL) {
412094332d3Sopenharmony_ci        return HDF_FAILURE;
413094332d3Sopenharmony_ci    }
414094332d3Sopenharmony_ci    if (mExtraParams.route != -1) {
415094332d3Sopenharmony_ci        render->renderParam.renderMode.hwInfo.pathroute = (PathRoute)mExtraParams.route;
416094332d3Sopenharmony_ci    }
417094332d3Sopenharmony_ci    if (mExtraParams.format != -1) {
418094332d3Sopenharmony_ci        render->renderParam.frameRenderMode.attrs.format = (AudioFormat)mExtraParams.format;
419094332d3Sopenharmony_ci    }
420094332d3Sopenharmony_ci    if (mExtraParams.channels != 0) {
421094332d3Sopenharmony_ci        render->renderParam.frameRenderMode.attrs.channelCount = mExtraParams.channels;
422094332d3Sopenharmony_ci    }
423094332d3Sopenharmony_ci    if (mExtraParams.flag) {
424094332d3Sopenharmony_ci        render->renderParam.frameRenderMode.frames = mExtraParams.frames;
425094332d3Sopenharmony_ci    }
426094332d3Sopenharmony_ci    if (mExtraParams.sampleRate != 0) {
427094332d3Sopenharmony_ci        render->renderParam.frameRenderMode.attrs.sampleRate = mExtraParams.sampleRate;
428094332d3Sopenharmony_ci    }
429094332d3Sopenharmony_ci#ifdef A2DP_HDI_SERVICE
430094332d3Sopenharmony_ci    if (mExtraParams.audioStreamCtl == 1) {
431094332d3Sopenharmony_ci        HDF_LOGI("SetValue, try to suspendPlaying");
432094332d3Sopenharmony_ci        OHOS::Bluetooth::SuspendPlaying();
433094332d3Sopenharmony_ci    }
434094332d3Sopenharmony_ci#endif
435094332d3Sopenharmony_ci    return HDF_SUCCESS;
436094332d3Sopenharmony_ci}
437094332d3Sopenharmony_ci
438094332d3Sopenharmony_ciint32_t AudioRenderSetExtraParams(AudioHandle handle, const char *keyValueList)
439094332d3Sopenharmony_ci{
440094332d3Sopenharmony_ci    struct AudioHwRender *render = reinterpret_cast<struct AudioHwRender *>(handle);
441094332d3Sopenharmony_ci    if (render == NULL || keyValueList == NULL) {
442094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
443094332d3Sopenharmony_ci    }
444094332d3Sopenharmony_ci    int32_t count = 0;
445094332d3Sopenharmony_ci    int32_t sumOk = 0;
446094332d3Sopenharmony_ci    struct ExtraParams mExtraParams;
447094332d3Sopenharmony_ci    if (AudioSetExtraParams(keyValueList, &count, &mExtraParams, &sumOk) < 0) {
448094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
449094332d3Sopenharmony_ci    }
450094332d3Sopenharmony_ci    if (count != 0 && sumOk == count) {
451094332d3Sopenharmony_ci        SetValue(mExtraParams, render);
452094332d3Sopenharmony_ci        return AUDIO_HAL_SUCCESS;
453094332d3Sopenharmony_ci    } else {
454094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
455094332d3Sopenharmony_ci    }
456094332d3Sopenharmony_ci}
457094332d3Sopenharmony_ci
458094332d3Sopenharmony_ciint32_t AudioRenderGetExtraParams(AudioHandle handle, char *keyValueList, int32_t listLenth)
459094332d3Sopenharmony_ci{
460094332d3Sopenharmony_ci    struct AudioHwRender *render = reinterpret_cast<struct AudioHwRender *>(handle);
461094332d3Sopenharmony_ci    if (render == NULL || keyValueList == NULL || listLenth <= 0) {
462094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
463094332d3Sopenharmony_ci    }
464094332d3Sopenharmony_ci    int32_t bufferSize = strlen(ROUTE_SAMPLE) + strlen(FORMAT_SAMPLE) + strlen(CHANNELS_SAMPLE)
465094332d3Sopenharmony_ci                    + strlen(FRAME_COUNT_SAMPLE) + strlen(SAMPLING_RATE_SAMPLE) + 1;
466094332d3Sopenharmony_ci    if (listLenth < bufferSize) {
467094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
468094332d3Sopenharmony_ci    }
469094332d3Sopenharmony_ci    int32_t ret = AddElementToList(keyValueList, listLenth, AUDIO_ATTR_PARAM_ROUTE,
470094332d3Sopenharmony_ci        &render->renderParam.renderMode.hwInfo.pathroute);
471094332d3Sopenharmony_ci    if (ret < 0) {
472094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
473094332d3Sopenharmony_ci    }
474094332d3Sopenharmony_ci    ret = AddElementToList(keyValueList, listLenth, AUDIO_ATTR_PARAM_FORMAT,
475094332d3Sopenharmony_ci        &render->renderParam.frameRenderMode.attrs.format);
476094332d3Sopenharmony_ci    if (ret < 0) {
477094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
478094332d3Sopenharmony_ci    }
479094332d3Sopenharmony_ci    ret = AddElementToList(keyValueList, listLenth, AUDIO_ATTR_PARAM_CHANNELS,
480094332d3Sopenharmony_ci        &render->renderParam.frameRenderMode.attrs.channelCount);
481094332d3Sopenharmony_ci    if (ret < 0) {
482094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
483094332d3Sopenharmony_ci    }
484094332d3Sopenharmony_ci    ret = AddElementToList(keyValueList, listLenth, AUDIO_ATTR_PARAM_FRAME_COUNT,
485094332d3Sopenharmony_ci        &render->renderParam.frameRenderMode.frames);
486094332d3Sopenharmony_ci    if (ret < 0) {
487094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
488094332d3Sopenharmony_ci    }
489094332d3Sopenharmony_ci    ret = AddElementToList(keyValueList, listLenth, AUDIO_ATTR_PARAM_SAMPLING_RATE,
490094332d3Sopenharmony_ci        &render->renderParam.frameRenderMode.attrs.sampleRate);
491094332d3Sopenharmony_ci    if (ret < 0) {
492094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
493094332d3Sopenharmony_ci    }
494094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
495094332d3Sopenharmony_ci}
496094332d3Sopenharmony_ci
497094332d3Sopenharmony_ciint32_t AudioRenderReqMmapBuffer(AudioHandle handle, int32_t reqSize, struct AudioMmapBufferDescriptor *desc)
498094332d3Sopenharmony_ci{
499094332d3Sopenharmony_ci    (void)desc;
500094332d3Sopenharmony_ci    HDF_LOGI("AudioRenderReqMmapBuffer Success!");
501094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
502094332d3Sopenharmony_ci}
503094332d3Sopenharmony_ci
504094332d3Sopenharmony_ciint32_t AudioRenderGetMmapPosition(AudioHandle handle, uint64_t *frames, struct AudioTimeStamp *time)
505094332d3Sopenharmony_ci{
506094332d3Sopenharmony_ci    struct AudioHwRender *render = reinterpret_cast<struct AudioHwRender *>(handle);
507094332d3Sopenharmony_ci    if (render == NULL || frames == NULL || time == NULL) {
508094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
509094332d3Sopenharmony_ci    }
510094332d3Sopenharmony_ci    *frames = render->renderParam.frameRenderMode.frames;
511094332d3Sopenharmony_ci    render->renderParam.frameRenderMode.time.tvSec = (int64_t)render->renderParam.frameRenderMode.frames /
512094332d3Sopenharmony_ci                                       (int64_t)render->renderParam.frameRenderMode.attrs.sampleRate;
513094332d3Sopenharmony_ci    int64_t lastBufFrames = render->renderParam.frameRenderMode.frames %
514094332d3Sopenharmony_ci                        ((int64_t)render->renderParam.frameRenderMode.attrs.sampleRate);
515094332d3Sopenharmony_ci    render->renderParam.frameRenderMode.time.tvNSec =
516094332d3Sopenharmony_ci        (lastBufFrames * SEC_TO_NSEC) / ((int64_t)render->renderParam.frameRenderMode.attrs.sampleRate);
517094332d3Sopenharmony_ci    *time = render->renderParam.frameRenderMode.time;
518094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
519094332d3Sopenharmony_ci}
520094332d3Sopenharmony_ci
521094332d3Sopenharmony_ciint32_t AudioRenderTurnStandbyMode(AudioHandle handle)
522094332d3Sopenharmony_ci{
523094332d3Sopenharmony_ci    struct AudioHwRender *render = reinterpret_cast<struct AudioHwRender *>(handle);
524094332d3Sopenharmony_ci    if (render == NULL) {
525094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
526094332d3Sopenharmony_ci    }
527094332d3Sopenharmony_ci    int32_t ret = AudioRenderStop((AudioHandle)render);
528094332d3Sopenharmony_ci    if (ret < 0) {
529094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
530094332d3Sopenharmony_ci    }
531094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
532094332d3Sopenharmony_ci}
533094332d3Sopenharmony_ci
534094332d3Sopenharmony_ciint32_t AudioRenderAudioDevDump(AudioHandle handle, int32_t range, int32_t fd)
535094332d3Sopenharmony_ci{
536094332d3Sopenharmony_ci    struct AudioHwRender *render = reinterpret_cast<struct AudioHwRender *>(handle);
537094332d3Sopenharmony_ci    if (render == NULL) {
538094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
539094332d3Sopenharmony_ci    }
540094332d3Sopenharmony_ci    dprintf(fd, "%s%d\n", "Number of errors: ", render->errorLog.totalErrors);
541094332d3Sopenharmony_ci    if (range < RANGE_MIN - 1 || range > RANGE_MAX) {
542094332d3Sopenharmony_ci        dprintf(fd, "%s\n", "Out of range, invalid output");
543094332d3Sopenharmony_ci        return AUDIO_HAL_SUCCESS;
544094332d3Sopenharmony_ci    }
545094332d3Sopenharmony_ci    uint32_t mSize = render->errorLog.iter;
546094332d3Sopenharmony_ci    if (range < RANGE_MIN) {
547094332d3Sopenharmony_ci        dprintf(fd, "%-5s  %-10s  %s\n", "count", "errorCode", "Time");
548094332d3Sopenharmony_ci        for (uint32_t i = 0; i < mSize; i++) {
549094332d3Sopenharmony_ci            dprintf(fd, FORMAT_TWO, render->errorLog.errorDump[i].count + 1,
550094332d3Sopenharmony_ci                    render->errorLog.errorDump[i].errorCode,
551094332d3Sopenharmony_ci                    render->errorLog.errorDump[i].currentTime);
552094332d3Sopenharmony_ci        }
553094332d3Sopenharmony_ci    } else {
554094332d3Sopenharmony_ci        dprintf(fd, "%-5s  %-10s  %-20s  %-15s  %s\n", "count", "errorCode", "frames", "fail reason", "Time");
555094332d3Sopenharmony_ci        for (uint32_t i = 0; i < mSize; i++) {
556094332d3Sopenharmony_ci            dprintf(fd, FORMAT_ONE, render->errorLog.errorDump[i].count + 1,
557094332d3Sopenharmony_ci                    render->errorLog.errorDump[i].errorCode,
558094332d3Sopenharmony_ci                    render->errorLog.errorDump[i].frames,
559094332d3Sopenharmony_ci                    render->errorLog.errorDump[i].reason,
560094332d3Sopenharmony_ci                    render->errorLog.errorDump[i].currentTime);
561094332d3Sopenharmony_ci        }
562094332d3Sopenharmony_ci    }
563094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
564094332d3Sopenharmony_ci}
565094332d3Sopenharmony_ciint32_t CallbackProcessing(AudioHandle handle, AudioCallbackType callBackType)
566094332d3Sopenharmony_ci{
567094332d3Sopenharmony_ci    struct AudioHwRender *render = reinterpret_cast<struct AudioHwRender *>(handle);
568094332d3Sopenharmony_ci    if (render == NULL) {
569094332d3Sopenharmony_ci        HDF_LOGI("Unregistered callback.\n");
570094332d3Sopenharmony_ci        return HDF_FAILURE;
571094332d3Sopenharmony_ci    }
572094332d3Sopenharmony_ci    if (render->renderParam.frameRenderMode.callback == NULL) {
573094332d3Sopenharmony_ci        return HDF_FAILURE;
574094332d3Sopenharmony_ci    }
575094332d3Sopenharmony_ci    bool isCallBack = true;
576094332d3Sopenharmony_ci    switch (callBackType) {
577094332d3Sopenharmony_ci        case AUDIO_NONBLOCK_WRITE_COMPLETED:
578094332d3Sopenharmony_ci        case AUDIO_DRAIN_COMPLETED:
579094332d3Sopenharmony_ci        case AUDIO_FLUSH_COMPLETED:
580094332d3Sopenharmony_ci        case AUDIO_RENDER_FULL:
581094332d3Sopenharmony_ci        case AUDIO_ERROR_OCCUR:
582094332d3Sopenharmony_ci            isCallBack = true;
583094332d3Sopenharmony_ci            break;
584094332d3Sopenharmony_ci        default:
585094332d3Sopenharmony_ci            isCallBack = false;
586094332d3Sopenharmony_ci            break;
587094332d3Sopenharmony_ci    }
588094332d3Sopenharmony_ci    if (!isCallBack) {
589094332d3Sopenharmony_ci        HDF_LOGI("No callback processing is required.\n");
590094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
591094332d3Sopenharmony_ci    }
592094332d3Sopenharmony_ci    render->renderParam.frameRenderMode.callback(callBackType, NULL, render->renderParam.frameRenderMode.cookie);
593094332d3Sopenharmony_ci    return HDF_SUCCESS;
594094332d3Sopenharmony_ci}
595094332d3Sopenharmony_ci
596094332d3Sopenharmony_ciint32_t AudioRenderRegCallback(struct AudioRender *render, RenderCallback callback, void *cookie)
597094332d3Sopenharmony_ci{
598094332d3Sopenharmony_ci    struct AudioHwRender *pRender = reinterpret_cast<struct AudioHwRender *>(render);
599094332d3Sopenharmony_ci    if (pRender == NULL) {
600094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
601094332d3Sopenharmony_ci    }
602094332d3Sopenharmony_ci    pRender->renderParam.frameRenderMode.callback = callback;
603094332d3Sopenharmony_ci    pRender->renderParam.frameRenderMode.cookie = cookie;
604094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
605094332d3Sopenharmony_ci}
606094332d3Sopenharmony_ci
607094332d3Sopenharmony_ciint32_t AudioRenderDrainBuffer(struct AudioRender *render, AudioDrainNotifyType *type)
608094332d3Sopenharmony_ci{
609094332d3Sopenharmony_ci    struct AudioHwRender *pRender = reinterpret_cast<struct AudioHwRender *>(render);
610094332d3Sopenharmony_ci    if (pRender == NULL || type == NULL) {
611094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
612094332d3Sopenharmony_ci    }
613094332d3Sopenharmony_ci    return AUDIO_HAL_ERR_NOT_SUPPORT;
614094332d3Sopenharmony_ci}
615094332d3Sopenharmony_ci}