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