1 | /* $Id: DrvHostAudioAlsaStubs.cpp 88966 2021-05-10 13:40:35Z 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/ldr.h>
|
---|
21 | #include <VBox/log.h>
|
---|
22 | #include <iprt/err.h>
|
---|
23 |
|
---|
24 | #include <alsa/asoundlib.h>
|
---|
25 | #include <errno.h>
|
---|
26 |
|
---|
27 | #include "DrvHostAudioAlsaStubs.h"
|
---|
28 |
|
---|
29 | #define VBOX_ALSA_LIB "libasound.so.2"
|
---|
30 |
|
---|
31 | #define PROXY_STUB(function, rettype, signature, shortsig) \
|
---|
32 | static rettype (*pfn_ ## function) signature; \
|
---|
33 | \
|
---|
34 | extern "C" rettype VBox_##function signature; \
|
---|
35 | rettype VBox_##function signature \
|
---|
36 | { \
|
---|
37 | return pfn_ ## function shortsig; \
|
---|
38 | }
|
---|
39 |
|
---|
40 | PROXY_STUB(snd_lib_error_set_handler, int, (snd_lib_error_handler_t handler),
|
---|
41 | (handler))
|
---|
42 | PROXY_STUB(snd_strerror, const char *, (int errnum), (errnum))
|
---|
43 |
|
---|
44 | PROXY_STUB(snd_device_name_hint, int,
|
---|
45 | (int card, const char *iface, void ***hints),
|
---|
46 | (card, iface, hints))
|
---|
47 | PROXY_STUB(snd_device_name_free_hint, int,
|
---|
48 | (void **hints),
|
---|
49 | (hints))
|
---|
50 | PROXY_STUB(snd_device_name_get_hint, char *,
|
---|
51 | (const void *hint, const char *id),
|
---|
52 | (hint, id))
|
---|
53 |
|
---|
54 | static int fallback_snd_device_name_hint(int card, const char *iface, void ***hints)
|
---|
55 | {
|
---|
56 | RT_NOREF(card, iface);
|
---|
57 | *hints = NULL;
|
---|
58 | return -ENOSYS;
|
---|
59 | }
|
---|
60 |
|
---|
61 | static int fallback_snd_device_name_free_hint(void **hints)
|
---|
62 | {
|
---|
63 | RT_NOREF(hints);
|
---|
64 | return 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | static char *fallback_snd_device_name_get_hint(const void *hint, const char *id)
|
---|
68 | {
|
---|
69 | RT_NOREF(hint, id);
|
---|
70 | return NULL;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * PCM
|
---|
75 | */
|
---|
76 |
|
---|
77 | PROXY_STUB(snd_pcm_avail_update, snd_pcm_sframes_t, (snd_pcm_t *pcm), (pcm))
|
---|
78 | PROXY_STUB(snd_pcm_avail_delay, int,
|
---|
79 | (snd_pcm_t *pcm, snd_pcm_sframes_t *availp, snd_pcm_sframes_t *delayp),
|
---|
80 | (pcm, availp, delayp))
|
---|
81 | PROXY_STUB(snd_pcm_close, int, (snd_pcm_t *pcm), (pcm))
|
---|
82 | PROXY_STUB(snd_pcm_delay, int, (snd_pcm_t *pcm, snd_pcm_sframes_t *delayp), (pcm, delayp))
|
---|
83 | PROXY_STUB(snd_pcm_nonblock, int, (snd_pcm_t *pcm, int *onoff),
|
---|
84 | (pcm, onoff))
|
---|
85 | PROXY_STUB(snd_pcm_drain, int, (snd_pcm_t *pcm),
|
---|
86 | (pcm))
|
---|
87 | PROXY_STUB(snd_pcm_drop, int, (snd_pcm_t *pcm), (pcm))
|
---|
88 | PROXY_STUB(snd_pcm_open, int,
|
---|
89 | (snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode),
|
---|
90 | (pcm, name, stream, mode))
|
---|
91 | PROXY_STUB(snd_pcm_prepare, int, (snd_pcm_t *pcm), (pcm))
|
---|
92 | PROXY_STUB(snd_pcm_readi, snd_pcm_sframes_t,
|
---|
93 | (snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size),
|
---|
94 | (pcm, buffer, size))
|
---|
95 | PROXY_STUB(snd_pcm_resume, int, (snd_pcm_t *pcm), (pcm))
|
---|
96 | PROXY_STUB(snd_pcm_state, snd_pcm_state_t, (snd_pcm_t *pcm), (pcm))
|
---|
97 | PROXY_STUB(snd_pcm_state_name, const char *, (snd_pcm_state_t state), (state))
|
---|
98 | PROXY_STUB(snd_pcm_writei, snd_pcm_sframes_t,
|
---|
99 | (snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size),
|
---|
100 | (pcm, buffer, size))
|
---|
101 | PROXY_STUB(snd_pcm_start, int, (snd_pcm_t *pcm), (pcm))
|
---|
102 |
|
---|
103 | static int fallback_snd_pcm_avail_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *availp, snd_pcm_sframes_t *delayp)
|
---|
104 | {
|
---|
105 | *availp = pfn_snd_pcm_avail_update(pcm);
|
---|
106 | int ret = pfn_snd_pcm_delay(pcm, delayp);
|
---|
107 | if (ret >= 0 && *availp < 0)
|
---|
108 | ret = (int)*availp;
|
---|
109 | return ret;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * HW
|
---|
114 | */
|
---|
115 |
|
---|
116 | PROXY_STUB(snd_pcm_hw_params, int,
|
---|
117 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params),
|
---|
118 | (pcm, params))
|
---|
119 | PROXY_STUB(snd_pcm_hw_params_any, int,
|
---|
120 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params),
|
---|
121 | (pcm, params))
|
---|
122 | PROXY_STUB(snd_pcm_hw_params_get_buffer_size, int,
|
---|
123 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
|
---|
124 | (params, val))
|
---|
125 | PROXY_STUB(snd_pcm_hw_params_get_buffer_size_min, int,
|
---|
126 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
|
---|
127 | (params, val))
|
---|
128 | PROXY_STUB(snd_pcm_hw_params_get_period_size, int,
|
---|
129 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir),
|
---|
130 | (params, frames, dir))
|
---|
131 | PROXY_STUB(snd_pcm_hw_params_get_period_size_min, int,
|
---|
132 | (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir),
|
---|
133 | (params, frames, dir))
|
---|
134 | PROXY_STUB(snd_pcm_hw_params_set_rate_near, int,
|
---|
135 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
|
---|
136 | (pcm, params, val, dir))
|
---|
137 | PROXY_STUB(snd_pcm_hw_params_set_access, int,
|
---|
138 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access),
|
---|
139 | (pcm, params, _access))
|
---|
140 | PROXY_STUB(snd_pcm_hw_params_set_buffer_time_near, int,
|
---|
141 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
|
---|
142 | (pcm, params, val, dir))
|
---|
143 | PROXY_STUB(snd_pcm_hw_params_set_buffer_size_near, int,
|
---|
144 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
|
---|
145 | (pcm, params, val))
|
---|
146 | PROXY_STUB(snd_pcm_hw_params_set_channels_near, int,
|
---|
147 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val),
|
---|
148 | (pcm, params, val))
|
---|
149 | PROXY_STUB(snd_pcm_hw_params_set_period_size_near, int,
|
---|
150 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir),
|
---|
151 | (pcm, params, val, dir))
|
---|
152 | PROXY_STUB(snd_pcm_hw_params_set_period_time_near, int,
|
---|
153 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
|
---|
154 | (pcm, params, val, dir))
|
---|
155 | PROXY_STUB(snd_pcm_hw_params_sizeof, size_t, (void), ())
|
---|
156 | PROXY_STUB(snd_pcm_hw_params_set_format, int,
|
---|
157 | (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val),
|
---|
158 | (pcm, params, val))
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * SW
|
---|
162 | */
|
---|
163 |
|
---|
164 | PROXY_STUB(snd_pcm_sw_params, int,
|
---|
165 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params),
|
---|
166 | (pcm, params))
|
---|
167 | PROXY_STUB(snd_pcm_sw_params_current, int,
|
---|
168 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params),
|
---|
169 | (pcm, params))
|
---|
170 | PROXY_STUB(snd_pcm_sw_params_get_start_threshold, int,
|
---|
171 | (const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val),
|
---|
172 | (params, val))
|
---|
173 | PROXY_STUB(snd_pcm_sw_params_set_avail_min, int,
|
---|
174 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val),
|
---|
175 | (pcm, params, val))
|
---|
176 | PROXY_STUB(snd_pcm_sw_params_set_start_threshold, int,
|
---|
177 | (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val),
|
---|
178 | (pcm, params, val))
|
---|
179 | PROXY_STUB(snd_pcm_sw_params_sizeof, size_t, (void), ())
|
---|
180 |
|
---|
181 | typedef struct
|
---|
182 | {
|
---|
183 | const char *name;
|
---|
184 | void (**pfn)(void);
|
---|
185 | void (*pfnFallback)(void);
|
---|
186 | } SHARED_FUNC;
|
---|
187 |
|
---|
188 | #define ELEMENT(function) { #function , (void (**)(void)) & pfn_ ## function, NULL }
|
---|
189 | #define ELEMENT_FALLBACK(function) { #function , (void (**)(void)) & pfn_ ## function, (void (*)(void))fallback_ ## function }
|
---|
190 | static SHARED_FUNC SharedFuncs[] =
|
---|
191 | {
|
---|
192 | ELEMENT(snd_lib_error_set_handler),
|
---|
193 | ELEMENT(snd_strerror),
|
---|
194 |
|
---|
195 | ELEMENT_FALLBACK(snd_device_name_hint),
|
---|
196 | ELEMENT_FALLBACK(snd_device_name_get_hint),
|
---|
197 | ELEMENT_FALLBACK(snd_device_name_free_hint),
|
---|
198 |
|
---|
199 | ELEMENT(snd_pcm_avail_update),
|
---|
200 | ELEMENT_FALLBACK(snd_pcm_avail_delay),
|
---|
201 | ELEMENT(snd_pcm_close),
|
---|
202 | ELEMENT(snd_pcm_delay),
|
---|
203 | ELEMENT(snd_pcm_drain),
|
---|
204 | ELEMENT(snd_pcm_drop),
|
---|
205 | ELEMENT(snd_pcm_nonblock),
|
---|
206 | ELEMENT(snd_pcm_open),
|
---|
207 | ELEMENT(snd_pcm_prepare),
|
---|
208 | ELEMENT(snd_pcm_resume),
|
---|
209 | ELEMENT(snd_pcm_state),
|
---|
210 | ELEMENT(snd_pcm_state_name),
|
---|
211 |
|
---|
212 | ELEMENT(snd_pcm_readi),
|
---|
213 | ELEMENT(snd_pcm_start),
|
---|
214 | ELEMENT(snd_pcm_writei),
|
---|
215 |
|
---|
216 | ELEMENT(snd_pcm_hw_params),
|
---|
217 | ELEMENT(snd_pcm_hw_params_any),
|
---|
218 | ELEMENT(snd_pcm_hw_params_sizeof),
|
---|
219 | ELEMENT(snd_pcm_hw_params_get_buffer_size),
|
---|
220 | ELEMENT(snd_pcm_hw_params_get_buffer_size_min),
|
---|
221 | ELEMENT(snd_pcm_hw_params_get_period_size_min),
|
---|
222 | ELEMENT(snd_pcm_hw_params_set_access),
|
---|
223 | ELEMENT(snd_pcm_hw_params_set_buffer_size_near),
|
---|
224 | ELEMENT(snd_pcm_hw_params_set_buffer_time_near),
|
---|
225 | ELEMENT(snd_pcm_hw_params_set_channels_near),
|
---|
226 | ELEMENT(snd_pcm_hw_params_set_format),
|
---|
227 | ELEMENT(snd_pcm_hw_params_get_period_size),
|
---|
228 | ELEMENT(snd_pcm_hw_params_set_period_size_near),
|
---|
229 | ELEMENT(snd_pcm_hw_params_set_period_time_near),
|
---|
230 | ELEMENT(snd_pcm_hw_params_set_rate_near),
|
---|
231 |
|
---|
232 | ELEMENT(snd_pcm_sw_params),
|
---|
233 | ELEMENT(snd_pcm_sw_params_current),
|
---|
234 | ELEMENT(snd_pcm_sw_params_get_start_threshold),
|
---|
235 | ELEMENT(snd_pcm_sw_params_set_avail_min),
|
---|
236 | ELEMENT(snd_pcm_sw_params_set_start_threshold),
|
---|
237 | ELEMENT(snd_pcm_sw_params_sizeof),
|
---|
238 | };
|
---|
239 | #undef ELEMENT
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Try to dynamically load the ALSA libraries. This function is not
|
---|
243 | * thread-safe, and should be called before attempting to use any of the
|
---|
244 | * ALSA functions.
|
---|
245 | *
|
---|
246 | * @returns iprt status code
|
---|
247 | */
|
---|
248 | int audioLoadAlsaLib(void)
|
---|
249 | {
|
---|
250 | static enum { NO = 0, YES, FAIL } isLibLoaded = NO;
|
---|
251 | RTLDRMOD hLib;
|
---|
252 |
|
---|
253 | LogFlowFunc(("\n"));
|
---|
254 | /* If this is not NO then the function has obviously been called twice,
|
---|
255 | which is likely to be a bug. */
|
---|
256 | if (NO != isLibLoaded)
|
---|
257 | {
|
---|
258 | AssertMsgFailed(("isLibLoaded == %s\n", YES == isLibLoaded ? "YES" : "NO"));
|
---|
259 | return YES == isLibLoaded ? VINF_SUCCESS : VERR_NOT_SUPPORTED;
|
---|
260 | }
|
---|
261 | isLibLoaded = FAIL;
|
---|
262 | int rc = RTLdrLoad(VBOX_ALSA_LIB, &hLib);
|
---|
263 | if (RT_FAILURE(rc))
|
---|
264 | {
|
---|
265 | LogRelFunc(("Failed to load library %s (%Rrc)\n", VBOX_ALSA_LIB, rc));
|
---|
266 | return rc;
|
---|
267 | }
|
---|
268 | for (uintptr_t i = 0; i < RT_ELEMENTS(SharedFuncs); i++)
|
---|
269 | {
|
---|
270 | rc = RTLdrGetSymbol(hLib, SharedFuncs[i].name, (void **)SharedFuncs[i].pfn);
|
---|
271 | if (RT_SUCCESS(rc))
|
---|
272 | { /* likely */ }
|
---|
273 | else if (SharedFuncs[i].pfnFallback && rc == VERR_SYMBOL_NOT_FOUND)
|
---|
274 | *SharedFuncs[i].pfn = SharedFuncs[i].pfnFallback;
|
---|
275 | else
|
---|
276 | {
|
---|
277 | LogRelFunc(("Failed to load library %s: Getting symbol %s failed: %Rrc\n", VBOX_ALSA_LIB, SharedFuncs[i].name, rc));
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 | }
|
---|
281 | isLibLoaded = YES;
|
---|
282 | return rc;
|
---|
283 | }
|
---|
284 |
|
---|