Some non-mmap ALSA fixes
[openal-soft/openbsd.git] / Alc / alsa.c
blob91a4543d680919d74849930a6b10031b207a631d
1 /**
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
26 #ifdef HAVE_DLFCN_H
27 #include <dlfcn.h>
28 #endif
29 #include "alMain.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
33 #include <alsa/asoundlib.h>
36 typedef struct {
37 snd_pcm_t *pcmHandle;
38 snd_pcm_format_t format;
40 ALvoid *buffer;
41 ALsizei size;
43 int killNow;
44 ALvoid *thread;
45 } alsa_data;
47 typedef struct {
48 ALCchar *name;
49 int card, dev;
50 } DevMap;
52 static void *alsa_handle;
53 #define MAKE_FUNC(f) static typeof(f) * p##f
54 MAKE_FUNC(snd_strerror);
55 MAKE_FUNC(snd_pcm_open);
56 MAKE_FUNC(snd_pcm_close);
57 MAKE_FUNC(snd_pcm_nonblock);
58 MAKE_FUNC(snd_pcm_frames_to_bytes);
59 MAKE_FUNC(snd_pcm_hw_params_malloc);
60 MAKE_FUNC(snd_pcm_hw_params_free);
61 MAKE_FUNC(snd_pcm_hw_params_any);
62 MAKE_FUNC(snd_pcm_hw_params_set_access);
63 MAKE_FUNC(snd_pcm_hw_params_set_format);
64 MAKE_FUNC(snd_pcm_hw_params_set_channels);
65 MAKE_FUNC(snd_pcm_hw_params_set_periods_near);
66 MAKE_FUNC(snd_pcm_hw_params_set_rate_near);
67 MAKE_FUNC(snd_pcm_hw_params_set_rate);
68 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_near);
69 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_min);
70 MAKE_FUNC(snd_pcm_hw_params_get_access);
71 MAKE_FUNC(snd_pcm_hw_params);
72 MAKE_FUNC(snd_pcm_prepare);
73 MAKE_FUNC(snd_pcm_start);
74 MAKE_FUNC(snd_pcm_resume);
75 MAKE_FUNC(snd_pcm_state);
76 MAKE_FUNC(snd_pcm_avail_update);
77 MAKE_FUNC(snd_pcm_areas_silence);
78 MAKE_FUNC(snd_pcm_mmap_begin);
79 MAKE_FUNC(snd_pcm_mmap_commit);
80 MAKE_FUNC(snd_pcm_writei);
81 MAKE_FUNC(snd_pcm_drain);
82 MAKE_FUNC(snd_pcm_info_malloc);
83 MAKE_FUNC(snd_pcm_info_free);
84 MAKE_FUNC(snd_pcm_info_set_device);
85 MAKE_FUNC(snd_pcm_info_set_subdevice);
86 MAKE_FUNC(snd_pcm_info_set_stream);
87 MAKE_FUNC(snd_pcm_info_get_name);
88 MAKE_FUNC(snd_ctl_pcm_next_device);
89 MAKE_FUNC(snd_ctl_pcm_info);
90 MAKE_FUNC(snd_ctl_open);
91 MAKE_FUNC(snd_ctl_close);
92 MAKE_FUNC(snd_ctl_card_info_malloc);
93 MAKE_FUNC(snd_ctl_card_info_free);
94 MAKE_FUNC(snd_ctl_card_info);
95 MAKE_FUNC(snd_ctl_card_info_get_name);
96 MAKE_FUNC(snd_card_next);
97 #undef MAKE_FUNC
99 #define MAX_DEVICES 16
100 #define MAX_ALL_DEVICES 32
102 static DevMap allDevNameMap[MAX_ALL_DEVICES];
103 static ALCchar *alsaDeviceList[MAX_DEVICES];
104 static ALCchar *alsaCaptureDeviceList[MAX_DEVICES];
106 static int xrun_recovery(snd_pcm_t *handle, int err)
108 if (err == -EPIPE)
109 { /* under-run */
110 err = psnd_pcm_prepare(handle);
111 if(err >= 0)
112 err = psnd_pcm_start(handle);
113 if (err < 0)
114 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
116 else if (err == -ESTRPIPE)
118 while ((err = psnd_pcm_resume(handle)) == -EAGAIN)
119 usleep(1); /* wait until the suspend flag is released */
120 if (err < 0)
122 err = psnd_pcm_prepare(handle);
123 if(err >= 0)
124 err = psnd_pcm_start(handle);
125 if (err < 0)
126 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
129 return err;
133 static ALuint ALSAProc(ALvoid *ptr)
135 ALCdevice *pDevice = (ALCdevice*)ptr;
136 alsa_data *data = (alsa_data*)pDevice->ExtraData;
137 const snd_pcm_channel_area_t *areas = NULL;
138 snd_pcm_sframes_t avail, commitres;
139 snd_pcm_uframes_t offset, frames;
140 char *WritePtr;
141 int WriteCnt;
142 int err;
144 while(!data->killNow)
146 snd_pcm_state_t state = psnd_pcm_state(data->pcmHandle);
147 if(state == SND_PCM_STATE_XRUN)
149 err = xrun_recovery(data->pcmHandle, -EPIPE);
150 if (err < 0)
152 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
153 break;
156 else if (state == SND_PCM_STATE_SUSPENDED)
158 err = xrun_recovery(data->pcmHandle, -ESTRPIPE);
159 if (err < 0)
161 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err));
162 break;
166 avail = psnd_pcm_avail_update(data->pcmHandle);
167 if(avail < 0)
169 err = xrun_recovery(data->pcmHandle, avail);
170 if (err < 0)
172 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
173 break;
177 // make sure there's frames to process
178 if(avail == 0)
180 usleep(1000);
181 continue;
184 // it is possible that contiguous areas are smaller, thus we use a loop
185 while (avail > 0)
187 frames = avail;
189 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
190 if (err < 0)
192 err = xrun_recovery(data->pcmHandle, err);
193 if (err < 0)
194 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
195 break;
198 SuspendContext(NULL);
199 WritePtr = (char*)areas->addr + (offset * areas->step / 8);
200 WriteCnt = psnd_pcm_frames_to_bytes(data->pcmHandle, frames);
201 aluMixData(pDevice->Context, WritePtr, WriteCnt, pDevice->Format);
202 ProcessContext(NULL);
204 commitres = psnd_pcm_mmap_commit(data->pcmHandle, offset, frames);
205 if (commitres < 0 || (commitres-frames) != 0)
207 AL_PRINT("mmap commit error: %s\n",
208 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
209 break;
212 avail -= frames;
216 return 0;
219 static ALuint ALSANoMMapProc(ALvoid *ptr)
221 ALCdevice *pDevice = (ALCdevice*)ptr;
222 alsa_data *data = (alsa_data*)pDevice->ExtraData;
223 snd_pcm_sframes_t avail;
224 char *WritePtr;
226 while(!data->killNow)
228 SuspendContext(NULL);
229 aluMixData(pDevice->Context, data->buffer, data->size, pDevice->Format);
230 ProcessContext(NULL);
232 WritePtr = data->buffer;
233 avail = (snd_pcm_uframes_t)data->size / psnd_pcm_frames_to_bytes(data->pcmHandle, 1);
234 while(avail > 0)
236 int ret = psnd_pcm_writei(data->pcmHandle, WritePtr, avail);
237 switch (ret)
239 case -EAGAIN:
240 continue;
241 case -ESTRPIPE:
242 do {
243 ret = psnd_pcm_resume(data->pcmHandle);
244 } while(ret == -EAGAIN);
245 break;
246 case -EPIPE:
247 break;
248 default:
249 if (ret >= 0)
251 WritePtr += psnd_pcm_frames_to_bytes(data->pcmHandle, ret);
252 avail -= ret;
254 break;
256 if (ret < 0)
258 ret = psnd_pcm_prepare(data->pcmHandle);
259 if(ret < 0)
260 break;
265 return 0;
268 static void fill_silence(snd_pcm_t *pcmHandle, snd_pcm_format_t alsaFormat, int channels)
270 const snd_pcm_channel_area_t *areas = NULL;
271 snd_pcm_sframes_t avail, commitres;
272 snd_pcm_uframes_t offset, frames;
273 int err;
275 avail = psnd_pcm_avail_update(pcmHandle);
276 if(avail < 0)
278 err = xrun_recovery(pcmHandle, avail);
279 if (err < 0)
281 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
282 return;
286 // it is possible that contiguous areas are smaller, thus we use a loop
287 while (avail > 0)
289 frames = avail;
291 err = psnd_pcm_mmap_begin(pcmHandle, &areas, &offset, &frames);
292 if (err < 0)
294 err = xrun_recovery(pcmHandle, err);
295 if (err < 0)
297 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
298 break;
300 continue;
303 psnd_pcm_areas_silence(areas, offset, channels, frames, alsaFormat);
305 commitres = psnd_pcm_mmap_commit(pcmHandle, offset, frames);
306 if (commitres < 0 || (commitres-frames) != 0)
308 AL_PRINT("mmap commit error: %s\n",
309 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
310 break;
313 avail -= frames;
317 static ALCboolean alsa_open_playback(ALCdevice *device, const ALCchar *deviceName)
319 snd_pcm_uframes_t bufferSizeInFrames;
320 snd_pcm_hw_params_t *p = NULL;
321 snd_pcm_access_t access;
322 unsigned int periods;
323 alsa_data *data;
324 char driver[64];
325 char *err;
326 int i;
328 strncpy(driver, GetConfigValue("alsa", "device", "default"), sizeof(driver)-1);
329 driver[sizeof(driver)-1] = 0;
330 if(deviceName)
332 size_t idx;
334 for(idx = 0;idx < MAX_ALL_DEVICES;idx++)
336 if(allDevNameMap[idx].name &&
337 strcmp(deviceName, allDevNameMap[idx].name) == 0)
339 device->szDeviceName = allDevNameMap[idx].name;
340 if(idx > 0)
341 sprintf(driver, "hw:%d,%d", allDevNameMap[idx].card, allDevNameMap[idx].dev);
342 goto open_alsa;
345 for(idx = 0;idx < MAX_DEVICES;idx++)
347 if(alsaDeviceList[idx] &&
348 strcmp(deviceName, alsaDeviceList[idx]) == 0)
350 device->szDeviceName = alsaDeviceList[idx];
351 if(idx > 0)
352 sprintf(driver, "hw:%zd,0", idx-1);
353 goto open_alsa;
356 return ALC_FALSE;
358 else
359 device->szDeviceName = alsaDeviceList[0];
361 open_alsa:
362 data = (alsa_data*)calloc(1, sizeof(alsa_data));
364 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
365 if(i < 0)
367 usleep(200000);
368 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
370 if(i >= 0)
372 i = psnd_pcm_nonblock(data->pcmHandle, 0);
373 if(i < 0)
374 psnd_pcm_close(data->pcmHandle);
376 if(i < 0)
378 free(data);
379 AL_PRINT("Could not open playback device '%s': %s\n", driver, psnd_strerror(i));
380 return ALC_FALSE;
383 switch(aluBytesFromFormat(device->Format))
385 case 1:
386 data->format = SND_PCM_FORMAT_U8;
387 break;
388 case 2:
389 data->format = SND_PCM_FORMAT_S16;
390 break;
391 default:
392 data->format = SND_PCM_FORMAT_UNKNOWN;
393 AL_PRINT("Unknown format?! %x\n", device->Format);
396 periods = GetConfigValueInt("alsa", "periods", 4);
397 if((int)periods <= 0)
398 periods = 4;
399 bufferSizeInFrames = device->UpdateFreq;
401 psnd_pcm_hw_params_malloc(&p);
402 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
403 /* start with the largest configuration space possible */
404 if(!(ok(psnd_pcm_hw_params_any(data->pcmHandle, p), "any") &&
405 /* set interleaved access */
406 (ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set access") ||
407 ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_RW_INTERLEAVED), "set access")) &&
408 /* set format (implicitly sets sample bits) */
409 ok(psnd_pcm_hw_params_set_format(data->pcmHandle, p, data->format), "set format") &&
410 /* set channels (implicitly sets frame bits) */
411 ok(psnd_pcm_hw_params_set_channels(data->pcmHandle, p, device->Channels), "set channels") &&
412 /* set periods (implicitly constrains period/buffer parameters) */
413 ok(psnd_pcm_hw_params_set_periods_near(data->pcmHandle, p, &periods, NULL), "set periods near") &&
414 /* set rate (implicitly constrains period/buffer parameters) */
415 ok(psnd_pcm_hw_params_set_rate_near(data->pcmHandle, p, &device->Frequency, NULL), "set rate near") &&
416 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
417 ok(psnd_pcm_hw_params_set_buffer_size_near(data->pcmHandle, p, &bufferSizeInFrames), "set buffer size near") &&
418 /* install and prepare hardware configuration */
419 ok(psnd_pcm_hw_params(data->pcmHandle, p), "set params")))
421 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
422 psnd_pcm_hw_params_free(p);
423 psnd_pcm_close(data->pcmHandle);
424 free(data);
425 return ALC_FALSE;
427 #undef ok
429 if((i=psnd_pcm_hw_params_get_access(p, &access)) < 0)
431 AL_PRINT("get_access failed: %s\n", psnd_strerror(i));
432 psnd_pcm_hw_params_free(p);
433 psnd_pcm_close(data->pcmHandle);
434 free(data);
435 return ALC_FALSE;
438 psnd_pcm_hw_params_free(p);
440 device->MaxNoOfSources = 256;
441 device->UpdateFreq = bufferSizeInFrames;
443 data->size = psnd_pcm_frames_to_bytes(data->pcmHandle, bufferSizeInFrames);
444 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
446 data->buffer = malloc(data->size);
447 if(!data->buffer)
449 AL_PRINT("buffer malloc failed\n");
450 psnd_pcm_close(data->pcmHandle);
451 free(data);
452 return ALC_FALSE;
455 else
457 i = psnd_pcm_prepare(data->pcmHandle);
458 if(i < 0)
460 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
461 psnd_pcm_close(data->pcmHandle);
462 free(data->buffer);
463 free(data);
464 return ALC_FALSE;
467 fill_silence(data->pcmHandle, data->format, device->Channels);
469 i = psnd_pcm_start(data->pcmHandle);
470 if(i < 0)
472 AL_PRINT("start error: %s\n", psnd_strerror(i));
473 psnd_pcm_close(data->pcmHandle);
474 free(data->buffer);
475 free(data);
476 return ALC_FALSE;
480 device->ExtraData = data;
481 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
482 data->thread = StartThread(ALSANoMMapProc, device);
483 else
484 data->thread = StartThread(ALSAProc, device);
485 if(data->thread == NULL)
487 psnd_pcm_close(data->pcmHandle);
488 device->ExtraData = NULL;
489 free(data->buffer);
490 free(data);
491 return ALC_FALSE;
494 return ALC_TRUE;
497 static void alsa_close_playback(ALCdevice *device)
499 alsa_data *data = (alsa_data*)device->ExtraData;
500 data->killNow = 1;
501 StopThread(data->thread);
502 psnd_pcm_close(data->pcmHandle);
504 free(data->buffer);
505 free(data);
506 device->ExtraData = NULL;
510 static ALCboolean alsa_open_capture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
512 snd_pcm_format_t alsaFormat;
513 snd_pcm_hw_params_t *p;
514 unsigned int periods = 4;
515 snd_pcm_uframes_t bufferSizeInFrames = SampleSize;
516 alsa_data *data;
517 char driver[64];
518 char *err;
519 int i;
521 strncpy(driver, GetConfigValue("alsa", "capture", "default"), sizeof(driver)-1);
522 driver[sizeof(driver)-1] = 0;
523 if(deviceName)
525 size_t idx;
527 for(idx = 0;idx < MAX_DEVICES;idx++)
529 if(alsaCaptureDeviceList[idx] &&
530 strcmp(deviceName, alsaCaptureDeviceList[idx]) == 0)
532 pDevice->szDeviceName = alsaCaptureDeviceList[idx];
533 if(idx > 0)
534 sprintf(driver, "hw:%zd,0", idx-1);
535 goto open_alsa;
538 return ALC_FALSE;
540 else
541 pDevice->szDeviceName = alsaCaptureDeviceList[0];
543 open_alsa:
544 data = (alsa_data*)calloc(1, sizeof(alsa_data));
546 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
547 if(i < 0)
549 usleep(200000);
550 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
552 if(i >= 0)
554 i = psnd_pcm_nonblock(data->pcmHandle, 0);
555 if(i < 0)
556 psnd_pcm_close(data->pcmHandle);
558 if(i < 0)
560 free(data);
561 AL_PRINT("Could not open capture device '%s': %s\n", driver, psnd_strerror(i));
562 return ALC_FALSE;
565 switch(aluBytesFromFormat(format))
567 case 1:
568 alsaFormat = SND_PCM_FORMAT_U8;
569 break;
570 case 2:
571 alsaFormat = SND_PCM_FORMAT_S16;
572 break;
573 default:
574 alsaFormat = SND_PCM_FORMAT_UNKNOWN;
575 AL_PRINT("Unknown format?! %x\n", format);
578 psnd_pcm_hw_params_malloc(&p);
579 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
580 /* start with the largest configuration space possible */
581 if(!(ok(psnd_pcm_hw_params_any(data->pcmHandle, p), "any") &&
582 /* set interleaved access */
583 ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set access") &&
584 /* set format (implicitly sets sample bits) */
585 ok(psnd_pcm_hw_params_set_format(data->pcmHandle, p, alsaFormat), "set format") &&
586 /* set channels (implicitly sets frame bits) */
587 ok(psnd_pcm_hw_params_set_channels(data->pcmHandle, p, pDevice->Channels), "set channels") &&
588 /* set periods (implicitly constrains period/buffer parameters) */
589 ok(psnd_pcm_hw_params_set_periods_near(data->pcmHandle, p, &periods, NULL), "set periods near") &&
590 /* set rate (implicitly constrains period/buffer parameters) */
591 ok(psnd_pcm_hw_params_set_rate(data->pcmHandle, p, frequency, 0), "set rate") &&
592 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
593 ok(psnd_pcm_hw_params_set_buffer_size_min(data->pcmHandle, p, &bufferSizeInFrames), "set buffer size min") &&
594 /* install and prepare hardware configuration */
595 ok(psnd_pcm_hw_params(data->pcmHandle, p), "set params")))
597 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
598 psnd_pcm_hw_params_free(p);
599 psnd_pcm_close(data->pcmHandle);
600 free(data);
601 return ALC_FALSE;
603 #undef ok
604 psnd_pcm_hw_params_free(p);
606 i = psnd_pcm_prepare(data->pcmHandle);
607 if(i < 0)
609 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
610 psnd_pcm_close(data->pcmHandle);
611 free(data);
612 return ALC_FALSE;
615 pDevice->ExtraData = data;
616 return ALC_TRUE;
619 static void alsa_close_capture(ALCdevice *pDevice)
621 alsa_data *data = (alsa_data*)pDevice->ExtraData;
622 psnd_pcm_close(data->pcmHandle);
624 free(data);
625 pDevice->ExtraData = NULL;
628 static void alsa_start_capture(ALCdevice *pDevice)
630 alsa_data *data = (alsa_data*)pDevice->ExtraData;
631 psnd_pcm_start(data->pcmHandle);
634 static void alsa_stop_capture(ALCdevice *pDevice)
636 alsa_data *data = (alsa_data*)pDevice->ExtraData;
637 psnd_pcm_drain(data->pcmHandle);
640 static void alsa_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
642 alsa_data *data = (alsa_data*)pDevice->ExtraData;
643 const snd_pcm_channel_area_t *areas = NULL;
644 snd_pcm_sframes_t frames, commitres;
645 snd_pcm_uframes_t size, offset;
646 int err;
648 frames = psnd_pcm_avail_update(data->pcmHandle);
649 if(frames < 0)
651 err = xrun_recovery(data->pcmHandle, frames);
652 if (err < 0)
653 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
654 else
655 frames = psnd_pcm_avail_update(data->pcmHandle);
657 if (frames < (snd_pcm_sframes_t)lSamples)
659 SetALCError(ALC_INVALID_VALUE);
660 return;
663 // it is possible that contiguous areas are smaller, thus we use a loop
664 while (lSamples > 0)
666 char *Pointer;
667 int Count;
669 size = lSamples;
670 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &size);
671 if (err < 0)
673 err = xrun_recovery(data->pcmHandle, err);
674 if (err < 0)
676 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
677 break;
679 continue;
682 Pointer = (char*)areas->addr + (offset * areas->step / 8);
683 Count = size * pDevice->FrameSize;
685 memcpy(pBuffer, Pointer, Count);
686 pBuffer = (char*)pBuffer + Count;
688 commitres = psnd_pcm_mmap_commit(data->pcmHandle, offset, size);
689 if (commitres < 0 || (commitres-size) != 0)
691 AL_PRINT("mmap commit error: %s\n",
692 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
693 break;
696 lSamples -= size;
700 static ALCuint alsa_available_samples(ALCdevice *pDevice)
702 alsa_data *data = (alsa_data*)pDevice->ExtraData;
703 snd_pcm_sframes_t frames = psnd_pcm_avail_update(data->pcmHandle);
704 if(frames < 0)
706 int err = xrun_recovery(data->pcmHandle, frames);
707 if (err < 0)
708 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
709 else
710 frames = psnd_pcm_avail_update(data->pcmHandle);
711 if(frames < 0) /* ew.. */
712 SetALCError(ALC_INVALID_DEVICE);
714 return max(frames, 0);
718 BackendFuncs alsa_funcs = {
719 alsa_open_playback,
720 alsa_close_playback,
721 alsa_open_capture,
722 alsa_close_capture,
723 alsa_start_capture,
724 alsa_stop_capture,
725 alsa_capture_samples,
726 alsa_available_samples
729 void alc_alsa_init(BackendFuncs *func_list)
731 snd_ctl_t *handle;
732 int card, err, dev, idx = 1;
733 snd_ctl_card_info_t *info;
734 snd_pcm_info_t *pcminfo;
735 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
736 char name[128];
737 char *str;
739 *func_list = alsa_funcs;
741 #ifdef HAVE_DLFCN_H
742 alsa_handle = dlopen("libasound.so.2", RTLD_NOW);
743 if(!alsa_handle)
744 return;
745 dlerror();
747 #define LOAD_FUNC(f) do { \
748 p##f = (typeof(f)*)dlsym(alsa_handle, #f); \
749 if((str=dlerror()) != NULL) \
751 dlclose(alsa_handle); \
752 alsa_handle = NULL; \
753 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
754 return; \
756 } while(0)
757 #else
758 str = NULL;
759 alsa_handle = NULL;
760 #define LOAD_FUNC(f) p##f = f
761 #endif
763 LOAD_FUNC(snd_strerror);
764 LOAD_FUNC(snd_pcm_open);
765 LOAD_FUNC(snd_pcm_close);
766 LOAD_FUNC(snd_pcm_nonblock);
767 LOAD_FUNC(snd_pcm_frames_to_bytes);
768 LOAD_FUNC(snd_pcm_hw_params_malloc);
769 LOAD_FUNC(snd_pcm_hw_params_free);
770 LOAD_FUNC(snd_pcm_hw_params_any);
771 LOAD_FUNC(snd_pcm_hw_params_set_access);
772 LOAD_FUNC(snd_pcm_hw_params_set_format);
773 LOAD_FUNC(snd_pcm_hw_params_set_channels);
774 LOAD_FUNC(snd_pcm_hw_params_set_periods_near);
775 LOAD_FUNC(snd_pcm_hw_params_set_rate_near);
776 LOAD_FUNC(snd_pcm_hw_params_set_rate);
777 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near);
778 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min);
779 LOAD_FUNC(snd_pcm_hw_params_get_access);
780 LOAD_FUNC(snd_pcm_hw_params);
781 LOAD_FUNC(snd_pcm_prepare);
782 LOAD_FUNC(snd_pcm_start);
783 LOAD_FUNC(snd_pcm_resume);
784 LOAD_FUNC(snd_pcm_state);
785 LOAD_FUNC(snd_pcm_avail_update);
786 LOAD_FUNC(snd_pcm_areas_silence);
787 LOAD_FUNC(snd_pcm_mmap_begin);
788 LOAD_FUNC(snd_pcm_mmap_commit);
789 LOAD_FUNC(snd_pcm_writei);
790 LOAD_FUNC(snd_pcm_drain);
792 LOAD_FUNC(snd_pcm_info_malloc);
793 LOAD_FUNC(snd_pcm_info_free);
794 LOAD_FUNC(snd_pcm_info_set_device);
795 LOAD_FUNC(snd_pcm_info_set_subdevice);
796 LOAD_FUNC(snd_pcm_info_set_stream);
797 LOAD_FUNC(snd_pcm_info_get_name);
798 LOAD_FUNC(snd_ctl_pcm_next_device);
799 LOAD_FUNC(snd_ctl_pcm_info);
800 LOAD_FUNC(snd_ctl_open);
801 LOAD_FUNC(snd_ctl_close);
802 LOAD_FUNC(snd_ctl_card_info_malloc);
803 LOAD_FUNC(snd_ctl_card_info_free);
804 LOAD_FUNC(snd_ctl_card_info);
805 LOAD_FUNC(snd_ctl_card_info_get_name);
806 LOAD_FUNC(snd_card_next);
808 #undef LOAD_FUNC
810 psnd_ctl_card_info_malloc(&info);
811 psnd_pcm_info_malloc(&pcminfo);
813 card = -1;
814 if(psnd_card_next(&card) < 0 || card < 0)
815 AL_PRINT("no playback cards found...\n");
816 else
818 alsaDeviceList[0] = AppendDeviceList("ALSA Software on default");
819 allDevNameMap[0].name = AppendAllDeviceList("ALSA Software on default");
822 while (card >= 0) {
823 sprintf(name, "hw:%d", card);
824 if ((err = psnd_ctl_open(&handle, name, 0)) < 0) {
825 AL_PRINT("control open (%i): %s\n", card, psnd_strerror(err));
826 goto next_card;
828 if ((err = psnd_ctl_card_info(handle, info)) < 0) {
829 AL_PRINT("control hardware info (%i): %s\n", card, psnd_strerror(err));
830 psnd_ctl_close(handle);
831 goto next_card;
833 if(card < MAX_DEVICES-1) {
834 snprintf(name, sizeof(name), "ALSA Software on %s",
835 psnd_ctl_card_info_get_name(info));
836 alsaDeviceList[card+1] = AppendDeviceList(name);
839 dev = -1;
840 while (idx < MAX_ALL_DEVICES) {
841 const char *cname, *dname;
843 if (psnd_ctl_pcm_next_device(handle, &dev)<0)
844 AL_PRINT("snd_ctl_pcm_next_device failed\n");
845 if (dev < 0)
846 break;
847 psnd_pcm_info_set_device(pcminfo, dev);
848 psnd_pcm_info_set_subdevice(pcminfo, 0);
849 psnd_pcm_info_set_stream(pcminfo, stream);
850 if ((err = psnd_ctl_pcm_info(handle, pcminfo)) < 0) {
851 if (err != -ENOENT)
852 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
853 continue;
856 cname = psnd_ctl_card_info_get_name(info);
857 dname = psnd_pcm_info_get_name(pcminfo);
858 snprintf(name, sizeof(name), "ALSA Software on %s [%s]",
859 cname, dname);
860 allDevNameMap[idx].name = AppendAllDeviceList(name);
861 allDevNameMap[idx].card = card;
862 allDevNameMap[idx].dev = dev;
863 idx++;
865 psnd_ctl_close(handle);
866 next_card:
867 if(psnd_card_next(&card) < 0) {
868 AL_PRINT("snd_card_next failed\n");
869 break;
874 stream = SND_PCM_STREAM_CAPTURE;
876 card = -1;
877 if(psnd_card_next(&card) < 0 || card < 0) {
878 AL_PRINT("no capture cards found...\n");
879 psnd_pcm_info_free(pcminfo);
880 psnd_ctl_card_info_free(info);
881 return;
884 alsaCaptureDeviceList[0] = AppendCaptureDeviceList("ALSA Capture on default");
886 while (card >= 0) {
887 sprintf(name, "hw:%d", card);
888 handle = NULL;
889 if ((err = psnd_ctl_open(&handle, name, 0)) < 0) {
890 AL_PRINT("control open (%i): %s\n", card, psnd_strerror(err));
892 if (err >= 0 && (err = psnd_ctl_card_info(handle, info)) < 0) {
893 AL_PRINT("control hardware info (%i): %s\n", card, psnd_strerror(err));
894 psnd_ctl_close(handle);
896 else if (err >= 0 && card < MAX_DEVICES-1)
898 snprintf(name, sizeof(name), "ALSA Capture on %s",
899 psnd_ctl_card_info_get_name(info));
900 alsaCaptureDeviceList[card+1] = AppendCaptureDeviceList(name);
902 if(handle) psnd_ctl_close(handle);
903 if(psnd_card_next(&card) < 0) {
904 AL_PRINT("snd_card_next failed\n");
905 break;
908 psnd_pcm_info_free(pcminfo);
909 psnd_ctl_card_info_free(info);