]> git.proxmox.com Git - mirror_qemu.git/blob - audio/alsaaudio.c
janitor: do not rely on indirect inclusions of or from qemu-char.h
[mirror_qemu.git] / audio / alsaaudio.c
1 /*
2 * QEMU ALSA audio driver
3 *
4 * Copyright (c) 2005 Vassili Karpov (malc)
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24 #include <alsa/asoundlib.h>
25 #include "qemu-common.h"
26 #include "main-loop.h"
27 #include "qemu-char.h"
28 #include "audio.h"
29
30 #if QEMU_GNUC_PREREQ(4, 3)
31 #pragma GCC diagnostic ignored "-Waddress"
32 #endif
33
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
36
37 struct pollhlp {
38 snd_pcm_t *handle;
39 struct pollfd *pfds;
40 int count;
41 int mask;
42 };
43
44 typedef struct ALSAVoiceOut {
45 HWVoiceOut hw;
46 int wpos;
47 int pending;
48 void *pcm_buf;
49 snd_pcm_t *handle;
50 struct pollhlp pollhlp;
51 } ALSAVoiceOut;
52
53 typedef struct ALSAVoiceIn {
54 HWVoiceIn hw;
55 snd_pcm_t *handle;
56 void *pcm_buf;
57 struct pollhlp pollhlp;
58 } ALSAVoiceIn;
59
60 static struct {
61 int size_in_usec_in;
62 int size_in_usec_out;
63 const char *pcm_name_in;
64 const char *pcm_name_out;
65 unsigned int buffer_size_in;
66 unsigned int period_size_in;
67 unsigned int buffer_size_out;
68 unsigned int period_size_out;
69 unsigned int threshold;
70
71 int buffer_size_in_overridden;
72 int period_size_in_overridden;
73
74 int buffer_size_out_overridden;
75 int period_size_out_overridden;
76 int verbose;
77 } conf = {
78 .buffer_size_out = 4096,
79 .period_size_out = 1024,
80 .pcm_name_out = "default",
81 .pcm_name_in = "default",
82 };
83
84 struct alsa_params_req {
85 int freq;
86 snd_pcm_format_t fmt;
87 int nchannels;
88 int size_in_usec;
89 int override_mask;
90 unsigned int buffer_size;
91 unsigned int period_size;
92 };
93
94 struct alsa_params_obt {
95 int freq;
96 audfmt_e fmt;
97 int endianness;
98 int nchannels;
99 snd_pcm_uframes_t samples;
100 };
101
102 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
103 {
104 va_list ap;
105
106 va_start (ap, fmt);
107 AUD_vlog (AUDIO_CAP, fmt, ap);
108 va_end (ap);
109
110 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
111 }
112
113 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
114 int err,
115 const char *typ,
116 const char *fmt,
117 ...
118 )
119 {
120 va_list ap;
121
122 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
123
124 va_start (ap, fmt);
125 AUD_vlog (AUDIO_CAP, fmt, ap);
126 va_end (ap);
127
128 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
129 }
130
131 static void alsa_fini_poll (struct pollhlp *hlp)
132 {
133 int i;
134 struct pollfd *pfds = hlp->pfds;
135
136 if (pfds) {
137 for (i = 0; i < hlp->count; ++i) {
138 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
139 }
140 g_free (pfds);
141 }
142 hlp->pfds = NULL;
143 hlp->count = 0;
144 hlp->handle = NULL;
145 }
146
147 static void alsa_anal_close1 (snd_pcm_t **handlep)
148 {
149 int err = snd_pcm_close (*handlep);
150 if (err) {
151 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
152 }
153 *handlep = NULL;
154 }
155
156 static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
157 {
158 alsa_fini_poll (hlp);
159 alsa_anal_close1 (handlep);
160 }
161
162 static int alsa_recover (snd_pcm_t *handle)
163 {
164 int err = snd_pcm_prepare (handle);
165 if (err < 0) {
166 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
167 return -1;
168 }
169 return 0;
170 }
171
172 static int alsa_resume (snd_pcm_t *handle)
173 {
174 int err = snd_pcm_resume (handle);
175 if (err < 0) {
176 alsa_logerr (err, "Failed to resume handle %p\n", handle);
177 return -1;
178 }
179 return 0;
180 }
181
182 static void alsa_poll_handler (void *opaque)
183 {
184 int err, count;
185 snd_pcm_state_t state;
186 struct pollhlp *hlp = opaque;
187 unsigned short revents;
188
189 count = poll (hlp->pfds, hlp->count, 0);
190 if (count < 0) {
191 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
192 return;
193 }
194
195 if (!count) {
196 return;
197 }
198
199 /* XXX: ALSA example uses initial count, not the one returned by
200 poll, correct? */
201 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
202 hlp->count, &revents);
203 if (err < 0) {
204 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
205 return;
206 }
207
208 if (!(revents & hlp->mask)) {
209 if (conf.verbose) {
210 dolog ("revents = %d\n", revents);
211 }
212 return;
213 }
214
215 state = snd_pcm_state (hlp->handle);
216 switch (state) {
217 case SND_PCM_STATE_SETUP:
218 alsa_recover (hlp->handle);
219 break;
220
221 case SND_PCM_STATE_XRUN:
222 alsa_recover (hlp->handle);
223 break;
224
225 case SND_PCM_STATE_SUSPENDED:
226 alsa_resume (hlp->handle);
227 break;
228
229 case SND_PCM_STATE_PREPARED:
230 audio_run ("alsa run (prepared)");
231 break;
232
233 case SND_PCM_STATE_RUNNING:
234 audio_run ("alsa run (running)");
235 break;
236
237 default:
238 dolog ("Unexpected state %d\n", state);
239 }
240 }
241
242 static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
243 {
244 int i, count, err;
245 struct pollfd *pfds;
246
247 count = snd_pcm_poll_descriptors_count (handle);
248 if (count <= 0) {
249 dolog ("Could not initialize poll mode\n"
250 "Invalid number of poll descriptors %d\n", count);
251 return -1;
252 }
253
254 pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
255 if (!pfds) {
256 dolog ("Could not initialize poll mode\n");
257 return -1;
258 }
259
260 err = snd_pcm_poll_descriptors (handle, pfds, count);
261 if (err < 0) {
262 alsa_logerr (err, "Could not initialize poll mode\n"
263 "Could not obtain poll descriptors\n");
264 g_free (pfds);
265 return -1;
266 }
267
268 for (i = 0; i < count; ++i) {
269 if (pfds[i].events & POLLIN) {
270 err = qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler,
271 NULL, hlp);
272 }
273 if (pfds[i].events & POLLOUT) {
274 if (conf.verbose) {
275 dolog ("POLLOUT %d %d\n", i, pfds[i].fd);
276 }
277 err = qemu_set_fd_handler (pfds[i].fd, NULL,
278 alsa_poll_handler, hlp);
279 }
280 if (conf.verbose) {
281 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
282 pfds[i].events, i, pfds[i].fd, err);
283 }
284
285 if (err) {
286 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
287 pfds[i].events, i, pfds[i].fd, err);
288
289 while (i--) {
290 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
291 }
292 g_free (pfds);
293 return -1;
294 }
295 }
296 hlp->pfds = pfds;
297 hlp->count = count;
298 hlp->handle = handle;
299 hlp->mask = mask;
300 return 0;
301 }
302
303 static int alsa_poll_out (HWVoiceOut *hw)
304 {
305 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
306
307 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
308 }
309
310 static int alsa_poll_in (HWVoiceIn *hw)
311 {
312 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
313
314 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
315 }
316
317 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
318 {
319 return audio_pcm_sw_write (sw, buf, len);
320 }
321
322 static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt, int endianness)
323 {
324 switch (fmt) {
325 case AUD_FMT_S8:
326 return SND_PCM_FORMAT_S8;
327
328 case AUD_FMT_U8:
329 return SND_PCM_FORMAT_U8;
330
331 case AUD_FMT_S16:
332 if (endianness) {
333 return SND_PCM_FORMAT_S16_BE;
334 }
335 else {
336 return SND_PCM_FORMAT_S16_LE;
337 }
338
339 case AUD_FMT_U16:
340 if (endianness) {
341 return SND_PCM_FORMAT_U16_BE;
342 }
343 else {
344 return SND_PCM_FORMAT_U16_LE;
345 }
346
347 case AUD_FMT_S32:
348 if (endianness) {
349 return SND_PCM_FORMAT_S32_BE;
350 }
351 else {
352 return SND_PCM_FORMAT_S32_LE;
353 }
354
355 case AUD_FMT_U32:
356 if (endianness) {
357 return SND_PCM_FORMAT_U32_BE;
358 }
359 else {
360 return SND_PCM_FORMAT_U32_LE;
361 }
362
363 default:
364 dolog ("Internal logic error: Bad audio format %d\n", fmt);
365 #ifdef DEBUG_AUDIO
366 abort ();
367 #endif
368 return SND_PCM_FORMAT_U8;
369 }
370 }
371
372 static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
373 int *endianness)
374 {
375 switch (alsafmt) {
376 case SND_PCM_FORMAT_S8:
377 *endianness = 0;
378 *fmt = AUD_FMT_S8;
379 break;
380
381 case SND_PCM_FORMAT_U8:
382 *endianness = 0;
383 *fmt = AUD_FMT_U8;
384 break;
385
386 case SND_PCM_FORMAT_S16_LE:
387 *endianness = 0;
388 *fmt = AUD_FMT_S16;
389 break;
390
391 case SND_PCM_FORMAT_U16_LE:
392 *endianness = 0;
393 *fmt = AUD_FMT_U16;
394 break;
395
396 case SND_PCM_FORMAT_S16_BE:
397 *endianness = 1;
398 *fmt = AUD_FMT_S16;
399 break;
400
401 case SND_PCM_FORMAT_U16_BE:
402 *endianness = 1;
403 *fmt = AUD_FMT_U16;
404 break;
405
406 case SND_PCM_FORMAT_S32_LE:
407 *endianness = 0;
408 *fmt = AUD_FMT_S32;
409 break;
410
411 case SND_PCM_FORMAT_U32_LE:
412 *endianness = 0;
413 *fmt = AUD_FMT_U32;
414 break;
415
416 case SND_PCM_FORMAT_S32_BE:
417 *endianness = 1;
418 *fmt = AUD_FMT_S32;
419 break;
420
421 case SND_PCM_FORMAT_U32_BE:
422 *endianness = 1;
423 *fmt = AUD_FMT_U32;
424 break;
425
426 default:
427 dolog ("Unrecognized audio format %d\n", alsafmt);
428 return -1;
429 }
430
431 return 0;
432 }
433
434 static void alsa_dump_info (struct alsa_params_req *req,
435 struct alsa_params_obt *obt,
436 snd_pcm_format_t obtfmt)
437 {
438 dolog ("parameter | requested value | obtained value\n");
439 dolog ("format | %10d | %10d\n", req->fmt, obtfmt);
440 dolog ("channels | %10d | %10d\n",
441 req->nchannels, obt->nchannels);
442 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
443 dolog ("============================================\n");
444 dolog ("requested: buffer size %d period size %d\n",
445 req->buffer_size, req->period_size);
446 dolog ("obtained: samples %ld\n", obt->samples);
447 }
448
449 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
450 {
451 int err;
452 snd_pcm_sw_params_t *sw_params;
453
454 snd_pcm_sw_params_alloca (&sw_params);
455
456 err = snd_pcm_sw_params_current (handle, sw_params);
457 if (err < 0) {
458 dolog ("Could not fully initialize DAC\n");
459 alsa_logerr (err, "Failed to get current software parameters\n");
460 return;
461 }
462
463 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
464 if (err < 0) {
465 dolog ("Could not fully initialize DAC\n");
466 alsa_logerr (err, "Failed to set software threshold to %ld\n",
467 threshold);
468 return;
469 }
470
471 err = snd_pcm_sw_params (handle, sw_params);
472 if (err < 0) {
473 dolog ("Could not fully initialize DAC\n");
474 alsa_logerr (err, "Failed to set software parameters\n");
475 return;
476 }
477 }
478
479 static int alsa_open (int in, struct alsa_params_req *req,
480 struct alsa_params_obt *obt, snd_pcm_t **handlep)
481 {
482 snd_pcm_t *handle;
483 snd_pcm_hw_params_t *hw_params;
484 int err;
485 int size_in_usec;
486 unsigned int freq, nchannels;
487 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
488 snd_pcm_uframes_t obt_buffer_size;
489 const char *typ = in ? "ADC" : "DAC";
490 snd_pcm_format_t obtfmt;
491
492 freq = req->freq;
493 nchannels = req->nchannels;
494 size_in_usec = req->size_in_usec;
495
496 snd_pcm_hw_params_alloca (&hw_params);
497
498 err = snd_pcm_open (
499 &handle,
500 pcm_name,
501 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
502 SND_PCM_NONBLOCK
503 );
504 if (err < 0) {
505 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
506 return -1;
507 }
508
509 err = snd_pcm_hw_params_any (handle, hw_params);
510 if (err < 0) {
511 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
512 goto err;
513 }
514
515 err = snd_pcm_hw_params_set_access (
516 handle,
517 hw_params,
518 SND_PCM_ACCESS_RW_INTERLEAVED
519 );
520 if (err < 0) {
521 alsa_logerr2 (err, typ, "Failed to set access type\n");
522 goto err;
523 }
524
525 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
526 if (err < 0 && conf.verbose) {
527 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
528 }
529
530 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
531 if (err < 0) {
532 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
533 goto err;
534 }
535
536 err = snd_pcm_hw_params_set_channels_near (
537 handle,
538 hw_params,
539 &nchannels
540 );
541 if (err < 0) {
542 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
543 req->nchannels);
544 goto err;
545 }
546
547 if (nchannels != 1 && nchannels != 2) {
548 alsa_logerr2 (err, typ,
549 "Can not handle obtained number of channels %d\n",
550 nchannels);
551 goto err;
552 }
553
554 if (req->buffer_size) {
555 unsigned long obt;
556
557 if (size_in_usec) {
558 int dir = 0;
559 unsigned int btime = req->buffer_size;
560
561 err = snd_pcm_hw_params_set_buffer_time_near (
562 handle,
563 hw_params,
564 &btime,
565 &dir
566 );
567 obt = btime;
568 }
569 else {
570 snd_pcm_uframes_t bsize = req->buffer_size;
571
572 err = snd_pcm_hw_params_set_buffer_size_near (
573 handle,
574 hw_params,
575 &bsize
576 );
577 obt = bsize;
578 }
579 if (err < 0) {
580 alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n",
581 size_in_usec ? "time" : "size", req->buffer_size);
582 goto err;
583 }
584
585 if ((req->override_mask & 2) && (obt - req->buffer_size))
586 dolog ("Requested buffer %s %u was rejected, using %lu\n",
587 size_in_usec ? "time" : "size", req->buffer_size, obt);
588 }
589
590 if (req->period_size) {
591 unsigned long obt;
592
593 if (size_in_usec) {
594 int dir = 0;
595 unsigned int ptime = req->period_size;
596
597 err = snd_pcm_hw_params_set_period_time_near (
598 handle,
599 hw_params,
600 &ptime,
601 &dir
602 );
603 obt = ptime;
604 }
605 else {
606 int dir = 0;
607 snd_pcm_uframes_t psize = req->period_size;
608
609 err = snd_pcm_hw_params_set_period_size_near (
610 handle,
611 hw_params,
612 &psize,
613 &dir
614 );
615 obt = psize;
616 }
617
618 if (err < 0) {
619 alsa_logerr2 (err, typ, "Failed to set period %s to %d\n",
620 size_in_usec ? "time" : "size", req->period_size);
621 goto err;
622 }
623
624 if (((req->override_mask & 1) && (obt - req->period_size)))
625 dolog ("Requested period %s %u was rejected, using %lu\n",
626 size_in_usec ? "time" : "size", req->period_size, obt);
627 }
628
629 err = snd_pcm_hw_params (handle, hw_params);
630 if (err < 0) {
631 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
632 goto err;
633 }
634
635 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
636 if (err < 0) {
637 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
638 goto err;
639 }
640
641 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
642 if (err < 0) {
643 alsa_logerr2 (err, typ, "Failed to get format\n");
644 goto err;
645 }
646
647 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
648 dolog ("Invalid format was returned %d\n", obtfmt);
649 goto err;
650 }
651
652 err = snd_pcm_prepare (handle);
653 if (err < 0) {
654 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
655 goto err;
656 }
657
658 if (!in && conf.threshold) {
659 snd_pcm_uframes_t threshold;
660 int bytes_per_sec;
661
662 bytes_per_sec = freq << (nchannels == 2);
663
664 switch (obt->fmt) {
665 case AUD_FMT_S8:
666 case AUD_FMT_U8:
667 break;
668
669 case AUD_FMT_S16:
670 case AUD_FMT_U16:
671 bytes_per_sec <<= 1;
672 break;
673
674 case AUD_FMT_S32:
675 case AUD_FMT_U32:
676 bytes_per_sec <<= 2;
677 break;
678 }
679
680 threshold = (conf.threshold * bytes_per_sec) / 1000;
681 alsa_set_threshold (handle, threshold);
682 }
683
684 obt->nchannels = nchannels;
685 obt->freq = freq;
686 obt->samples = obt_buffer_size;
687
688 *handlep = handle;
689
690 if (conf.verbose &&
691 (obtfmt != req->fmt ||
692 obt->nchannels != req->nchannels ||
693 obt->freq != req->freq)) {
694 dolog ("Audio parameters for %s\n", typ);
695 alsa_dump_info (req, obt, obtfmt);
696 }
697
698 #ifdef DEBUG
699 alsa_dump_info (req, obt, obtfmt);
700 #endif
701 return 0;
702
703 err:
704 alsa_anal_close1 (&handle);
705 return -1;
706 }
707
708 static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
709 {
710 snd_pcm_sframes_t avail;
711
712 avail = snd_pcm_avail_update (handle);
713 if (avail < 0) {
714 if (avail == -EPIPE) {
715 if (!alsa_recover (handle)) {
716 avail = snd_pcm_avail_update (handle);
717 }
718 }
719
720 if (avail < 0) {
721 alsa_logerr (avail,
722 "Could not obtain number of available frames\n");
723 return -1;
724 }
725 }
726
727 return avail;
728 }
729
730 static void alsa_write_pending (ALSAVoiceOut *alsa)
731 {
732 HWVoiceOut *hw = &alsa->hw;
733
734 while (alsa->pending) {
735 int left_till_end_samples = hw->samples - alsa->wpos;
736 int len = audio_MIN (alsa->pending, left_till_end_samples);
737 char *src = advance (alsa->pcm_buf, alsa->wpos << hw->info.shift);
738
739 while (len) {
740 snd_pcm_sframes_t written;
741
742 written = snd_pcm_writei (alsa->handle, src, len);
743
744 if (written <= 0) {
745 switch (written) {
746 case 0:
747 if (conf.verbose) {
748 dolog ("Failed to write %d frames (wrote zero)\n", len);
749 }
750 return;
751
752 case -EPIPE:
753 if (alsa_recover (alsa->handle)) {
754 alsa_logerr (written, "Failed to write %d frames\n",
755 len);
756 return;
757 }
758 if (conf.verbose) {
759 dolog ("Recovering from playback xrun\n");
760 }
761 continue;
762
763 case -ESTRPIPE:
764 /* stream is suspended and waiting for an
765 application recovery */
766 if (alsa_resume (alsa->handle)) {
767 alsa_logerr (written, "Failed to write %d frames\n",
768 len);
769 return;
770 }
771 if (conf.verbose) {
772 dolog ("Resuming suspended output stream\n");
773 }
774 continue;
775
776 case -EAGAIN:
777 return;
778
779 default:
780 alsa_logerr (written, "Failed to write %d frames from %p\n",
781 len, src);
782 return;
783 }
784 }
785
786 alsa->wpos = (alsa->wpos + written) % hw->samples;
787 alsa->pending -= written;
788 len -= written;
789 }
790 }
791 }
792
793 static int alsa_run_out (HWVoiceOut *hw, int live)
794 {
795 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
796 int decr;
797 snd_pcm_sframes_t avail;
798
799 avail = alsa_get_avail (alsa->handle);
800 if (avail < 0) {
801 dolog ("Could not get number of available playback frames\n");
802 return 0;
803 }
804
805 decr = audio_MIN (live, avail);
806 decr = audio_pcm_hw_clip_out (hw, alsa->pcm_buf, decr, alsa->pending);
807 alsa->pending += decr;
808 alsa_write_pending (alsa);
809 return decr;
810 }
811
812 static void alsa_fini_out (HWVoiceOut *hw)
813 {
814 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
815
816 ldebug ("alsa_fini\n");
817 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
818
819 if (alsa->pcm_buf) {
820 g_free (alsa->pcm_buf);
821 alsa->pcm_buf = NULL;
822 }
823 }
824
825 static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
826 {
827 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
828 struct alsa_params_req req;
829 struct alsa_params_obt obt;
830 snd_pcm_t *handle;
831 struct audsettings obt_as;
832
833 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
834 req.freq = as->freq;
835 req.nchannels = as->nchannels;
836 req.period_size = conf.period_size_out;
837 req.buffer_size = conf.buffer_size_out;
838 req.size_in_usec = conf.size_in_usec_out;
839 req.override_mask =
840 (conf.period_size_out_overridden ? 1 : 0) |
841 (conf.buffer_size_out_overridden ? 2 : 0);
842
843 if (alsa_open (0, &req, &obt, &handle)) {
844 return -1;
845 }
846
847 obt_as.freq = obt.freq;
848 obt_as.nchannels = obt.nchannels;
849 obt_as.fmt = obt.fmt;
850 obt_as.endianness = obt.endianness;
851
852 audio_pcm_init_info (&hw->info, &obt_as);
853 hw->samples = obt.samples;
854
855 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
856 if (!alsa->pcm_buf) {
857 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
858 hw->samples, 1 << hw->info.shift);
859 alsa_anal_close1 (&handle);
860 return -1;
861 }
862
863 alsa->handle = handle;
864 return 0;
865 }
866
867 #define VOICE_CTL_PAUSE 0
868 #define VOICE_CTL_PREPARE 1
869 #define VOICE_CTL_START 2
870
871 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
872 {
873 int err;
874
875 if (ctl == VOICE_CTL_PAUSE) {
876 err = snd_pcm_drop (handle);
877 if (err < 0) {
878 alsa_logerr (err, "Could not stop %s\n", typ);
879 return -1;
880 }
881 }
882 else {
883 err = snd_pcm_prepare (handle);
884 if (err < 0) {
885 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
886 return -1;
887 }
888 if (ctl == VOICE_CTL_START) {
889 err = snd_pcm_start(handle);
890 if (err < 0) {
891 alsa_logerr (err, "Could not start handle for %s\n", typ);
892 return -1;
893 }
894 }
895 }
896
897 return 0;
898 }
899
900 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
901 {
902 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
903
904 switch (cmd) {
905 case VOICE_ENABLE:
906 {
907 va_list ap;
908 int poll_mode;
909
910 va_start (ap, cmd);
911 poll_mode = va_arg (ap, int);
912 va_end (ap);
913
914 ldebug ("enabling voice\n");
915 if (poll_mode && alsa_poll_out (hw)) {
916 poll_mode = 0;
917 }
918 hw->poll_mode = poll_mode;
919 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PREPARE);
920 }
921
922 case VOICE_DISABLE:
923 ldebug ("disabling voice\n");
924 if (hw->poll_mode) {
925 hw->poll_mode = 0;
926 alsa_fini_poll (&alsa->pollhlp);
927 }
928 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PAUSE);
929 }
930
931 return -1;
932 }
933
934 static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
935 {
936 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
937 struct alsa_params_req req;
938 struct alsa_params_obt obt;
939 snd_pcm_t *handle;
940 struct audsettings obt_as;
941
942 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
943 req.freq = as->freq;
944 req.nchannels = as->nchannels;
945 req.period_size = conf.period_size_in;
946 req.buffer_size = conf.buffer_size_in;
947 req.size_in_usec = conf.size_in_usec_in;
948 req.override_mask =
949 (conf.period_size_in_overridden ? 1 : 0) |
950 (conf.buffer_size_in_overridden ? 2 : 0);
951
952 if (alsa_open (1, &req, &obt, &handle)) {
953 return -1;
954 }
955
956 obt_as.freq = obt.freq;
957 obt_as.nchannels = obt.nchannels;
958 obt_as.fmt = obt.fmt;
959 obt_as.endianness = obt.endianness;
960
961 audio_pcm_init_info (&hw->info, &obt_as);
962 hw->samples = obt.samples;
963
964 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
965 if (!alsa->pcm_buf) {
966 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
967 hw->samples, 1 << hw->info.shift);
968 alsa_anal_close1 (&handle);
969 return -1;
970 }
971
972 alsa->handle = handle;
973 return 0;
974 }
975
976 static void alsa_fini_in (HWVoiceIn *hw)
977 {
978 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
979
980 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
981
982 if (alsa->pcm_buf) {
983 g_free (alsa->pcm_buf);
984 alsa->pcm_buf = NULL;
985 }
986 }
987
988 static int alsa_run_in (HWVoiceIn *hw)
989 {
990 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
991 int hwshift = hw->info.shift;
992 int i;
993 int live = audio_pcm_hw_get_live_in (hw);
994 int dead = hw->samples - live;
995 int decr;
996 struct {
997 int add;
998 int len;
999 } bufs[2] = {
1000 { .add = hw->wpos, .len = 0 },
1001 { .add = 0, .len = 0 }
1002 };
1003 snd_pcm_sframes_t avail;
1004 snd_pcm_uframes_t read_samples = 0;
1005
1006 if (!dead) {
1007 return 0;
1008 }
1009
1010 avail = alsa_get_avail (alsa->handle);
1011 if (avail < 0) {
1012 dolog ("Could not get number of captured frames\n");
1013 return 0;
1014 }
1015
1016 if (!avail) {
1017 snd_pcm_state_t state;
1018
1019 state = snd_pcm_state (alsa->handle);
1020 switch (state) {
1021 case SND_PCM_STATE_PREPARED:
1022 avail = hw->samples;
1023 break;
1024 case SND_PCM_STATE_SUSPENDED:
1025 /* stream is suspended and waiting for an application recovery */
1026 if (alsa_resume (alsa->handle)) {
1027 dolog ("Failed to resume suspended input stream\n");
1028 return 0;
1029 }
1030 if (conf.verbose) {
1031 dolog ("Resuming suspended input stream\n");
1032 }
1033 break;
1034 default:
1035 if (conf.verbose) {
1036 dolog ("No frames available and ALSA state is %d\n", state);
1037 }
1038 return 0;
1039 }
1040 }
1041
1042 decr = audio_MIN (dead, avail);
1043 if (!decr) {
1044 return 0;
1045 }
1046
1047 if (hw->wpos + decr > hw->samples) {
1048 bufs[0].len = (hw->samples - hw->wpos);
1049 bufs[1].len = (decr - (hw->samples - hw->wpos));
1050 }
1051 else {
1052 bufs[0].len = decr;
1053 }
1054
1055 for (i = 0; i < 2; ++i) {
1056 void *src;
1057 struct st_sample *dst;
1058 snd_pcm_sframes_t nread;
1059 snd_pcm_uframes_t len;
1060
1061 len = bufs[i].len;
1062
1063 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
1064 dst = hw->conv_buf + bufs[i].add;
1065
1066 while (len) {
1067 nread = snd_pcm_readi (alsa->handle, src, len);
1068
1069 if (nread <= 0) {
1070 switch (nread) {
1071 case 0:
1072 if (conf.verbose) {
1073 dolog ("Failed to read %ld frames (read zero)\n", len);
1074 }
1075 goto exit;
1076
1077 case -EPIPE:
1078 if (alsa_recover (alsa->handle)) {
1079 alsa_logerr (nread, "Failed to read %ld frames\n", len);
1080 goto exit;
1081 }
1082 if (conf.verbose) {
1083 dolog ("Recovering from capture xrun\n");
1084 }
1085 continue;
1086
1087 case -EAGAIN:
1088 goto exit;
1089
1090 default:
1091 alsa_logerr (
1092 nread,
1093 "Failed to read %ld frames from %p\n",
1094 len,
1095 src
1096 );
1097 goto exit;
1098 }
1099 }
1100
1101 hw->conv (dst, src, nread);
1102
1103 src = advance (src, nread << hwshift);
1104 dst += nread;
1105
1106 read_samples += nread;
1107 len -= nread;
1108 }
1109 }
1110
1111 exit:
1112 hw->wpos = (hw->wpos + read_samples) % hw->samples;
1113 return read_samples;
1114 }
1115
1116 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
1117 {
1118 return audio_pcm_sw_read (sw, buf, size);
1119 }
1120
1121 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
1122 {
1123 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
1124
1125 switch (cmd) {
1126 case VOICE_ENABLE:
1127 {
1128 va_list ap;
1129 int poll_mode;
1130
1131 va_start (ap, cmd);
1132 poll_mode = va_arg (ap, int);
1133 va_end (ap);
1134
1135 ldebug ("enabling voice\n");
1136 if (poll_mode && alsa_poll_in (hw)) {
1137 poll_mode = 0;
1138 }
1139 hw->poll_mode = poll_mode;
1140
1141 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_START);
1142 }
1143
1144 case VOICE_DISABLE:
1145 ldebug ("disabling voice\n");
1146 if (hw->poll_mode) {
1147 hw->poll_mode = 0;
1148 alsa_fini_poll (&alsa->pollhlp);
1149 }
1150 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_PAUSE);
1151 }
1152
1153 return -1;
1154 }
1155
1156 static void *alsa_audio_init (void)
1157 {
1158 return &conf;
1159 }
1160
1161 static void alsa_audio_fini (void *opaque)
1162 {
1163 (void) opaque;
1164 }
1165
1166 static struct audio_option alsa_options[] = {
1167 {
1168 .name = "DAC_SIZE_IN_USEC",
1169 .tag = AUD_OPT_BOOL,
1170 .valp = &conf.size_in_usec_out,
1171 .descr = "DAC period/buffer size in microseconds (otherwise in frames)"
1172 },
1173 {
1174 .name = "DAC_PERIOD_SIZE",
1175 .tag = AUD_OPT_INT,
1176 .valp = &conf.period_size_out,
1177 .descr = "DAC period size (0 to go with system default)",
1178 .overriddenp = &conf.period_size_out_overridden
1179 },
1180 {
1181 .name = "DAC_BUFFER_SIZE",
1182 .tag = AUD_OPT_INT,
1183 .valp = &conf.buffer_size_out,
1184 .descr = "DAC buffer size (0 to go with system default)",
1185 .overriddenp = &conf.buffer_size_out_overridden
1186 },
1187 {
1188 .name = "ADC_SIZE_IN_USEC",
1189 .tag = AUD_OPT_BOOL,
1190 .valp = &conf.size_in_usec_in,
1191 .descr =
1192 "ADC period/buffer size in microseconds (otherwise in frames)"
1193 },
1194 {
1195 .name = "ADC_PERIOD_SIZE",
1196 .tag = AUD_OPT_INT,
1197 .valp = &conf.period_size_in,
1198 .descr = "ADC period size (0 to go with system default)",
1199 .overriddenp = &conf.period_size_in_overridden
1200 },
1201 {
1202 .name = "ADC_BUFFER_SIZE",
1203 .tag = AUD_OPT_INT,
1204 .valp = &conf.buffer_size_in,
1205 .descr = "ADC buffer size (0 to go with system default)",
1206 .overriddenp = &conf.buffer_size_in_overridden
1207 },
1208 {
1209 .name = "THRESHOLD",
1210 .tag = AUD_OPT_INT,
1211 .valp = &conf.threshold,
1212 .descr = "(undocumented)"
1213 },
1214 {
1215 .name = "DAC_DEV",
1216 .tag = AUD_OPT_STR,
1217 .valp = &conf.pcm_name_out,
1218 .descr = "DAC device name (for instance dmix)"
1219 },
1220 {
1221 .name = "ADC_DEV",
1222 .tag = AUD_OPT_STR,
1223 .valp = &conf.pcm_name_in,
1224 .descr = "ADC device name"
1225 },
1226 {
1227 .name = "VERBOSE",
1228 .tag = AUD_OPT_BOOL,
1229 .valp = &conf.verbose,
1230 .descr = "Behave in a more verbose way"
1231 },
1232 { /* End of list */ }
1233 };
1234
1235 static struct audio_pcm_ops alsa_pcm_ops = {
1236 .init_out = alsa_init_out,
1237 .fini_out = alsa_fini_out,
1238 .run_out = alsa_run_out,
1239 .write = alsa_write,
1240 .ctl_out = alsa_ctl_out,
1241
1242 .init_in = alsa_init_in,
1243 .fini_in = alsa_fini_in,
1244 .run_in = alsa_run_in,
1245 .read = alsa_read,
1246 .ctl_in = alsa_ctl_in,
1247 };
1248
1249 struct audio_driver alsa_audio_driver = {
1250 .name = "alsa",
1251 .descr = "ALSA http://www.alsa-project.org",
1252 .options = alsa_options,
1253 .init = alsa_audio_init,
1254 .fini = alsa_audio_fini,
1255 .pcm_ops = &alsa_pcm_ops,
1256 .can_be_default = 1,
1257 .max_voices_out = INT_MAX,
1258 .max_voices_in = INT_MAX,
1259 .voice_size_out = sizeof (ALSAVoiceOut),
1260 .voice_size_in = sizeof (ALSAVoiceIn)
1261 };