1 /*
2 * Copyright (c) 2022 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
16 #include "audio_pathselect.h"
17 #include "cJSON.h"
18 #include "hdf_types.h"
19 #include "osal_mem.h"
20 #include "securec.h"
21 #include "audio_common.h"
22 #include "audio_internal.h"
23 #include "audio_uhdf_log.h"
24 #include "v1_0/audio_types.h"
25
26 #define HDF_LOG_TAG AUDIO_HDI_IMPL
27
28 #define SPEAKER "Speaker"
29 #define HEADPHONES "headphones"
30 #define MIC "mic"
31 #define HS_MIC "micHs"
32 #define JSON_UNPRINT 1
33
34 #define CJSONFILE_CONFIG_PATH HDF_CONFIG_DIR "/audio_paths.json"
35
36 static cJSON *g_cJsonObj = NULL;
37
38 /* Depend on Audio_types.h : enum AudioCategory */
39 enum AudioCategoryPathSel {
40 PATH_USE_IN_MEDIA = 0,
41 PATH_USE_IN_COMMUNICATION,
42 PATH_USE_TYPE_MAX,
43 };
44
45 enum AudioPortPinPathSel {
46 PATH_DEV_NONE = 0x0u, /* Invalid pin */
47 PATH_DEV_OUT_SPEAKER = 0x1u, /* Speaker output pin */
48 PATH_DEV_OUT_HEADSET = 0x2u, /* Wired headset pin for output */
49 PATH_DEV_OUT_LINEOUT = 0x4u, /* Line-out pin */
50 PATH_DEV_OUT_HDMI = 0x8u, /* HDMI output pin */
51 PATH_DEV_MID = 0x8000000u, /* Microphone input pin */
52 PATH_DEV_IN_MIC = 0x8000001u, /* Microphone input pin */
53 PATH_DEV_IN_HS_MIC = 0x8000002u, /* Wired headset microphone pin for input */
54 PATH_DEV_IN_LINEIN = 0x8000004u, /* Line-in pin */
55 PATH_DEV_MAX,
56 };
57
AudioPathSelGetConfToJsonObj()58 int32_t AudioPathSelGetConfToJsonObj()
59 {
60 FILE *fpJson = NULL;
61 char *pJsonStr = NULL;
62 if (g_cJsonObj != NULL) {
63 return HDF_SUCCESS;
64 }
65 fpJson = fopen(CJSONFILE_CONFIG_PATH, "r");
66 if (fpJson == NULL) {
67 AUDIO_FUNC_LOGE("audio_paths.json file fail!");
68 return HDF_FAILURE;
69 }
70 if (fseek(fpJson, 0, SEEK_END) != HDF_SUCCESS) {
71 AUDIO_FUNC_LOGE("fseek fail!");
72 (void)fclose(fpJson);
73 return HDF_FAILURE;
74 }
75 int32_t jsonStrSize = ftell(fpJson);
76 rewind(fpJson);
77 if (jsonStrSize <= 0) {
78 (void)fclose(fpJson);
79 return HDF_FAILURE;
80 }
81 pJsonStr = (char *)OsalMemCalloc(jsonStrSize + 1);
82 if (pJsonStr == NULL) {
83 (void)fclose(fpJson);
84 return HDF_FAILURE;
85 }
86 if (fread(pJsonStr, jsonStrSize, 1, fpJson) != 1) {
87 AUDIO_FUNC_LOGE("read to file fail!");
88 (void)fclose(fpJson);
89 fpJson = NULL;
90 AudioMemFree((void **)&pJsonStr);
91 return HDF_FAILURE;
92 }
93 (void)fclose(fpJson);
94 fpJson = NULL;
95 #ifndef JSON_UNPRINT
96 AUDIO_FUNC_LOGI("pJsonStr = %{public}s", pJsonStr);
97 #endif
98 g_cJsonObj = cJSON_Parse(pJsonStr);
99 if (g_cJsonObj == NULL) {
100 AUDIO_FUNC_LOGE("cJSON_GetErrorPtr() = %{public}s", cJSON_GetErrorPtr());
101 AudioMemFree((void **)&pJsonStr);
102 return HDF_FAILURE;
103 }
104 AudioMemFree((void **)&pJsonStr);
105 return HDF_SUCCESS;
106 }
107
AudioPathSelGetDeviceType(enum AudioPortPin pins)108 static const char *AudioPathSelGetDeviceType(enum AudioPortPin pins)
109 {
110 if (pins < PATH_DEV_NONE || pins > PATH_DEV_MAX) {
111 return NULL;
112 }
113 switch (pins) {
114 case PATH_DEV_OUT_SPEAKER:
115 return SPEAKER;
116 case PATH_DEV_OUT_HEADSET:
117 return HEADPHONES;
118 case PATH_DEV_IN_MIC:
119 return MIC;
120 case PATH_DEV_IN_HS_MIC:
121 return HS_MIC;
122 default:
123 AUDIO_FUNC_LOGE("UseCase not support!");
124 break;
125 }
126 return NULL;
127 }
128
AudioPathSelGetUseCase(enum AudioCategory type)129 static const char *AudioPathSelGetUseCase(enum AudioCategory type)
130 {
131 static const char *usecaseType[PATH_USE_TYPE_MAX + 1] = {
132 [PATH_USE_IN_MEDIA] = "deep-buffer-playback",
133 [PATH_USE_IN_COMMUNICATION] = "low-latency-playback",
134 [PATH_USE_TYPE_MAX] = "none",
135 };
136
137 if (type < 0 || type > PATH_USE_TYPE_MAX) {
138 return NULL;
139 }
140 return usecaseType[type];
141 }
142
AudioCJsonCardServiceItemCheck(cJSON * cJsonObj,const char * firstItem,const char * secondItem,const char * thirdItem)143 static int32_t AudioCJsonCardServiceItemCheck(
144 cJSON *cJsonObj, const char *firstItem, const char *secondItem, const char *thirdItem)
145 {
146 if (cJsonObj == NULL || firstItem == NULL || secondItem == NULL || thirdItem == NULL) {
147 AUDIO_FUNC_LOGE("cJsonObj or firstItem or secondItem or thirdItem is NULL!");
148 return HDF_FAILURE;
149 }
150 cJSON *cardNode = cJSON_GetObjectItem(cJsonObj, firstItem);
151 if (cardNode == NULL) {
152 AUDIO_FUNC_LOGE("failed to check item when firstItem[%{public}s] gets object!", firstItem);
153 return HDF_FAILURE;
154 }
155 cJSON *cardList = cardNode->child;
156 if (cardList == NULL) {
157 AUDIO_FUNC_LOGE("no child when firstItem[%{public}s] gets object!", firstItem);
158 return HDF_FAILURE;
159 }
160
161 cJSON *pathNode = cJSON_GetObjectItem(cardList, secondItem);
162 if (pathNode == NULL) {
163 AUDIO_FUNC_LOGE("failed to check item when secondItem[%{public}s] gets object!", secondItem);
164 return HDF_ERR_NOT_SUPPORT;
165 }
166 cJSON *deviceNode = cJSON_GetObjectItem(cardList, thirdItem);
167 if (deviceNode == NULL) {
168 AUDIO_FUNC_LOGE("failed to check item when thirdItem[%{public}s] gets object!", thirdItem);
169 return HDF_ERR_NOT_SUPPORT;
170 }
171 return HDF_SUCCESS;
172 }
173
AudioPathSelGetPlanRenderScene(struct AudioHwRenderParam * renderSceneParam)174 static int32_t AudioPathSelGetPlanRenderScene(struct AudioHwRenderParam *renderSceneParam)
175 {
176 AUDIO_FUNC_LOGI();
177 int32_t ret;
178 if (renderSceneParam == NULL) {
179 AUDIO_FUNC_LOGE("AudioPathSelGetPlanRenderScene param Is NULL");
180 return HDF_FAILURE;
181 }
182 char pathName[PATH_NAME_LEN] = {0};
183 enum AudioPortPin pins = renderSceneParam->renderMode.hwInfo.deviceDescript.pins;
184 if (pins >= PATH_DEV_MAX || pins < PATH_DEV_NONE) {
185 AUDIO_FUNC_LOGE("deviceDescript pins error!");
186 return HDF_FAILURE;
187 }
188 enum AudioCategory type = renderSceneParam->frameRenderMode.attrs.type;
189 const char *useCase = AudioPathSelGetUseCase(type);
190 const char *deviceType = AudioPathSelGetDeviceType(pins);
191 if (useCase == NULL || deviceType == NULL) {
192 AUDIO_FUNC_LOGE("pins or type not support!");
193 return HDF_FAILURE;
194 }
195 if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "%s %s", useCase, deviceType) < 0) {
196 AUDIO_FUNC_LOGE("snprintf_s Invalid!");
197 return HDF_FAILURE;
198 }
199 ret = AudioCJsonCardServiceItemCheck(
200 g_cJsonObj, renderSceneParam->renderMode.hwInfo.cardServiceName, pathName, deviceType);
201 if (ret != HDF_SUCCESS) {
202 return ret;
203 }
204 ret = strncpy_s(renderSceneParam->renderMode.hwInfo.pathSelect.useCase, NAME_LEN, useCase, strlen(useCase) + 1);
205 if (ret != 0) {
206 AUDIO_FUNC_LOGE("strncpy_s failed!");
207 return HDF_FAILURE;
208 }
209 ret = strncpy_s(renderSceneParam->renderMode.hwInfo.pathSelect.deviceInfo.deviceType, NAME_LEN, deviceType,
210 strlen(deviceType) + 1);
211 if (ret != 0) {
212 AUDIO_FUNC_LOGE("strncpy_s failed!");
213 return HDF_FAILURE;
214 }
215 return HDF_SUCCESS;
216 }
217
AudioPathSelGetPlanCaptureScene(struct AudioHwCaptureParam * captureSceneParam)218 static int32_t AudioPathSelGetPlanCaptureScene(struct AudioHwCaptureParam *captureSceneParam)
219 {
220 AUDIO_FUNC_LOGI();
221 int32_t ret;
222 if (captureSceneParam == NULL) {
223 AUDIO_FUNC_LOGE("AudioPathSelGetPlanCaptureScene param Is NULL");
224 return HDF_FAILURE;
225 }
226 char pathName[PATH_NAME_LEN] = {0};
227 enum AudioPortPin pins = captureSceneParam->captureMode.hwInfo.deviceDescript.pins;
228 if (pins >= PATH_DEV_MAX || pins < PATH_DEV_MID) {
229 AUDIO_FUNC_LOGE("deviceDescript pins error!");
230 return HDF_FAILURE;
231 }
232 enum AudioCategory type = captureSceneParam->frameCaptureMode.attrs.type;
233 const char *useCase = AudioPathSelGetUseCase(type);
234 const char *deviceType = AudioPathSelGetDeviceType(pins);
235 if (useCase == NULL || deviceType == NULL) {
236 AUDIO_FUNC_LOGE("pins or type not support!");
237 return HDF_FAILURE;
238 }
239 if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "%s %s", useCase, deviceType) < 0) {
240 AUDIO_FUNC_LOGE("snprintf_s failed!");
241 return HDF_FAILURE;
242 }
243 ret = AudioCJsonCardServiceItemCheck(
244 g_cJsonObj, captureSceneParam->captureMode.hwInfo.cardServiceName, pathName, deviceType);
245 if (ret != HDF_SUCCESS) {
246 return ret;
247 }
248 ret = strncpy_s(captureSceneParam->captureMode.hwInfo.pathSelect.useCase, NAME_LEN, useCase, strlen(useCase) + 1);
249 if (ret != 0) {
250 AUDIO_FUNC_LOGE("strncpy_s failed!");
251 return HDF_FAILURE;
252 }
253 ret = strncpy_s(captureSceneParam->captureMode.hwInfo.pathSelect.deviceInfo.deviceType, NAME_LEN, deviceType,
254 strlen(deviceType) + 1);
255 if (ret != 0) {
256 AUDIO_FUNC_LOGE("strncpy_s failed!");
257 return HDF_FAILURE;
258 }
259 return HDF_SUCCESS;
260 }
261
AudioCJsonParseGetSubItem(cJSON * cJsonObj,const char * firstItem,const char * secondItem,cJSON ** deviceList)262 static int32_t AudioCJsonParseGetSubItem(
263 cJSON *cJsonObj, const char *firstItem, const char *secondItem, cJSON **deviceList)
264 {
265 if (cJsonObj == NULL || firstItem == NULL || secondItem == NULL || deviceList == NULL) {
266 AUDIO_FUNC_LOGE("cJsonObj or firstItem or secondItem or deviceList is null");
267 return HDF_FAILURE;
268 }
269 cJSON *cardNode = cJSON_GetObjectItem(cJsonObj, firstItem);
270 if (cardNode == NULL) {
271 AUDIO_FUNC_LOGE("firstItem[%{public}s] Get Object Fail!", firstItem);
272 return HDF_FAILURE;
273 }
274
275 cJSON *cardList = cardNode->child;
276 if (cardList == NULL) {
277 AUDIO_FUNC_LOGE("firstItem[%{public}s] no child!", firstItem);
278 return HDF_FAILURE;
279 }
280
281 cJSON *deviceNode = cJSON_GetObjectItem(cardList, secondItem);
282 if (deviceNode == NULL) {
283 AUDIO_FUNC_LOGE("secondItem[%{public}s] Get Object Fail!", secondItem);
284 return HDF_FAILURE;
285 }
286 *deviceList = deviceNode->child;
287 if (*deviceList == NULL) {
288 AUDIO_FUNC_LOGE("deviceList is NULL!");
289 return HDF_FAILURE;
290 }
291 return HDF_SUCCESS;
292 }
293
AudioCapturePathSelGetUsecaseDevice(struct AudioHwCaptureParam * captureParam,const char * pathName)294 static int32_t AudioCapturePathSelGetUsecaseDevice(struct AudioHwCaptureParam *captureParam, const char *pathName)
295 {
296 if (captureParam == NULL || pathName == NULL || g_cJsonObj == NULL) {
297 AUDIO_FUNC_LOGE("AudioCapturePathSelGetUsecaseDevice param Is NULL");
298 return HDF_FAILURE;
299 }
300 int32_t pathIndex = 0;
301 char *pathKey = NULL;
302 int32_t ret;
303
304 cJSON *pathList = NULL;
305 ret = AudioCJsonParseGetSubItem(g_cJsonObj, captureParam->captureMode.hwInfo.cardServiceName, pathName, &pathList);
306 if (ret != HDF_SUCCESS) {
307 return ret;
308 }
309 while (pathList != NULL) {
310 cJSON *device = cJSON_GetObjectItem(pathList, "name");
311 if (device == NULL) {
312 AUDIO_FUNC_LOGE("Get Object Invalid!");
313 return HDF_FAILURE;
314 }
315 pathKey = device->valuestring;
316 if (pathKey == NULL) {
317 pathList = pathList->next;
318 continue;
319 }
320 device = cJSON_GetObjectItem(pathList, "value");
321 if (device == NULL) {
322 return HDF_FAILURE;
323 }
324 captureParam->captureMode.hwInfo.pathSelect.pathPlan[pathIndex].value = device->valueint;
325 ret = strncpy_s(captureParam->captureMode.hwInfo.pathSelect.pathPlan[pathIndex].pathPlanName, PATHPLAN_LEN,
326 pathKey, strlen(pathKey) + 1);
327 if (ret != 0) {
328 AUDIO_FUNC_LOGE("strncpy_s failed!");
329 return HDF_FAILURE;
330 }
331 pathList = pathList->next;
332 pathIndex++;
333 }
334 if (pathIndex >= PATHPLAN_COUNT || pathIndex < 0) {
335 AUDIO_FUNC_LOGE("AudioCapturePathSel Get Object Fail!");
336 return HDF_FAILURE;
337 }
338 captureParam->captureMode.hwInfo.pathSelect.useCaseDeviceNum = pathIndex;
339 return HDF_SUCCESS;
340 }
341
AudioCapturePathSelGetDeviceSplit(struct AudioHwCaptureParam * captureParam,cJSON * deviceList)342 static int32_t AudioCapturePathSelGetDeviceSplit(struct AudioHwCaptureParam *captureParam, cJSON *deviceList)
343 {
344 AUDIO_FUNC_LOGI();
345 int32_t decIndex = 0;
346 char *decKey = NULL;
347 int32_t decValue;
348 int32_t ret;
349 cJSON *devObj = NULL;
350 if (captureParam == NULL || deviceList == NULL) {
351 AUDIO_FUNC_LOGE("param Is NULL");
352 return HDF_FAILURE;
353 }
354 while (deviceList != NULL) {
355 devObj = cJSON_GetObjectItem(deviceList, "name");
356 if (devObj == NULL) {
357 AUDIO_FUNC_LOGE("Get Object Fail!");
358 return HDF_FAILURE;
359 }
360 decKey = devObj->valuestring;
361 if (decKey == NULL) {
362 deviceList = deviceList->next;
363 continue;
364 }
365 devObj = cJSON_GetObjectItem(deviceList, "value");
366 if (devObj == NULL) {
367 return HDF_FAILURE;
368 }
369 decValue = devObj->valueint;
370 captureParam->captureMode.hwInfo.pathSelect.deviceInfo.deviceSwitchs[decIndex].value = decValue;
371 ret = strncpy_s(captureParam->captureMode.hwInfo.pathSelect.deviceInfo.deviceSwitchs[decIndex].deviceSwitch,
372 PATHPLAN_LEN, decKey, strlen(decKey) + 1);
373 if (ret != 0) {
374 AUDIO_FUNC_LOGE("strncpy_s failed!");
375 return HDF_FAILURE;
376 }
377 deviceList = deviceList->next;
378 decIndex++;
379 }
380 if (decIndex >= PATHPLAN_COUNT || decIndex < 0) {
381 AUDIO_FUNC_LOGE("Get Object Fail!");
382 return HDF_FAILURE;
383 }
384 captureParam->captureMode.hwInfo.pathSelect.deviceInfo.deviceNum = decIndex;
385 return HDF_SUCCESS;
386 }
387
AudioCapturePathSelGetDevice(struct AudioHwCaptureParam * captureParam,const char * deviceType)388 static int32_t AudioCapturePathSelGetDevice(struct AudioHwCaptureParam *captureParam, const char *deviceType)
389 {
390 AUDIO_FUNC_LOGI();
391 if (captureParam == NULL || deviceType == NULL || g_cJsonObj == NULL) {
392 AUDIO_FUNC_LOGE("AudioCapturePathSelGetUsecaseDevice param Is NULL");
393 return HDF_FAILURE;
394 }
395
396 cJSON *cardNode = cJSON_GetObjectItem(g_cJsonObj, captureParam->captureMode.hwInfo.cardServiceName);
397 if (cardNode == NULL) {
398 AUDIO_FUNC_LOGE("cardNode Get Object Fail cardServiceName = %{public}s !",
399 captureParam->captureMode.hwInfo.cardServiceName);
400 return HDF_FAILURE;
401 }
402 cJSON *cardList = cardNode->child;
403 if (cardList == NULL) {
404 AUDIO_FUNC_LOGE("AudioRenderPathSel Get cardList Fail!");
405 return HDF_FAILURE;
406 }
407
408 cJSON *deviceNode = cJSON_GetObjectItem(cardList, deviceType);
409 if (deviceNode == NULL) {
410 AUDIO_FUNC_LOGE("Get deviceType Fail!");
411 return HDF_FAILURE;
412 }
413 cJSON *deviceList = deviceNode->child;
414 if (deviceList == NULL) {
415 AUDIO_FUNC_LOGE("Get deviceList Fail!");
416 return HDF_FAILURE;
417 }
418 if (AudioCapturePathSelGetDeviceSplit(captureParam, deviceList) < 0) {
419 AUDIO_FUNC_LOGE("AudioCapturePathSelGetDeviceSplit Fail!");
420 return HDF_FAILURE;
421 }
422 return HDF_SUCCESS;
423 }
424
AudioPathSelGetPlanCapture(struct AudioHwCaptureParam * captureParam)425 static int32_t AudioPathSelGetPlanCapture(struct AudioHwCaptureParam *captureParam)
426 {
427 AUDIO_FUNC_LOGI();
428 int32_t ret;
429 if (captureParam == NULL) {
430 AUDIO_FUNC_LOGE("AudioPathSelGetPlanCapture param Is NULL");
431 return HDF_FAILURE;
432 }
433 char pathName[PATH_NAME_LEN] = {0};
434 enum AudioPortPin pins = captureParam->captureMode.hwInfo.deviceDescript.pins;
435 if (pins <= PATH_DEV_MID) {
436 AUDIO_FUNC_LOGE("deviceDescript pins error!");
437 return HDF_FAILURE;
438 }
439 enum AudioCategory type = captureParam->frameCaptureMode.attrs.type;
440 const char *useCase = AudioPathSelGetUseCase(type);
441 const char *deviceType = AudioPathSelGetDeviceType(pins);
442 if (useCase == NULL || deviceType == NULL) {
443 AUDIO_FUNC_LOGE("pins or type not support!");
444 return HDF_FAILURE;
445 }
446 ret = strncpy_s(captureParam->captureMode.hwInfo.pathSelect.useCase, NAME_LEN, useCase, strlen(useCase) + 1);
447 if (ret != 0) {
448 AUDIO_FUNC_LOGE("strncpy_s failed!");
449 return HDF_FAILURE;
450 }
451 ret = strncpy_s(captureParam->captureMode.hwInfo.pathSelect.deviceInfo.deviceType, NAME_LEN, deviceType,
452 strlen(deviceType) + 1);
453 if (ret != 0) {
454 AUDIO_FUNC_LOGE("strncpy_s failed!");
455 return HDF_FAILURE;
456 }
457 if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "%s %s", useCase, deviceType) < 0) {
458 AUDIO_FUNC_LOGE("snprintf_s failed!");
459 return HDF_FAILURE;
460 }
461 if (AudioCapturePathSelGetUsecaseDevice(captureParam, pathName) < 0) {
462 AUDIO_FUNC_LOGE("AudioCapturePathSelGetUsecaseDevice failed!");
463 return HDF_FAILURE;
464 }
465 if (AudioCapturePathSelGetDevice(captureParam, deviceType) < 0) {
466 AUDIO_FUNC_LOGE("AudioCapturePathSelGetDevice failed!");
467 return HDF_FAILURE;
468 }
469 return HDF_SUCCESS;
470 }
471
AudioRenderPathSelGetUsecaseDevice(struct AudioHwRenderParam * renderParam,const char * pathName)472 static int32_t AudioRenderPathSelGetUsecaseDevice(struct AudioHwRenderParam *renderParam, const char *pathName)
473 {
474 if (renderParam == NULL || pathName == NULL || g_cJsonObj == NULL) {
475 AUDIO_FUNC_LOGE("AudioPathSelGetUsecaseDevice param Is NULL");
476 return HDF_FAILURE;
477 }
478 int32_t pathIndex = 0;
479 char *pathKey = NULL;
480 int32_t ret;
481
482 cJSON *pathList = NULL;
483 ret = AudioCJsonParseGetSubItem(g_cJsonObj, renderParam->renderMode.hwInfo.cardServiceName, pathName, &pathList);
484 if (ret != HDF_SUCCESS) {
485 return ret;
486 }
487 while (pathList != NULL) {
488 cJSON *device = cJSON_GetObjectItem(pathList, "name");
489 if (device == NULL) {
490 AUDIO_FUNC_LOGE("Get Object Fail!");
491 return HDF_FAILURE;
492 }
493 pathKey = device->valuestring;
494 if (pathKey == NULL) {
495 pathList = pathList->next;
496 continue;
497 }
498 device = cJSON_GetObjectItem(pathList, "value");
499 if (device == NULL) {
500 return HDF_FAILURE;
501 }
502 renderParam->renderMode.hwInfo.pathSelect.pathPlan[pathIndex].value = device->valueint;
503 ret = strncpy_s(renderParam->renderMode.hwInfo.pathSelect.pathPlan[pathIndex].pathPlanName, PATHPLAN_LEN,
504 pathKey, strlen(pathKey));
505 if (ret != 0) {
506 AUDIO_FUNC_LOGE("strncpy_s is Fail!");
507 return HDF_FAILURE;
508 }
509 pathList = pathList->next;
510 pathIndex++;
511 }
512 if (pathIndex >= PATHPLAN_COUNT || pathIndex < 0) {
513 AUDIO_FUNC_LOGE("AudioRenderPathSel Get Object Fail!");
514 return HDF_FAILURE;
515 }
516 renderParam->renderMode.hwInfo.pathSelect.useCaseDeviceNum = pathIndex;
517 return HDF_SUCCESS;
518 }
519
AudioRenderPathSelGetDevice(struct AudioHwRenderParam * renderParam,const char * deviceType)520 static int32_t AudioRenderPathSelGetDevice(struct AudioHwRenderParam *renderParam, const char *deviceType)
521 {
522 if (renderParam == NULL || deviceType == NULL || g_cJsonObj == NULL) {
523 AUDIO_FUNC_LOGE("AudioPathSelGetDevice param Is NULL");
524 return HDF_FAILURE;
525 }
526 char *decKey = NULL;
527 int32_t decIndex = 0;
528 int32_t ret;
529
530 cJSON *deviceList = NULL;
531 ret =
532 AudioCJsonParseGetSubItem(g_cJsonObj, renderParam->renderMode.hwInfo.cardServiceName, deviceType, &deviceList);
533 if (ret != HDF_SUCCESS) {
534 return ret;
535 }
536 while (deviceList != NULL) {
537 cJSON *device = cJSON_GetObjectItem(deviceList, "name");
538 if (device == NULL) {
539 AUDIO_FUNC_LOGE("Get Object Invalid!");
540 return HDF_FAILURE;
541 }
542 decKey = device->valuestring;
543 if (decKey == NULL) {
544 deviceList = deviceList->next;
545 continue;
546 }
547 device = cJSON_GetObjectItem(deviceList, "value");
548 if (device == NULL) {
549 return HDF_FAILURE;
550 }
551 renderParam->renderMode.hwInfo.pathSelect.deviceInfo.deviceSwitchs[decIndex].value = device->valueint;
552 ret = strncpy_s(renderParam->renderMode.hwInfo.pathSelect.deviceInfo.deviceSwitchs[decIndex].deviceSwitch,
553 PATHPLAN_LEN, decKey, strlen(decKey) + 1);
554 if (ret != 0) {
555 AUDIO_FUNC_LOGE("strncpy_s is Fail!");
556 return HDF_FAILURE;
557 }
558 deviceList = deviceList->next;
559 decIndex++;
560 }
561 if (decIndex >= PATHPLAN_COUNT || decIndex < 0) {
562 AUDIO_FUNC_LOGE("Get Object Fail!");
563 return HDF_FAILURE;
564 }
565 renderParam->renderMode.hwInfo.pathSelect.deviceInfo.deviceNum = decIndex;
566 return HDF_SUCCESS;
567 }
568
AudioPathSelGetPlanRender(struct AudioHwRenderParam * renderParam)569 static int32_t AudioPathSelGetPlanRender(struct AudioHwRenderParam *renderParam)
570 {
571 AUDIO_FUNC_LOGI();
572 int32_t ret;
573 if (renderParam == NULL) {
574 AUDIO_FUNC_LOGE("AudioPathSelGetPlanRender param Is NULL");
575 return HDF_FAILURE;
576 }
577 char pathName[PATH_NAME_LEN] = {0};
578 enum AudioPortPin pins = renderParam->renderMode.hwInfo.deviceDescript.pins;
579 if (pins >= PATH_DEV_MID) {
580 AUDIO_FUNC_LOGE("deviceDescript pins error!");
581 return HDF_FAILURE;
582 }
583 enum AudioCategory type = renderParam->frameRenderMode.attrs.type;
584 const char *useCase = AudioPathSelGetUseCase(type);
585 const char *deviceType = AudioPathSelGetDeviceType(pins);
586 if (useCase == NULL || deviceType == NULL) {
587 AUDIO_FUNC_LOGE("pins or type not support!");
588 return HDF_FAILURE;
589 }
590 ret = strncpy_s(renderParam->renderMode.hwInfo.pathSelect.useCase, NAME_LEN, useCase, strlen(useCase) + 1);
591 if (ret != 0) {
592 AUDIO_FUNC_LOGE("strncpy_s is Fail!");
593 return HDF_FAILURE;
594 }
595 ret = strncpy_s(
596 renderParam->renderMode.hwInfo.pathSelect.deviceInfo.deviceType, NAME_LEN, deviceType, strlen(deviceType) + 1);
597 if (ret != 0) {
598 AUDIO_FUNC_LOGE("strncpy_s is Fail!");
599 return HDF_FAILURE;
600 }
601 if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "%s %s", useCase, deviceType) < 0) {
602 AUDIO_FUNC_LOGE("snprintf_s failed!");
603 return HDF_FAILURE;
604 }
605 if (AudioRenderPathSelGetUsecaseDevice(renderParam, pathName) < 0) {
606 AUDIO_FUNC_LOGE("AudioRenderPathSelGetUsecaseDevice failed!");
607 return HDF_FAILURE;
608 }
609 if (AudioRenderPathSelGetDevice(renderParam, deviceType) < 0) {
610 AUDIO_FUNC_LOGE("AudioRenderPathSelGetDevice failed!");
611 return HDF_FAILURE;
612 }
613 return HDF_SUCCESS;
614 }
615
AudioPathSelAnalysisJson(const AudioHandle adapterParam,enum AudioAdaptType adaptType)616 int32_t AudioPathSelAnalysisJson(const AudioHandle adapterParam, enum AudioAdaptType adaptType)
617 {
618 AUDIO_FUNC_LOGI();
619 if (adaptType < 0 || adapterParam == NULL) {
620 AUDIO_FUNC_LOGE("Param Invaild!");
621 return HDF_FAILURE;
622 }
623 struct AudioHwRenderParam *renderParam = NULL;
624 struct AudioHwCaptureParam *captureParam = NULL;
625 struct AudioHwRenderParam *renderSceneCheck = NULL;
626 struct AudioHwCaptureParam *captureScenceCheck = NULL;
627 switch (adaptType) {
628 case RENDER_PATH_SELECT:
629 renderParam = (struct AudioHwRenderParam *)adapterParam;
630 return (AudioPathSelGetPlanRender(renderParam));
631 case CAPTURE_PATH_SELECT:
632 captureParam = (struct AudioHwCaptureParam *)adapterParam;
633 return (AudioPathSelGetPlanCapture(captureParam));
634 /* Scene is supported */
635 case CHECKSCENE_PATH_SELECT:
636 renderSceneCheck = (struct AudioHwRenderParam *)adapterParam;
637 return (AudioPathSelGetPlanRenderScene(renderSceneCheck));
638 case CHECKSCENE_PATH_SELECT_CAPTURE:
639 captureScenceCheck = (struct AudioHwCaptureParam *)adapterParam;
640 return (AudioPathSelGetPlanCaptureScene(captureScenceCheck));
641 default:
642 AUDIO_FUNC_LOGE("Path select mode invalid");
643 break;
644 }
645 return HDF_FAILURE;
646 }
647