ivshmem: print error on invalid peer id
[qemu/ar7.git] / audio / alsaaudio.c
blob6315b2d7467c802410dea2efbaa09d939f28ca18
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"
28 #include "trace.h"
30 #if QEMU_GNUC_PREREQ(4, 3)
31 #pragma GCC diagnostic ignored "-Waddress"
32 #endif
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
37 typedef struct ALSAConf {
38 int size_in_usec_in;
39 int size_in_usec_out;
40 const char *pcm_name_in;
41 const char *pcm_name_out;
42 unsigned int buffer_size_in;
43 unsigned int period_size_in;
44 unsigned int buffer_size_out;
45 unsigned int period_size_out;
46 unsigned int threshold;
48 int buffer_size_in_overridden;
49 int period_size_in_overridden;
51 int buffer_size_out_overridden;
52 int period_size_out_overridden;
53 } ALSAConf;
55 struct pollhlp {
56 snd_pcm_t *handle;
57 struct pollfd *pfds;
58 ALSAConf *conf;
59 int count;
60 int mask;
63 typedef struct ALSAVoiceOut {
64 HWVoiceOut hw;
65 int wpos;
66 int pending;
67 void *pcm_buf;
68 snd_pcm_t *handle;
69 struct pollhlp pollhlp;
70 } ALSAVoiceOut;
72 typedef struct ALSAVoiceIn {
73 HWVoiceIn hw;
74 snd_pcm_t *handle;
75 void *pcm_buf;
76 struct pollhlp pollhlp;
77 } ALSAVoiceIn;
79 struct alsa_params_req {
80 int freq;
81 snd_pcm_format_t fmt;
82 int nchannels;
83 int size_in_usec;
84 int override_mask;
85 unsigned int buffer_size;
86 unsigned int period_size;
89 struct alsa_params_obt {
90 int freq;
91 audfmt_e fmt;
92 int endianness;
93 int nchannels;
94 snd_pcm_uframes_t samples;
97 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
99 va_list ap;
101 va_start (ap, fmt);
102 AUD_vlog (AUDIO_CAP, fmt, ap);
103 va_end (ap);
105 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
108 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
109 int err,
110 const char *typ,
111 const char *fmt,
115 va_list ap;
117 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
119 va_start (ap, fmt);
120 AUD_vlog (AUDIO_CAP, fmt, ap);
121 va_end (ap);
123 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
126 static void alsa_fini_poll (struct pollhlp *hlp)
128 int i;
129 struct pollfd *pfds = hlp->pfds;
131 if (pfds) {
132 for (i = 0; i < hlp->count; ++i) {
133 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
135 g_free (pfds);
137 hlp->pfds = NULL;
138 hlp->count = 0;
139 hlp->handle = NULL;
142 static void alsa_anal_close1 (snd_pcm_t **handlep)
144 int err = snd_pcm_close (*handlep);
145 if (err) {
146 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
148 *handlep = NULL;
151 static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
153 alsa_fini_poll (hlp);
154 alsa_anal_close1 (handlep);
157 static int alsa_recover (snd_pcm_t *handle)
159 int err = snd_pcm_prepare (handle);
160 if (err < 0) {
161 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
162 return -1;
164 return 0;
167 static int alsa_resume (snd_pcm_t *handle)
169 int err = snd_pcm_resume (handle);
170 if (err < 0) {
171 alsa_logerr (err, "Failed to resume handle %p\n", handle);
172 return -1;
174 return 0;
177 static void alsa_poll_handler (void *opaque)
179 int err, count;
180 snd_pcm_state_t state;
181 struct pollhlp *hlp = opaque;
182 unsigned short revents;
184 count = poll (hlp->pfds, hlp->count, 0);
185 if (count < 0) {
186 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
187 return;
190 if (!count) {
191 return;
194 /* XXX: ALSA example uses initial count, not the one returned by
195 poll, correct? */
196 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
197 hlp->count, &revents);
198 if (err < 0) {
199 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
200 return;
203 if (!(revents & hlp->mask)) {
204 trace_alsa_revents(revents);
205 return;
208 state = snd_pcm_state (hlp->handle);
209 switch (state) {
210 case SND_PCM_STATE_SETUP:
211 alsa_recover (hlp->handle);
212 break;
214 case SND_PCM_STATE_XRUN:
215 alsa_recover (hlp->handle);
216 break;
218 case SND_PCM_STATE_SUSPENDED:
219 alsa_resume (hlp->handle);
220 break;
222 case SND_PCM_STATE_PREPARED:
223 audio_run ("alsa run (prepared)");
224 break;
226 case SND_PCM_STATE_RUNNING:
227 audio_run ("alsa run (running)");
228 break;
230 default:
231 dolog ("Unexpected state %d\n", state);
235 static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
237 int i, count, err;
238 struct pollfd *pfds;
240 count = snd_pcm_poll_descriptors_count (handle);
241 if (count <= 0) {
242 dolog ("Could not initialize poll mode\n"
243 "Invalid number of poll descriptors %d\n", count);
244 return -1;
247 pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
248 if (!pfds) {
249 dolog ("Could not initialize poll mode\n");
250 return -1;
253 err = snd_pcm_poll_descriptors (handle, pfds, count);
254 if (err < 0) {
255 alsa_logerr (err, "Could not initialize poll mode\n"
256 "Could not obtain poll descriptors\n");
257 g_free (pfds);
258 return -1;
261 for (i = 0; i < count; ++i) {
262 if (pfds[i].events & POLLIN) {
263 qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp);
265 if (pfds[i].events & POLLOUT) {
266 trace_alsa_pollout(i, pfds[i].fd);
267 qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
269 trace_alsa_set_handler(pfds[i].events, i, pfds[i].fd, err);
272 hlp->pfds = pfds;
273 hlp->count = count;
274 hlp->handle = handle;
275 hlp->mask = mask;
276 return 0;
279 static int alsa_poll_out (HWVoiceOut *hw)
281 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
283 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
286 static int alsa_poll_in (HWVoiceIn *hw)
288 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
290 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
293 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
295 return audio_pcm_sw_write (sw, buf, len);
298 static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt, int endianness)
300 switch (fmt) {
301 case AUD_FMT_S8:
302 return SND_PCM_FORMAT_S8;
304 case AUD_FMT_U8:
305 return SND_PCM_FORMAT_U8;
307 case AUD_FMT_S16:
308 if (endianness) {
309 return SND_PCM_FORMAT_S16_BE;
311 else {
312 return SND_PCM_FORMAT_S16_LE;
315 case AUD_FMT_U16:
316 if (endianness) {
317 return SND_PCM_FORMAT_U16_BE;
319 else {
320 return SND_PCM_FORMAT_U16_LE;
323 case AUD_FMT_S32:
324 if (endianness) {
325 return SND_PCM_FORMAT_S32_BE;
327 else {
328 return SND_PCM_FORMAT_S32_LE;
331 case AUD_FMT_U32:
332 if (endianness) {
333 return SND_PCM_FORMAT_U32_BE;
335 else {
336 return SND_PCM_FORMAT_U32_LE;
339 default:
340 dolog ("Internal logic error: Bad audio format %d\n", fmt);
341 #ifdef DEBUG_AUDIO
342 abort ();
343 #endif
344 return SND_PCM_FORMAT_U8;
348 static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
349 int *endianness)
351 switch (alsafmt) {
352 case SND_PCM_FORMAT_S8:
353 *endianness = 0;
354 *fmt = AUD_FMT_S8;
355 break;
357 case SND_PCM_FORMAT_U8:
358 *endianness = 0;
359 *fmt = AUD_FMT_U8;
360 break;
362 case SND_PCM_FORMAT_S16_LE:
363 *endianness = 0;
364 *fmt = AUD_FMT_S16;
365 break;
367 case SND_PCM_FORMAT_U16_LE:
368 *endianness = 0;
369 *fmt = AUD_FMT_U16;
370 break;
372 case SND_PCM_FORMAT_S16_BE:
373 *endianness = 1;
374 *fmt = AUD_FMT_S16;
375 break;
377 case SND_PCM_FORMAT_U16_BE:
378 *endianness = 1;
379 *fmt = AUD_FMT_U16;
380 break;
382 case SND_PCM_FORMAT_S32_LE:
383 *endianness = 0;
384 *fmt = AUD_FMT_S32;
385 break;
387 case SND_PCM_FORMAT_U32_LE:
388 *endianness = 0;
389 *fmt = AUD_FMT_U32;
390 break;
392 case SND_PCM_FORMAT_S32_BE:
393 *endianness = 1;
394 *fmt = AUD_FMT_S32;
395 break;
397 case SND_PCM_FORMAT_U32_BE:
398 *endianness = 1;
399 *fmt = AUD_FMT_U32;
400 break;
402 default:
403 dolog ("Unrecognized audio format %d\n", alsafmt);
404 return -1;
407 return 0;
410 static void alsa_dump_info (struct alsa_params_req *req,
411 struct alsa_params_obt *obt,
412 snd_pcm_format_t obtfmt)
414 dolog ("parameter | requested value | obtained value\n");
415 dolog ("format | %10d | %10d\n", req->fmt, obtfmt);
416 dolog ("channels | %10d | %10d\n",
417 req->nchannels, obt->nchannels);
418 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
419 dolog ("============================================\n");
420 dolog ("requested: buffer size %d period size %d\n",
421 req->buffer_size, req->period_size);
422 dolog ("obtained: samples %ld\n", obt->samples);
425 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
427 int err;
428 snd_pcm_sw_params_t *sw_params;
430 snd_pcm_sw_params_alloca (&sw_params);
432 err = snd_pcm_sw_params_current (handle, sw_params);
433 if (err < 0) {
434 dolog ("Could not fully initialize DAC\n");
435 alsa_logerr (err, "Failed to get current software parameters\n");
436 return;
439 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
440 if (err < 0) {
441 dolog ("Could not fully initialize DAC\n");
442 alsa_logerr (err, "Failed to set software threshold to %ld\n",
443 threshold);
444 return;
447 err = snd_pcm_sw_params (handle, sw_params);
448 if (err < 0) {
449 dolog ("Could not fully initialize DAC\n");
450 alsa_logerr (err, "Failed to set software parameters\n");
451 return;
455 static int alsa_open (int in, struct alsa_params_req *req,
456 struct alsa_params_obt *obt, snd_pcm_t **handlep,
457 ALSAConf *conf)
459 snd_pcm_t *handle;
460 snd_pcm_hw_params_t *hw_params;
461 int err;
462 int size_in_usec;
463 unsigned int freq, nchannels;
464 const char *pcm_name = in ? conf->pcm_name_in : conf->pcm_name_out;
465 snd_pcm_uframes_t obt_buffer_size;
466 const char *typ = in ? "ADC" : "DAC";
467 snd_pcm_format_t obtfmt;
469 freq = req->freq;
470 nchannels = req->nchannels;
471 size_in_usec = req->size_in_usec;
473 snd_pcm_hw_params_alloca (&hw_params);
475 err = snd_pcm_open (
476 &handle,
477 pcm_name,
478 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
479 SND_PCM_NONBLOCK
481 if (err < 0) {
482 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
483 return -1;
486 err = snd_pcm_hw_params_any (handle, hw_params);
487 if (err < 0) {
488 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
489 goto err;
492 err = snd_pcm_hw_params_set_access (
493 handle,
494 hw_params,
495 SND_PCM_ACCESS_RW_INTERLEAVED
497 if (err < 0) {
498 alsa_logerr2 (err, typ, "Failed to set access type\n");
499 goto err;
502 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
503 if (err < 0) {
504 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
507 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
508 if (err < 0) {
509 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
510 goto err;
513 err = snd_pcm_hw_params_set_channels_near (
514 handle,
515 hw_params,
516 &nchannels
518 if (err < 0) {
519 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
520 req->nchannels);
521 goto err;
524 if (nchannels != 1 && nchannels != 2) {
525 alsa_logerr2 (err, typ,
526 "Can not handle obtained number of channels %d\n",
527 nchannels);
528 goto err;
531 if (req->buffer_size) {
532 unsigned long obt;
534 if (size_in_usec) {
535 int dir = 0;
536 unsigned int btime = req->buffer_size;
538 err = snd_pcm_hw_params_set_buffer_time_near (
539 handle,
540 hw_params,
541 &btime,
542 &dir
544 obt = btime;
546 else {
547 snd_pcm_uframes_t bsize = req->buffer_size;
549 err = snd_pcm_hw_params_set_buffer_size_near (
550 handle,
551 hw_params,
552 &bsize
554 obt = bsize;
556 if (err < 0) {
557 alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n",
558 size_in_usec ? "time" : "size", req->buffer_size);
559 goto err;
562 if ((req->override_mask & 2) && (obt - req->buffer_size))
563 dolog ("Requested buffer %s %u was rejected, using %lu\n",
564 size_in_usec ? "time" : "size", req->buffer_size, obt);
567 if (req->period_size) {
568 unsigned long obt;
570 if (size_in_usec) {
571 int dir = 0;
572 unsigned int ptime = req->period_size;
574 err = snd_pcm_hw_params_set_period_time_near (
575 handle,
576 hw_params,
577 &ptime,
578 &dir
580 obt = ptime;
582 else {
583 int dir = 0;
584 snd_pcm_uframes_t psize = req->period_size;
586 err = snd_pcm_hw_params_set_period_size_near (
587 handle,
588 hw_params,
589 &psize,
590 &dir
592 obt = psize;
595 if (err < 0) {
596 alsa_logerr2 (err, typ, "Failed to set period %s to %d\n",
597 size_in_usec ? "time" : "size", req->period_size);
598 goto err;
601 if (((req->override_mask & 1) && (obt - req->period_size)))
602 dolog ("Requested period %s %u was rejected, using %lu\n",
603 size_in_usec ? "time" : "size", req->period_size, obt);
606 err = snd_pcm_hw_params (handle, hw_params);
607 if (err < 0) {
608 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
609 goto err;
612 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
613 if (err < 0) {
614 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
615 goto err;
618 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
619 if (err < 0) {
620 alsa_logerr2 (err, typ, "Failed to get format\n");
621 goto err;
624 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
625 dolog ("Invalid format was returned %d\n", obtfmt);
626 goto err;
629 err = snd_pcm_prepare (handle);
630 if (err < 0) {
631 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
632 goto err;
635 if (!in && conf->threshold) {
636 snd_pcm_uframes_t threshold;
637 int bytes_per_sec;
639 bytes_per_sec = freq << (nchannels == 2);
641 switch (obt->fmt) {
642 case AUD_FMT_S8:
643 case AUD_FMT_U8:
644 break;
646 case AUD_FMT_S16:
647 case AUD_FMT_U16:
648 bytes_per_sec <<= 1;
649 break;
651 case AUD_FMT_S32:
652 case AUD_FMT_U32:
653 bytes_per_sec <<= 2;
654 break;
657 threshold = (conf->threshold * bytes_per_sec) / 1000;
658 alsa_set_threshold (handle, threshold);
661 obt->nchannels = nchannels;
662 obt->freq = freq;
663 obt->samples = obt_buffer_size;
665 *handlep = handle;
667 if (obtfmt != req->fmt ||
668 obt->nchannels != req->nchannels ||
669 obt->freq != req->freq) {
670 dolog ("Audio parameters for %s\n", typ);
671 alsa_dump_info (req, obt, obtfmt);
674 #ifdef DEBUG
675 alsa_dump_info (req, obt, obtfmt);
676 #endif
677 return 0;
679 err:
680 alsa_anal_close1 (&handle);
681 return -1;
684 static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
686 snd_pcm_sframes_t avail;
688 avail = snd_pcm_avail_update (handle);
689 if (avail < 0) {
690 if (avail == -EPIPE) {
691 if (!alsa_recover (handle)) {
692 avail = snd_pcm_avail_update (handle);
696 if (avail < 0) {
697 alsa_logerr (avail,
698 "Could not obtain number of available frames\n");
699 return -1;
703 return avail;
706 static void alsa_write_pending (ALSAVoiceOut *alsa)
708 HWVoiceOut *hw = &alsa->hw;
710 while (alsa->pending) {
711 int left_till_end_samples = hw->samples - alsa->wpos;
712 int len = audio_MIN (alsa->pending, left_till_end_samples);
713 char *src = advance (alsa->pcm_buf, alsa->wpos << hw->info.shift);
715 while (len) {
716 snd_pcm_sframes_t written;
718 written = snd_pcm_writei (alsa->handle, src, len);
720 if (written <= 0) {
721 switch (written) {
722 case 0:
723 trace_alsa_wrote_zero(len);
724 return;
726 case -EPIPE:
727 if (alsa_recover (alsa->handle)) {
728 alsa_logerr (written, "Failed to write %d frames\n",
729 len);
730 return;
732 trace_alsa_xrun_out();
733 continue;
735 case -ESTRPIPE:
736 /* stream is suspended and waiting for an
737 application recovery */
738 if (alsa_resume (alsa->handle)) {
739 alsa_logerr (written, "Failed to write %d frames\n",
740 len);
741 return;
743 trace_alsa_resume_out();
744 continue;
746 case -EAGAIN:
747 return;
749 default:
750 alsa_logerr (written, "Failed to write %d frames from %p\n",
751 len, src);
752 return;
756 alsa->wpos = (alsa->wpos + written) % hw->samples;
757 alsa->pending -= written;
758 len -= written;
763 static int alsa_run_out (HWVoiceOut *hw, int live)
765 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
766 int decr;
767 snd_pcm_sframes_t avail;
769 avail = alsa_get_avail (alsa->handle);
770 if (avail < 0) {
771 dolog ("Could not get number of available playback frames\n");
772 return 0;
775 decr = audio_MIN (live, avail);
776 decr = audio_pcm_hw_clip_out (hw, alsa->pcm_buf, decr, alsa->pending);
777 alsa->pending += decr;
778 alsa_write_pending (alsa);
779 return decr;
782 static void alsa_fini_out (HWVoiceOut *hw)
784 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
786 ldebug ("alsa_fini\n");
787 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
789 g_free(alsa->pcm_buf);
790 alsa->pcm_buf = NULL;
793 static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
794 void *drv_opaque)
796 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
797 struct alsa_params_req req;
798 struct alsa_params_obt obt;
799 snd_pcm_t *handle;
800 struct audsettings obt_as;
801 ALSAConf *conf = drv_opaque;
803 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
804 req.freq = as->freq;
805 req.nchannels = as->nchannels;
806 req.period_size = conf->period_size_out;
807 req.buffer_size = conf->buffer_size_out;
808 req.size_in_usec = conf->size_in_usec_out;
809 req.override_mask =
810 (conf->period_size_out_overridden ? 1 : 0) |
811 (conf->buffer_size_out_overridden ? 2 : 0);
813 if (alsa_open (0, &req, &obt, &handle, conf)) {
814 return -1;
817 obt_as.freq = obt.freq;
818 obt_as.nchannels = obt.nchannels;
819 obt_as.fmt = obt.fmt;
820 obt_as.endianness = obt.endianness;
822 audio_pcm_init_info (&hw->info, &obt_as);
823 hw->samples = obt.samples;
825 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
826 if (!alsa->pcm_buf) {
827 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
828 hw->samples, 1 << hw->info.shift);
829 alsa_anal_close1 (&handle);
830 return -1;
833 alsa->handle = handle;
834 alsa->pollhlp.conf = conf;
835 return 0;
838 #define VOICE_CTL_PAUSE 0
839 #define VOICE_CTL_PREPARE 1
840 #define VOICE_CTL_START 2
842 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
844 int err;
846 if (ctl == VOICE_CTL_PAUSE) {
847 err = snd_pcm_drop (handle);
848 if (err < 0) {
849 alsa_logerr (err, "Could not stop %s\n", typ);
850 return -1;
853 else {
854 err = snd_pcm_prepare (handle);
855 if (err < 0) {
856 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
857 return -1;
859 if (ctl == VOICE_CTL_START) {
860 err = snd_pcm_start(handle);
861 if (err < 0) {
862 alsa_logerr (err, "Could not start handle for %s\n", typ);
863 return -1;
868 return 0;
871 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
873 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
875 switch (cmd) {
876 case VOICE_ENABLE:
878 va_list ap;
879 int poll_mode;
881 va_start (ap, cmd);
882 poll_mode = va_arg (ap, int);
883 va_end (ap);
885 ldebug ("enabling voice\n");
886 if (poll_mode && alsa_poll_out (hw)) {
887 poll_mode = 0;
889 hw->poll_mode = poll_mode;
890 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PREPARE);
893 case VOICE_DISABLE:
894 ldebug ("disabling voice\n");
895 if (hw->poll_mode) {
896 hw->poll_mode = 0;
897 alsa_fini_poll (&alsa->pollhlp);
899 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PAUSE);
902 return -1;
905 static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
907 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
908 struct alsa_params_req req;
909 struct alsa_params_obt obt;
910 snd_pcm_t *handle;
911 struct audsettings obt_as;
912 ALSAConf *conf = drv_opaque;
914 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
915 req.freq = as->freq;
916 req.nchannels = as->nchannels;
917 req.period_size = conf->period_size_in;
918 req.buffer_size = conf->buffer_size_in;
919 req.size_in_usec = conf->size_in_usec_in;
920 req.override_mask =
921 (conf->period_size_in_overridden ? 1 : 0) |
922 (conf->buffer_size_in_overridden ? 2 : 0);
924 if (alsa_open (1, &req, &obt, &handle, conf)) {
925 return -1;
928 obt_as.freq = obt.freq;
929 obt_as.nchannels = obt.nchannels;
930 obt_as.fmt = obt.fmt;
931 obt_as.endianness = obt.endianness;
933 audio_pcm_init_info (&hw->info, &obt_as);
934 hw->samples = obt.samples;
936 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
937 if (!alsa->pcm_buf) {
938 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
939 hw->samples, 1 << hw->info.shift);
940 alsa_anal_close1 (&handle);
941 return -1;
944 alsa->handle = handle;
945 alsa->pollhlp.conf = conf;
946 return 0;
949 static void alsa_fini_in (HWVoiceIn *hw)
951 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
953 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
955 g_free(alsa->pcm_buf);
956 alsa->pcm_buf = NULL;
959 static int alsa_run_in (HWVoiceIn *hw)
961 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
962 int hwshift = hw->info.shift;
963 int i;
964 int live = audio_pcm_hw_get_live_in (hw);
965 int dead = hw->samples - live;
966 int decr;
967 struct {
968 int add;
969 int len;
970 } bufs[2] = {
971 { .add = hw->wpos, .len = 0 },
972 { .add = 0, .len = 0 }
974 snd_pcm_sframes_t avail;
975 snd_pcm_uframes_t read_samples = 0;
977 if (!dead) {
978 return 0;
981 avail = alsa_get_avail (alsa->handle);
982 if (avail < 0) {
983 dolog ("Could not get number of captured frames\n");
984 return 0;
987 if (!avail) {
988 snd_pcm_state_t state;
990 state = snd_pcm_state (alsa->handle);
991 switch (state) {
992 case SND_PCM_STATE_PREPARED:
993 avail = hw->samples;
994 break;
995 case SND_PCM_STATE_SUSPENDED:
996 /* stream is suspended and waiting for an application recovery */
997 if (alsa_resume (alsa->handle)) {
998 dolog ("Failed to resume suspended input stream\n");
999 return 0;
1001 trace_alsa_resume_in();
1002 break;
1003 default:
1004 trace_alsa_no_frames(state);
1005 return 0;
1009 decr = audio_MIN (dead, avail);
1010 if (!decr) {
1011 return 0;
1014 if (hw->wpos + decr > hw->samples) {
1015 bufs[0].len = (hw->samples - hw->wpos);
1016 bufs[1].len = (decr - (hw->samples - hw->wpos));
1018 else {
1019 bufs[0].len = decr;
1022 for (i = 0; i < 2; ++i) {
1023 void *src;
1024 struct st_sample *dst;
1025 snd_pcm_sframes_t nread;
1026 snd_pcm_uframes_t len;
1028 len = bufs[i].len;
1030 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
1031 dst = hw->conv_buf + bufs[i].add;
1033 while (len) {
1034 nread = snd_pcm_readi (alsa->handle, src, len);
1036 if (nread <= 0) {
1037 switch (nread) {
1038 case 0:
1039 trace_alsa_read_zero(len);
1040 goto exit;
1042 case -EPIPE:
1043 if (alsa_recover (alsa->handle)) {
1044 alsa_logerr (nread, "Failed to read %ld frames\n", len);
1045 goto exit;
1047 trace_alsa_xrun_in();
1048 continue;
1050 case -EAGAIN:
1051 goto exit;
1053 default:
1054 alsa_logerr (
1055 nread,
1056 "Failed to read %ld frames from %p\n",
1057 len,
1060 goto exit;
1064 hw->conv (dst, src, nread);
1066 src = advance (src, nread << hwshift);
1067 dst += nread;
1069 read_samples += nread;
1070 len -= nread;
1074 exit:
1075 hw->wpos = (hw->wpos + read_samples) % hw->samples;
1076 return read_samples;
1079 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
1081 return audio_pcm_sw_read (sw, buf, size);
1084 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
1086 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
1088 switch (cmd) {
1089 case VOICE_ENABLE:
1091 va_list ap;
1092 int poll_mode;
1094 va_start (ap, cmd);
1095 poll_mode = va_arg (ap, int);
1096 va_end (ap);
1098 ldebug ("enabling voice\n");
1099 if (poll_mode && alsa_poll_in (hw)) {
1100 poll_mode = 0;
1102 hw->poll_mode = poll_mode;
1104 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_START);
1107 case VOICE_DISABLE:
1108 ldebug ("disabling voice\n");
1109 if (hw->poll_mode) {
1110 hw->poll_mode = 0;
1111 alsa_fini_poll (&alsa->pollhlp);
1113 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_PAUSE);
1116 return -1;
1119 static ALSAConf glob_conf = {
1120 .buffer_size_out = 4096,
1121 .period_size_out = 1024,
1122 .pcm_name_out = "default",
1123 .pcm_name_in = "default",
1126 static void *alsa_audio_init (void)
1128 ALSAConf *conf = g_malloc(sizeof(ALSAConf));
1129 *conf = glob_conf;
1130 return conf;
1133 static void alsa_audio_fini (void *opaque)
1135 g_free(opaque);
1138 static struct audio_option alsa_options[] = {
1140 .name = "DAC_SIZE_IN_USEC",
1141 .tag = AUD_OPT_BOOL,
1142 .valp = &glob_conf.size_in_usec_out,
1143 .descr = "DAC period/buffer size in microseconds (otherwise in frames)"
1146 .name = "DAC_PERIOD_SIZE",
1147 .tag = AUD_OPT_INT,
1148 .valp = &glob_conf.period_size_out,
1149 .descr = "DAC period size (0 to go with system default)",
1150 .overriddenp = &glob_conf.period_size_out_overridden
1153 .name = "DAC_BUFFER_SIZE",
1154 .tag = AUD_OPT_INT,
1155 .valp = &glob_conf.buffer_size_out,
1156 .descr = "DAC buffer size (0 to go with system default)",
1157 .overriddenp = &glob_conf.buffer_size_out_overridden
1160 .name = "ADC_SIZE_IN_USEC",
1161 .tag = AUD_OPT_BOOL,
1162 .valp = &glob_conf.size_in_usec_in,
1163 .descr =
1164 "ADC period/buffer size in microseconds (otherwise in frames)"
1167 .name = "ADC_PERIOD_SIZE",
1168 .tag = AUD_OPT_INT,
1169 .valp = &glob_conf.period_size_in,
1170 .descr = "ADC period size (0 to go with system default)",
1171 .overriddenp = &glob_conf.period_size_in_overridden
1174 .name = "ADC_BUFFER_SIZE",
1175 .tag = AUD_OPT_INT,
1176 .valp = &glob_conf.buffer_size_in,
1177 .descr = "ADC buffer size (0 to go with system default)",
1178 .overriddenp = &glob_conf.buffer_size_in_overridden
1181 .name = "THRESHOLD",
1182 .tag = AUD_OPT_INT,
1183 .valp = &glob_conf.threshold,
1184 .descr = "(undocumented)"
1187 .name = "DAC_DEV",
1188 .tag = AUD_OPT_STR,
1189 .valp = &glob_conf.pcm_name_out,
1190 .descr = "DAC device name (for instance dmix)"
1193 .name = "ADC_DEV",
1194 .tag = AUD_OPT_STR,
1195 .valp = &glob_conf.pcm_name_in,
1196 .descr = "ADC device name"
1198 { /* End of list */ }
1201 static struct audio_pcm_ops alsa_pcm_ops = {
1202 .init_out = alsa_init_out,
1203 .fini_out = alsa_fini_out,
1204 .run_out = alsa_run_out,
1205 .write = alsa_write,
1206 .ctl_out = alsa_ctl_out,
1208 .init_in = alsa_init_in,
1209 .fini_in = alsa_fini_in,
1210 .run_in = alsa_run_in,
1211 .read = alsa_read,
1212 .ctl_in = alsa_ctl_in,
1215 struct audio_driver alsa_audio_driver = {
1216 .name = "alsa",
1217 .descr = "ALSA http://www.alsa-project.org",
1218 .options = alsa_options,
1219 .init = alsa_audio_init,
1220 .fini = alsa_audio_fini,
1221 .pcm_ops = &alsa_pcm_ops,
1222 .can_be_default = 1,
1223 .max_voices_out = INT_MAX,
1224 .max_voices_in = INT_MAX,
1225 .voice_size_out = sizeof (ALSAVoiceOut),
1226 .voice_size_in = sizeof (ALSAVoiceIn)