1 | /* $Id: DrvHostAudioAlsaStubs.cpp 89427 2021-06-01 10:25:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Stubs for libasound.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
|
---|
19 | #include <iprt/assert.h>
|
---|
20 | #include <iprt/err.h>
|
---|
21 | #include <iprt/ldr.h>
|
---|
22 | #include <VBox/log.h>
|
---|
23 | #include <iprt/once.h>
|
---|
24 |
|
---|
25 | #include <alsa/asoundlib.h>
|
---|
26 | #include <errno.h>
|
---|
27 |
|
---|
28 | #include "DrvHostAudioAlsaStubs.h"
|
---|
29 |
|
---|
30 | #define VBOX_ALSA_LIB "libasound.so.2"
|
---|
31 |
|
---|
32 | #define PROXY_STUB(function, rettype, signature, shortsig) \
|
---|
33 | static rettype (*pfn_ ## function) signature; \
|
---|
34 | \
|
---|
35 | extern "C" rettype VBox_##function signature; \
|
---|
36 | rettype VBox_##function signature \
|
---|
37 | { \
|
---|
38 | return pfn_ ## function shortsig; \
|
---|
39 | }
|
---|
40 |
|
---|
41 | PROXY_STUB(snd_lib_error_set_handler, int, (snd_lib_error_handler_t handler),
|
---|
42 | (handler))
|
---|
43 | PROXY_STUB(snd_strerror, const char *, (int errnum), (errnum))
|
---|
44 |
|
---|
45 | PROXY_STUB(snd_device_name_hint, int,
|
---|
46 | (int card, const char *iface, void ***hints),
|
---|
47 | (card, iface, hints))
|
---|
48 | PROXY_STUB(snd_device_name_free_hint, int,
|
---|
49 | (void **hints),
|
---|
50 | (hints))
|
---|
51 | PROXY_STUB(snd_device_name_get_hint, char *,
|
---|
52 | (const void *hint, const char *id),
|
---|
53 | (hint, id))
|
---|
54 |
|
---|
55 | static int fallback_snd_device_name_hint(int card, const char *iface, void ***hints)
|
---|
56 | {
|
---|
57 | RT_NOREF(card, iface);
|
---|
58 | *hints = NULL;
|
---|
59 | return -ENOSYS;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static int fallback_snd_device_name_free_hint(void **hints)
|
---|
63 | {
|
---|
64 | RT_NOREF(hints);
|
---|
65 | return 0;
|
---|
66 | }
|
---|
67 |
|
---|
68 | static char *fallback_snd_device_name_get_hint(const void *hint, const char *id)
|
---|
69 | {
|
---|
70 | RT_NOREF(hint, id);
|
---|
71 | return NULL;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * PCM
|
---|
76 | */
|
---|
77 |
|
---|
78 | PROXY_STUB(snd_pcm_avail_update, snd_pcm_sframes_t, (snd_pcm_t *pcm), (pcm))
|
---|
79 | PROXY_STUB(snd_pcm_avail_delay, int,
|
---|
80 | (snd_pcm_t *pcm, snd_pcm_sframes_t *availp, snd_pcm_sframes_t *delayp),
|
---|
81 | (pcm, availp, delayp))
|
---|
82 | PROXY_STUB(snd_pcm_close, int, (snd_pcm_t *pcm), (pcm))
|
---|
83 | PROXY_STUB(snd_pcm_delay, int, (snd_pcm_t *pcm, snd_pcm_sframes_t *delayp), (pcm, delayp))
|
---|
84 | PROXY_STUB(snd_pcm_nonblock, int, (snd_pcm_t *pcm, int *onoff),
|
---|
85 | (pcm, onoff))
|
---|
86 | PROXY_STUB(snd_pcm_drain, int, (snd_pcm_t *pcm),
|
---|
87 | (pcm))
|
---|
88 | PROXY_STUB(snd_pcm_drop, int, (snd_pcm_t *pcm), (pcm))
|
---|
89 | PROXY_STUB(snd_pcm_open, int,
|
---|
90 | (snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode),
|
---|
91 | (pcm, name, stream, mode))
|
---|
92 | PROXY_STUB(snd_pcm_prepare, int, (snd_pcm_t *pcm), (pcm))
|
---|
93 | PROXY_STUB(snd_pcm_readi, snd_pcm_sframes_t,
|
---|
94 | (snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size),
|
---|
95 | (pcm, buffer, size))
|
---|
96 | PROXY_STUB(snd_pcm_resume, int, (snd_pcm_t *pcm), (pcm))
|
---|
97 | PROXY_STUB(snd_pcm_state, snd_pcm_state_t, (snd_pcm_t *pcm), (pcm))
|
---|
98 | PROXY_STUB(snd_pcm_state_name, const char *, (snd_pcm_state_t state), (state))
|
---|
99 | PROXY_STUB(snd_pcm_writei, snd_pcm_sframes_t,
|
---|
100 | (snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size),
|
---|
101 | (pcm, buffer, size))
|
---|
102 | PROXY_STUB(snd_pcm_start, int, (snd_pcm_t *pcm), (pcm))
|
---|
103 |
|
---|
104 | static int fallback_snd_pcm_avail_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *availp, snd_pcm_sframes_t *delayp)
|
---|
105 | {
|
---|
106 | *availp = pfn_snd_pcm_avail_update(pcm);
|
---|
107 | int ret = pfn_snd_pcm_delay(pcm, delayp);
|
---|
108 | if (ret >= 0 && *availp < 0)
|
---|
109 | ret = (int)*availp;
|
---|
110 | return ret;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * HW
|
---|
115 | */
|
---|
116 |
|
---|
117 | PROXY_STUB(snd_pcm_hw_params, int,
|
---|
118 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params),
|
---|
119 | (pcm, params))
|
---|
120 | PROXY_STUB(snd_pcm_hw_params_any, int,
|
---|
121 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params),
|
---|
122 | (pcm, params))
|
---|
123 | PROXY_STUB(snd_pcm_hw_params_get_buffer_size, int,
|
---|
124 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
|
---|
125 | (params, val))
|
---|
126 | PROXY_STUB(snd_pcm_hw_params_get_buffer_size_min, int,
|
---|
127 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
|
---|
128 | (params, val))
|
---|
129 | PROXY_STUB(snd_pcm_hw_params_get_period_size, int,
|
---|
130 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir),
|
---|
131 | (params, frames, dir))
|
---|
132 | PROXY_STUB(snd_pcm_hw_params_get_period_size_min, int,
|
---|
133 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir),
|
---|
134 | (params, frames, dir))
|
---|
135 | PROXY_STUB(snd_pcm_hw_params_set_rate_near, int,
|
---|
136 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
|
---|
137 | (pcm, params, val, dir))
|
---|
138 | PROXY_STUB(snd_pcm_hw_params_set_access, int,
|
---|
139 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access),
|
---|
140 | (pcm, params, _access))
|
---|
141 | PROXY_STUB(snd_pcm_hw_params_set_buffer_time_near, int,
|
---|
142 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
|
---|
143 | (pcm, params, val, dir))
|
---|
144 | PROXY_STUB(snd_pcm_hw_params_set_buffer_size_near, int,
|
---|
145 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
|
---|
146 | (pcm, params, val))
|
---|
147 | PROXY_STUB(snd_pcm_hw_params_set_channels_near, int,
|
---|
148 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val),
|
---|
149 | (pcm, params, val))
|
---|
150 | PROXY_STUB(snd_pcm_hw_params_set_period_size_near, int,
|
---|
151 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir),
|
---|
152 | (pcm, params, val, dir))
|
---|
153 | PROXY_STUB(snd_pcm_hw_params_set_period_time_near, int,
|
---|
154 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
|
---|
155 | (pcm, params, val, dir))
|
---|
156 | PROXY_STUB(snd_pcm_hw_params_sizeof, size_t, (void), ())
|
---|
157 | PROXY_STUB(snd_pcm_hw_params_set_format, int,
|
---|
158 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val),
|
---|
159 | (pcm, params, val))
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * SW
|
---|
163 | */
|
---|
164 |
|
---|
165 | PROXY_STUB(snd_pcm_sw_params, int,
|
---|
166 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params),
|
---|
167 | (pcm, params))
|
---|
168 | PROXY_STUB(snd_pcm_sw_params_current, int,
|
---|
169 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params),
|
---|
170 | (pcm, params))
|
---|
171 | PROXY_STUB(snd_pcm_sw_params_get_start_threshold, int,
|
---|
172 | (const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val),
|
---|
173 | (params, val))
|
---|
174 | PROXY_STUB(snd_pcm_sw_params_set_avail_min, int,
|
---|
175 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val),
|
---|
176 | (pcm, params, val))
|
---|
177 | PROXY_STUB(snd_pcm_sw_params_set_start_threshold, int,
|
---|
178 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val),
|
---|
179 | (pcm, params, val))
|
---|
180 | PROXY_STUB(snd_pcm_sw_params_sizeof, size_t, (void), ())
|
---|
181 |
|
---|
182 | typedef struct
|
---|
183 | {
|
---|
184 | const char *name;
|
---|
185 | void (**pfn)(void);
|
---|
186 | void (*pfnFallback)(void);
|
---|
187 | } SHARED_FUNC;
|
---|
188 |
|
---|
189 | #define ELEMENT(function) { #function , (void (**)(void)) & pfn_ ## function, NULL }
|
---|
190 | #define ELEMENT_FALLBACK(function) { #function , (void (**)(void)) & pfn_ ## function, (void (*)(void))fallback_ ## function }
|
---|
191 | static SHARED_FUNC SharedFuncs[] =
|
---|
192 | {
|
---|
193 | ELEMENT(snd_lib_error_set_handler),
|
---|
194 | ELEMENT(snd_strerror),
|
---|
195 |
|
---|
196 | ELEMENT_FALLBACK(snd_device_name_hint),
|
---|
197 | ELEMENT_FALLBACK(snd_device_name_get_hint),
|
---|
198 | ELEMENT_FALLBACK(snd_device_name_free_hint),
|
---|
199 |
|
---|
200 | ELEMENT(snd_pcm_avail_update),
|
---|
201 | ELEMENT_FALLBACK(snd_pcm_avail_delay),
|
---|
202 | ELEMENT(snd_pcm_close),
|
---|
203 | ELEMENT(snd_pcm_delay),
|
---|
204 | ELEMENT(snd_pcm_drain),
|
---|
205 | ELEMENT(snd_pcm_drop),
|
---|
206 | ELEMENT(snd_pcm_nonblock),
|
---|
207 | ELEMENT(snd_pcm_open),
|
---|
208 | ELEMENT(snd_pcm_prepare),
|
---|
209 | ELEMENT(snd_pcm_resume),
|
---|
210 | ELEMENT(snd_pcm_state),
|
---|
211 | ELEMENT(snd_pcm_state_name),
|
---|
212 |
|
---|
213 | ELEMENT(snd_pcm_readi),
|
---|
214 | ELEMENT(snd_pcm_start),
|
---|
215 | ELEMENT(snd_pcm_writei),
|
---|
216 |
|
---|
217 | ELEMENT(snd_pcm_hw_params),
|
---|
218 | ELEMENT(snd_pcm_hw_params_any),
|
---|
219 | ELEMENT(snd_pcm_hw_params_sizeof),
|
---|
220 | ELEMENT(snd_pcm_hw_params_get_buffer_size),
|
---|
221 | ELEMENT(snd_pcm_hw_params_get_buffer_size_min),
|
---|
222 | ELEMENT(snd_pcm_hw_params_get_period_size_min),
|
---|
223 | ELEMENT(snd_pcm_hw_params_set_access),
|
---|
224 | ELEMENT(snd_pcm_hw_params_set_buffer_size_near),
|
---|
225 | ELEMENT(snd_pcm_hw_params_set_buffer_time_near),
|
---|
226 | ELEMENT(snd_pcm_hw_params_set_channels_near),
|
---|
227 | ELEMENT(snd_pcm_hw_params_set_format),
|
---|
228 | ELEMENT(snd_pcm_hw_params_get_period_size),
|
---|
229 | ELEMENT(snd_pcm_hw_params_set_period_size_near),
|
---|
230 | ELEMENT(snd_pcm_hw_params_set_period_time_near),
|
---|
231 | ELEMENT(snd_pcm_hw_params_set_rate_near),
|
---|
232 |
|
---|
233 | ELEMENT(snd_pcm_sw_params),
|
---|
234 | ELEMENT(snd_pcm_sw_params_current),
|
---|
235 | ELEMENT(snd_pcm_sw_params_get_start_threshold),
|
---|
236 | ELEMENT(snd_pcm_sw_params_set_avail_min),
|
---|
237 | ELEMENT(snd_pcm_sw_params_set_start_threshold),
|
---|
238 | ELEMENT(snd_pcm_sw_params_sizeof),
|
---|
239 | };
|
---|
240 | #undef ELEMENT
|
---|
241 |
|
---|
242 | /** Init once. */
|
---|
243 | static RTONCE g_AlsaLibInitOnce = RTONCE_INITIALIZER;
|
---|
244 |
|
---|
245 |
|
---|
246 | /** @callback_method_impl{FNRTONCE} */
|
---|
247 | static DECLCALLBACK(int32_t) drvHostAudioAlsaLibInitOnce(void *pvUser)
|
---|
248 | {
|
---|
249 | RT_NOREF(pvUser);
|
---|
250 | LogFlowFunc(("\n"));
|
---|
251 |
|
---|
252 | RTLDRMOD hMod = NIL_RTLDRMOD;
|
---|
253 | int rc = RTLdrLoadSystemEx(VBOX_ALSA_LIB, RTLDRLOAD_FLAGS_NO_UNLOAD, &hMod);
|
---|
254 | if (RT_SUCCESS(rc))
|
---|
255 | {
|
---|
256 | for (uintptr_t i = 0; i < RT_ELEMENTS(SharedFuncs); i++)
|
---|
257 | {
|
---|
258 | rc = RTLdrGetSymbol(hMod, SharedFuncs[i].name, (void **)SharedFuncs[i].pfn);
|
---|
259 | if (RT_SUCCESS(rc))
|
---|
260 | { /* likely */ }
|
---|
261 | else if (SharedFuncs[i].pfnFallback && rc == VERR_SYMBOL_NOT_FOUND)
|
---|
262 | *SharedFuncs[i].pfn = SharedFuncs[i].pfnFallback;
|
---|
263 | else
|
---|
264 | {
|
---|
265 | LogRelFunc(("Failed to load library %s: Getting symbol %s failed: %Rrc\n", VBOX_ALSA_LIB, SharedFuncs[i].name, rc));
|
---|
266 | return rc;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|
270 | else
|
---|
271 | LogRelFunc(("Failed to load library %s (%Rrc)\n", VBOX_ALSA_LIB, rc));
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Try to dynamically load the ALSA libraries.
|
---|
278 | *
|
---|
279 | * @returns VBox status code.
|
---|
280 | */
|
---|
281 | int audioLoadAlsaLib(void)
|
---|
282 | {
|
---|
283 | LogFlowFunc(("\n"));
|
---|
284 | return RTOnce(&g_AlsaLibInitOnce, drvHostAudioAlsaLibInitOnce, NULL);
|
---|
285 | }
|
---|
286 |
|
---|