alsaaudio: Remove unused error handling of qemu_set_fd_handler
[qemu/ar7.git] / audio / alsaaudio.c
blobed7655de861e74064bf342bcd47fd016c3295f13
1 /*
2 * QEMU ALSA audio driver
4 * Copyright (c) 2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <alsa/asoundlib.h>
25 #include "qemu-common.h"
26 #include "qemu/main-loop.h"
27 #include "audio.h"
29 #if QEMU_GNUC_PREREQ(4, 3)
30 #pragma GCC diagnostic ignored "-Waddress"
31 #endif
33 #define AUDIO_CAP "alsa"
34 #include "audio_int.h"
36 struct pollhlp {
37 snd_pcm_t *handle;
38 struct pollfd *pfds;
39 int count;
40 int mask;
43 typedef struct ALSAVoiceOut {
44 HWVoiceOut hw;
45 int wpos;
46 int pending;
47 void *pcm_buf;
48 snd_pcm_t *handle;
49 struct pollhlp pollhlp;
50 } ALSAVoiceOut;
52 typedef struct ALSAVoiceIn {
53 HWVoiceIn hw;
54 snd_pcm_t *handle;
55 void *pcm_buf;
56 struct pollhlp pollhlp;
57 } ALSAVoiceIn;
59 static struct {
60 int size_in_usec_in;
61 int size_in_usec_out;
62 const char *pcm_name_in;
63 const char *pcm_name_out;
64 unsigned int buffer_size_in;
65 unsigned int period_size_in;
66 unsigned int buffer_size_out;
67 unsigned int period_size_out;
68 unsigned int threshold;
70 int buffer_size_in_overridden;
71 int period_size_in_overridden;
73 int buffer_size_out_overridden;
74 int period_size_out_overridden;
75 int verbose;
76 } conf = {
77 .buffer_size_out = 4096,
78 .period_size_out = 1024,
79 .pcm_name_out = "default",
80 .pcm_name_in = "default",
83 struct alsa_params_req {
84 int freq;
85 snd_pcm_format_t fmt;
86 int nchannels;
87 int size_in_usec;
88 int override_mask;
89 unsigned int buffer_size;
90 unsigned int period_size;
93 struct alsa_params_obt {
94 int freq;
95 audfmt_e fmt;
96 int endianness;
97 int nchannels;
98 snd_pcm_uframes_t samples;
101 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
103 va_list ap;
105 va_start (ap, fmt);
106 AUD_vlog (AUDIO_CAP, fmt, ap);
107 va_end (ap);
109 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
112 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
113 int err,
114 const char *typ,
115 const char *fmt,
119 va_list ap;
121 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
123 va_start (ap, fmt);
124 AUD_vlog (AUDIO_CAP, fmt, ap);
125 va_end (ap);
127 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
130 static void alsa_fini_poll (struct pollhlp *hlp)
132 int i;
133 struct pollfd *pfds = hlp->pfds;
135 if (pfds) {
136 for (i = 0; i < hlp->count; ++i) {
137 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
139 g_free (pfds);
141 hlp->pfds = NULL;
142 hlp->count = 0;
143 hlp->handle = NULL;
146 static void alsa_anal_close1 (snd_pcm_t **handlep)
148 int err = snd_pcm_close (*handlep);
149 if (err) {
150 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
152 *handlep = NULL;
155 static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
157 alsa_fini_poll (hlp);
158 alsa_anal_close1 (handlep);
161 static int alsa_recover (snd_pcm_t *handle)
163 int err = snd_pcm_prepare (handle);
164 if (err < 0) {
165 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
166 return -1;
168 return 0;
171 static int alsa_resume (snd_pcm_t *handle)
173 int err = snd_pcm_resume (handle);
174 if (err < 0) {
175 alsa_logerr (err, "Failed to resume handle %p\n", handle);
176 return -1;
178 return 0;
181 static void alsa_poll_handler (void *opaque)
183 int err, count;
184 snd_pcm_state_t state;
185 struct pollhlp *hlp = opaque;
186 unsigned short revents;
188 count = poll (hlp->pfds, hlp->count, 0);
189 if (count < 0) {
190 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
191 return;
194 if (!count) {
195 return;
198 /* XXX: ALSA example uses initial count, not the one returned by
199 poll, correct? */
200 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
201 hlp->count, &revents);
202 if (err < 0) {
203 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
204 return;
207 if (!(revents & hlp->mask)) {
208 if (conf.verbose) {
209 dolog ("revents = %d\n", revents);
211 return;
214 state = snd_pcm_state (hlp->handle);
215 switch (state) {
216 case SND_PCM_STATE_SETUP:
217 alsa_recover (hlp->handle);
218 break;
220 case SND_PCM_STATE_XRUN:
221 alsa_recover (hlp->handle);
222 break;
224 case SND_PCM_STATE_SUSPENDED:
225 alsa_resume (hlp->handle);
226 break;
228 case SND_PCM_STATE_PREPARED:
229 audio_run ("alsa run (prepared)");
230 break;
232 case SND_PCM_STATE_RUNNING:
233 audio_run ("alsa run (running)");
234 break;
236 default:
237 dolog ("Unexpected state %d\n", state);
241 static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
243 int i, count, err;
244 struct pollfd *pfds;
246 count = snd_pcm_poll_descriptors_count (handle);
247 if (count <= 0) {
248 dolog ("Could not initialize poll mode\n"
249 "Invalid number of poll descriptors %d\n", count);
250 return -1;
253 pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
254 if (!pfds) {
255 dolog ("Could not initialize poll mode\n");
256 return -1;
259 err = snd_pcm_poll_descriptors (handle, pfds, count);
260 if (err < 0) {
261 alsa_logerr (err, "Could not initialize poll mode\n"
262 "Could not obtain poll descriptors\n");
263 g_free (pfds);
264 return -1;
267 for (i = 0; i < count; ++i) {
268 if (pfds[i].events & POLLIN) {
269 qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp);
271 if (pfds[i].events & POLLOUT) {
272 if (conf.verbose) {
273 dolog ("POLLOUT %d %d\n", i, pfds[i].fd);
275 qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
277 if (conf.verbose) {
278 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
279 pfds[i].events, i, pfds[i].fd, err);
283 hlp->pfds = pfds;
284 hlp->count = count;
285 hlp->handle = handle;
286 hlp->mask = mask;
287 return 0;
290 static int alsa_poll_out (HWVoiceOut *hw)
292 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
294 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
297 static int alsa_poll_in (HWVoiceIn *hw)
299 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
301 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
304 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
306 return audio_pcm_sw_write (sw, buf, len);
309 static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt, int endianness)
311 switch (fmt) {
312 case AUD_FMT_S8:
313 return SND_PCM_FORMAT_S8;
315 case AUD_FMT_U8:
316 return SND_PCM_FORMAT_U8;
318 case AUD_FMT_S16:
319 if (endianness) {
320 return SND_PCM_FORMAT_S16_BE;
322 else {
323 return SND_PCM_FORMAT_S16_LE;
326 case AUD_FMT_U16:
327 if (endianness) {
328 return SND_PCM_FORMAT_U16_BE;
330 else {
331 return SND_PCM_FORMAT_U16_LE;
334 case AUD_FMT_S32:
335 if (endianness) {
336 return SND_PCM_FORMAT_S32_BE;
338 else {
339 return SND_PCM_FORMAT_S32_LE;
342 case AUD_FMT_U32:
343 if (endianness) {
344 return SND_PCM_FORMAT_U32_BE;
346 else {
347 return SND_PCM_FORMAT_U32_LE;
350 default:
351 dolog ("Internal logic error: Bad audio format %d\n", fmt);
352 #ifdef DEBUG_AUDIO
353 abort ();
354 #endif
355 return SND_PCM_FORMAT_U8;
359 static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
360 int *endianness)
362 switch (alsafmt) {
363 case SND_PCM_FORMAT_S8:
364 *endianness = 0;
365 *fmt = AUD_FMT_S8;
366 break;
368 case SND_PCM_FORMAT_U8:
369 *endianness = 0;
370 *fmt = AUD_FMT_U8;
371 break;
373 case SND_PCM_FORMAT_S16_LE:
374 *endianness = 0;
375 *fmt = AUD_FMT_S16;
376 break;
378 case SND_PCM_FORMAT_U16_LE:
379 *endianness = 0;
380 *fmt = AUD_FMT_U16;
381 break;
383 case SND_PCM_FORMAT_S16_BE:
384 *endianness = 1;
385 *fmt = AUD_FMT_S16;
386 break;
388 case SND_PCM_FORMAT_U16_BE:
389 *endianness = 1;
390 *fmt = AUD_FMT_U16;
391 break;
393 case SND_PCM_FORMAT_S32_LE:
394 *endianness = 0;
395 *fmt = AUD_FMT_S32;
396 break;
398 case SND_PCM_FORMAT_U32_LE:
399 *endianness = 0;
400 *fmt = AUD_FMT_U32;
401 break;
403 case SND_PCM_FORMAT_S32_BE:
404 *endianness = 1;
405 *fmt = AUD_FMT_S32;
406 break;
408 case SND_PCM_FORMAT_U32_BE:
409 *endianness = 1;
410 *fmt = AUD_FMT_U32;
411 break;
413 default:
414 dolog ("Unrecognized audio format %d\n", alsafmt);
415 return -1;
418 return 0;
421 static void alsa_dump_info (struct alsa_params_req *req,
422 struct alsa_params_obt *obt,
423 snd_pcm_format_t obtfmt)
425 dolog ("parameter | requested value | obtained value\n");
426 dolog ("format | %10d | %10d\n", req->fmt, obtfmt);
427 dolog ("channels | %10d | %10d\n",
428 req->nchannels, obt->nchannels);
429 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
430 dolog ("============================================\n");
431 dolog ("requested: buffer size %d period size %d\n",
432 req->buffer_size, req->period_size);
433 dolog ("obtained: samples %ld\n", obt->samples);
436 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
438 int err;
439 snd_pcm_sw_params_t *sw_params;
441 snd_pcm_sw_params_alloca (&sw_params);
443 err = snd_pcm_sw_params_current (handle, sw_params);
444 if (err < 0) {
445 dolog ("Could not fully initialize DAC\n");
446 alsa_logerr (err, "Failed to get current software parameters\n");
447 return;
450 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
451 if (err < 0) {
452 dolog ("Could not fully initialize DAC\n");
453 alsa_logerr (err, "Failed to set software threshold to %ld\n",
454 threshold);
455 return;
458 err = snd_pcm_sw_params (handle, sw_params);
459 if (err < 0) {
460 dolog ("Could not fully initialize DAC\n");
461 alsa_logerr (err, "Failed to set software parameters\n");
462 return;
466 static int alsa_open (int in, struct alsa_params_req *req,
467 struct alsa_params_obt *obt, snd_pcm_t **handlep)
469 snd_pcm_t *handle;
470 snd_pcm_hw_params_t *hw_params;
471 int err;
472 int size_in_usec;
473 unsigned int freq, nchannels;
474 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
475 snd_pcm_uframes_t obt_buffer_size;
476 const char *typ = in ? "ADC" : "DAC";
477 snd_pcm_format_t obtfmt;
479 freq = req->freq;
480 nchannels = req->nchannels;
481 size_in_usec = req->size_in_usec;
483 snd_pcm_hw_params_alloca (&hw_params);
485 err = snd_pcm_open (
486 &handle,
487 pcm_name,
488 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
489 SND_PCM_NONBLOCK
491 if (err < 0) {
492 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
493 return -1;
496 err = snd_pcm_hw_params_any (handle, hw_params);
497 if (err < 0) {
498 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
499 goto err;
502 err = snd_pcm_hw_params_set_access (
503 handle,
504 hw_params,
505 SND_PCM_ACCESS_RW_INTERLEAVED
507 if (err < 0) {
508 alsa_logerr2 (err, typ, "Failed to set access type\n");
509 goto err;
512 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
513 if (err < 0 && conf.verbose) {
514 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
517 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
518 if (err < 0) {
519 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
520 goto err;
523 err = snd_pcm_hw_params_set_channels_near (
524 handle,
525 hw_params,
526 &nchannels
528 if (err < 0) {
529 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
530 req->nchannels);
531 goto err;
534 if (nchannels != 1 && nchannels != 2) {
535 alsa_logerr2 (err, typ,
536 "Can not handle obtained number of channels %d\n",
537 nchannels);
538 goto err;
541 if (req->buffer_size) {
542 unsigned long obt;
544 if (size_in_usec) {
545 int dir = 0;
546 unsigned int btime = req->buffer_size;
548 err = snd_pcm_hw_params_set_buffer_time_near (
549 handle,
550 hw_params,
551 &btime,
552 &dir
554 obt = btime;
556 else {
557 snd_pcm_uframes_t bsize = req->buffer_size;
559 err = snd_pcm_hw_params_set_buffer_size_near (
560 handle,
561 hw_params,
562 &bsize
564 obt = bsize;
566 if (err < 0) {
567 alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n",
568 size_in_usec ? "time" : "size", req->buffer_size);
569 goto err;
572 if ((req->override_mask & 2) && (obt - req->buffer_size))
573 dolog ("Requested buffer %s %u was rejected, using %lu\n",
574 size_in_usec ? "time" : "size", req->buffer_size, obt);
577 if (req->period_size) {
578 unsigned long obt;
580 if (size_in_usec) {
581 int dir = 0;
582 unsigned int ptime = req->period_size;
584 err = snd_pcm_hw_params_set_period_time_near (
585 handle,
586 hw_params,
587 &ptime,
588 &dir
590 obt = ptime;
592 else {
593 int dir = 0;
594 snd_pcm_uframes_t psize = req->period_size;
596 err = snd_pcm_hw_params_set_period_size_near (
597 handle,
598 hw_params,
599 &psize,
600 &dir
602 obt = psize;
605 if (err < 0) {
606 alsa_logerr2 (err, typ, "Failed to set period %s to %d\n",
607 size_in_usec ? "time" : "size", req->period_size);
608 goto err;
611 if (((req->override_mask & 1) && (obt - req->period_size)))
612 dolog ("Requested period %s %u was rejected, using %lu\n",
613 size_in_usec ? "time" : "size", req->period_size, obt);
616 err = snd_pcm_hw_params (handle, hw_params);
617 if (err < 0) {
618 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
619 goto err;
622 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
623 if (err < 0) {
624 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
625 goto err;
628 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
629 if (err < 0) {
630 alsa_logerr2 (err, typ, "Failed to get format\n");
631 goto err;
634 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
635 dolog ("Invalid format was returned %d\n", obtfmt);
636 goto err;
639 err = snd_pcm_prepare (handle);
640 if (err < 0) {
641 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
642 goto err;
645 if (!in && conf.threshold) {
646 snd_pcm_uframes_t threshold;
647 int bytes_per_sec;
649 bytes_per_sec = freq << (nchannels == 2);
651 switch (obt->fmt) {
652 case AUD_FMT_S8:
653 case AUD_FMT_U8:
654 break;
656 case AUD_FMT_S16:
657 case AUD_FMT_U16:
658 bytes_per_sec <<= 1;
659 break;
661 case AUD_FMT_S32:
662 case AUD_FMT_U32:
663 bytes_per_sec <<= 2;
664 break;
667 threshold = (conf.threshold * bytes_per_sec) / 1000;
668 alsa_set_threshold (handle, threshold);
671 obt->nchannels = nchannels;
672 obt->freq = freq;
673 obt->samples = obt_buffer_size;
675 *handlep = handle;
677 if (conf.verbose &&
678 (obtfmt != req->fmt ||
679 obt->nchannels != req->nchannels ||
680 obt->freq != req->freq)) {
681 dolog ("Audio parameters for %s\n", typ);
682 alsa_dump_info (req, obt, obtfmt);
685 #ifdef DEBUG
686 alsa_dump_info (req, obt, obtfmt);
687 #endif
688 return 0;
690 err:
691 alsa_anal_close1 (&handle);
692 return -1;
695 static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
697 snd_pcm_sframes_t avail;
699 avail = snd_pcm_avail_update (handle);
700 if (avail < 0) {
701 if (avail == -EPIPE) {
702 if (!alsa_recover (handle)) {
703 avail = snd_pcm_avail_update (handle);
707 if (avail < 0) {
708 alsa_logerr (avail,
709 "Could not obtain number of available frames\n");
710 return -1;
714 return avail;
717 static void alsa_write_pending (ALSAVoiceOut *alsa)
719 HWVoiceOut *hw = &alsa->hw;
721 while (alsa->pending) {
722 int left_till_end_samples = hw->samples - alsa->wpos;
723 int len = audio_MIN (alsa->pending, left_till_end_samples);
724 char *src = advance (alsa->pcm_buf, alsa->wpos << hw->info.shift);
726 while (len) {
727 snd_pcm_sframes_t written;
729 written = snd_pcm_writei (alsa->handle, src, len);
731 if (written <= 0) {
732 switch (written) {
733 case 0:
734 if (conf.verbose) {
735 dolog ("Failed to write %d frames (wrote zero)\n", len);
737 return;
739 case -EPIPE:
740 if (alsa_recover (alsa->handle)) {
741 alsa_logerr (written, "Failed to write %d frames\n",
742 len);
743 return;
745 if (conf.verbose) {
746 dolog ("Recovering from playback xrun\n");
748 continue;
750 case -ESTRPIPE:
751 /* stream is suspended and waiting for an
752 application recovery */
753 if (alsa_resume (alsa->handle)) {
754 alsa_logerr (written, "Failed to write %d frames\n",
755 len);
756 return;
758 if (conf.verbose) {
759 dolog ("Resuming suspended output stream\n");
761 continue;
763 case -EAGAIN:
764 return;
766 default:
767 alsa_logerr (written, "Failed to write %d frames from %p\n",
768 len, src);
769 return;
773 alsa->wpos = (alsa->wpos + written) % hw->samples;
774 alsa->pending -= written;
775 len -= written;
780 static int alsa_run_out (HWVoiceOut *hw, int live)
782 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
783 int decr;
784 snd_pcm_sframes_t avail;
786 avail = alsa_get_avail (alsa->handle);
787 if (avail < 0) {
788 dolog ("Could not get number of available playback frames\n");
789 return 0;
792 decr = audio_MIN (live, avail);
793 decr = audio_pcm_hw_clip_out (hw, alsa->pcm_buf, decr, alsa->pending);
794 alsa->pending += decr;
795 alsa_write_pending (alsa);
796 return decr;
799 static void alsa_fini_out (HWVoiceOut *hw)
801 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
803 ldebug ("alsa_fini\n");
804 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
806 g_free(alsa->pcm_buf);
807 alsa->pcm_buf = NULL;
810 static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
812 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
813 struct alsa_params_req req;
814 struct alsa_params_obt obt;
815 snd_pcm_t *handle;
816 struct audsettings obt_as;
818 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
819 req.freq = as->freq;
820 req.nchannels = as->nchannels;
821 req.period_size = conf.period_size_out;
822 req.buffer_size = conf.buffer_size_out;
823 req.size_in_usec = conf.size_in_usec_out;
824 req.override_mask =
825 (conf.period_size_out_overridden ? 1 : 0) |
826 (conf.buffer_size_out_overridden ? 2 : 0);
828 if (alsa_open (0, &req, &obt, &handle)) {
829 return -1;
832 obt_as.freq = obt.freq;
833 obt_as.nchannels = obt.nchannels;
834 obt_as.fmt = obt.fmt;
835 obt_as.endianness = obt.endianness;
837 audio_pcm_init_info (&hw->info, &obt_as);
838 hw->samples = obt.samples;
840 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
841 if (!alsa->pcm_buf) {
842 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
843 hw->samples, 1 << hw->info.shift);
844 alsa_anal_close1 (&handle);
845 return -1;
848 alsa->handle = handle;
849 return 0;
852 #define VOICE_CTL_PAUSE 0
853 #define VOICE_CTL_PREPARE 1
854 #define VOICE_CTL_START 2
856 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
858 int err;
860 if (ctl == VOICE_CTL_PAUSE) {
861 err = snd_pcm_drop (handle);
862 if (err < 0) {
863 alsa_logerr (err, "Could not stop %s\n", typ);
864 return -1;
867 else {
868 err = snd_pcm_prepare (handle);
869 if (err < 0) {
870 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
871 return -1;
873 if (ctl == VOICE_CTL_START) {
874 err = snd_pcm_start(handle);
875 if (err < 0) {
876 alsa_logerr (err, "Could not start handle for %s\n", typ);
877 return -1;
882 return 0;
885 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
887 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
889 switch (cmd) {
890 case VOICE_ENABLE:
892 va_list ap;
893 int poll_mode;
895 va_start (ap, cmd);
896 poll_mode = va_arg (ap, int);
897 va_end (ap);
899 ldebug ("enabling voice\n");
900 if (poll_mode && alsa_poll_out (hw)) {
901 poll_mode = 0;
903 hw->poll_mode = poll_mode;
904 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PREPARE);
907 case VOICE_DISABLE:
908 ldebug ("disabling voice\n");
909 if (hw->poll_mode) {
910 hw->poll_mode = 0;
911 alsa_fini_poll (&alsa->pollhlp);
913 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PAUSE);
916 return -1;
919 static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
921 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
922 struct alsa_params_req req;
923 struct alsa_params_obt obt;
924 snd_pcm_t *handle;
925 struct audsettings obt_as;
927 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
928 req.freq = as->freq;
929 req.nchannels = as->nchannels;
930 req.period_size = conf.period_size_in;
931 req.buffer_size = conf.buffer_size_in;
932 req.size_in_usec = conf.size_in_usec_in;
933 req.override_mask =
934 (conf.period_size_in_overridden ? 1 : 0) |
935 (conf.buffer_size_in_overridden ? 2 : 0);
937 if (alsa_open (1, &req, &obt, &handle)) {
938 return -1;
941 obt_as.freq = obt.freq;
942 obt_as.nchannels = obt.nchannels;
943 obt_as.fmt = obt.fmt;
944 obt_as.endianness = obt.endianness;
946 audio_pcm_init_info (&hw->info, &obt_as);
947 hw->samples = obt.samples;
949 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
950 if (!alsa->pcm_buf) {
951 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
952 hw->samples, 1 << hw->info.shift);
953 alsa_anal_close1 (&handle);
954 return -1;
957 alsa->handle = handle;
958 return 0;
961 static void alsa_fini_in (HWVoiceIn *hw)
963 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
965 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
967 g_free(alsa->pcm_buf);
968 alsa->pcm_buf = NULL;
971 static int alsa_run_in (HWVoiceIn *hw)
973 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
974 int hwshift = hw->info.shift;
975 int i;
976 int live = audio_pcm_hw_get_live_in (hw);
977 int dead = hw->samples - live;
978 int decr;
979 struct {
980 int add;
981 int len;
982 } bufs[2] = {
983 { .add = hw->wpos, .len = 0 },
984 { .add = 0, .len = 0 }
986 snd_pcm_sframes_t avail;
987 snd_pcm_uframes_t read_samples = 0;
989 if (!dead) {
990 return 0;
993 avail = alsa_get_avail (alsa->handle);
994 if (avail < 0) {
995 dolog ("Could not get number of captured frames\n");
996 return 0;
999 if (!avail) {
1000 snd_pcm_state_t state;
1002 state = snd_pcm_state (alsa->handle);
1003 switch (state) {
1004 case SND_PCM_STATE_PREPARED:
1005 avail = hw->samples;
1006 break;
1007 case SND_PCM_STATE_SUSPENDED:
1008 /* stream is suspended and waiting for an application recovery */
1009 if (alsa_resume (alsa->handle)) {
1010 dolog ("Failed to resume suspended input stream\n");
1011 return 0;
1013 if (conf.verbose) {
1014 dolog ("Resuming suspended input stream\n");
1016 break;
1017 default:
1018 if (conf.verbose) {
1019 dolog ("No frames available and ALSA state is %d\n", state);
1021 return 0;
1025 decr = audio_MIN (dead, avail);
1026 if (!decr) {
1027 return 0;
1030 if (hw->wpos + decr > hw->samples) {
1031 bufs[0].len = (hw->samples - hw->wpos);
1032 bufs[1].len = (decr - (hw->samples - hw->wpos));
1034 else {
1035 bufs[0].len = decr;
1038 for (i = 0; i < 2; ++i) {
1039 void *src;
1040 struct st_sample *dst;
1041 snd_pcm_sframes_t nread;
1042 snd_pcm_uframes_t len;
1044 len = bufs[i].len;
1046 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
1047 dst = hw->conv_buf + bufs[i].add;
1049 while (len) {
1050 nread = snd_pcm_readi (alsa->handle, src, len);
1052 if (nread <= 0) {
1053 switch (nread) {
1054 case 0:
1055 if (conf.verbose) {
1056 dolog ("Failed to read %ld frames (read zero)\n", len);
1058 goto exit;
1060 case -EPIPE:
1061 if (alsa_recover (alsa->handle)) {
1062 alsa_logerr (nread, "Failed to read %ld frames\n", len);
1063 goto exit;
1065 if (conf.verbose) {
1066 dolog ("Recovering from capture xrun\n");
1068 continue;
1070 case -EAGAIN:
1071 goto exit;
1073 default:
1074 alsa_logerr (
1075 nread,
1076 "Failed to read %ld frames from %p\n",
1077 len,
1080 goto exit;
1084 hw->conv (dst, src, nread);
1086 src = advance (src, nread << hwshift);
1087 dst += nread;
1089 read_samples += nread;
1090 len -= nread;
1094 exit:
1095 hw->wpos = (hw->wpos + read_samples) % hw->samples;
1096 return read_samples;
1099 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
1101 return audio_pcm_sw_read (sw, buf, size);
1104 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
1106 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
1108 switch (cmd) {
1109 case VOICE_ENABLE:
1111 va_list ap;
1112 int poll_mode;
1114 va_start (ap, cmd);
1115 poll_mode = va_arg (ap, int);
1116 va_end (ap);
1118 ldebug ("enabling voice\n");
1119 if (poll_mode && alsa_poll_in (hw)) {
1120 poll_mode = 0;
1122 hw->poll_mode = poll_mode;
1124 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_START);
1127 case VOICE_DISABLE:
1128 ldebug ("disabling voice\n");
1129 if (hw->poll_mode) {
1130 hw->poll_mode = 0;
1131 alsa_fini_poll (&alsa->pollhlp);
1133 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_PAUSE);
1136 return -1;
1139 static void *alsa_audio_init (void)
1141 return &conf;
1144 static void alsa_audio_fini (void *opaque)
1146 (void) opaque;
1149 static struct audio_option alsa_options[] = {
1151 .name = "DAC_SIZE_IN_USEC",
1152 .tag = AUD_OPT_BOOL,
1153 .valp = &conf.size_in_usec_out,
1154 .descr = "DAC period/buffer size in microseconds (otherwise in frames)"
1157 .name = "DAC_PERIOD_SIZE",
1158 .tag = AUD_OPT_INT,
1159 .valp = &conf.period_size_out,
1160 .descr = "DAC period size (0 to go with system default)",
1161 .overriddenp = &conf.period_size_out_overridden
1164 .name = "DAC_BUFFER_SIZE",
1165 .tag = AUD_OPT_INT,
1166 .valp = &conf.buffer_size_out,
1167 .descr = "DAC buffer size (0 to go with system default)",
1168 .overriddenp = &conf.buffer_size_out_overridden
1171 .name = "ADC_SIZE_IN_USEC",
1172 .tag = AUD_OPT_BOOL,
1173 .valp = &conf.size_in_usec_in,
1174 .descr =
1175 "ADC period/buffer size in microseconds (otherwise in frames)"
1178 .name = "ADC_PERIOD_SIZE",
1179 .tag = AUD_OPT_INT,
1180 .valp = &conf.period_size_in,
1181 .descr = "ADC period size (0 to go with system default)",
1182 .overriddenp = &conf.period_size_in_overridden
1185 .name = "ADC_BUFFER_SIZE",
1186 .tag = AUD_OPT_INT,
1187 .valp = &conf.buffer_size_in,
1188 .descr = "ADC buffer size (0 to go with system default)",
1189 .overriddenp = &conf.buffer_size_in_overridden
1192 .name = "THRESHOLD",
1193 .tag = AUD_OPT_INT,
1194 .valp = &conf.threshold,
1195 .descr = "(undocumented)"
1198 .name = "DAC_DEV",
1199 .tag = AUD_OPT_STR,
1200 .valp = &conf.pcm_name_out,
1201 .descr = "DAC device name (for instance dmix)"
1204 .name = "ADC_DEV",
1205 .tag = AUD_OPT_STR,
1206 .valp = &conf.pcm_name_in,
1207 .descr = "ADC device name"
1210 .name = "VERBOSE",
1211 .tag = AUD_OPT_BOOL,
1212 .valp = &conf.verbose,
1213 .descr = "Behave in a more verbose way"
1215 { /* End of list */ }
1218 static struct audio_pcm_ops alsa_pcm_ops = {
1219 .init_out = alsa_init_out,
1220 .fini_out = alsa_fini_out,
1221 .run_out = alsa_run_out,
1222 .write = alsa_write,
1223 .ctl_out = alsa_ctl_out,
1225 .init_in = alsa_init_in,
1226 .fini_in = alsa_fini_in,
1227 .run_in = alsa_run_in,
1228 .read = alsa_read,
1229 .ctl_in = alsa_ctl_in,
1232 struct audio_driver alsa_audio_driver = {
1233 .name = "alsa",
1234 .descr = "ALSA http://www.alsa-project.org",
1235 .options = alsa_options,
1236 .init = alsa_audio_init,
1237 .fini = alsa_audio_fini,
1238 .pcm_ops = &alsa_pcm_ops,
1239 .can_be_default = 1,
1240 .max_voices_out = INT_MAX,
1241 .max_voices_in = INT_MAX,
1242 .voice_size_out = sizeof (ALSAVoiceOut),
1243 .voice_size_in = sizeof (ALSAVoiceIn)