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