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_adapter.h"
19094332d3Sopenharmony_ci#include "fast_audio_render.h"
20094332d3Sopenharmony_cinamespace OHOS::HDI::Audio_Bluetooth {
21094332d3Sopenharmony_ciconstexpr int CONFIG_CHANNEL_COUNT = 2; // two channels
22094332d3Sopenharmony_ciconstexpr float GAIN_MAX = 50.0;
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ciconstexpr int DEFAULT_RENDER_SAMPLING_RATE = 48000;
25094332d3Sopenharmony_ciconstexpr int DEEP_BUFFER_RENDER_PERIOD_SIZE = 4096;
26094332d3Sopenharmony_ciconstexpr int DEEP_BUFFER_RENDER_PERIOD_COUNT = 8;
27094332d3Sopenharmony_ciconstexpr const char *TYPE_RENDER = "Render";
28094332d3Sopenharmony_ciconstexpr const char *TYPE_CAPTURE = "Capture";
29094332d3Sopenharmony_ciconstexpr const int SHIFT_RIGHT_31_BITS = 31;
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cistatic void GetFastRenderFuncs(struct AudioHwRender *hwRender)
32094332d3Sopenharmony_ci{
33094332d3Sopenharmony_ci    hwRender->common.control.Start = FastRenderStart;
34094332d3Sopenharmony_ci    hwRender->common.control.Stop = FastRenderStop;
35094332d3Sopenharmony_ci    hwRender->common.control.Pause = FastRenderPause;
36094332d3Sopenharmony_ci    hwRender->common.control.Resume = FastRenderResume;
37094332d3Sopenharmony_ci    hwRender->common.control.Flush = FastRenderFlush;
38094332d3Sopenharmony_ci    hwRender->common.control.TurnStandbyMode = FastRenderTurnStandbyMode;
39094332d3Sopenharmony_ci    hwRender->common.control.AudioDevDump = FastRenderAudioDevDump;
40094332d3Sopenharmony_ci    hwRender->common.attr.GetFrameSize = FastRenderGetFrameSize;
41094332d3Sopenharmony_ci    hwRender->common.attr.GetFrameCount = FastRenderGetFrameCount;
42094332d3Sopenharmony_ci    hwRender->common.attr.SetSampleAttributes = FastRenderSetSampleAttributes;
43094332d3Sopenharmony_ci    hwRender->common.attr.GetSampleAttributes = FastRenderGetSampleAttributes;
44094332d3Sopenharmony_ci    hwRender->common.attr.GetCurrentChannelId = FastRenderGetCurrentChannelId;
45094332d3Sopenharmony_ci    hwRender->common.attr.SetExtraParams = FastRenderSetExtraParams;
46094332d3Sopenharmony_ci    hwRender->common.attr.GetExtraParams = FastRenderGetExtraParams;
47094332d3Sopenharmony_ci    hwRender->common.attr.ReqMmapBuffer = FastRenderReqMmapBuffer;
48094332d3Sopenharmony_ci    hwRender->common.attr.GetMmapPosition = FastRenderGetMmapPosition;
49094332d3Sopenharmony_ci    hwRender->common.scene.CheckSceneCapability = FastRenderCheckSceneCapability;
50094332d3Sopenharmony_ci    hwRender->common.scene.SelectScene = FastRenderSelectScene;
51094332d3Sopenharmony_ci    hwRender->common.volume.SetMute = FastRenderSetMute;
52094332d3Sopenharmony_ci    hwRender->common.volume.GetMute = FastRenderGetMute;
53094332d3Sopenharmony_ci    hwRender->common.volume.SetVolume = FastRenderSetVolume;
54094332d3Sopenharmony_ci    hwRender->common.volume.GetVolume = FastRenderGetVolume;
55094332d3Sopenharmony_ci    hwRender->common.volume.GetGainThreshold = FastRenderGetGainThreshold;
56094332d3Sopenharmony_ci    hwRender->common.volume.GetGain = FastRenderGetGain;
57094332d3Sopenharmony_ci    hwRender->common.volume.SetGain = FastRenderSetGain;
58094332d3Sopenharmony_ci    hwRender->common.GetLatency = FastRenderGetLatency;
59094332d3Sopenharmony_ci    hwRender->common.RenderFrame = FastRenderRenderFrame;
60094332d3Sopenharmony_ci    hwRender->common.GetRenderPosition = FastRenderGetRenderPosition;
61094332d3Sopenharmony_ci    hwRender->common.SetRenderSpeed = FastRenderSetRenderSpeed;
62094332d3Sopenharmony_ci    hwRender->common.GetRenderSpeed = FastRenderGetRenderSpeed;
63094332d3Sopenharmony_ci    hwRender->common.SetChannelMode = FastRenderSetChannelMode;
64094332d3Sopenharmony_ci    hwRender->common.GetChannelMode = FastRenderGetChannelMode;
65094332d3Sopenharmony_ci    hwRender->common.RegCallback = FastRenderRegCallback;
66094332d3Sopenharmony_ci    hwRender->common.DrainBuffer = FastRenderDrainBuffer;
67094332d3Sopenharmony_ci}
68094332d3Sopenharmony_ci
69094332d3Sopenharmony_cistatic void GetNormalRenderFuncs(struct AudioHwRender *hwRender)
70094332d3Sopenharmony_ci{
71094332d3Sopenharmony_ci    hwRender->common.control.Start = AudioRenderStart;
72094332d3Sopenharmony_ci    hwRender->common.control.Stop = AudioRenderStop;
73094332d3Sopenharmony_ci    hwRender->common.control.Pause = AudioRenderPause;
74094332d3Sopenharmony_ci    hwRender->common.control.Resume = AudioRenderResume;
75094332d3Sopenharmony_ci    hwRender->common.control.Flush = AudioRenderFlush;
76094332d3Sopenharmony_ci    hwRender->common.control.TurnStandbyMode = AudioRenderTurnStandbyMode;
77094332d3Sopenharmony_ci    hwRender->common.control.AudioDevDump = AudioRenderAudioDevDump;
78094332d3Sopenharmony_ci    hwRender->common.attr.GetFrameSize = AudioRenderGetFrameSize;
79094332d3Sopenharmony_ci    hwRender->common.attr.GetFrameCount = AudioRenderGetFrameCount;
80094332d3Sopenharmony_ci    hwRender->common.attr.SetSampleAttributes = AudioRenderSetSampleAttributes;
81094332d3Sopenharmony_ci    hwRender->common.attr.GetSampleAttributes = AudioRenderGetSampleAttributes;
82094332d3Sopenharmony_ci    hwRender->common.attr.GetCurrentChannelId = AudioRenderGetCurrentChannelId;
83094332d3Sopenharmony_ci    hwRender->common.attr.SetExtraParams = AudioRenderSetExtraParams;
84094332d3Sopenharmony_ci    hwRender->common.attr.GetExtraParams = AudioRenderGetExtraParams;
85094332d3Sopenharmony_ci    hwRender->common.attr.ReqMmapBuffer = AudioRenderReqMmapBuffer;
86094332d3Sopenharmony_ci    hwRender->common.attr.GetMmapPosition = AudioRenderGetMmapPosition;
87094332d3Sopenharmony_ci    hwRender->common.scene.CheckSceneCapability = AudioRenderCheckSceneCapability;
88094332d3Sopenharmony_ci    hwRender->common.scene.SelectScene = AudioRenderSelectScene;
89094332d3Sopenharmony_ci    hwRender->common.volume.SetMute = AudioRenderSetMute;
90094332d3Sopenharmony_ci    hwRender->common.volume.GetMute = AudioRenderGetMute;
91094332d3Sopenharmony_ci    hwRender->common.volume.SetVolume = AudioRenderSetVolume;
92094332d3Sopenharmony_ci    hwRender->common.volume.GetVolume = AudioRenderGetVolume;
93094332d3Sopenharmony_ci    hwRender->common.volume.GetGainThreshold = AudioRenderGetGainThreshold;
94094332d3Sopenharmony_ci    hwRender->common.volume.GetGain = AudioRenderGetGain;
95094332d3Sopenharmony_ci    hwRender->common.volume.SetGain = AudioRenderSetGain;
96094332d3Sopenharmony_ci    hwRender->common.GetLatency = AudioRenderGetLatency;
97094332d3Sopenharmony_ci    hwRender->common.RenderFrame = AudioRenderRenderFrame;
98094332d3Sopenharmony_ci    hwRender->common.GetRenderPosition = AudioRenderGetRenderPosition;
99094332d3Sopenharmony_ci    hwRender->common.SetRenderSpeed = AudioRenderSetRenderSpeed;
100094332d3Sopenharmony_ci    hwRender->common.GetRenderSpeed = AudioRenderGetRenderSpeed;
101094332d3Sopenharmony_ci    hwRender->common.SetChannelMode = AudioRenderSetChannelMode;
102094332d3Sopenharmony_ci    hwRender->common.GetChannelMode = AudioRenderGetChannelMode;
103094332d3Sopenharmony_ci    hwRender->common.RegCallback = AudioRenderRegCallback;
104094332d3Sopenharmony_ci    hwRender->common.DrainBuffer = AudioRenderDrainBuffer;
105094332d3Sopenharmony_ci}
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_ciint32_t GetAudioRenderFunc(struct AudioHwRender *hwRender, const char *adapterName)
108094332d3Sopenharmony_ci{
109094332d3Sopenharmony_ci    if (hwRender == nullptr || adapterName == nullptr) {
110094332d3Sopenharmony_ci        return HDF_FAILURE;
111094332d3Sopenharmony_ci    }
112094332d3Sopenharmony_ci    if (strcmp(adapterName, "bt_a2dp_fast") == 0) {
113094332d3Sopenharmony_ci        GetFastRenderFuncs(hwRender);
114094332d3Sopenharmony_ci    } else {
115094332d3Sopenharmony_ci        GetNormalRenderFuncs(hwRender);
116094332d3Sopenharmony_ci    }
117094332d3Sopenharmony_ci    return HDF_SUCCESS;
118094332d3Sopenharmony_ci}
119094332d3Sopenharmony_ci
120094332d3Sopenharmony_ciint32_t CheckParaDesc(const struct AudioDeviceDescriptor *desc, const char *type)
121094332d3Sopenharmony_ci{
122094332d3Sopenharmony_ci    if (desc == NULL || type == NULL) {
123094332d3Sopenharmony_ci        return HDF_FAILURE;
124094332d3Sopenharmony_ci    }
125094332d3Sopenharmony_ci    if ((desc->portId) >> SHIFT_RIGHT_31_BITS) {
126094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
127094332d3Sopenharmony_ci    }
128094332d3Sopenharmony_ci    AudioPortPin pins = desc->pins;
129094332d3Sopenharmony_ci    if (!strcmp(type, TYPE_CAPTURE)) {
130094332d3Sopenharmony_ci        if (pins == PIN_IN_MIC || pins == PIN_IN_HS_MIC || pins == PIN_IN_LINEIN) {
131094332d3Sopenharmony_ci            return HDF_SUCCESS;
132094332d3Sopenharmony_ci        } else {
133094332d3Sopenharmony_ci            return HDF_ERR_NOT_SUPPORT;
134094332d3Sopenharmony_ci        }
135094332d3Sopenharmony_ci    } else if (!strcmp(type, TYPE_RENDER)) {
136094332d3Sopenharmony_ci        if (pins == PIN_OUT_SPEAKER || pins == PIN_OUT_HEADSET || pins == PIN_OUT_LINEOUT || pins == PIN_OUT_HDMI) {
137094332d3Sopenharmony_ci            return HDF_SUCCESS;
138094332d3Sopenharmony_ci        } else {
139094332d3Sopenharmony_ci            return HDF_ERR_NOT_SUPPORT;
140094332d3Sopenharmony_ci        }
141094332d3Sopenharmony_ci    }
142094332d3Sopenharmony_ci    return HDF_ERR_NOT_SUPPORT;
143094332d3Sopenharmony_ci}
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_ciint32_t CheckParaAttr(const struct AudioSampleAttributes *attrs)
146094332d3Sopenharmony_ci{
147094332d3Sopenharmony_ci    if (attrs == NULL) {
148094332d3Sopenharmony_ci        return HDF_FAILURE;
149094332d3Sopenharmony_ci    }
150094332d3Sopenharmony_ci    int32_t ret = ((attrs->sampleRate) >> SHIFT_RIGHT_31_BITS) + ((attrs->channelCount) >> SHIFT_RIGHT_31_BITS) +
151094332d3Sopenharmony_ci        ((attrs->period) >> SHIFT_RIGHT_31_BITS) + ((attrs->frameSize) >> SHIFT_RIGHT_31_BITS) +
152094332d3Sopenharmony_ci        ((attrs->startThreshold) >> SHIFT_RIGHT_31_BITS) + ((attrs->stopThreshold) >> SHIFT_RIGHT_31_BITS) +
153094332d3Sopenharmony_ci        ((attrs->silenceThreshold) >> SHIFT_RIGHT_31_BITS);
154094332d3Sopenharmony_ci    if (ret > 0) {
155094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
156094332d3Sopenharmony_ci    }
157094332d3Sopenharmony_ci    AudioCategory audioCategory = attrs->type;
158094332d3Sopenharmony_ci    if (audioCategory < AUDIO_IN_MEDIA || audioCategory > AUDIO_MMAP_NOIRQ) {
159094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
160094332d3Sopenharmony_ci    }
161094332d3Sopenharmony_ci    AudioFormat audioFormat = attrs->format;
162094332d3Sopenharmony_ci    return CheckAttrFormat(audioFormat);
163094332d3Sopenharmony_ci}
164094332d3Sopenharmony_ci
165094332d3Sopenharmony_ciint32_t AttrFormatToBit(const struct AudioSampleAttributes *attrs, int32_t *format)
166094332d3Sopenharmony_ci{
167094332d3Sopenharmony_ci    if (attrs == NULL || format == NULL) {
168094332d3Sopenharmony_ci        return HDF_FAILURE;
169094332d3Sopenharmony_ci    }
170094332d3Sopenharmony_ci    AudioFormat audioFormat = attrs->format;
171094332d3Sopenharmony_ci    switch (audioFormat) {
172094332d3Sopenharmony_ci        case AUDIO_FORMAT_TYPE_PCM_8_BIT:
173094332d3Sopenharmony_ci            *format = BIT_NUM_8;
174094332d3Sopenharmony_ci            return HDF_SUCCESS;
175094332d3Sopenharmony_ci        case AUDIO_FORMAT_TYPE_PCM_16_BIT:
176094332d3Sopenharmony_ci            *format = BIT_NUM_16;
177094332d3Sopenharmony_ci            return HDF_SUCCESS;
178094332d3Sopenharmony_ci        case AUDIO_FORMAT_TYPE_PCM_24_BIT:
179094332d3Sopenharmony_ci            *format = BIT_NUM_24;
180094332d3Sopenharmony_ci            return HDF_SUCCESS;
181094332d3Sopenharmony_ci        case AUDIO_FORMAT_TYPE_PCM_32_BIT:
182094332d3Sopenharmony_ci            *format = BIT_NUM_32;
183094332d3Sopenharmony_ci            return HDF_SUCCESS;
184094332d3Sopenharmony_ci        default:
185094332d3Sopenharmony_ci            return HDF_ERR_NOT_SUPPORT;
186094332d3Sopenharmony_ci    }
187094332d3Sopenharmony_ci}
188094332d3Sopenharmony_ci
189094332d3Sopenharmony_ciint32_t InitHwRenderParam(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc,
190094332d3Sopenharmony_ci                          const struct AudioSampleAttributes *attrs)
191094332d3Sopenharmony_ci{
192094332d3Sopenharmony_ci    if (hwRender == NULL || desc == NULL || attrs == NULL) {
193094332d3Sopenharmony_ci        HDF_LOGE("InitHwRenderParam param Is NULL");
194094332d3Sopenharmony_ci        return HDF_FAILURE;
195094332d3Sopenharmony_ci    }
196094332d3Sopenharmony_ci    int32_t ret = CheckParaDesc(desc, TYPE_RENDER);
197094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
198094332d3Sopenharmony_ci        HDF_LOGE("CheckParaDesc Fail");
199094332d3Sopenharmony_ci        return ret;
200094332d3Sopenharmony_ci    }
201094332d3Sopenharmony_ci    ret = CheckParaAttr(attrs);
202094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
203094332d3Sopenharmony_ci        HDF_LOGE("CheckParaAttr Fail");
204094332d3Sopenharmony_ci        return ret;
205094332d3Sopenharmony_ci    }
206094332d3Sopenharmony_ci    int32_t formatValue = -1;
207094332d3Sopenharmony_ci    ret = AttrFormatToBit(attrs, &formatValue);
208094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
209094332d3Sopenharmony_ci        HDF_LOGE("AttrFormatToBit Fail");
210094332d3Sopenharmony_ci        return ret;
211094332d3Sopenharmony_ci    }
212094332d3Sopenharmony_ci    if (attrs->channelCount == 0) {
213094332d3Sopenharmony_ci        return HDF_FAILURE;
214094332d3Sopenharmony_ci    }
215094332d3Sopenharmony_ci    hwRender->renderParam.renderMode.hwInfo.deviceDescript = *desc;
216094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs = *attrs;
217094332d3Sopenharmony_ci    hwRender->renderParam.renderMode.ctlParam.audioGain.gainMax = GAIN_MAX;  // init gainMax
218094332d3Sopenharmony_ci    hwRender->renderParam.renderMode.ctlParam.audioGain.gainMin = 0;
219094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.frames = 0;
220094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.time.tvNSec = 0;
221094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.time.tvSec = 0;
222094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.byteRate = DEFAULT_RENDER_SAMPLING_RATE;
223094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.periodSize = DEEP_BUFFER_RENDER_PERIOD_SIZE;
224094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.periodCount = DEEP_BUFFER_RENDER_PERIOD_COUNT;
225094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs.period = attrs->period;
226094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs.frameSize = attrs->frameSize;
227094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs.startThreshold = attrs->startThreshold;
228094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs.stopThreshold = attrs->stopThreshold;
229094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs.silenceThreshold = attrs->silenceThreshold;
230094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs.isBigEndian = attrs->isBigEndian;
231094332d3Sopenharmony_ci    hwRender->renderParam.frameRenderMode.attrs.isSignedData = attrs->isSignedData;
232094332d3Sopenharmony_ci    return HDF_SUCCESS;
233094332d3Sopenharmony_ci}
234094332d3Sopenharmony_ci
235094332d3Sopenharmony_ciAudioFormat g_formatIdZero = AUDIO_FORMAT_TYPE_PCM_16_BIT;
236094332d3Sopenharmony_ciint32_t InitForGetPortCapability(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex)
237094332d3Sopenharmony_ci{
238094332d3Sopenharmony_ci    if (capabilityIndex == NULL) {
239094332d3Sopenharmony_ci        HDF_LOGE("capabilityIndex Is NULL");
240094332d3Sopenharmony_ci        return HDF_FAILURE;
241094332d3Sopenharmony_ci    }
242094332d3Sopenharmony_ci    /* get capabilityIndex from driver or default */
243094332d3Sopenharmony_ci    if (portIndex.dir != PORT_OUT) {
244094332d3Sopenharmony_ci        capabilityIndex->hardwareMode = true;
245094332d3Sopenharmony_ci        capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
246094332d3Sopenharmony_ci        capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
247094332d3Sopenharmony_ci        return HDF_SUCCESS;
248094332d3Sopenharmony_ci    }
249094332d3Sopenharmony_ci    if (portIndex.portId == 0) {
250094332d3Sopenharmony_ci        capabilityIndex->hardwareMode = true;
251094332d3Sopenharmony_ci        capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
252094332d3Sopenharmony_ci        capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
253094332d3Sopenharmony_ci        capabilityIndex->deviceType = portIndex.dir;
254094332d3Sopenharmony_ci        capabilityIndex->deviceId = PIN_OUT_SPEAKER;
255094332d3Sopenharmony_ci        capabilityIndex->formatNum = 1;
256094332d3Sopenharmony_ci        capabilityIndex->formats = &g_formatIdZero;
257094332d3Sopenharmony_ci        capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000;
258094332d3Sopenharmony_ci        capabilityIndex->subPortsNum = 1;
259094332d3Sopenharmony_ci        capabilityIndex->subPorts =
260094332d3Sopenharmony_ci            reinterpret_cast<struct AudioSubPortCapability *>(calloc(capabilityIndex->subPortsNum,
261094332d3Sopenharmony_ci            sizeof(struct AudioSubPortCapability)));
262094332d3Sopenharmony_ci        if (capabilityIndex->subPorts == NULL) {
263094332d3Sopenharmony_ci            HDF_LOGE("capabilityIndex subPorts is NULL!");
264094332d3Sopenharmony_ci            return HDF_FAILURE;
265094332d3Sopenharmony_ci        }
266094332d3Sopenharmony_ci        capabilityIndex->subPorts->portId = portIndex.portId;
267094332d3Sopenharmony_ci        capabilityIndex->subPorts->desc = portIndex.portName;
268094332d3Sopenharmony_ci        capabilityIndex->subPorts->mask = PORT_PASSTHROUGH_LPCM;
269094332d3Sopenharmony_ci        return HDF_SUCCESS;
270094332d3Sopenharmony_ci    }
271094332d3Sopenharmony_ci    if (portIndex.portId == 1) {
272094332d3Sopenharmony_ci        capabilityIndex->hardwareMode = true;
273094332d3Sopenharmony_ci        capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
274094332d3Sopenharmony_ci        capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
275094332d3Sopenharmony_ci        capabilityIndex->deviceType = portIndex.dir;
276094332d3Sopenharmony_ci        capabilityIndex->deviceId = PIN_OUT_HEADSET;
277094332d3Sopenharmony_ci        capabilityIndex->formatNum = 1;
278094332d3Sopenharmony_ci        capabilityIndex->formats = &g_formatIdZero;
279094332d3Sopenharmony_ci        capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000 | AUDIO_SAMPLE_RATE_MASK_8000;
280094332d3Sopenharmony_ci        return HDF_SUCCESS;
281094332d3Sopenharmony_ci    }
282094332d3Sopenharmony_ci    if (portIndex.portId == HDMI_PORT_ID) {
283094332d3Sopenharmony_ci        return HdmiPortInit(portIndex, capabilityIndex);
284094332d3Sopenharmony_ci    }
285094332d3Sopenharmony_ci    return HDF_FAILURE;
286094332d3Sopenharmony_ci}
287094332d3Sopenharmony_ci
288094332d3Sopenharmony_civoid AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability *portCapabilitys, int32_t num)
289094332d3Sopenharmony_ci{
290094332d3Sopenharmony_ci    int32_t i = 0;
291094332d3Sopenharmony_ci    if (portCapabilitys == NULL) {
292094332d3Sopenharmony_ci        return;
293094332d3Sopenharmony_ci    }
294094332d3Sopenharmony_ci    while (i < num) {
295094332d3Sopenharmony_ci        if (&portCapabilitys[i] == NULL) {
296094332d3Sopenharmony_ci            break;
297094332d3Sopenharmony_ci        }
298094332d3Sopenharmony_ci        AudioMemFree((void **)(&portCapabilitys[i].capability.subPorts));
299094332d3Sopenharmony_ci        i++;
300094332d3Sopenharmony_ci    }
301094332d3Sopenharmony_ci    return;
302094332d3Sopenharmony_ci}
303094332d3Sopenharmony_ci
304094332d3Sopenharmony_ciint32_t AudioAdapterInitAllPorts(struct AudioAdapter *adapter)
305094332d3Sopenharmony_ci{
306094332d3Sopenharmony_ci    struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
307094332d3Sopenharmony_ci    if (hwAdapter == NULL) {
308094332d3Sopenharmony_ci        HDF_LOGE("hwAdapter Is NULL");
309094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
310094332d3Sopenharmony_ci    }
311094332d3Sopenharmony_ci    if (hwAdapter->portCapabilitys != NULL) {
312094332d3Sopenharmony_ci        HDF_LOGE("portCapabilitys already Init!");
313094332d3Sopenharmony_ci        return AUDIO_HAL_SUCCESS;
314094332d3Sopenharmony_ci    }
315094332d3Sopenharmony_ci    uint32_t portNum = hwAdapter->adapterDescriptor.portNum;
316094332d3Sopenharmony_ci    struct AudioPort *ports = hwAdapter->adapterDescriptor.ports;
317094332d3Sopenharmony_ci    if (ports == NULL) {
318094332d3Sopenharmony_ci        HDF_LOGE("ports is NULL!");
319094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
320094332d3Sopenharmony_ci    }
321094332d3Sopenharmony_ci    if (portNum == 0) {
322094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
323094332d3Sopenharmony_ci    }
324094332d3Sopenharmony_ci    struct AudioPortAndCapability *portCapability =
325094332d3Sopenharmony_ci        reinterpret_cast<struct AudioPortAndCapability *>(calloc(portNum, sizeof(struct AudioPortAndCapability)));
326094332d3Sopenharmony_ci    if (portCapability == NULL) {
327094332d3Sopenharmony_ci        HDF_LOGE("portCapability is NULL!");
328094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
329094332d3Sopenharmony_ci    }
330094332d3Sopenharmony_ci    for (uint32_t i = 0; i < portNum; i++) {
331094332d3Sopenharmony_ci        portCapability[i].port = ports[i];
332094332d3Sopenharmony_ci        if (InitForGetPortCapability(ports[i], &portCapability[i].capability)) {
333094332d3Sopenharmony_ci            HDF_LOGE("ports Init Fail!");
334094332d3Sopenharmony_ci            AudioAdapterReleaseCapSubPorts(portCapability, portNum);
335094332d3Sopenharmony_ci            AudioMemFree((void **)&portCapability);
336094332d3Sopenharmony_ci            return AUDIO_HAL_ERR_INTERNAL;
337094332d3Sopenharmony_ci        }
338094332d3Sopenharmony_ci    }
339094332d3Sopenharmony_ci    hwAdapter->portCapabilitys = portCapability;
340094332d3Sopenharmony_ci    hwAdapter->portCapabilitys->mode = PORT_PASSTHROUGH_LPCM;
341094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
342094332d3Sopenharmony_ci}
343094332d3Sopenharmony_ci
344094332d3Sopenharmony_civoid AudioReleaseRenderHandle(struct AudioHwRender *hwRender)
345094332d3Sopenharmony_ci{
346094332d3Sopenharmony_ci    return;
347094332d3Sopenharmony_ci}
348094332d3Sopenharmony_ci
349094332d3Sopenharmony_ciint32_t AudioAdapterCreateRenderPre(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc,
350094332d3Sopenharmony_ci                                    const struct AudioSampleAttributes *attrs, const struct AudioHwAdapter *hwAdapter)
351094332d3Sopenharmony_ci{
352094332d3Sopenharmony_ci    HDF_LOGD("%s", __func__);
353094332d3Sopenharmony_ci    if (hwAdapter == NULL || hwRender == NULL || desc == NULL || attrs == NULL) {
354094332d3Sopenharmony_ci        HDF_LOGE("Pointer is null!");
355094332d3Sopenharmony_ci        return HDF_FAILURE;
356094332d3Sopenharmony_ci    }
357094332d3Sopenharmony_ci
358094332d3Sopenharmony_ci    /* Fill hwRender para */
359094332d3Sopenharmony_ci    if (InitHwRenderParam(hwRender, desc, attrs) < 0) {
360094332d3Sopenharmony_ci        return HDF_FAILURE;
361094332d3Sopenharmony_ci    }
362094332d3Sopenharmony_ci
363094332d3Sopenharmony_ci    if (GetAudioRenderFunc(hwRender, hwAdapter->adapterDescriptor.adapterName) < 0) {
364094332d3Sopenharmony_ci        return HDF_FAILURE;
365094332d3Sopenharmony_ci    }
366094332d3Sopenharmony_ci    /* Select Path */
367094332d3Sopenharmony_ci    if (hwAdapter->adapterDescriptor.adapterName == NULL) {
368094332d3Sopenharmony_ci        HDF_LOGE("pointer is null!");
369094332d3Sopenharmony_ci        return HDF_FAILURE;
370094332d3Sopenharmony_ci    }
371094332d3Sopenharmony_ci    uint32_t adapterNameLen = strlen(hwAdapter->adapterDescriptor.adapterName);
372094332d3Sopenharmony_ci    if (adapterNameLen == 0) {
373094332d3Sopenharmony_ci        HDF_LOGE("adapterNameLen is null!");
374094332d3Sopenharmony_ci        return HDF_FAILURE;
375094332d3Sopenharmony_ci    }
376094332d3Sopenharmony_ci    /* Get Adapter name */
377094332d3Sopenharmony_ci    int32_t ret = strncpy_s(hwRender->renderParam.renderMode.hwInfo.adapterName, NAME_LEN - 1,
378094332d3Sopenharmony_ci                            hwAdapter->adapterDescriptor.adapterName, adapterNameLen);
379094332d3Sopenharmony_ci    if (ret != EOK) {
380094332d3Sopenharmony_ci        HDF_LOGE("copy fail");
381094332d3Sopenharmony_ci        return HDF_FAILURE;
382094332d3Sopenharmony_ci    }
383094332d3Sopenharmony_ci    return HDF_SUCCESS;
384094332d3Sopenharmony_ci}
385094332d3Sopenharmony_ci
386094332d3Sopenharmony_ciint32_t AudioAdapterCreateRender(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc,
387094332d3Sopenharmony_ci                                 const struct AudioSampleAttributes *attrs, struct AudioRender **render)
388094332d3Sopenharmony_ci{
389094332d3Sopenharmony_ci    struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
390094332d3Sopenharmony_ci    if (hwAdapter == NULL || desc == NULL || attrs == NULL || render == NULL) {
391094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
392094332d3Sopenharmony_ci    }
393094332d3Sopenharmony_ci    if (hwAdapter->adapterMgrRenderFlag > 0) {
394094332d3Sopenharmony_ci        HDF_LOGE("Create render repeatedly!");
395094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
396094332d3Sopenharmony_ci    }
397094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(calloc(1, sizeof(*hwRender)));
398094332d3Sopenharmony_ci    if (hwRender == NULL) {
399094332d3Sopenharmony_ci        HDF_LOGE("hwRender is NULL!");
400094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_MALLOC_FAIL;
401094332d3Sopenharmony_ci    }
402094332d3Sopenharmony_ci    int32_t ret = AudioAdapterCreateRenderPre(hwRender, desc, attrs, hwAdapter);
403094332d3Sopenharmony_ci    if (ret != 0) {
404094332d3Sopenharmony_ci        HDF_LOGE("AudioAdapterCreateRenderPre fail");
405094332d3Sopenharmony_ci        AudioMemFree(reinterpret_cast<void **>(&hwRender));
406094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
407094332d3Sopenharmony_ci    }
408094332d3Sopenharmony_ci    hwAdapter->adapterMgrRenderFlag++;
409094332d3Sopenharmony_ci    *render = &hwRender->common;
410094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
411094332d3Sopenharmony_ci}
412094332d3Sopenharmony_ci
413094332d3Sopenharmony_ciint32_t AudioAdapterDestroyRender(struct AudioAdapter *adapter, struct AudioRender *render)
414094332d3Sopenharmony_ci{
415094332d3Sopenharmony_ci    HDF_LOGI("enter");
416094332d3Sopenharmony_ci    struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
417094332d3Sopenharmony_ci    if (hwAdapter == NULL || render == NULL) {
418094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
419094332d3Sopenharmony_ci    }
420094332d3Sopenharmony_ci    if (hwAdapter->adapterMgrRenderFlag > 0) {
421094332d3Sopenharmony_ci        hwAdapter->adapterMgrRenderFlag--;
422094332d3Sopenharmony_ci    }
423094332d3Sopenharmony_ci    struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(render);
424094332d3Sopenharmony_ci    if (hwRender == NULL) {
425094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
426094332d3Sopenharmony_ci    }
427094332d3Sopenharmony_ci    if (hwRender->renderParam.frameRenderMode.buffer != NULL) {
428094332d3Sopenharmony_ci        HDF_LOGI("render not stop, first stop it.");
429094332d3Sopenharmony_ci        int ret = render->control.Stop((AudioHandle)render);
430094332d3Sopenharmony_ci        if (ret < 0) {
431094332d3Sopenharmony_ci            HDF_LOGE("render Stop failed");
432094332d3Sopenharmony_ci        }
433094332d3Sopenharmony_ci    }
434094332d3Sopenharmony_ci    AudioReleaseRenderHandle(hwRender);
435094332d3Sopenharmony_ci    AudioMemFree(reinterpret_cast<void **>(&hwRender->renderParam.frameRenderMode.buffer));
436094332d3Sopenharmony_ci    AudioMemFree(reinterpret_cast<void **>(&render));
437094332d3Sopenharmony_ci    HDF_LOGI("AudioAdapterDestroyRender cleaned.");
438094332d3Sopenharmony_ci    return AUDIO_HAL_SUCCESS;
439094332d3Sopenharmony_ci}
440094332d3Sopenharmony_ci
441094332d3Sopenharmony_ciint32_t AudioAdapterGetPortCapability(struct AudioAdapter *adapter, const struct AudioPort *port,
442094332d3Sopenharmony_ci                                      struct AudioPortCapability *capability)
443094332d3Sopenharmony_ci{
444094332d3Sopenharmony_ci    struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
445094332d3Sopenharmony_ci    if (hwAdapter == NULL || port == NULL || port->portName == NULL || capability == NULL) {
446094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
447094332d3Sopenharmony_ci    }
448094332d3Sopenharmony_ci    if (port->portId < 0) {
449094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
450094332d3Sopenharmony_ci    }
451094332d3Sopenharmony_ci    struct AudioPortAndCapability *hwAdapterPortCapabilitys = hwAdapter->portCapabilitys;
452094332d3Sopenharmony_ci    if (hwAdapterPortCapabilitys == NULL) {
453094332d3Sopenharmony_ci        HDF_LOGE("hwAdapter portCapabilitys is NULL!");
454094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
455094332d3Sopenharmony_ci    }
456094332d3Sopenharmony_ci    int32_t portNum = hwAdapter->adapterDescriptor.portNum;
457094332d3Sopenharmony_ci    while (hwAdapterPortCapabilitys != NULL && portNum) {
458094332d3Sopenharmony_ci        if (hwAdapterPortCapabilitys->port.portId == port->portId) {
459094332d3Sopenharmony_ci            *capability = hwAdapterPortCapabilitys->capability;
460094332d3Sopenharmony_ci            return AUDIO_HAL_SUCCESS;
461094332d3Sopenharmony_ci        }
462094332d3Sopenharmony_ci        hwAdapterPortCapabilitys++;
463094332d3Sopenharmony_ci        portNum--;
464094332d3Sopenharmony_ci    }
465094332d3Sopenharmony_ci    return AUDIO_HAL_ERR_INTERNAL;
466094332d3Sopenharmony_ci}
467094332d3Sopenharmony_ci
468094332d3Sopenharmony_ciint32_t AudioAdapterSetPassthroughMode(struct AudioAdapter *adapter,
469094332d3Sopenharmony_ci    const struct AudioPort *port, AudioPortPassthroughMode mode)
470094332d3Sopenharmony_ci{
471094332d3Sopenharmony_ci    if (adapter == NULL || port == NULL || port->portName == NULL) {
472094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
473094332d3Sopenharmony_ci    }
474094332d3Sopenharmony_ci    if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
475094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
476094332d3Sopenharmony_ci    }
477094332d3Sopenharmony_ci    struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
478094332d3Sopenharmony_ci    if (hwAdapter->portCapabilitys == NULL) {
479094332d3Sopenharmony_ci        HDF_LOGE("The pointer is null!");
480094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
481094332d3Sopenharmony_ci    }
482094332d3Sopenharmony_ci    struct AudioPortAndCapability *portCapabilityTemp = hwAdapter->portCapabilitys;
483094332d3Sopenharmony_ci    struct AudioPortCapability *portCapability = NULL;
484094332d3Sopenharmony_ci    int32_t portNum = hwAdapter->adapterDescriptor.portNum;
485094332d3Sopenharmony_ci    while (portCapabilityTemp != NULL && portNum > 0) {
486094332d3Sopenharmony_ci        if (portCapabilityTemp->port.portId == port->portId) {
487094332d3Sopenharmony_ci            portCapability = &portCapabilityTemp->capability;
488094332d3Sopenharmony_ci            break;
489094332d3Sopenharmony_ci        }
490094332d3Sopenharmony_ci        portCapabilityTemp++;
491094332d3Sopenharmony_ci        portNum--;
492094332d3Sopenharmony_ci    }
493094332d3Sopenharmony_ci    if (portCapability == NULL || portNum <= 0) {
494094332d3Sopenharmony_ci        HDF_LOGE("hwAdapter portCapabilitys is Not Find!");
495094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
496094332d3Sopenharmony_ci    }
497094332d3Sopenharmony_ci    struct AudioSubPortCapability *subPortCapability = portCapability->subPorts;
498094332d3Sopenharmony_ci    if (subPortCapability == NULL) {
499094332d3Sopenharmony_ci        HDF_LOGE("portCapability->subPorts is NULL!");
500094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
501094332d3Sopenharmony_ci    }
502094332d3Sopenharmony_ci    int32_t subPortNum = portCapability->subPortsNum;
503094332d3Sopenharmony_ci    while (subPortCapability != NULL && subPortNum > 0) {
504094332d3Sopenharmony_ci        if (subPortCapability->mask == mode) {
505094332d3Sopenharmony_ci            portCapabilityTemp->mode = mode;
506094332d3Sopenharmony_ci            break;
507094332d3Sopenharmony_ci        }
508094332d3Sopenharmony_ci        subPortCapability++;
509094332d3Sopenharmony_ci        subPortNum--;
510094332d3Sopenharmony_ci    }
511094332d3Sopenharmony_ci    if (subPortNum > 0) {
512094332d3Sopenharmony_ci        return AUDIO_HAL_SUCCESS;
513094332d3Sopenharmony_ci    }
514094332d3Sopenharmony_ci    return AUDIO_HAL_ERR_INTERNAL;
515094332d3Sopenharmony_ci}
516094332d3Sopenharmony_ci
517094332d3Sopenharmony_ciint32_t AudioAdapterGetPassthroughMode(struct AudioAdapter *adapter, const struct AudioPort *port,
518094332d3Sopenharmony_ci                                       AudioPortPassthroughMode *mode)
519094332d3Sopenharmony_ci{
520094332d3Sopenharmony_ci    if (adapter == NULL || port == NULL || port->portName == NULL || mode == NULL) {
521094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INVALID_PARAM;
522094332d3Sopenharmony_ci    }
523094332d3Sopenharmony_ci    if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
524094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
525094332d3Sopenharmony_ci    }
526094332d3Sopenharmony_ci    struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
527094332d3Sopenharmony_ci    if (hwAdapter->portCapabilitys == NULL) {
528094332d3Sopenharmony_ci        HDF_LOGE("portCapabilitys pointer is null!");
529094332d3Sopenharmony_ci        return AUDIO_HAL_ERR_INTERNAL;
530094332d3Sopenharmony_ci    }
531094332d3Sopenharmony_ci    struct AudioPortAndCapability *portCapabilitys = hwAdapter->portCapabilitys;
532094332d3Sopenharmony_ci    int32_t portNum = hwAdapter->adapterDescriptor.portNum;
533094332d3Sopenharmony_ci    while (portCapabilitys != NULL && portNum > 0) {
534094332d3Sopenharmony_ci        if (portCapabilitys->port.portId == port->portId) {
535094332d3Sopenharmony_ci            *mode = portCapabilitys->mode;
536094332d3Sopenharmony_ci            return AUDIO_HAL_SUCCESS;
537094332d3Sopenharmony_ci        }
538094332d3Sopenharmony_ci        portCapabilitys++;
539094332d3Sopenharmony_ci        portNum--;
540094332d3Sopenharmony_ci    }
541094332d3Sopenharmony_ci    return AUDIO_HAL_ERR_INTERNAL;
542094332d3Sopenharmony_ci}
543094332d3Sopenharmony_ci
544094332d3Sopenharmony_ciint32_t AudioAdapterSetExtraParams(struct AudioAdapter *adapter, enum AudioExtParamKey key,
545094332d3Sopenharmony_ci                                   const char *condition, const char *value)
546094332d3Sopenharmony_ci{
547094332d3Sopenharmony_ci    (void)adapter;
548094332d3Sopenharmony_ci    (void)key;
549094332d3Sopenharmony_ci    (void)condition;
550094332d3Sopenharmony_ci    (void)value;
551094332d3Sopenharmony_ci    return HDF_ERR_NOT_SUPPORT;
552094332d3Sopenharmony_ci}
553094332d3Sopenharmony_ci
554094332d3Sopenharmony_ciint32_t AudioAdapterGetExtraParams(struct AudioAdapter *adapter, enum AudioExtParamKey key,
555094332d3Sopenharmony_ci                                   const char *condition, char *value, int32_t length)
556094332d3Sopenharmony_ci{
557094332d3Sopenharmony_ci    (void)adapter;
558094332d3Sopenharmony_ci    (void)key;
559094332d3Sopenharmony_ci    (void)condition;
560094332d3Sopenharmony_ci    (void)value;
561094332d3Sopenharmony_ci    (void)length;
562094332d3Sopenharmony_ci    return HDF_ERR_NOT_SUPPORT;
563094332d3Sopenharmony_ci}
564094332d3Sopenharmony_ci
565094332d3Sopenharmony_ci}