2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
32 #include "backends/base.h"
34 #include <alsa/asoundlib.h>
37 static const ALCchar alsaDevice
[] = "ALSA Default";
41 #define ALSA_FUNCS(MAGIC) \
42 MAGIC(snd_strerror); \
43 MAGIC(snd_pcm_open); \
44 MAGIC(snd_pcm_close); \
45 MAGIC(snd_pcm_nonblock); \
46 MAGIC(snd_pcm_frames_to_bytes); \
47 MAGIC(snd_pcm_bytes_to_frames); \
48 MAGIC(snd_pcm_hw_params_malloc); \
49 MAGIC(snd_pcm_hw_params_free); \
50 MAGIC(snd_pcm_hw_params_any); \
51 MAGIC(snd_pcm_hw_params_current); \
52 MAGIC(snd_pcm_hw_params_set_access); \
53 MAGIC(snd_pcm_hw_params_set_format); \
54 MAGIC(snd_pcm_hw_params_set_channels); \
55 MAGIC(snd_pcm_hw_params_set_periods_near); \
56 MAGIC(snd_pcm_hw_params_set_rate_near); \
57 MAGIC(snd_pcm_hw_params_set_rate); \
58 MAGIC(snd_pcm_hw_params_set_rate_resample); \
59 MAGIC(snd_pcm_hw_params_set_buffer_time_near); \
60 MAGIC(snd_pcm_hw_params_set_period_time_near); \
61 MAGIC(snd_pcm_hw_params_set_buffer_size_near); \
62 MAGIC(snd_pcm_hw_params_set_period_size_near); \
63 MAGIC(snd_pcm_hw_params_set_buffer_size_min); \
64 MAGIC(snd_pcm_hw_params_get_buffer_time_min); \
65 MAGIC(snd_pcm_hw_params_get_buffer_time_max); \
66 MAGIC(snd_pcm_hw_params_get_period_time_min); \
67 MAGIC(snd_pcm_hw_params_get_period_time_max); \
68 MAGIC(snd_pcm_hw_params_get_buffer_size); \
69 MAGIC(snd_pcm_hw_params_get_period_size); \
70 MAGIC(snd_pcm_hw_params_get_access); \
71 MAGIC(snd_pcm_hw_params_get_periods); \
72 MAGIC(snd_pcm_hw_params_test_format); \
73 MAGIC(snd_pcm_hw_params_test_channels); \
74 MAGIC(snd_pcm_hw_params); \
75 MAGIC(snd_pcm_sw_params_malloc); \
76 MAGIC(snd_pcm_sw_params_current); \
77 MAGIC(snd_pcm_sw_params_set_avail_min); \
78 MAGIC(snd_pcm_sw_params_set_stop_threshold); \
79 MAGIC(snd_pcm_sw_params); \
80 MAGIC(snd_pcm_sw_params_free); \
81 MAGIC(snd_pcm_prepare); \
82 MAGIC(snd_pcm_start); \
83 MAGIC(snd_pcm_resume); \
84 MAGIC(snd_pcm_reset); \
85 MAGIC(snd_pcm_wait); \
86 MAGIC(snd_pcm_delay); \
87 MAGIC(snd_pcm_state); \
88 MAGIC(snd_pcm_avail_update); \
89 MAGIC(snd_pcm_areas_silence); \
90 MAGIC(snd_pcm_mmap_begin); \
91 MAGIC(snd_pcm_mmap_commit); \
92 MAGIC(snd_pcm_readi); \
93 MAGIC(snd_pcm_writei); \
94 MAGIC(snd_pcm_drain); \
95 MAGIC(snd_pcm_drop); \
96 MAGIC(snd_pcm_recover); \
97 MAGIC(snd_pcm_info_malloc); \
98 MAGIC(snd_pcm_info_free); \
99 MAGIC(snd_pcm_info_set_device); \
100 MAGIC(snd_pcm_info_set_subdevice); \
101 MAGIC(snd_pcm_info_set_stream); \
102 MAGIC(snd_pcm_info_get_name); \
103 MAGIC(snd_ctl_pcm_next_device); \
104 MAGIC(snd_ctl_pcm_info); \
105 MAGIC(snd_ctl_open); \
106 MAGIC(snd_ctl_close); \
107 MAGIC(snd_ctl_card_info_malloc); \
108 MAGIC(snd_ctl_card_info_free); \
109 MAGIC(snd_ctl_card_info); \
110 MAGIC(snd_ctl_card_info_get_name); \
111 MAGIC(snd_ctl_card_info_get_id); \
112 MAGIC(snd_card_next); \
113 MAGIC(snd_config_update_free_global)
115 static void *alsa_handle
;
116 #define MAKE_FUNC(f) static __typeof(f) * p##f
117 ALSA_FUNCS(MAKE_FUNC
);
120 #define snd_strerror psnd_strerror
121 #define snd_pcm_open psnd_pcm_open
122 #define snd_pcm_close psnd_pcm_close
123 #define snd_pcm_nonblock psnd_pcm_nonblock
124 #define snd_pcm_frames_to_bytes psnd_pcm_frames_to_bytes
125 #define snd_pcm_bytes_to_frames psnd_pcm_bytes_to_frames
126 #define snd_pcm_hw_params_malloc psnd_pcm_hw_params_malloc
127 #define snd_pcm_hw_params_free psnd_pcm_hw_params_free
128 #define snd_pcm_hw_params_any psnd_pcm_hw_params_any
129 #define snd_pcm_hw_params_current psnd_pcm_hw_params_current
130 #define snd_pcm_hw_params_set_access psnd_pcm_hw_params_set_access
131 #define snd_pcm_hw_params_set_format psnd_pcm_hw_params_set_format
132 #define snd_pcm_hw_params_set_channels psnd_pcm_hw_params_set_channels
133 #define snd_pcm_hw_params_set_periods_near psnd_pcm_hw_params_set_periods_near
134 #define snd_pcm_hw_params_set_rate_near psnd_pcm_hw_params_set_rate_near
135 #define snd_pcm_hw_params_set_rate psnd_pcm_hw_params_set_rate
136 #define snd_pcm_hw_params_set_rate_resample psnd_pcm_hw_params_set_rate_resample
137 #define snd_pcm_hw_params_set_buffer_time_near psnd_pcm_hw_params_set_buffer_time_near
138 #define snd_pcm_hw_params_set_period_time_near psnd_pcm_hw_params_set_period_time_near
139 #define snd_pcm_hw_params_set_buffer_size_near psnd_pcm_hw_params_set_buffer_size_near
140 #define snd_pcm_hw_params_set_period_size_near psnd_pcm_hw_params_set_period_size_near
141 #define snd_pcm_hw_params_set_buffer_size_min psnd_pcm_hw_params_set_buffer_size_min
142 #define snd_pcm_hw_params_get_buffer_time_min psnd_pcm_hw_params_get_buffer_time_min
143 #define snd_pcm_hw_params_get_buffer_time_max psnd_pcm_hw_params_get_buffer_time_max
144 #define snd_pcm_hw_params_get_period_time_min psnd_pcm_hw_params_get_period_time_min
145 #define snd_pcm_hw_params_get_period_time_max psnd_pcm_hw_params_get_period_time_max
146 #define snd_pcm_hw_params_get_buffer_size psnd_pcm_hw_params_get_buffer_size
147 #define snd_pcm_hw_params_get_period_size psnd_pcm_hw_params_get_period_size
148 #define snd_pcm_hw_params_get_access psnd_pcm_hw_params_get_access
149 #define snd_pcm_hw_params_get_periods psnd_pcm_hw_params_get_periods
150 #define snd_pcm_hw_params_test_format psnd_pcm_hw_params_test_format
151 #define snd_pcm_hw_params_test_channels psnd_pcm_hw_params_test_channels
152 #define snd_pcm_hw_params psnd_pcm_hw_params
153 #define snd_pcm_sw_params_malloc psnd_pcm_sw_params_malloc
154 #define snd_pcm_sw_params_current psnd_pcm_sw_params_current
155 #define snd_pcm_sw_params_set_avail_min psnd_pcm_sw_params_set_avail_min
156 #define snd_pcm_sw_params_set_stop_threshold psnd_pcm_sw_params_set_stop_threshold
157 #define snd_pcm_sw_params psnd_pcm_sw_params
158 #define snd_pcm_sw_params_free psnd_pcm_sw_params_free
159 #define snd_pcm_prepare psnd_pcm_prepare
160 #define snd_pcm_start psnd_pcm_start
161 #define snd_pcm_resume psnd_pcm_resume
162 #define snd_pcm_reset psnd_pcm_reset
163 #define snd_pcm_wait psnd_pcm_wait
164 #define snd_pcm_delay psnd_pcm_delay
165 #define snd_pcm_state psnd_pcm_state
166 #define snd_pcm_avail_update psnd_pcm_avail_update
167 #define snd_pcm_areas_silence psnd_pcm_areas_silence
168 #define snd_pcm_mmap_begin psnd_pcm_mmap_begin
169 #define snd_pcm_mmap_commit psnd_pcm_mmap_commit
170 #define snd_pcm_readi psnd_pcm_readi
171 #define snd_pcm_writei psnd_pcm_writei
172 #define snd_pcm_drain psnd_pcm_drain
173 #define snd_pcm_drop psnd_pcm_drop
174 #define snd_pcm_recover psnd_pcm_recover
175 #define snd_pcm_info_malloc psnd_pcm_info_malloc
176 #define snd_pcm_info_free psnd_pcm_info_free
177 #define snd_pcm_info_set_device psnd_pcm_info_set_device
178 #define snd_pcm_info_set_subdevice psnd_pcm_info_set_subdevice
179 #define snd_pcm_info_set_stream psnd_pcm_info_set_stream
180 #define snd_pcm_info_get_name psnd_pcm_info_get_name
181 #define snd_ctl_pcm_next_device psnd_ctl_pcm_next_device
182 #define snd_ctl_pcm_info psnd_ctl_pcm_info
183 #define snd_ctl_open psnd_ctl_open
184 #define snd_ctl_close psnd_ctl_close
185 #define snd_ctl_card_info_malloc psnd_ctl_card_info_malloc
186 #define snd_ctl_card_info_free psnd_ctl_card_info_free
187 #define snd_ctl_card_info psnd_ctl_card_info
188 #define snd_ctl_card_info_get_name psnd_ctl_card_info_get_name
189 #define snd_ctl_card_info_get_id psnd_ctl_card_info_get_id
190 #define snd_card_next psnd_card_next
191 #define snd_config_update_free_global psnd_config_update_free_global
195 static ALCboolean
alsa_load(void)
197 ALCboolean error
= ALC_FALSE
;
202 alsa_handle
= LoadLib("libasound.so.2");
207 #define LOAD_FUNC(f) do { \
208 p##f = GetSymbol(alsa_handle, #f); \
213 ALSA_FUNCS(LOAD_FUNC
);
218 CloseLib(alsa_handle
);
231 al_string device_name
;
233 TYPEDEF_VECTOR(DevMap
, vector_DevMap
)
235 static vector_DevMap PlaybackDevices
;
236 static vector_DevMap CaptureDevices
;
238 static void clear_devlist(vector_DevMap
*devlist
)
240 #define FREE_DEV(i) do { \
241 AL_STRING_DEINIT((i)->name); \
242 AL_STRING_DEINIT((i)->device_name); \
244 VECTOR_FOR_EACH(DevMap
, *devlist
, FREE_DEV
);
245 VECTOR_RESIZE(*devlist
, 0);
250 static const char *prefix_name(snd_pcm_stream_t stream
)
252 assert(stream
== SND_PCM_STREAM_PLAYBACK
|| stream
== SND_PCM_STREAM_CAPTURE
);
253 return (stream
==SND_PCM_STREAM_PLAYBACK
) ? "device-prefix" : "capture-prefix";
256 static void probe_devices(snd_pcm_stream_t stream
, vector_DevMap
*DeviceList
)
258 const char *main_prefix
= "plughw:";
260 snd_ctl_card_info_t
*info
;
261 snd_pcm_info_t
*pcminfo
;
265 clear_devlist(DeviceList
);
267 snd_ctl_card_info_malloc(&info
);
268 snd_pcm_info_malloc(&pcminfo
);
270 AL_STRING_INIT(entry
.name
);
271 AL_STRING_INIT(entry
.device_name
);
272 al_string_copy_cstr(&entry
.name
, alsaDevice
);
273 al_string_copy_cstr(&entry
.device_name
, GetConfigValue(NULL
, "alsa", (stream
==SND_PCM_STREAM_PLAYBACK
) ?
274 "device" : "capture", "default"));
275 VECTOR_PUSH_BACK(*DeviceList
, entry
);
278 if((err
=snd_card_next(&card
)) < 0)
279 ERR("Failed to find a card: %s\n", snd_strerror(err
));
280 ConfigValueStr(NULL
, "alsa", prefix_name(stream
), &main_prefix
);
283 const char *card_prefix
= main_prefix
;
284 const char *cardname
, *cardid
;
287 snprintf(name
, sizeof(name
), "hw:%d", card
);
288 if((err
= snd_ctl_open(&handle
, name
, 0)) < 0)
290 ERR("control open (hw:%d): %s\n", card
, snd_strerror(err
));
293 if((err
= snd_ctl_card_info(handle
, info
)) < 0)
295 ERR("control hardware info (hw:%d): %s\n", card
, snd_strerror(err
));
296 snd_ctl_close(handle
);
300 cardname
= snd_ctl_card_info_get_name(info
);
301 cardid
= snd_ctl_card_info_get_id(info
);
303 snprintf(name
, sizeof(name
), "%s-%s", prefix_name(stream
), cardid
);
304 ConfigValueStr(NULL
, "alsa", name
, &card_prefix
);
309 const char *device_prefix
= card_prefix
;
313 if(snd_ctl_pcm_next_device(handle
, &dev
) < 0)
314 ERR("snd_ctl_pcm_next_device failed\n");
318 snd_pcm_info_set_device(pcminfo
, dev
);
319 snd_pcm_info_set_subdevice(pcminfo
, 0);
320 snd_pcm_info_set_stream(pcminfo
, stream
);
321 if((err
= snd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
323 ERR("control digital audio info (hw:%d): %s\n", card
, snd_strerror(err
));
327 devname
= snd_pcm_info_get_name(pcminfo
);
329 snprintf(name
, sizeof(name
), "%s-%s-%d", prefix_name(stream
), cardid
, dev
);
330 ConfigValueStr(NULL
, "alsa", name
, &device_prefix
);
332 snprintf(name
, sizeof(name
), "%s, %s (CARD=%s,DEV=%d)",
333 cardname
, devname
, cardid
, dev
);
334 snprintf(device
, sizeof(device
), "%sCARD=%s,DEV=%d",
335 device_prefix
, cardid
, dev
);
337 TRACE("Got device \"%s\", \"%s\"\n", name
, device
);
338 AL_STRING_INIT(entry
.name
);
339 AL_STRING_INIT(entry
.device_name
);
340 al_string_copy_cstr(&entry
.name
, name
);
341 al_string_copy_cstr(&entry
.device_name
, device
);
342 VECTOR_PUSH_BACK(*DeviceList
, entry
);
344 snd_ctl_close(handle
);
346 if(snd_card_next(&card
) < 0) {
347 ERR("snd_card_next failed\n");
352 snd_pcm_info_free(pcminfo
);
353 snd_ctl_card_info_free(info
);
357 static int verify_state(snd_pcm_t
*handle
)
359 snd_pcm_state_t state
= snd_pcm_state(handle
);
364 case SND_PCM_STATE_OPEN
:
365 case SND_PCM_STATE_SETUP
:
366 case SND_PCM_STATE_PREPARED
:
367 case SND_PCM_STATE_RUNNING
:
368 case SND_PCM_STATE_DRAINING
:
369 case SND_PCM_STATE_PAUSED
:
373 case SND_PCM_STATE_XRUN
:
374 if((err
=snd_pcm_recover(handle
, -EPIPE
, 1)) < 0)
377 case SND_PCM_STATE_SUSPENDED
:
378 if((err
=snd_pcm_recover(handle
, -ESTRPIPE
, 1)) < 0)
381 case SND_PCM_STATE_DISCONNECTED
:
389 typedef struct ALCplaybackAlsa
{
390 DERIVE_FROM_TYPE(ALCbackend
);
392 snd_pcm_t
*pcmHandle
;
397 volatile int killNow
;
401 static int ALCplaybackAlsa_mixerProc(void *ptr
);
402 static int ALCplaybackAlsa_mixerNoMMapProc(void *ptr
);
404 static void ALCplaybackAlsa_Construct(ALCplaybackAlsa
*self
, ALCdevice
*device
);
405 static DECLARE_FORWARD(ALCplaybackAlsa
, ALCbackend
, void, Destruct
)
406 static ALCenum
ALCplaybackAlsa_open(ALCplaybackAlsa
*self
, const ALCchar
*name
);
407 static void ALCplaybackAlsa_close(ALCplaybackAlsa
*self
);
408 static ALCboolean
ALCplaybackAlsa_reset(ALCplaybackAlsa
*self
);
409 static ALCboolean
ALCplaybackAlsa_start(ALCplaybackAlsa
*self
);
410 static void ALCplaybackAlsa_stop(ALCplaybackAlsa
*self
);
411 static DECLARE_FORWARD2(ALCplaybackAlsa
, ALCbackend
, ALCenum
, captureSamples
, void*, ALCuint
)
412 static DECLARE_FORWARD(ALCplaybackAlsa
, ALCbackend
, ALCuint
, availableSamples
)
413 static ClockLatency
ALCplaybackAlsa_getClockLatency(ALCplaybackAlsa
*self
);
414 static DECLARE_FORWARD(ALCplaybackAlsa
, ALCbackend
, void, lock
)
415 static DECLARE_FORWARD(ALCplaybackAlsa
, ALCbackend
, void, unlock
)
416 DECLARE_DEFAULT_ALLOCATORS(ALCplaybackAlsa
)
418 DEFINE_ALCBACKEND_VTABLE(ALCplaybackAlsa
);
421 static void ALCplaybackAlsa_Construct(ALCplaybackAlsa
*self
, ALCdevice
*device
)
423 ALCbackend_Construct(STATIC_CAST(ALCbackend
, self
), device
);
424 SET_VTABLE2(ALCplaybackAlsa
, ALCbackend
, self
);
428 static int ALCplaybackAlsa_mixerProc(void *ptr
)
430 ALCplaybackAlsa
*self
= (ALCplaybackAlsa
*)ptr
;
431 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
432 const snd_pcm_channel_area_t
*areas
= NULL
;
433 snd_pcm_uframes_t update_size
, num_updates
;
434 snd_pcm_sframes_t avail
, commitres
;
435 snd_pcm_uframes_t offset
, frames
;
440 althrd_setname(althrd_current(), MIXER_THREAD_NAME
);
442 update_size
= device
->UpdateSize
;
443 num_updates
= device
->NumUpdates
;
444 while(!self
->killNow
)
446 int state
= verify_state(self
->pcmHandle
);
449 ERR("Invalid state detected: %s\n", snd_strerror(state
));
450 ALCplaybackAlsa_lock(self
);
451 aluHandleDisconnect(device
);
452 ALCplaybackAlsa_unlock(self
);
456 avail
= snd_pcm_avail_update(self
->pcmHandle
);
459 ERR("available update failed: %s\n", snd_strerror(avail
));
463 if((snd_pcm_uframes_t
)avail
> update_size
*(num_updates
+1))
465 WARN("available samples exceeds the buffer size\n");
466 snd_pcm_reset(self
->pcmHandle
);
470 // make sure there's frames to process
471 if((snd_pcm_uframes_t
)avail
< update_size
)
473 if(state
!= SND_PCM_STATE_RUNNING
)
475 err
= snd_pcm_start(self
->pcmHandle
);
478 ERR("start failed: %s\n", snd_strerror(err
));
482 if(snd_pcm_wait(self
->pcmHandle
, 1000) == 0)
483 ERR("Wait timeout... buffer size too low?\n");
486 avail
-= avail
%update_size
;
488 // it is possible that contiguous areas are smaller, thus we use a loop
489 ALCplaybackAlsa_lock(self
);
494 err
= snd_pcm_mmap_begin(self
->pcmHandle
, &areas
, &offset
, &frames
);
497 ERR("mmap begin error: %s\n", snd_strerror(err
));
501 WritePtr
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
502 aluMixData(device
, WritePtr
, frames
);
504 commitres
= snd_pcm_mmap_commit(self
->pcmHandle
, offset
, frames
);
505 if(commitres
< 0 || (commitres
-frames
) != 0)
507 ERR("mmap commit error: %s\n",
508 snd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
514 ALCplaybackAlsa_unlock(self
);
520 static int ALCplaybackAlsa_mixerNoMMapProc(void *ptr
)
522 ALCplaybackAlsa
*self
= (ALCplaybackAlsa
*)ptr
;
523 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
524 snd_pcm_uframes_t update_size
, num_updates
;
525 snd_pcm_sframes_t avail
;
530 althrd_setname(althrd_current(), MIXER_THREAD_NAME
);
532 update_size
= device
->UpdateSize
;
533 num_updates
= device
->NumUpdates
;
534 while(!self
->killNow
)
536 int state
= verify_state(self
->pcmHandle
);
539 ERR("Invalid state detected: %s\n", snd_strerror(state
));
540 ALCplaybackAlsa_lock(self
);
541 aluHandleDisconnect(device
);
542 ALCplaybackAlsa_unlock(self
);
546 avail
= snd_pcm_avail_update(self
->pcmHandle
);
549 ERR("available update failed: %s\n", snd_strerror(avail
));
553 if((snd_pcm_uframes_t
)avail
> update_size
*num_updates
)
555 WARN("available samples exceeds the buffer size\n");
556 snd_pcm_reset(self
->pcmHandle
);
560 if((snd_pcm_uframes_t
)avail
< update_size
)
562 if(state
!= SND_PCM_STATE_RUNNING
)
564 err
= snd_pcm_start(self
->pcmHandle
);
567 ERR("start failed: %s\n", snd_strerror(err
));
571 if(snd_pcm_wait(self
->pcmHandle
, 1000) == 0)
572 ERR("Wait timeout... buffer size too low?\n");
576 ALCplaybackAlsa_lock(self
);
577 WritePtr
= self
->buffer
;
578 avail
= snd_pcm_bytes_to_frames(self
->pcmHandle
, self
->size
);
579 aluMixData(device
, WritePtr
, avail
);
583 int ret
= snd_pcm_writei(self
->pcmHandle
, WritePtr
, avail
);
588 #if ESTRPIPE != EPIPE
593 ret
= snd_pcm_recover(self
->pcmHandle
, ret
, 1);
600 WritePtr
+= snd_pcm_frames_to_bytes(self
->pcmHandle
, ret
);
607 ret
= snd_pcm_prepare(self
->pcmHandle
);
612 ALCplaybackAlsa_unlock(self
);
619 static ALCenum
ALCplaybackAlsa_open(ALCplaybackAlsa
*self
, const ALCchar
*name
)
621 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
622 const char *driver
= NULL
;
629 if(VECTOR_SIZE(PlaybackDevices
) == 0)
630 probe_devices(SND_PCM_STREAM_PLAYBACK
, &PlaybackDevices
);
632 #define MATCH_NAME(i) (al_string_cmp_cstr((i)->name, name) == 0)
633 VECTOR_FIND_IF(iter
, const DevMap
, PlaybackDevices
, MATCH_NAME
);
635 if(iter
== VECTOR_END(PlaybackDevices
))
636 return ALC_INVALID_VALUE
;
637 driver
= al_string_get_cstr(iter
->device_name
);
642 driver
= GetConfigValue(NULL
, "alsa", "device", "default");
645 TRACE("Opening device \"%s\"\n", driver
);
646 err
= snd_pcm_open(&self
->pcmHandle
, driver
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
649 ERR("Could not open playback device '%s': %s\n", driver
, snd_strerror(err
));
650 return ALC_OUT_OF_MEMORY
;
653 /* Free alsa's global config tree. Otherwise valgrind reports a ton of leaks. */
654 snd_config_update_free_global();
656 al_string_copy_cstr(&device
->DeviceName
, name
);
661 static void ALCplaybackAlsa_close(ALCplaybackAlsa
*self
)
663 snd_pcm_close(self
->pcmHandle
);
666 static ALCboolean
ALCplaybackAlsa_reset(ALCplaybackAlsa
*self
)
668 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
669 snd_pcm_uframes_t periodSizeInFrames
;
670 unsigned int periodLen
, bufferLen
;
671 snd_pcm_sw_params_t
*sp
= NULL
;
672 snd_pcm_hw_params_t
*hp
= NULL
;
673 snd_pcm_format_t format
= -1;
674 snd_pcm_access_t access
;
675 unsigned int periods
;
682 switch(device
->FmtType
)
685 format
= SND_PCM_FORMAT_S8
;
688 format
= SND_PCM_FORMAT_U8
;
691 format
= SND_PCM_FORMAT_S16
;
694 format
= SND_PCM_FORMAT_U16
;
697 format
= SND_PCM_FORMAT_S32
;
700 format
= SND_PCM_FORMAT_U32
;
703 format
= SND_PCM_FORMAT_FLOAT
;
707 allowmmap
= GetConfigValueBool(al_string_get_cstr(device
->DeviceName
), "alsa", "mmap", 1);
708 periods
= device
->NumUpdates
;
709 periodLen
= (ALuint64
)device
->UpdateSize
* 1000000 / device
->Frequency
;
710 bufferLen
= periodLen
* periods
;
711 rate
= device
->Frequency
;
713 snd_pcm_hw_params_malloc(&hp
);
714 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
715 CHECK(snd_pcm_hw_params_any(self
->pcmHandle
, hp
));
716 /* set interleaved access */
717 if(!allowmmap
|| snd_pcm_hw_params_set_access(self
->pcmHandle
, hp
, SND_PCM_ACCESS_MMAP_INTERLEAVED
) < 0)
720 CHECK(snd_pcm_hw_params_set_access(self
->pcmHandle
, hp
, SND_PCM_ACCESS_RW_INTERLEAVED
));
722 /* test and set format (implicitly sets sample bits) */
723 if(snd_pcm_hw_params_test_format(self
->pcmHandle
, hp
, format
) < 0)
725 static const struct {
726 snd_pcm_format_t format
;
727 enum DevFmtType fmttype
;
729 { SND_PCM_FORMAT_FLOAT
, DevFmtFloat
},
730 { SND_PCM_FORMAT_S32
, DevFmtInt
},
731 { SND_PCM_FORMAT_U32
, DevFmtUInt
},
732 { SND_PCM_FORMAT_S16
, DevFmtShort
},
733 { SND_PCM_FORMAT_U16
, DevFmtUShort
},
734 { SND_PCM_FORMAT_S8
, DevFmtByte
},
735 { SND_PCM_FORMAT_U8
, DevFmtUByte
},
739 for(k
= 0;k
< COUNTOF(formatlist
);k
++)
741 format
= formatlist
[k
].format
;
742 if(snd_pcm_hw_params_test_format(self
->pcmHandle
, hp
, format
) >= 0)
744 device
->FmtType
= formatlist
[k
].fmttype
;
749 CHECK(snd_pcm_hw_params_set_format(self
->pcmHandle
, hp
, format
));
750 /* test and set channels (implicitly sets frame bits) */
751 if(snd_pcm_hw_params_test_channels(self
->pcmHandle
, hp
, ChannelsFromDevFmt(device
->FmtChans
)) < 0)
753 static const enum DevFmtChannels channellist
[] = {
762 for(k
= 0;k
< COUNTOF(channellist
);k
++)
764 if(snd_pcm_hw_params_test_channels(self
->pcmHandle
, hp
, ChannelsFromDevFmt(channellist
[k
])) >= 0)
766 device
->FmtChans
= channellist
[k
];
771 CHECK(snd_pcm_hw_params_set_channels(self
->pcmHandle
, hp
, ChannelsFromDevFmt(device
->FmtChans
)));
772 /* set rate (implicitly constrains period/buffer parameters) */
773 if(!GetConfigValueBool(al_string_get_cstr(device
->DeviceName
), "alsa", "allow-resampler", 0) ||
774 !(device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
776 if(snd_pcm_hw_params_set_rate_resample(self
->pcmHandle
, hp
, 0) < 0)
777 ERR("Failed to disable ALSA resampler\n");
779 else if(snd_pcm_hw_params_set_rate_resample(self
->pcmHandle
, hp
, 1) < 0)
780 ERR("Failed to enable ALSA resampler\n");
781 CHECK(snd_pcm_hw_params_set_rate_near(self
->pcmHandle
, hp
, &rate
, NULL
));
782 /* set buffer time (implicitly constrains period/buffer parameters) */
783 if((err
=snd_pcm_hw_params_set_buffer_time_near(self
->pcmHandle
, hp
, &bufferLen
, NULL
)) < 0)
784 ERR("snd_pcm_hw_params_set_buffer_time_near failed: %s\n", snd_strerror(err
));
785 /* set period time (implicitly sets buffer size/bytes/time and period size/bytes) */
786 if((err
=snd_pcm_hw_params_set_period_time_near(self
->pcmHandle
, hp
, &periodLen
, NULL
)) < 0)
787 ERR("snd_pcm_hw_params_set_period_time_near failed: %s\n", snd_strerror(err
));
788 /* install and prepare hardware configuration */
789 CHECK(snd_pcm_hw_params(self
->pcmHandle
, hp
));
790 /* retrieve configuration info */
791 CHECK(snd_pcm_hw_params_get_access(hp
, &access
));
792 CHECK(snd_pcm_hw_params_get_period_size(hp
, &periodSizeInFrames
, NULL
));
793 CHECK(snd_pcm_hw_params_get_periods(hp
, &periods
, &dir
));
795 WARN("Inexact period count: %u (%d)\n", periods
, dir
);
797 snd_pcm_hw_params_free(hp
);
799 snd_pcm_sw_params_malloc(&sp
);
801 CHECK(snd_pcm_sw_params_current(self
->pcmHandle
, sp
));
802 CHECK(snd_pcm_sw_params_set_avail_min(self
->pcmHandle
, sp
, periodSizeInFrames
));
803 CHECK(snd_pcm_sw_params_set_stop_threshold(self
->pcmHandle
, sp
, periodSizeInFrames
*periods
));
804 CHECK(snd_pcm_sw_params(self
->pcmHandle
, sp
));
806 snd_pcm_sw_params_free(sp
);
809 device
->NumUpdates
= periods
;
810 device
->UpdateSize
= periodSizeInFrames
;
811 device
->Frequency
= rate
;
813 SetDefaultChannelOrder(device
);
818 ERR("%s failed: %s\n", funcerr
, snd_strerror(err
));
819 if(hp
) snd_pcm_hw_params_free(hp
);
820 if(sp
) snd_pcm_sw_params_free(sp
);
824 static ALCboolean
ALCplaybackAlsa_start(ALCplaybackAlsa
*self
)
826 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
827 int (*thread_func
)(void*) = NULL
;
828 snd_pcm_hw_params_t
*hp
= NULL
;
829 snd_pcm_access_t access
;
833 snd_pcm_hw_params_malloc(&hp
);
834 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
835 CHECK(snd_pcm_hw_params_current(self
->pcmHandle
, hp
));
836 /* retrieve configuration info */
837 CHECK(snd_pcm_hw_params_get_access(hp
, &access
));
839 snd_pcm_hw_params_free(hp
);
842 self
->size
= snd_pcm_frames_to_bytes(self
->pcmHandle
, device
->UpdateSize
);
843 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
845 self
->buffer
= al_malloc(16, self
->size
);
848 ERR("buffer malloc failed\n");
851 thread_func
= ALCplaybackAlsa_mixerNoMMapProc
;
855 err
= snd_pcm_prepare(self
->pcmHandle
);
858 ERR("snd_pcm_prepare(data->pcmHandle) failed: %s\n", snd_strerror(err
));
861 thread_func
= ALCplaybackAlsa_mixerProc
;
864 if(althrd_create(&self
->thread
, thread_func
, self
) != althrd_success
)
866 ERR("Could not create playback thread\n");
867 al_free(self
->buffer
);
875 ERR("%s failed: %s\n", funcerr
, snd_strerror(err
));
876 if(hp
) snd_pcm_hw_params_free(hp
);
880 static void ALCplaybackAlsa_stop(ALCplaybackAlsa
*self
)
888 althrd_join(self
->thread
, &res
);
890 al_free(self
->buffer
);
894 static ClockLatency
ALCplaybackAlsa_getClockLatency(ALCplaybackAlsa
*self
)
896 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
897 snd_pcm_sframes_t delay
= 0;
901 ALCplaybackAlsa_lock(self
);
902 ret
.ClockTime
= GetDeviceClockTime(device
);
903 if((err
=snd_pcm_delay(self
->pcmHandle
, &delay
)) < 0)
905 ERR("Failed to get pcm delay: %s\n", snd_strerror(err
));
908 if(delay
< 0) delay
= 0;
909 ret
.Latency
= delay
* DEVICE_CLOCK_RES
/ device
->Frequency
;
910 ALCplaybackAlsa_unlock(self
);
916 typedef struct ALCcaptureAlsa
{
917 DERIVE_FROM_TYPE(ALCbackend
);
919 snd_pcm_t
*pcmHandle
;
925 ll_ringbuffer_t
*ring
;
927 snd_pcm_sframes_t last_avail
;
930 static void ALCcaptureAlsa_Construct(ALCcaptureAlsa
*self
, ALCdevice
*device
);
931 static DECLARE_FORWARD(ALCcaptureAlsa
, ALCbackend
, void, Destruct
)
932 static ALCenum
ALCcaptureAlsa_open(ALCcaptureAlsa
*self
, const ALCchar
*name
);
933 static void ALCcaptureAlsa_close(ALCcaptureAlsa
*self
);
934 static DECLARE_FORWARD(ALCcaptureAlsa
, ALCbackend
, ALCboolean
, reset
)
935 static ALCboolean
ALCcaptureAlsa_start(ALCcaptureAlsa
*self
);
936 static void ALCcaptureAlsa_stop(ALCcaptureAlsa
*self
);
937 static ALCenum
ALCcaptureAlsa_captureSamples(ALCcaptureAlsa
*self
, ALCvoid
*buffer
, ALCuint samples
);
938 static ALCuint
ALCcaptureAlsa_availableSamples(ALCcaptureAlsa
*self
);
939 static ClockLatency
ALCcaptureAlsa_getClockLatency(ALCcaptureAlsa
*self
);
940 static DECLARE_FORWARD(ALCcaptureAlsa
, ALCbackend
, void, lock
)
941 static DECLARE_FORWARD(ALCcaptureAlsa
, ALCbackend
, void, unlock
)
942 DECLARE_DEFAULT_ALLOCATORS(ALCcaptureAlsa
)
944 DEFINE_ALCBACKEND_VTABLE(ALCcaptureAlsa
);
947 static void ALCcaptureAlsa_Construct(ALCcaptureAlsa
*self
, ALCdevice
*device
)
949 ALCbackend_Construct(STATIC_CAST(ALCbackend
, self
), device
);
950 SET_VTABLE2(ALCcaptureAlsa
, ALCbackend
, self
);
954 static ALCenum
ALCcaptureAlsa_open(ALCcaptureAlsa
*self
, const ALCchar
*name
)
956 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
957 const char *driver
= NULL
;
958 snd_pcm_hw_params_t
*hp
;
959 snd_pcm_uframes_t bufferSizeInFrames
;
960 snd_pcm_uframes_t periodSizeInFrames
;
961 ALboolean needring
= AL_FALSE
;
962 snd_pcm_format_t format
= -1;
970 if(VECTOR_SIZE(CaptureDevices
) == 0)
971 probe_devices(SND_PCM_STREAM_CAPTURE
, &CaptureDevices
);
973 #define MATCH_NAME(i) (al_string_cmp_cstr((i)->name, name) == 0)
974 VECTOR_FIND_IF(iter
, const DevMap
, CaptureDevices
, MATCH_NAME
);
976 if(iter
== VECTOR_END(CaptureDevices
))
977 return ALC_INVALID_VALUE
;
978 driver
= al_string_get_cstr(iter
->device_name
);
983 driver
= GetConfigValue(NULL
, "alsa", "capture", "default");
986 TRACE("Opening device \"%s\"\n", driver
);
987 err
= snd_pcm_open(&self
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
990 ERR("Could not open capture device '%s': %s\n", driver
, snd_strerror(err
));
991 return ALC_INVALID_VALUE
;
994 /* Free alsa's global config tree. Otherwise valgrind reports a ton of leaks. */
995 snd_config_update_free_global();
997 switch(device
->FmtType
)
1000 format
= SND_PCM_FORMAT_S8
;
1003 format
= SND_PCM_FORMAT_U8
;
1006 format
= SND_PCM_FORMAT_S16
;
1009 format
= SND_PCM_FORMAT_U16
;
1012 format
= SND_PCM_FORMAT_S32
;
1015 format
= SND_PCM_FORMAT_U32
;
1018 format
= SND_PCM_FORMAT_FLOAT
;
1023 bufferSizeInFrames
= maxu(device
->UpdateSize
*device
->NumUpdates
,
1024 100*device
->Frequency
/1000);
1025 periodSizeInFrames
= minu(bufferSizeInFrames
, 25*device
->Frequency
/1000);
1027 snd_pcm_hw_params_malloc(&hp
);
1028 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
1029 CHECK(snd_pcm_hw_params_any(self
->pcmHandle
, hp
));
1030 /* set interleaved access */
1031 CHECK(snd_pcm_hw_params_set_access(self
->pcmHandle
, hp
, SND_PCM_ACCESS_RW_INTERLEAVED
));
1032 /* set format (implicitly sets sample bits) */
1033 CHECK(snd_pcm_hw_params_set_format(self
->pcmHandle
, hp
, format
));
1034 /* set channels (implicitly sets frame bits) */
1035 CHECK(snd_pcm_hw_params_set_channels(self
->pcmHandle
, hp
, ChannelsFromDevFmt(device
->FmtChans
)));
1036 /* set rate (implicitly constrains period/buffer parameters) */
1037 CHECK(snd_pcm_hw_params_set_rate(self
->pcmHandle
, hp
, device
->Frequency
, 0));
1038 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
1039 if(snd_pcm_hw_params_set_buffer_size_min(self
->pcmHandle
, hp
, &bufferSizeInFrames
) < 0)
1041 TRACE("Buffer too large, using intermediate ring buffer\n");
1043 CHECK(snd_pcm_hw_params_set_buffer_size_near(self
->pcmHandle
, hp
, &bufferSizeInFrames
));
1045 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
1046 CHECK(snd_pcm_hw_params_set_period_size_near(self
->pcmHandle
, hp
, &periodSizeInFrames
, NULL
));
1047 /* install and prepare hardware configuration */
1048 CHECK(snd_pcm_hw_params(self
->pcmHandle
, hp
));
1049 /* retrieve configuration info */
1050 CHECK(snd_pcm_hw_params_get_period_size(hp
, &periodSizeInFrames
, NULL
));
1052 snd_pcm_hw_params_free(hp
);
1057 self
->ring
= ll_ringbuffer_create(
1058 device
->UpdateSize
*device
->NumUpdates
+ 1,
1059 FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
)
1063 ERR("ring buffer create failed\n");
1068 al_string_copy_cstr(&device
->DeviceName
, name
);
1070 return ALC_NO_ERROR
;
1073 ERR("%s failed: %s\n", funcerr
, snd_strerror(err
));
1074 if(hp
) snd_pcm_hw_params_free(hp
);
1077 ll_ringbuffer_free(self
->ring
);
1079 snd_pcm_close(self
->pcmHandle
);
1081 return ALC_INVALID_VALUE
;
1084 static void ALCcaptureAlsa_close(ALCcaptureAlsa
*self
)
1086 snd_pcm_close(self
->pcmHandle
);
1087 ll_ringbuffer_free(self
->ring
);
1089 al_free(self
->buffer
);
1090 self
->buffer
= NULL
;
1093 static ALCboolean
ALCcaptureAlsa_start(ALCcaptureAlsa
*self
)
1095 int err
= snd_pcm_start(self
->pcmHandle
);
1098 ERR("start failed: %s\n", snd_strerror(err
));
1099 aluHandleDisconnect(STATIC_CAST(ALCbackend
, self
)->mDevice
);
1103 self
->doCapture
= AL_TRUE
;
1107 static void ALCcaptureAlsa_stop(ALCcaptureAlsa
*self
)
1112 /* OpenAL requires access to unread audio after stopping, but ALSA's
1113 * snd_pcm_drain is unreliable and snd_pcm_drop drops it. Capture what's
1114 * available now so it'll be available later after the drop. */
1115 avail
= ALCcaptureAlsa_availableSamples(self
);
1116 if(!self
->ring
&& avail
> 0)
1118 /* The ring buffer implicitly captures when checking availability.
1119 * Direct access needs to explicitly capture it into temp storage. */
1123 size
= snd_pcm_frames_to_bytes(self
->pcmHandle
, avail
);
1124 ptr
= al_malloc(16, size
);
1127 ALCcaptureAlsa_captureSamples(self
, ptr
, avail
);
1128 al_free(self
->buffer
);
1133 err
= snd_pcm_drop(self
->pcmHandle
);
1135 ERR("drop failed: %s\n", snd_strerror(err
));
1136 self
->doCapture
= AL_FALSE
;
1139 static ALCenum
ALCcaptureAlsa_captureSamples(ALCcaptureAlsa
*self
, ALCvoid
*buffer
, ALCuint samples
)
1141 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
1145 ll_ringbuffer_read(self
->ring
, buffer
, samples
);
1146 return ALC_NO_ERROR
;
1149 self
->last_avail
-= samples
;
1150 while(device
->Connected
&& samples
> 0)
1152 snd_pcm_sframes_t amt
= 0;
1156 /* First get any data stored from the last stop */
1157 amt
= snd_pcm_bytes_to_frames(self
->pcmHandle
, self
->size
);
1158 if((snd_pcm_uframes_t
)amt
> samples
) amt
= samples
;
1160 amt
= snd_pcm_frames_to_bytes(self
->pcmHandle
, amt
);
1161 memcpy(buffer
, self
->buffer
, amt
);
1163 if(self
->size
> amt
)
1165 memmove(self
->buffer
, self
->buffer
+amt
, self
->size
- amt
);
1170 al_free(self
->buffer
);
1171 self
->buffer
= NULL
;
1174 amt
= snd_pcm_bytes_to_frames(self
->pcmHandle
, amt
);
1176 else if(self
->doCapture
)
1177 amt
= snd_pcm_readi(self
->pcmHandle
, buffer
, samples
);
1180 ERR("read error: %s\n", snd_strerror(amt
));
1184 if((amt
=snd_pcm_recover(self
->pcmHandle
, amt
, 1)) >= 0)
1186 amt
= snd_pcm_start(self
->pcmHandle
);
1188 amt
= snd_pcm_avail_update(self
->pcmHandle
);
1192 ERR("restore error: %s\n", snd_strerror(amt
));
1193 aluHandleDisconnect(device
);
1196 /* If the amount available is less than what's asked, we lost it
1197 * during recovery. So just give silence instead. */
1198 if((snd_pcm_uframes_t
)amt
< samples
)
1203 buffer
= (ALbyte
*)buffer
+ amt
;
1207 memset(buffer
, ((device
->FmtType
== DevFmtUByte
) ? 0x80 : 0),
1208 snd_pcm_frames_to_bytes(self
->pcmHandle
, samples
));
1210 return ALC_NO_ERROR
;
1213 static ALCuint
ALCcaptureAlsa_availableSamples(ALCcaptureAlsa
*self
)
1215 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
1216 snd_pcm_sframes_t avail
= 0;
1218 if(device
->Connected
&& self
->doCapture
)
1219 avail
= snd_pcm_avail_update(self
->pcmHandle
);
1222 ERR("avail update failed: %s\n", snd_strerror(avail
));
1224 if((avail
=snd_pcm_recover(self
->pcmHandle
, avail
, 1)) >= 0)
1227 avail
= snd_pcm_start(self
->pcmHandle
);
1229 avail
= snd_pcm_avail_update(self
->pcmHandle
);
1233 ERR("restore error: %s\n", snd_strerror(avail
));
1234 aluHandleDisconnect(device
);
1240 if(avail
< 0) avail
= 0;
1241 avail
+= snd_pcm_bytes_to_frames(self
->pcmHandle
, self
->size
);
1242 if(avail
> self
->last_avail
) self
->last_avail
= avail
;
1243 return self
->last_avail
;
1248 ll_ringbuffer_data_t vec
[2];
1249 snd_pcm_sframes_t amt
;
1251 ll_ringbuffer_get_write_vector(self
->ring
, vec
);
1252 if(vec
[0].len
== 0) break;
1254 amt
= (vec
[0].len
< (snd_pcm_uframes_t
)avail
) ?
1255 vec
[0].len
: (snd_pcm_uframes_t
)avail
;
1256 amt
= snd_pcm_readi(self
->pcmHandle
, vec
[0].buf
, amt
);
1259 ERR("read error: %s\n", snd_strerror(amt
));
1263 if((amt
=snd_pcm_recover(self
->pcmHandle
, amt
, 1)) >= 0)
1266 amt
= snd_pcm_start(self
->pcmHandle
);
1268 amt
= snd_pcm_avail_update(self
->pcmHandle
);
1272 ERR("restore error: %s\n", snd_strerror(amt
));
1273 aluHandleDisconnect(device
);
1280 ll_ringbuffer_write_advance(self
->ring
, amt
);
1284 return ll_ringbuffer_read_space(self
->ring
);
1287 static ClockLatency
ALCcaptureAlsa_getClockLatency(ALCcaptureAlsa
*self
)
1289 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
1290 snd_pcm_sframes_t delay
= 0;
1294 ALCcaptureAlsa_lock(self
);
1295 ret
.ClockTime
= GetDeviceClockTime(device
);
1296 if((err
=snd_pcm_delay(self
->pcmHandle
, &delay
)) < 0)
1298 ERR("Failed to get pcm delay: %s\n", snd_strerror(err
));
1301 if(delay
< 0) delay
= 0;
1302 ret
.Latency
= delay
* DEVICE_CLOCK_RES
/ device
->Frequency
;
1303 ALCcaptureAlsa_unlock(self
);
1309 static inline void AppendAllDevicesList2(const DevMap
*entry
)
1310 { AppendAllDevicesList(al_string_get_cstr(entry
->name
)); }
1311 static inline void AppendCaptureDeviceList2(const DevMap
*entry
)
1312 { AppendCaptureDeviceList(al_string_get_cstr(entry
->name
)); }
1314 typedef struct ALCalsaBackendFactory
{
1315 DERIVE_FROM_TYPE(ALCbackendFactory
);
1316 } ALCalsaBackendFactory
;
1317 #define ALCALSABACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCalsaBackendFactory, ALCbackendFactory) } }
1319 static ALCboolean
ALCalsaBackendFactory_init(ALCalsaBackendFactory
* UNUSED(self
))
1321 VECTOR_INIT(PlaybackDevices
);
1322 VECTOR_INIT(CaptureDevices
);
1329 static void ALCalsaBackendFactory_deinit(ALCalsaBackendFactory
* UNUSED(self
))
1331 clear_devlist(&PlaybackDevices
);
1332 VECTOR_DEINIT(PlaybackDevices
);
1334 clear_devlist(&CaptureDevices
);
1335 VECTOR_DEINIT(CaptureDevices
);
1339 CloseLib(alsa_handle
);
1344 static ALCboolean
ALCalsaBackendFactory_querySupport(ALCalsaBackendFactory
* UNUSED(self
), ALCbackend_Type type
)
1346 if(type
== ALCbackend_Playback
|| type
== ALCbackend_Capture
)
1351 static void ALCalsaBackendFactory_probe(ALCalsaBackendFactory
* UNUSED(self
), enum DevProbe type
)
1355 case ALL_DEVICE_PROBE
:
1356 probe_devices(SND_PCM_STREAM_PLAYBACK
, &PlaybackDevices
);
1357 VECTOR_FOR_EACH(const DevMap
, PlaybackDevices
, AppendAllDevicesList2
);
1360 case CAPTURE_DEVICE_PROBE
:
1361 probe_devices(SND_PCM_STREAM_CAPTURE
, &CaptureDevices
);
1362 VECTOR_FOR_EACH(const DevMap
, CaptureDevices
, AppendCaptureDeviceList2
);
1367 static ALCbackend
* ALCalsaBackendFactory_createBackend(ALCalsaBackendFactory
* UNUSED(self
), ALCdevice
*device
, ALCbackend_Type type
)
1369 if(type
== ALCbackend_Playback
)
1371 ALCplaybackAlsa
*backend
;
1372 NEW_OBJ(backend
, ALCplaybackAlsa
)(device
);
1373 if(!backend
) return NULL
;
1374 return STATIC_CAST(ALCbackend
, backend
);
1376 if(type
== ALCbackend_Capture
)
1378 ALCcaptureAlsa
*backend
;
1379 NEW_OBJ(backend
, ALCcaptureAlsa
)(device
);
1380 if(!backend
) return NULL
;
1381 return STATIC_CAST(ALCbackend
, backend
);
1387 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCalsaBackendFactory
);
1390 ALCbackendFactory
*ALCalsaBackendFactory_getFactory(void)
1392 static ALCalsaBackendFactory factory
= ALCALSABACKENDFACTORY_INITIALIZER
;
1393 return STATIC_CAST(ALCbackendFactory
, &factory
);