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 return HDF_FAILURE;
411 }
412 }
413
414 if (sinkRet != HDF_SUCCESS || sourcesRet != HDF_SUCCESS) {
415 /* free nodes by sink and source malloc nodes memory */
416 AudioCommonFreeVdiRouteVdi(vdiRoute);
417 return HDF_FAILURE;
418 }
419
420 return HDF_SUCCESS;
421 }
422
AudioCommonSceneToVdiSceneVdi(const struct AudioSceneDescriptor * scene,struct AudioSceneDescriptorVdi * vdiScene)423 int32_t AudioCommonSceneToVdiSceneVdi(const struct AudioSceneDescriptor *scene,
424 struct AudioSceneDescriptorVdi *vdiScene)
425 {
426 CHECK_NULL_PTR_RETURN_VALUE(scene, HDF_ERR_INVALID_PARAM);
427 CHECK_NULL_PTR_RETURN_VALUE(vdiScene, HDF_ERR_INVALID_PARAM);
428
429 vdiScene->scene.id = scene->scene.id;
430 return AudioCommonDevDescToVdiDevDescVdi(&scene->desc, &vdiScene->desc);
431 }
432
AudioCommonSampleAttrToVdiSampleAttrVdi(const struct AudioSampleAttributes * attrs,struct AudioSampleAttributesVdi * vdiAttrs)433 int32_t AudioCommonSampleAttrToVdiSampleAttrVdi(const struct AudioSampleAttributes *attrs,
434 struct AudioSampleAttributesVdi *vdiAttrs)
435 {
436 CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
437 CHECK_NULL_PTR_RETURN_VALUE(vdiAttrs, HDF_ERR_INVALID_PARAM);
438
439 vdiAttrs->type = (enum AudioCategoryVdi)attrs->type;
440 vdiAttrs->interleaved = attrs->interleaved;
441 vdiAttrs->format = (enum AudioFormatVdi)attrs->format;
442 vdiAttrs->sampleRate = attrs->sampleRate;
443 vdiAttrs->channelCount = attrs->channelCount;
444 vdiAttrs->channelLayout = attrs->channelLayout;
445 vdiAttrs->period = attrs->period;
446 vdiAttrs->frameSize = attrs->frameSize;
447 vdiAttrs->isBigEndian = attrs->isBigEndian;
448 vdiAttrs->isSignedData = attrs->isSignedData;
449 vdiAttrs->startThreshold = attrs->startThreshold;
450 vdiAttrs->stopThreshold = attrs->stopThreshold;
451 vdiAttrs->silenceThreshold = attrs->silenceThreshold;
452 vdiAttrs->streamId = attrs->streamId;
453 vdiAttrs->sourceType = attrs->sourceType;
454 if (vdiAttrs->type == AUDIO_VDI_OFFLOAD) {
455 vdiAttrs->offloadInfo.sampleRate = attrs->offloadInfo.sampleRate;
456 vdiAttrs->offloadInfo.channelCount = attrs->offloadInfo.channelCount;
457 vdiAttrs->offloadInfo.channelLayout = attrs->offloadInfo.channelLayout;
458 vdiAttrs->offloadInfo.bitRate = attrs->offloadInfo.bitRate;
459 vdiAttrs->offloadInfo.bitWidth = attrs->offloadInfo.bitWidth;
460 vdiAttrs->offloadInfo.format = (enum AudioFormatVdi)attrs->offloadInfo.format;
461 vdiAttrs->offloadInfo.offloadBufferSize = attrs->offloadInfo.offloadBufferSize;
462 vdiAttrs->offloadInfo.duration = attrs->offloadInfo.duration;
463 }
464 return HDF_SUCCESS;
465 }
466
AudioCommonVdiSampleAttrToSampleAttrVdi(const struct AudioSampleAttributesVdi * vdiAttrs,struct AudioSampleAttributes * attrs)467 int32_t AudioCommonVdiSampleAttrToSampleAttrVdi(const struct AudioSampleAttributesVdi *vdiAttrs,
468 struct AudioSampleAttributes *attrs)
469 {
470 CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
471 CHECK_NULL_PTR_RETURN_VALUE(vdiAttrs, HDF_ERR_INVALID_PARAM);
472
473 attrs->type = (enum AudioCategory)vdiAttrs->type;
474 attrs->interleaved = vdiAttrs->interleaved;
475 attrs->format = (enum AudioFormat)vdiAttrs->format;
476 attrs->sampleRate = vdiAttrs->sampleRate;
477 attrs->channelCount = vdiAttrs->channelCount;
478 attrs->period = vdiAttrs->period;
479 attrs->frameSize = vdiAttrs->frameSize;
480 attrs->isBigEndian = vdiAttrs->isBigEndian;
481 attrs->isSignedData = vdiAttrs->isSignedData;
482 attrs->startThreshold = vdiAttrs->startThreshold;
483 attrs->stopThreshold = vdiAttrs->stopThreshold;
484 attrs->silenceThreshold = vdiAttrs->silenceThreshold;
485 attrs->streamId = vdiAttrs->streamId;
486
487 return HDF_SUCCESS;
488 }
489
AudioCommonFrameInfoToVdiFrameInfoVdi(const struct AudioFrameLen * frameLen,struct AudioCaptureFrameInfoVdi * frameInfoVdi)490 int32_t AudioCommonFrameInfoToVdiFrameInfoVdi(const struct AudioFrameLen *frameLen,
491 struct AudioCaptureFrameInfoVdi *frameInfoVdi)
492 {
493 CHECK_NULL_PTR_RETURN_VALUE(frameLen, HDF_ERR_INVALID_PARAM);
494 CHECK_NULL_PTR_RETURN_VALUE(frameInfoVdi, HDF_ERR_INVALID_PARAM);
495
496 if (frameLen->frameLen <= 0 || frameLen->frameEcLen <= 0) {
497 AUDIO_FUNC_LOGE("frameLen len err, frameLen = [%{public}u], frameEcLen = [%{public}u]",
498 frameLen->frameLen, frameLen->frameEcLen);
499 return HDF_ERR_INVALID_PARAM;
500 }
501 frameInfoVdi->frameLen = frameLen->frameLen;
502 frameInfoVdi->frameEcLen = frameLen->frameEcLen;
503 frameInfoVdi->frame = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameLen->frameLen));
504 if (frameInfoVdi->frame == NULL) {
505 AUDIO_FUNC_LOGE("frameInfoVdi->frame null");
506 return HDF_ERR_MALLOC_FAIL;
507 }
508 frameInfoVdi->frameEc = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameLen->frameEcLen));
509 if (frameInfoVdi->frameEc == NULL) {
510 OsalMemFree((void *)frameInfoVdi->frame);
511 AUDIO_FUNC_LOGE("frameInfoVdi->frameEc null");
512 return HDF_ERR_MALLOC_FAIL;
513 }
514
515 return HDF_SUCCESS;
516 }
517
AudioCommonVdiFrameInfoToFrameInfoVdi(struct AudioCaptureFrameInfoVdi * frameInfoVdi,struct AudioCaptureFrameInfo * frameInfo)518 int32_t AudioCommonVdiFrameInfoToFrameInfoVdi(struct AudioCaptureFrameInfoVdi *frameInfoVdi,
519 struct AudioCaptureFrameInfo *frameInfo)
520 {
521 CHECK_NULL_PTR_RETURN_VALUE(frameInfo, HDF_ERR_INVALID_PARAM);
522 CHECK_NULL_PTR_RETURN_VALUE(frameInfoVdi, HDF_ERR_INVALID_PARAM);
523
524 if (frameInfoVdi->frameLen <= 0 || frameInfoVdi->frameEcLen <= 0 ||
525 frameInfoVdi->frameLen > AUDIO_FRAME_LEN_MAX || frameInfoVdi->frameEcLen > AUDIO_FRAME_LEN_MAX) {
526 AUDIO_FUNC_LOGE("frameLen len err, frameLen = [%{public}u], frameEcLen = [%{public}u]",
527 frameInfoVdi->frameLen, frameInfoVdi->frameEcLen);
528 return HDF_ERR_INVALID_PARAM;
529 }
530 frameInfo->frameLen = frameInfoVdi->frameLen;
531 frameInfo->frameEcLen = frameInfoVdi->frameEcLen;
532 frameInfo->frame = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameInfo->frameLen));
533 if (frameInfo->frame == NULL) {
534 frameInfo->frameLen = 0;
535 AUDIO_FUNC_LOGE("frameInfo->frame null");
536 return HDF_ERR_MALLOC_FAIL;
537 }
538 int32_t ret = memcpy_s(frameInfo->frame, (size_t)frameInfo->frameLen, frameInfoVdi->frame,
539 (size_t)frameInfoVdi->frameLen);
540 if (ret != HDF_SUCCESS) {
541 AUDIO_FUNC_LOGE("memcpy_s frame fail");
542 return HDF_FAILURE;
543 }
544
545 frameInfo->frameEc = (int8_t*)OsalMemCalloc(sizeof(int8_t) * (frameInfo->frameEcLen));
546 if (frameInfo->frameEc == NULL) {
547 frameInfo->frameLen = 0;
548 frameInfo->frameEcLen = 0;
549 OsalMemFree((void *)frameInfo->frame);
550 AUDIO_FUNC_LOGE("frameInfo->frameEc null");
551 return HDF_ERR_MALLOC_FAIL;
552 }
553 ret = memcpy_s(frameInfo->frameEc, (size_t)frameInfo->frameEcLen, frameInfoVdi->frameEc,
554 (size_t)frameInfoVdi->frameEcLen);
555 if (ret != HDF_SUCCESS) {
556 AUDIO_FUNC_LOGE("memcpy_s frameEc fail");
557 return HDF_FAILURE;
558 }
559 frameInfo->replyBytes = frameInfoVdi->replyBytes;
560 frameInfo->replyBytesEc = frameInfoVdi->replyBytesEc;
561
562 return HDF_SUCCESS;
563 }
564
SetThreadPriority(void)565 void SetThreadPriority(void)
566 {
567 struct sched_param param;
568 param.sched_priority = 1;
569 if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
570 AUDIO_FUNC_LOGE("failed to set thread priority");
571 }
572 }