]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-sink.c
call snd_pcm_hwsync() expclicitly before we access any of the status fields, since...
[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 #include <asoundlib.h>
32
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
35 #include <pulse/timeval.h>
36
37 #include <pulsecore/core.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/memchunk.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/sample-util.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/macro.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/rtclock.h>
51 #include <pulsecore/time-smoother.h>
52
53 #include "alsa-util.h"
54 #include "module-alsa-sink-symdef.h"
55
56 PA_MODULE_AUTHOR("Lennart Poettering");
57 PA_MODULE_DESCRIPTION("ALSA Sink");
58 PA_MODULE_VERSION(PACKAGE_VERSION);
59 PA_MODULE_LOAD_ONCE(FALSE);
60 PA_MODULE_USAGE(
61 "sink_name=<name for the sink> "
62 "device=<ALSA device> "
63 "device_id=<ALSA device id> "
64 "format=<sample format> "
65 "rate=<sample rate> "
66 "channels=<number of channels> "
67 "channel_map=<channel map> "
68 "fragments=<number of fragments> "
69 "fragment_size=<fragment size> "
70 "mmap=<enable memory mapping?> "
71 "tsched=<enable system timer based scheduling mode?> "
72 "tsched_buffer_size=<buffer size when using timer based scheduling> "
73 "tsched_buffer_watermark=<lower fill watermark>");
74
75 #define DEFAULT_DEVICE "default"
76 #define DEFAULT_TSCHED_BUFFER_USEC (2*PA_USEC_PER_SEC)
77 #define DEFAULT_TSCHED_WATERMARK_USEC (10*PA_USEC_PER_MSEC)
78
79 struct userdata {
80 pa_core *core;
81 pa_module *module;
82 pa_sink *sink;
83
84 pa_thread *thread;
85 pa_thread_mq thread_mq;
86 pa_rtpoll *rtpoll;
87
88 snd_pcm_t *pcm_handle;
89
90 pa_alsa_fdlist *mixer_fdl;
91 snd_mixer_t *mixer_handle;
92 snd_mixer_elem_t *mixer_elem;
93 long hw_volume_max, hw_volume_min;
94 long hw_dB_max, hw_dB_min;
95 pa_bool_t hw_dB_supported;
96
97 size_t frame_size, fragment_size, hwbuf_size, tsched_watermark;
98 unsigned nfragments;
99 pa_memchunk memchunk;
100
101 char *device_name;
102
103 pa_bool_t use_mmap, use_tsched;
104
105 pa_bool_t first;
106
107 pa_rtpoll_item *alsa_rtpoll_item;
108
109 snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
110
111 pa_smoother *smoother;
112 int64_t frame_index;
113
114 snd_pcm_sframes_t hwbuf_unused_frames;
115 };
116
117 static const char* const valid_modargs[] = {
118 "sink_name",
119 "device",
120 "device_id",
121 "format",
122 "rate",
123 "channels",
124 "channel_map",
125 "fragments",
126 "fragment_size",
127 "mmap",
128 "tsched",
129 "tsched_buffer_size",
130 "tsched_buffer_watermark",
131 NULL
132 };
133
134 static int mmap_write(struct userdata *u) {
135 int work_done = 0;
136
137 pa_assert(u);
138 pa_sink_assert_ref(u->sink);
139
140 for (;;) {
141 pa_memchunk chunk;
142 void *p;
143 snd_pcm_sframes_t n;
144 int err;
145 const snd_pcm_channel_area_t *areas;
146 snd_pcm_uframes_t offset, frames;
147
148 snd_pcm_hwsync(u->pcm_handle);
149
150 /* First we determine how many samples are missing to fill the
151 * buffer up to 100% */
152
153 if (PA_UNLIKELY((n = snd_pcm_avail_update(u->pcm_handle)) < 0)) {
154
155 pa_log_debug("snd_pcm_avail_update: %s", snd_strerror(n));
156
157 if (err == -EAGAIN) {
158 pa_log_debug("EAGAIN");
159 return work_done;
160 }
161
162 if (n == -EPIPE)
163 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
164
165 if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0) {
166 u->first = TRUE;
167 continue;
168 }
169
170 pa_log("snd_pcm_recover: %s", snd_strerror(err));
171 return -1;
172 }
173
174 /* We only use part of the buffer that matches our
175 * dynamically requested latency */
176
177 if (PA_UNLIKELY(n <= u->hwbuf_unused_frames))
178 return work_done;
179
180 frames = n = n - u->hwbuf_unused_frames;
181
182 if (PA_UNLIKELY((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0)) {
183
184 pa_log_debug("snd_pcm_mmap_begin: %s", snd_strerror(err));
185
186 if (err == -EAGAIN) {
187 pa_log_debug("EAGAIN");
188 return work_done;
189 }
190
191 if (err == -EPIPE)
192 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
193
194 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0) {
195 u->first = TRUE;
196 continue;
197 }
198
199 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
200 return -1;
201 }
202
203 /* Check these are multiples of 8 bit */
204 pa_assert((areas[0].first & 7) == 0);
205 pa_assert((areas[0].step & 7)== 0);
206
207 /* We assume a single interleaved memory buffer */
208 pa_assert((areas[0].first >> 3) == 0);
209 pa_assert((areas[0].step >> 3) == u->frame_size);
210
211 p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
212
213 chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, 1);
214 chunk.length = pa_memblock_get_length(chunk.memblock);
215 chunk.index = 0;
216
217 pa_sink_render_into_full(u->sink, &chunk);
218
219 /* FIXME: Maybe we can do something to keep this memory block
220 * a little bit longer around? */
221 pa_memblock_unref_fixed(chunk.memblock);
222
223 if (PA_UNLIKELY((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0)) {
224
225 pa_log_debug("snd_pcm_mmap_commit: %s", snd_strerror(err));
226
227 if (err == -EAGAIN) {
228 pa_log_debug("EAGAIN");
229 return work_done;
230 }
231
232 if (err == -EPIPE)
233 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
234
235 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0) {
236 u->first = TRUE;
237 continue;
238 }
239
240 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
241 return -1;
242 }
243
244 work_done = 1;
245
246 u->frame_index += frames;
247
248 pa_log_debug("wrote %llu frames", (unsigned long long) frames);
249
250 if (PA_LIKELY(frames >= (snd_pcm_uframes_t) n))
251 return work_done;
252 }
253 }
254
255 static int unix_write(struct userdata *u) {
256 snd_pcm_status_t *status;
257 int work_done = 0;
258
259 snd_pcm_status_alloca(&status);
260
261 pa_assert(u);
262 pa_sink_assert_ref(u->sink);
263
264 for (;;) {
265 void *p;
266 snd_pcm_sframes_t n, frames;
267 int err;
268
269 snd_pcm_hwsync(u->pcm_handle);
270
271 if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0)) {
272 pa_log("Failed to query DSP status data: %s", snd_strerror(err));
273 return -1;
274 }
275
276 if (PA_UNLIKELY(snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size))
277 pa_log_debug("Buffer underrun!");
278
279 n = snd_pcm_status_get_avail(status);
280
281 /* We only use part of the buffer that matches our
282 * dynamically requested latency */
283
284 if (PA_UNLIKELY(n <= u->hwbuf_unused_frames))
285 return work_done;
286
287 n -= u->hwbuf_unused_frames;
288
289 if (u->memchunk.length <= 0)
290 pa_sink_render(u->sink, n * u->frame_size, &u->memchunk);
291
292 pa_assert(u->memchunk.length > 0);
293
294 frames = u->memchunk.length / u->frame_size;
295
296 if (frames > n)
297 frames = n;
298
299 p = pa_memblock_acquire(u->memchunk.memblock);
300 frames = snd_pcm_writei(u->pcm_handle, (const uint8_t*) p + u->memchunk.index, frames);
301 pa_memblock_release(u->memchunk.memblock);
302
303 pa_assert(frames != 0);
304
305 if (PA_UNLIKELY(frames < 0)) {
306
307 if (frames == -EAGAIN) {
308 pa_log_debug("EAGAIN");
309 return work_done;
310 }
311
312 if (frames == -EPIPE)
313 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
314
315 if ((frames = snd_pcm_recover(u->pcm_handle, frames, 1)) == 0) {
316 u->first = TRUE;
317 continue;
318 }
319
320 pa_log("Failed to write data to DSP: %s", snd_strerror(frames));
321 return -1;
322 }
323
324 u->memchunk.index += frames * u->frame_size;
325 u->memchunk.length -= frames * u->frame_size;
326
327 if (u->memchunk.length <= 0) {
328 pa_memblock_unref(u->memchunk.memblock);
329 pa_memchunk_reset(&u->memchunk);
330 }
331
332 work_done = 1;
333
334 u->frame_index += frames;
335
336 if (PA_LIKELY(frames >= n))
337 return work_done;
338 }
339 }
340
341 static void update_smoother(struct userdata *u) {
342 snd_pcm_sframes_t delay = 0;
343 int64_t frames;
344 int err;
345 pa_usec_t now1, now2;
346
347 pa_assert(u);
348 pa_assert(u->pcm_handle);
349
350 /* Let's update the time smoother */
351
352 snd_pcm_hwsync(u->pcm_handle);
353 snd_pcm_avail_update(u->pcm_handle);
354
355 if (PA_UNLIKELY((err = snd_pcm_delay(u->pcm_handle, &delay)) < 0)) {
356 pa_log_warn("Failed to get delay: %s", snd_strerror(err));
357 return;
358 }
359
360 frames = u->frame_index - delay;
361
362 pa_log_debug("frame_index = %llu, delay = %llu, p = %llu", (unsigned long long) u->frame_index, (unsigned long long) delay, (unsigned long long) frames);
363
364 now1 = pa_rtclock_usec();
365 now2 = pa_bytes_to_usec(frames * u->frame_size, &u->sink->sample_spec);
366 pa_smoother_put(u->smoother, now1, now2);
367 }
368
369 static pa_usec_t sink_get_latency(struct userdata *u) {
370 pa_usec_t r = 0;
371 int64_t delay;
372
373 pa_assert(u);
374
375 delay = u->frame_index - pa_smoother_get(u->smoother, pa_rtclock_usec());
376
377 if (delay > 0)
378 r = pa_bytes_to_usec(delay * u->frame_size, &u->sink->sample_spec);
379
380 if (u->memchunk.memblock)
381 r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
382
383 return r;
384 }
385
386 static int build_pollfd(struct userdata *u) {
387 int err;
388 struct pollfd *pollfd;
389 int n;
390
391 pa_assert(u);
392 pa_assert(u->pcm_handle);
393
394 if ((n = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
395 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
396 return -1;
397 }
398
399 if (u->alsa_rtpoll_item)
400 pa_rtpoll_item_free(u->alsa_rtpoll_item);
401
402 u->alsa_rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, n);
403 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, NULL);
404
405 if ((err = snd_pcm_poll_descriptors(u->pcm_handle, pollfd, n)) < 0) {
406 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
407 return -1;
408 }
409
410 return 0;
411 }
412
413 static int suspend(struct userdata *u) {
414 pa_assert(u);
415 pa_assert(u->pcm_handle);
416
417 pa_smoother_pause(u->smoother, pa_rtclock_usec());
418
419 /* Let's suspend */
420 snd_pcm_drain(u->pcm_handle);
421 snd_pcm_close(u->pcm_handle);
422 u->pcm_handle = NULL;
423
424 if (u->alsa_rtpoll_item) {
425 pa_rtpoll_item_free(u->alsa_rtpoll_item);
426 u->alsa_rtpoll_item = NULL;
427 }
428
429 pa_log_info("Device suspended...");
430
431 return 0;
432 }
433
434 static pa_usec_t hw_sleep_time(struct userdata *u) {
435 pa_usec_t usec, wm;
436
437 pa_assert(u);
438
439 usec = pa_sink_get_requested_latency(u->sink);
440
441 if (usec <= 0)
442 usec = pa_bytes_to_usec(u->hwbuf_size, &u->sink->sample_spec);
443
444 pa_log_debug("hw buffer time: %u ms", (unsigned) (usec / PA_USEC_PER_MSEC));
445
446 wm = pa_bytes_to_usec(u->tsched_watermark, &u->sink->sample_spec);
447
448 if (usec >= wm)
449 usec -= wm;
450 else
451 usec /= 2;
452
453 pa_log_debug("after watermark: %u ms", (unsigned) (usec / PA_USEC_PER_MSEC));
454
455 return usec;
456 }
457
458 static void update_hwbuf_unused_frames(struct userdata *u) {
459 pa_usec_t usec;
460 size_t b;
461
462 pa_assert(u);
463
464 if ((usec = pa_sink_get_requested_latency(u->sink)) <= 0) {
465 /* Use the full buffer if noone asked us for anything
466 * specific */
467 u->hwbuf_unused_frames = 0;
468 return;
469 }
470
471 b = pa_usec_to_bytes(usec, &u->sink->sample_spec);
472
473 /* We need at least one sample in our buffer */
474
475 if (PA_UNLIKELY(b < u->frame_size))
476 b = u->frame_size;
477
478 u->hwbuf_unused_frames =
479 PA_LIKELY(b < u->hwbuf_size) ?
480 ((u->hwbuf_size - b) / u->frame_size) : 0;
481 }
482
483 static int update_sw_params(struct userdata *u) {
484 snd_pcm_uframes_t avail_min;
485 int err;
486
487 pa_assert(u);
488
489 if (u->use_tsched) {
490 pa_usec_t usec;
491
492 usec = hw_sleep_time(u);
493
494 avail_min = pa_usec_to_bytes(usec, &u->sink->sample_spec) / u->frame_size;
495
496 if (avail_min <= 0)
497 avail_min = 1;
498
499 } else
500 avail_min = 1;
501
502 avail_min = (snd_pcm_uframes_t) -1;
503
504 pa_log("setting avail_min=%lu", (unsigned long) avail_min);
505
506 if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) {
507 pa_log("Failed to set software parameters: %s", snd_strerror(err));
508 return err;
509 }
510
511 update_hwbuf_unused_frames(u);
512
513 pa_log("hwbuf_unused_frames=%lu", (unsigned long) u->hwbuf_unused_frames);
514
515 return 0;
516 }
517
518 static int unsuspend(struct userdata *u) {
519 pa_sample_spec ss;
520 int err;
521 pa_bool_t b, d;
522 unsigned nfrags;
523 snd_pcm_uframes_t period_size;
524
525 pa_assert(u);
526 pa_assert(!u->pcm_handle);
527
528 pa_log_info("Trying resume...");
529
530 snd_config_update_free_global();
531 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
532 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
533 goto fail;
534 }
535
536 ss = u->sink->sample_spec;
537 nfrags = u->nfragments;
538 period_size = u->fragment_size / u->frame_size;
539 b = u->use_mmap;
540 d = u->use_tsched;
541
542 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) {
543 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
544 goto fail;
545 }
546
547 if (b != u->use_mmap || d != u->use_tsched) {
548 pa_log_warn("Resume failed, couldn't get original access mode.");
549 goto fail;
550 }
551
552 if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
553 pa_log_warn("Resume failed, couldn't restore original sample settings.");
554 goto fail;
555 }
556
557 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
558 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
559 goto fail;
560 }
561
562 if (update_sw_params(u) < 0)
563 goto fail;
564
565 if (build_pollfd(u) < 0)
566 goto fail;
567
568 /* FIXME: We need to reload the volume somehow */
569
570 u->first = TRUE;
571
572 pa_log_info("Resumed successfully...");
573
574 return 0;
575
576 fail:
577 if (u->pcm_handle) {
578 snd_pcm_close(u->pcm_handle);
579 u->pcm_handle = NULL;
580 }
581
582 return -1;
583 }
584
585 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
586 struct userdata *u = PA_SINK(o)->userdata;
587
588 switch (code) {
589
590 case PA_SINK_MESSAGE_GET_LATENCY: {
591 pa_usec_t r = 0;
592
593 if (u->pcm_handle)
594 r = sink_get_latency(u);
595
596 *((pa_usec_t*) data) = r;
597
598 return 0;
599 }
600
601 case PA_SINK_MESSAGE_SET_STATE:
602
603 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
604
605 case PA_SINK_SUSPENDED:
606 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
607
608 if (suspend(u) < 0)
609 return -1;
610
611 break;
612
613 case PA_SINK_IDLE:
614 case PA_SINK_RUNNING:
615
616 if (u->sink->thread_info.state == PA_SINK_INIT) {
617 if (build_pollfd(u) < 0)
618 return -1;
619 }
620
621 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
622 if (unsuspend(u) < 0)
623 return -1;
624 }
625
626 break;
627
628 case PA_SINK_UNLINKED:
629 case PA_SINK_INIT:
630 ;
631 }
632
633 break;
634
635 /* case PA_SINK_MESSAGE_ADD_INPUT: */
636 /* case PA_SINK_MESSAGE_REMOVE_INPUT: */
637 /* case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: { */
638 /* int r = pa_sink_process_msg(o, code, data, offset, chunk); */
639 /* update_hwbuf_unused_frames(u); */
640 /* return r; */
641 /* } */
642 }
643
644 return pa_sink_process_msg(o, code, data, offset, chunk);
645 }
646
647 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
648 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
649
650 pa_assert(u);
651 pa_assert(u->mixer_handle);
652
653 if (mask == SND_CTL_EVENT_MASK_REMOVE)
654 return 0;
655
656 if (mask & SND_CTL_EVENT_MASK_VALUE) {
657 pa_sink_get_volume(u->sink);
658 pa_sink_get_mute(u->sink);
659 }
660
661 return 0;
662 }
663
664 static int sink_get_volume_cb(pa_sink *s) {
665 struct userdata *u = s->userdata;
666 int err;
667 int i;
668
669 pa_assert(u);
670 pa_assert(u->mixer_elem);
671
672 for (i = 0; i < s->sample_spec.channels; i++) {
673 long alsa_vol;
674
675 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
676
677 if (u->hw_dB_supported) {
678
679 if ((err = snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) >= 0) {
680 s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
681 continue;
682 }
683
684 u->hw_dB_supported = FALSE;
685 }
686
687 if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
688 goto fail;
689
690 s->volume.values[i] = (pa_volume_t) roundf(((float) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
691 }
692
693 return 0;
694
695 fail:
696 pa_log_error("Unable to read volume: %s", snd_strerror(err));
697
698 return -1;
699 }
700
701 static int sink_set_volume_cb(pa_sink *s) {
702 struct userdata *u = s->userdata;
703 int err;
704 int i;
705
706 pa_assert(u);
707 pa_assert(u->mixer_elem);
708
709 for (i = 0; i < s->sample_spec.channels; i++) {
710 long alsa_vol;
711 pa_volume_t vol;
712
713 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
714
715 vol = PA_MIN(s->volume.values[i], PA_VOLUME_NORM);
716
717 if (u->hw_dB_supported) {
718 alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100);
719 alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max);
720
721 if ((err = snd_mixer_selem_set_playback_dB(u->mixer_elem, u->mixer_map[i], alsa_vol, -1)) >= 0) {
722
723 if (snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
724 s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
725
726 continue;
727 }
728
729 u->hw_dB_supported = FALSE;
730
731 }
732
733 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
734 alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_volume_min, u->hw_volume_max);
735
736 if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0)
737 goto fail;
738
739 if (snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
740 s->volume.values[i] = (pa_volume_t) roundf(((float) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
741 }
742
743 return 0;
744
745 fail:
746 pa_log_error("Unable to set volume: %s", snd_strerror(err));
747
748 return -1;
749 }
750
751 static int sink_get_mute_cb(pa_sink *s) {
752 struct userdata *u = s->userdata;
753 int err, sw;
754
755 pa_assert(u);
756 pa_assert(u->mixer_elem);
757
758 if ((err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw)) < 0) {
759 pa_log_error("Unable to get switch: %s", snd_strerror(err));
760 return -1;
761 }
762
763 s->muted = !sw;
764
765 return 0;
766 }
767
768 static int sink_set_mute_cb(pa_sink *s) {
769 struct userdata *u = s->userdata;
770 int err;
771
772 pa_assert(u);
773 pa_assert(u->mixer_elem);
774
775 if ((err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->muted)) < 0) {
776 pa_log_error("Unable to set switch: %s", snd_strerror(err));
777 return -1;
778 }
779
780 return 0;
781 }
782
783 static void sink_update_requested_latency_cb(pa_sink *s) {
784 struct userdata *u = s->userdata;
785
786 pa_assert(u);
787
788 update_sw_params(u);
789 }
790
791 static void thread_func(void *userdata) {
792 struct userdata *u = userdata;
793
794 pa_assert(u);
795
796 pa_log_debug("Thread starting up");
797
798 if (u->core->realtime_scheduling)
799 pa_make_realtime(u->core->realtime_priority);
800
801 pa_thread_mq_install(&u->thread_mq);
802 pa_rtpoll_install(u->rtpoll);
803
804 /* update_hwbuf_unused_frames(u); */
805
806 for (;;) {
807 int ret;
808
809 pa_log_debug("loop");
810
811 /* Render some data and write it to the dsp */
812 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
813 int work_done = 0;
814
815 if (u->sink->thread_info.rewind_nbytes > 0 && u->use_tsched) {
816 snd_pcm_sframes_t frames, limit, unused;
817
818 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) u->sink->thread_info.rewind_nbytes);
819
820 frames = u->sink->thread_info.rewind_nbytes / u->frame_size;
821
822 snd_pcm_hwsync(u->pcm_handle);
823 if ((unused = snd_pcm_avail_update(u->pcm_handle)) < 0) {
824 pa_log("snd_pcm_avail_update() failed: %s", snd_strerror(unused));
825 goto fail;
826 }
827
828 limit = (u->hwbuf_size / u->frame_size) - unused;
829
830 if (frames > limit)
831 frames = limit;
832
833 frames = 0;
834
835 if (frames > 0) {
836
837 pa_log_debug("Limited to %lu bytes.", (unsigned long) frames * u->frame_size);
838
839 if ((frames = snd_pcm_rewind(u->pcm_handle, frames)) < 0) {
840 pa_log("snd_pcm_rewind() failed: %s", snd_strerror(frames));
841 goto fail;
842 }
843
844 if ((u->sink->thread_info.rewind_nbytes = frames * u->frame_size) <= 0)
845 pa_log_info("Tried rewind, but was apparently not possible.");
846 else {
847 u->frame_index -= frames;
848 pa_log_debug("Rewound %lu bytes.", (unsigned long) u->sink->thread_info.rewind_nbytes);
849 pa_sink_process_rewind(u->sink);
850 }
851 } else {
852 pa_log_debug("Mhmm, actually there is nothing to rewind.");
853 }
854 }
855
856 if (u->use_mmap) {
857 if ((work_done = mmap_write(u)) < 0)
858 goto fail;
859 } else {
860 if ((work_done = unix_write(u)) < 0)
861 goto fail;
862 }
863
864 pa_log_debug("work_done = %i", work_done);
865
866 if (work_done) {
867
868 if (u->first) {
869 pa_log_info("Starting playback.");
870 snd_pcm_start(u->pcm_handle);
871 u->first = FALSE;
872
873 pa_smoother_resume(u->smoother, pa_rtclock_usec());
874 }
875
876 update_smoother(u);
877 }
878
879 if (u->use_tsched) {
880 pa_usec_t usec, cusec;
881
882 /* OK, the playback buffer is now full, let's
883 * calculate when to wake up next */
884
885 usec = hw_sleep_time(u);
886
887 pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) usec / PA_USEC_PER_MSEC);
888
889 /* Convert from the sound card time domain to the
890 * system time domain */
891 cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), usec);
892
893 pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC);
894
895 /* We don't trust the conversion, so we wake up whatever comes first */
896 pa_rtpoll_set_timer_relative(u->rtpoll, PA_MIN(usec, cusec));
897 }
898
899 } else if (u->use_tsched)
900
901 /* OK, we're in an invalid state, let's disable our timers */
902 pa_rtpoll_set_timer_disabled(u->rtpoll);
903
904 /* Hmm, nothing to do. Let's sleep */
905 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
906 goto fail;
907
908 if (ret == 0)
909 goto finish;
910
911 /* Tell ALSA about this and process its response */
912 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
913 struct pollfd *pollfd;
914 unsigned short revents = 0;
915 int err;
916 unsigned n;
917
918 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
919
920 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
921 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
922 goto fail;
923 }
924
925 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
926 snd_pcm_state_t state;
927
928 if (revents & POLLERR)
929 pa_log_warn("Got POLLERR from ALSA");
930 if (revents & POLLNVAL)
931 pa_log_warn("Got POLLNVAL from ALSA");
932 if (revents & POLLHUP)
933 pa_log_warn("Got POLLHUP from ALSA");
934
935 state = snd_pcm_state(u->pcm_handle);
936 pa_log_warn("PCM state is %s", snd_pcm_state_name(state));
937
938 /* Try to recover from this error */
939
940 switch (state) {
941
942 case SND_PCM_STATE_XRUN:
943 if ((err = snd_pcm_recover(u->pcm_handle, -EPIPE, 1)) != 0) {
944 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
945 goto fail;
946 }
947 break;
948
949 case SND_PCM_STATE_SUSPENDED:
950 if ((err = snd_pcm_recover(u->pcm_handle, -ESTRPIPE, 1)) != 0) {
951 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
952 goto fail;
953 }
954 break;
955
956 default:
957
958 snd_pcm_drop(u->pcm_handle);
959
960 if ((err = snd_pcm_prepare(u->pcm_handle)) < 0) {
961 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
962 goto fail;
963 }
964 break;
965 }
966
967 u->first = TRUE;
968 }
969
970 pa_log_debug("alsa revents = %i", revents);
971 }
972 }
973
974 fail:
975 /* If this was no regular exit from the loop we have to continue
976 * processing messages until we received PA_MESSAGE_SHUTDOWN */
977 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
978 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
979
980 finish:
981 pa_log_debug("Thread shutting down");
982 }
983
984 int pa__init(pa_module*m) {
985
986 pa_modargs *ma = NULL;
987 struct userdata *u = NULL;
988 const char *dev_id;
989 pa_sample_spec ss;
990 pa_channel_map map;
991 uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark;
992 snd_pcm_uframes_t period_frames, tsched_frames;
993 size_t frame_size;
994 snd_pcm_info_t *pcm_info = NULL;
995 int err;
996 const char *name;
997 char *name_buf = NULL;
998 pa_bool_t namereg_fail;
999 pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d;
1000 pa_usec_t usec;
1001 pa_sink_new_data data;
1002 static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
1003 [SND_PCM_CLASS_GENERIC] = "sound",
1004 [SND_PCM_CLASS_MULTI] = NULL,
1005 [SND_PCM_CLASS_MODEM] = "modem",
1006 [SND_PCM_CLASS_DIGITIZER] = NULL
1007 };
1008
1009 snd_pcm_info_alloca(&pcm_info);
1010
1011 pa_assert(m);
1012
1013 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1014 pa_log("Failed to parse module arguments");
1015 goto fail;
1016 }
1017
1018 ss = m->core->default_sample_spec;
1019 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
1020 pa_log("Failed to parse sample specification and channel map");
1021 goto fail;
1022 }
1023
1024 frame_size = pa_frame_size(&ss);
1025
1026 nfrags = m->core->default_n_fragments;
1027 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
1028 if (frag_size <= 0)
1029 frag_size = frame_size;
1030 tsched_size = pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
1031 tsched_watermark = pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
1032
1033 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
1034 pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
1035 pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
1036 pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
1037 pa_log("Failed to parse buffer metrics");
1038 goto fail;
1039 }
1040
1041 hwbuf_size = frag_size * nfrags;
1042 period_frames = frag_size/frame_size;
1043 tsched_frames = tsched_size/frame_size;
1044
1045 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
1046 pa_log("Failed to parse mmap argument.");
1047 goto fail;
1048 }
1049
1050 if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
1051 pa_log("Failed to parse timer_scheduling argument.");
1052 goto fail;
1053 }
1054
1055 if (use_tsched && !pa_rtclock_hrtimer()) {
1056 pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
1057 use_tsched = FALSE;
1058 }
1059
1060 u = pa_xnew0(struct userdata, 1);
1061 u->core = m->core;
1062 u->module = m;
1063 m->userdata = u;
1064 u->use_mmap = use_mmap;
1065 u->use_tsched = use_tsched;
1066 u->first = TRUE;
1067 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
1068 u->rtpoll = pa_rtpoll_new();
1069 u->alsa_rtpoll_item = NULL;
1070 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
1071
1072 u->smoother = pa_smoother_new(DEFAULT_TSCHED_BUFFER_USEC*2, DEFAULT_TSCHED_BUFFER_USEC*2, TRUE);
1073 usec = pa_rtclock_usec();
1074 pa_smoother_set_time_offset(u->smoother, usec);
1075 pa_smoother_pause(u->smoother, usec);
1076
1077 snd_config_update_free_global();
1078
1079 b = use_mmap;
1080 d = use_tsched;
1081
1082 if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
1083
1084 if (!(u->pcm_handle = pa_alsa_open_by_device_id(
1085 dev_id,
1086 &u->device_name,
1087 &ss, &map,
1088 SND_PCM_STREAM_PLAYBACK,
1089 &nfrags, &period_frames, tsched_frames,
1090 &b, &d)))
1091
1092 goto fail;
1093
1094 } else {
1095
1096 if (!(u->pcm_handle = pa_alsa_open_by_device_string(
1097 pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
1098 &u->device_name,
1099 &ss, &map,
1100 SND_PCM_STREAM_PLAYBACK,
1101 &nfrags, &period_frames, tsched_frames,
1102 &b, &d)))
1103 goto fail;
1104
1105 }
1106
1107 pa_assert(u->device_name);
1108 pa_log_info("Successfully opened device %s.", u->device_name);
1109
1110 if (use_mmap && !b) {
1111 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1112 u->use_mmap = use_mmap = FALSE;
1113 }
1114
1115 if (use_tsched && (!b || !d)) {
1116 pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1117 u->use_tsched = use_tsched = FALSE;
1118 }
1119
1120 if (u->use_mmap)
1121 pa_log_info("Successfully enabled mmap() mode.");
1122
1123 if (u->use_tsched)
1124 pa_log_info("Successfully enabled timer-based scheduling mode.");
1125
1126 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
1127 pa_log("Error fetching PCM info: %s", snd_strerror(err));
1128 goto fail;
1129 }
1130
1131 /* ALSA might tweak the sample spec, so recalculate the frame size */
1132 frame_size = pa_frame_size(&ss);
1133
1134 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
1135 pa_log_warn("Error opening mixer: %s", snd_strerror(err));
1136 else {
1137 pa_bool_t found = FALSE;
1138
1139 if (pa_alsa_prepare_mixer(u->mixer_handle, u->device_name) >= 0)
1140 found = TRUE;
1141 else {
1142 snd_pcm_info_t *info;
1143
1144 snd_pcm_info_alloca(&info);
1145
1146 if (snd_pcm_info(u->pcm_handle, info) >= 0) {
1147 char *md;
1148 int card;
1149
1150 if ((card = snd_pcm_info_get_card(info)) >= 0) {
1151
1152 md = pa_sprintf_malloc("hw:%i", card);
1153
1154 if (strcmp(u->device_name, md))
1155 if (pa_alsa_prepare_mixer(u->mixer_handle, md) >= 0)
1156 found = TRUE;
1157 pa_xfree(md);
1158 }
1159 }
1160 }
1161
1162 if (found)
1163 if (!(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Master", "PCM")))
1164 found = FALSE;
1165
1166 if (!found) {
1167 snd_mixer_close(u->mixer_handle);
1168 u->mixer_handle = NULL;
1169 }
1170 }
1171
1172 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
1173 namereg_fail = TRUE;
1174 else {
1175 name = name_buf = pa_sprintf_malloc("alsa_output.%s", u->device_name);
1176 namereg_fail = FALSE;
1177 }
1178
1179 pa_sink_new_data_init(&data);
1180 data.driver = __FILE__;
1181 data.module = m;
1182 pa_sink_new_data_set_name(&data, name);
1183 data.namereg_fail = namereg_fail;
1184 pa_sink_new_data_set_sample_spec(&data, &ss);
1185 pa_sink_new_data_set_channel_map(&data, &map);
1186
1187 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
1188 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "alsa");
1189 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, snd_pcm_info_get_name(pcm_info));
1190
1191 if (class_table[snd_pcm_info_get_class(pcm_info)])
1192 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, class_table[snd_pcm_info_get_class(pcm_info)]);
1193
1194 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap_rewrite" : (u->use_mmap ? "mmap" : "serial"));
1195
1196 u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
1197 pa_sink_new_data_done(&data);
1198 pa_xfree(name_buf);
1199
1200 if (!u->sink) {
1201 pa_log("Failed to create sink object");
1202 goto fail;
1203 }
1204
1205 u->sink->parent.process_msg = sink_process_msg;
1206 u->sink->update_requested_latency = sink_update_requested_latency_cb;
1207 u->sink->userdata = u;
1208
1209 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1210 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1211
1212 u->frame_size = frame_size;
1213 u->fragment_size = frag_size = period_frames * frame_size;
1214 u->nfragments = nfrags;
1215 u->hwbuf_size = u->fragment_size * nfrags;
1216 u->hwbuf_unused_frames = 0;
1217 u->tsched_watermark = tsched_watermark;
1218 u->frame_index = 0;
1219 u->hw_dB_supported = FALSE;
1220 u->hw_dB_min = u->hw_dB_max = 0;
1221 u->hw_volume_min = u->hw_volume_max = 0;
1222
1223 u->sink->thread_info.max_rewind = use_tsched ? u->hwbuf_size : 0;
1224
1225 if (!use_tsched)
1226 u->sink->min_latency = pa_bytes_to_usec(u->hwbuf_size, &ss);
1227
1228 pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms",
1229 nfrags, (long unsigned) u->fragment_size,
1230 (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC);
1231
1232 if (use_tsched)
1233 pa_log_info("Time scheduling watermark is %0.2fms",
1234 (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC);
1235
1236 if (update_sw_params(u) < 0)
1237 goto fail;
1238
1239 pa_memchunk_reset(&u->memchunk);
1240
1241 if (u->mixer_handle) {
1242 pa_assert(u->mixer_elem);
1243
1244 if (snd_mixer_selem_has_playback_volume(u->mixer_elem))
1245
1246 if (pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, TRUE) >= 0 &&
1247 snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) >= 0) {
1248
1249 pa_bool_t suitable = TRUE;
1250
1251 pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
1252
1253 if (u->hw_volume_min > u->hw_volume_max) {
1254
1255 pa_log_info("Minimal volume %li larger than maximum volume %li. Strange stuff Falling back to software volume control.", u->hw_volume_min, u->hw_volume_max);
1256 suitable = FALSE;
1257
1258 } else if (u->hw_volume_max - u->hw_volume_min < 3) {
1259
1260 pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
1261 suitable = FALSE;
1262
1263 } else if (snd_mixer_selem_get_playback_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) >= 0) {
1264
1265 pa_log_info("Volume ranges from %0.2f dB to %0.2f dB.", u->hw_dB_min/100.0, u->hw_dB_max/100.0);
1266
1267 /* Let's see if this thing actually is useful for muting */
1268 if (u->hw_dB_min > -6000) {
1269 pa_log_info("Device cannot attenuate for more than -60 dB (only %0.2f dB supported), falling back to software volume control.", ((double) u->hw_dB_min) / 100);
1270
1271 suitable = FALSE;
1272 } else if (u->hw_dB_max < 0) {
1273
1274 pa_log_info("Device is still attenuated at maximum volume setting (%0.2f dB is maximum). Strange stuff. Falling back to software volume control.", ((double) u->hw_dB_max) / 100);
1275 suitable = FALSE;
1276
1277 } else if (u->hw_dB_min >= u->hw_dB_max) {
1278
1279 pa_log_info("Minimal dB (%0.2f) larger or equal to maximum dB (%0.2f). Strange stuff. Falling back to software volume control.", ((double) u->hw_dB_min) / 100, ((double) u->hw_dB_max) / 100);
1280 suitable = FALSE;
1281
1282 } else {
1283
1284 if (u->hw_dB_max > 0) {
1285 /* dB > 0 means overamplification, and clipping, we don't want that here */
1286 pa_log_info("Device can do overamplification for %0.2f dB. Limiting to 0 db", ((double) u->hw_dB_max) / 100);
1287 u->hw_dB_max = 0;
1288 }
1289
1290 u->hw_dB_supported = TRUE;
1291 }
1292 }
1293
1294 if (suitable) {
1295 u->sink->get_volume = sink_get_volume_cb;
1296 u->sink->set_volume = sink_set_volume_cb;
1297 u->sink->flags |= PA_SINK_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SINK_DECIBEL_VOLUME : 0);
1298 pa_log_info("Using hardware volume control. %s dB scale.", u->hw_dB_supported ? "Using" : "Not using");
1299
1300 } else {
1301 pa_log_info("Using software volume control. Trying to reset sound card to 0 dB.");
1302 pa_alsa_0dB_playback(u->mixer_elem);
1303 }
1304 }
1305
1306 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
1307 u->sink->get_mute = sink_get_mute_cb;
1308 u->sink->set_mute = sink_set_mute_cb;
1309 u->sink->flags |= PA_SINK_HW_MUTE_CTRL;
1310 }
1311
1312 u->mixer_fdl = pa_alsa_fdlist_new();
1313
1314 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
1315 pa_log("Failed to initialize file descriptor monitoring");
1316 goto fail;
1317 }
1318
1319 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
1320 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
1321 } else
1322 u->mixer_fdl = NULL;
1323
1324 pa_alsa_dump(u->pcm_handle);
1325
1326 if (!(u->thread = pa_thread_new(thread_func, u))) {
1327 pa_log("Failed to create thread.");
1328 goto fail;
1329 }
1330
1331 /* Get initial mixer settings */
1332 if (data.volume_is_set) {
1333 if (u->sink->set_volume)
1334 u->sink->set_volume(u->sink);
1335 } else {
1336 if (u->sink->get_volume)
1337 u->sink->get_volume(u->sink);
1338 }
1339
1340 if (data.muted_is_set) {
1341 if (u->sink->set_mute)
1342 u->sink->set_mute(u->sink);
1343 } else {
1344 if (u->sink->get_mute)
1345 u->sink->get_mute(u->sink);
1346 }
1347
1348 pa_sink_put(u->sink);
1349
1350 pa_modargs_free(ma);
1351
1352 return 0;
1353
1354 fail:
1355
1356 if (ma)
1357 pa_modargs_free(ma);
1358
1359 pa__done(m);
1360
1361 return -1;
1362 }
1363
1364 void pa__done(pa_module*m) {
1365 struct userdata *u;
1366
1367 pa_assert(m);
1368
1369 if (!(u = m->userdata))
1370 return;
1371
1372 if (u->sink)
1373 pa_sink_unlink(u->sink);
1374
1375 if (u->thread) {
1376 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1377 pa_thread_free(u->thread);
1378 }
1379
1380 pa_thread_mq_done(&u->thread_mq);
1381
1382 if (u->sink)
1383 pa_sink_unref(u->sink);
1384
1385 if (u->memchunk.memblock)
1386 pa_memblock_unref(u->memchunk.memblock);
1387
1388 if (u->alsa_rtpoll_item)
1389 pa_rtpoll_item_free(u->alsa_rtpoll_item);
1390
1391 if (u->rtpoll)
1392 pa_rtpoll_free(u->rtpoll);
1393
1394 if (u->mixer_fdl)
1395 pa_alsa_fdlist_free(u->mixer_fdl);
1396
1397 if (u->mixer_handle)
1398 snd_mixer_close(u->mixer_handle);
1399
1400 if (u->pcm_handle) {
1401 snd_pcm_drop(u->pcm_handle);
1402 snd_pcm_close(u->pcm_handle);
1403 }
1404
1405 if (u->smoother)
1406 pa_smoother_free(u->smoother);
1407
1408 pa_xfree(u->device_name);
1409 pa_xfree(u);
1410
1411 snd_config_update_free_global();
1412 }