VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/alsa_stubs.c@ 88220

Last change on this file since 88220 was 88220, checked in by vboxsync, 4 years ago

DrvHostAudioAlsa: Don't set the play threshold higher than the ALSA buffer size. duh. Restructured the open stream code. bugref:9890

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