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 
22 #define HDF_LOG_TAG    HDF_AUDIO_PRIMARY_IMPL
23 #define AUDIO_FORMAT_NUM_MAX 15
24 #define AUDIO_ROUTE_NUM_MAX 2
25 #define AUDIO_SAMPLE_FORMAT_NUM_MAX 30
26 #define AUDIO_SUB_PORT_NUM_MAX 10
27 
AudioCommonDevDescToVdiDevDescVdi(const struct AudioDeviceDescriptor * desc,struct AudioDeviceDescriptorVdi * vdiDesc)28 int32_t AudioCommonDevDescToVdiDevDescVdi(const struct AudioDeviceDescriptor *desc,
29     struct AudioDeviceDescriptorVdi *vdiDesc)
30 {
31     CHECK_NULL_PTR_RETURN_VALUE(desc, HDF_FAILURE);
32     CHECK_NULL_PTR_RETURN_VALUE(vdiDesc, HDF_FAILURE);
33 
34     vdiDesc->portId = desc->portId;
35     vdiDesc->pins = (enum AudioPortPinVdi)desc->pins;
36     vdiDesc->desc = strdup(desc->desc); // free by caller
37     if (vdiDesc->desc == NULL) {
38         AUDIO_FUNC_LOGE("strdup fail, desc->desc = %{public}s", desc->desc);
39         return HDF_FAILURE;
40     }
41     return HDF_SUCCESS;
42 }
43 
AudioCommonAttrsToVdiAttrsVdi(const struct AudioSampleAttributes * attrs,struct AudioSampleAttributesVdi * vdiAttrs)44 void AudioCommonAttrsToVdiAttrsVdi(const struct AudioSampleAttributes *attrs, struct AudioSampleAttributesVdi *vdiAttrs)
45 {
46     CHECK_NULL_PTR_RETURN(attrs);
47     CHECK_NULL_PTR_RETURN(vdiAttrs);
48     AUDIO_FUNC_LOGI("render or capture type is %{public}d", attrs->type);
49     vdiAttrs->type = (enum AudioCategoryVdi)attrs->type;
50     vdiAttrs->interleaved = attrs->interleaved;
51     vdiAttrs->format = (enum AudioFormatVdi)attrs->format;
52     vdiAttrs->sampleRate = attrs->sampleRate;
53     vdiAttrs->channelCount = attrs->channelCount;
54     vdiAttrs->channelLayout = attrs->channelLayout;
55     vdiAttrs->period = attrs->period;
56     vdiAttrs->frameSize = attrs->frameSize;
57     vdiAttrs->isBigEndian = attrs->isBigEndian;
58     vdiAttrs->isSignedData = attrs->isSignedData;
59     vdiAttrs->startThreshold = attrs->startThreshold;
60     vdiAttrs->stopThreshold = attrs->stopThreshold;
61     vdiAttrs->silenceThreshold = attrs->silenceThreshold;
62     vdiAttrs->streamId = attrs->streamId;
63     vdiAttrs->sourceType = attrs->sourceType;
64     if (vdiAttrs->type == AUDIO_VDI_OFFLOAD) {
65         vdiAttrs->offloadInfo.sampleRate = attrs->offloadInfo.sampleRate;
66         vdiAttrs->offloadInfo.channelCount = attrs->offloadInfo.channelCount;
67         vdiAttrs->offloadInfo.channelLayout = attrs->offloadInfo.channelLayout;
68         vdiAttrs->offloadInfo.bitRate = attrs->offloadInfo.bitRate;
69         vdiAttrs->offloadInfo.bitWidth = attrs->offloadInfo.bitWidth;
70         vdiAttrs->offloadInfo.format = (enum AudioFormatVdi)attrs->offloadInfo.format;
71         vdiAttrs->offloadInfo.offloadBufferSize = attrs->offloadInfo.offloadBufferSize;
72         vdiAttrs->offloadInfo.duration = attrs->offloadInfo.duration;
73     }
74 }
75 
AudioCommonPortToVdiPortVdi(const struct AudioPort * port,struct AudioPortVdi * vdiPort)76 int32_t AudioCommonPortToVdiPortVdi(const struct AudioPort *port, struct AudioPortVdi *vdiPort)
77 {
78     CHECK_NULL_PTR_RETURN_VALUE(vdiPort, HDF_ERR_INVALID_PARAM);
79     CHECK_NULL_PTR_RETURN_VALUE(port, HDF_ERR_INVALID_PARAM);
80 
81     vdiPort->dir = (enum AudioPortDirectionVdi)port->dir;
82     vdiPort->portId = port->portId;
83     vdiPort->portName = strdup(port->portName); // free by caller
84     if (vdiPort->portName == NULL) {
85         AUDIO_FUNC_LOGE("strdup fail, port->portName = %{public}s", port->portName);
86         return HDF_FAILURE;
87     }
88 
89     return HDF_SUCCESS;
90 }
91 
AudioFormatsToFormatsVdi(const enum AudioFormatVdi * vdiFormats,uint32_t vdiFormatNum,enum AudioFormat ** formats,uint32_t * formatsLen)92 static int32_t AudioFormatsToFormatsVdi(const enum AudioFormatVdi *vdiFormats, uint32_t vdiFormatNum,
93     enum AudioFormat **formats, uint32_t *formatsLen)
94 {
95     CHECK_NULL_PTR_RETURN_VALUE(vdiFormats, HDF_ERR_INVALID_PARAM);
96     CHECK_NULL_PTR_RETURN_VALUE(formats, HDF_ERR_INVALID_PARAM);
97     CHECK_NULL_PTR_RETURN_VALUE(formatsLen, HDF_ERR_INVALID_PARAM);
98 
99     if (vdiFormatNum >= AUDIO_FORMAT_NUM_MAX || vdiFormatNum == 0) {
100         AUDIO_FUNC_LOGE("VdiFormats to formats len fail");
101         return HDF_ERR_INVALID_PARAM;
102     }
103 
104     uint32_t size = vdiFormatNum * sizeof(enum AudioFormat);
105     enum AudioFormat *formatTmp = (enum AudioFormat *)OsalMemCalloc(size);  // free by caller
106     if (formatTmp == NULL) {
107         AUDIO_FUNC_LOGE("formatTmp malloc fail");
108         return HDF_ERR_MALLOC_FAIL;
109     }
110 
111     int32_t ret = memcpy_s((void*)formatTmp, size, (void*)vdiFormats, vdiFormatNum * sizeof(enum AudioFormatVdi));
112     if (ret != HDF_SUCCESS) {
113         OsalMemFree((void *)formatTmp);
114         AUDIO_FUNC_LOGE("format cpy fail=%{public}d", ret);
115         return HDF_FAILURE;
116     }
117 
118     *formats = formatTmp;
119     *formatsLen = size;
120     return HDF_SUCCESS;
121 }
122 
AudioReleaseSubPortsVdi(struct AudioSubPortCapability ** subPorts,uint32_t * subPortsLen)123 static void AudioReleaseSubPortsVdi(struct AudioSubPortCapability **subPorts, uint32_t *subPortsLen)
124 {
125     struct AudioSubPortCapability *subPortsTmp = NULL;
126 
127     CHECK_NULL_PTR_RETURN(subPorts);
128     CHECK_NULL_PTR_RETURN(subPortsLen);
129 
130     uint32_t subPortsNum = *subPortsLen / sizeof(struct AudioSubPortCapability);
131     if (subPortsNum >= AUDIO_SUB_PORT_NUM_MAX) {
132         AUDIO_FUNC_LOGE("AudioReleaseSubPortsVdi len fail");
133         return;
134     }
135 
136     subPortsTmp = *subPorts;
137     for (uint32_t i = 0; i < subPortsNum; i++) {
138         OsalMemFree((void *)subPortsTmp[i].desc);
139     }
140 
141     OsalMemFree((void *)subPortsTmp);
142     subPortsTmp = NULL;
143 }
144 
AudioSubPortsToSubPortsVdi(const struct AudioSubPortCapabilityVdi * vdiSubPorts,uint32_t vdiSubPortsNum,struct AudioSubPortCapability ** subPorts,uint32_t * subPortsLen)145 static int32_t AudioSubPortsToSubPortsVdi(const struct AudioSubPortCapabilityVdi *vdiSubPorts, uint32_t vdiSubPortsNum,
146     struct AudioSubPortCapability **subPorts, uint32_t *subPortsLen)
147 {
148     CHECK_NULL_PTR_RETURN_VALUE(vdiSubPorts, HDF_ERR_INVALID_PARAM);
149     CHECK_NULL_PTR_RETURN_VALUE(subPorts, HDF_ERR_INVALID_PARAM);
150     CHECK_NULL_PTR_RETURN_VALUE(subPortsLen, HDF_ERR_INVALID_PARAM);
151 
152     if (vdiSubPortsNum >= AUDIO_SUB_PORT_NUM_MAX || vdiSubPortsNum == 0) {
153         AUDIO_FUNC_LOGE("VdiSubPorts to subPorts len fail");
154         return HDF_ERR_INVALID_PARAM;
155     }
156 
157     uint32_t size = vdiSubPortsNum * sizeof(struct AudioSubPortCapability);
158     struct AudioSubPortCapability *subPortsTmp = (struct AudioSubPortCapability *)OsalMemCalloc(size);
159     if (subPortsTmp == NULL) {
160         AUDIO_FUNC_LOGE("subPortsTmp malloc fail");
161         return HDF_ERR_MALLOC_FAIL;
162     }
163 
164     for (uint32_t i = 0; i < vdiSubPortsNum; i++) {
165         subPortsTmp[i].portId = vdiSubPorts[i].portId;
166         subPortsTmp[i].mask = (enum AudioPortPassthroughMode)vdiSubPorts[i].mask;
167         subPortsTmp[i].desc = strdup(vdiSubPorts[i].desc);
168         if (subPortsTmp[i].desc == NULL) {
169             *subPorts = subPortsTmp;
170             *subPortsLen = size;
171             AUDIO_FUNC_LOGE("strdup fail, vdiSubPorts[%{public}d].desc = %{public}s", i, vdiSubPorts[i].desc);
172             return HDF_FAILURE;
173         }
174     }
175 
176     *subPorts = subPortsTmp;
177     *subPortsLen = size;
178     return HDF_SUCCESS;
179 }
180 
AudioSampleFormatToSampleFormatsVdi(const enum AudioSampleFormatVdi * vdiSampleFormat,uint32_t vdiSupportSampleFormatNum,enum AudioSampleFormat ** sampleFormat,uint32_t * sampleFormatsLen)181 static int32_t AudioSampleFormatToSampleFormatsVdi(const enum AudioSampleFormatVdi *vdiSampleFormat,
182     uint32_t vdiSupportSampleFormatNum, enum AudioSampleFormat **sampleFormat, uint32_t *sampleFormatsLen)
183 {
184     CHECK_NULL_PTR_RETURN_VALUE(vdiSampleFormat, HDF_ERR_INVALID_PARAM);
185     CHECK_NULL_PTR_RETURN_VALUE(sampleFormat, HDF_ERR_INVALID_PARAM);
186     CHECK_NULL_PTR_RETURN_VALUE(sampleFormatsLen, HDF_ERR_INVALID_PARAM);
187 
188     if (vdiSupportSampleFormatNum >= AUDIO_SAMPLE_FORMAT_NUM_MAX || vdiSupportSampleFormatNum == 0) {
189         AUDIO_FUNC_LOGE("vdiSampleFormat to sampleFormats len fail");
190         return HDF_ERR_INVALID_PARAM;
191     }
192 
193     uint32_t size = vdiSupportSampleFormatNum * sizeof(enum AudioSampleFormat);
194     enum AudioSampleFormat *sampleFormatTmp = (enum AudioSampleFormat *)OsalMemCalloc(size);
195     if (sampleFormatTmp == NULL) {
196         AUDIO_FUNC_LOGE("sampleFormatTmp malloc fail");
197         return HDF_ERR_MALLOC_FAIL;
198     }
199 
200     int32_t ret = memcpy_s((void *)sampleFormatTmp, size, (void*)vdiSampleFormat,
201         vdiSupportSampleFormatNum * sizeof(enum AudioSampleFormatVdi));
202     if (ret != HDF_SUCCESS) {
203         OsalMemFree((void *)sampleFormatTmp);
204         AUDIO_FUNC_LOGE("sampleFormatTmp cpy fail=%{public}d", ret);
205         return HDF_FAILURE;
206     }
207 
208     *sampleFormat = sampleFormatTmp;
209     *sampleFormatsLen = size;
210     return HDF_SUCCESS;
211 }
212 
AudioCommonVdiPortCapToPortCapVdi(const struct AudioPortCapabilityVdi * vdiPortCap,struct AudioPortCapability * portCap)213 void AudioCommonVdiPortCapToPortCapVdi(const struct AudioPortCapabilityVdi *vdiPortCap,
214     struct AudioPortCapability *portCap)
215 {
216     CHECK_NULL_PTR_RETURN(portCap);
217     CHECK_NULL_PTR_RETURN(vdiPortCap);
218 
219     portCap->deviceType = vdiPortCap->deviceType;
220     portCap->deviceId = vdiPortCap->deviceId;
221     portCap->hardwareMode = vdiPortCap->hardwareMode;
222     portCap->sampleRateMasks = vdiPortCap->sampleRateMasks;
223     portCap->channelMasks = (enum AudioChannelMask)vdiPortCap->channelMasks;
224     portCap->channelCount = vdiPortCap->channelCount;
225 
226     int32_t ret = AudioFormatsToFormatsVdi(vdiPortCap->formats, vdiPortCap->formatNum, &portCap->formats,
227         &portCap->formatsLen);
228     if (ret != HDF_SUCCESS) {
229         AUDIO_FUNC_LOGE("AudioFormatsToFormatsVdi fail");
230         return;
231     }
232 
233     ret = AudioSubPortsToSubPortsVdi(vdiPortCap->subPorts, vdiPortCap->subPortsLen,
234         &portCap->subPorts, &portCap->subPortsLen);
235     if (ret != HDF_SUCCESS) {
236         OsalMemFree((void *)portCap->formats);
237         AudioReleaseSubPortsVdi(&portCap->subPorts, &portCap->subPortsLen);
238         portCap->formats = NULL;
239         AUDIO_FUNC_LOGE("VdiSubPortsToSubPorts fail");
240         return;
241     }
242 
243     ret = AudioSampleFormatToSampleFormatsVdi(vdiPortCap->supportSampleFormats, vdiPortCap->supportSampleFormatsLen,
244         &portCap->supportSampleFormats, &portCap->supportSampleFormatsLen);
245     if (ret != HDF_SUCCESS) {
246         OsalMemFree((void *)portCap->formats);
247         AudioReleaseSubPortsVdi(&portCap->subPorts, &portCap->subPortsLen);
248         portCap->formats = NULL;
249         AUDIO_FUNC_LOGE("VdiSampleFormatToSampleFormats fail");
250         return;
251     }
252 }
253 
AudioCommonFreeVdiRouteVdi(struct AudioRouteVdi * vdiRoute)254 void AudioCommonFreeVdiRouteVdi(struct AudioRouteVdi *vdiRoute)
255 {
256     CHECK_NULL_PTR_RETURN(vdiRoute);
257 
258     if (vdiRoute->sinks != NULL) {
259         if (vdiRoute->sinksLen > AUDIO_ROUTE_NUM_MAX) {
260             AUDIO_FUNC_LOGE("sinksLen para error");
261             return;
262         }
263 
264         for (uint32_t i = 0; i < vdiRoute->sinksLen; i++) {
265             if (vdiRoute->sinks[i].type == AUDIO_VDI_PORT_DEVICE_TYPE) {
266                 OsalMemFree((void *)vdiRoute->sinks[i].ext.device.desc);
267             }
268         }
269         OsalMemFree((void *)vdiRoute->sinks);
270     }
271 
272     if (vdiRoute->sources != NULL) {
273         if (vdiRoute->sourcesLen > AUDIO_ROUTE_NUM_MAX) {
274             AUDIO_FUNC_LOGE("sourcesLen para error");
275             return;
276         }
277 
278         for (uint32_t i = 0; i < vdiRoute->sourcesLen; i++) {
279             if (vdiRoute->sources[i].type == AUDIO_VDI_PORT_DEVICE_TYPE) {
280                 OsalMemFree((void *)vdiRoute->sources[i].ext.device.desc);
281             }
282         }
283         OsalMemFree((void *)vdiRoute->sources);
284     }
285 }
286 
AudioCommonRouteNodeToVdiRouteNodeVdi(struct AudioRouteNode * routeNode,struct AudioRouteNodeVdi * vdiRouteNode)287 static int32_t AudioCommonRouteNodeToVdiRouteNodeVdi(struct AudioRouteNode *routeNode,
288     struct AudioRouteNodeVdi *vdiRouteNode)
289 {
290     vdiRouteNode->portId = routeNode->portId;
291     vdiRouteNode->role = (enum AudioPortRoleVdi)routeNode->role;
292     vdiRouteNode->type = (enum AudioPortTypeVdi)routeNode->type;
293 
294     if (routeNode->type == AUDIO_VDI_PORT_DEVICE_TYPE) {
295         vdiRouteNode->ext.device.moduleId = routeNode->ext.device.moduleId;
296         vdiRouteNode->ext.device.type = (enum AudioPortPinVdi)routeNode->ext.device.type;
297         vdiRouteNode->ext.device.desc = strdup(routeNode->ext.device.desc);
298         if (vdiRouteNode->ext.device.desc == NULL) {
299             AUDIO_FUNC_LOGE("strdup fail, routeNode->ext.device.desc = %{public}s", routeNode->ext.device.desc);
300             return HDF_FAILURE;
301         }
302         return HDF_SUCCESS;
303     }
304 
305     if (routeNode->type == AUDIO_VDI_PORT_MIX_TYPE) {
306         vdiRouteNode->ext.mix.moduleId = routeNode->ext.mix.moduleId;
307         vdiRouteNode->ext.mix.streamId = routeNode->ext.mix.streamId;
308         vdiRouteNode->ext.mix.source = routeNode->ext.mix.source;
309         return HDF_SUCCESS;
310     }
311 
312     if (routeNode->type == AUDIO_VDI_PORT_SESSION_TYPE) {
313         vdiRouteNode->ext.session.sessionType = (enum AudioSessionTypeVdi)routeNode->ext.session.sessionType;
314         return HDF_SUCCESS;
315     }
316 
317     AUDIO_FUNC_LOGE("not match route node type");
318     return HDF_FAILURE;
319 }
320 
AudioCommonSinkToVdiSinkVdi(const struct AudioRoute * route,struct AudioRouteVdi * vdiRoute)321 static int32_t AudioCommonSinkToVdiSinkVdi(const struct AudioRoute *route, struct AudioRouteVdi *vdiRoute)
322 {
323     struct AudioRouteNodeVdi *nodes = NULL;
324     if (route->sinksLen > AUDIO_ROUTE_NUM_MAX) {
325         AUDIO_FUNC_LOGE("sinksLen para err");
326         return HDF_ERR_INVALID_PARAM;
327     }
328 
329     nodes = (struct AudioRouteNodeVdi *)OsalMemCalloc(route->sinksLen * sizeof(struct AudioRouteNodeVdi));
330     if (nodes == NULL) {
331         AUDIO_FUNC_LOGE("nodes null");
332         return HDF_ERR_MALLOC_FAIL;
333     }
334     vdiRoute->sinks = nodes;
335     vdiRoute->sinksLen = route->sinksLen;
336 
337     for (uint32_t i = 0; i < vdiRoute->sinksLen; i++) {
338         int32_t ret = AudioCommonRouteNodeToVdiRouteNodeVdi(&route->sinks[i], &vdiRoute->sinks[i]);
339         if (ret != HDF_SUCCESS) {
340             AUDIO_FUNC_LOGE("sink routeNode to vdiRouteNode fail");
341             /* nodes release by AudioCommonFreeVdiRouteVdi */
342             return HDF_FAILURE;
343         }
344     }
345 
346     return HDF_SUCCESS;
347 }
348 
AudioCommonSourceToVdiSourceVdi(const struct AudioRoute * route,struct AudioRouteVdi * vdiRoute)349 static int32_t AudioCommonSourceToVdiSourceVdi(const struct AudioRoute *route, struct AudioRouteVdi *vdiRoute)
350 {
351     struct AudioRouteNodeVdi *nodes = NULL;
352     if (route->sourcesLen > AUDIO_ROUTE_NUM_MAX) {
353         AUDIO_FUNC_LOGE("sinksLen para err");
354         return HDF_ERR_INVALID_PARAM;
355     }
356 
357     nodes = (struct AudioRouteNodeVdi *)OsalMemCalloc(route->sourcesLen * sizeof(struct AudioRouteNodeVdi));
358     if (nodes == NULL) {
359         AUDIO_FUNC_LOGE("nodes null");
360         return HDF_ERR_MALLOC_FAIL;
361     }
362     vdiRoute->sources = nodes;
363     vdiRoute->sourcesLen = route->sourcesLen;
364 
365     for (uint32_t i = 0; i < vdiRoute->sourcesLen; i++) {
366         int32_t ret = AudioCommonRouteNodeToVdiRouteNodeVdi(&route->sources[i], &vdiRoute->sources[i]);
367         if (ret != HDF_SUCCESS) {
368             AUDIO_FUNC_LOGE(" source routeNode to vdiRouteNode fail");
369             /* nodes release by AudioCommonFreeVdiRouteVdi */
370             return HDF_FAILURE;
371         }
372     }
373 
374     return HDF_SUCCESS;
375 }
376 
AudioCommonRouteToVdiRouteVdi(const struct AudioRoute * route,struct AudioRouteVdi * vdiRoute)377 int32_t AudioCommonRouteToVdiRouteVdi(const struct AudioRoute *route, struct AudioRouteVdi *vdiRoute)
378 {
379     int32_t sinkRet = HDF_SUCCESS;
380     int32_t sourcesRet = HDF_SUCCESS;
381 
382     CHECK_NULL_PTR_RETURN_VALUE(route, HDF_ERR_INVALID_PARAM);
383     CHECK_NULL_PTR_RETURN_VALUE(vdiRoute, HDF_ERR_INVALID_PARAM);
384 
385     if (route->sinks != NULL && route->sinksLen > 0) {
386         sinkRet = AudioCommonSinkToVdiSinkVdi(route, vdiRoute);
387         if (sinkRet != HDF_SUCCESS) {
388             AUDIO_FUNC_LOGE(" sink routeNode to vdiRouteNode fail");
389         }
390     }
391 
392     if (route->sources != NULL && route->sourcesLen > 0) {
393         sourcesRet = AudioCommonSourceToVdiSourceVdi(route, vdiRoute);
394         if (sourcesRet != HDF_SUCCESS) {
395             AUDIO_FUNC_LOGE(" source routeNode to vdiRouteNode fail");
396         }
397     }
398 
399     if (sinkRet != HDF_SUCCESS || sourcesRet != HDF_SUCCESS) {
400         /* free nodes by sink and source malloc nodes memory */
401         AudioCommonFreeVdiRouteVdi(vdiRoute);
402         return HDF_FAILURE;
403     }
404 
405     return HDF_SUCCESS;
406 }
407 
AudioCommonSceneToVdiSceneVdi(const struct AudioSceneDescriptor * scene,struct AudioSceneDescriptorVdi * vdiScene)408 int32_t AudioCommonSceneToVdiSceneVdi(const struct AudioSceneDescriptor *scene,
409     struct AudioSceneDescriptorVdi *vdiScene)
410 {
411     CHECK_NULL_PTR_RETURN_VALUE(scene, HDF_ERR_INVALID_PARAM);
412     CHECK_NULL_PTR_RETURN_VALUE(vdiScene, HDF_ERR_INVALID_PARAM);
413 
414     vdiScene->scene.id = scene->scene.id;
415     return AudioCommonDevDescToVdiDevDescVdi(&scene->desc, &vdiScene->desc);
416 }
417 
AudioCommonSampleAttrToVdiSampleAttrVdi(const struct AudioSampleAttributes * attrs,struct AudioSampleAttributesVdi * vdiAttrs)418 int32_t AudioCommonSampleAttrToVdiSampleAttrVdi(const struct AudioSampleAttributes *attrs,
419     struct AudioSampleAttributesVdi *vdiAttrs)
420 {
421     CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
422     CHECK_NULL_PTR_RETURN_VALUE(vdiAttrs, HDF_ERR_INVALID_PARAM);
423 
424     vdiAttrs->type = (enum AudioCategoryVdi)attrs->type;
425     vdiAttrs->interleaved = attrs->interleaved;
426     vdiAttrs->format = (enum AudioFormatVdi)attrs->format;
427     vdiAttrs->sampleRate = attrs->sampleRate;
428     vdiAttrs->channelCount = attrs->channelCount;
429     vdiAttrs->channelLayout = attrs->channelLayout;
430     vdiAttrs->period = attrs->period;
431     vdiAttrs->frameSize = attrs->frameSize;
432     vdiAttrs->isBigEndian = attrs->isBigEndian;
433     vdiAttrs->isSignedData = attrs->isSignedData;
434     vdiAttrs->startThreshold = attrs->startThreshold;
435     vdiAttrs->stopThreshold = attrs->stopThreshold;
436     vdiAttrs->silenceThreshold = attrs->silenceThreshold;
437     vdiAttrs->streamId = attrs->streamId;
438     vdiAttrs->sourceType = attrs->sourceType;
439     if (vdiAttrs->type == AUDIO_VDI_OFFLOAD) {
440         vdiAttrs->offloadInfo.sampleRate = attrs->offloadInfo.sampleRate;
441         vdiAttrs->offloadInfo.channelCount = attrs->offloadInfo.channelCount;
442         vdiAttrs->offloadInfo.channelLayout = attrs->offloadInfo.channelLayout;
443         vdiAttrs->offloadInfo.bitRate = attrs->offloadInfo.bitRate;
444         vdiAttrs->offloadInfo.bitWidth = attrs->offloadInfo.bitWidth;
445         vdiAttrs->offloadInfo.format = (enum AudioFormatVdi)attrs->offloadInfo.format;
446         vdiAttrs->offloadInfo.offloadBufferSize = attrs->offloadInfo.offloadBufferSize;
447         vdiAttrs->offloadInfo.duration = attrs->offloadInfo.duration;
448     }
449     return HDF_SUCCESS;
450 }
451 
AudioCommonVdiSampleAttrToSampleAttrVdi(const struct AudioSampleAttributesVdi * vdiAttrs,struct AudioSampleAttributes * attrs)452 int32_t AudioCommonVdiSampleAttrToSampleAttrVdi(const struct AudioSampleAttributesVdi *vdiAttrs,
453     struct AudioSampleAttributes *attrs)
454 {
455     CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
456     CHECK_NULL_PTR_RETURN_VALUE(vdiAttrs, HDF_ERR_INVALID_PARAM);
457 
458     attrs->type = (enum AudioCategory)vdiAttrs->type;
459     attrs->interleaved = vdiAttrs->interleaved;
460     attrs->format = (enum AudioFormat)vdiAttrs->format;
461     attrs->sampleRate = vdiAttrs->sampleRate;
462     attrs->channelCount = vdiAttrs->channelCount;
463     attrs->period = vdiAttrs->period;
464     attrs->frameSize = vdiAttrs->frameSize;
465     attrs->isBigEndian = vdiAttrs->isBigEndian;
466     attrs->isSignedData = vdiAttrs->isSignedData;
467     attrs->startThreshold = vdiAttrs->startThreshold;
468     attrs->stopThreshold = vdiAttrs->stopThreshold;
469     attrs->silenceThreshold = vdiAttrs->silenceThreshold;
470     attrs->streamId = vdiAttrs->streamId;
471 
472     return HDF_SUCCESS;
473 }
474