]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-sink.c
don't hit an assert when we cannot resume a device
[pulseaudio] / src / modules / module-alsa-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_POLL_H
32 #include <sys/poll.h>
33 #else
34 #include "poll.h"
35 #endif
36
37 #include <asoundlib.h>
38
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
41
42 #include <pulsecore/core.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/memchunk.h>
45 #include <pulsecore/sink.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/sample-util.h>
49 #include <pulsecore/log.h>
50 #include <pulsecore/macro.h>
51 #include <pulsecore/thread.h>
52 #include <pulsecore/core-error.h>
53
54 #include "alsa-util.h"
55 #include "module-alsa-sink-symdef.h"
56
57 PA_MODULE_AUTHOR("Lennart Poettering")
58 PA_MODULE_DESCRIPTION("ALSA Sink")
59 PA_MODULE_VERSION(PACKAGE_VERSION)
60 PA_MODULE_USAGE(
61 "sink_name=<name for the sink> "
62 "device=<ALSA device> "
63 "format=<sample format> "
64 "channels=<number of channels> "
65 "rate=<sample rate> "
66 "fragments=<number of fragments> "
67 "fragment_size=<fragment size> "
68 "channel_map=<channel map> "
69 "mmap=<enable memory mapping?>")
70
71 #define DEFAULT_DEVICE "default"
72
73 #define DEFAULT_NFRAGS 4
74 #define DEFAULT_FRAGSIZE_MSEC 25
75
76 struct userdata {
77 pa_core *core;
78 pa_module *module;
79 pa_sink *sink;
80 pa_thread *thread;
81 pa_asyncmsgq *asyncmsgq;
82
83 snd_pcm_t *pcm_handle;
84
85 pa_alsa_fdlist *mixer_fdl;
86 snd_mixer_t *mixer_handle;
87 snd_mixer_elem_t *mixer_elem;
88 long hw_volume_max, hw_volume_min;
89
90 size_t frame_size, fragment_size, hwbuf_size;
91 unsigned nfragments;
92 pa_memchunk memchunk;
93
94 char *device_name;
95
96 int use_mmap;
97
98 int first;
99
100 struct pollfd *pollfd;
101 int n_alsa_fds;
102 };
103
104 enum {
105 POLLFD_ASYNCQ,
106 POLLFD_ALSA_BASE
107 };
108
109 static const char* const valid_modargs[] = {
110 "device",
111 "sink_name",
112 "format",
113 "channels",
114 "rate",
115 "fragments",
116 "fragment_size",
117 "channel_map",
118 "mmap",
119 NULL
120 };
121
122 static int mmap_write(struct userdata *u) {
123 snd_pcm_sframes_t n;
124 int err;
125 const snd_pcm_channel_area_t *areas;
126 snd_pcm_uframes_t offset, frames;
127 int work_done = 0;
128
129 pa_assert(u);
130 pa_assert(u->sink);
131
132 for (;;) {
133 pa_memchunk chunk;
134 void *p;
135
136 if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
137
138 if (n == -EPIPE) {
139 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
140 u->first = 1;
141 }
142
143 if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0)
144 continue;
145
146 if (err == -EAGAIN)
147 return work_done;
148
149 pa_log("snd_pcm_avail_update: %s", snd_strerror(n));
150 return -1;
151 }
152
153 /* pa_log("Got request for %i samples", (int) n); */
154
155 if (n <= 0)
156 return work_done;
157
158 frames = n;
159
160 if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
161
162 if (err == -EPIPE) {
163 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
164 u->first = 1;
165 }
166
167 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
168 continue;
169
170 if (err == -EAGAIN)
171 return work_done;
172
173 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
174 return -1;
175 }
176
177 /* Check these are multiples of 8 bit */
178 pa_assert((areas[0].first & 7) == 0);
179 pa_assert((areas[0].step & 7)== 0);
180
181 /* We assume a single interleaved memory buffer */
182 pa_assert((areas[0].first >> 3) == 0);
183 pa_assert((areas[0].step >> 3) == u->frame_size);
184
185 p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
186
187 chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, 1);
188 chunk.length = pa_memblock_get_length(chunk.memblock);
189 chunk.index = 0;
190
191 pa_sink_render_into_full(u->sink, &chunk);
192
193 /* FIXME: Maybe we can do something to keep this memory block
194 * a little bit longer around? */
195 pa_memblock_unref_fixed(chunk.memblock);
196
197 if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
198
199 if (err == -EPIPE) {
200 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
201 u->first = 1;
202 }
203
204 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
205 continue;
206
207 if (err == -EAGAIN)
208 return work_done;
209
210 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
211 return -1;
212 }
213
214 work_done = 1;
215
216 /* pa_log("wrote %i samples", (int) frames); */
217 }
218 }
219
220 static pa_usec_t sink_get_latency(struct userdata *u) {
221 pa_usec_t r = 0;
222 snd_pcm_sframes_t frames = 0;
223 int err;
224
225 pa_assert(u);
226
227 snd_pcm_avail_update(u->pcm_handle);
228
229 if ((err = snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
230 pa_log("Failed to get delay: %s", snd_strerror(err));
231 return 0;
232 }
233
234 if (frames > 0)
235 r = pa_bytes_to_usec(frames * u->frame_size, &u->sink->sample_spec);
236
237 if (u->memchunk.memblock)
238 r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
239
240 return r;
241 }
242
243 static int build_pollfd(struct userdata *u) {
244 int err;
245
246 pa_assert(u);
247 pa_assert(u->pcm_handle);
248
249 if ((u->n_alsa_fds = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
250 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(u->n_alsa_fds));
251 return -1;
252 }
253
254 pa_xfree(u->pollfd);
255 u->pollfd = pa_xnew0(struct pollfd, POLLFD_ALSA_BASE + u->n_alsa_fds);
256
257 u->pollfd[POLLFD_ASYNCQ].fd = pa_asyncmsgq_get_fd(u->asyncmsgq);
258 u->pollfd[POLLFD_ASYNCQ].events = POLLIN;
259
260 if ((err = snd_pcm_poll_descriptors(u->pcm_handle, u->pollfd+POLLFD_ALSA_BASE, u->n_alsa_fds)) < 0) {
261 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
262 return -1;
263 }
264
265 return 0;
266 }
267
268 static int suspend(struct userdata *u) {
269 pa_assert(u);
270 pa_assert(u->pcm_handle);
271
272 snd_pcm_drain(u->pcm_handle); /* Let's suspend */
273 snd_pcm_close(u->pcm_handle);
274 u->pcm_handle = NULL;
275
276 pa_log_debug("Device suspended...");
277
278 return 0;
279 }
280
281 static int unsuspend(struct userdata *u) {
282 pa_sample_spec ss;
283 int err, b;
284 unsigned nfrags;
285 snd_pcm_uframes_t period_size;
286
287 pa_assert(u);
288 pa_assert(!u->pcm_handle);
289
290 pa_log_debug("Trying resume...");
291
292 snd_config_update_free_global();
293 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
294 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
295 goto fail;
296 }
297
298 ss = u->sink->sample_spec;
299 nfrags = u->nfragments;
300 period_size = u->fragment_size / u->frame_size;
301 b = u->use_mmap;
302
303 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
304 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
305 goto fail;
306 }
307
308 if (b != u->use_mmap) {
309 pa_log_warn("Resume failed, couldn't get original access mode.");
310 goto fail;
311 }
312
313 if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
314 pa_log_warn("Resume failed, couldn't restore original sample settings.");
315 goto fail;
316 }
317
318 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
319 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
320 goto fail;
321 }
322
323 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
324 pa_log("Failed to set software parameters: %s", snd_strerror(err));
325 goto fail;
326 }
327
328 if (build_pollfd(u) < 0)
329 goto fail;
330
331 /* FIXME: We need to reload the volume somehow */
332
333 u->first = 1;
334
335 pa_log_debug("Resumed successfully...");
336
337 return 0;
338
339 fail:
340 if (u->pcm_handle) {
341 snd_pcm_close(u->pcm_handle);
342 u->pcm_handle = NULL;
343 }
344
345 return -1;
346 }
347
348 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
349 struct userdata *u = PA_SINK(o)->userdata;
350
351 switch (code) {
352
353 case PA_SINK_MESSAGE_GET_LATENCY: {
354 pa_usec_t r = 0;
355
356 if (u->pcm_handle)
357 r = sink_get_latency(u);
358
359 *((pa_usec_t*) data) = r;
360
361 break;
362 }
363
364 case PA_SINK_MESSAGE_SET_STATE:
365
366 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
367
368 case PA_SINK_SUSPENDED:
369 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
370
371 if (suspend(u) < 0)
372 return -1;
373
374 break;
375
376 case PA_SINK_IDLE:
377 case PA_SINK_RUNNING:
378
379 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
380 if (unsuspend(u) < 0)
381 return -1;
382 }
383
384 break;
385
386 case PA_SINK_DISCONNECTED:
387 ;
388 }
389
390 break;
391 }
392
393 return pa_sink_process_msg(o, code, data, offset, chunk);
394 }
395
396 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
397 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
398
399 pa_assert(u);
400 pa_assert(u->mixer_handle);
401
402 if (mask == SND_CTL_EVENT_MASK_REMOVE)
403 return 0;
404
405 if (mask & SND_CTL_EVENT_MASK_VALUE) {
406 pa_sink_get_volume(u->sink);
407 pa_sink_get_mute(u->sink);
408 }
409
410 return 0;
411 }
412
413 static int sink_get_volume_cb(pa_sink *s) {
414 struct userdata *u = s->userdata;
415 int err;
416 int i;
417
418 pa_assert(u);
419 pa_assert(u->mixer_elem);
420
421 for (i = 0; i < s->sample_spec.channels; i++) {
422 long set_vol, vol;
423
424 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
425
426 if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol)) < 0)
427 goto fail;
428
429 set_vol = (long) roundf(((float) s->volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
430
431 /* Try to avoid superfluous volume changes */
432 if (set_vol != vol)
433 s->volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
434 }
435
436 return 0;
437
438 fail:
439 pa_log_error("Unable to read volume: %s", snd_strerror(err));
440
441 s->get_volume = NULL;
442 s->set_volume = NULL;
443 return -1;
444 }
445
446 static int sink_set_volume_cb(pa_sink *s) {
447 struct userdata *u = s->userdata;
448 int err;
449 int i;
450
451 pa_assert(u);
452 pa_assert(u->mixer_elem);
453
454 for (i = 0; i < s->sample_spec.channels; i++) {
455 long alsa_vol;
456 pa_volume_t vol;
457
458 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
459
460 vol = s->volume.values[i];
461
462 if (vol > PA_VOLUME_NORM)
463 vol = PA_VOLUME_NORM;
464
465 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
466
467 if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, alsa_vol)) < 0)
468 goto fail;
469 }
470
471 return 0;
472
473 fail:
474 pa_log_error("Unable to set volume: %s", snd_strerror(err));
475
476 s->get_volume = NULL;
477 s->set_volume = NULL;
478 return -1;
479 }
480
481 static int sink_get_mute_cb(pa_sink *s) {
482 struct userdata *u = s->userdata;
483 int err, sw;
484
485 pa_assert(u);
486 pa_assert(u->mixer_elem);
487
488 if ((err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw)) < 0) {
489 pa_log_error("Unable to get switch: %s", snd_strerror(err));
490
491 s->get_mute = NULL;
492 s->set_mute = NULL;
493 return -1;
494 }
495
496 s->muted = !sw;
497
498 return 0;
499 }
500
501 static int sink_set_mute_cb(pa_sink *s) {
502 struct userdata *u = s->userdata;
503 int err;
504
505 pa_assert(u);
506 pa_assert(u->mixer_elem);
507
508 if ((err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->muted)) < 0) {
509 pa_log_error("Unable to set switch: %s", snd_strerror(err));
510
511 s->get_mute = NULL;
512 s->set_mute = NULL;
513 return -1;
514 }
515
516 return 0;
517 }
518
519 static void thread_func(void *userdata) {
520
521 struct userdata *u = userdata;
522 unsigned short revents = 0;
523 snd_pcm_status_t *status;
524 int err;
525
526 pa_assert(u);
527 snd_pcm_status_alloca(&status);
528
529 pa_log_debug("Thread starting up");
530
531 if (build_pollfd(u) < 0)
532 goto fail;
533
534 for (;;) {
535 pa_msgobject *object;
536 int code;
537 void *data;
538 pa_memchunk chunk;
539 int64_t offset;
540 int r;
541
542 /* pa_log("loop"); */
543
544 /* Check whether there is a message for us to process */
545 if (pa_asyncmsgq_get(u->asyncmsgq, &object, &code, &data, &offset, &chunk, 0) == 0) {
546 int ret;
547
548 /* pa_log("processing msg"); */
549
550 if (!object && code == PA_MESSAGE_SHUTDOWN) {
551 pa_asyncmsgq_done(u->asyncmsgq, 0);
552 goto finish;
553 }
554
555 ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
556 pa_asyncmsgq_done(u->asyncmsgq, ret);
557 continue;
558 }
559
560 /* pa_log("loop2"); */
561
562 /* Render some data and write it to the dsp */
563
564 if (PA_SINK_OPENED(u->sink->thread_info.state) && ((revents & POLLOUT) || u->first == 1)) {
565 int work_done = 0;
566 pa_assert(u->pcm_handle);
567
568 if (u->use_mmap) {
569
570 if ((work_done = mmap_write(u)) < 0)
571 goto fail;
572
573 } else {
574
575 for (;;) {
576 void *p;
577 snd_pcm_sframes_t t;
578 ssize_t l;
579
580 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
581 pa_log("Failed to query DSP status data: %s", snd_strerror(t));
582 goto fail;
583 }
584
585 if (snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size)
586 pa_log_debug("Buffer underrun!");
587
588 l = snd_pcm_status_get_avail(status) * u->frame_size;
589
590 /* pa_log("%u bytes to write", l); */
591
592 if (l <= 0)
593 break;
594
595 if (u->memchunk.length <= 0)
596 pa_sink_render(u->sink, l, &u->memchunk);
597
598 pa_assert(u->memchunk.length > 0);
599
600 p = pa_memblock_acquire(u->memchunk.memblock);
601 t = snd_pcm_writei(u->pcm_handle, (const uint8_t*) p + u->memchunk.index, u->memchunk.length / u->frame_size);
602 pa_memblock_release(u->memchunk.memblock);
603
604 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
605
606 pa_assert(t != 0);
607
608 if (t < 0) {
609
610 if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
611 continue;
612
613 if (t == -EAGAIN) {
614 pa_log_debug("EAGAIN");
615 break;
616 } else {
617 pa_log("Failed to write data to DSP: %s", snd_strerror(t));
618 goto fail;
619 }
620 }
621
622 u->memchunk.index += t * u->frame_size;
623 u->memchunk.length -= t * u->frame_size;
624
625 if (u->memchunk.length <= 0) {
626 pa_memblock_unref(u->memchunk.memblock);
627 pa_memchunk_reset(&u->memchunk);
628 }
629
630 work_done = 1;
631
632 if (t * u->frame_size >= (unsigned) l)
633 break;
634 }
635 }
636
637 revents &= ~POLLOUT;
638
639 if (work_done) {
640
641 if (u->first) {
642 pa_log_info("Starting playback.");
643 snd_pcm_start(u->pcm_handle);
644 u->first = 0;
645 }
646
647 continue;
648 }
649 }
650
651 /* Hmm, nothing to do. Let's sleep */
652 if (pa_asyncmsgq_before_poll(u->asyncmsgq) < 0)
653 continue;
654
655 /* pa_log("polling for %i", POLLFD_ALSA_BASE + (PA_SINK_OPENED(u->sink->thread_info.state) ? u->n_alsa_fds : 0)); */
656 r = poll(u->pollfd, POLLFD_ALSA_BASE + (PA_SINK_OPENED(u->sink->thread_info.state) ? u->n_alsa_fds : 0), -1);
657 /* pa_log("poll end"); */
658
659 pa_asyncmsgq_after_poll(u->asyncmsgq);
660
661 if (r < 0) {
662 if (errno == EINTR) {
663 u->pollfd[POLLFD_ASYNCQ].revents = 0;
664 revents = 0;
665 continue;
666 }
667
668 pa_log("poll() failed: %s", pa_cstrerror(errno));
669 goto fail;
670 }
671
672 pa_assert(r > 0);
673
674 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
675 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, u->pollfd + POLLFD_ALSA_BASE, u->n_alsa_fds, &revents)) < 0) {
676 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
677 goto fail;
678 }
679
680 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
681 if (revents & POLLERR)
682 pa_log_warn("Got POLLERR from ALSA");
683 if (revents & POLLNVAL)
684 pa_log_warn("Got POLLNVAL from ALSA");
685 if (revents & POLLHUP)
686 pa_log_warn("Got POLLHUP from ALSA");
687
688 goto fail;
689 }
690 /* pa_log("got alsa event"); */
691 } else
692 revents = 0;
693
694 pa_assert((u->pollfd[POLLFD_ASYNCQ].revents & ~POLLIN) == 0);
695 }
696
697 fail:
698 /* We have to continue processing messages until we receive the
699 * SHUTDOWN message */
700 pa_asyncmsgq_post(u->core->asyncmsgq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
701 pa_asyncmsgq_wait_for(u->asyncmsgq, PA_MESSAGE_SHUTDOWN);
702
703 finish:
704 pa_log_debug("Thread shutting down");
705 }
706
707 int pa__init(pa_core *c, pa_module*m) {
708
709 pa_modargs *ma = NULL;
710 int ret = -1;
711 struct userdata *u = NULL;
712 const char *dev;
713 pa_sample_spec ss;
714 pa_channel_map map;
715 uint32_t nfrags, frag_size;
716 snd_pcm_uframes_t period_size;
717 size_t frame_size;
718 snd_pcm_info_t *pcm_info = NULL;
719 int err;
720 char *t;
721 const char *name;
722 char *name_buf = NULL;
723 int namereg_fail;
724 int use_mmap = 1, b;
725
726 pa_assert(c);
727 pa_assert(m);
728
729 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
730 pa_log("Failed to parse module arguments");
731 goto fail;
732 }
733
734 ss = c->default_sample_spec;
735 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
736 pa_log("Failed to parse sample specification and channel map");
737 goto fail;
738 }
739
740 frame_size = pa_frame_size(&ss);
741
742 nfrags = DEFAULT_NFRAGS;
743 frag_size = pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC*1000, &ss);
744 if (frag_size <= 0)
745 frag_size = frame_size;
746
747 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
748 pa_log("Failed to parse buffer metrics");
749 goto fail;
750 }
751 period_size = frag_size/frame_size;
752
753 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
754 pa_log("Failed to parse mmap argument.");
755 goto fail;
756 }
757
758 u = pa_xnew0(struct userdata, 1);
759 u->core = c;
760 u->module = m;
761 m->userdata = u;
762 u->use_mmap = use_mmap;
763 u->first = 1;
764 u->n_alsa_fds = 0;
765 u->pollfd = NULL;
766 pa_assert_se(u->asyncmsgq = pa_asyncmsgq_new(0));
767
768 snd_config_update_free_global();
769 if ((err = snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
770 pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
771 goto fail;
772 }
773
774 u->device_name = pa_xstrdup(dev);
775
776 if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
777 (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
778 pa_log("Error fetching PCM info: %s", snd_strerror(err));
779 goto fail;
780 }
781
782 b = use_mmap;
783 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
784 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
785 goto fail;
786 }
787
788 if (use_mmap && !b) {
789 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
790 u->use_mmap = use_mmap = b;
791 }
792
793 if (u->use_mmap)
794 pa_log_info("Successfully enabled mmap() mode.");
795
796 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
797 pa_log("Failed to set software parameters: %s", snd_strerror(err));
798 goto fail;
799 }
800
801 /* ALSA might tweak the sample spec, so recalculate the frame size */
802 frame_size = pa_frame_size(&ss);
803
804 if (ss.channels != map.channels)
805 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
806 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
807
808 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
809 pa_log_warn("Error opening mixer: %s", snd_strerror(err));
810 else {
811
812 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
813 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Master", "PCM"))) {
814
815 snd_mixer_close(u->mixer_handle);
816 u->mixer_handle = NULL;
817 }
818 }
819
820 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
821 namereg_fail = 1;
822 else {
823 name = name_buf = pa_sprintf_malloc("alsa_output.%s", dev);
824 namereg_fail = 0;
825 }
826
827 u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map);
828 pa_xfree(name_buf);
829
830 if (!u->sink) {
831 pa_log("Failed to create sink object");
832 goto fail;
833 }
834
835 u->sink->parent.process_msg = sink_process_msg;
836 u->sink->userdata = u;
837
838 pa_sink_set_module(u->sink, m);
839 pa_sink_set_asyncmsgq(u->sink, u->asyncmsgq);
840 pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
841 "ALSA PCM on %s (%s)",
842 dev,
843 snd_pcm_info_get_name(pcm_info)));
844 pa_xfree(t);
845
846 u->sink->is_hardware = 1;
847
848 u->frame_size = frame_size;
849 u->fragment_size = frag_size = period_size * frame_size;
850 u->nfragments = nfrags;
851 u->hwbuf_size = u->fragment_size * nfrags;
852
853 pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
854
855 pa_memchunk_reset(&u->memchunk);
856
857 if (u->mixer_handle) {
858 /* Initialize mixer code */
859
860 pa_assert(u->mixer_elem);
861
862 if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
863 int i;
864
865 for (i = 0;i < ss.channels; i++) {
866 if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i))
867 break;
868 }
869
870 if (i == ss.channels) {
871 u->sink->get_volume = sink_get_volume_cb;
872 u->sink->set_volume = sink_set_volume_cb;
873 snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
874 }
875 }
876
877 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
878 u->sink->get_mute = sink_get_mute_cb;
879 u->sink->set_mute = sink_set_mute_cb;
880 }
881
882 u->mixer_fdl = pa_alsa_fdlist_new();
883
884 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
885 pa_log("failed to initialise file descriptor monitoring");
886 goto fail;
887 }
888
889 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
890 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
891 } else
892 u->mixer_fdl = NULL;
893
894 if (!(u->thread = pa_thread_new(thread_func, u))) {
895 pa_log("Failed to create thread.");
896 goto fail;
897 }
898
899 /* Get initial mixer settings */
900 if (u->sink->get_volume)
901 u->sink->get_volume(u->sink);
902 if (u->sink->get_mute)
903 u->sink->get_mute(u->sink);
904
905 ret = 0;
906
907 finish:
908
909 if (ma)
910 pa_modargs_free(ma);
911
912 if (pcm_info)
913 snd_pcm_info_free(pcm_info);
914
915 return ret;
916
917 fail:
918
919 if (u)
920 pa__done(c, m);
921
922 goto finish;
923 }
924
925 void pa__done(pa_core *c, pa_module*m) {
926 struct userdata *u;
927
928 pa_assert(c);
929 pa_assert(m);
930
931 if (!(u = m->userdata))
932 return;
933
934 if (u->sink)
935 pa_sink_disconnect(u->sink);
936
937 if (u->thread) {
938 pa_asyncmsgq_send(u->asyncmsgq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
939 pa_thread_free(u->thread);
940 }
941
942 if (u->asyncmsgq)
943 pa_asyncmsgq_free(u->asyncmsgq);
944
945 if (u->sink)
946 pa_sink_unref(u->sink);
947
948 if (u->memchunk.memblock)
949 pa_memblock_unref(u->memchunk.memblock);
950
951 if (u->mixer_fdl)
952 pa_alsa_fdlist_free(u->mixer_fdl);
953
954 if (u->mixer_handle)
955 snd_mixer_close(u->mixer_handle);
956
957 if (u->pcm_handle) {
958 snd_pcm_drop(u->pcm_handle);
959 snd_pcm_close(u->pcm_handle);
960 }
961
962 pa_xfree(u->pollfd);
963 pa_xfree(u->device_name);
964 pa_xfree(u);
965
966 snd_config_update_free_global();
967 }
968