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