• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_manager.h"
17 #include <limits.h>
18 #include "audio_adapter_info_common.h"
19 #include "audio_hal_log.h"
20 #include "audio_interface_lib_capture.h"
21 #include "audio_interface_lib_render.h"
22 
23 #define HDF_LOG_TAG hal_audio_manager
24 
25 struct AudioAdapterDescriptor *g_localAddrAudioAdapterOut = NULL; // add for Fuzz
26 int g_localAdapterNum = 0; // add for Fuzz
27 
28 BindServiceRenderSo g_bindServiceRender = NULL;
29 InterfaceLibModeRenderSo g_interfaceLibModeRender = NULL;
30 CloseServiceRenderSo g_closeServiceRender = NULL;
31 
32 BindServiceCaptureSo g_bindServiceCapture = NULL;
33 InterfaceLibModeCaptureSo g_interfaceLibModeCapture = NULL;
34 CloseServiceCaptureSo g_closeServiceCapture = NULL;
35 
36 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
37 PathSelGetConfToJsonObj g_pathSelGetConfToJsonObj = NULL;
38 PathSelAnalysisJson g_pathSelAnalysisJson = NULL;
39 #endif
40 
41 static const char *g_captureSoPath = HDF_LIBRARY_FULL_PATH("libhdi_audio_interface_lib_capture");
42 static const char *g_renderSoPath = HDF_LIBRARY_FULL_PATH("libhdi_audio_interface_lib_render");
43 
44 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
45 static const char *g_pathSelectSoPath = HDF_LIBRARY_FULL_PATH("libhdi_audio_path_select");
46 #endif
47 
48 static void *g_ptrCaptureHandle = NULL;
49 static void *g_ptrRenderHandle = NULL;
50 
51 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
52 static void *g_ptrPathSelHandle = NULL;
53 #endif
54 
AudioSoGetBindServiceRender(void)55 BindServiceRenderSo *AudioSoGetBindServiceRender(void)
56 {
57     return &g_bindServiceRender;
58 }
59 
AudioSoGetInterfaceLibModeRender(void)60 InterfaceLibModeRenderSo *AudioSoGetInterfaceLibModeRender(void)
61 {
62     return &g_interfaceLibModeRender;
63 }
64 
AudioSoGetCloseServiceRender(void)65 CloseServiceRenderSo *AudioSoGetCloseServiceRender(void)
66 {
67     return &g_closeServiceRender;
68 }
69 
AudioSoGetBindServiceCapture(void)70 BindServiceCaptureSo *AudioSoGetBindServiceCapture(void)
71 {
72     return &g_bindServiceCapture;
73 }
74 
AudioSoGetInterfaceLibModeCapture(void)75 InterfaceLibModeCaptureSo *AudioSoGetInterfaceLibModeCapture(void)
76 {
77     return &g_interfaceLibModeCapture;
78 }
79 
AudioSoGetCloseServiceCapture(void)80 CloseServiceCaptureSo *AudioSoGetCloseServiceCapture(void)
81 {
82     return &g_closeServiceCapture;
83 }
84 
85 
86 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
AudioSoGetPathSelGetConfToJsonObj(void)87 PathSelGetConfToJsonObj *AudioSoGetPathSelGetConfToJsonObj(void)
88 {
89     return &g_pathSelGetConfToJsonObj;
90 }
91 
AudioSoGetPathSelAnalysisJson(void)92 PathSelAnalysisJson *AudioSoGetPathSelAnalysisJson(void)
93 {
94     return &g_pathSelAnalysisJson;
95 }
96 #endif
97 
InitCaptureSoHandle(const char * captureSoPath)98 int32_t InitCaptureSoHandle(const char *captureSoPath)
99 {
100     if (captureSoPath == NULL) {
101         LOG_FUN_ERR("captureSoPath is NULL");
102         return HDF_FAILURE;
103     }
104     char pathBuf[PATH_MAX] = {'\0'};
105     if (realpath(captureSoPath, pathBuf) == NULL) {
106         return HDF_FAILURE;
107     }
108     if (g_ptrCaptureHandle == NULL) {
109         char *cPathBuf = pathBuf;
110         g_ptrCaptureHandle = dlopen(cPathBuf, RTLD_LAZY);
111         if (g_ptrCaptureHandle == NULL) {
112             LOG_FUN_ERR("open lib capture so fail, reason:%s", dlerror());
113             return HDF_FAILURE;
114         }
115         g_bindServiceCapture = dlsym(g_ptrCaptureHandle, "AudioBindServiceCapture");
116         g_interfaceLibModeCapture = dlsym(g_ptrCaptureHandle, "AudioInterfaceLibModeCapture");
117         g_closeServiceCapture = dlsym(g_ptrCaptureHandle, "AudioCloseServiceCapture");
118         if (g_bindServiceCapture == NULL || g_interfaceLibModeCapture == NULL || g_closeServiceCapture == NULL) {
119             LOG_FUN_ERR("lib capture so func not found!");
120             AudioDlClose(&g_ptrCaptureHandle);
121             return HDF_FAILURE;
122         }
123     }
124     return HDF_SUCCESS;
125 }
126 
InitRenderSoHandle(const char * renderSoPath)127 int32_t InitRenderSoHandle(const char *renderSoPath)
128 {
129     if (renderSoPath == NULL) {
130         LOG_FUN_ERR("renderSoPath is NULL");
131         return HDF_FAILURE;
132     }
133     char pathBuf[PATH_MAX] = {'\0'};
134     if (realpath(renderSoPath, pathBuf) == NULL) {
135         return HDF_FAILURE;
136     }
137     if (g_ptrRenderHandle == NULL) {
138         char *cPathBuf = pathBuf;
139         g_ptrRenderHandle = dlopen(cPathBuf, RTLD_LAZY);
140         if (g_ptrRenderHandle == NULL) {
141             LOG_FUN_ERR("open lib render so fail, reason:%s", dlerror());
142             return HDF_FAILURE;
143         }
144         g_bindServiceRender = dlsym(g_ptrRenderHandle, "AudioBindServiceRender");
145         g_interfaceLibModeRender = dlsym(g_ptrRenderHandle, "AudioInterfaceLibModeRender");
146         g_closeServiceRender = dlsym(g_ptrRenderHandle, "AudioCloseServiceRender");
147         if (g_bindServiceRender == NULL || g_interfaceLibModeRender == NULL || g_closeServiceRender == NULL) {
148             LOG_FUN_ERR("lib render so func not found!");
149             AudioDlClose(&g_ptrRenderHandle);
150             return HDF_FAILURE;
151         }
152     }
153     return HDF_SUCCESS;
154 }
155 
156 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
InitPathSelectSoHandle(const char * pathSelectSoPath)157 int32_t InitPathSelectSoHandle(const char *pathSelectSoPath)
158 {
159     if (pathSelectSoPath == NULL) {
160         LOG_FUN_ERR("pathSelectSoPath is NULL");
161         return HDF_FAILURE;
162     }
163     char pathBuf[PATH_MAX] = {'\0'};
164     if (realpath(pathSelectSoPath, pathBuf) == NULL) {
165         return HDF_FAILURE;
166     }
167     if (g_ptrPathSelHandle == NULL) {
168         char *cPathBuf = pathBuf;
169         g_ptrPathSelHandle = dlopen(cPathBuf, RTLD_LAZY);
170         if (g_ptrPathSelHandle == NULL) {
171             LOG_FUN_ERR("open lib PathSelct so fail, reason:%s", dlerror());
172             return HDF_FAILURE;
173         }
174         g_pathSelGetConfToJsonObj = dlsym(g_ptrPathSelHandle, "AudioPathSelGetConfToJsonObj");
175         g_pathSelAnalysisJson = dlsym(g_ptrPathSelHandle, "AudioPathSelAnalysisJson");
176         if (g_pathSelGetConfToJsonObj == NULL || g_pathSelAnalysisJson == NULL) {
177             LOG_FUN_ERR("lib PathSelct so func not found!");
178             AudioDlClose(&g_ptrPathSelHandle);
179             return HDF_FAILURE;
180         }
181     }
182     return HDF_SUCCESS;
183 }
184 #endif
185 
AudioManagerGetAllAdapters(struct AudioManager * manager,struct AudioAdapterDescriptor ** descs,int * size)186 int32_t AudioManagerGetAllAdapters(struct AudioManager *manager,
187                                    struct AudioAdapterDescriptor **descs,
188                                    int *size)
189 {
190     LOG_FUN_INFO();
191     if (manager == NULL || descs == NULL || size == NULL) {
192         return AUDIO_HAL_ERR_INVALID_PARAM;
193     }
194     int32_t ret = AudioAdaptersForUser(descs, size);
195     if (ret < 0) {
196         LOG_FUN_ERR("AudioAdaptersForUser FAIL!");
197         return AUDIO_HAL_ERR_NOTREADY; // Failed to read sound card configuration file
198     }
199     if (g_captureSoPath == NULL || g_renderSoPath == NULL) {
200         LOG_FUN_ERR("sopath is error");
201         return AUDIO_HAL_ERR_INTERNAL;
202     }
203     if (*descs != NULL && size != NULL && (*size) > 0) { // Fuzz test
204         g_localAddrAudioAdapterOut  = *descs;
205         g_localAdapterNum = *size;
206     } else {
207         LOG_FUN_ERR("Get AudioAdapterDescriptor Failed");
208         return AUDIO_HAL_ERR_INVALID_OBJECT;
209     }
210     ret = InitCaptureSoHandle(g_captureSoPath);
211     if (ret < 0) {
212         LOG_FUN_ERR("InitCaptureSoHandle FAIL!");
213         return AUDIO_HAL_ERR_INTERNAL;
214     }
215     ret = InitRenderSoHandle(g_renderSoPath);
216     if (ret < 0) {
217         LOG_FUN_ERR("InitRenderSoHandle FAIL!");
218         AudioDlClose(&g_ptrCaptureHandle);
219         return AUDIO_HAL_ERR_INTERNAL;
220     }
221 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
222     ret = InitPathSelectSoHandle(g_pathSelectSoPath);
223     if (ret < 0 || g_pathSelGetConfToJsonObj == NULL) {
224         LOG_FUN_ERR("InitPathSelectSoHandle FAIL!");
225         AudioDlClose(&g_ptrRenderHandle);
226         AudioDlClose(&g_ptrCaptureHandle);
227         return AUDIO_HAL_ERR_INTERNAL;
228     }
229     ret = g_pathSelGetConfToJsonObj();
230     if (ret < 0) {
231         LOG_FUN_ERR("g_pathSelGetConfToJsonObj FAIL!");
232         AudioDlClose(&g_ptrRenderHandle);
233         AudioDlClose(&g_ptrCaptureHandle);
234         AudioDlClose(&g_ptrPathSelHandle);
235         return AUDIO_HAL_ERR_INTERNAL;
236     }
237 #endif
238     return AUDIO_HAL_SUCCESS;
239 }
240 
AudioManagerLoadAdapter(struct AudioManager * manager,const struct AudioAdapterDescriptor * desc,struct AudioAdapter ** adapter)241 int32_t AudioManagerLoadAdapter(struct AudioManager *manager, const struct AudioAdapterDescriptor *desc,
242                                 struct AudioAdapter **adapter)
243 {
244     LOG_FUN_INFO();
245     if (manager == NULL || desc == NULL || desc->adapterName == NULL || desc->ports == NULL || adapter == NULL) {
246         return AUDIO_HAL_ERR_INVALID_PARAM;
247     }
248     bool descFlag = false;
249     if (g_localAdapterNum <= 0 || g_localAdapterNum > SUPPORT_ADAPTER_NUM_MAX) {
250         return AUDIO_HAL_ERR_INTERNAL;
251     }
252     if (g_localAddrAudioAdapterOut != NULL) { // Fuzz test
253         for (int index = 0; index < g_localAdapterNum; index++) {
254             if (&g_localAddrAudioAdapterOut[index] == desc) {
255                 descFlag = true;
256                 break;
257             }
258         }
259         if (!descFlag) {
260             LOG_FUN_ERR("The desc address passed in is invalid");
261             return AUDIO_HAL_ERR_INVALID_OBJECT;
262         }
263     }
264     LOGV("%s: adapter name %s", __func__, desc->adapterName);
265     if (AudioAdapterExist(desc->adapterName)) {
266         LOGE("%s: not supported this adapter %s", __func__, desc->adapterName);
267         return AUDIO_HAL_ERR_INVALID_PARAM;
268     }
269     struct AudioHwAdapter *hwAdapter = (struct AudioHwAdapter *)calloc(1, sizeof(*hwAdapter));
270     if (hwAdapter == NULL) {
271         LOGE("%s: calloc AudioHwAdapter failed", __func__);
272         return AUDIO_HAL_ERR_MALLOC_FAIL;
273     }
274     hwAdapter->common.InitAllPorts = AudioAdapterInitAllPorts;
275     hwAdapter->common.CreateRender = AudioAdapterCreateRender;
276     hwAdapter->common.DestroyRender = AudioAdapterDestroyRender;
277     hwAdapter->common.CreateCapture = AudioAdapterCreateCapture;
278     hwAdapter->common.DestroyCapture = AudioAdapterDestroyCapture;
279     hwAdapter->common.GetPortCapability = AudioAdapterGetPortCapability;
280     hwAdapter->common.SetPassthroughMode = AudioAdapterSetPassthroughMode;
281     hwAdapter->common.GetPassthroughMode = AudioAdapterGetPassthroughMode;
282     hwAdapter->adapterDescriptor = *desc;
283     hwAdapter->adapterMgrRenderFlag = 0; // The adapterMgrRenderFlag init is zero
284     hwAdapter->adapterMgrCaptureFlag = 0; // The adapterMgrCaptureFlag init is zero
285     int32_t ret = AudioAddAdapterAddrToList((AudioHandle)(&hwAdapter->common), desc);
286     if (ret < 0) { // add for Fuzz
287         LOG_FUN_ERR("AudioAdapterAddrGet check Failed");
288         AudioMemFree((void **)&hwAdapter);
289         return ret;
290     }
291     *adapter = &hwAdapter->common;
292     return AUDIO_HAL_SUCCESS;
293 }
294 
AudioManagerUnloadAdapter(struct AudioManager * manager,struct AudioAdapter * adapter)295 void AudioManagerUnloadAdapter(struct AudioManager *manager, struct AudioAdapter *adapter)
296 {
297     int32_t ret = AudioCheckAdapterAddr((AudioHandle)adapter);
298     if (ret < 0) {
299         LOG_FUN_ERR("The adapter address passed in is invalid");
300         return;
301     }
302     struct AudioHwAdapter *hwAdapter = (struct AudioHwAdapter *)adapter;
303     if (manager == NULL || hwAdapter == NULL) {
304         return;
305     }
306     if (hwAdapter->portCapabilitys != NULL) {
307         int32_t portNum = hwAdapter->adapterDescriptor.portNum;
308         int32_t i = 0;
309         while (i < portNum) {
310             if (&hwAdapter->portCapabilitys[i] != NULL) {
311                 AudioMemFree((void **)&hwAdapter->portCapabilitys[i].capability.subPorts);
312             }
313             i++;
314         }
315         AudioMemFree((void **)&hwAdapter->portCapabilitys);
316     }
317     if (AudioDelAdapterAddrFromList((AudioHandle)adapter)) {
318         LOG_FUN_ERR("adapter or render not in MgrList");
319     }
320     AudioMemFree((void **)&adapter);
321 }
322 
323 static struct AudioManager g_audioManagerFuncs = {
324     .GetAllAdapters = AudioManagerGetAllAdapters,
325     .LoadAdapter = AudioManagerLoadAdapter,
326     .UnloadAdapter = AudioManagerUnloadAdapter,
327 };
328 
GetAudioManagerFuncs(void)329 struct AudioManager *GetAudioManagerFuncs(void)
330 {
331     static bool audioAdapterAddrMgrFlag = false;
332     if (!audioAdapterAddrMgrFlag) {
333         AudioAdapterAddrMgrInit(); // memset for Fuzz
334         audioAdapterAddrMgrFlag = true;
335     }
336     return &g_audioManagerFuncs;
337 }
338