VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostAudioAlsaStubs.cpp@ 91571

Last change on this file since 91571 was 91571, checked in by vboxsync, 3 years ago

Audio/Validation Kit: Implemented setting the system's master volume to 100% on ALSA / PulseAudio stacks. ​bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: DrvHostAudioAlsaStubs.cpp 91571 2021-10-05 15:37:20Z 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
41PROXY_STUB(snd_lib_error_set_handler, int, (snd_lib_error_handler_t handler),
42 (handler))
43PROXY_STUB(snd_strerror, const char *, (int errnum), (errnum))
44
45PROXY_STUB(snd_device_name_hint, int,
46 (int card, const char *iface, void ***hints),
47 (card, iface, hints))
48PROXY_STUB(snd_device_name_free_hint, int,
49 (void **hints),
50 (hints))
51PROXY_STUB(snd_device_name_get_hint, char *,
52 (const void *hint, const char *id),
53 (hint, id))
54
55static 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
62static int fallback_snd_device_name_free_hint(void **hints)
63{
64 RT_NOREF(hints);
65 return 0;
66}
67
68static 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
78PROXY_STUB(snd_pcm_avail_update, snd_pcm_sframes_t, (snd_pcm_t *pcm), (pcm))
79PROXY_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))
82PROXY_STUB(snd_pcm_close, int, (snd_pcm_t *pcm), (pcm))
83PROXY_STUB(snd_pcm_delay, int, (snd_pcm_t *pcm, snd_pcm_sframes_t *delayp), (pcm, delayp))
84PROXY_STUB(snd_pcm_nonblock, int, (snd_pcm_t *pcm, int *onoff),
85 (pcm, onoff))
86PROXY_STUB(snd_pcm_drain, int, (snd_pcm_t *pcm),
87 (pcm))
88PROXY_STUB(snd_pcm_drop, int, (snd_pcm_t *pcm), (pcm))
89PROXY_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))
92PROXY_STUB(snd_pcm_prepare, int, (snd_pcm_t *pcm), (pcm))
93PROXY_STUB(snd_pcm_readi, snd_pcm_sframes_t,
94 (snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size),
95 (pcm, buffer, size))
96PROXY_STUB(snd_pcm_resume, int, (snd_pcm_t *pcm), (pcm))
97PROXY_STUB(snd_pcm_set_chmap, int, (snd_pcm_t *pcm, snd_pcm_chmap_t const *map), (pcm, map))
98PROXY_STUB(snd_pcm_state, snd_pcm_state_t, (snd_pcm_t *pcm), (pcm))
99PROXY_STUB(snd_pcm_state_name, const char *, (snd_pcm_state_t state), (state))
100PROXY_STUB(snd_pcm_writei, snd_pcm_sframes_t,
101 (snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size),
102 (pcm, buffer, size))
103PROXY_STUB(snd_pcm_start, int, (snd_pcm_t *pcm), (pcm))
104
105static int fallback_snd_pcm_avail_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *availp, snd_pcm_sframes_t *delayp)
106{
107 *availp = pfn_snd_pcm_avail_update(pcm);
108 int ret = pfn_snd_pcm_delay(pcm, delayp);
109 if (ret >= 0 && *availp < 0)
110 ret = (int)*availp;
111 return ret;
112}
113
114static int fallback_snd_pcm_set_chmap(snd_pcm_t *pcm, snd_pcm_chmap_t const *map)
115{
116 RT_NOREF(pcm, map);
117 return 0;
118}
119
120/*
121 * HW
122 */
123
124PROXY_STUB(snd_pcm_hw_params, int,
125 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params),
126 (pcm, params))
127PROXY_STUB(snd_pcm_hw_params_any, int,
128 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params),
129 (pcm, params))
130PROXY_STUB(snd_pcm_hw_params_get_buffer_size, int,
131 (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
132 (params, val))
133PROXY_STUB(snd_pcm_hw_params_get_buffer_size_min, int,
134 (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
135 (params, val))
136PROXY_STUB(snd_pcm_hw_params_get_period_size, int,
137 (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir),
138 (params, frames, dir))
139PROXY_STUB(snd_pcm_hw_params_get_period_size_min, int,
140 (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir),
141 (params, frames, dir))
142PROXY_STUB(snd_pcm_hw_params_set_rate_near, int,
143 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
144 (pcm, params, val, dir))
145PROXY_STUB(snd_pcm_hw_params_set_access, int,
146 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access),
147 (pcm, params, _access))
148PROXY_STUB(snd_pcm_hw_params_set_buffer_time_near, int,
149 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
150 (pcm, params, val, dir))
151PROXY_STUB(snd_pcm_hw_params_set_buffer_size_near, int,
152 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val),
153 (pcm, params, val))
154PROXY_STUB(snd_pcm_hw_params_set_channels_near, int,
155 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val),
156 (pcm, params, val))
157PROXY_STUB(snd_pcm_hw_params_set_period_size_near, int,
158 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir),
159 (pcm, params, val, dir))
160PROXY_STUB(snd_pcm_hw_params_set_period_time_near, int,
161 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir),
162 (pcm, params, val, dir))
163PROXY_STUB(snd_pcm_hw_params_sizeof, size_t, (void), ())
164PROXY_STUB(snd_pcm_hw_params_set_format, int,
165 (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val),
166 (pcm, params, val))
167
168/*
169 * SW
170 */
171
172PROXY_STUB(snd_pcm_sw_params, int,
173 (snd_pcm_t *pcm, snd_pcm_sw_params_t *params),
174 (pcm, params))
175PROXY_STUB(snd_pcm_sw_params_current, int,
176 (snd_pcm_t *pcm, snd_pcm_sw_params_t *params),
177 (pcm, params))
178PROXY_STUB(snd_pcm_sw_params_get_start_threshold, int,
179 (const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val),
180 (params, val))
181PROXY_STUB(snd_pcm_sw_params_set_avail_min, int,
182 (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val),
183 (pcm, params, val))
184PROXY_STUB(snd_pcm_sw_params_set_start_threshold, int,
185 (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val),
186 (pcm, params, val))
187PROXY_STUB(snd_pcm_sw_params_sizeof, size_t, (void), ())
188
189/*
190 * Mixer
191 */
192
193PROXY_STUB(snd_mixer_selem_id_sizeof, size_t,
194 (void), ())
195PROXY_STUB(snd_mixer_open, int,
196 (snd_mixer_t **mixer, int mode),
197 (mixer, mode))
198PROXY_STUB(snd_mixer_attach, int,
199 (snd_mixer_t *mixer, const char *name),
200 (mixer, name))
201PROXY_STUB(snd_mixer_close, int,
202 (snd_mixer_t *mixer),
203 (mixer))
204PROXY_STUB(snd_mixer_selem_id_set_index, void,
205 (snd_mixer_selem_id_t *obj, unsigned int val),
206 (obj, val))
207PROXY_STUB(snd_mixer_selem_id_set_name, void,
208 (snd_mixer_selem_id_t *obj, const char *val),
209 (obj, val))
210PROXY_STUB(snd_mixer_selem_set_playback_volume, int,
211 (snd_mixer_elem_t *elem, snd_mixer_selem_channel_id_t channel, long value),
212 (elem, channel, value))
213PROXY_STUB(snd_mixer_selem_get_playback_volume_range, int,
214 (snd_mixer_elem_t *elem, long *min, long *max),
215 (elem, min, max))
216PROXY_STUB(snd_mixer_selem_set_capture_volume, int,
217 (snd_mixer_elem_t *elem, snd_mixer_selem_channel_id_t channel, long value),
218 (elem, channel, value))
219PROXY_STUB(snd_mixer_selem_get_capture_volume_range, int,
220 (snd_mixer_elem_t *elem, long *min, long *max),
221 (elem, min, max))
222PROXY_STUB(snd_mixer_selem_register, int,
223 (snd_mixer_t *mixer, snd_mixer_selem_regopt *options, snd_mixer_class_t **classp),
224 (mixer, options, classp))
225PROXY_STUB(snd_mixer_load, int,
226 (snd_mixer_t *mixer),
227 (mixer))
228PROXY_STUB(snd_mixer_find_selem, snd_mixer_elem_t *,
229 (snd_mixer_t *mixer, const snd_mixer_selem_id_t *id),
230 (mixer, id))
231
232typedef struct
233{
234 const char *name;
235 void (**pfn)(void);
236 void (*pfnFallback)(void);
237} SHARED_FUNC;
238
239#define ELEMENT(function) { #function , (void (**)(void)) & pfn_ ## function, NULL }
240#define ELEMENT_FALLBACK(function) { #function , (void (**)(void)) & pfn_ ## function, (void (*)(void))fallback_ ## function }
241static SHARED_FUNC SharedFuncs[] =
242{
243 ELEMENT(snd_lib_error_set_handler),
244 ELEMENT(snd_strerror),
245
246 ELEMENT_FALLBACK(snd_device_name_hint),
247 ELEMENT_FALLBACK(snd_device_name_get_hint),
248 ELEMENT_FALLBACK(snd_device_name_free_hint),
249
250 ELEMENT(snd_pcm_avail_update),
251 ELEMENT_FALLBACK(snd_pcm_avail_delay),
252 ELEMENT(snd_pcm_close),
253 ELEMENT(snd_pcm_delay),
254 ELEMENT(snd_pcm_drain),
255 ELEMENT(snd_pcm_drop),
256 ELEMENT(snd_pcm_nonblock),
257 ELEMENT(snd_pcm_open),
258 ELEMENT(snd_pcm_prepare),
259 ELEMENT(snd_pcm_resume),
260 ELEMENT_FALLBACK(snd_pcm_set_chmap),
261 ELEMENT(snd_pcm_state),
262 ELEMENT(snd_pcm_state_name),
263
264 ELEMENT(snd_pcm_readi),
265 ELEMENT(snd_pcm_start),
266 ELEMENT(snd_pcm_writei),
267
268 ELEMENT(snd_pcm_hw_params),
269 ELEMENT(snd_pcm_hw_params_any),
270 ELEMENT(snd_pcm_hw_params_sizeof),
271 ELEMENT(snd_pcm_hw_params_get_buffer_size),
272 ELEMENT(snd_pcm_hw_params_get_buffer_size_min),
273 ELEMENT(snd_pcm_hw_params_get_period_size_min),
274 ELEMENT(snd_pcm_hw_params_set_access),
275 ELEMENT(snd_pcm_hw_params_set_buffer_size_near),
276 ELEMENT(snd_pcm_hw_params_set_buffer_time_near),
277 ELEMENT(snd_pcm_hw_params_set_channels_near),
278 ELEMENT(snd_pcm_hw_params_set_format),
279 ELEMENT(snd_pcm_hw_params_get_period_size),
280 ELEMENT(snd_pcm_hw_params_set_period_size_near),
281 ELEMENT(snd_pcm_hw_params_set_period_time_near),
282 ELEMENT(snd_pcm_hw_params_set_rate_near),
283
284 ELEMENT(snd_pcm_sw_params),
285 ELEMENT(snd_pcm_sw_params_current),
286 ELEMENT(snd_pcm_sw_params_get_start_threshold),
287 ELEMENT(snd_pcm_sw_params_set_avail_min),
288 ELEMENT(snd_pcm_sw_params_set_start_threshold),
289 ELEMENT(snd_pcm_sw_params_sizeof),
290
291 ELEMENT(snd_mixer_selem_id_sizeof),
292 ELEMENT(snd_mixer_open),
293 ELEMENT(snd_mixer_attach),
294 ELEMENT(snd_mixer_close),
295 ELEMENT(snd_mixer_selem_id_set_index),
296 ELEMENT(snd_mixer_selem_id_set_name),
297 ELEMENT(snd_mixer_selem_set_playback_volume),
298 ELEMENT(snd_mixer_selem_get_playback_volume_range),
299 ELEMENT(snd_mixer_selem_set_capture_volume),
300 ELEMENT(snd_mixer_selem_get_capture_volume_range),
301 ELEMENT(snd_mixer_selem_register),
302 ELEMENT(snd_mixer_load),
303 ELEMENT(snd_mixer_find_selem),
304
305};
306#undef ELEMENT
307
308/** Init once. */
309static RTONCE g_AlsaLibInitOnce = RTONCE_INITIALIZER;
310
311
312/** @callback_method_impl{FNRTONCE} */
313static DECLCALLBACK(int32_t) drvHostAudioAlsaLibInitOnce(void *pvUser)
314{
315 RT_NOREF(pvUser);
316 LogFlowFunc(("\n"));
317
318 RTLDRMOD hMod = NIL_RTLDRMOD;
319 int rc = RTLdrLoadSystemEx(VBOX_ALSA_LIB, RTLDRLOAD_FLAGS_NO_UNLOAD, &hMod);
320 if (RT_SUCCESS(rc))
321 {
322 for (uintptr_t i = 0; i < RT_ELEMENTS(SharedFuncs); i++)
323 {
324 rc = RTLdrGetSymbol(hMod, SharedFuncs[i].name, (void **)SharedFuncs[i].pfn);
325 if (RT_SUCCESS(rc))
326 { /* likely */ }
327 else if (SharedFuncs[i].pfnFallback && rc == VERR_SYMBOL_NOT_FOUND)
328 *SharedFuncs[i].pfn = SharedFuncs[i].pfnFallback;
329 else
330 {
331 LogRelFunc(("Failed to load library %s: Getting symbol %s failed: %Rrc\n", VBOX_ALSA_LIB, SharedFuncs[i].name, rc));
332 return rc;
333 }
334 }
335 }
336 else
337 LogRelFunc(("Failed to load library %s (%Rrc)\n", VBOX_ALSA_LIB, rc));
338 return rc;
339}
340
341
342/**
343 * Try to dynamically load the ALSA libraries.
344 *
345 * @returns VBox status code.
346 */
347int audioLoadAlsaLib(void)
348{
349 LogFlowFunc(("\n"));
350 return RTOnce(&g_AlsaLibInitOnce, drvHostAudioAlsaLibInitOnce, NULL);
351}
352
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette