• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include <dlfcn.h>
17 #include <limits.h>
18 #include <unistd.h>
19 #include "hdf_base.h"
20 #include "hdf_types.h"
21 #include "osal_mem.h"
22 #include "securec.h"
23 #include "stub_collector.h"
24 #include "audio_adapter_info_common.h"
25 #include "audio_common.h"
26 #include "audio_uhdf_log.h"
27 #include "audio_internal.h"
28 
29 #define HDF_LOG_TAG HDF_AUDIO_PRIMARY_IMPL
30 
31 BindServiceRenderPassthrough g_bindServiceRender = NULL;
32 InterfaceLibModeRenderPassthrough g_interfaceLibModeRender = NULL;
33 CloseServiceRenderPassthrough g_closeServiceRender = NULL;
34 
35 BindServiceCapturePassthrough g_bindServiceCapture = NULL;
36 InterfaceLibModeCapturePassthrough g_interfaceLibModeCapture = NULL;
37 CloseServiceCapturePassthrough g_closeServiceCapture = NULL;
38 
39 InterfaceLibModeGetAllCardInfo g_getAllCardInfo = NULL;
40 
41 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
42 PathSelGetConfToJsonObj g_pathSelGetConfToJsonObj = NULL;
43 PathSelAnalysisJson g_pathSelAnalysisJson = NULL;
44 #endif
45 
46 static const char *g_capturePassthroughPath = HDF_LIBRARY_FULL_PATH("libaudio_capture_adapter");
47 static const char *g_renderPassthroughPath = HDF_LIBRARY_FULL_PATH("libaudio_render_adapter");
48 
49 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
50 static const char *g_pathSelectPassthroughPath = HDF_LIBRARY_FULL_PATH("libaudio_path_select");
51 #endif
52 
53 static void *g_ptrCaptureHandle = NULL;
54 static void *g_ptrRenderHandle = NULL;
55 
56 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
57 static void *g_ptrPathSelHandle = NULL;
58 #endif
59 
AudioPassthroughGetBindServiceRender(void)60 BindServiceRenderPassthrough *AudioPassthroughGetBindServiceRender(void)
61 {
62     return &g_bindServiceRender;
63 }
64 
AudioPassthroughGetInterfaceLibModeRender(void)65 InterfaceLibModeRenderPassthrough *AudioPassthroughGetInterfaceLibModeRender(void)
66 {
67     return &g_interfaceLibModeRender;
68 }
69 
AudioPassthroughGetCloseServiceRender(void)70 CloseServiceRenderPassthrough *AudioPassthroughGetCloseServiceRender(void)
71 {
72     return &g_closeServiceRender;
73 }
74 
AudioPassthroughGetBindServiceCapture(void)75 BindServiceCapturePassthrough *AudioPassthroughGetBindServiceCapture(void)
76 {
77     return &g_bindServiceCapture;
78 }
79 
AudioPassthroughGetInterfaceLibModeCapture(void)80 InterfaceLibModeCapturePassthrough *AudioPassthroughGetInterfaceLibModeCapture(void)
81 {
82     return &g_interfaceLibModeCapture;
83 }
84 
AudioPassthroughGetCloseServiceCapture(void)85 CloseServiceCapturePassthrough *AudioPassthroughGetCloseServiceCapture(void)
86 {
87     return &g_closeServiceCapture;
88 }
89 
90 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
AudioPassthroughGetPathSelGetConfToJsonObj(void)91 PathSelGetConfToJsonObj *AudioPassthroughGetPathSelGetConfToJsonObj(void)
92 {
93     return &g_pathSelGetConfToJsonObj;
94 }
95 
AudioPassthroughGetPathSelAnalysisJson(void)96 PathSelAnalysisJson *AudioPassthroughGetPathSelAnalysisJson(void)
97 {
98     return &g_pathSelAnalysisJson;
99 }
100 #endif
101 
InitCapturePassthroughHandle(const char * capturePassthroughPath)102 static int32_t InitCapturePassthroughHandle(const char *capturePassthroughPath)
103 {
104     if (capturePassthroughPath == NULL) {
105         AUDIO_FUNC_LOGE("capturePassthroughPath is NULL");
106         return HDF_FAILURE;
107     }
108     char pathBuf[PATH_MAX] = {'\0'};
109     if (realpath(capturePassthroughPath, pathBuf) == NULL) {
110         return HDF_FAILURE;
111     }
112     if (g_ptrCaptureHandle == NULL) {
113         g_ptrCaptureHandle = dlopen(pathBuf, RTLD_LAZY);
114         if (g_ptrCaptureHandle == NULL) {
115             AUDIO_FUNC_LOGE("open lib capture so fail, reason:%{public}s", dlerror());
116             return HDF_FAILURE;
117         }
118         g_bindServiceCapture = dlsym(g_ptrCaptureHandle, "AudioBindService");
119         g_interfaceLibModeCapture = dlsym(g_ptrCaptureHandle, "AudioInterfaceLibModeCapture");
120         g_closeServiceCapture = dlsym(g_ptrCaptureHandle, "AudioCloseService");
121         if (g_getAllCardInfo == NULL) {
122             g_getAllCardInfo = dlsym(g_ptrCaptureHandle, "AudioGetAllCardInfo");
123         }
124         if (g_bindServiceCapture == NULL || g_interfaceLibModeCapture == NULL || g_closeServiceCapture == NULL) {
125             AUDIO_FUNC_LOGE("lib capture so func not found!");
126             AudioDlClose(&g_ptrCaptureHandle);
127             return HDF_FAILURE;
128         }
129     }
130     return HDF_SUCCESS;
131 }
132 
InitRenderPassthroughHandle(const char * renderPassthroughPath)133 static int32_t InitRenderPassthroughHandle(const char *renderPassthroughPath)
134 {
135     if (renderPassthroughPath == NULL) {
136         AUDIO_FUNC_LOGE("renderPassthroughPath is NULL");
137         return HDF_FAILURE;
138     }
139     char pathBuf[PATH_MAX] = {'\0'};
140     if (realpath(renderPassthroughPath, pathBuf) == NULL) {
141         return HDF_FAILURE;
142     }
143     if (g_ptrRenderHandle == NULL) {
144         g_ptrRenderHandle = dlopen(pathBuf, RTLD_LAZY);
145         if (g_ptrRenderHandle == NULL) {
146             AUDIO_FUNC_LOGE("open lib render so fail, reason:%{public}s", dlerror());
147             return HDF_FAILURE;
148         }
149         g_bindServiceRender = dlsym(g_ptrRenderHandle, "AudioBindService");
150         g_interfaceLibModeRender = dlsym(g_ptrRenderHandle, "AudioInterfaceLibModeRender");
151         g_closeServiceRender = dlsym(g_ptrRenderHandle, "AudioCloseService");
152         if (g_getAllCardInfo == NULL) {
153             g_getAllCardInfo = dlsym(g_ptrRenderHandle, "AudioGetAllCardInfo");
154         }
155         if (g_bindServiceRender == NULL || g_interfaceLibModeRender == NULL || g_closeServiceRender == NULL) {
156             AUDIO_FUNC_LOGE("lib render so func not found!");
157             AudioDlClose(&g_ptrRenderHandle);
158             return HDF_FAILURE;
159         }
160     }
161     return HDF_SUCCESS;
162 }
163 
164 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
InitPathSelectPassthroughHandle(const char * pathSelectPassthroughPath)165 static int32_t InitPathSelectPassthroughHandle(const char *pathSelectPassthroughPath)
166 {
167     if (pathSelectPassthroughPath == NULL) {
168         AUDIO_FUNC_LOGE("pathSelectPassthroughPath is NULL");
169         return HDF_FAILURE;
170     }
171     char pathBuf[PATH_MAX] = {'\0'};
172     if (realpath(pathSelectPassthroughPath, pathBuf) == NULL) {
173         return HDF_FAILURE;
174     }
175     if (g_ptrPathSelHandle == NULL) {
176         g_ptrPathSelHandle = dlopen(pathBuf, RTLD_LAZY);
177         if (g_ptrPathSelHandle == NULL) {
178             AUDIO_FUNC_LOGE("open lib PathSelct so fail, reason:%{public}s", dlerror());
179             return HDF_FAILURE;
180         }
181         g_pathSelGetConfToJsonObj = dlsym(g_ptrPathSelHandle, "AudioPathSelGetConfToJsonObj");
182         g_pathSelAnalysisJson = dlsym(g_ptrPathSelHandle, "AudioPathSelAnalysisJson");
183         if (g_pathSelGetConfToJsonObj == NULL || g_pathSelAnalysisJson == NULL) {
184             AUDIO_FUNC_LOGE("lib PathSelct so func not found!");
185             AudioDlClose(&g_ptrPathSelHandle);
186             return HDF_FAILURE;
187         }
188     }
189     return HDF_SUCCESS;
190 }
191 #endif
192 
AudioManagerServiceGetFreeAdapterPos(struct IAudioManager * manager,const char * adapterName)193 static int32_t AudioManagerServiceGetFreeAdapterPos(struct IAudioManager *manager, const char *adapterName)
194 {
195     int32_t i;
196     if (manager == NULL || adapterName == NULL || strlen(adapterName) == 0) {
197         AUDIO_FUNC_LOGE("Invalid input param!");
198         return SUPPORT_ADAPTER_NUM_MAX;
199     }
200 
201     struct AudioHwManager *audioManagerSer = (struct AudioHwManager *)manager;
202     for (i = 0; i < SUPPORT_ADAPTER_NUM_MAX; i++) {
203         if (!strncmp(adapterName, audioManagerSer->adapterInfos[i].adapterName, ADAPTER_NAME_LEN)) {
204             AUDIO_FUNC_LOGE("adapterName(%{public}s) already load!", adapterName);
205             return SUPPORT_ADAPTER_NUM_MAX;
206         }
207     }
208 
209     for (i = 0; i < SUPPORT_ADAPTER_NUM_MAX; i++) {
210         if (strlen(audioManagerSer->adapterInfos[i].adapterName) == 0 &&
211             audioManagerSer->adapterInfos[i].adapterServicePtr == NULL) {
212             return i;
213         }
214     }
215 
216     AUDIO_FUNC_LOGE("no free pos!");
217     return SUPPORT_ADAPTER_NUM_MAX;
218 }
219 
AudioManagerServiceAddAdapter(struct IAudioManager * manager,struct IAudioAdapter * adapter,int32_t pos)220 static int32_t AudioManagerServiceAddAdapter(
221     struct IAudioManager *manager, struct IAudioAdapter *adapter, int32_t pos)
222 {
223     struct AudioHwManager *hwManager = (struct AudioHwManager *)manager;
224     struct AudioHwAdapter *hwAdapter = (struct AudioHwAdapter *)adapter;
225 
226     if (hwManager == NULL) {
227         AUDIO_FUNC_LOGE("audioManagerSer is null!");
228         return AUDIO_ERR_INVALID_PARAM;
229     }
230     if (hwAdapter == NULL) {
231         AUDIO_FUNC_LOGE("adapterName is null!");
232         return AUDIO_ERR_INVALID_PARAM;
233     }
234     if (pos >= SUPPORT_ADAPTER_NUM_MAX) {
235         AUDIO_FUNC_LOGE("pos out of range!");
236         return AUDIO_ERR_INVALID_PARAM;
237     }
238     int32_t ret = strncpy_s(hwManager->adapterInfos[pos].adapterName, ADAPTER_NAME_LEN,
239         hwAdapter->adapterDescriptor.adapterName, strlen(hwAdapter->adapterDescriptor.adapterName));
240     if (ret != 0) {
241         AUDIO_FUNC_LOGE("strncpy_s for adapterName failed!");
242         return AUDIO_ERR_INTERNAL;
243     }
244 
245     hwManager->adapterInfos[pos].refCnt = 1; // first init set 1
246     hwManager->adapterInfos[pos].adapterServicePtr = hwAdapter;
247     AUDIO_FUNC_LOGI("load adaptername[%{public}s], refCount[1]", hwManager->adapterInfos[pos].adapterName);
248 
249     return AUDIO_SUCCESS;
250 }
251 
AudioManagerServiceFindAdapterPos(struct IAudioManager * manager,const char * adapterName)252 static uint32_t AudioManagerServiceFindAdapterPos(struct IAudioManager *manager, const char *adapterName)
253 {
254     uint32_t i;
255 
256     struct AudioHwManager *audioManagerSer = (struct AudioHwManager *)manager;
257     for (i = 0; i < SUPPORT_ADAPTER_NUM_MAX; i++) {
258         if (strncmp(adapterName, audioManagerSer->adapterInfos[i].adapterName, ADAPTER_NAME_LEN) == 0 &&
259             audioManagerSer->adapterInfos[i].adapterServicePtr != NULL) {
260             return i;
261         }
262     }
263 
264     AUDIO_FUNC_LOGI("can not find adapterName(%{public}s), malloc new pos", adapterName);
265     return SUPPORT_ADAPTER_NUM_MAX;
266 }
267 
AudioInitDynamicLib(void)268 static int32_t AudioInitDynamicLib(void)
269 {
270     if (g_capturePassthroughPath == NULL || g_renderPassthroughPath == NULL) {
271         AUDIO_FUNC_LOGE("sopath is error");
272         return AUDIO_ERR_INTERNAL;
273     }
274 
275     int32_t ret = InitCapturePassthroughHandle(g_capturePassthroughPath);
276     if (ret < 0) {
277         AUDIO_FUNC_LOGE("InitCapturePassthroughHandle FAIL!");
278         return AUDIO_ERR_INTERNAL;
279     }
280 
281     ret = InitRenderPassthroughHandle(g_renderPassthroughPath);
282     if (ret < 0) {
283         AUDIO_FUNC_LOGE("InitRenderPassthroughHandle FAIL!");
284         AudioDlClose(&g_ptrCaptureHandle);
285         return AUDIO_ERR_INTERNAL;
286     }
287 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
288     ret = InitPathSelectPassthroughHandle(g_pathSelectPassthroughPath);
289     if (ret < 0 || g_pathSelGetConfToJsonObj == NULL) {
290         AUDIO_FUNC_LOGE("InitPathSelectPassthroughHandle FAIL!");
291         AudioDlClose(&g_ptrRenderHandle);
292         AudioDlClose(&g_ptrCaptureHandle);
293         return AUDIO_ERR_INTERNAL;
294     }
295 
296     ret = g_pathSelGetConfToJsonObj();
297     if (ret < 0) {
298         AUDIO_FUNC_LOGE("g_pathSelGetConfToJsonObj FAIL!");
299         AudioDlClose(&g_ptrRenderHandle);
300         AudioDlClose(&g_ptrCaptureHandle);
301         AudioDlClose(&g_ptrPathSelHandle);
302         return AUDIO_ERR_INTERNAL;
303     }
304 #endif
305     return AUDIO_SUCCESS;
306 }
307 
AudioManagerGetAllAdapters(struct IAudioManager * manager,struct AudioAdapterDescriptor * descs,uint32_t * size)308 int32_t AudioManagerGetAllAdapters(struct IAudioManager *manager,
309     struct AudioAdapterDescriptor *descs, uint32_t *size)
310 {
311     AUDIO_FUNC_LOGI("enter!");
312     if (manager == NULL || descs == NULL || size == NULL) {
313         return AUDIO_ERR_INVALID_PARAM;
314     }
315 
316     int32_t ret = AudioInitDynamicLib();
317     if (ret != AUDIO_SUCCESS) {
318         AUDIO_FUNC_LOGE("AudioInitDynamicLib FAIL! ret = %{public}d", ret);
319         return ret;
320     }
321 
322     ret = AudioAdaptersForUser(g_getAllCardInfo, descs, size);
323     if (ret < 0) {
324         AUDIO_FUNC_LOGE("AudioAdaptersForUser FAIL!");
325         return AUDIO_ERR_NOTREADY;
326     }
327 
328     return AUDIO_SUCCESS;
329 }
330 
LoadAdapterImpl(const struct AudioAdapterDescriptor * desc,struct IAudioAdapter ** adapter)331 static int32_t LoadAdapterImpl(const struct AudioAdapterDescriptor *desc, struct IAudioAdapter **adapter)
332 {
333     if (desc == NULL || adapter == NULL) {
334         return AUDIO_ERR_INVALID_PARAM;
335     }
336     struct AudioHwAdapter *hwAdapter = (struct AudioHwAdapter *)OsalMemCalloc(sizeof(struct AudioHwAdapter));
337     if (hwAdapter == NULL) {
338         AUDIO_FUNC_LOGE("OsalMemCalloc AudioHwAdapter failed");
339         return AUDIO_ERR_MALLOC_FAIL;
340     }
341 
342     (void)memset_s(hwAdapter, sizeof(struct AudioHwAdapter), 0, sizeof(struct AudioHwAdapter));
343     hwAdapter->common.InitAllPorts = AudioAdapterInitAllPorts;
344     hwAdapter->common.CreateRender = AudioAdapterCreateRender;
345     hwAdapter->common.DestroyRender = AudioAdapterDestroyRender;
346     hwAdapter->common.CreateCapture = AudioAdapterCreateCapture;
347     hwAdapter->common.DestroyCapture = AudioAdapterDestroyCapture;
348     hwAdapter->common.GetPortCapability = AudioAdapterGetPortCapability;
349     hwAdapter->common.SetPassthroughMode = AudioAdapterSetPassthroughMode;
350     hwAdapter->common.GetPassthroughMode = AudioAdapterGetPassthroughMode;
351     hwAdapter->common.GetDeviceStatus = AudioAdapterGetDeviceStatus;
352     hwAdapter->adapterDescriptor = *desc;
353 
354     *adapter = &(hwAdapter->common);
355     return AUDIO_SUCCESS;
356 }
357 
LoadAdapterPrimary(const struct AudioAdapterDescriptor * desc,struct IAudioAdapter ** adapter)358 static int32_t LoadAdapterPrimary(const struct AudioAdapterDescriptor *desc, struct IAudioAdapter **adapter)
359 {
360     if (desc == NULL || adapter == NULL) {
361         AUDIO_FUNC_LOGE("primary desc or adapter is null");
362         return AUDIO_ERR_INVALID_PARAM;
363     }
364 
365     int32_t ret = LoadAdapterImpl(desc, adapter);
366     if (ret != AUDIO_SUCCESS) {
367         return ret;
368     }
369 
370     return AUDIO_SUCCESS;
371 }
372 
LoadAdapterUsb(const struct AudioAdapterDescriptor * desc,struct IAudioAdapter ** adapter)373 static int32_t LoadAdapterUsb(const struct AudioAdapterDescriptor *desc, struct IAudioAdapter **adapter)
374 {
375     if (desc == NULL || adapter == NULL) {
376         AUDIO_FUNC_LOGE("usb desc or adapter is null");
377         return AUDIO_ERR_INVALID_PARAM;
378     }
379 
380     int32_t ret = LoadAdapterImpl(desc, adapter);
381     if (ret != AUDIO_SUCCESS) {
382         return ret;
383     }
384 
385     return AUDIO_SUCCESS;
386 }
387 
LoadAdapterA2dp(const struct AudioAdapterDescriptor * desc,struct IAudioAdapter ** adapter)388 static int32_t LoadAdapterA2dp(const struct AudioAdapterDescriptor *desc, struct IAudioAdapter **adapter)
389 {
390     if (desc == NULL || adapter == NULL) {
391         return AUDIO_ERR_INVALID_PARAM;
392     }
393 
394     return AUDIO_ERR_NOT_SUPPORT;
395 }
396 
SelectAppropriateAdapter(enum AudioAdapterType adapterType,const struct AudioAdapterDescriptor * desc,struct IAudioAdapter ** adapter)397 static int32_t SelectAppropriateAdapter(
398     enum AudioAdapterType adapterType, const struct AudioAdapterDescriptor *desc, struct IAudioAdapter **adapter)
399 {
400     int32_t ret;
401 
402     if (desc == NULL || adapter == NULL) {
403         return AUDIO_ERR_INVALID_PARAM;
404     }
405     switch (adapterType) {
406         case AUDIO_ADAPTER_PRIMARY:
407         case AUDIO_ADAPTER_PRIMARY_EXT:
408         case AUDIO_ADAPTER_HDMI:
409             ret = LoadAdapterPrimary(desc, adapter);
410             if (ret != AUDIO_SUCCESS) {
411                 AUDIO_FUNC_LOGE("LoadAdapterPrimary failed.");
412                 return ret;
413             }
414             break;
415         case AUDIO_ADAPTER_USB:
416             ret = LoadAdapterUsb(desc, adapter);
417             if (ret != AUDIO_SUCCESS) {
418                 AUDIO_FUNC_LOGE("LoadAdapterUsb failed.");
419                 return ret;
420             }
421             break;
422         case AUDIO_ADAPTER_A2DP:
423             ret = LoadAdapterA2dp(desc, adapter);
424             if (ret != AUDIO_SUCCESS) {
425                 AUDIO_FUNC_LOGE("LoadAdapterA2dp failed.");
426                 return ret;
427             }
428             break;
429         default:
430             AUDIO_FUNC_LOGE("An unsupported Adapter.");
431             return AUDIO_ERR_NOT_SUPPORT;
432     }
433 
434     return AUDIO_SUCCESS;
435 }
436 
AudioManagerIncreaseAdapterRef(struct IAudioManager * manager,uint32_t infoIndex,struct IAudioAdapter ** adapter)437 static int32_t AudioManagerIncreaseAdapterRef(struct IAudioManager *manager, uint32_t infoIndex,
438     struct IAudioAdapter **adapter)
439 {
440     struct AudioHwManager *audioManagerSer = (struct AudioHwManager *)manager;
441 
442     if (audioManagerSer->adapterInfos[infoIndex].adapterServicePtr == NULL) {
443         AUDIO_FUNC_LOGE("Invalid adapterServicePtr param!");
444         return AUDIO_ERR_INVALID_PARAM;
445     }
446 
447     audioManagerSer->adapterInfos[infoIndex].refCnt++;
448     *adapter = &(audioManagerSer->adapterInfos[infoIndex].adapterServicePtr->common);
449 
450     AUDIO_FUNC_LOGI("load adaptername[%{public}s], refCount[%{public}d] increase",
451         audioManagerSer->adapterInfos[infoIndex].adapterName, audioManagerSer->adapterInfos[infoIndex].refCnt);
452     return AUDIO_SUCCESS;
453 }
454 
AudioManagerDecreaseAdapterRef(struct IAudioManager * manager,uint32_t infoIndex)455 static int32_t AudioManagerDecreaseAdapterRef(struct IAudioManager *manager, uint32_t infoIndex)
456 {
457     struct AudioHwManager *audioManagerSer = (struct AudioHwManager *)manager;
458 
459     if (audioManagerSer->adapterInfos[infoIndex].refCnt == 0) {
460         AUDIO_FUNC_LOGE("Invalid adapterInfos[%{public}d] had released", infoIndex);
461         return AUDIO_ERR_INVALID_PARAM;
462     }
463 
464     audioManagerSer->adapterInfos[infoIndex].refCnt--;
465     AUDIO_FUNC_LOGI("unload adaptername[%{public}s], refCount[%{public}d] decrease",
466         audioManagerSer->adapterInfos[infoIndex].adapterName, audioManagerSer->adapterInfos[infoIndex].refCnt);
467     return AUDIO_SUCCESS;
468 }
469 
AudioManagerEnforceClearRef(struct IAudioManager * manager,uint32_t infoIndex)470 static void AudioManagerEnforceClearRef(struct IAudioManager *manager, uint32_t infoIndex)
471 {
472     struct AudioHwManager *audioManagerSer = (struct AudioHwManager *)manager;
473     audioManagerSer->adapterInfos[infoIndex].refCnt = 0;
474 }
475 
AudioManagerServiceRemvAdapter(struct IAudioManager * manager,uint32_t pos)476 static int32_t AudioManagerServiceRemvAdapter(struct IAudioManager *manager, uint32_t pos)
477 {
478     if (manager == NULL || pos >= SUPPORT_ADAPTER_NUM_MAX) {
479         AUDIO_FUNC_LOGE("Invalid input param!");
480         return AUDIO_ERR_INVALID_PARAM;
481     }
482 
483     struct AudioHwManager *audioManagerSer = (struct AudioHwManager *)manager;
484     struct AudioHwAdapter *hwAdapter = (struct AudioHwAdapter *)audioManagerSer->adapterInfos[pos].adapterServicePtr;
485 
486     if (audioManagerSer->adapterInfos[pos].refCnt != 0) {
487         return AudioManagerDecreaseAdapterRef(manager, pos);
488     }
489 
490     StubCollectorRemoveObject(IAUDIOADAPTER_INTERFACE_DESC, hwAdapter);
491 
492     if (hwAdapter == NULL) {
493         AUDIO_FUNC_LOGE("manager == NULL || hwAdapter == NULL");
494         return AUDIO_ERR_INVALID_PARAM;
495     }
496     if (hwAdapter->portCapabilitys != NULL) {
497         uint32_t portsLen = hwAdapter->adapterDescriptor.portsLen;
498         uint32_t i = 0;
499         while (i < portsLen) {
500             if (&hwAdapter->portCapabilitys[i] != NULL) {
501                 AudioMemFree((void **)&hwAdapter->portCapabilitys[i].capability.subPorts);
502             }
503             i++;
504         }
505         AudioMemFree((void **)&hwAdapter->portCapabilitys);
506     }
507 
508     AudioMemFree((void **)&hwAdapter);
509     audioManagerSer->adapterInfos[pos].adapterServicePtr = NULL;
510     AUDIO_FUNC_LOGI("audio unload adapterName[%{public}s] success", audioManagerSer->adapterInfos[pos].adapterName);
511 
512     (void)memset_s(audioManagerSer->adapterInfos[pos].adapterName, ADAPTER_NAME_LEN, 0, ADAPTER_NAME_LEN);
513 
514     return AUDIO_SUCCESS;
515 }
516 
AudioManagerLoadAdapter(struct IAudioManager * manager,const struct AudioAdapterDescriptor * desc,struct IAudioAdapter ** adapter)517 int32_t AudioManagerLoadAdapter(
518     struct IAudioManager *manager, const struct AudioAdapterDescriptor *desc, struct IAudioAdapter **adapter)
519 {
520     if (manager == NULL || desc == NULL || desc->adapterName == NULL || desc->ports == NULL || adapter == NULL) {
521         AUDIO_FUNC_LOGE("Invalid input param!");
522         return AUDIO_ERR_INVALID_PARAM;
523     }
524 
525     AUDIO_FUNC_LOGI("adapter name %{public}s", desc->adapterName);
526     int32_t index = AudioAdapterExist(desc->adapterName);
527     if (index < 0) {
528         AUDIO_FUNC_LOGE("not supported this adapter %{public}s", desc->adapterName);
529         return AUDIO_ERR_INVALID_PARAM;
530     }
531 
532     uint32_t pos = AudioManagerServiceFindAdapterPos(manager, desc->adapterName);
533     if (pos < SUPPORT_ADAPTER_NUM_MAX) {
534         return AudioManagerIncreaseAdapterRef(manager, pos, adapter);
535     }
536 
537     pos = AudioManagerServiceGetFreeAdapterPos(manager, desc->adapterName);
538     if (pos >= SUPPORT_ADAPTER_NUM_MAX) {
539         AUDIO_FUNC_LOGE("AudioManagerServiceGetFreeAdapterPos failed!");
540         return HDF_FAILURE;
541     }
542 
543     enum AudioAdapterType sndCardType = MatchAdapterType(desc->adapterName, desc->ports[0].portId);
544     int32_t ret = SelectAppropriateAdapter(sndCardType, &(AudioAdapterGetConfigDescs()[index]), adapter);
545     if (ret != AUDIO_SUCCESS) {
546         AUDIO_FUNC_LOGE("Load adapter failed.");
547         return ret;
548     }
549 
550     ret = AudioManagerServiceAddAdapter(manager, *adapter, pos);
551     if (ret != AUDIO_SUCCESS) {
552         AUDIO_FUNC_LOGE("Add adapter to list failed.");
553         AudioManagerEnforceClearRef(manager, pos);
554         AudioManagerServiceRemvAdapter(manager, pos);
555         return ret;
556     }
557 
558     return AUDIO_SUCCESS;
559 }
560 
AudioManagerUnloadAdapter(struct IAudioManager * manager,const char * adapterName)561 int32_t AudioManagerUnloadAdapter(struct IAudioManager *manager, const char *adapterName)
562 {
563     if (manager == NULL || adapterName == NULL || strlen(adapterName) == 0) {
564         AUDIO_FUNC_LOGE("Invalid input param!");
565         return AUDIO_ERR_INVALID_PARAM;
566     }
567 
568     uint32_t pos = AudioManagerServiceFindAdapterPos(manager, adapterName);
569     if (pos >= SUPPORT_ADAPTER_NUM_MAX) {
570         AUDIO_FUNC_LOGE("AudioManagerServiceUnloadAdapter failed!");
571         return AUDIO_ERR_INVALID_PARAM;
572     }
573 
574     int ret = AudioManagerServiceRemvAdapter(manager, pos);
575     if (ret != AUDIO_SUCCESS) {
576         AUDIO_FUNC_LOGE("AudioManagerServiceRemvAdapter failed!");
577         return ret;
578     }
579 
580     AUDIO_FUNC_LOGI("AudioManagerUnloadAdapter success!");
581     return AUDIO_SUCCESS;
582 }
583 
ReleaseAudioManagerObject(struct IAudioManager * manager)584 int32_t ReleaseAudioManagerObject(struct IAudioManager *manager)
585 {
586     struct AudioHwManager *service = (struct AudioHwManager *)manager;
587     if (ReleaseAudioManagerObjectComm(manager)) {
588         OsalMemFree(service);
589         service = NULL;
590     }
591 
592     return AUDIO_SUCCESS;
593 }
594 
AudioManagerCreateIfInstance(void)595 struct IAudioManager *AudioManagerCreateIfInstance(void)
596 {
597     AUDIO_FUNC_LOGI("audio manager create if instance");
598     struct AudioHwManager *service = (struct AudioHwManager *)OsalMemCalloc(sizeof(struct AudioHwManager));
599     if (service == NULL) {
600         AUDIO_FUNC_LOGE("OsalMemCalloc service failed!");
601         return NULL;
602     }
603 
604     service->interface.GetAllAdapters = AudioManagerGetAllAdapters;
605     service->interface.LoadAdapter = AudioManagerLoadAdapter;
606     service->interface.UnloadAdapter = AudioManagerUnloadAdapter;
607     service->interface.ReleaseAudioManagerObject = ReleaseAudioManagerObject;
608 
609     return &(service->interface);
610 }
611 
AudioManagerDestroyIfInstance(struct IAudioManager * manager)612 int32_t AudioManagerDestroyIfInstance(struct IAudioManager *manager)
613 {
614     return ReleaseAudioManagerObject(manager);
615 }