• 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 
GetAudioProxyCaptureFunc(struct AudioHwCapture * hwCapture)148 int32_t GetAudioProxyCaptureFunc(struct AudioHwCapture *hwCapture)
149 {
150     if (hwCapture == nullptr) {
151         return HDF_FAILURE;
152     }
153     hwCapture->common.control.Start = AudioProxyCaptureStart;
154     hwCapture->common.control.Stop = AudioProxyCaptureStop;
155     hwCapture->common.control.Pause = AudioProxyCapturePause;
156     hwCapture->common.control.Resume = AudioProxyCaptureResume;
157     hwCapture->common.control.Flush = AudioProxyCaptureFlush;
158     hwCapture->common.volume.SetMute = AudioProxyCaptureSetMute;
159     hwCapture->common.volume.GetMute = AudioProxyCaptureGetMute;
160     hwCapture->common.CaptureFrame = AudioProxyCaptureCaptureFrame;
161     return HDF_SUCCESS;
162 }
163 
InitHwRenderParam(struct AudioHwRender * hwRender,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs)164 int32_t InitHwRenderParam(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc,
165                           const struct AudioSampleAttributes *attrs)
166 {
167     if (hwRender == NULL || desc == NULL || attrs == NULL) {
168         HDF_LOGE("InitHwRenderParam param Is NULL");
169         return HDF_FAILURE;
170     }
171     hwRender->renderParam.renderMode.hwInfo.deviceDescript = *desc;
172     hwRender->renderParam.frameRenderMode.attrs = *attrs;
173     return HDF_SUCCESS;
174 }
175 
InitHwCaptureParam(struct AudioHwCapture * hwCapture,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs)176 int32_t InitHwCaptureParam(struct AudioHwCapture *hwCapture, const struct AudioDeviceDescriptor *desc,
177     const struct AudioSampleAttributes *attrs)
178 {
179     if (hwCapture == nullptr || desc == nullptr || attrs == nullptr) {
180         HDF_LOGE("InitHwCaptureParam param Is NULL");
181         return HDF_FAILURE;
182     }
183     hwCapture->captureParam.captureMode.hwInfo.deviceDescript = *desc;
184     hwCapture->captureParam.frameCaptureMode.attrs = *attrs;
185     return HDF_SUCCESS;
186 }
187 
188 AudioFormat g_formatIdZero = AUDIO_FORMAT_TYPE_PCM_16_BIT;
InitForGetPortCapability(struct AudioPort portIndex,struct AudioPortCapability * capabilityIndex)189 int32_t InitForGetPortCapability(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex)
190 {
191     if (capabilityIndex == NULL) {
192         HDF_LOGE("capabilityIndex Is NULL");
193         return HDF_FAILURE;
194     }
195     /* get capabilityIndex from driver or default */
196     if (portIndex.dir != PORT_OUT) {
197         capabilityIndex->hardwareMode = true;
198         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
199         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
200         return HDF_SUCCESS;
201     }
202     if (portIndex.portId == 0) {
203         capabilityIndex->hardwareMode = true;
204         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
205         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
206         capabilityIndex->deviceType = portIndex.dir;
207         capabilityIndex->deviceId = PIN_OUT_SPEAKER;
208         capabilityIndex->formatNum = 1;
209         capabilityIndex->formats = &g_formatIdZero;
210         capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000;
211         capabilityIndex->subPortsNum = 1;
212         capabilityIndex->subPorts =
213             reinterpret_cast<struct AudioSubPortCapability *>(calloc(capabilityIndex->subPortsNum,
214             sizeof(struct AudioSubPortCapability)));
215         if (capabilityIndex->subPorts == NULL) {
216             HDF_LOGE("pointer is null!");
217             return HDF_FAILURE;
218         }
219         capabilityIndex->subPorts->portId = portIndex.portId;
220         capabilityIndex->subPorts->desc = portIndex.portName;
221         capabilityIndex->subPorts->mask = PORT_PASSTHROUGH_LPCM;
222         return HDF_SUCCESS;
223     }
224     if (portIndex.portId == 1) {
225         capabilityIndex->hardwareMode = true;
226         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
227         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
228         capabilityIndex->deviceType = portIndex.dir;
229         capabilityIndex->deviceId = PIN_OUT_HEADSET;
230         capabilityIndex->formatNum = 1;
231         capabilityIndex->formats = &g_formatIdZero;
232         capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000 | AUDIO_SAMPLE_RATE_MASK_8000;
233         return HDF_SUCCESS;
234     }
235     if (portIndex.portId == HDMI_PORT_ID) {
236         return HdmiPortInit(portIndex, capabilityIndex);
237     }
238     return HDF_FAILURE;
239 }
240 
AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability * portCapabilitys,const int32_t num)241 void AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability *portCapabilitys, const int32_t num)
242 {
243     int32_t i = 0;
244     if (portCapabilitys == NULL) {
245         return;
246     }
247     while (i < num) {
248         if (&portCapabilitys[i] == NULL) {
249             break;
250         }
251         AudioMemFree((void **)(&portCapabilitys[i].capability.subPorts));
252         i++;
253     }
254     return;
255 }
256 
AudioProxyAdapterInitAllPorts(struct AudioAdapter * adapter)257 int32_t AudioProxyAdapterInitAllPorts(struct AudioAdapter *adapter)
258 {
259     struct HdfSBuf *data = NULL;
260     struct HdfSBuf *reply = NULL;
261     const char *adapterName = NULL;
262     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
263     if (hwAdapter == NULL || hwAdapter->adapterDescriptor.adapterName == NULL ||
264         hwAdapter->proxyRemoteHandle == NULL) {
265         HDF_LOGE("hwAdapter Is NULL");
266         return AUDIO_HAL_ERR_INVALID_PARAM;
267     }
268     /* Fake data */
269     uint32_t portNum = hwAdapter->adapterDescriptor.portNum;
270     struct AudioPort *ports = hwAdapter->adapterDescriptor.ports;
271     if (ports == NULL || portNum == 0) {
272         HDF_LOGE("ports is NULL!");
273         return AUDIO_HAL_ERR_INTERNAL;
274     }
275     struct AudioPortAndCapability *portCapability = reinterpret_cast<struct AudioPortAndCapability *>(calloc(portNum,
276         sizeof(struct AudioPortAndCapability)));
277     if (portCapability == NULL) {
278         HDF_LOGE("portCapability is NULL!");
279         return AUDIO_HAL_ERR_MALLOC_FAIL;
280     }
281     for (uint32_t i = 0; i < portNum; i++) {
282         portCapability[i].port = ports[i];
283         if (InitForGetPortCapability(ports[i], &portCapability[i].capability)) {
284             HDF_LOGE("ports Init Invalid!");
285             AudioAdapterReleaseCapSubPorts(portCapability, portNum);
286             AudioMemFree((void **)&portCapability);
287             return AUDIO_HAL_ERR_INTERNAL;
288         }
289     }
290     hwAdapter->portCapabilitys = portCapability;
291     hwAdapter->portCapabilitys->mode = PORT_PASSTHROUGH_LPCM;
292     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
293         return AUDIO_HAL_ERR_INTERNAL;
294     }
295     adapterName = hwAdapter->adapterDescriptor.adapterName;
296     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
297         AudioProxyBufReplyRecycle(data, reply);
298         return AUDIO_HAL_ERR_INTERNAL;
299     }
300     if (!HdfSbufWriteString(data, adapterName)) {
301         AudioProxyBufReplyRecycle(data, reply);
302         return AUDIO_HAL_ERR_INTERNAL;
303     }
304     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_INIT_PORTS, data, reply);
305     if (ret < 0) {
306         HDF_LOGE("Get Failed AudioAdapter!");
307         AudioProxyBufReplyRecycle(data, reply);
308         return ret;
309     }
310     AudioProxyBufReplyRecycle(data, reply);
311     return AUDIO_HAL_SUCCESS;
312 }
313 
AudioProxyAdapterCreateRenderSplit(const struct AudioHwAdapter * hwAdapter,struct AudioHwRender * hwRender)314 int32_t AudioProxyAdapterCreateRenderSplit(const struct AudioHwAdapter *hwAdapter, struct AudioHwRender *hwRender)
315 {
316     if (hwAdapter == NULL || hwRender == NULL) {
317         return HDF_FAILURE;
318     }
319     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
320         return HDF_FAILURE;
321     }
322     uint32_t adapterNameLen = strlen(hwAdapter->adapterDescriptor.adapterName);
323     /* Get Adapter name */
324     int32_t ret = strncpy_s(hwRender->renderParam.renderMode.hwInfo.adapterName, NAME_LEN - 1,
325         hwAdapter->adapterDescriptor.adapterName, adapterNameLen);
326     if (ret != EOK) {
327         return HDF_FAILURE;
328     }
329     return HDF_SUCCESS;
330 }
331 
AudioProxyAdapterCreateCaptureSplit(const struct AudioHwAdapter * hwAdapter,struct AudioHwCapture * hwCapture)332 int32_t AudioProxyAdapterCreateCaptureSplit(const struct AudioHwAdapter *hwAdapter, struct AudioHwCapture *hwCapture)
333 {
334     if (hwAdapter == nullptr || hwCapture == nullptr) {
335         return HDF_FAILURE;
336     }
337     if (hwAdapter->adapterDescriptor.adapterName == nullptr) {
338         return HDF_FAILURE;
339     }
340     uint32_t adapterNameLen = strlen(hwAdapter->adapterDescriptor.adapterName);
341     /* Get Adapter name */
342     int32_t ret = strncpy_s(hwCapture->captureParam.captureMode.hwInfo.adapterName, NAME_LEN - 1,
343         hwAdapter->adapterDescriptor.adapterName, adapterNameLen);
344     if (ret != EOK) {
345         return HDF_FAILURE;
346     }
347     return HDF_SUCCESS;
348 }
349 
AudioProxyAdapterCreateRender(struct AudioAdapter * adapter,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs,struct AudioRender ** render)350 int32_t AudioProxyAdapterCreateRender(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc,
351                                       const struct AudioSampleAttributes *attrs, struct AudioRender **render)
352 {
353     HDF_LOGI("%{public}s, ", __func__);
354     struct HdfSBuf *data = NULL;
355     struct HdfSBuf *reply = NULL;
356     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
357     if (hwAdapter == NULL || hwAdapter->proxyRemoteHandle == NULL || desc == NULL || attrs == NULL || render == NULL) {
358         return AUDIO_HAL_ERR_INVALID_PARAM;
359     }
360     struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(calloc(1, sizeof(*hwRender)));
361     if (hwRender == NULL) {
362         HDF_LOGE("hwRender is NULL!");
363         return AUDIO_HAL_ERR_MALLOC_FAIL;
364     }
365     hwRender->proxyRemoteHandle = hwAdapter->proxyRemoteHandle;
366     if (GetAudioProxyRenderFunc(hwRender) < 0) {
367         AudioMemFree(reinterpret_cast<void **>(&hwRender));
368         return AUDIO_HAL_ERR_INTERNAL;
369     }
370     /* Fill hwRender para */
371     if (InitHwRenderParam(hwRender, desc, attrs) < 0) {
372         AudioMemFree(reinterpret_cast<void **>(&hwRender));
373         return AUDIO_HAL_ERR_INTERNAL;
374     }
375     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
376         AudioMemFree(reinterpret_cast<void **>(&hwRender));
377         return AUDIO_HAL_ERR_INTERNAL;
378     }
379     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
380         AudioProxyBufReplyRecycle(data, reply);
381         AudioMemFree(reinterpret_cast<void **>(&hwRender));
382         return AUDIO_HAL_ERR_INTERNAL;
383     }
384     if (AudioProxyCommonInitCreateData(data, hwAdapter, desc, attrs) < 0) {
385         HDF_LOGE("Failed to obtain reply");
386         AudioProxyBufReplyRecycle(data, reply);
387         AudioMemFree(reinterpret_cast<void **>(&hwRender));
388         return AUDIO_HAL_ERR_INTERNAL;
389     }
390     int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, AUDIO_HDI_RENDER_CREATE_RENDER, data, reply);
391     if (ret < 0) {
392         HDF_LOGE("Send Server fail!");
393         AudioProxyBufReplyRecycle(data, reply);
394         AudioMemFree(reinterpret_cast<void **>(&hwRender));
395         return ret;
396     }
397     AudioProxyBufReplyRecycle(data, reply);
398     if (AudioProxyAdapterCreateRenderSplit(hwAdapter, hwRender) < 0) {
399         AudioMemFree(reinterpret_cast<void **>(&hwRender));
400         return AUDIO_HAL_ERR_INTERNAL;
401     }
402     *render = &hwRender->common;
403     return AUDIO_HAL_SUCCESS;
404 }
405 
InitializeHwCapture(struct AudioHwCapture * hwCapture,struct AudioHwAdapter * hwAdapter,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs)406 bool InitializeHwCapture(struct AudioHwCapture *hwCapture, struct AudioHwAdapter *hwAdapter,
407     const struct AudioDeviceDescriptor *desc, const struct AudioSampleAttributes *attrs)
408 {
409     hwCapture->proxyRemoteHandle = hwAdapter->proxyRemoteHandle;
410     if (GetAudioProxyCaptureFunc(hwCapture) < 0) {
411         return false;
412     }
413     if (InitHwCaptureParam(hwCapture, desc, attrs) < 0) {
414         return false;
415     }
416     return true;
417 }
418 
AudioProxyAdapterCreateCapture(struct AudioAdapter * adapter,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs,struct AudioCapture ** capture)419 int32_t AudioProxyAdapterCreateCapture(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc,
420     const struct AudioSampleAttributes *attrs, struct AudioCapture **capture)
421 {
422     HDF_LOGI("%{public}s", __func__);
423     struct HdfSBuf *data = nullptr;
424     struct HdfSBuf *reply = nullptr;
425     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
426     if (hwAdapter == nullptr || hwAdapter->proxyRemoteHandle == nullptr || desc == nullptr ||
427         attrs == nullptr || capture == nullptr) {
428         return AUDIO_HAL_ERR_INVALID_PARAM;
429     }
430     struct AudioHwCapture *hwCapture = reinterpret_cast<struct AudioHwCapture *>(calloc(1, sizeof(*hwCapture)));
431     if (hwCapture == nullptr) {
432         HDF_LOGE("hwCapture is NULL!");
433         return AUDIO_HAL_ERR_MALLOC_FAIL;
434     }
435     if (!InitializeHwCapture(hwCapture, hwAdapter, desc, attrs)) {
436         AudioMemFree(reinterpret_cast<void **>(&hwCapture));
437         return AUDIO_HAL_ERR_INTERNAL;
438     }
439     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
440         AudioMemFree(reinterpret_cast<void **>(&hwCapture));
441         return AUDIO_HAL_ERR_INTERNAL;
442     }
443     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
444         AudioProxyBufReplyRecycle(data, reply);
445         AudioMemFree(reinterpret_cast<void **>(&hwCapture));
446         return AUDIO_HAL_ERR_INTERNAL;
447     }
448     if (AudioProxyCommonInitCreateData(data, hwAdapter, desc, attrs) < 0) {
449         HDF_LOGE("Failed to obtain reply");
450         AudioProxyBufReplyRecycle(data, reply);
451         AudioMemFree(reinterpret_cast<void **>(&hwCapture));
452         return AUDIO_HAL_ERR_INTERNAL;
453     }
454     int32_t ret = AudioProxyDispatchCall(hwCapture->proxyRemoteHandle, AUDIO_HDI_CAPTURE_CREATE_CAPTURE, data, reply);
455     if (ret < 0) {
456         HDF_LOGE("Send Server fail!");
457         AudioProxyBufReplyRecycle(data, reply);
458         AudioMemFree(reinterpret_cast<void **>(&hwCapture));
459         return ret;
460     }
461     AudioProxyBufReplyRecycle(data, reply);
462     if (AudioProxyAdapterCreateCaptureSplit(hwAdapter, hwCapture) < 0) {
463         AudioMemFree(reinterpret_cast<void **>(&hwCapture));
464         return AUDIO_HAL_ERR_INTERNAL;
465     }
466     *capture = &hwCapture->common;
467     return AUDIO_HAL_SUCCESS;
468 }
469 
AudioProxyAdapterDestroyRender(struct AudioAdapter * adapter,struct AudioRender * render)470 int32_t AudioProxyAdapterDestroyRender(struct AudioAdapter *adapter, struct AudioRender *render)
471 {
472     struct HdfSBuf *data = NULL;
473     struct HdfSBuf *reply = NULL;
474     if (adapter == NULL || render == NULL) {
475         return AUDIO_HAL_ERR_INVALID_PARAM;
476     }
477     struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(render);
478     if (hwRender == NULL || hwRender->proxyRemoteHandle == NULL) {
479         return AUDIO_HAL_ERR_INVALID_PARAM;
480     }
481     if (AudioProxyPreprocessRender(reinterpret_cast<AudioHwRender *>(render), &data, &reply) < 0) {
482         return AUDIO_HAL_ERR_INTERNAL;
483     }
484     int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, AUDIO_HDI_RENDER_DESTROY, data, reply);
485     if (ret < 0) {
486         if (ret != HDF_ERR_INVALID_OBJECT) {
487             HDF_LOGE("AudioRenderRenderFrame FAIL");
488         }
489         AudioProxyBufReplyRecycle(data, reply);
490         return ret;
491     }
492     AudioMemFree(reinterpret_cast<void **>(&hwRender->renderParam.frameRenderMode.buffer));
493     AudioMemFree(reinterpret_cast<void **>(&render));
494     AudioProxyBufReplyRecycle(data, reply);
495     return AUDIO_HAL_SUCCESS;
496 }
497 
AudioProxyAdapterDestroyCapture(struct AudioAdapter * adapter,struct AudioCapture * capture)498 int32_t AudioProxyAdapterDestroyCapture(struct AudioAdapter *adapter, struct AudioCapture *capture)
499 {
500     struct HdfSBuf *data = nullptr;
501     struct HdfSBuf *reply = nullptr;
502     if (adapter == nullptr || capture == nullptr) {
503         return AUDIO_HAL_ERR_INVALID_PARAM;
504     }
505     struct AudioHwCapture *hwCapture = reinterpret_cast<struct AudioHwCapture *>(capture);
506     if (hwCapture == nullptr || hwCapture->proxyRemoteHandle == nullptr) {
507         return AUDIO_HAL_ERR_INVALID_PARAM;
508     }
509     if (AudioProxyPreprocessCapture(reinterpret_cast<AudioHwCapture *>(capture), &data, &reply) < 0) {
510         return AUDIO_HAL_ERR_INTERNAL;
511     }
512     int32_t ret = AudioProxyDispatchCall(hwCapture->proxyRemoteHandle, AUDIO_HDI_CAPTURE_DESTROY, data, reply);
513     if (ret < 0) {
514         if (ret != HDF_ERR_INVALID_OBJECT) {
515             HDF_LOGE("AudioDestroyCapture FAIL");
516         }
517         AudioProxyBufReplyRecycle(data, reply);
518         return ret;
519     }
520     AudioMemFree(reinterpret_cast<void **>(&capture));
521     AudioProxyBufReplyRecycle(data, reply);
522     return AUDIO_HAL_SUCCESS;
523 }
524 
AudioProxyAdapterWritePortCapability(const struct AudioHwAdapter * hwAdapter,const struct AudioPort * port,struct HdfSBuf * data)525 int32_t AudioProxyAdapterWritePortCapability(const struct AudioHwAdapter *hwAdapter,
526     const struct AudioPort *port, struct HdfSBuf *data)
527 {
528     if (hwAdapter == NULL || port == NULL || data == NULL) {
529         return HDF_FAILURE;
530     }
531     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
532         return HDF_FAILURE;
533     }
534     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
535     if (!HdfSbufWriteString(data, adapterName)) {
536         return HDF_FAILURE;
537     }
538     uint32_t tempDir = (uint32_t)port->dir;
539     if (!HdfSbufWriteUint32(data, tempDir)) {
540         return HDF_FAILURE;
541     }
542     if (!HdfSbufWriteUint32(data, port->portId)) {
543         return HDF_FAILURE;
544     }
545     if (port->portName == NULL) {
546         return HDF_FAILURE;
547     }
548     if (!HdfSbufWriteString(data, port->portName)) {
549         return HDF_FAILURE;
550     }
551     return HDF_SUCCESS;
552 }
553 
AudioProxyAdapterGetPortCapability(struct AudioAdapter * adapter,const struct AudioPort * port,struct AudioPortCapability * capability)554 int32_t AudioProxyAdapterGetPortCapability(struct AudioAdapter *adapter,
555     const struct AudioPort *port, struct AudioPortCapability *capability)
556 {
557     HDF_LOGI("%{public}s, ", __func__);
558     if (adapter == NULL || port == NULL || port->portName == NULL || capability == NULL) {
559         return AUDIO_HAL_ERR_INVALID_PARAM;
560     }
561     if (port->portId < 0) {
562         return AUDIO_HAL_ERR_INTERNAL;
563     }
564     struct HdfSBuf *data = NULL;
565     struct HdfSBuf *reply = NULL;
566     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
567         return AUDIO_HAL_ERR_INTERNAL;
568     }
569     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
570     if (hwAdapter == NULL || !HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
571         AudioProxyBufReplyRecycle(data, reply);
572         return AUDIO_HAL_ERR_INTERNAL;
573     }
574     if (AudioProxyAdapterWritePortCapability(hwAdapter, port, data)) {
575         AudioProxyBufReplyRecycle(data, reply);
576         return AUDIO_HAL_ERR_INTERNAL;
577     }
578     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_GET_PORT_CAPABILITY, data, reply);
579     if (ret < 0) {
580         AudioProxyBufReplyRecycle(data, reply);
581         return ret;
582     }
583     AudioProxyBufReplyRecycle(data, reply);
584     /* proxy must init local capability ,this capability the same of Server's */
585     struct AudioPortAndCapability *hwAdapterPortCapabilitys = hwAdapter->portCapabilitys;
586     if (hwAdapterPortCapabilitys == NULL) {
587         HDF_LOGE("hwAdapter portCapabilitys is NULL!");
588         return AUDIO_HAL_ERR_INTERNAL;
589     }
590     int32_t portNum = hwAdapter->adapterDescriptor.portNum;
591     while (hwAdapterPortCapabilitys != NULL && (portNum > 0)) {
592         if (hwAdapterPortCapabilitys->port.portId == port->portId) {
593             *capability = hwAdapterPortCapabilitys->capability;
594             return AUDIO_HAL_SUCCESS;
595         }
596         hwAdapterPortCapabilitys++;
597         portNum--;
598     }
599     return AUDIO_HAL_ERR_INTERNAL;
600 }
601 
AudioProxyAdapterSetAndGetPassthroughModeSBuf(struct HdfSBuf * data,const struct HdfSBuf * reply,const struct AudioPort * port)602 int32_t AudioProxyAdapterSetAndGetPassthroughModeSBuf(struct HdfSBuf *data,
603     const struct HdfSBuf *reply, const struct AudioPort *port)
604 {
605     if (data == NULL || port == NULL || port->portName == NULL) {
606         return HDF_FAILURE;
607     }
608     uint32_t tempDir = port->dir;
609     if (!HdfSbufWriteUint32(data, tempDir)) {
610         return HDF_FAILURE;
611     }
612     if (!HdfSbufWriteUint32(data, port->portId)) {
613         return HDF_FAILURE;
614     }
615     if (!HdfSbufWriteString(data, port->portName)) {
616         HDF_LOGE("HdfSbufWriteString error");
617         return HDF_FAILURE;
618     }
619     return HDF_SUCCESS;
620 }
621 
AudioProxyAdapterSetPassthroughMode(struct AudioAdapter * adapter,const struct AudioPort * port,AudioPortPassthroughMode mode)622 int32_t AudioProxyAdapterSetPassthroughMode(struct AudioAdapter *adapter,
623     const struct AudioPort *port, AudioPortPassthroughMode mode)
624 {
625     HDF_LOGI("%{public}s, ", __func__);
626     struct HdfSBuf *data = NULL;
627     struct HdfSBuf *reply = NULL;
628     if (adapter == NULL || port == NULL || port->portName == NULL) {
629         return AUDIO_HAL_ERR_INVALID_PARAM;
630     }
631     if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
632         return AUDIO_HAL_ERR_INTERNAL;
633     }
634     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
635         HDF_LOGE("AudioProxyPreprocessSBuf Fail");
636         return AUDIO_HAL_ERR_INTERNAL;
637     }
638     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
639     if (hwAdapter == NULL || !HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data) ||
640         hwAdapter->adapterDescriptor.adapterName == NULL) {
641         AudioProxyBufReplyRecycle(data, reply);
642         return AUDIO_HAL_ERR_INTERNAL;
643     }
644     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
645     if (!HdfSbufWriteString(data, adapterName)) {
646         HDF_LOGE("adapterName Write Fail");
647         AudioProxyBufReplyRecycle(data, reply);
648         return AUDIO_HAL_ERR_INTERNAL;
649     }
650     if (AudioProxyAdapterSetAndGetPassthroughModeSBuf(data, reply, port) < 0) {
651         HDF_LOGE("Failed to obtain data");
652         AudioProxyBufReplyRecycle(data, reply);
653         return AUDIO_HAL_ERR_INTERNAL;
654     }
655     uint32_t tempMode = (uint32_t)mode;
656     if (!HdfSbufWriteUint32(data, tempMode)) {
657         HDF_LOGE("Mode Write Fail");
658         AudioProxyBufReplyRecycle(data, reply);
659         return AUDIO_HAL_ERR_INTERNAL;
660     }
661     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_SET_PASS_MODE, data, reply);
662     if (ret < 0) {
663         AudioProxyBufReplyRecycle(data, reply);
664         HDF_LOGE("Failed to send server");
665         return ret;
666     }
667     AudioProxyBufReplyRecycle(data, reply);
668     return AUDIO_HAL_SUCCESS;
669 }
670 
AudioProxyAdapterGetPassthroughMode(struct AudioAdapter * adapter,const struct AudioPort * port,AudioPortPassthroughMode * mode)671 int32_t AudioProxyAdapterGetPassthroughMode(struct AudioAdapter *adapter,
672     const struct AudioPort *port, AudioPortPassthroughMode *mode)
673 {
674     struct HdfSBuf *data = NULL;
675     struct HdfSBuf *reply = NULL;
676     if (adapter == NULL || port == NULL || port->portName == NULL || mode == NULL) {
677         return AUDIO_HAL_ERR_INVALID_PARAM;
678     }
679     if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
680         return AUDIO_HAL_ERR_INTERNAL;
681     }
682     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
683         return AUDIO_HAL_ERR_INTERNAL;
684     }
685     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
686     if (hwAdapter == NULL || !HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
687         AudioProxyBufReplyRecycle(data, reply);
688         return AUDIO_HAL_ERR_INTERNAL;
689     }
690     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
691         AudioProxyBufReplyRecycle(data, reply);
692         return AUDIO_HAL_ERR_INTERNAL;
693     }
694     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
695     if (!HdfSbufWriteString(data, adapterName)) {
696         AudioProxyBufReplyRecycle(data, reply);
697         return AUDIO_HAL_ERR_INTERNAL;
698     }
699     if (AudioProxyAdapterSetAndGetPassthroughModeSBuf(data, reply, port) < 0) {
700         HDF_LOGE("Failed to obtain data");
701         AudioProxyBufReplyRecycle(data, reply);
702         return AUDIO_HAL_ERR_INTERNAL;
703     }
704     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_GET_PASS_MODE, data, reply);
705     if (ret < 0) {
706         AudioProxyBufReplyRecycle(data, reply);
707         return ret;
708     }
709     uint32_t tempMode = 0;
710     if (!HdfSbufReadUint32(reply, &tempMode)) {
711         AudioProxyBufReplyRecycle(data, reply);
712         return AUDIO_HAL_ERR_INTERNAL;
713     }
714     *mode = (AudioPortPassthroughMode)tempMode;
715     AudioProxyBufReplyRecycle(data, reply);
716     return AUDIO_HAL_SUCCESS;
717 }
718 }