blob: 74ead97d87a0b736e92b0a5ffe10b4165c955ab5 [file] [log] [blame]
bellard1d14ffa2005-10-30 18:58:22 +00001/*
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>
pbrook749bc4b2007-11-17 17:35:54 +000025#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/main-loop.h"
pbrook749bc4b2007-11-17 17:35:54 +000027#include "audio.h"
bellard1d14ffa2005-10-30 18:58:22 +000028
malc26378722009-07-17 05:57:04 +040029#if QEMU_GNUC_PREREQ(4, 3)
30#pragma GCC diagnostic ignored "-Waddress"
31#endif
32
bellard1d14ffa2005-10-30 18:58:22 +000033#define AUDIO_CAP "alsa"
34#include "audio_int.h"
35
malc8b438ba2009-09-12 02:29:25 +040036struct pollhlp {
37 snd_pcm_t *handle;
38 struct pollfd *pfds;
39 int count;
malcb4f763b2009-09-14 03:53:25 +040040 int mask;
malc8b438ba2009-09-12 02:29:25 +040041};
42
bellard1d14ffa2005-10-30 18:58:22 +000043typedef struct ALSAVoiceOut {
44 HWVoiceOut hw;
malc541ba4e2009-09-18 10:59:50 +040045 int wpos;
46 int pending;
bellard1d14ffa2005-10-30 18:58:22 +000047 void *pcm_buf;
48 snd_pcm_t *handle;
malc8b438ba2009-09-12 02:29:25 +040049 struct pollhlp pollhlp;
bellard1d14ffa2005-10-30 18:58:22 +000050} ALSAVoiceOut;
51
52typedef struct ALSAVoiceIn {
53 HWVoiceIn hw;
54 snd_pcm_t *handle;
55 void *pcm_buf;
malc8b438ba2009-09-12 02:29:25 +040056 struct pollhlp pollhlp;
bellard1d14ffa2005-10-30 18:58:22 +000057} ALSAVoiceIn;
58
59static struct {
60 int size_in_usec_in;
61 int size_in_usec_out;
62 const char *pcm_name_in;
63 const char *pcm_name_out;
64 unsigned int buffer_size_in;
65 unsigned int period_size_in;
66 unsigned int buffer_size_out;
67 unsigned int period_size_out;
68 unsigned int threshold;
69
thsfe8f0962007-07-12 10:59:21 +000070 int buffer_size_in_overridden;
71 int period_size_in_overridden;
bellard1d14ffa2005-10-30 18:58:22 +000072
thsfe8f0962007-07-12 10:59:21 +000073 int buffer_size_out_overridden;
74 int period_size_out_overridden;
bellard571ec3d2005-11-20 16:24:34 +000075 int verbose;
bellard1d14ffa2005-10-30 18:58:22 +000076} conf = {
malcde2ca4f2009-10-02 03:19:30 +040077 .buffer_size_out = 4096,
78 .period_size_out = 1024,
bellard8ead62c2006-07-04 16:51:32 +000079 .pcm_name_out = "default",
80 .pcm_name_in = "default",
bellard1d14ffa2005-10-30 18:58:22 +000081};
82
83struct alsa_params_req {
balrogca9cc282008-01-14 04:24:29 +000084 int freq;
85 snd_pcm_format_t fmt;
86 int nchannels;
malc7a24c802008-06-21 17:14:57 +000087 int size_in_usec;
malc64333892008-07-09 19:25:28 +000088 int override_mask;
bellard1d14ffa2005-10-30 18:58:22 +000089 unsigned int buffer_size;
90 unsigned int period_size;
91};
92
93struct alsa_params_obt {
94 int freq;
95 audfmt_e fmt;
balrogca9cc282008-01-14 04:24:29 +000096 int endianness;
bellard1d14ffa2005-10-30 18:58:22 +000097 int nchannels;
bellardc0fe3822005-11-05 18:55:28 +000098 snd_pcm_uframes_t samples;
bellard1d14ffa2005-10-30 18:58:22 +000099};
100
101static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
102{
103 va_list ap;
104
105 va_start (ap, fmt);
106 AUD_vlog (AUDIO_CAP, fmt, ap);
107 va_end (ap);
108
109 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
110}
111
112static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
113 int err,
114 const char *typ,
115 const char *fmt,
116 ...
117 )
118{
119 va_list ap;
120
bellardc0fe3822005-11-05 18:55:28 +0000121 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
bellard1d14ffa2005-10-30 18:58:22 +0000122
123 va_start (ap, fmt);
124 AUD_vlog (AUDIO_CAP, fmt, ap);
125 va_end (ap);
126
127 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
128}
129
malc6ebfda12009-09-14 03:51:48 +0400130static void alsa_fini_poll (struct pollhlp *hlp)
131{
132 int i;
133 struct pollfd *pfds = hlp->pfds;
134
135 if (pfds) {
136 for (i = 0; i < hlp->count; ++i) {
137 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
138 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500139 g_free (pfds);
malc6ebfda12009-09-14 03:51:48 +0400140 }
141 hlp->pfds = NULL;
142 hlp->count = 0;
143 hlp->handle = NULL;
144}
145
146static void alsa_anal_close1 (snd_pcm_t **handlep)
bellard1d14ffa2005-10-30 18:58:22 +0000147{
148 int err = snd_pcm_close (*handlep);
149 if (err) {
150 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
151 }
152 *handlep = NULL;
153}
154
malc6ebfda12009-09-14 03:51:48 +0400155static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
156{
157 alsa_fini_poll (hlp);
158 alsa_anal_close1 (handlep);
159}
160
malc8b438ba2009-09-12 02:29:25 +0400161static int alsa_recover (snd_pcm_t *handle)
162{
163 int err = snd_pcm_prepare (handle);
164 if (err < 0) {
165 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
166 return -1;
167 }
168 return 0;
169}
170
171static int alsa_resume (snd_pcm_t *handle)
172{
173 int err = snd_pcm_resume (handle);
174 if (err < 0) {
175 alsa_logerr (err, "Failed to resume handle %p\n", handle);
176 return -1;
177 }
178 return 0;
179}
180
181static void alsa_poll_handler (void *opaque)
182{
183 int err, count;
184 snd_pcm_state_t state;
185 struct pollhlp *hlp = opaque;
186 unsigned short revents;
187
188 count = poll (hlp->pfds, hlp->count, 0);
189 if (count < 0) {
190 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
191 return;
192 }
193
194 if (!count) {
195 return;
196 }
197
198 /* XXX: ALSA example uses initial count, not the one returned by
199 poll, correct? */
200 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
201 hlp->count, &revents);
202 if (err < 0) {
203 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
204 return;
205 }
206
malcb4f763b2009-09-14 03:53:25 +0400207 if (!(revents & hlp->mask)) {
malc8b438ba2009-09-12 02:29:25 +0400208 if (conf.verbose) {
209 dolog ("revents = %d\n", revents);
210 }
211 return;
212 }
213
214 state = snd_pcm_state (hlp->handle);
215 switch (state) {
malcd9812b02010-02-28 18:34:21 +0300216 case SND_PCM_STATE_SETUP:
217 alsa_recover (hlp->handle);
218 break;
219
malc8b438ba2009-09-12 02:29:25 +0400220 case SND_PCM_STATE_XRUN:
221 alsa_recover (hlp->handle);
222 break;
223
224 case SND_PCM_STATE_SUSPENDED:
225 alsa_resume (hlp->handle);
226 break;
227
228 case SND_PCM_STATE_PREPARED:
229 audio_run ("alsa run (prepared)");
230 break;
231
232 case SND_PCM_STATE_RUNNING:
233 audio_run ("alsa run (running)");
234 break;
235
236 default:
237 dolog ("Unexpected state %d\n", state);
238 }
239}
240
malcb4f763b2009-09-14 03:53:25 +0400241static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
malc8b438ba2009-09-12 02:29:25 +0400242{
243 int i, count, err;
244 struct pollfd *pfds;
245
246 count = snd_pcm_poll_descriptors_count (handle);
247 if (count <= 0) {
248 dolog ("Could not initialize poll mode\n"
249 "Invalid number of poll descriptors %d\n", count);
250 return -1;
251 }
252
253 pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
254 if (!pfds) {
255 dolog ("Could not initialize poll mode\n");
256 return -1;
257 }
258
259 err = snd_pcm_poll_descriptors (handle, pfds, count);
260 if (err < 0) {
261 alsa_logerr (err, "Could not initialize poll mode\n"
262 "Could not obtain poll descriptors\n");
Anthony Liguori7267c092011-08-20 22:09:37 -0500263 g_free (pfds);
malc8b438ba2009-09-12 02:29:25 +0400264 return -1;
265 }
266
267 for (i = 0; i < count; ++i) {
268 if (pfds[i].events & POLLIN) {
269 err = qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler,
270 NULL, hlp);
271 }
272 if (pfds[i].events & POLLOUT) {
273 if (conf.verbose) {
274 dolog ("POLLOUT %d %d\n", i, pfds[i].fd);
275 }
276 err = qemu_set_fd_handler (pfds[i].fd, NULL,
277 alsa_poll_handler, hlp);
278 }
279 if (conf.verbose) {
280 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
281 pfds[i].events, i, pfds[i].fd, err);
282 }
283
284 if (err) {
285 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
286 pfds[i].events, i, pfds[i].fd, err);
287
288 while (i--) {
289 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
290 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500291 g_free (pfds);
malc8b438ba2009-09-12 02:29:25 +0400292 return -1;
293 }
294 }
295 hlp->pfds = pfds;
296 hlp->count = count;
297 hlp->handle = handle;
malcb4f763b2009-09-14 03:53:25 +0400298 hlp->mask = mask;
malc8b438ba2009-09-12 02:29:25 +0400299 return 0;
300}
301
302static int alsa_poll_out (HWVoiceOut *hw)
303{
304 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
305
malcb4f763b2009-09-14 03:53:25 +0400306 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
malc8b438ba2009-09-12 02:29:25 +0400307}
308
309static int alsa_poll_in (HWVoiceIn *hw)
310{
311 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
312
malcb4f763b2009-09-14 03:53:25 +0400313 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
malc8b438ba2009-09-12 02:29:25 +0400314}
315
bellard1d14ffa2005-10-30 18:58:22 +0000316static int alsa_write (SWVoiceOut *sw, void *buf, int len)
317{
318 return audio_pcm_sw_write (sw, buf, len);
319}
320
Michael Walled66bddd2011-01-08 17:53:30 +0100321static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt, int endianness)
bellard1d14ffa2005-10-30 18:58:22 +0000322{
323 switch (fmt) {
324 case AUD_FMT_S8:
325 return SND_PCM_FORMAT_S8;
326
327 case AUD_FMT_U8:
328 return SND_PCM_FORMAT_U8;
329
330 case AUD_FMT_S16:
Michael Walled66bddd2011-01-08 17:53:30 +0100331 if (endianness) {
332 return SND_PCM_FORMAT_S16_BE;
333 }
334 else {
335 return SND_PCM_FORMAT_S16_LE;
336 }
bellard1d14ffa2005-10-30 18:58:22 +0000337
338 case AUD_FMT_U16:
Michael Walled66bddd2011-01-08 17:53:30 +0100339 if (endianness) {
340 return SND_PCM_FORMAT_U16_BE;
341 }
342 else {
343 return SND_PCM_FORMAT_U16_LE;
344 }
bellard1d14ffa2005-10-30 18:58:22 +0000345
thsf941aa22007-02-17 22:19:29 +0000346 case AUD_FMT_S32:
Michael Walled66bddd2011-01-08 17:53:30 +0100347 if (endianness) {
348 return SND_PCM_FORMAT_S32_BE;
349 }
350 else {
351 return SND_PCM_FORMAT_S32_LE;
352 }
thsf941aa22007-02-17 22:19:29 +0000353
354 case AUD_FMT_U32:
Michael Walled66bddd2011-01-08 17:53:30 +0100355 if (endianness) {
356 return SND_PCM_FORMAT_U32_BE;
357 }
358 else {
359 return SND_PCM_FORMAT_U32_LE;
360 }
thsf941aa22007-02-17 22:19:29 +0000361
bellard1d14ffa2005-10-30 18:58:22 +0000362 default:
363 dolog ("Internal logic error: Bad audio format %d\n", fmt);
364#ifdef DEBUG_AUDIO
365 abort ();
366#endif
367 return SND_PCM_FORMAT_U8;
368 }
369}
370
balrogca9cc282008-01-14 04:24:29 +0000371static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
372 int *endianness)
bellard1d14ffa2005-10-30 18:58:22 +0000373{
374 switch (alsafmt) {
375 case SND_PCM_FORMAT_S8:
376 *endianness = 0;
377 *fmt = AUD_FMT_S8;
378 break;
379
380 case SND_PCM_FORMAT_U8:
381 *endianness = 0;
382 *fmt = AUD_FMT_U8;
383 break;
384
385 case SND_PCM_FORMAT_S16_LE:
386 *endianness = 0;
387 *fmt = AUD_FMT_S16;
388 break;
389
390 case SND_PCM_FORMAT_U16_LE:
391 *endianness = 0;
392 *fmt = AUD_FMT_U16;
393 break;
394
395 case SND_PCM_FORMAT_S16_BE:
396 *endianness = 1;
397 *fmt = AUD_FMT_S16;
398 break;
399
400 case SND_PCM_FORMAT_U16_BE:
401 *endianness = 1;
402 *fmt = AUD_FMT_U16;
403 break;
404
thsf941aa22007-02-17 22:19:29 +0000405 case SND_PCM_FORMAT_S32_LE:
406 *endianness = 0;
407 *fmt = AUD_FMT_S32;
408 break;
409
410 case SND_PCM_FORMAT_U32_LE:
411 *endianness = 0;
412 *fmt = AUD_FMT_U32;
413 break;
414
415 case SND_PCM_FORMAT_S32_BE:
416 *endianness = 1;
417 *fmt = AUD_FMT_S32;
418 break;
419
420 case SND_PCM_FORMAT_U32_BE:
421 *endianness = 1;
422 *fmt = AUD_FMT_U32;
423 break;
424
bellard1d14ffa2005-10-30 18:58:22 +0000425 default:
426 dolog ("Unrecognized audio format %d\n", alsafmt);
427 return -1;
428 }
429
430 return 0;
431}
432
bellard1d14ffa2005-10-30 18:58:22 +0000433static void alsa_dump_info (struct alsa_params_req *req,
malc8bb414d2010-04-21 15:40:23 +0400434 struct alsa_params_obt *obt,
435 snd_pcm_format_t obtfmt)
bellard1d14ffa2005-10-30 18:58:22 +0000436{
437 dolog ("parameter | requested value | obtained value\n");
malc8bb414d2010-04-21 15:40:23 +0400438 dolog ("format | %10d | %10d\n", req->fmt, obtfmt);
bellard1d14ffa2005-10-30 18:58:22 +0000439 dolog ("channels | %10d | %10d\n",
440 req->nchannels, obt->nchannels);
441 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
442 dolog ("============================================\n");
443 dolog ("requested: buffer size %d period size %d\n",
444 req->buffer_size, req->period_size);
bellardc0fe3822005-11-05 18:55:28 +0000445 dolog ("obtained: samples %ld\n", obt->samples);
bellard1d14ffa2005-10-30 18:58:22 +0000446}
bellard1d14ffa2005-10-30 18:58:22 +0000447
448static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
449{
450 int err;
451 snd_pcm_sw_params_t *sw_params;
452
453 snd_pcm_sw_params_alloca (&sw_params);
454
455 err = snd_pcm_sw_params_current (handle, sw_params);
456 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000457 dolog ("Could not fully initialize DAC\n");
bellard1d14ffa2005-10-30 18:58:22 +0000458 alsa_logerr (err, "Failed to get current software parameters\n");
459 return;
460 }
461
462 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
463 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000464 dolog ("Could not fully initialize DAC\n");
bellard1d14ffa2005-10-30 18:58:22 +0000465 alsa_logerr (err, "Failed to set software threshold to %ld\n",
466 threshold);
467 return;
468 }
469
470 err = snd_pcm_sw_params (handle, sw_params);
471 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000472 dolog ("Could not fully initialize DAC\n");
bellard1d14ffa2005-10-30 18:58:22 +0000473 alsa_logerr (err, "Failed to set software parameters\n");
474 return;
475 }
476}
477
478static int alsa_open (int in, struct alsa_params_req *req,
479 struct alsa_params_obt *obt, snd_pcm_t **handlep)
480{
481 snd_pcm_t *handle;
482 snd_pcm_hw_params_t *hw_params;
ths60fe76f2007-12-16 03:02:09 +0000483 int err;
malc7a24c802008-06-21 17:14:57 +0000484 int size_in_usec;
ths60fe76f2007-12-16 03:02:09 +0000485 unsigned int freq, nchannels;
bellard1d14ffa2005-10-30 18:58:22 +0000486 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
bellard1d14ffa2005-10-30 18:58:22 +0000487 snd_pcm_uframes_t obt_buffer_size;
488 const char *typ = in ? "ADC" : "DAC";
balrogca9cc282008-01-14 04:24:29 +0000489 snd_pcm_format_t obtfmt;
bellard1d14ffa2005-10-30 18:58:22 +0000490
491 freq = req->freq;
bellard1d14ffa2005-10-30 18:58:22 +0000492 nchannels = req->nchannels;
malc7a24c802008-06-21 17:14:57 +0000493 size_in_usec = req->size_in_usec;
bellard1d14ffa2005-10-30 18:58:22 +0000494
495 snd_pcm_hw_params_alloca (&hw_params);
496
497 err = snd_pcm_open (
498 &handle,
499 pcm_name,
500 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
501 SND_PCM_NONBLOCK
502 );
503 if (err < 0) {
504 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
505 return -1;
506 }
507
508 err = snd_pcm_hw_params_any (handle, hw_params);
509 if (err < 0) {
510 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
511 goto err;
512 }
513
514 err = snd_pcm_hw_params_set_access (
515 handle,
516 hw_params,
517 SND_PCM_ACCESS_RW_INTERLEAVED
518 );
519 if (err < 0) {
520 alsa_logerr2 (err, typ, "Failed to set access type\n");
521 goto err;
522 }
523
524 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
balrogca9cc282008-01-14 04:24:29 +0000525 if (err < 0 && conf.verbose) {
bellard1d14ffa2005-10-30 18:58:22 +0000526 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
bellard1d14ffa2005-10-30 18:58:22 +0000527 }
528
529 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
530 if (err < 0) {
531 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
532 goto err;
533 }
534
535 err = snd_pcm_hw_params_set_channels_near (
536 handle,
537 hw_params,
538 &nchannels
539 );
540 if (err < 0) {
541 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
542 req->nchannels);
543 goto err;
544 }
545
546 if (nchannels != 1 && nchannels != 2) {
547 alsa_logerr2 (err, typ,
548 "Can not handle obtained number of channels %d\n",
549 nchannels);
550 goto err;
551 }
552
malc7a24c802008-06-21 17:14:57 +0000553 if (req->buffer_size) {
malcf3b52982008-06-21 20:09:32 +0000554 unsigned long obt;
555
malc7a24c802008-06-21 17:14:57 +0000556 if (size_in_usec) {
557 int dir = 0;
558 unsigned int btime = req->buffer_size;
bellard1d14ffa2005-10-30 18:58:22 +0000559
560 err = snd_pcm_hw_params_set_buffer_time_near (
561 handle,
562 hw_params,
malc7a24c802008-06-21 17:14:57 +0000563 &btime,
564 &dir
bellardc0fe3822005-11-05 18:55:28 +0000565 );
malcf3b52982008-06-21 20:09:32 +0000566 obt = btime;
bellard1d14ffa2005-10-30 18:58:22 +0000567 }
568 else {
malc7a24c802008-06-21 17:14:57 +0000569 snd_pcm_uframes_t bsize = req->buffer_size;
bellard1d14ffa2005-10-30 18:58:22 +0000570
malc7a24c802008-06-21 17:14:57 +0000571 err = snd_pcm_hw_params_set_buffer_size_near (
bellard1d14ffa2005-10-30 18:58:22 +0000572 handle,
573 hw_params,
malc7a24c802008-06-21 17:14:57 +0000574 &bsize
bellard1d14ffa2005-10-30 18:58:22 +0000575 );
malcf3b52982008-06-21 20:09:32 +0000576 obt = bsize;
malc7a24c802008-06-21 17:14:57 +0000577 }
578 if (err < 0) {
579 alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n",
580 size_in_usec ? "time" : "size", req->buffer_size);
581 goto err;
bellard1d14ffa2005-10-30 18:58:22 +0000582 }
malcf3b52982008-06-21 20:09:32 +0000583
malc64333892008-07-09 19:25:28 +0000584 if ((req->override_mask & 2) && (obt - req->buffer_size))
malcf3b52982008-06-21 20:09:32 +0000585 dolog ("Requested buffer %s %u was rejected, using %lu\n",
586 size_in_usec ? "time" : "size", req->buffer_size, obt);
bellard1d14ffa2005-10-30 18:58:22 +0000587 }
malc7a24c802008-06-21 17:14:57 +0000588
589 if (req->period_size) {
malcf3b52982008-06-21 20:09:32 +0000590 unsigned long obt;
591
malc7a24c802008-06-21 17:14:57 +0000592 if (size_in_usec) {
593 int dir = 0;
594 unsigned int ptime = req->period_size;
595
596 err = snd_pcm_hw_params_set_period_time_near (
597 handle,
598 hw_params,
599 &ptime,
600 &dir
601 );
malcf3b52982008-06-21 20:09:32 +0000602 obt = ptime;
malc7a24c802008-06-21 17:14:57 +0000603 }
604 else {
malca7bb29b2008-07-09 19:25:24 +0000605 int dir = 0;
malc7a24c802008-06-21 17:14:57 +0000606 snd_pcm_uframes_t psize = req->period_size;
607
malca7bb29b2008-07-09 19:25:24 +0000608 err = snd_pcm_hw_params_set_period_size_near (
malc7a24c802008-06-21 17:14:57 +0000609 handle,
610 hw_params,
malca7bb29b2008-07-09 19:25:24 +0000611 &psize,
612 &dir
malc7a24c802008-06-21 17:14:57 +0000613 );
malcf3b52982008-06-21 20:09:32 +0000614 obt = psize;
malc7a24c802008-06-21 17:14:57 +0000615 }
616
617 if (err < 0) {
618 alsa_logerr2 (err, typ, "Failed to set period %s to %d\n",
619 size_in_usec ? "time" : "size", req->period_size);
620 goto err;
621 }
malcf3b52982008-06-21 20:09:32 +0000622
malc541ba4e2009-09-18 10:59:50 +0400623 if (((req->override_mask & 1) && (obt - req->period_size)))
malcf3b52982008-06-21 20:09:32 +0000624 dolog ("Requested period %s %u was rejected, using %lu\n",
625 size_in_usec ? "time" : "size", req->period_size, obt);
bellard1d14ffa2005-10-30 18:58:22 +0000626 }
627
628 err = snd_pcm_hw_params (handle, hw_params);
629 if (err < 0) {
630 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
631 goto err;
632 }
633
634 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
635 if (err < 0) {
636 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
637 goto err;
638 }
639
balrogca9cc282008-01-14 04:24:29 +0000640 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
641 if (err < 0) {
642 alsa_logerr2 (err, typ, "Failed to get format\n");
643 goto err;
644 }
645
646 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
647 dolog ("Invalid format was returned %d\n", obtfmt);
648 goto err;
649 }
650
bellard1d14ffa2005-10-30 18:58:22 +0000651 err = snd_pcm_prepare (handle);
652 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000653 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
bellard1d14ffa2005-10-30 18:58:22 +0000654 goto err;
655 }
656
bellard1d14ffa2005-10-30 18:58:22 +0000657 if (!in && conf.threshold) {
658 snd_pcm_uframes_t threshold;
659 int bytes_per_sec;
660
balrogca9cc282008-01-14 04:24:29 +0000661 bytes_per_sec = freq << (nchannels == 2);
662
663 switch (obt->fmt) {
664 case AUD_FMT_S8:
665 case AUD_FMT_U8:
666 break;
667
668 case AUD_FMT_S16:
669 case AUD_FMT_U16:
670 bytes_per_sec <<= 1;
671 break;
672
673 case AUD_FMT_S32:
674 case AUD_FMT_U32:
675 bytes_per_sec <<= 2;
676 break;
677 }
bellard1d14ffa2005-10-30 18:58:22 +0000678
679 threshold = (conf.threshold * bytes_per_sec) / 1000;
680 alsa_set_threshold (handle, threshold);
681 }
682
bellard1d14ffa2005-10-30 18:58:22 +0000683 obt->nchannels = nchannels;
684 obt->freq = freq;
bellardc0fe3822005-11-05 18:55:28 +0000685 obt->samples = obt_buffer_size;
balrogca9cc282008-01-14 04:24:29 +0000686
bellard1d14ffa2005-10-30 18:58:22 +0000687 *handlep = handle;
688
balrogca9cc282008-01-14 04:24:29 +0000689 if (conf.verbose &&
malc8bb414d2010-04-21 15:40:23 +0400690 (obtfmt != req->fmt ||
balrogca9cc282008-01-14 04:24:29 +0000691 obt->nchannels != req->nchannels ||
692 obt->freq != req->freq)) {
Vagrant Cascadianf093feb2010-02-26 13:39:46 -0800693 dolog ("Audio parameters for %s\n", typ);
malc8bb414d2010-04-21 15:40:23 +0400694 alsa_dump_info (req, obt, obtfmt);
bellard1d14ffa2005-10-30 18:58:22 +0000695 }
696
697#ifdef DEBUG
malc8bb414d2010-04-21 15:40:23 +0400698 alsa_dump_info (req, obt, obtfmt);
bellard1d14ffa2005-10-30 18:58:22 +0000699#endif
700 return 0;
701
702 err:
malc6ebfda12009-09-14 03:51:48 +0400703 alsa_anal_close1 (&handle);
bellard1d14ffa2005-10-30 18:58:22 +0000704 return -1;
705}
706
bellard571ec3d2005-11-20 16:24:34 +0000707static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
708{
709 snd_pcm_sframes_t avail;
710
711 avail = snd_pcm_avail_update (handle);
712 if (avail < 0) {
713 if (avail == -EPIPE) {
714 if (!alsa_recover (handle)) {
715 avail = snd_pcm_avail_update (handle);
716 }
717 }
718
719 if (avail < 0) {
720 alsa_logerr (avail,
721 "Could not obtain number of available frames\n");
722 return -1;
723 }
724 }
725
726 return avail;
727}
728
malc541ba4e2009-09-18 10:59:50 +0400729static void alsa_write_pending (ALSAVoiceOut *alsa)
730{
731 HWVoiceOut *hw = &alsa->hw;
732
733 while (alsa->pending) {
734 int left_till_end_samples = hw->samples - alsa->wpos;
735 int len = audio_MIN (alsa->pending, left_till_end_samples);
736 char *src = advance (alsa->pcm_buf, alsa->wpos << hw->info.shift);
737
738 while (len) {
739 snd_pcm_sframes_t written;
740
741 written = snd_pcm_writei (alsa->handle, src, len);
742
743 if (written <= 0) {
744 switch (written) {
745 case 0:
746 if (conf.verbose) {
747 dolog ("Failed to write %d frames (wrote zero)\n", len);
748 }
749 return;
750
751 case -EPIPE:
752 if (alsa_recover (alsa->handle)) {
753 alsa_logerr (written, "Failed to write %d frames\n",
754 len);
755 return;
756 }
757 if (conf.verbose) {
758 dolog ("Recovering from playback xrun\n");
759 }
760 continue;
761
762 case -ESTRPIPE:
763 /* stream is suspended and waiting for an
764 application recovery */
765 if (alsa_resume (alsa->handle)) {
766 alsa_logerr (written, "Failed to write %d frames\n",
767 len);
768 return;
769 }
770 if (conf.verbose) {
771 dolog ("Resuming suspended output stream\n");
772 }
773 continue;
774
775 case -EAGAIN:
776 return;
777
778 default:
779 alsa_logerr (written, "Failed to write %d frames from %p\n",
780 len, src);
781 return;
782 }
783 }
784
785 alsa->wpos = (alsa->wpos + written) % hw->samples;
786 alsa->pending -= written;
787 len -= written;
788 }
789 }
790}
791
malcbdff2532009-09-18 11:37:39 +0400792static int alsa_run_out (HWVoiceOut *hw, int live)
bellard1d14ffa2005-10-30 18:58:22 +0000793{
794 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
malcbdff2532009-09-18 11:37:39 +0400795 int decr;
bellard1d14ffa2005-10-30 18:58:22 +0000796 snd_pcm_sframes_t avail;
797
bellard571ec3d2005-11-20 16:24:34 +0000798 avail = alsa_get_avail (alsa->handle);
bellard1d14ffa2005-10-30 18:58:22 +0000799 if (avail < 0) {
bellard571ec3d2005-11-20 16:24:34 +0000800 dolog ("Could not get number of available playback frames\n");
bellard1d14ffa2005-10-30 18:58:22 +0000801 return 0;
802 }
803
bellard1d14ffa2005-10-30 18:58:22 +0000804 decr = audio_MIN (live, avail);
malc541ba4e2009-09-18 10:59:50 +0400805 decr = audio_pcm_hw_clip_out (hw, alsa->pcm_buf, decr, alsa->pending);
806 alsa->pending += decr;
807 alsa_write_pending (alsa);
bellard1d14ffa2005-10-30 18:58:22 +0000808 return decr;
809}
810
811static void alsa_fini_out (HWVoiceOut *hw)
812{
813 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
814
815 ldebug ("alsa_fini\n");
malc6ebfda12009-09-14 03:51:48 +0400816 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
bellard1d14ffa2005-10-30 18:58:22 +0000817
Markus Armbrusterfb7da622014-06-06 18:35:13 +0200818 g_free(alsa->pcm_buf);
819 alsa->pcm_buf = NULL;
bellard1d14ffa2005-10-30 18:58:22 +0000820}
821
malc1ea879e2008-12-03 22:48:44 +0000822static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +0000823{
824 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
825 struct alsa_params_req req;
826 struct alsa_params_obt obt;
bellard1d14ffa2005-10-30 18:58:22 +0000827 snd_pcm_t *handle;
malc1ea879e2008-12-03 22:48:44 +0000828 struct audsettings obt_as;
bellard1d14ffa2005-10-30 18:58:22 +0000829
Michael Walled66bddd2011-01-08 17:53:30 +0100830 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
bellardc0fe3822005-11-05 18:55:28 +0000831 req.freq = as->freq;
832 req.nchannels = as->nchannels;
bellard1d14ffa2005-10-30 18:58:22 +0000833 req.period_size = conf.period_size_out;
834 req.buffer_size = conf.buffer_size_out;
malc23fb6002008-07-03 11:29:15 +0000835 req.size_in_usec = conf.size_in_usec_out;
Gerd Hoffmann97f155d2009-06-17 13:51:20 +0200836 req.override_mask =
837 (conf.period_size_out_overridden ? 1 : 0) |
838 (conf.buffer_size_out_overridden ? 2 : 0);
bellard1d14ffa2005-10-30 18:58:22 +0000839
840 if (alsa_open (0, &req, &obt, &handle)) {
841 return -1;
842 }
843
bellardc0fe3822005-11-05 18:55:28 +0000844 obt_as.freq = obt.freq;
845 obt_as.nchannels = obt.nchannels;
balrogca9cc282008-01-14 04:24:29 +0000846 obt_as.fmt = obt.fmt;
847 obt_as.endianness = obt.endianness;
bellardc0fe3822005-11-05 18:55:28 +0000848
bellardd929eba2006-07-04 21:47:22 +0000849 audio_pcm_init_info (&hw->info, &obt_as);
bellardc0fe3822005-11-05 18:55:28 +0000850 hw->samples = obt.samples;
bellard1d14ffa2005-10-30 18:58:22 +0000851
bellardc0fe3822005-11-05 18:55:28 +0000852 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
bellard1d14ffa2005-10-30 18:58:22 +0000853 if (!alsa->pcm_buf) {
bellard4787c712005-11-11 00:06:10 +0000854 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
855 hw->samples, 1 << hw->info.shift);
malc6ebfda12009-09-14 03:51:48 +0400856 alsa_anal_close1 (&handle);
bellard1d14ffa2005-10-30 18:58:22 +0000857 return -1;
858 }
859
860 alsa->handle = handle;
bellard571ec3d2005-11-20 16:24:34 +0000861 return 0;
862}
863
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200864#define VOICE_CTL_PAUSE 0
865#define VOICE_CTL_PREPARE 1
866#define VOICE_CTL_START 2
867
868static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
bellard571ec3d2005-11-20 16:24:34 +0000869{
870 int err;
871
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200872 if (ctl == VOICE_CTL_PAUSE) {
bellard571ec3d2005-11-20 16:24:34 +0000873 err = snd_pcm_drop (handle);
874 if (err < 0) {
bellard32d448c2005-11-20 18:53:05 +0000875 alsa_logerr (err, "Could not stop %s\n", typ);
bellard571ec3d2005-11-20 16:24:34 +0000876 return -1;
877 }
878 }
879 else {
880 err = snd_pcm_prepare (handle);
881 if (err < 0) {
bellard32d448c2005-11-20 18:53:05 +0000882 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
bellard571ec3d2005-11-20 16:24:34 +0000883 return -1;
884 }
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200885 if (ctl == VOICE_CTL_START) {
886 err = snd_pcm_start(handle);
887 if (err < 0) {
888 alsa_logerr (err, "Could not start handle for %s\n", typ);
889 return -1;
890 }
891 }
bellard571ec3d2005-11-20 16:24:34 +0000892 }
893
bellard1d14ffa2005-10-30 18:58:22 +0000894 return 0;
895}
896
897static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
898{
bellard1d14ffa2005-10-30 18:58:22 +0000899 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
900
901 switch (cmd) {
902 case VOICE_ENABLE:
malc301901b2009-10-02 02:37:40 +0400903 {
904 va_list ap;
905 int poll_mode;
906
907 va_start (ap, cmd);
908 poll_mode = va_arg (ap, int);
909 va_end (ap);
910
911 ldebug ("enabling voice\n");
912 if (poll_mode && alsa_poll_out (hw)) {
913 poll_mode = 0;
914 }
915 hw->poll_mode = poll_mode;
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200916 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PREPARE);
malc8b438ba2009-09-12 02:29:25 +0400917 }
bellard1d14ffa2005-10-30 18:58:22 +0000918
919 case VOICE_DISABLE:
920 ldebug ("disabling voice\n");
Jindrich Makovicka22d948a2010-10-17 19:28:14 +0200921 if (hw->poll_mode) {
922 hw->poll_mode = 0;
923 alsa_fini_poll (&alsa->pollhlp);
924 }
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200925 return alsa_voice_ctl (alsa->handle, "playback", VOICE_CTL_PAUSE);
bellard1d14ffa2005-10-30 18:58:22 +0000926 }
bellard571ec3d2005-11-20 16:24:34 +0000927
928 return -1;
bellard1d14ffa2005-10-30 18:58:22 +0000929}
930
malc1ea879e2008-12-03 22:48:44 +0000931static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +0000932{
933 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
934 struct alsa_params_req req;
935 struct alsa_params_obt obt;
bellard1d14ffa2005-10-30 18:58:22 +0000936 snd_pcm_t *handle;
malc1ea879e2008-12-03 22:48:44 +0000937 struct audsettings obt_as;
bellard1d14ffa2005-10-30 18:58:22 +0000938
Michael Walled66bddd2011-01-08 17:53:30 +0100939 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
bellardc0fe3822005-11-05 18:55:28 +0000940 req.freq = as->freq;
941 req.nchannels = as->nchannels;
bellard1d14ffa2005-10-30 18:58:22 +0000942 req.period_size = conf.period_size_in;
943 req.buffer_size = conf.buffer_size_in;
malc7a24c802008-06-21 17:14:57 +0000944 req.size_in_usec = conf.size_in_usec_in;
Gerd Hoffmann97f155d2009-06-17 13:51:20 +0200945 req.override_mask =
946 (conf.period_size_in_overridden ? 1 : 0) |
947 (conf.buffer_size_in_overridden ? 2 : 0);
bellard1d14ffa2005-10-30 18:58:22 +0000948
949 if (alsa_open (1, &req, &obt, &handle)) {
950 return -1;
951 }
952
bellardc0fe3822005-11-05 18:55:28 +0000953 obt_as.freq = obt.freq;
954 obt_as.nchannels = obt.nchannels;
balrogca9cc282008-01-14 04:24:29 +0000955 obt_as.fmt = obt.fmt;
956 obt_as.endianness = obt.endianness;
bellardc0fe3822005-11-05 18:55:28 +0000957
bellardd929eba2006-07-04 21:47:22 +0000958 audio_pcm_init_info (&hw->info, &obt_as);
bellardc0fe3822005-11-05 18:55:28 +0000959 hw->samples = obt.samples;
960
961 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
bellard1d14ffa2005-10-30 18:58:22 +0000962 if (!alsa->pcm_buf) {
bellard4787c712005-11-11 00:06:10 +0000963 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
964 hw->samples, 1 << hw->info.shift);
malc6ebfda12009-09-14 03:51:48 +0400965 alsa_anal_close1 (&handle);
bellard1d14ffa2005-10-30 18:58:22 +0000966 return -1;
967 }
968
969 alsa->handle = handle;
970 return 0;
971}
972
973static void alsa_fini_in (HWVoiceIn *hw)
974{
975 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
976
malc6ebfda12009-09-14 03:51:48 +0400977 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
bellard1d14ffa2005-10-30 18:58:22 +0000978
Markus Armbrusterfb7da622014-06-06 18:35:13 +0200979 g_free(alsa->pcm_buf);
980 alsa->pcm_buf = NULL;
bellard1d14ffa2005-10-30 18:58:22 +0000981}
982
983static int alsa_run_in (HWVoiceIn *hw)
984{
985 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
986 int hwshift = hw->info.shift;
987 int i;
988 int live = audio_pcm_hw_get_live_in (hw);
989 int dead = hw->samples - live;
bellard571ec3d2005-11-20 16:24:34 +0000990 int decr;
bellard1d14ffa2005-10-30 18:58:22 +0000991 struct {
992 int add;
993 int len;
994 } bufs[2] = {
malc98f9f482009-08-11 20:48:02 +0400995 { .add = hw->wpos, .len = 0 },
996 { .add = 0, .len = 0 }
bellard1d14ffa2005-10-30 18:58:22 +0000997 };
bellard571ec3d2005-11-20 16:24:34 +0000998 snd_pcm_sframes_t avail;
bellard1d14ffa2005-10-30 18:58:22 +0000999 snd_pcm_uframes_t read_samples = 0;
1000
1001 if (!dead) {
1002 return 0;
1003 }
1004
bellard571ec3d2005-11-20 16:24:34 +00001005 avail = alsa_get_avail (alsa->handle);
1006 if (avail < 0) {
1007 dolog ("Could not get number of captured frames\n");
1008 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001009 }
1010
Bjørn Mork86635822009-07-30 09:44:26 +02001011 if (!avail) {
1012 snd_pcm_state_t state;
1013
1014 state = snd_pcm_state (alsa->handle);
1015 switch (state) {
1016 case SND_PCM_STATE_PREPARED:
1017 avail = hw->samples;
1018 break;
1019 case SND_PCM_STATE_SUSPENDED:
1020 /* stream is suspended and waiting for an application recovery */
1021 if (alsa_resume (alsa->handle)) {
1022 dolog ("Failed to resume suspended input stream\n");
1023 return 0;
1024 }
1025 if (conf.verbose) {
1026 dolog ("Resuming suspended input stream\n");
1027 }
1028 break;
1029 default:
1030 if (conf.verbose) {
1031 dolog ("No frames available and ALSA state is %d\n", state);
1032 }
1033 return 0;
1034 }
bellard571ec3d2005-11-20 16:24:34 +00001035 }
1036
1037 decr = audio_MIN (dead, avail);
1038 if (!decr) {
1039 return 0;
1040 }
1041
1042 if (hw->wpos + decr > hw->samples) {
1043 bufs[0].len = (hw->samples - hw->wpos);
1044 bufs[1].len = (decr - (hw->samples - hw->wpos));
1045 }
1046 else {
1047 bufs[0].len = decr;
1048 }
bellard1d14ffa2005-10-30 18:58:22 +00001049
1050 for (i = 0; i < 2; ++i) {
1051 void *src;
malc1ea879e2008-12-03 22:48:44 +00001052 struct st_sample *dst;
bellard1d14ffa2005-10-30 18:58:22 +00001053 snd_pcm_sframes_t nread;
1054 snd_pcm_uframes_t len;
1055
1056 len = bufs[i].len;
1057
1058 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
1059 dst = hw->conv_buf + bufs[i].add;
1060
1061 while (len) {
1062 nread = snd_pcm_readi (alsa->handle, src, len);
1063
bellard571ec3d2005-11-20 16:24:34 +00001064 if (nread <= 0) {
bellard1d14ffa2005-10-30 18:58:22 +00001065 switch (nread) {
bellard571ec3d2005-11-20 16:24:34 +00001066 case 0:
1067 if (conf.verbose) {
1068 dolog ("Failed to read %ld frames (read zero)\n", len);
bellard1d14ffa2005-10-30 18:58:22 +00001069 }
bellard1d14ffa2005-10-30 18:58:22 +00001070 goto exit;
1071
bellard571ec3d2005-11-20 16:24:34 +00001072 case -EPIPE:
1073 if (alsa_recover (alsa->handle)) {
1074 alsa_logerr (nread, "Failed to read %ld frames\n", len);
1075 goto exit;
1076 }
1077 if (conf.verbose) {
1078 dolog ("Recovering from capture xrun\n");
1079 }
bellard1d14ffa2005-10-30 18:58:22 +00001080 continue;
1081
bellard571ec3d2005-11-20 16:24:34 +00001082 case -EAGAIN:
1083 goto exit;
1084
bellard1d14ffa2005-10-30 18:58:22 +00001085 default:
1086 alsa_logerr (
1087 nread,
1088 "Failed to read %ld frames from %p\n",
1089 len,
1090 src
1091 );
1092 goto exit;
1093 }
1094 }
1095
Michael Walle00e07672011-01-05 01:05:47 +01001096 hw->conv (dst, src, nread);
bellard1d14ffa2005-10-30 18:58:22 +00001097
1098 src = advance (src, nread << hwshift);
1099 dst += nread;
1100
1101 read_samples += nread;
1102 len -= nread;
1103 }
1104 }
1105
1106 exit:
1107 hw->wpos = (hw->wpos + read_samples) % hw->samples;
1108 return read_samples;
1109}
1110
1111static int alsa_read (SWVoiceIn *sw, void *buf, int size)
1112{
1113 return audio_pcm_sw_read (sw, buf, size);
1114}
1115
1116static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
1117{
bellard571ec3d2005-11-20 16:24:34 +00001118 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
1119
1120 switch (cmd) {
1121 case VOICE_ENABLE:
malca628b862009-10-03 03:30:06 +04001122 {
1123 va_list ap;
1124 int poll_mode;
malc8b438ba2009-09-12 02:29:25 +04001125
malca628b862009-10-03 03:30:06 +04001126 va_start (ap, cmd);
1127 poll_mode = va_arg (ap, int);
1128 va_end (ap);
1129
1130 ldebug ("enabling voice\n");
1131 if (poll_mode && alsa_poll_in (hw)) {
1132 poll_mode = 0;
1133 }
1134 hw->poll_mode = poll_mode;
1135
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +02001136 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_START);
malca628b862009-10-03 03:30:06 +04001137 }
bellard571ec3d2005-11-20 16:24:34 +00001138
1139 case VOICE_DISABLE:
1140 ldebug ("disabling voice\n");
malc8b438ba2009-09-12 02:29:25 +04001141 if (hw->poll_mode) {
1142 hw->poll_mode = 0;
1143 alsa_fini_poll (&alsa->pollhlp);
1144 }
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +02001145 return alsa_voice_ctl (alsa->handle, "capture", VOICE_CTL_PAUSE);
bellard571ec3d2005-11-20 16:24:34 +00001146 }
1147
1148 return -1;
bellard1d14ffa2005-10-30 18:58:22 +00001149}
1150
1151static void *alsa_audio_init (void)
1152{
1153 return &conf;
1154}
1155
1156static void alsa_audio_fini (void *opaque)
1157{
1158 (void) opaque;
1159}
1160
1161static struct audio_option alsa_options[] = {
malc98f9f482009-08-11 20:48:02 +04001162 {
1163 .name = "DAC_SIZE_IN_USEC",
1164 .tag = AUD_OPT_BOOL,
1165 .valp = &conf.size_in_usec_out,
1166 .descr = "DAC period/buffer size in microseconds (otherwise in frames)"
1167 },
1168 {
1169 .name = "DAC_PERIOD_SIZE",
1170 .tag = AUD_OPT_INT,
1171 .valp = &conf.period_size_out,
1172 .descr = "DAC period size (0 to go with system default)",
1173 .overriddenp = &conf.period_size_out_overridden
1174 },
1175 {
1176 .name = "DAC_BUFFER_SIZE",
1177 .tag = AUD_OPT_INT,
1178 .valp = &conf.buffer_size_out,
1179 .descr = "DAC buffer size (0 to go with system default)",
1180 .overriddenp = &conf.buffer_size_out_overridden
1181 },
1182 {
1183 .name = "ADC_SIZE_IN_USEC",
1184 .tag = AUD_OPT_BOOL,
1185 .valp = &conf.size_in_usec_in,
1186 .descr =
1187 "ADC period/buffer size in microseconds (otherwise in frames)"
1188 },
1189 {
1190 .name = "ADC_PERIOD_SIZE",
1191 .tag = AUD_OPT_INT,
1192 .valp = &conf.period_size_in,
1193 .descr = "ADC period size (0 to go with system default)",
1194 .overriddenp = &conf.period_size_in_overridden
1195 },
1196 {
1197 .name = "ADC_BUFFER_SIZE",
1198 .tag = AUD_OPT_INT,
1199 .valp = &conf.buffer_size_in,
1200 .descr = "ADC buffer size (0 to go with system default)",
1201 .overriddenp = &conf.buffer_size_in_overridden
1202 },
1203 {
1204 .name = "THRESHOLD",
1205 .tag = AUD_OPT_INT,
1206 .valp = &conf.threshold,
1207 .descr = "(undocumented)"
1208 },
1209 {
1210 .name = "DAC_DEV",
1211 .tag = AUD_OPT_STR,
1212 .valp = &conf.pcm_name_out,
1213 .descr = "DAC device name (for instance dmix)"
1214 },
1215 {
1216 .name = "ADC_DEV",
1217 .tag = AUD_OPT_STR,
1218 .valp = &conf.pcm_name_in,
1219 .descr = "ADC device name"
1220 },
1221 {
1222 .name = "VERBOSE",
1223 .tag = AUD_OPT_BOOL,
1224 .valp = &conf.verbose,
1225 .descr = "Behave in a more verbose way"
1226 },
Juan Quintela2700efa2009-08-11 02:31:16 +02001227 { /* End of list */ }
bellard1d14ffa2005-10-30 18:58:22 +00001228};
1229
blueswir135f4b582008-10-06 18:08:30 +00001230static struct audio_pcm_ops alsa_pcm_ops = {
Juan Quintela1dd3e4d2009-08-11 02:31:15 +02001231 .init_out = alsa_init_out,
1232 .fini_out = alsa_fini_out,
1233 .run_out = alsa_run_out,
1234 .write = alsa_write,
1235 .ctl_out = alsa_ctl_out,
bellard1d14ffa2005-10-30 18:58:22 +00001236
Juan Quintela1dd3e4d2009-08-11 02:31:15 +02001237 .init_in = alsa_init_in,
1238 .fini_in = alsa_fini_in,
1239 .run_in = alsa_run_in,
1240 .read = alsa_read,
malc8b438ba2009-09-12 02:29:25 +04001241 .ctl_in = alsa_ctl_in,
bellard1d14ffa2005-10-30 18:58:22 +00001242};
1243
1244struct audio_driver alsa_audio_driver = {
Juan Quintelabee37f32009-08-11 02:31:14 +02001245 .name = "alsa",
1246 .descr = "ALSA http://www.alsa-project.org",
1247 .options = alsa_options,
1248 .init = alsa_audio_init,
1249 .fini = alsa_audio_fini,
1250 .pcm_ops = &alsa_pcm_ops,
1251 .can_be_default = 1,
1252 .max_voices_out = INT_MAX,
1253 .max_voices_in = INT_MAX,
1254 .voice_size_out = sizeof (ALSAVoiceOut),
1255 .voice_size_in = sizeof (ALSAVoiceIn)
bellard1d14ffa2005-10-30 18:58:22 +00001256};