Debian Codegraph @

lib/gensio_sound_alsa.h

raw symbols SCIP
1 /*
2 * gensio - A library for abstracting stream I/O
3 * Copyright (C) 2022 Corey Minyard <[email protected]>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 */
7
8
9 #include <alsa/asoundlib.h>
10 #include <gensio/gensio_class.h>
11
12 struct alsa_info {
13 snd_pcm_t *pcm;
14 /* File descriptor info from ALSA. */
15 struct pollfd *fds;
16 struct gensio_iod **iods;
17 unsigned int nrfds;
18 struct gensio_timer *close_timer;
19 };
20
21 static void
22 gensio_sound_alsa_api_close_dev(struct sound_info *si)
23 {
24 struct gensio_os_funcs *o = si->soundll->o;
25 struct alsa_info *a = si->pinfo;
26 unsigned int i;
27
28 if (!a)
29 return;
30
31 for (i = 0; a->iods && i < a->nrfds; i++) {
32 if (!a->iods[i])
33 continue;
34 if (!si->soundll->stream_running)
35 o->clear_fd_handlers_norpt(a->iods[i]);
36 o->release_iod(a->iods[i]);
37 }
38
39 if (a->pcm) {
40 snd_pcm_close(a->pcm);
41 a->pcm = NULL;
42 }
43 if (a->fds) {
44 o->free(o, a->fds);
45 a->fds = NULL;
46 }
47 if (a->iods) {
48 o->free(o, a->iods);
49 a->iods = NULL;
50 }
51 a->nrfds = 0;
52 }
53
54 struct alsa_sound_format_cnv {
55 enum gensio_sound_fmt_type gformat;
56 snd_pcm_format_t format;
57 } alsa_sound_format_cnv[] = {
58 { GENSIO_SOUND_FMT_DOUBLE,SND_PCM_FORMAT_FLOAT64 },
59 { GENSIO_SOUND_FMT_FLOAT, SND_PCM_FORMAT_FLOAT },
60 { GENSIO_SOUND_FMT_S32, SND_PCM_FORMAT_S32 },
61 { GENSIO_SOUND_FMT_S24, SND_PCM_FORMAT_S24 },
62 { GENSIO_SOUND_FMT_S16, SND_PCM_FORMAT_S16 },
63 { GENSIO_SOUND_FMT_S8, SND_PCM_FORMAT_S8 },
64 { GENSIO_SOUND_FMT_U32, SND_PCM_FORMAT_U32 },
65 { GENSIO_SOUND_FMT_U24, SND_PCM_FORMAT_U24 },
66 { GENSIO_SOUND_FMT_U16, SND_PCM_FORMAT_U16 },
67 { GENSIO_SOUND_FMT_U8, SND_PCM_FORMAT_U8 },
68 { GENSIO_SOUND_FMT_DOUBLE_BE, SND_PCM_FORMAT_FLOAT64_BE },
69 { GENSIO_SOUND_FMT_FLOAT_BE, SND_PCM_FORMAT_FLOAT_BE },
70 { GENSIO_SOUND_FMT_S32_BE, SND_PCM_FORMAT_S32_BE },
71 { GENSIO_SOUND_FMT_U32_BE, SND_PCM_FORMAT_U32_BE },
72 { GENSIO_SOUND_FMT_S24_BE, SND_PCM_FORMAT_S24_BE },
73 { GENSIO_SOUND_FMT_U24_BE, SND_PCM_FORMAT_U24_BE },
74 { GENSIO_SOUND_FMT_S16_BE, SND_PCM_FORMAT_S16_BE },
75 { GENSIO_SOUND_FMT_U16_BE, SND_PCM_FORMAT_U16_BE },
76 { GENSIO_SOUND_FMT_DOUBLE_LE, SND_PCM_FORMAT_FLOAT64_LE },
77 { GENSIO_SOUND_FMT_FLOAT_LE, SND_PCM_FORMAT_FLOAT_LE },
78 { GENSIO_SOUND_FMT_S32_LE, SND_PCM_FORMAT_S32_LE },
79 { GENSIO_SOUND_FMT_U32_LE, SND_PCM_FORMAT_U32_LE },
80 { GENSIO_SOUND_FMT_S24_LE, SND_PCM_FORMAT_S24_LE },
81 { GENSIO_SOUND_FMT_U24_LE, SND_PCM_FORMAT_U24_LE },
82 { GENSIO_SOUND_FMT_S16_LE, SND_PCM_FORMAT_S16_LE },
83 { GENSIO_SOUND_FMT_U16_LE, SND_PCM_FORMAT_U16_LE },
84
85 { GENSIO_SOUND_FMT_UNKNOWN }
86 };
87
88 static enum gensio_sound_fmt_type fallback_order[] = {
89 GENSIO_SOUND_FMT_DOUBLE,
90 GENSIO_SOUND_FMT_DOUBLE_ALT,
91 GENSIO_SOUND_FMT_S32,
92 GENSIO_SOUND_FMT_U32,
93 GENSIO_SOUND_FMT_S32_ALT,
94 GENSIO_SOUND_FMT_U32_ALT,
95 GENSIO_SOUND_FMT_FLOAT,
96 GENSIO_SOUND_FMT_FLOAT_ALT,
97 GENSIO_SOUND_FMT_S16,
98 GENSIO_SOUND_FMT_U16,
99 GENSIO_SOUND_FMT_S16_ALT,
100 GENSIO_SOUND_FMT_U16_ALT,
101 GENSIO_SOUND_FMT_S8,
102 GENSIO_SOUND_FMT_U8,
103
104 GENSIO_SOUND_FMT_UNKNOWN,
105 };
106
107 static snd_pcm_format_t
108 gensio_sound_fmt_to_pcm(enum gensio_sound_fmt_type gfmt)
109 {
110 unsigned int i;
111
112 for (i = 0;
113 alsa_sound_format_cnv[i].gformat != GENSIO_SOUND_FMT_UNKNOWN;
114 i++) {
115 if (alsa_sound_format_cnv[i].gformat == gfmt)
116 return alsa_sound_format_cnv[i].format;
117 }
118 assert(0);
119 return 0;
120 }
121
122 static int
123 gensio_sound_alsa_set_hwparams(struct sound_info *si)
124 {
125 struct gensio_os_funcs *o = si->soundll->o;
126 struct alsa_info *a = si->pinfo;
127 snd_pcm_hw_params_t *params;
128 snd_pcm_uframes_t frsize;
129 int err;
130
131 snd_pcm_hw_params_alloca(&params);
132
133 err = snd_pcm_hw_params_any(a->pcm, params);
134 if (err < 0) {
135 gensio_log(o, GENSIO_LOG_ERR,
136 "alsa error from snd_pcm_hw_params_any: %s\n",
137 snd_strerror(err));
138 goto out_err;
139 }
140
141 err = snd_pcm_hw_params_set_rate_resample(a->pcm, params, !si->hwrateonly);
142 if (err < 0) {
143 gensio_log(o, GENSIO_LOG_ERR,
144 "alsa error from snd_pcm_hw_params_set_rate_resample: %s\n",
145 snd_strerror(err));
146 goto out_err;
147 }
148
149 err = snd_pcm_hw_params_set_access(a->pcm, params,
150 SND_PCM_ACCESS_RW_INTERLEAVED);
151 if (err < 0) {
152 gensio_log(o, GENSIO_LOG_ERR,
153 "alsa error from snd_pcm_hw_params_set_access: %s\n",
154 snd_strerror(err));
155 goto out_err;
156 }
157
158 if (si->cnv.pfmt != GENSIO_SOUND_FMT_UNKNOWN) {
159 err = snd_pcm_hw_params_set_format(a->pcm, params,
160 gensio_sound_fmt_to_pcm(si->cnv.pfmt));
161 } else {
162 err = snd_pcm_hw_params_set_format(a->pcm, params,
163 gensio_sound_fmt_to_pcm(si->cnv.ufmt));
164 if (err < 0) {
165 unsigned int i;
166
167 /*
168 * We didn't get the requested one, try the possible values
169 * and use the one we find.
170 */
171 for (i = 0; fallback_order[i] != GENSIO_SOUND_FMT_UNKNOWN; i++) {
172 err = snd_pcm_hw_params_set_format(a->pcm, params,
173 gensio_sound_fmt_to_pcm(fallback_order[i]));
174 if (err >= 0) {
175 setup_convv(si, fallback_order[i]);
176 break;
177 }
178 }
179 }
180 }
181 if (err < 0) {
182 gensio_log(o, GENSIO_LOG_ERR,
183 "alsa error from snd_pcm_hw_params_set_format %d: %s\n",
184 si->cnv.ufmt, snd_strerror(err));
185 goto out_err;
186 }
187 if (si->cnv.enabled) {
188 si->cnv.pframesize = (gensiods) si->cnv.psize * si->chans;
189 si->cnv.buf = o->zalloc(o, si->bufsize * si->cnv.pframesize);
190 if (!si->cnv.buf)
191 return GE_NOMEM;
192 }
193
194 err = snd_pcm_hw_params_set_channels(a->pcm, params, si->chans);
195 if (err < 0) {
196 gensio_log(o, GENSIO_LOG_ERR,
197 "alsa error from snd_pcm_hw_params_set_channels: %s\n",
198 snd_strerror(err));
199 goto out_err;
200 }
201
202 err = snd_pcm_hw_params_set_rate(a->pcm, params, si->samplerate, 0);
203 if (err < 0) {
204 gensio_log(o, GENSIO_LOG_ERR,
205 "alsa error from snd_pcm_hw_params_set_rate: %s\n",
206 snd_strerror(err));
207 goto out_err;
208 }
209
210 frsize = si->bufsize * si->num_bufs;
211 err = snd_pcm_hw_params_set_buffer_size_near(a->pcm, params, &frsize);
212 if (err < 0) {
213 gensio_log(o, GENSIO_LOG_ERR,
214 "alsa error from snd_pcm_hw_params_set_buffer_size_max: %s\n",
215 snd_strerror(err));
216 goto out_err;
217 }
218
219 /*
220 * The below seems to be necessary to set arbitrary rates. It was
221 * ifdef-ed out, but rate setting wasn't always working.
222 */
223 {
224 /* Period time, in usecs. Do u64 arithmetic to avoid overflow. */
225 uint64_t lperiod_time = (si->bufsize * 1000000ULL) / si->samplerate;
226 unsigned int period_time = lperiod_time;
227 int dir;
228
229 err = snd_pcm_hw_params_set_period_time_near(a->pcm, params,
230 &period_time, &dir);
231 if (err < 0) {
232 gensio_log(o, GENSIO_LOG_ERR,
233 "alsa error from snd_pcm_hw_params_ser_period_time_near: %s\n",
234 snd_strerror(err));
235 goto out_err;
236 }
237 }
238
239 /* write the parameters to device */
240 err = snd_pcm_hw_params(a->pcm, params);
241 if (err < 0) {
242 gensio_log(o, GENSIO_LOG_ERR,
243 "alsa error from snd_pcm_hw_params: %s\n",
244 snd_strerror(err));
245 goto out_err;
246 }
247
248 return 0;
249 out_err:
250 return GE_OSERR;
251 }
252
253 static int
254 gensio_sound_alsa_set_swparams(struct sound_info *si)
255 {
256 struct gensio_os_funcs *o = si->soundll->o;
257 struct alsa_info *a = si->pinfo;
258 snd_pcm_sw_params_t *params;
259 int err;
260
261 snd_pcm_sw_params_alloca(&params);
262
263 /* get the current swparams */
264 err = snd_pcm_sw_params_current(a->pcm, params);
265 if (err < 0) {
266 gensio_log(o, GENSIO_LOG_ERR,
267 "alsa error from snd_pcm_sw_params_current: %s\n",
268 snd_strerror(err));
269 goto out_err;
270 }
271
272 /* start the transfer when a buffer is written: */
273 err = snd_pcm_sw_params_set_start_threshold(a->pcm, params, si->bufsize);
274 if (err < 0) {
275 gensio_log(o, GENSIO_LOG_ERR,
276 "alsa error from snd_pcm_sw_params_set_start_threshold: %s\n",
277 snd_strerror(err));
278 goto out_err;
279 }
280
281 /*
282 * Allow the transfer when at least period_size ont buffer can be
283 * processed.
284 */
285 err = snd_pcm_sw_params_set_avail_min(a->pcm, params, si->bufsize);
286 if (err < 0) {
287 gensio_log(o, GENSIO_LOG_ERR,
288 "alsa error from snd_pcm_sw_params_set_avail_min: %s\n",
289 snd_strerror(err));
290 goto out_err;
291 }
292
293 #if 0
294 err = snd_pcm_sw_params_set_period_event(a->pcm, params, 1);
295 if (err < 0) {
296 gensio_log(o, GENSIO_LOG_ERR,
297 "alsa error from snd_pcm_sw_params_set_period_event: %s\n",
298 snd_strerror(err));
299 goto out_err;
300 }
301 #endif
302
303 err = snd_pcm_sw_params(a->pcm, params);
304 if (err < 0) {
305 gensio_log(o, GENSIO_LOG_ERR,
306 "alsa error from snd_pcm_sw_params: %s\n",
307 snd_strerror(err));
308 goto out_err;
309 }
310
311 return 0;
312
313 out_err:
314 return GE_OSERR;
315 }
316
317 static bool
318 gensio_sound_alsa_check_xrun_recovery(struct sound_info *si, int rv)
319 {
320 struct alsa_info *a = si->pinfo;
321 snd_pcm_state_t state = snd_pcm_state(a->pcm);
322
323 switch (state) {
324 case SND_PCM_STATE_XRUN:
325 rv = snd_pcm_prepare(a->pcm);
326 if (rv == 0)
327 return true;
328 return rv;
329
330 case SND_PCM_STATE_SUSPENDED:
331 rv = snd_pcm_resume(a->pcm);
332 if (rv == -EAGAIN)
333 return false;
334 if (rv < 0)
335 rv = snd_pcm_prepare(a->pcm);
336 if (rv == 0)
337 return true;
338 break;
339
340 default:
341 break;
342 }
343
344 if (rv) {
345 gensio_log(si->soundll->o, GENSIO_LOG_ERR,
346 "alsa error from xrun_recovery: %s\n",
347 snd_strerror(rv));
348 si->soundll->err = GE_OSERR;
349 gensio_sound_sched_deferred_op(si->soundll);
350 }
351
352 return false;
353 }
354
355 static void
356 gensio_sound_alsa_do_read(struct sound_info *si)
357 {
358 struct alsa_info *a = si->pinfo;
359 struct sound_ll *soundll = si->soundll;
360 int rv;
361
362 gensio_sound_alsa_check_xrun_recovery(si, 0);
363 if (soundll->err)
364 return;
365
366 if (si->cnv.enabled) {
367 rv = snd_pcm_readi(a->pcm,
368 si->cnv.buf + (si->len * si->cnv.pframesize),
369 si->bufsize - si->len);
370 } else {
371 rv = snd_pcm_readi(a->pcm, si->buf + (si->len * si->framesize),
372 si->bufsize - si->len);
373 }
374
375 if (rv < 0) {
376 if (rv == -EAGAIN || rv == -EBUSY)
377 return;
378 gensio_sound_alsa_check_xrun_recovery(si, rv);
379 } else {
380 si->len += rv;
381 assert(si->len <= si->bufsize);
382 if (si->len == si->bufsize) {
383 if (si->cnv.enabled) {
384 const unsigned char *ibuf = si->cnv.buf;
385 unsigned char *obuf = si->buf;
386 gensiods i;
387
388 for (i = 0; i < si->bufsize * si->chans; i++)
389 si->cnv.convin(&ibuf, &obuf, &si->cnv);
390 }
391 si->ready = true;
392 }
393 }
394 }
395
396 static void
397 gensio_sound_alsa_api_set_read(struct sound_info *si, bool enable)
398 {
399 struct gensio_os_funcs *o = si->soundll->o;
400 struct alsa_info *a = si->pinfo;
401 unsigned int i;
402
403 for (i = 0; i < a->nrfds; i++) {
404 if (a->fds[i].events & POLLIN)
405 o->set_read_handler(a->iods[i], enable);
406 if (a->fds[i].events & POLLOUT)
407 o->set_write_handler(a->iods[i], enable);
408 if (a->fds[i].events & POLLERR)
409 o->set_except_handler(a->iods[i], enable);
410 }
411 if (enable && !si->ready)
412 gensio_sound_alsa_do_read(si);
413 }
414
415 static void
416 gensio_sound_alsa_api_set_write(struct sound_info *si, bool enable)
417 {
418 struct gensio_os_funcs *o = si->soundll->o;
419 struct alsa_info *a = si->pinfo;
420 unsigned int i;
421
422 for (i = 0; i < a->nrfds; i++) {
423 if (a->fds[i].events & POLLIN)
424 o->set_read_handler(a->iods[i], enable);
425 if (a->fds[i].events & POLLOUT)
426 o->set_write_handler(a->iods[i], enable);
427 if (a->fds[i].events & POLLERR)
428 o->set_except_handler(a->iods[i], enable);
429 }
430 }
431
432 static void
433 gensio_sound_alsa_read_handlerb(struct gensio_iod *iod, void *cb_data,
434 unsigned short ievents)
435 {
436 struct sound_info *si = cb_data;
437 struct alsa_info *a = si->pinfo;
438 struct sound_ll *soundll = si->soundll;
439 unsigned int i;
440 unsigned short revents;
441
442 gensio_sound_ll_lock(soundll);
443 for (i = 0; i < a->nrfds; i++)
444 a->fds[i].revents = ievents;
445 revents = 0;
446 snd_pcm_poll_descriptors_revents(a->pcm, a->fds, a->nrfds, &revents);
447 if (revents & (POLLERR | POLLIN | POLLOUT)) {
448 restart:
449 if (soundll->in.ready || soundll->err)
450 gensio_sound_ll_check_read(soundll);
451 if (!soundll->in.ready && !soundll->err) {
452 gensio_sound_alsa_do_read(&soundll->in);
453 if (soundll->in.ready || soundll->err)
454 goto restart;
455 }
456 }
457 gensio_sound_ll_unlock(soundll);
458 }
459
460 static void
461 gensio_sound_alsa_read_handler(struct gensio_iod *iod, void *cb_data)
462 {
463 gensio_sound_alsa_read_handlerb(iod, cb_data, POLLIN);
464 }
465
466 static void
467 gensio_sound_alsa_read_write_handler(struct gensio_iod *iod, void *cb_data)
468 {
469 gensio_sound_alsa_read_handlerb(iod, cb_data, POLLOUT);
470 }
471
472 static void
473 gensio_sound_alsa_read_exc_handler(struct gensio_iod *iod, void *cb_data)
474 {
475 gensio_sound_alsa_read_handlerb(iod, cb_data, POLLERR);
476 }
477
478 static void
479 gensio_sound_alsa_write_handlerb(struct gensio_iod *iod, void *cb_data,
480 unsigned short ievents)
481 {
482 struct sound_info *si = cb_data;
483 struct alsa_info *a = si->pinfo;
484 struct sound_ll *soundll = si->soundll;
485 unsigned int i;
486 unsigned short revents;
487
488 gensio_sound_ll_lock(soundll);
489 for (i = 0; i < a->nrfds; i++)
490 a->fds[i].revents = ievents;
491 revents = 0;
492 snd_pcm_poll_descriptors_revents(a->pcm, a->fds, a->nrfds, &revents);
493 /* For some weird reason we get POLLIN on the output device. */
494 if (revents & (POLLERR | POLLOUT | POLLIN)) {
495 si->ready = true;
496 gensio_sound_ll_check_write(soundll);
497 }
498 gensio_sound_ll_unlock(soundll);
499 }
500
501 static void
502 gensio_sound_alsa_write_handler(struct gensio_iod *iod, void *cb_data)
503 {
504 gensio_sound_alsa_write_handlerb(iod, cb_data, POLLOUT);
505 }
506
507 static void
508 gensio_sound_alsa_write_read_handler(struct gensio_iod *iod, void *cb_data)
509 {
510 gensio_sound_alsa_write_handlerb(iod, cb_data, POLLIN);
511 }
512
513 static void
514 gensio_sound_alsa_write_exc_handler(struct gensio_iod *iod, void *cb_data)
515 {
516 gensio_sound_alsa_write_handlerb(iod, cb_data, POLLERR);
517 }
518
519 static void
520 gensio_sound_alsa_cleared_handler(struct gensio_iod *iod, void *cb_data)
521 {
522 struct sound_info *si = cb_data;
523 struct sound_ll *soundll = si->soundll;
524
525 gensio_sound_ll_lock(soundll);
526 soundll->nr_waiting_close--;
527 if (soundll->nr_waiting_close == 0) {
528 soundll->do_close_now = true;
529 gensio_sound_sched_deferred_op(soundll);
530 }
531 gensio_sound_ll_unlock(soundll);
532 }
533
534
535 static void
536 gensio_sound_alsa_timeout(struct gensio_timer *t, void *cb_data)
537 {
538 struct sound_info *si = cb_data;
539 struct alsa_info *a = si->pinfo;
540 unsigned int i;
541
542 for (i = 0; i < a->nrfds; i++)
543 si->soundll->o->clear_fd_handlers(a->iods[i]);
544 }
545
546 static unsigned long
547 gensio_sound_alsa_drain_count(struct sound_info *si)
548 {
549 struct alsa_info *a = si->pinfo;
550 snd_pcm_sframes_t frames_left;
551
552 snd_pcm_delay(a->pcm, &frames_left);
553 return frames_left;
554 }
555
556 static unsigned int
557 gensio_sound_alsa_api_start_close(struct sound_info *si)
558 {
559 struct gensio_os_funcs *o = si->soundll->o;
560 struct alsa_info *a = si->pinfo;
561 gensio_time timeout;
562 snd_pcm_sframes_t frames_left = 0;
563 uint64_t drain_time;
564
565 if (!si->is_input && a->nrfds > 0) {
566 /* Wait for output to drain. */
567 snd_pcm_delay(a->pcm, &frames_left);
568 drain_time = frames_left * GENSIO_NSECS_IN_SEC / si->samplerate;
569 timeout.secs = drain_time / GENSIO_NSECS_IN_SEC;
570 timeout.nsecs = drain_time % GENSIO_NSECS_IN_SEC;
571 assert(o->start_timer(a->close_timer, &timeout) == 0);
572 } else if (a->nrfds > 0) {
573 gensio_sound_alsa_timeout(NULL, si);
574 }
575 return a->nrfds;
576 }
577
578 static int
579 gensio_sound_alsa_api_write(struct sound_info *out, const unsigned char *buf,
580 gensiods buflen, gensiods *nr_written)
581 {
582 struct alsa_info *a = out->pinfo;
583 snd_pcm_sframes_t rv;
584
585 retry:
586 rv = snd_pcm_writei(a->pcm, buf, buflen);
587 if (rv < 0) {
588 if (rv == -EBUSY || rv == -EAGAIN) {
589 out->ready = false;
590 *nr_written = 0;
591 return 0;
592 }
593 if (gensio_sound_alsa_check_xrun_recovery(out, rv))
594 goto retry;
595 return out->soundll->err;
596 } else {
597 *nr_written = rv;
598 }
599 return 0;
600 }
601
602 static int
603 gensio_sound_alsa_api_open_dev(struct sound_info *si)
604 {
605 struct gensio_os_funcs *o = si->soundll->o;
606 struct alsa_info *a = si->pinfo;
607 int err;
608 unsigned int i;
609 snd_pcm_stream_t stype = (si->is_input ? SND_PCM_STREAM_CAPTURE :
610 SND_PCM_STREAM_PLAYBACK);
611
612 err = snd_pcm_open(&a->pcm, si->devname, stype, SND_PCM_NONBLOCK);
613 if (err < 0) {
614 gensio_log(o, GENSIO_LOG_ERR,
615 "alsa error from snd_pcm_open: %s",
616 snd_strerror(err));
617 return GE_OSERR;
618 }
619
620 err = gensio_sound_alsa_set_hwparams(si);
621 if (err) {
622 gensio_sound_alsa_api_close_dev(si);
623 return err;
624 }
625 err = gensio_sound_alsa_set_swparams(si);
626 if (err) {
627 gensio_sound_alsa_api_close_dev(si);
628 return err;
629 }
630
631 a->nrfds = snd_pcm_poll_descriptors_count(a->pcm);
632 if (a->nrfds == 0) {
633 gensio_sound_alsa_api_close_dev(si);
634 return GE_INCONSISTENT;
635 }
636
637 a->fds = o->zalloc(o, a->nrfds * sizeof(struct pollfd));
638 if (!a->fds) {
639 gensio_sound_alsa_api_close_dev(si);
640 return GE_NOMEM;
641 }
642
643 a->iods = o->zalloc(o, a->nrfds * sizeof(struct gensio_iod *));
644 if (!a->iods) {
645 gensio_sound_alsa_api_close_dev(si);
646 return GE_NOMEM;
647 }
648
649 err = snd_pcm_poll_descriptors(a->pcm, a->fds, a->nrfds);
650 if (err < 0) {
651 gensio_log(o, GENSIO_LOG_ERR,
652 "alsa error from snd_pcm_poll_descriptors: %s",
653 snd_strerror(err));
654 gensio_sound_alsa_api_close_dev(si);
655 return GE_OSERR;
656 }
657
658 for (i = 0; i < a->nrfds; i++) {
659 err = o->add_iod(o, GENSIO_IOD_PIPE, a->fds[i].fd,
660 &(a->iods[i]));
661 if (err) {
662 gensio_sound_alsa_api_close_dev(si);
663 return err;
664 }
665 err = o->set_fd_handlers(a->iods[i], si,
666 (stype == SND_PCM_STREAM_CAPTURE ?
667 gensio_sound_alsa_read_handler :
668 gensio_sound_alsa_write_read_handler),
669 (stype == SND_PCM_STREAM_PLAYBACK ?
670 gensio_sound_alsa_write_handler :
671 gensio_sound_alsa_read_write_handler),
672 (stype == SND_PCM_STREAM_CAPTURE ?
673 gensio_sound_alsa_read_exc_handler :
674 gensio_sound_alsa_write_exc_handler),
675 gensio_sound_alsa_cleared_handler);
676 if (err) {
677 gensio_sound_alsa_api_close_dev(si);
678 return err;
679 }
680 }
681
682 return 0;
683 }
684
685 static int
686 gensio_sound_alsa_api_devices(struct gensio_os_funcs *o,
687 char ***rnames, char ***rspecs, gensiods *rcount)
688 {
689 void **hints, **n;
690 gensiods count = 0, size = 0;
691 char **names = NULL, **specs = NULL;
692
693 if (snd_device_name_hint(-1, "pcm", &hints) < 0) {
694 *rcount = 0;
695 return 0;
696 }
697
698 for (n = hints; *n != NULL; n++) {
699 char *name, *desc, *io;
700 unsigned int i, j, len;
701
702 name = snd_device_name_get_hint(*n, "NAME");
703 if (!name)
704 continue;
705 desc = snd_device_name_get_hint(*n, "DESC");
706 if (desc) {
707 char *n2;
708
709 len = strlen(name) + strlen(desc);
710 for (i = 0; desc[i]; i++) {
711 if (desc[i] == '\n')
712 len += 4;
713 }
714 n2 = malloc(len + 7); /* extra for nil and "\n " */
715 if (!n2) {
716 free(name);
717 free(desc);
718 goto out_nomem;
719 }
720 strncpy(n2, name, len + 6);
721 i = strlen(n2);
722 strncpy(n2 + i, "\n ", 6);
723 i += 5;
724 for (j = 0; desc[j]; j++) {
725 if (desc[j] == '\n') {
726 strncpy(n2 + i, "\n ", 6);
727 i += 5;
728 } else {
729 n2[i++] = desc[j];
730 }
731 }
732 n2[i++] = '\n';
733 n2[i++] = '\0';
734 free(name);
735 free(desc);
736 name = n2;
737 }
738 io = snd_device_name_get_hint(*n, "IOID");
739 if (io) {
740 io[0] = tolower(io[0]);
741 } else {
742 io = strdup("input,output");
743 if (!io) {
744 free(name);
745 goto out_nomem;
746 }
747 }
748
749 if (count >= size) {
750 if (extend_sound_devs(&names, &specs, &size)) {
751 free(name);
752 free(io);
753 goto out_nomem;
754 }
755 }
756 names[count] = name;
757 name = NULL;
758 specs[count] = io;
759 count++;
760 }
761 snd_device_name_free_hint(hints);
762 *rnames = names;
763 *rspecs = specs;
764 *rcount = count;
765
766 return 0;
767
768 out_nomem:
769 snd_device_name_free_hint(hints);
770 gensio_sound_devices_free(names, specs, count);
771 return GE_NOMEM;
772 }
773
774 static void
775 gensio_sound_alsa_cleanup_func(void)
776 {
777 snd_config_update_free_global();
778 }
779
780 static struct gensio_class_cleanup gensio_sound_alsa_class_cleanup = {
781 /*
782 * If you don't call this, lots of cached information gets left
783 * lying around in the alsa code and valgrind complains.
784 */
785 .cleanup = gensio_sound_alsa_cleanup_func
786 };
787
788 static int
789 gensio_sound_alsa_api_setup(struct gensio_pparm_info *p,
790 struct sound_info *si, struct gensio_sound_info *io)
791 {
792 struct gensio_os_funcs *o = si->soundll->o;
793 struct alsa_info *a;
794 char *start, *end;
795 unsigned int len;
796
797 /*
798 * Extract the card name/number (the value after the ":" and
799 * before the ","), like in "plughw:1,0".
800 */
801 start = strchr(io->devname, ':');
802 if (!start) {
803 gensio_pparm_log(p, "devname %s has no ':' in it", io->devname);
804 return GE_INVAL;
805 }
806 start++;
807 end = strchr(start, ',');
808 if (!end)
809 len = strlen(start);
810 else
811 len = end - start;
812 si->cardname = gensio_strndup(o, start, len);
813 if (!si->cardname)
814 return GE_NOMEM;
815
816 gensio_register_class_cleanup(&gensio_sound_alsa_class_cleanup);
817 si->pinfo = o->zalloc(o, sizeof(struct alsa_info));
818 if (!si->pinfo) {
819 o->free(o, si->cardname);
820 si->cardname = NULL;
821 return GE_NOMEM;
822 }
823 a = si->pinfo;
824
825 a->close_timer = o->alloc_timer(o, gensio_sound_alsa_timeout, si);
826 if (!a->close_timer) {
827 o->free(o, si->pinfo);
828 si->pinfo = NULL;
829 o->free(o, si->cardname);
830 si->cardname = NULL;
831 return GE_NOMEM;
832 }
833
834 return 0;
835 }
836
837 static void
838 gensio_sound_alsa_api_cleanup(struct sound_info *si)
839 {
840 struct gensio_os_funcs *o = si->soundll->o;
841 struct alsa_info *a = si->pinfo;
842
843 if (a) {
844 if (a->close_timer)
845 o->free_timer(a->close_timer);
846 o->free(o, a);
847 si->pinfo = NULL;
848 }
849 }
850
851 static struct sound_type alsa_sound_type = {
852 "alsa",
853 .setup = gensio_sound_alsa_api_setup,
854 .cleanup = gensio_sound_alsa_api_cleanup,
855 .open_dev = gensio_sound_alsa_api_open_dev,
856 .close_dev = gensio_sound_alsa_api_close_dev,
857 .sub_write = gensio_sound_alsa_api_write,
858 .write = gensio_sound_api_default_write,
859 .set_write_enable = gensio_sound_alsa_api_set_write,
860 .set_read_enable = gensio_sound_alsa_api_set_read,
861 .start_close = gensio_sound_alsa_api_start_close,
862 .drain_count = gensio_sound_alsa_drain_count,
863 .devices = gensio_sound_alsa_api_devices
864 };
865
866 #define ALSA_INIT &alsa_sound_type,