• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "audio_common_vdi.h"
16 
17 #include "osal_mem.h"
18 #include "securec.h"
19 #include <hdf_base.h>
20 #include "audio_uhdf_log.h"
21 #include <sched.h>
22 
23 #define HDF_LOG_TAG    HDF_AUDIO_PRIMARY_IMPL
24 #define AUDIO_FORMAT_NUM_MAX 15
25 #define AUDIO_ROUTE_NUM_MAX 2
26 #define AUDIO_SAMPLE_FORMAT_NUM_MAX 30
27 #define AUDIO_SUB_PORT_NUM_MAX 10
28 #define AUDIO_FRAME_LEN_MAX (100 * 1024 * 1024)
29 
AudioCommonDevDescToVdiDevDescVdi(const struct AudioDeviceDescriptor * desc,struct AudioDeviceDescriptorVdi * vdiDesc)30 int32_t AudioCommonDevDescToVdiDevDescVdi(const struct AudioDeviceDescriptor *desc,
31     struct AudioDeviceDescriptorVdi *vdiDesc)
32 {
33     CHECK_NULL_PTR_RETURN_VALUE(desc, HDF_FAILURE);
34     CHECK_NULL_PTR_RETURN_VALUE(vdiDesc, HDF_FAILURE);
35 
36     vdiDesc->portId = desc->portId;
37     vdiDesc->pins = (enum AudioPortPinVdi)desc->pins;
38     vdiDesc->desc = strdup(desc->desc); // free by caller
39     if (vdiDesc->desc == NULL) {
40         AUDIO_FUNC_LOGE("strdup fail, desc->desc = %{public}s", desc->desc);
41         return HDF_FAILURE;
42     }
43     return HDF_SUCCESS;
44 }
45 
AudioCommonAttrsToVdiAttrsVdi(const struct AudioSampleAttributes * attrs,struct AudioSampleAttributesVdi * vdiAttrs)46 void AudioCommonAttrsToVdiAttrsVdi(const struct AudioSampleAttributes *attrs, struct AudioSampleAttributesVdi *vdiAttrs)
47 {
48     CHECK_NULL_PTR_RETURN(attrs);
49     CHECK_NULL_PTR_RETURN(vdiAttrs);
50     AUDIO_FUNC_LOGI("render or capture type is %{public}d", attrs->type);
51     vdiAttrs->type = (enum AudioCategoryVdi)attrs->type;
52     vdiAttrs->interleaved = attrs->interleaved;
53     vdiAttrs->format = (enum AudioFormatVdi)attrs->format;
54     vdiAttrs->sampleRate = attrs->sampleRate;
55     vdiAttrs->channelCount = attrs->channelCount;
56     vdiAttrs->channelLayout = attrs->channelLayout;
57     vdiAttrs->period = attrs->period;
58     vdiAttrs->frameSize = attrs->frameSize;
59     vdiAttrs->isBigEndian = attrs->isBigEndian;
60     vdiAttrs->isSignedData = attrs->isSignedData;
61     vdiAttrs->startThreshold = attrs->startThreshold;
62     vdiAttrs->stopThreshold = attrs->stopThreshold;
63     vdiAttrs->silenceThreshold = attrs->silenceThreshold;
64     vdiAttrs->streamId = attrs->streamId;
65     vdiAttrs->sourceType = attrs->sourceType;
66     if (vdiAttrs->type == AUDIO_VDI_OFFLOAD) {
67         vdiAttrs->offloadInfo.sampleRate = attrs->offloadInfo.sampleRate;
68         vdiAttrs->offloadInfo.channelCount = attrs->offloadInfo.channelCount;
69         vdiAttrs->offloadInfo.channelLayout = attrs->offloadInfo.channelLayout;
70         vdiAttrs->offloadInfo.bitRate = attrs->offloadInfo.bitRate;
71         vdiAttrs->offloadInfo.bitWidth = attrs->offloadInfo.bitWidth;
72         vdiAttrs->offloadInfo.format = (enum AudioFormatVdi)attrs->offloadInfo.format;
73         vdiAttrs->offloadInfo.offloadBufferSize = attrs->offloadInfo.offloadBufferSize;
74         vdiAttrs->offloadInfo.duration = attrs->offloadInfo.duration;
75     }
76     vdiAttrs->ecSampleAttributes.ecInterleaved = attrs->ecSampleAttributes.ecInterleaved;
77     vdiAttrs->ecSampleAttributes.ecFormat = (enum AudioFormatVdi)attrs->ecSampleAttributes.ecFormat;
78     vdiAttrs->ecSampleAttributes.ecSampleRate = attrs->ecSampleAttributes.ecSampleRate;
79     vdiAttrs->ecSampleAttributes.ecChannelCount = attrs->ecSampleAttributes.ecChannelCount;
80     vdiAttrs->ecSampleAttributes.ecChannelLayout = attrs->ecSampleAttributes.ecChannelLayout;
81     vdiAttrs->ecSampleAttributes.ecPeriod = attrs->ecSampleAttributes.ecPeriod;
82     vdiAttrs->ecSampleAttributes.ecFrameSize = attrs->ecSampleAttributes.ecFrameSize;
83     vdiAttrs->ecSampleAttributes.ecIsBigEndian = attrs->ecSampleAttributes.ecIsBigEndian;
84     vdiAttrs->ecSampleAttributes.ecIsSignedData = attrs->ecSampleAttributes.ecIsSignedData;
85     vdiAttrs->ecSampleAttributes.ecStartThreshold = attrs->ecSampleAttributes.ecStartThreshold;
86     vdiAttrs->ecSampleAttributes.ecStopThreshold = attrs->ecSampleAttributes.ecStopThreshold;
87     vdiAttrs->ecSampleAttributes.ecSilenceThreshold = attrs->ecSampleAttributes.ecSilenceThreshold;
88 }
89 
AudioCommonPortToVdiPortVdi(const struct AudioPort * port,struct AudioPortVdi * vdiPort)90 int32_t AudioCommonPortToVdiPortVdi(const struct AudioPort *port, struct AudioPortVdi *vdiPort)
91 {
92     CHECK_NULL_PTR_RETURN_VALUE(vdiPort, HDF_ERR_INVALID_PARAM);
93     CHECK_NULL_PTR_RETURN_VALUE(port, HDF_ERR_INVALID_PARAM);
94 
95     vdiPort->dir = (enum AudioPortDirectionVdi)port->dir;
96     vdiPort->portId = port->portId;
97     vdiPort->portName = strdup(port->portName); // free by caller
98     if (vdiPort->portName == NULL) {
99         AUDIO_FUNC_LOGE("strdup fail, port->portName = %{public}s", port->portName);
100         return HDF_FAILURE;
101     }
102 
103     return HDF_SUCCESS;
104 }
105 
AudioFormatsToFormatsVdi(const enum AudioFormatVdi * vdiFormats,uint32_t vdiFormatNum,enum AudioFormat ** formats,uint32_t * formatsLen)106 static int32_t AudioFormatsToFormatsVdi(const enum AudioFormatVdi *vdiFormats, uint32_t vdiFormatNum,
107     enum AudioFormat **formats, uint32_t *formatsLen)
108 {
109     CHECK_NULL_PTR_RETURN_VALUE(vdiFormats, HDF_ERR_INVALID_PARAM);
110     CHECK_NULL_PTR_RETURN_VALUE(formats, HDF_ERR_INVALID_PARAM);
111     CHECK_NULL_PTR_RETURN_VALUE(formatsLen, HDF_ERR_INVALID_PARAM);
112 
113     if (vdiFormatNum >= AUDIO_FORMAT_NUM_MAX || vdiFormatNum == 0) {
114         AUDIO_FUNC_LOGE("VdiFormats to formats len fail");
115         return HDF_ERR_INVALID_PARAM;
116     }
117 
118     uint32_t size = vdiFormatNum * sizeof(enum AudioFormat);
119     enum AudioFormat *formatTmp = (enum AudioFormat *)OsalMemCalloc(size);  // free by caller
120     if (formatTmp == NULL) {
121         AUDIO_FUNC_LOGE("formatTmp malloc fail");
122         return HDF_ERR_MALLOC_FAIL;
123     }
124 
125     int32_t ret = memcpy_s((void*)formatTmp, size, (void*)vdiFormats, vdiFormatNum * sizeof(enum AudioFormatVdi));
126     if (ret != HDF_SUCCESS) {
127         OsalMemFree((void *)formatTmp);
128         AUDIO_FUNC_LOGE("format cpy fail=%{public}d", ret);
129         return HDF_FAILURE;
130     }
131 
132     *formats = formatTmp;
133     *formatsLen = size;
134     return HDF_SUCCESS;
135 }
136 
AudioReleaseSubPortsVdi(struct AudioSubPortCapability ** subPorts,uint32_t * subPortsLen)137 static void AudioReleaseSubPortsVdi(struct AudioSubPortCapability **subPorts, uint32_t *subPortsLen)
138 {
139     struct AudioSubPortCapability *subPortsTmp = NULL;
140 
141     CHECK_NULL_PTR_RETURN(subPorts);
142     CHECK_NULL_PTR_RETURN(subPortsLen);
143 
144     uint32_t subPortsNum = *subPortsLen / sizeof(struct AudioSubPortCapability);
145     if (subPortsNum >= AUDIO_SUB_PORT_NUM_MAX) {
146         AUDIO_FUNC_LOGE("AudioReleaseSubPortsVdi len fail");
147         return;
148     }
149 
150     subPortsTmp = *subPorts;
151     for (uint32_t i = 0; i < subPortsNum; i++) {
152         OsalMemFree((void *)subPortsTmp[i].desc);
153     }
154 
155     OsalMemFree((void *)subPortsTmp);
156     subPortsTmp = NULL;
157 }
158 
AudioSubPortsToSubPortsVdi(const struct AudioSubPortCapabilityVdi * vdiSubPorts,uint32_t vdiSubPortsNum,struct AudioSubPortCapability ** subPorts,uint32_t * subPortsLen)159 static int32_t AudioSubPortsToSubPortsVdi(const struct AudioSubPortCapabilityVdi *vdiSubPorts, uint32_t vdiSubPortsNum,
160     struct AudioSubPortCapability **subPorts, uint32_t *subPortsLen)
161 {
162     CHECK_NULL_PTR_RETURN_VALUE(vdiSubPorts, HDF_ERR_INVALID_PARAM);
163     CHECK_NULL_PTR_RETURN_VALUE(subPorts, HDF_ERR_INVALID_PARAM);
164     CHECK_NULL_PTR_RETURN_VALUE(subPortsLen, HDF_ERR_INVALID_PARAM);
165 
166     if (vdiSubPortsNum >= AUDIO_SUB_PORT_NUM_MAX || vdiSubPortsNum == 0) {
167         AUDIO_FUNC_LOGE("VdiSubPorts to subPorts len fail");
168         return HDF_ERR_INVALID_PARAM;
169     }
170 
171     uint32_t size = vdiSubPortsNum * sizeof(struct AudioSubPortCapability);
172     struct AudioSubPortCapability *subPortsTmp = (struct AudioSubPortCapability *)OsalMemCalloc(size);
173     if (subPortsTmp == NULL) {
174         AUDIO_FUNC_LOGE("subPortsTmp malloc fail");
175         return HDF_ERR_MALLOC_FAIL;
176     }
177 
178     for (uint32_t i = 0; i < vdiSubPortsNum; i++) {
179         subPortsTmp[i].portId = vdiSubPorts[i].portId;
180         subPortsTmp[i].mask = (enum AudioPortPassthroughMode)vdiSubPorts[i].mask;
181         subPortsTmp[i].desc = strdup(vdiSubPorts[i].desc);
182         if (subPortsTmp[i].desc == NULL) {
183             *subPorts = subPortsTmp;
184             *subPortsLen = size;
185             AUDIO_FUNC_LOGE("strdup fail, vdiSubPorts[%{public}d].desc = %{public}s", i, vdiSubPorts[i].desc);
186             return HDF_FAILURE;
187         }
188     }
189 
190     *subPorts = subPortsTmp;
191     *subPortsLen = size;
192     return HDF_SUCCESS;
193 }
194 
AudioSampleFormatToSampleFormatsVdi(const enum AudioSampleFormatVdi * vdiSampleFormat,uint32_t vdiSupportSampleFormatNum,enum AudioSampleFormat ** sampleFormat,uint32_t * sampleFormatsLen)195 static int32_t AudioSampleFormatToSampleFormatsVdi(const enum AudioSampleFormatVdi *vdiSampleFormat,
196     uint32_t vdiSupportSampleFormatNum, enum AudioSampleFormat **sampleFormat, uint32_t *sampleFormatsLen)
197 {
198     CHECK_NULL_PTR_RETURN_VALUE(vdiSampleFormat, HDF_ERR_INVALID_PARAM);
199     CHECK_NULL_PTR_RETURN_VALUE(sampleFormat, HDF_ERR_INVALID_PARAM);
200     CHECK_NULL_PTR_RETURN_VALUE(sampleFormatsLen, HDF_ERR_INVALID_PARAM);
201 
202     if (vdiSupportSampleFormatNum >= AUDIO_SAMPLE_FORMAT_NUM_MAX || vdiSupportSampleFormatNum == 0) {
203         AUDIO_FUNC_LOGE("vdiSampleFormat to sampleFormats len fail");
204         return HDF_ERR_INVALID_PARAM;
205     }
206 
207     uint32_t size = vdiSupportSampleFormatNum * sizeof(enum AudioSampleFormat);
208     enum AudioSampleFormat *sampleFormatTmp = (enum AudioSampleFormat *)OsalMemCalloc(size);
209     if (sampleFormatTmp == NULL) {
210         AUDIO_FUNC_LOGE("sampleFormatTmp malloc fail");
211         return HDF_ERR_MALLOC_FAIL;
212     }
213 
214     int32_t ret = memcpy_s((void *)sampleFormatTmp, size, (void*)vdiSampleFormat,
215         vdiSupportSampleFormatNum * sizeof(enum AudioSampleFormatVdi));
216     if (ret != HDF_SUCCESS) {
217         OsalMemFree((void *)sampleFormatTmp);
218         AUDIO_FUNC_LOGE("sampleFormatTmp cpy fail=%{public}d", ret);
219         return HDF_FAILURE;
220     }
221 
222     *sampleFormat = sampleFormatTmp;
223     *sampleFormatsLen = size;
224     return HDF_SUCCESS;
225 }
226 
AudioCommonVdiPortCapToPortCapVdi(const struct AudioPortCapabilityVdi * vdiPortCap,struct AudioPortCapability * portCap)227 void AudioCommonVdiPortCapToPortCapVdi(const struct AudioPortCapabilityVdi *vdiPortCap,
228     struct AudioPortCapability *portCap)
229 {
230     CHECK_NULL_PTR_RETURN(portCap);
231     CHECK_NULL_PTR_RETURN(vdiPortCap);
232 
233     portCap->deviceType = vdiPortCap->deviceType;
234     portCap->deviceId = vdiPortCap->deviceId;
235     portCap->hardwareMode = vdiPortCap->hardwareMode;
236     portCap->sampleRateMasks = vdiPortCap->sampleRateMasks;
237     portCap->channelMasks = (enum AudioChannelMask)vdiPortCap->channelMasks;
238     portCap->channelCount = vdiPortCap->channelCount;
239 
240     int32_t ret = AudioFormatsToFormatsVdi(vdiPortCap->formats, vdiPortCap->formatNum, &portCap->formats,
241         &portCap->formatsLen);
242     if (ret != HDF_SUCCESS) {
243         AUDIO_FUNC_LOGE("AudioFormatsToFormatsVdi fail");
244         return;
245     }
246 
247     ret = AudioSubPortsToSubPortsVdi(vdiPortCap->subPorts, vdiPortCap->subPortsLen,
248         &portCap->subPorts, &portCap->subPortsLen);
249     if (ret != HDF_SUCCESS) {
250         OsalMemFree((void *)portCap->formats);
251         AudioReleaseSubPortsVdi(&portCap->subPorts, &portCap->subPortsLen);
252         portCap->formats = NULL;
253         AUDIO_FUNC_LOGE("VdiSubPortsToSubPorts fail");
254         return;
255     }
256 
257     ret = AudioSampleFormatToSampleFormatsVdi(vdiPortCap->supportSampleFormats, vdiPortCap->supportSampleFormatsLen,
258         &portCap->supportSampleFormats, &portCap->supportSampleFormatsLen);
259     if (ret != HDF_SUCCESS) {
260         OsalMemFree((void *)portCap->formats);
261         AudioReleaseSubPortsVdi(&portCap->subPorts, &portCap->subPortsLen);
262         portCap->formats = NULL;
263         AUDIO_FUNC_LOGE("VdiSampleFormatToSampleFormats fail");
264         return;
265     }
266 }
267 
AudioCommonFreeVdiRouteVdi(struct AudioRouteVdi * vdiRoute)268 void AudioCommonFreeVdiRouteVdi(struct AudioRouteVdi *vdiRoute)
269 {
270     CHECK_NULL_PTR_RETURN(vdiRoute);
271 
272     if (vdiRoute->sinks != NULL) {
273         if (vdiRoute->sinksLen > AUDIO_ROUTE_NUM_MAX) {
274             AUDIO_FUNC_LOGE("sinksLen para error");
275             return;
276         }
277 
278         for (uint32_t i = 0; i < vdiRoute->sinksLen; i++) {
279             if (vdiRoute->sinks[i].type == AUDIO_VDI_PORT_DEVICE_TYPE) {
280                 OsalMemFree((void *)vdiRoute->sinks[i].ext.device.desc);
281             }
282         }
283         OsalMemFree((void *)vdiRoute->sinks);
284     }
285 
286     if (vdiRoute->sources != NULL) {
287         if (vdiRoute->sourcesLen > AUDIO_ROUTE_NUM_MAX) {
288             AUDIO_FUNC_LOGE("sourcesLen para error");
289             return;
290         }
291 
292         for (uint32_t i = 0; i < vdiRoute->sourcesLen; i++) {
293             if (vdiRoute->sources[i].type == AUDIO_VDI_PORT_DEVICE_TYPE) {
294                 OsalMemFree((void *)vdiRoute->sources[i].ext.device.desc);
295             }
296         }
297         OsalMemFree((void *)vdiRoute->sources);
298     }
299 }
300 
AudioCommonRouteNodeToVdiRouteNodeVdi(struct AudioRouteNode * routeNode,struct AudioRouteNodeVdi * vdiRouteNode)301 static int32_t AudioCommonRouteNodeToVdiRouteNodeVdi(struct AudioRouteNode *routeNode,
302     struct AudioRouteNodeVdi *vdiRouteNode)
303 {
304     vdiRouteNode->portId = routeNode->portId;
305     vdiRouteNode->role = (enum AudioPortRoleVdi)routeNode->role;
306     vdiRouteNode->type = (enum AudioPortTypeVdi)routeNode->type;
307 
308     if (routeNode->type == AUDIO_VDI_PORT_DEVICE_TYPE) {
309         vdiRouteNode->ext.device.moduleId = routeNode->ext.device.moduleId;
310         vdiRouteNode->ext.device.type = (enum AudioPortPinVdi)routeNode->ext.device.type;
311         vdiRouteNode->ext.device.desc = strdup(routeNode->ext.device.desc);
312         if (vdiRouteNode->ext.device.desc == NULL) {
313             AUDIO_FUNC_LOGE("strdup fail, routeNode->ext.device.desc = %{public}s", routeNode->ext.device.desc);
314             return HDF_FAILURE;
315         }
316         return HDF_SUCCESS;
317     }
318 
319     if (routeNode->type == AUDIO_VDI_PORT_MIX_TYPE) {
320         vdiRouteNode->ext.mix.moduleId = routeNode->ext.mix.moduleId;
321         vdiRouteNode->ext.mix.streamId = routeNode->ext.mix.streamId;
322         vdiRouteNode->ext.mix.source = routeNode->ext.mix.source;
323         return HDF_SUCCESS;
324     }
325 
326     if (routeNode->type == AUDIO_VDI_PORT_SESSION_TYPE) {
327         vdiRouteNode->ext.session.sessionType = (enum AudioSessionTypeVdi)routeNode->ext.session.sessionType;
328         return HDF_SUCCESS;
329     }
330 
331     AUDIO_FUNC_LOGE("not match route node type");
332     return HDF_FAILURE;
333 }
334 
AudioCommonSinkToVdiSinkVdi(const struct AudioRoute * route,struct AudioRouteVdi * vdiRoute)335 static int32_t AudioCommonSinkToVdiSinkVdi(const struct AudioRoute *route, struct AudioRouteVdi *vdiRoute)
336 {
337     struct AudioRouteNodeVdi *nodes = NULL;
338     if (route->sinksLen > AUDIO_ROUTE_NUM_MAX) {
339         AUDIO_FUNC_LOGE("sinksLen para err");
340         return HDF_ERR_INVALID_PARAM;
341     }
342 
343     nodes = (struct AudioRouteNodeVdi *)OsalMemCalloc(route->sinksLen * sizeof(struct AudioRouteNodeVdi));
344     if (nodes == NULL) {
345         AUDIO_FUNC_LOGE("nodes null");
346         return HDF_ERR_MALLOC_FAIL;
347     }
348     vdiRoute->sinks = nodes;
349     vdiRoute->sinksLen = route->sinksLen;
350 
351     for (uint32_t i = 0; i < vdiRoute->sinksLen; i++) {
352         int32_t ret = AudioCommonRouteNodeToVdiRouteNodeVdi(&route->sinks[i], &vdiRoute->sinks[i]);
353         if (ret != HDF_SUCCESS) {
354             AUDIO_FUNC_LOGE("sink routeNode to vdiRouteNode fail");
355             /* nodes release by AudioCommonFreeVdiRouteVdi */
356             return HDF_FAILURE;
357         }
358     }
359 
360     return HDF_SUCCESS;
361 }
362 
AudioCommonSourceToVdiSourceVdi(const struct AudioRoute * route,struct AudioRouteVdi * vdiRoute)363 static int32_t AudioCommonSourceToVdiSourceVdi(const struct AudioRoute *route, struct AudioRouteVdi *vdiRoute)
364 {
365     struct AudioRouteNodeVdi *nodes = NULL;
366     if (route->sourcesLen > AUDIO_ROUTE_NUM_MAX) {
367         AUDIO_FUNC_LOGE("sinksLen para err");
368         return HDF_ERR_INVALID_PARAM;
369     }
370 
371     nodes = (struct AudioRouteNodeVdi *)OsalMemCalloc(route->sourcesLen * sizeof(struct AudioRouteNodeVdi));
372     if (nodes == NULL) {
373         AUDIO_FUNC_LOGE("nodes null");
374         return HDF_ERR_MALLOC_FAIL;
375     }
376     vdiRoute->sources = nodes;
377     vdiRoute->sourcesLen = route->sourcesLen;
378 
379     for (uint32_t i = 0; i < vdiRoute->sourcesLen; i++) {
380         int32_t ret = AudioCommonRouteNodeToVdiRouteNodeVdi(&route->sources[i], &vdiRoute->sources[i]);
381         if (ret != HDF_SUCCESS) {
382             AUDIO_FUNC_LOGE(" source routeNode to vdiRouteNode fail");
383             /* nodes release by AudioCommonFreeVdiRouteVdi */
384             return HDF_FAILURE;
385         }
386     }
387 
388     return HDF_SUCCESS;
389 }
390 
AudioCommonRouteToVdiRouteVdi(const struct AudioRoute * route,struct AudioRouteVdi * vdiRoute)391 int32_t AudioCommonRouteToVdiRouteVdi(const struct AudioRoute *route, struct AudioRouteVdi *vdiRoute)
392 {
393     int32_t sinkRet = HDF_SUCCESS;
394     int32_t sourcesRet = HDF_SUCCESS;
395 
396     CHECK_NULL_PTR_RETURN_VALUE(route, HDF_ERR_INVALID_PARAM);
397     CHECK_NULL_PTR_RETURN_VALUE(vdiRoute, HDF_ERR_INVALID_PARAM);
398 
399     if (route->sinks != NULL && route->sinksLen > 0) {
400         sinkRet = AudioCommonSinkToVdiSinkVdi(route, vdiRoute);
401         if (sinkRet != HDF_SUCCESS) {
402             AUDIO_FUNC_LOGE(" sink routeNode to vdiRouteNode fail");
403         }
404     }
405 
406     if (route->sources != NULL && route->sourcesLen > 0) {
407         sourcesRet = AudioCommonSourceToVdiSourceVdi(route, vdiRoute);
408         if (sourcesRet != HDF_SUCCESS) {
409             AUDIO_FUNC_LOGE(" source routeNode to vdiRouteNode fail");
410         }
411     }
412 
413     if (sinkRet != HDF_SUCCESS || sourcesRet != HDF_SUCCESS) {
414         /* free nodes by sink and source malloc nodes memory */
415         AudioCommonFreeVdiRouteVdi(vdiRoute);
416         return HDF_FAILURE;
417     }
418 
419     return HDF_SUCCESS;
420 }
421 
AudioCommonSceneToVdiSceneVdi(const struct AudioSceneDescriptor * scene,struct AudioSceneDescriptorVdi * vdiScene)422 int32_t AudioCommonSceneToVdiSceneVdi(const struct AudioSceneDescriptor *scene,
423     struct AudioSceneDescriptorVdi *vdiScene)
424 {
425     CHECK_NULL_PTR_RETURN_VALUE(scene, HDF_ERR_INVALID_PARAM);
426     CHECK_NULL_PTR_RETURN_VALUE(vdiScene, HDF_ERR_INVALID_PARAM);
427 
428     vdiScene->scene.id = scene->scene.id;
429     return AudioCommonDevDescToVdiDevDescVdi(&scene->desc, &vdiScene->desc);
430 }
431 
AudioCommonSampleAttrToVdiSampleAttrVdi(const struct AudioSampleAttributes * attrs,struct AudioSampleAttributesVdi * vdiAttrs)432 int32_t AudioCommonSampleAttrToVdiSampleAttrVdi(const struct AudioSampleAttributes *attrs,
433     struct AudioSampleAttributesVdi *vdiAttrs)
434 {
435     CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
436     CHECK_NULL_PTR_RETURN_VALUE(vdiAttrs, HDF_ERR_INVALID_PARAM);
437 
438     vdiAttrs->type = (enum AudioCategoryVdi)attrs->type;
439     vdiAttrs->interleaved = attrs->interleaved;
440     vdiAttrs->format = (enum AudioFormatVdi)attrs->format;
441     vdiAttrs->sampleRate = attrs->sampleRate;
442     vdiAttrs->channelCount = attrs->channelCount;
443     vdiAttrs->channelLayout = attrs->channelLayout;
444     vdiAttrs->period = attrs->period;
445     vdiAttrs->frameSize = attrs->frameSize;
446     vdiAttrs->isBigEndian = attrs->isBigEndian;
447     vdiAttrs->isSignedData = attrs->isSignedData;
448     vdiAttrs->startThreshold = attrs->startThreshold;
449     vdiAttrs->stopThreshold = attrs->stopThreshold;
450     vdiAttrs->silenceThreshold = attrs->silenceThreshold;
451     vdiAttrs->streamId = attrs->streamId;
452     vdiAttrs->sourceType = attrs->sourceType;
453     if (vdiAttrs->type == AUDIO_VDI_OFFLOAD) {
454         vdiAttrs->offloadInfo.sampleRate = attrs->offloadInfo.sampleRate;
455         vdiAttrs->offloadInfo.channelCount = attrs->offloadInfo.channelCount;
456         vdiAttrs->offloadInfo.channelLayout = attrs->offloadInfo.channelLayout;
457         vdiAttrs->offloadInfo.bitRate = attrs->offloadInfo.bitRate;
458         vdiAttrs->offloadInfo.bitWidth = attrs->offloadInfo.bitWidth;
459         vdiAttrs->offloadInfo.format = (enum AudioFormatVdi)attrs->offloadInfo.format;
460         vdiAttrs->offloadInfo.offloadBufferSize = attrs->offloadInfo.offloadBufferSize;
461         vdiAttrs->offloadInfo.duration = attrs->offloadInfo.duration;
462     }
463     return HDF_SUCCESS;
464 }
465 
AudioCommonVdiSampleAttrToSampleAttrVdi(const struct AudioSampleAttributesVdi * vdiAttrs,struct AudioSampleAttributes * attrs)466 int32_t AudioCommonVdiSampleAttrToSampleAttrVdi(const struct AudioSampleAttributesVdi *vdiAttrs,
467     struct AudioSampleAttributes *attrs)
468 {
469     CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
470     CHECK_NULL_PTR_RETURN_VALUE(vdiAttrs, HDF_ERR_INVALID_PARAM);
471 
472     attrs->type = (enum AudioCategory)vdiAttrs->type;
473     attrs->interleaved = vdiAttrs->interleaved;
474     attrs->format = (enum AudioFormat)vdiAttrs->format;
475     attrs->sampleRate = vdiAttrs->sampleRate;
476     attrs->channelCount = vdiAttrs->channelCount;
477     attrs->period = vdiAttrs->period;
478     attrs->frameSize = vdiAttrs->frameSize;
479     attrs->isBigEndian = vdiAttrs->isBigEndian;
480     attrs->isSignedData = vdiAttrs->isSignedData;
481     attrs->startThreshold = vdiAttrs->startThreshold;
482     attrs->stopThreshold = vdiAttrs->stopThreshold;
483     attrs->silenceThreshold = vdiAttrs->silenceThreshold;
484     attrs->streamId = vdiAttrs->streamId;
485 
486     return HDF_SUCCESS;
487 }
488 
AudioCommonFrameInfoToVdiFrameInfoVdi(const struct AudioFrameLen * frameLen,struct AudioCaptureFrameInfoVdi * frameInfoVdi)489 int32_t AudioCommonFrameInfoToVdiFrameInfoVdi(const struct AudioFrameLen *frameLen,
490     struct AudioCaptureFrameInfoVdi *frameInfoVdi)
491 {
492     CHECK_NULL_PTR_RETURN_VALUE(frameLen, HDF_ERR_INVALID_PARAM);
493     CHECK_NULL_PTR_RETURN_VALUE(frameInfoVdi, HDF_ERR_INVALID_PARAM);
494 
495     if (frameLen->frameLen <= 0 || frameLen->frameEcLen <= 0) {
496         AUDIO_FUNC_LOGE("frameLen len err, frameLen = [%{public}u], frameEcLen = [%{public}u]",
497             frameLen->frameLen, frameLen->frameEcLen);
498         return HDF_ERR_INVALID_PARAM;
499     }
500     frameInfoVdi->frameLen = frameLen->frameLen;
501     frameInfoVdi->frameEcLen = frameLen->frameEcLen;
502     frameInfoVdi->frame = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameLen->frameLen));
503     if (frameInfoVdi->frame == NULL) {
504         AUDIO_FUNC_LOGE("frameInfoVdi->frame null");
505         return HDF_ERR_MALLOC_FAIL;
506     }
507     frameInfoVdi->frameEc = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameLen->frameEcLen));
508     if (frameInfoVdi->frameEc == NULL) {
509         OsalMemFree((void *)frameInfoVdi->frame);
510         AUDIO_FUNC_LOGE("frameInfoVdi->frameEc null");
511         return HDF_ERR_MALLOC_FAIL;
512     }
513 
514     return HDF_SUCCESS;
515 }
516 
AudioCommonVdiFrameInfoToFrameInfoVdi(struct AudioCaptureFrameInfoVdi * frameInfoVdi,struct AudioCaptureFrameInfo * frameInfo)517 int32_t AudioCommonVdiFrameInfoToFrameInfoVdi(struct AudioCaptureFrameInfoVdi *frameInfoVdi,
518     struct AudioCaptureFrameInfo *frameInfo)
519 {
520     CHECK_NULL_PTR_RETURN_VALUE(frameInfo, HDF_ERR_INVALID_PARAM);
521     CHECK_NULL_PTR_RETURN_VALUE(frameInfoVdi, HDF_ERR_INVALID_PARAM);
522 
523     if (frameInfoVdi->frameLen <= 0 || frameInfoVdi->frameEcLen <= 0 ||
524         frameInfoVdi->frameLen > AUDIO_FRAME_LEN_MAX || frameInfoVdi->frameEcLen > AUDIO_FRAME_LEN_MAX) {
525         AUDIO_FUNC_LOGE("frameLen len err, frameLen = [%{public}u], frameEcLen = [%{public}u]",
526             frameInfoVdi->frameLen, frameInfoVdi->frameEcLen);
527         return HDF_ERR_INVALID_PARAM;
528     }
529     frameInfo->frameLen = frameInfoVdi->frameLen;
530     frameInfo->frameEcLen = frameInfoVdi->frameEcLen;
531     frameInfo->frame = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameInfo->frameLen));
532     if (frameInfo->frame == NULL) {
533         frameInfo->frameLen = 0;
534         AUDIO_FUNC_LOGE("frameInfo->frame null");
535         return HDF_ERR_MALLOC_FAIL;
536     }
537     int32_t ret = memcpy_s(frameInfo->frame, (size_t)frameInfo->frameLen, frameInfoVdi->frame,
538         (size_t)frameInfoVdi->frameLen);
539     if (ret != HDF_SUCCESS) {
540         AUDIO_FUNC_LOGE("memcpy_s frame fail");
541         return HDF_FAILURE;
542     }
543 
544     frameInfo->frameEc = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameInfo->frameEcLen));
545     if (frameInfo->frameEc == NULL) {
546         frameInfo->frameLen = 0;
547         frameInfo->frameEcLen = 0;
548         OsalMemFree((void *)frameInfo->frame);
549         AUDIO_FUNC_LOGE("frameInfo->frameEc null");
550         return HDF_ERR_MALLOC_FAIL;
551     }
552     ret = memcpy_s(frameInfo->frameEc, (size_t)frameInfo->frameEcLen, frameInfoVdi->frameEc,
553         (size_t)frameInfoVdi->frameEcLen);
554     if (ret != HDF_SUCCESS) {
555         AUDIO_FUNC_LOGE("memcpy_s frameEc fail");
556         return HDF_FAILURE;
557     }
558     frameInfo->replyBytes = frameInfoVdi->replyBytes;
559     frameInfo->replyBytesEc = frameInfoVdi->replyBytesEc;
560 
561     return HDF_SUCCESS;
562 }
563 
SetThreadPriority(void)564 void SetThreadPriority(void)
565 {
566     struct sched_param param;
567     param.sched_priority = 1;
568     if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
569         AUDIO_FUNC_LOGE("failed to set thread priority");
570     }
571 }
572