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