Make sure the stream is playing when it's full
[openal-soft.git] / Alc / alsa.c
blob79092675dd0f7335a084347b1621f747057a299a
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 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
114 else if (err == -ESTRPIPE)
116 while ((err = psnd_pcm_resume(handle)) == -EAGAIN)
117 Sleep(1); /* wait until the suspend flag is released */
118 if (err < 0)
120 err = psnd_pcm_prepare(handle);
121 if (err < 0)
122 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
125 return err;
129 static ALuint ALSAProc(ALvoid *ptr)
131 ALCdevice *pDevice = (ALCdevice*)ptr;
132 alsa_data *data = (alsa_data*)pDevice->ExtraData;
133 const snd_pcm_channel_area_t *areas = NULL;
134 snd_pcm_sframes_t avail, commitres;
135 snd_pcm_uframes_t offset, frames;
136 char *WritePtr;
137 int WriteCnt;
138 int err;
140 while(!data->killNow)
142 snd_pcm_state_t state = psnd_pcm_state(data->pcmHandle);
143 if(state == SND_PCM_STATE_XRUN)
145 err = xrun_recovery(data->pcmHandle, -EPIPE);
146 if (err < 0)
148 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
149 break;
152 else if (state == SND_PCM_STATE_SUSPENDED)
154 err = xrun_recovery(data->pcmHandle, -ESTRPIPE);
155 if (err < 0)
157 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err));
158 break;
162 avail = psnd_pcm_avail_update(data->pcmHandle);
163 if(avail < 0)
165 err = xrun_recovery(data->pcmHandle, avail);
166 if (err < 0)
168 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
169 break;
173 // make sure there's frames to process
174 if(avail == 0)
176 if(state != SND_PCM_STATE_RUNNING)
178 err = psnd_pcm_start(data->pcmHandle);
179 if(err < 0)
181 err = xrun_recovery(data->pcmHandle, err);
182 if (err < 0)
184 AL_PRINT("start failed: %s\n", psnd_strerror(err));
185 break;
189 Sleep(1);
190 continue;
193 // it is possible that contiguous areas are smaller, thus we use a loop
194 while (avail > 0)
196 frames = avail;
198 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
199 if (err < 0)
201 err = xrun_recovery(data->pcmHandle, err);
202 if (err < 0)
203 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
204 break;
207 SuspendContext(NULL);
208 WritePtr = (char*)areas->addr + (offset * areas->step / 8);
209 WriteCnt = psnd_pcm_frames_to_bytes(data->pcmHandle, frames);
210 aluMixData(pDevice->Context, WritePtr, WriteCnt, pDevice->Format);
211 ProcessContext(NULL);
213 commitres = psnd_pcm_mmap_commit(data->pcmHandle, offset, frames);
214 if (commitres < 0 || (commitres-frames) != 0)
216 AL_PRINT("mmap commit error: %s\n",
217 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
218 break;
221 avail -= frames;
225 return 0;
228 static ALuint ALSANoMMapProc(ALvoid *ptr)
230 ALCdevice *pDevice = (ALCdevice*)ptr;
231 alsa_data *data = (alsa_data*)pDevice->ExtraData;
232 snd_pcm_sframes_t avail;
233 char *WritePtr;
235 while(!data->killNow)
237 SuspendContext(NULL);
238 aluMixData(pDevice->Context, data->buffer, data->size, pDevice->Format);
239 ProcessContext(NULL);
241 WritePtr = data->buffer;
242 avail = (snd_pcm_uframes_t)data->size / psnd_pcm_frames_to_bytes(data->pcmHandle, 1);
243 while(avail > 0)
245 int ret = psnd_pcm_writei(data->pcmHandle, WritePtr, avail);
246 switch (ret)
248 case -EAGAIN:
249 continue;
250 case -ESTRPIPE:
251 do {
252 ret = psnd_pcm_resume(data->pcmHandle);
253 } while(ret == -EAGAIN);
254 break;
255 case -EPIPE:
256 break;
257 default:
258 if (ret >= 0)
260 WritePtr += psnd_pcm_frames_to_bytes(data->pcmHandle, ret);
261 avail -= ret;
263 break;
265 if (ret < 0)
267 ret = psnd_pcm_prepare(data->pcmHandle);
268 if(ret < 0)
269 break;
274 return 0;
277 static void fill_silence(snd_pcm_t *pcmHandle, snd_pcm_format_t alsaFormat, int channels)
279 const snd_pcm_channel_area_t *areas = NULL;
280 snd_pcm_sframes_t avail, commitres;
281 snd_pcm_uframes_t offset, frames;
282 int err;
284 avail = psnd_pcm_avail_update(pcmHandle);
285 if(avail < 0)
287 AL_PRINT("available update failed: %s\n", psnd_strerror(avail));
288 return;
291 // it is possible that contiguous areas are smaller, thus we use a loop
292 while (avail > 0)
294 frames = avail;
296 err = psnd_pcm_mmap_begin(pcmHandle, &areas, &offset, &frames);
297 if (err < 0)
299 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
300 break;
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 if(!alsa_handle)
329 return ALC_FALSE;
331 strncpy(driver, GetConfigValue("alsa", "device", "default"), sizeof(driver)-1);
332 driver[sizeof(driver)-1] = 0;
333 if(deviceName)
335 size_t idx;
337 for(idx = 0;idx < MAX_ALL_DEVICES;idx++)
339 if(allDevNameMap[idx].name &&
340 strcmp(deviceName, allDevNameMap[idx].name) == 0)
342 device->szDeviceName = allDevNameMap[idx].name;
343 if(idx > 0)
344 sprintf(driver, "hw:%d,%d", allDevNameMap[idx].card, allDevNameMap[idx].dev);
345 goto open_alsa;
348 for(idx = 0;idx < MAX_DEVICES;idx++)
350 if(alsaDeviceList[idx] &&
351 strcmp(deviceName, alsaDeviceList[idx]) == 0)
353 device->szDeviceName = alsaDeviceList[idx];
354 if(idx > 0)
355 sprintf(driver, "hw:%zd,0", idx-1);
356 goto open_alsa;
359 return ALC_FALSE;
361 else
362 device->szDeviceName = alsaDeviceList[0];
364 open_alsa:
365 data = (alsa_data*)calloc(1, sizeof(alsa_data));
367 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
368 if(i < 0)
370 Sleep(200);
371 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
373 if(i >= 0)
375 i = psnd_pcm_nonblock(data->pcmHandle, 0);
376 if(i < 0)
377 psnd_pcm_close(data->pcmHandle);
379 if(i < 0)
381 free(data);
382 AL_PRINT("Could not open playback device '%s': %s\n", driver, psnd_strerror(i));
383 return ALC_FALSE;
386 switch(aluBytesFromFormat(device->Format))
388 case 1:
389 data->format = SND_PCM_FORMAT_U8;
390 break;
391 case 2:
392 data->format = SND_PCM_FORMAT_S16;
393 break;
394 default:
395 data->format = SND_PCM_FORMAT_UNKNOWN;
396 AL_PRINT("Unknown format?! %x\n", device->Format);
399 periods = GetConfigValueInt("alsa", "periods", 4);
400 if((int)periods <= 0)
401 periods = 4;
402 bufferSizeInFrames = device->UpdateFreq / periods;
404 psnd_pcm_hw_params_malloc(&p);
405 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
406 /* start with the largest configuration space possible */
407 if(!(ok(psnd_pcm_hw_params_any(data->pcmHandle, p), "any") &&
408 /* set interleaved access */
409 (ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set access") ||
410 ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_RW_INTERLEAVED), "set access")) &&
411 /* set format (implicitly sets sample bits) */
412 ok(psnd_pcm_hw_params_set_format(data->pcmHandle, p, data->format), "set format") &&
413 /* set channels (implicitly sets frame bits) */
414 ok(psnd_pcm_hw_params_set_channels(data->pcmHandle, p, device->Channels), "set channels") &&
415 /* set periods (implicitly constrains period/buffer parameters) */
416 ok(psnd_pcm_hw_params_set_periods_near(data->pcmHandle, p, &periods, NULL), "set periods near") &&
417 /* set rate (implicitly constrains period/buffer parameters) */
418 ok(psnd_pcm_hw_params_set_rate_near(data->pcmHandle, p, &device->Frequency, NULL), "set rate near") &&
419 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
420 ok(psnd_pcm_hw_params_set_buffer_size_near(data->pcmHandle, p, &bufferSizeInFrames), "set buffer size near") &&
421 /* install and prepare hardware configuration */
422 ok(psnd_pcm_hw_params(data->pcmHandle, p), "set params")))
424 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
425 psnd_pcm_hw_params_free(p);
426 psnd_pcm_close(data->pcmHandle);
427 free(data);
428 return ALC_FALSE;
430 #undef ok
432 if((i=psnd_pcm_hw_params_get_access(p, &access)) < 0)
434 AL_PRINT("get_access failed: %s\n", psnd_strerror(i));
435 psnd_pcm_hw_params_free(p);
436 psnd_pcm_close(data->pcmHandle);
437 free(data);
438 return ALC_FALSE;
441 psnd_pcm_hw_params_free(p);
443 device->MaxNoOfSources = 256;
444 device->UpdateFreq = bufferSizeInFrames;
446 data->size = psnd_pcm_frames_to_bytes(data->pcmHandle, device->UpdateFreq);
447 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
449 data->buffer = malloc(data->size);
450 if(!data->buffer)
452 AL_PRINT("buffer malloc failed\n");
453 psnd_pcm_close(data->pcmHandle);
454 free(data);
455 return ALC_FALSE;
458 else
460 i = psnd_pcm_prepare(data->pcmHandle);
461 if(i < 0)
463 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
464 psnd_pcm_close(data->pcmHandle);
465 free(data->buffer);
466 free(data);
467 return ALC_FALSE;
470 fill_silence(data->pcmHandle, data->format, device->Channels);
472 i = psnd_pcm_start(data->pcmHandle);
473 if(i < 0)
475 AL_PRINT("start error: %s\n", psnd_strerror(i));
476 psnd_pcm_close(data->pcmHandle);
477 free(data->buffer);
478 free(data);
479 return ALC_FALSE;
483 device->ExtraData = data;
484 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
485 data->thread = StartThread(ALSANoMMapProc, device);
486 else
487 data->thread = StartThread(ALSAProc, device);
488 if(data->thread == NULL)
490 psnd_pcm_close(data->pcmHandle);
491 device->ExtraData = NULL;
492 free(data->buffer);
493 free(data);
494 return ALC_FALSE;
497 return ALC_TRUE;
500 static void alsa_close_playback(ALCdevice *device)
502 alsa_data *data = (alsa_data*)device->ExtraData;
503 data->killNow = 1;
504 StopThread(data->thread);
505 psnd_pcm_close(data->pcmHandle);
507 free(data->buffer);
508 free(data);
509 device->ExtraData = NULL;
513 static ALCboolean alsa_open_capture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
515 snd_pcm_format_t alsaFormat;
516 snd_pcm_hw_params_t *p;
517 unsigned int periods = 4;
518 snd_pcm_uframes_t bufferSizeInFrames;
519 alsa_data *data;
520 char driver[64];
521 char *err;
522 int i;
524 if(!alsa_handle)
525 return ALC_FALSE;
527 strncpy(driver, GetConfigValue("alsa", "capture", "default"), sizeof(driver)-1);
528 driver[sizeof(driver)-1] = 0;
529 if(deviceName)
531 size_t idx;
533 for(idx = 0;idx < MAX_DEVICES;idx++)
535 if(alsaCaptureDeviceList[idx] &&
536 strcmp(deviceName, alsaCaptureDeviceList[idx]) == 0)
538 pDevice->szDeviceName = alsaCaptureDeviceList[idx];
539 if(idx > 0)
540 sprintf(driver, "hw:%zd,0", idx-1);
541 goto open_alsa;
544 return ALC_FALSE;
546 else
547 pDevice->szDeviceName = alsaCaptureDeviceList[0];
549 open_alsa:
550 data = (alsa_data*)calloc(1, sizeof(alsa_data));
552 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
553 if(i < 0)
555 Sleep(200);
556 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
558 if(i >= 0)
560 i = psnd_pcm_nonblock(data->pcmHandle, 0);
561 if(i < 0)
562 psnd_pcm_close(data->pcmHandle);
564 if(i < 0)
566 free(data);
567 AL_PRINT("Could not open capture device '%s': %s\n", driver, psnd_strerror(i));
568 return ALC_FALSE;
571 switch(aluBytesFromFormat(format))
573 case 1:
574 alsaFormat = SND_PCM_FORMAT_U8;
575 break;
576 case 2:
577 alsaFormat = SND_PCM_FORMAT_S16;
578 break;
579 default:
580 alsaFormat = SND_PCM_FORMAT_UNKNOWN;
581 AL_PRINT("Unknown format?! %x\n", format);
584 bufferSizeInFrames = SampleSize / periods;
586 psnd_pcm_hw_params_malloc(&p);
587 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
588 /* start with the largest configuration space possible */
589 if(!(ok(psnd_pcm_hw_params_any(data->pcmHandle, p), "any") &&
590 /* set interleaved access */
591 ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set access") &&
592 /* set format (implicitly sets sample bits) */
593 ok(psnd_pcm_hw_params_set_format(data->pcmHandle, p, alsaFormat), "set format") &&
594 /* set channels (implicitly sets frame bits) */
595 ok(psnd_pcm_hw_params_set_channels(data->pcmHandle, p, pDevice->Channels), "set channels") &&
596 /* set periods (implicitly constrains period/buffer parameters) */
597 ok(psnd_pcm_hw_params_set_periods_near(data->pcmHandle, p, &periods, NULL), "set periods near") &&
598 /* set rate (implicitly constrains period/buffer parameters) */
599 ok(psnd_pcm_hw_params_set_rate(data->pcmHandle, p, frequency, 0), "set rate") &&
600 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
601 ok(psnd_pcm_hw_params_set_buffer_size_min(data->pcmHandle, p, &bufferSizeInFrames), "set buffer size min") &&
602 /* install and prepare hardware configuration */
603 ok(psnd_pcm_hw_params(data->pcmHandle, p), "set params")))
605 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
606 psnd_pcm_hw_params_free(p);
607 psnd_pcm_close(data->pcmHandle);
608 free(data);
609 return ALC_FALSE;
611 #undef ok
612 psnd_pcm_hw_params_free(p);
614 i = psnd_pcm_prepare(data->pcmHandle);
615 if(i < 0)
617 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
618 psnd_pcm_close(data->pcmHandle);
619 free(data);
620 return ALC_FALSE;
623 pDevice->ExtraData = data;
624 return ALC_TRUE;
627 static void alsa_close_capture(ALCdevice *pDevice)
629 alsa_data *data = (alsa_data*)pDevice->ExtraData;
630 psnd_pcm_close(data->pcmHandle);
632 free(data);
633 pDevice->ExtraData = NULL;
636 static void alsa_start_capture(ALCdevice *pDevice)
638 alsa_data *data = (alsa_data*)pDevice->ExtraData;
639 psnd_pcm_start(data->pcmHandle);
642 static void alsa_stop_capture(ALCdevice *pDevice)
644 alsa_data *data = (alsa_data*)pDevice->ExtraData;
645 psnd_pcm_drain(data->pcmHandle);
648 static void alsa_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
650 alsa_data *data = (alsa_data*)pDevice->ExtraData;
651 const snd_pcm_channel_area_t *areas = NULL;
652 snd_pcm_sframes_t frames, commitres;
653 snd_pcm_uframes_t size, offset;
654 int err;
656 frames = psnd_pcm_avail_update(data->pcmHandle);
657 if(frames < 0)
659 err = xrun_recovery(data->pcmHandle, frames);
660 if (err < 0)
661 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
662 else
663 frames = psnd_pcm_avail_update(data->pcmHandle);
665 if (frames < (snd_pcm_sframes_t)lSamples)
667 SetALCError(ALC_INVALID_VALUE);
668 return;
671 // it is possible that contiguous areas are smaller, thus we use a loop
672 while (lSamples > 0)
674 char *Pointer;
675 int Count;
677 size = lSamples;
678 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &size);
679 if (err < 0)
681 err = xrun_recovery(data->pcmHandle, err);
682 if (err < 0)
684 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
685 break;
687 continue;
690 Pointer = (char*)areas->addr + (offset * areas->step / 8);
691 Count = size * pDevice->FrameSize;
693 memcpy(pBuffer, Pointer, Count);
694 pBuffer = (char*)pBuffer + Count;
696 commitres = psnd_pcm_mmap_commit(data->pcmHandle, offset, size);
697 if (commitres < 0 || (commitres-size) != 0)
699 AL_PRINT("mmap commit error: %s\n",
700 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
701 break;
704 lSamples -= size;
708 static ALCuint alsa_available_samples(ALCdevice *pDevice)
710 alsa_data *data = (alsa_data*)pDevice->ExtraData;
711 snd_pcm_sframes_t frames = psnd_pcm_avail_update(data->pcmHandle);
712 if(frames < 0)
714 int err = xrun_recovery(data->pcmHandle, frames);
715 if (err < 0)
716 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
717 else
718 frames = psnd_pcm_avail_update(data->pcmHandle);
719 if(frames < 0) /* ew.. */
720 SetALCError(ALC_INVALID_DEVICE);
722 return max(frames, 0);
726 BackendFuncs alsa_funcs = {
727 alsa_open_playback,
728 alsa_close_playback,
729 alsa_open_capture,
730 alsa_close_capture,
731 alsa_start_capture,
732 alsa_stop_capture,
733 alsa_capture_samples,
734 alsa_available_samples
737 void alc_alsa_init(BackendFuncs *func_list)
739 snd_ctl_t *handle;
740 int card, err, dev, idx = 1;
741 snd_ctl_card_info_t *info;
742 snd_pcm_info_t *pcminfo;
743 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
744 char name[128];
745 char *str;
747 *func_list = alsa_funcs;
749 #ifdef HAVE_DLFCN_H
750 alsa_handle = dlopen("libasound.so.2", RTLD_NOW);
751 if(!alsa_handle)
752 return;
753 dlerror();
755 #define LOAD_FUNC(f) do { \
756 p##f = (typeof(f)*)dlsym(alsa_handle, #f); \
757 if((str=dlerror()) != NULL) \
759 dlclose(alsa_handle); \
760 alsa_handle = NULL; \
761 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
762 return; \
764 } while(0)
765 #else
766 str = NULL;
767 alsa_handle = 0xDEADBEEF;
768 #define LOAD_FUNC(f) p##f = f
769 #endif
771 LOAD_FUNC(snd_strerror);
772 LOAD_FUNC(snd_pcm_open);
773 LOAD_FUNC(snd_pcm_close);
774 LOAD_FUNC(snd_pcm_nonblock);
775 LOAD_FUNC(snd_pcm_frames_to_bytes);
776 LOAD_FUNC(snd_pcm_hw_params_malloc);
777 LOAD_FUNC(snd_pcm_hw_params_free);
778 LOAD_FUNC(snd_pcm_hw_params_any);
779 LOAD_FUNC(snd_pcm_hw_params_set_access);
780 LOAD_FUNC(snd_pcm_hw_params_set_format);
781 LOAD_FUNC(snd_pcm_hw_params_set_channels);
782 LOAD_FUNC(snd_pcm_hw_params_set_periods_near);
783 LOAD_FUNC(snd_pcm_hw_params_set_rate_near);
784 LOAD_FUNC(snd_pcm_hw_params_set_rate);
785 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near);
786 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min);
787 LOAD_FUNC(snd_pcm_hw_params_get_access);
788 LOAD_FUNC(snd_pcm_hw_params);
789 LOAD_FUNC(snd_pcm_prepare);
790 LOAD_FUNC(snd_pcm_start);
791 LOAD_FUNC(snd_pcm_resume);
792 LOAD_FUNC(snd_pcm_state);
793 LOAD_FUNC(snd_pcm_avail_update);
794 LOAD_FUNC(snd_pcm_areas_silence);
795 LOAD_FUNC(snd_pcm_mmap_begin);
796 LOAD_FUNC(snd_pcm_mmap_commit);
797 LOAD_FUNC(snd_pcm_writei);
798 LOAD_FUNC(snd_pcm_drain);
800 LOAD_FUNC(snd_pcm_info_malloc);
801 LOAD_FUNC(snd_pcm_info_free);
802 LOAD_FUNC(snd_pcm_info_set_device);
803 LOAD_FUNC(snd_pcm_info_set_subdevice);
804 LOAD_FUNC(snd_pcm_info_set_stream);
805 LOAD_FUNC(snd_pcm_info_get_name);
806 LOAD_FUNC(snd_ctl_pcm_next_device);
807 LOAD_FUNC(snd_ctl_pcm_info);
808 LOAD_FUNC(snd_ctl_open);
809 LOAD_FUNC(snd_ctl_close);
810 LOAD_FUNC(snd_ctl_card_info_malloc);
811 LOAD_FUNC(snd_ctl_card_info_free);
812 LOAD_FUNC(snd_ctl_card_info);
813 LOAD_FUNC(snd_ctl_card_info_get_name);
814 LOAD_FUNC(snd_card_next);
816 #undef LOAD_FUNC
818 psnd_ctl_card_info_malloc(&info);
819 psnd_pcm_info_malloc(&pcminfo);
821 card = -1;
822 if(psnd_card_next(&card) < 0 || card < 0)
823 AL_PRINT("no playback cards found...\n");
824 else
826 alsaDeviceList[0] = AppendDeviceList("ALSA Software on default");
827 allDevNameMap[0].name = AppendAllDeviceList("ALSA Software on default");
830 while (card >= 0) {
831 sprintf(name, "hw:%d", card);
832 if ((err = psnd_ctl_open(&handle, name, 0)) < 0) {
833 AL_PRINT("control open (%i): %s\n", card, psnd_strerror(err));
834 goto next_card;
836 if ((err = psnd_ctl_card_info(handle, info)) < 0) {
837 AL_PRINT("control hardware info (%i): %s\n", card, psnd_strerror(err));
838 psnd_ctl_close(handle);
839 goto next_card;
841 if(card < MAX_DEVICES-1) {
842 snprintf(name, sizeof(name), "ALSA Software on %s",
843 psnd_ctl_card_info_get_name(info));
844 alsaDeviceList[card+1] = AppendDeviceList(name);
847 dev = -1;
848 while (idx < MAX_ALL_DEVICES) {
849 const char *cname, *dname;
851 if (psnd_ctl_pcm_next_device(handle, &dev)<0)
852 AL_PRINT("snd_ctl_pcm_next_device failed\n");
853 if (dev < 0)
854 break;
855 psnd_pcm_info_set_device(pcminfo, dev);
856 psnd_pcm_info_set_subdevice(pcminfo, 0);
857 psnd_pcm_info_set_stream(pcminfo, stream);
858 if ((err = psnd_ctl_pcm_info(handle, pcminfo)) < 0) {
859 if (err != -ENOENT)
860 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
861 continue;
864 cname = psnd_ctl_card_info_get_name(info);
865 dname = psnd_pcm_info_get_name(pcminfo);
866 snprintf(name, sizeof(name), "ALSA Software on %s [%s]",
867 cname, dname);
868 allDevNameMap[idx].name = AppendAllDeviceList(name);
869 allDevNameMap[idx].card = card;
870 allDevNameMap[idx].dev = dev;
871 idx++;
873 psnd_ctl_close(handle);
874 next_card:
875 if(psnd_card_next(&card) < 0) {
876 AL_PRINT("snd_card_next failed\n");
877 break;
882 stream = SND_PCM_STREAM_CAPTURE;
884 card = -1;
885 if(psnd_card_next(&card) < 0 || card < 0) {
886 AL_PRINT("no capture cards found...\n");
887 psnd_pcm_info_free(pcminfo);
888 psnd_ctl_card_info_free(info);
889 return;
892 alsaCaptureDeviceList[0] = AppendCaptureDeviceList("ALSA Capture on default");
894 while (card >= 0) {
895 sprintf(name, "hw:%d", card);
896 handle = NULL;
897 if ((err = psnd_ctl_open(&handle, name, 0)) < 0) {
898 AL_PRINT("control open (%i): %s\n", card, psnd_strerror(err));
900 if (err >= 0 && (err = psnd_ctl_card_info(handle, info)) < 0) {
901 AL_PRINT("control hardware info (%i): %s\n", card, psnd_strerror(err));
903 else if (err >= 0 && card < MAX_DEVICES-1)
905 snprintf(name, sizeof(name), "ALSA Capture on %s",
906 psnd_ctl_card_info_get_name(info));
907 alsaCaptureDeviceList[card+1] = AppendCaptureDeviceList(name);
909 if(handle) psnd_ctl_close(handle);
910 if(psnd_card_next(&card) < 0) {
911 AL_PRINT("snd_card_next failed\n");
912 break;
915 psnd_pcm_info_free(pcminfo);
916 psnd_ctl_card_info_free(info);