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_adapter_info_common.h"
17 #include "audio_interface_lib_capture.h"
18 #include "audio_interface_lib_render.h"
19 #include "audio_internal.h"
20
21 BindServiceRenderSo g_bindServiceRender = NULL;
22 InterfaceLibModeRenderSo g_interfaceLibModeRender = NULL;
23 CloseServiceRenderSo g_closeServiceRender = NULL;
24
25 BindServiceCaptureSo g_bindServiceCapture = NULL;
26 InterfaceLibModeCaptureSo g_interfaceLibModeCapture = NULL;
27 CloseServiceCaptureSo g_closeServiceCapture = NULL;
28
29 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
30 PathSelGetConfToJsonObj g_pathSelGetConfToJsonObj = NULL;
31 PathSelAnalysisJson g_pathSelAnalysisJson = NULL;
32 #endif
33
34 static char *g_captureSoPath = SO_INTERFACE_LIB_CAPTURE_PATH;
35 static char *g_renderSoPath = SO_INTERFACE_LIB_RENDER_PATH;
36
37 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
38 static char *g_pathSelectSoPath = SO_CJSON_LIB_PATHSELECT_PATH;
39 #endif
40
41 static void *g_ptrCaptureHandle = NULL;
42 static void *g_ptrRenderHandle = NULL;
43
44 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
45 static void *g_ptrPathSelHandle = NULL;
46 #endif
47
AudioSoGetBindServiceRender()48 BindServiceRenderSo *AudioSoGetBindServiceRender()
49 {
50 return &g_bindServiceRender;
51 }
52
AudioSoGetInterfaceLibModeRender()53 InterfaceLibModeRenderSo *AudioSoGetInterfaceLibModeRender()
54 {
55 return &g_interfaceLibModeRender;
56 }
57
AudioSoGetCloseServiceRender()58 CloseServiceRenderSo *AudioSoGetCloseServiceRender()
59 {
60 return &g_closeServiceRender;
61 }
62
AudioSoGetBindServiceCapture()63 BindServiceCaptureSo *AudioSoGetBindServiceCapture()
64 {
65 return &g_bindServiceCapture;
66 }
67
AudioSoGetInterfaceLibModeCapture()68 InterfaceLibModeCaptureSo *AudioSoGetInterfaceLibModeCapture()
69 {
70 return &g_interfaceLibModeCapture;
71 }
72
AudioSoGetCloseServiceCapture()73 CloseServiceCaptureSo *AudioSoGetCloseServiceCapture()
74 {
75 return &g_closeServiceCapture;
76 }
77
78
79 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
AudioSoGetPathSelGetConfToJsonObj()80 PathSelGetConfToJsonObj *AudioSoGetPathSelGetConfToJsonObj()
81 {
82 return &g_pathSelGetConfToJsonObj;
83 }
84
AudioSoGetPathSelAnalysisJson()85 PathSelAnalysisJson *AudioSoGetPathSelAnalysisJson()
86 {
87 return &g_pathSelAnalysisJson;
88 }
89 #endif
90
InitCaptureSoHandle(const char * captureSoPath)91 int32_t InitCaptureSoHandle(const char *captureSoPath)
92 {
93 if (captureSoPath == NULL) {
94 LOG_FUN_ERR("captureSoPath is NULL");
95 return HDF_FAILURE;
96 }
97 if (g_ptrCaptureHandle == NULL) {
98 g_ptrCaptureHandle = dlopen(captureSoPath, RTLD_LAZY);
99 if (g_ptrCaptureHandle == NULL) {
100 LOG_FUN_ERR("open lib capture so fail, reason:%s", dlerror());
101 return HDF_FAILURE;
102 }
103 g_bindServiceCapture = dlsym(g_ptrCaptureHandle, "AudioBindServiceCapture");
104 g_interfaceLibModeCapture = dlsym(g_ptrCaptureHandle, "AudioInterfaceLibModeCapture");
105 g_closeServiceCapture = dlsym(g_ptrCaptureHandle, "AudioCloseServiceCapture");
106 if (g_bindServiceCapture == NULL || g_interfaceLibModeCapture == NULL || g_closeServiceCapture == NULL) {
107 LOG_FUN_ERR("lib capture so func not found!");
108 AudioDlClose(&g_ptrCaptureHandle);
109 return HDF_FAILURE;
110 }
111 }
112 return HDF_SUCCESS;
113 }
114
InitRenderSoHandle(const char * renderSoPath)115 int32_t InitRenderSoHandle(const char *renderSoPath)
116 {
117 if (renderSoPath == NULL) {
118 LOG_FUN_ERR("renderSoPath is NULL");
119 return HDF_FAILURE;
120 }
121 if (g_ptrRenderHandle == NULL) {
122 g_ptrRenderHandle = dlopen(renderSoPath, RTLD_LAZY);
123 if (g_ptrRenderHandle == NULL) {
124 LOG_FUN_ERR("open lib render so fail, reason:%s", dlerror());
125 return HDF_FAILURE;
126 }
127 g_bindServiceRender = dlsym(g_ptrRenderHandle, "AudioBindServiceRender");
128 g_interfaceLibModeRender = dlsym(g_ptrRenderHandle, "AudioInterfaceLibModeRender");
129 g_closeServiceRender = dlsym(g_ptrRenderHandle, "AudioCloseServiceRender");
130 if (g_bindServiceRender == NULL || g_interfaceLibModeRender == NULL || g_closeServiceRender == NULL) {
131 LOG_FUN_ERR("lib render so func not found!");
132 AudioDlClose(&g_ptrRenderHandle);
133 return HDF_FAILURE;
134 }
135 }
136 return HDF_SUCCESS;
137 }
138
139 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
InitPathSelectSoHandle(const char * pathSelectSoPath)140 int32_t InitPathSelectSoHandle(const char *pathSelectSoPath)
141 {
142 if (pathSelectSoPath == NULL) {
143 LOG_FUN_ERR("pathSelectSoPath is NULL");
144 return HDF_FAILURE;
145 }
146 if (g_ptrPathSelHandle == NULL) {
147 g_ptrPathSelHandle = dlopen(pathSelectSoPath, RTLD_LAZY);
148 if (g_ptrPathSelHandle == NULL) {
149 LOG_FUN_ERR("open lib PathSelct so fail, reason:%s", dlerror());
150 return HDF_FAILURE;
151 }
152 g_pathSelGetConfToJsonObj = dlsym(g_ptrPathSelHandle, "AudioPathSelGetConfToJsonObj");
153 g_pathSelAnalysisJson = dlsym(g_ptrPathSelHandle, "AudioPathSelAnalysisJson");
154 if (g_pathSelGetConfToJsonObj == NULL || g_pathSelAnalysisJson == NULL) {
155 LOG_FUN_ERR("lib PathSelct so func not found!");
156 AudioDlClose(&g_ptrPathSelHandle);
157 return HDF_FAILURE;
158 }
159 }
160 return HDF_SUCCESS;
161 }
162 #endif
163
AudioManagerGetAllAdapters(struct AudioManager * manager,struct AudioAdapterDescriptor ** descs,int * size)164 int32_t AudioManagerGetAllAdapters(struct AudioManager *manager,
165 struct AudioAdapterDescriptor **descs,
166 int *size)
167 {
168 int32_t ret;
169 LOG_FUN_INFO();
170 if (manager == NULL || descs == NULL || size == NULL) {
171 return HDF_FAILURE;
172 }
173 ret = AudioAdaptersForUser(descs, size);
174 if (ret < 0) {
175 LOG_FUN_ERR("AudioAdaptersForUser FAIL!");
176
177 return HDF_FAILURE;
178 }
179 ret = InitCaptureSoHandle(g_captureSoPath);
180 if (ret < 0) {
181 LOG_FUN_ERR("InitCaptureSoHandle FAIL!");
182 return HDF_FAILURE;
183 }
184 ret = InitRenderSoHandle(g_renderSoPath);
185 if (ret < 0) {
186 LOG_FUN_ERR("InitRenderSoHandle FAIL!");
187 AudioDlClose(&g_ptrCaptureHandle);
188 return HDF_FAILURE;
189 }
190 #ifndef AUDIO_HAL_NOTSUPPORT_PATHSELECT
191 ret = InitPathSelectSoHandle(g_pathSelectSoPath);
192 if (ret < 0) {
193 LOG_FUN_ERR("InitPathSelectSoHandle FAIL!");
194 AudioDlClose(&g_ptrRenderHandle);
195 AudioDlClose(&g_ptrCaptureHandle);
196 return HDF_FAILURE;
197 }
198 ret = g_pathSelGetConfToJsonObj();
199 if (ret < 0) {
200 LOG_FUN_ERR("g_pathSelGetConfToJsonObj FAIL!");
201 AudioDlClose(&g_ptrRenderHandle);
202 AudioDlClose(&g_ptrCaptureHandle);
203 AudioDlClose(&g_ptrPathSelHandle);
204 return HDF_FAILURE;
205 }
206 #endif
207 return HDF_SUCCESS;
208 }
209
AudioManagerLoadAdapter(struct AudioManager * manager,const struct AudioAdapterDescriptor * desc,struct AudioAdapter ** adapter)210 int32_t AudioManagerLoadAdapter(struct AudioManager *manager, const struct AudioAdapterDescriptor *desc,
211 struct AudioAdapter **adapter)
212 {
213 LOG_FUN_INFO();
214 if (manager == NULL || desc == NULL || desc->adapterName == NULL || adapter == NULL) {
215 return HDF_FAILURE;
216 }
217 LOGV("%s: adapter name %s", __func__, desc->adapterName);
218 if (AudioAdapterExist(desc->adapterName)) {
219 LOGE("%s: not supported this adapter %s", __func__, desc->adapterName);
220 return HDF_FAILURE;
221 }
222 struct AudioHwAdapter *hwAdapter = (struct AudioHwAdapter *)calloc(1, sizeof(*hwAdapter));
223 if (hwAdapter == NULL) {
224 LOGE("%s: calloc AudioHwAdapter failed", __func__);
225 return -ENOMEM;
226 }
227 hwAdapter->common.InitAllPorts = AudioAdapterInitAllPorts;
228 hwAdapter->common.CreateRender = AudioAdapterCreateRender;
229 hwAdapter->common.DestroyRender = AudioAdapterDestroyRender;
230 hwAdapter->common.CreateCapture = AudioAdapterCreateCapture;
231 hwAdapter->common.DestroyCapture = AudioAdapterDestroyCapture;
232 hwAdapter->common.GetPortCapability = AudioAdapterGetPortCapability;
233 hwAdapter->common.SetPassthroughMode = AudioAdapterSetPassthroughMode;
234 hwAdapter->common.GetPassthroughMode = AudioAdapterGetPassthroughMode;
235 *adapter = &hwAdapter->common;
236 hwAdapter->adapterDescriptor = *desc;
237 hwAdapter->adapterMgrRenderFlag = 0; // The adapterMgrRenderFlag init is zero
238 hwAdapter->adapterMgrCaptureFlag = 0; // The adapterMgrCaptureFlag init is zero
239 return HDF_SUCCESS;
240 }
241
AudioManagerUnloadAdapter(struct AudioManager * manager,struct AudioAdapter * adapter)242 void AudioManagerUnloadAdapter(struct AudioManager *manager, struct AudioAdapter *adapter)
243 {
244 struct AudioHwAdapter *hwAdapter = (struct AudioHwAdapter *)adapter;
245 if (manager == NULL || hwAdapter == NULL) {
246 return;
247 }
248 if (hwAdapter->portCapabilitys != NULL) {
249 int32_t portNum = hwAdapter->adapterDescriptor.portNum;
250 int32_t i = 0;
251 while (i < portNum) {
252 if (&hwAdapter->portCapabilitys[i] != NULL) {
253 AudioMemFree((void **)&hwAdapter->portCapabilitys[i].capability.subPorts);
254 }
255 i++;
256 }
257 AudioMemFree((void **)&hwAdapter->portCapabilitys);
258 }
259 AudioMemFree((void **)&adapter);
260 }
261
262 static struct AudioManager g_audioManagerFuncs = {
263 .GetAllAdapters = AudioManagerGetAllAdapters,
264 .LoadAdapter = AudioManagerLoadAdapter,
265 .UnloadAdapter = AudioManagerUnloadAdapter,
266 };
267
GetAudioManagerFuncs(void)268 struct AudioManager *GetAudioManagerFuncs(void)
269 {
270 return &g_audioManagerFuncs;
271 }
272