VirtualBox

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

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

DrvHostAudioAlsa: Rewrote the pfnStreamGetPending implementation. Didn't take draining into account nor that the delay may always be non-zero due to fixed delays. bugref:9890

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