• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <hdf_log.h>
16 #include <hdf_remote_service.h>
17 #include "audio_proxy_common.h"
18 #include "audio_proxy_internal.h"
19 #include "audio_adapter_info_common.h"
20 
21 namespace OHOS::HDI::Audio_Bluetooth {
AudioProxyCommonInitAttrs(struct HdfSBuf * data,const struct AudioSampleAttributes * attrs)22 int32_t AudioProxyCommonInitAttrs(struct HdfSBuf *data, const struct AudioSampleAttributes *attrs)
23 {
24     if (data == NULL || attrs == NULL) {
25         HDF_LOGE("data == NULL || attrs == NULL");
26         return HDF_FAILURE;
27     }
28     uint32_t tempAtrr = (uint32_t)attrs->interleaved;
29     if (!HdfSbufWriteUint32(data, tempAtrr)) {
30         return HDF_FAILURE;
31     }
32     tempAtrr = (uint32_t)attrs->type;
33     if (!HdfSbufWriteUint32(data, tempAtrr)) {
34         return HDF_FAILURE;
35     }
36     if (!HdfSbufWriteUint32(data, attrs->period)) {
37         return HDF_FAILURE;
38     }
39     if (!HdfSbufWriteUint32(data, attrs->frameSize)) {
40         return HDF_FAILURE;
41     }
42     if (!HdfSbufWriteUint32(data, attrs->startThreshold)) {
43         HDF_LOGE("startThreshold Write Fail");
44         return HDF_FAILURE;
45     }
46     if (!HdfSbufWriteUint32(data, attrs->stopThreshold)) {
47         HDF_LOGE("stopThreshold Write Fail");
48         return HDF_FAILURE;
49     }
50     if (!HdfSbufWriteUint32(data, attrs->silenceThreshold)) {
51         return HDF_FAILURE;
52     }
53     tempAtrr = (uint32_t)attrs->isBigEndian;
54     if (!HdfSbufWriteUint32(data, tempAtrr)) {
55         return HDF_FAILURE;
56     }
57     tempAtrr = (uint32_t)attrs->isSignedData;
58     if (!HdfSbufWriteUint32(data, tempAtrr)) {
59         return HDF_FAILURE;
60     }
61     return HDF_SUCCESS;
62 }
63 
AudioProxyCommonInitCreateData(struct HdfSBuf * data,const struct AudioHwAdapter * adapter,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs)64 int32_t AudioProxyCommonInitCreateData(struct HdfSBuf *data, const struct AudioHwAdapter *adapter,
65     const struct AudioDeviceDescriptor *desc, const struct AudioSampleAttributes *attrs)
66 {
67     HDF_LOGI("%{public}s, ", __func__);
68     if (data == NULL || adapter == NULL || desc == NULL || attrs == NULL) {
69         HDF_LOGE("data == NULL || adapter == NULL || desc == NULL || attrs == NULL");
70         return HDF_FAILURE;
71     }
72     int32_t pid = getpid();
73     const char *adapterName = adapter->adapterDescriptor.adapterName;
74     if (adapterName == NULL) {
75         return HDF_FAILURE;
76     }
77     if (!HdfSbufWriteString(data, adapterName)) {
78         return HDF_FAILURE;
79     }
80     if (!HdfSbufWriteInt32(data, pid)) {
81         return HDF_FAILURE;
82     }
83     uint32_t tempAtrr = (uint32_t)attrs->format;
84     if (!HdfSbufWriteUint32(data, tempAtrr)) {
85         return HDF_FAILURE;
86     }
87     if (!HdfSbufWriteUint32(data, attrs->channelCount)) {
88         return HDF_FAILURE;
89     }
90     if (!HdfSbufWriteUint32(data, attrs->sampleRate)) {
91         return HDF_FAILURE;
92     }
93     if (AudioProxyCommonInitAttrs(data, attrs) < 0) {
94         return HDF_FAILURE;
95     }
96     if (!HdfSbufWriteUint32(data, desc->portId)) {
97         return HDF_FAILURE;
98     }
99     uint32_t tempDesc = (uint32_t)desc->pins;
100     if (!HdfSbufWriteUint32(data, tempDesc)) {
101         return HDF_FAILURE;
102     }
103     return HDF_SUCCESS;
104 }
105 
GetAudioProxyRenderFunc(struct AudioHwRender * hwRender)106 int32_t GetAudioProxyRenderFunc(struct AudioHwRender *hwRender)
107 {
108     if (hwRender == NULL) {
109         return HDF_FAILURE;
110     }
111     hwRender->common.control.Start = AudioProxyRenderStart;
112     hwRender->common.control.Stop = AudioProxyRenderStop;
113     hwRender->common.control.Pause = AudioProxyRenderPause;
114     hwRender->common.control.Resume = AudioProxyRenderResume;
115     hwRender->common.control.Flush = AudioProxyRenderFlush;
116     hwRender->common.control.TurnStandbyMode = AudioProxyRenderTurnStandbyMode;
117     hwRender->common.control.AudioDevDump = AudioProxyRenderAudioDevDump;
118     hwRender->common.attr.GetFrameSize = AudioProxyRenderGetFrameSize;
119     hwRender->common.attr.GetFrameCount = AudioProxyRenderGetFrameCount;
120     hwRender->common.attr.SetSampleAttributes = AudioProxyRenderSetSampleAttributes;
121     hwRender->common.attr.GetSampleAttributes = AudioProxyRenderGetSampleAttributes;
122     hwRender->common.attr.GetCurrentChannelId = AudioProxyRenderGetCurrentChannelId;
123     hwRender->common.attr.SetExtraParams = AudioProxyRenderSetExtraParams;
124     hwRender->common.attr.GetExtraParams = AudioProxyRenderGetExtraParams;
125     hwRender->common.attr.ReqMmapBuffer = AudioProxyRenderReqMmapBuffer;
126     hwRender->common.attr.GetMmapPosition = AudioProxyRenderGetMmapPosition;
127     hwRender->common.scene.CheckSceneCapability = AudioProxyRenderCheckSceneCapability;
128     hwRender->common.scene.SelectScene = AudioProxyRenderSelectScene;
129     hwRender->common.volume.SetMute = AudioProxyRenderSetMute;
130     hwRender->common.volume.GetMute = AudioProxyRenderGetMute;
131     hwRender->common.volume.SetVolume = AudioProxyRenderSetVolume;
132     hwRender->common.volume.GetVolume = AudioProxyRenderGetVolume;
133     hwRender->common.volume.GetGainThreshold = AudioProxyRenderGetGainThreshold;
134     hwRender->common.volume.GetGain = AudioProxyRenderGetGain;
135     hwRender->common.volume.SetGain = AudioProxyRenderSetGain;
136     hwRender->common.GetLatency = AudioProxyRenderGetLatency;
137     hwRender->common.RenderFrame = AudioProxyRenderRenderFrame;
138     hwRender->common.GetRenderPosition = AudioProxyRenderGetRenderPosition;
139     hwRender->common.SetRenderSpeed = AudioProxyRenderSetRenderSpeed;
140     hwRender->common.GetRenderSpeed = AudioProxyRenderGetRenderSpeed;
141     hwRender->common.SetChannelMode = AudioProxyRenderSetChannelMode;
142     hwRender->common.GetChannelMode = AudioProxyRenderGetChannelMode;
143     hwRender->common.RegCallback = AudioProxyRenderRegCallback;
144     hwRender->common.DrainBuffer = AudioProxyRenderDrainBuffer;
145     return HDF_SUCCESS;
146 }
147 
InitHwRenderParam(struct AudioHwRender * hwRender,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs)148 int32_t InitHwRenderParam(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc,
149                           const struct AudioSampleAttributes *attrs)
150 {
151     if (hwRender == NULL || desc == NULL || attrs == NULL) {
152         HDF_LOGE("InitHwRenderParam param Is NULL");
153         return HDF_FAILURE;
154     }
155     hwRender->renderParam.renderMode.hwInfo.deviceDescript = *desc;
156     hwRender->renderParam.frameRenderMode.attrs = *attrs;
157     return HDF_SUCCESS;
158 }
159 
160 AudioFormat g_formatIdZero = AUDIO_FORMAT_PCM_16_BIT;
InitForGetPortCapability(struct AudioPort portIndex,struct AudioPortCapability * capabilityIndex)161 int32_t InitForGetPortCapability(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex)
162 {
163     if (capabilityIndex == NULL) {
164         HDF_LOGE("capabilityIndex Is NULL");
165         return HDF_FAILURE;
166     }
167     /* get capabilityIndex from driver or default */
168     if (portIndex.dir != PORT_OUT) {
169         capabilityIndex->hardwareMode = true;
170         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
171         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
172         return HDF_SUCCESS;
173     }
174     if (portIndex.portId == 0) {
175         capabilityIndex->hardwareMode = true;
176         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
177         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
178         capabilityIndex->deviceType = portIndex.dir;
179         capabilityIndex->deviceId = PIN_OUT_SPEAKER;
180         capabilityIndex->formatNum = 1;
181         capabilityIndex->formats = &g_formatIdZero;
182         capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000;
183         capabilityIndex->subPortsNum = 1;
184         capabilityIndex->subPorts = (struct AudioSubPortCapability *)calloc(capabilityIndex->subPortsNum,
185             sizeof(struct AudioSubPortCapability));
186         if (capabilityIndex->subPorts == NULL) {
187             HDF_LOGE("pointer is null!");
188             return HDF_FAILURE;
189         }
190         capabilityIndex->subPorts->portId = portIndex.portId;
191         capabilityIndex->subPorts->desc = portIndex.portName;
192         capabilityIndex->subPorts->mask = PORT_PASSTHROUGH_LPCM;
193         return HDF_SUCCESS;
194     }
195     if (portIndex.portId == 1) {
196         capabilityIndex->hardwareMode = true;
197         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
198         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
199         capabilityIndex->deviceType = portIndex.dir;
200         capabilityIndex->deviceId = PIN_OUT_HEADSET;
201         capabilityIndex->formatNum = 1;
202         capabilityIndex->formats = &g_formatIdZero;
203         capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000 | AUDIO_SAMPLE_RATE_MASK_8000;
204         return HDF_SUCCESS;
205     }
206     if (portIndex.portId == HDMI_PORT_ID) {
207         return HdmiPortInit(portIndex, capabilityIndex);
208     }
209     return HDF_FAILURE;
210 }
211 
AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability * portCapabilitys,const int32_t num)212 void AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability *portCapabilitys, const int32_t num)
213 {
214     int32_t i = 0;
215     if (portCapabilitys == NULL) {
216         return;
217     }
218     while (i < num) {
219         if (&portCapabilitys[i] == NULL) {
220             break;
221         }
222         AudioMemFree((void **)(&portCapabilitys[i].capability.subPorts));
223         i++;
224     }
225     return;
226 }
227 
AudioProxyAdapterInitAllPorts(struct AudioAdapter * adapter)228 int32_t AudioProxyAdapterInitAllPorts(struct AudioAdapter *adapter)
229 {
230     struct HdfSBuf *data = NULL;
231     struct HdfSBuf *reply = NULL;
232     const char *adapterName = NULL;
233     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
234     if (hwAdapter == NULL || hwAdapter->adapterDescriptor.adapterName == NULL ||
235         hwAdapter->proxyRemoteHandle == NULL) {
236         HDF_LOGE("hwAdapter Is NULL");
237         return AUDIO_HAL_ERR_INVALID_PARAM;
238     }
239     /* Fake data */
240     uint32_t portNum = hwAdapter->adapterDescriptor.portNum;
241     struct AudioPort *ports = hwAdapter->adapterDescriptor.ports;
242     if (ports == NULL || portNum == 0) {
243         HDF_LOGE("ports is NULL!");
244         return AUDIO_HAL_ERR_INTERNAL;
245     }
246     struct AudioPortAndCapability *portCapability = (struct AudioPortAndCapability *)calloc(portNum,
247         sizeof(struct AudioPortAndCapability));
248     if (portCapability == NULL) {
249         HDF_LOGE("portCapability is NULL!");
250         return AUDIO_HAL_ERR_MALLOC_FAIL;
251     }
252     for (uint32_t i = 0; i < portNum; i++) {
253         portCapability[i].port = ports[i];
254         if (InitForGetPortCapability(ports[i], &portCapability[i].capability)) {
255             HDF_LOGE("ports Init Invalid!");
256             AudioAdapterReleaseCapSubPorts(portCapability, portNum);
257             AudioMemFree((void **)&portCapability);
258             return AUDIO_HAL_ERR_INTERNAL;
259         }
260     }
261     hwAdapter->portCapabilitys = portCapability;
262     hwAdapter->portCapabilitys->mode = PORT_PASSTHROUGH_LPCM;
263     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
264         return AUDIO_HAL_ERR_INTERNAL;
265     }
266     adapterName = hwAdapter->adapterDescriptor.adapterName;
267     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
268         AudioProxyBufReplyRecycle(data, reply);
269         return AUDIO_HAL_ERR_INTERNAL;
270     }
271     if (!HdfSbufWriteString(data, adapterName)) {
272         AudioProxyBufReplyRecycle(data, reply);
273         return AUDIO_HAL_ERR_INTERNAL;
274     }
275     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_INIT_PORTS, data, reply);
276     if (ret < 0) {
277         HDF_LOGE("Get Failed AudioAdapter!");
278         AudioProxyBufReplyRecycle(data, reply);
279         return ret;
280     }
281     AudioProxyBufReplyRecycle(data, reply);
282     return AUDIO_HAL_SUCCESS;
283 }
284 
AudioProxyAdapterCreateRenderSplit(const struct AudioHwAdapter * hwAdapter,struct AudioHwRender * hwRender)285 int32_t AudioProxyAdapterCreateRenderSplit(const struct AudioHwAdapter *hwAdapter, struct AudioHwRender *hwRender)
286 {
287     if (hwAdapter == NULL || hwRender == NULL) {
288         return HDF_FAILURE;
289     }
290     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
291         return HDF_FAILURE;
292     }
293     uint32_t adapterNameLen = strlen(hwAdapter->adapterDescriptor.adapterName);
294     /* Get Adapter name */
295     int32_t ret = strncpy_s(hwRender->renderParam.renderMode.hwInfo.adapterName, NAME_LEN - 1,
296         hwAdapter->adapterDescriptor.adapterName, adapterNameLen);
297     if (ret != EOK) {
298         return HDF_FAILURE;
299     }
300     return HDF_SUCCESS;
301 }
302 
AudioProxyAdapterCreateRender(struct AudioAdapter * adapter,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs,struct AudioRender ** render)303 int32_t AudioProxyAdapterCreateRender(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc,
304                                       const struct AudioSampleAttributes *attrs, struct AudioRender **render)
305 {
306     HDF_LOGI("%{public}s, ", __func__);
307     struct HdfSBuf *data = NULL;
308     struct HdfSBuf *reply = NULL;
309     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
310     if (hwAdapter == NULL || hwAdapter->proxyRemoteHandle == NULL || desc == NULL || attrs == NULL || render == NULL) {
311         return AUDIO_HAL_ERR_INVALID_PARAM;
312     }
313     struct AudioHwRender *hwRender = (struct AudioHwRender *)calloc(1, sizeof(*hwRender));
314     if (hwRender == NULL) {
315         HDF_LOGE("hwRender is NULL!");
316         return AUDIO_HAL_ERR_MALLOC_FAIL;
317     }
318     hwRender->proxyRemoteHandle = hwAdapter->proxyRemoteHandle;
319     if (GetAudioProxyRenderFunc(hwRender) < 0) {
320         AudioMemFree((void **)&hwRender);
321         return AUDIO_HAL_ERR_INTERNAL;
322     }
323     /* Fill hwRender para */
324     if (InitHwRenderParam(hwRender, desc, attrs) < 0) {
325         AudioMemFree((void **)&hwRender);
326         return AUDIO_HAL_ERR_INTERNAL;
327     }
328     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
329         AudioMemFree((void **)&hwRender);
330         return AUDIO_HAL_ERR_INTERNAL;
331     }
332     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
333         AudioProxyBufReplyRecycle(data, reply);
334         return AUDIO_HAL_ERR_INTERNAL;
335     }
336     if (AudioProxyCommonInitCreateData(data, hwAdapter, desc, attrs) < 0) {
337         HDF_LOGE("Failed to obtain reply");
338         AudioProxyBufReplyRecycle(data, reply);
339         AudioMemFree((void **)&hwRender);
340         return AUDIO_HAL_ERR_INTERNAL;
341     }
342     int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, AUDIO_HDI_RENDER_CREATE_RENDER, data, reply);
343     if (ret < 0) {
344         HDF_LOGE("Send Server fail!");
345         AudioProxyBufReplyRecycle(data, reply);
346         AudioMemFree((void **)&hwRender);
347         return ret;
348     }
349     AudioProxyBufReplyRecycle(data, reply);
350     if (AudioProxyAdapterCreateRenderSplit(hwAdapter, hwRender) < 0) {
351         AudioMemFree((void **)&hwRender);
352         return AUDIO_HAL_ERR_INTERNAL;
353     }
354     *render = &hwRender->common;
355     return AUDIO_HAL_SUCCESS;
356 }
357 
AudioProxyAdapterDestroyRender(struct AudioAdapter * adapter,struct AudioRender * render)358 int32_t AudioProxyAdapterDestroyRender(struct AudioAdapter *adapter, struct AudioRender *render)
359 {
360     struct HdfSBuf *data = NULL;
361     struct HdfSBuf *reply = NULL;
362     if (adapter == NULL || render == NULL) {
363         return AUDIO_HAL_ERR_INVALID_PARAM;
364     }
365     struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(render);
366     if (hwRender == NULL || hwRender->proxyRemoteHandle == NULL) {
367         return AUDIO_HAL_ERR_INVALID_PARAM;
368     }
369     if (AudioProxyPreprocessRender((AudioHwRender *)render, &data, &reply) < 0) {
370         return AUDIO_HAL_ERR_INTERNAL;
371     }
372     int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, AUDIO_HDI_RENDER_DESTROY, data, reply);
373     if (ret < 0) {
374         if (ret != HDF_ERR_INVALID_OBJECT) {
375             HDF_LOGE("AudioRenderRenderFrame FAIL");
376         }
377         AudioProxyBufReplyRecycle(data, reply);
378         return ret;
379     }
380     AudioMemFree((void **)&hwRender->renderParam.frameRenderMode.buffer);
381     AudioMemFree((void **)&render);
382     AudioProxyBufReplyRecycle(data, reply);
383     return AUDIO_HAL_SUCCESS;
384 }
385 
AudioProxyAdapterWritePortCapability(const struct AudioHwAdapter * hwAdapter,const struct AudioPort * port,struct HdfSBuf * data)386 int32_t AudioProxyAdapterWritePortCapability(const struct AudioHwAdapter *hwAdapter,
387     const struct AudioPort *port, struct HdfSBuf *data)
388 {
389     if (hwAdapter == NULL || port == NULL || data == NULL) {
390         return HDF_FAILURE;
391     }
392     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
393         return HDF_FAILURE;
394     }
395     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
396     if (!HdfSbufWriteString(data, adapterName)) {
397         return HDF_FAILURE;
398     }
399     uint32_t tempDir = (uint32_t)port->dir;
400     if (!HdfSbufWriteUint32(data, tempDir)) {
401         return HDF_FAILURE;
402     }
403     if (!HdfSbufWriteUint32(data, port->portId)) {
404         return HDF_FAILURE;
405     }
406     if (port->portName == NULL) {
407         return HDF_FAILURE;
408     }
409     if (!HdfSbufWriteString(data, port->portName)) {
410         return HDF_FAILURE;
411     }
412     return HDF_SUCCESS;
413 }
414 
AudioProxyAdapterGetPortCapability(struct AudioAdapter * adapter,const struct AudioPort * port,struct AudioPortCapability * capability)415 int32_t AudioProxyAdapterGetPortCapability(struct AudioAdapter *adapter,
416     const struct AudioPort *port, struct AudioPortCapability *capability)
417 {
418     HDF_LOGI("%{public}s, ", __func__);
419     if (adapter == NULL || port == NULL || port->portName == NULL || capability == NULL) {
420         return AUDIO_HAL_ERR_INVALID_PARAM;
421     }
422     if (port->portId < 0) {
423         return AUDIO_HAL_ERR_INTERNAL;
424     }
425     struct HdfSBuf *data = NULL;
426     struct HdfSBuf *reply = NULL;
427     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
428         return AUDIO_HAL_ERR_INTERNAL;
429     }
430     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
431     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
432         AudioProxyBufReplyRecycle(data, reply);
433         return AUDIO_HAL_ERR_INTERNAL;
434     }
435     if (hwAdapter == NULL || hwAdapter->proxyRemoteHandle == NULL) {
436         AudioProxyBufReplyRecycle(data, reply);
437         return AUDIO_HAL_ERR_INTERNAL;
438     }
439     if (AudioProxyAdapterWritePortCapability(hwAdapter, port, data)) {
440         AudioProxyBufReplyRecycle(data, reply);
441         return AUDIO_HAL_ERR_INTERNAL;
442     }
443     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_GET_PORT_CAPABILITY, data, reply);
444     if (ret < 0) {
445         AudioProxyBufReplyRecycle(data, reply);
446         return ret;
447     }
448     AudioProxyBufReplyRecycle(data, reply);
449     /* proxy must init local capability ,this capability the same of Server's */
450     struct AudioPortAndCapability *hwAdapterPortCapabilitys = hwAdapter->portCapabilitys;
451     if (hwAdapterPortCapabilitys == NULL) {
452         HDF_LOGE("hwAdapter portCapabilitys is NULL!");
453         return AUDIO_HAL_ERR_INTERNAL;
454     }
455     int32_t portNum = hwAdapter->adapterDescriptor.portNum;
456     while (hwAdapterPortCapabilitys != NULL && (portNum > 0)) {
457         if (hwAdapterPortCapabilitys->port.portId == port->portId) {
458             *capability = hwAdapterPortCapabilitys->capability;
459             return AUDIO_HAL_SUCCESS;
460         }
461         hwAdapterPortCapabilitys++;
462         portNum--;
463     }
464     return AUDIO_HAL_ERR_INTERNAL;
465 }
466 
AudioProxyAdapterSetAndGetPassthroughModeSBuf(struct HdfSBuf * data,const struct HdfSBuf * reply,const struct AudioPort * port)467 int32_t AudioProxyAdapterSetAndGetPassthroughModeSBuf(struct HdfSBuf *data,
468     const struct HdfSBuf *reply, const struct AudioPort *port)
469 {
470     if (data == NULL || port == NULL || port->portName == NULL) {
471         return HDF_FAILURE;
472     }
473     uint32_t tempDir = port->dir;
474     if (!HdfSbufWriteUint32(data, tempDir)) {
475         return HDF_FAILURE;
476     }
477     if (!HdfSbufWriteUint32(data, port->portId)) {
478         return HDF_FAILURE;
479     }
480     if (!HdfSbufWriteString(data, port->portName)) {
481         HDF_LOGE("HdfSbufWriteString error");
482         return HDF_FAILURE;
483     }
484     return HDF_SUCCESS;
485 }
486 
AudioProxyAdapterSetPassthroughMode(struct AudioAdapter * adapter,const struct AudioPort * port,AudioPortPassthroughMode mode)487 int32_t AudioProxyAdapterSetPassthroughMode(struct AudioAdapter *adapter,
488     const struct AudioPort *port, AudioPortPassthroughMode mode)
489 {
490     HDF_LOGI("%{public}s, ", __func__);
491     struct HdfSBuf *data = NULL;
492     struct HdfSBuf *reply = NULL;
493     if (adapter == NULL || port == NULL || port->portName == NULL) {
494         return AUDIO_HAL_ERR_INVALID_PARAM;
495     }
496     if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
497         return AUDIO_HAL_ERR_INTERNAL;
498     }
499     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
500         HDF_LOGE("AudioProxyPreprocessSBuf Fail");
501         return AUDIO_HAL_ERR_INTERNAL;
502     }
503     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
504     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
505         AudioProxyBufReplyRecycle(data, reply);
506         return AUDIO_HAL_ERR_INTERNAL;
507     }
508     if (hwAdapter == NULL || hwAdapter->proxyRemoteHandle == NULL ||
509         hwAdapter->adapterDescriptor.adapterName == NULL) {
510         AudioProxyBufReplyRecycle(data, reply);
511         return AUDIO_HAL_ERR_INTERNAL;
512     }
513     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
514     if (!HdfSbufWriteString(data, adapterName)) {
515         HDF_LOGE("adapterName Write Fail");
516         AudioProxyBufReplyRecycle(data, reply);
517         return AUDIO_HAL_ERR_INTERNAL;
518     }
519     if (AudioProxyAdapterSetAndGetPassthroughModeSBuf(data, reply, port) < 0) {
520         HDF_LOGE("Failed to obtain data");
521         AudioProxyBufReplyRecycle(data, reply);
522         return AUDIO_HAL_ERR_INTERNAL;
523     }
524     uint32_t tempMode = (uint32_t)mode;
525     if (!HdfSbufWriteUint32(data, tempMode)) {
526         HDF_LOGE("Mode Write Fail");
527         AudioProxyBufReplyRecycle(data, reply);
528         return AUDIO_HAL_ERR_INTERNAL;
529     }
530     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_SET_PASS_MODE, data, reply);
531     if (ret < 0) {
532         AudioProxyBufReplyRecycle(data, reply);
533         HDF_LOGE("Failed to send server");
534         return ret;
535     }
536     AudioProxyBufReplyRecycle(data, reply);
537     return AUDIO_HAL_SUCCESS;
538 }
539 
AudioProxyAdapterGetPassthroughMode(struct AudioAdapter * adapter,const struct AudioPort * port,AudioPortPassthroughMode * mode)540 int32_t AudioProxyAdapterGetPassthroughMode(struct AudioAdapter *adapter,
541     const struct AudioPort *port, AudioPortPassthroughMode *mode)
542 {
543     struct HdfSBuf *data = NULL;
544     struct HdfSBuf *reply = NULL;
545     if (adapter == NULL || port == NULL || port->portName == NULL || mode == NULL) {
546         return AUDIO_HAL_ERR_INVALID_PARAM;
547     }
548     if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
549         return AUDIO_HAL_ERR_INTERNAL;
550     }
551     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
552         return AUDIO_HAL_ERR_INTERNAL;
553     }
554     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
555     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
556         AudioProxyBufReplyRecycle(data, reply);
557         return AUDIO_HAL_ERR_INTERNAL;
558     }
559     if (hwAdapter == NULL || hwAdapter->proxyRemoteHandle == NULL) {
560         AudioProxyBufReplyRecycle(data, reply);
561         return AUDIO_HAL_ERR_INTERNAL;
562     }
563     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
564         AudioProxyBufReplyRecycle(data, reply);
565         return AUDIO_HAL_ERR_INTERNAL;
566     }
567     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
568     if (!HdfSbufWriteString(data, adapterName)) {
569         AudioProxyBufReplyRecycle(data, reply);
570         return AUDIO_HAL_ERR_INTERNAL;
571     }
572     if (AudioProxyAdapterSetAndGetPassthroughModeSBuf(data, reply, port) < 0) {
573         HDF_LOGE("Failed to obtain data");
574         AudioProxyBufReplyRecycle(data, reply);
575         return AUDIO_HAL_ERR_INTERNAL;
576     }
577     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_GET_PASS_MODE, data, reply);
578     if (ret < 0) {
579         AudioProxyBufReplyRecycle(data, reply);
580         return ret;
581     }
582     uint32_t tempMode = 0;
583     if (!HdfSbufReadUint32(reply, &tempMode)) {
584         AudioProxyBufReplyRecycle(data, reply);
585         return AUDIO_HAL_ERR_INTERNAL;
586     }
587     *mode = (AudioPortPassthroughMode)tempMode;
588     AudioProxyBufReplyRecycle(data, reply);
589     return AUDIO_HAL_SUCCESS;
590 }
591 }