]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-sink.c
init min/max latency properly; fix avail_min updating
[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_within_thread(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_within_thread(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 pa_log("setting avail_min=%lu", (unsigned long) avail_min);
503
504 if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) {
505 pa_log("Failed to set software parameters: %s", snd_strerror(err));
506 return err;
507 }
508
509 update_hwbuf_unused_frames(u);
510
511 pa_log("hwbuf_unused_frames=%lu", (unsigned long) u->hwbuf_unused_frames);
512
513 return 0;
514 }
515
516 static int unsuspend(struct userdata *u) {
517 pa_sample_spec ss;
518 int err;
519 pa_bool_t b, d;
520 unsigned nfrags;
521 snd_pcm_uframes_t period_size;
522
523 pa_assert(u);
524 pa_assert(!u->pcm_handle);
525
526 pa_log_info("Trying resume...");
527
528 snd_config_update_free_global();
529 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
530 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
531 goto fail;
532 }
533
534 ss = u->sink->sample_spec;
535 nfrags = u->nfragments;
536 period_size = u->fragment_size / u->frame_size;
537 b = u->use_mmap;
538 d = u->use_tsched;
539
540 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) {
541 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
542 goto fail;
543 }
544
545 if (b != u->use_mmap || d != u->use_tsched) {
546 pa_log_warn("Resume failed, couldn't get original access mode.");
547 goto fail;
548 }
549
550 if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
551 pa_log_warn("Resume failed, couldn't restore original sample settings.");
552 goto fail;
553 }
554
555 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
556 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
557 goto fail;
558 }
559
560 if (update_sw_params(u) < 0)
561 goto fail;
562
563 if (build_pollfd(u) < 0)
564 goto fail;
565
566 /* FIXME: We need to reload the volume somehow */
567
568 u->first = TRUE;
569
570 pa_log_info("Resumed successfully...");
571
572 return 0;
573
574 fail:
575 if (u->pcm_handle) {
576 snd_pcm_close(u->pcm_handle);
577 u->pcm_handle = NULL;
578 }
579
580 return -1;
581 }
582
583 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
584 struct userdata *u = PA_SINK(o)->userdata;
585
586 switch (code) {
587
588 case PA_SINK_MESSAGE_GET_LATENCY: {
589 pa_usec_t r = 0;
590
591 if (u->pcm_handle)
592 r = sink_get_latency(u);
593
594 *((pa_usec_t*) data) = r;
595
596 return 0;
597 }
598
599 case PA_SINK_MESSAGE_SET_STATE:
600
601 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
602
603 case PA_SINK_SUSPENDED:
604 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
605
606 if (suspend(u) < 0)
607 return -1;
608
609 break;
610
611 case PA_SINK_IDLE:
612 case PA_SINK_RUNNING:
613
614 if (u->sink->thread_info.state == PA_SINK_INIT) {
615 if (build_pollfd(u) < 0)
616 return -1;
617 }
618
619 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
620 if (unsuspend(u) < 0)
621 return -1;
622 }
623
624 break;
625
626 case PA_SINK_UNLINKED:
627 case PA_SINK_INIT:
628 ;
629 }
630
631 break;
632
633 /* case PA_SINK_MESSAGE_ADD_INPUT: */
634 /* case PA_SINK_MESSAGE_REMOVE_INPUT: */
635 /* case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: { */
636 /* int r = pa_sink_process_msg(o, code, data, offset, chunk); */
637 /* update_hwbuf_unused_frames(u); */
638 /* return r; */
639 /* } */
640 }
641
642 return pa_sink_process_msg(o, code, data, offset, chunk);
643 }
644
645 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
646 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
647
648 pa_assert(u);
649 pa_assert(u->mixer_handle);
650
651 if (mask == SND_CTL_EVENT_MASK_REMOVE)
652 return 0;
653
654 if (mask & SND_CTL_EVENT_MASK_VALUE) {
655 pa_sink_get_volume(u->sink);
656 pa_sink_get_mute(u->sink);
657 }
658
659 return 0;
660 }
661
662 static int sink_get_volume_cb(pa_sink *s) {
663 struct userdata *u = s->userdata;
664 int err;
665 int i;
666
667 pa_assert(u);
668 pa_assert(u->mixer_elem);
669
670 for (i = 0; i < s->sample_spec.channels; i++) {
671 long alsa_vol;
672
673 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
674
675 if (u->hw_dB_supported) {
676
677 if ((err = snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) >= 0) {
678 s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
679 continue;
680 }
681
682 u->hw_dB_supported = FALSE;
683 }
684
685 if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
686 goto fail;
687
688 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));
689 }
690
691 return 0;
692
693 fail:
694 pa_log_error("Unable to read volume: %s", snd_strerror(err));
695
696 return -1;
697 }
698
699 static int sink_set_volume_cb(pa_sink *s) {
700 struct userdata *u = s->userdata;
701 int err;
702 int i;
703
704 pa_assert(u);
705 pa_assert(u->mixer_elem);
706
707 for (i = 0; i < s->sample_spec.channels; i++) {
708 long alsa_vol;
709 pa_volume_t vol;
710
711 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
712
713 vol = PA_MIN(s->volume.values[i], PA_VOLUME_NORM);
714
715 if (u->hw_dB_supported) {
716 alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100);
717 alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max);
718
719 if ((err = snd_mixer_selem_set_playback_dB(u->mixer_elem, u->mixer_map[i], alsa_vol, -1)) >= 0) {
720
721 if (snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
722 s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
723
724 continue;
725 }
726
727 u->hw_dB_supported = FALSE;
728
729 }
730
731 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
732 alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_volume_min, u->hw_volume_max);
733
734 if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0)
735 goto fail;
736
737 if (snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
738 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));
739 }
740
741 return 0;
742
743 fail:
744 pa_log_error("Unable to set volume: %s", snd_strerror(err));
745
746 return -1;
747 }
748
749 static int sink_get_mute_cb(pa_sink *s) {
750 struct userdata *u = s->userdata;
751 int err, sw;
752
753 pa_assert(u);
754 pa_assert(u->mixer_elem);
755
756 if ((err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw)) < 0) {
757 pa_log_error("Unable to get switch: %s", snd_strerror(err));
758 return -1;
759 }
760
761 s->muted = !sw;
762
763 return 0;
764 }
765
766 static int sink_set_mute_cb(pa_sink *s) {
767 struct userdata *u = s->userdata;
768 int err;
769
770 pa_assert(u);
771 pa_assert(u->mixer_elem);
772
773 if ((err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->muted)) < 0) {
774 pa_log_error("Unable to set switch: %s", snd_strerror(err));
775 return -1;
776 }
777
778 return 0;
779 }
780
781 static void sink_update_requested_latency_cb(pa_sink *s) {
782 struct userdata *u = s->userdata;
783
784 pa_assert(u);
785
786 update_sw_params(u);
787 }
788
789 static void thread_func(void *userdata) {
790 struct userdata *u = userdata;
791
792 pa_assert(u);
793
794 pa_log_debug("Thread starting up");
795
796 if (u->core->realtime_scheduling)
797 pa_make_realtime(u->core->realtime_priority);
798
799 pa_thread_mq_install(&u->thread_mq);
800 pa_rtpoll_install(u->rtpoll);
801
802 /* update_hwbuf_unused_frames(u); */
803
804 for (;;) {
805 int ret;
806
807 pa_log_debug("loop");
808
809 /* Render some data and write it to the dsp */
810 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
811 int work_done = 0;
812
813 if (u->sink->thread_info.rewind_nbytes > 0 && u->use_tsched) {
814 snd_pcm_sframes_t frames, limit, unused;
815
816 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) u->sink->thread_info.rewind_nbytes);
817
818 frames = u->sink->thread_info.rewind_nbytes / u->frame_size;
819
820 snd_pcm_hwsync(u->pcm_handle);
821 if ((unused = snd_pcm_avail_update(u->pcm_handle)) < 0) {
822 pa_log("snd_pcm_avail_update() failed: %s", snd_strerror(unused));
823 goto fail;
824 }
825
826 limit = (u->hwbuf_size / u->frame_size) - unused;
827
828 if (frames > limit)
829 frames = limit;
830
831 frames = 0;
832
833 if (frames > 0) {
834
835 pa_log_debug("Limited to %lu bytes.", (unsigned long) frames * u->frame_size);
836
837 if ((frames = snd_pcm_rewind(u->pcm_handle, frames)) < 0) {
838 pa_log("snd_pcm_rewind() failed: %s", snd_strerror(frames));
839 goto fail;
840 }
841
842 if ((u->sink->thread_info.rewind_nbytes = frames * u->frame_size) <= 0)
843 pa_log_info("Tried rewind, but was apparently not possible.");
844 else {
845 u->frame_index -= frames;
846 pa_log_debug("Rewound %lu bytes.", (unsigned long) u->sink->thread_info.rewind_nbytes);
847 pa_sink_process_rewind(u->sink);
848 }
849 } else {
850 pa_log_debug("Mhmm, actually there is nothing to rewind.");
851 }
852 }
853
854 if (u->use_mmap) {
855 if ((work_done = mmap_write(u)) < 0)
856 goto fail;
857 } else {
858 if ((work_done = unix_write(u)) < 0)
859 goto fail;
860 }
861
862 pa_log_debug("work_done = %i", work_done);
863
864 if (work_done) {
865
866 if (u->first) {
867 pa_log_info("Starting playback.");
868 snd_pcm_start(u->pcm_handle);
869 u->first = FALSE;
870
871 pa_smoother_resume(u->smoother, pa_rtclock_usec());
872 }
873
874 update_smoother(u);
875 }
876
877 if (u->use_tsched) {
878 pa_usec_t usec, cusec;
879
880 /* OK, the playback buffer is now full, let's
881 * calculate when to wake up next */
882
883 usec = hw_sleep_time(u);
884
885 pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) usec / PA_USEC_PER_MSEC);
886
887 /* Convert from the sound card time domain to the
888 * system time domain */
889 cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), usec);
890
891 pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC);
892
893 /* We don't trust the conversion, so we wake up whatever comes first */
894 pa_rtpoll_set_timer_relative(u->rtpoll, PA_MIN(usec, cusec));
895 }
896
897 } else if (u->use_tsched)
898
899 /* OK, we're in an invalid state, let's disable our timers */
900 pa_rtpoll_set_timer_disabled(u->rtpoll);
901
902 /* Hmm, nothing to do. Let's sleep */
903 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
904 goto fail;
905
906 if (ret == 0)
907 goto finish;
908
909 /* Tell ALSA about this and process its response */
910 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
911 struct pollfd *pollfd;
912 unsigned short revents = 0;
913 int err;
914 unsigned n;
915
916 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
917
918 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
919 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
920 goto fail;
921 }
922
923 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
924 snd_pcm_state_t state;
925
926 if (revents & POLLERR)
927 pa_log_warn("Got POLLERR from ALSA");
928 if (revents & POLLNVAL)
929 pa_log_warn("Got POLLNVAL from ALSA");
930 if (revents & POLLHUP)
931 pa_log_warn("Got POLLHUP from ALSA");
932
933 state = snd_pcm_state(u->pcm_handle);
934 pa_log_warn("PCM state is %s", snd_pcm_state_name(state));
935
936 /* Try to recover from this error */
937
938 switch (state) {
939
940 case SND_PCM_STATE_XRUN:
941 if ((err = snd_pcm_recover(u->pcm_handle, -EPIPE, 1)) != 0) {
942 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
943 goto fail;
944 }
945 break;
946
947 case SND_PCM_STATE_SUSPENDED:
948 if ((err = snd_pcm_recover(u->pcm_handle, -ESTRPIPE, 1)) != 0) {
949 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
950 goto fail;
951 }
952 break;
953
954 default:
955
956 snd_pcm_drop(u->pcm_handle);
957
958 if ((err = snd_pcm_prepare(u->pcm_handle)) < 0) {
959 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
960 goto fail;
961 }
962 break;
963 }
964
965 u->first = TRUE;
966 }
967
968 pa_log_debug("alsa revents = %i", revents);
969 }
970 }
971
972 fail:
973 /* If this was no regular exit from the loop we have to continue
974 * processing messages until we received PA_MESSAGE_SHUTDOWN */
975 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
976 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
977
978 finish:
979 pa_log_debug("Thread shutting down");
980 }
981
982 int pa__init(pa_module*m) {
983
984 pa_modargs *ma = NULL;
985 struct userdata *u = NULL;
986 const char *dev_id;
987 pa_sample_spec ss;
988 pa_channel_map map;
989 uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark;
990 snd_pcm_uframes_t period_frames, tsched_frames;
991 size_t frame_size;
992 snd_pcm_info_t *pcm_info = NULL;
993 int err;
994 const char *name;
995 char *name_buf = NULL;
996 pa_bool_t namereg_fail;
997 pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d;
998 pa_usec_t usec;
999 pa_sink_new_data data;
1000 static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
1001 [SND_PCM_CLASS_GENERIC] = "sound",
1002 [SND_PCM_CLASS_MULTI] = NULL,
1003 [SND_PCM_CLASS_MODEM] = "modem",
1004 [SND_PCM_CLASS_DIGITIZER] = NULL
1005 };
1006
1007 snd_pcm_info_alloca(&pcm_info);
1008
1009 pa_assert(m);
1010
1011 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1012 pa_log("Failed to parse module arguments");
1013 goto fail;
1014 }
1015
1016 ss = m->core->default_sample_spec;
1017 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
1018 pa_log("Failed to parse sample specification and channel map");
1019 goto fail;
1020 }
1021
1022 frame_size = pa_frame_size(&ss);
1023
1024 nfrags = m->core->default_n_fragments;
1025 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
1026 if (frag_size <= 0)
1027 frag_size = frame_size;
1028 tsched_size = pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
1029 tsched_watermark = pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
1030
1031 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
1032 pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
1033 pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
1034 pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
1035 pa_log("Failed to parse buffer metrics");
1036 goto fail;
1037 }
1038
1039 hwbuf_size = frag_size * nfrags;
1040 period_frames = frag_size/frame_size;
1041 tsched_frames = tsched_size/frame_size;
1042
1043 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
1044 pa_log("Failed to parse mmap argument.");
1045 goto fail;
1046 }
1047
1048 if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
1049 pa_log("Failed to parse timer_scheduling argument.");
1050 goto fail;
1051 }
1052
1053 if (use_tsched && !pa_rtclock_hrtimer()) {
1054 pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
1055 use_tsched = FALSE;
1056 }
1057
1058 u = pa_xnew0(struct userdata, 1);
1059 u->core = m->core;
1060 u->module = m;
1061 m->userdata = u;
1062 u->use_mmap = use_mmap;
1063 u->use_tsched = use_tsched;
1064 u->first = TRUE;
1065 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
1066 u->rtpoll = pa_rtpoll_new();
1067 u->alsa_rtpoll_item = NULL;
1068 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
1069
1070 u->smoother = pa_smoother_new(DEFAULT_TSCHED_BUFFER_USEC*2, DEFAULT_TSCHED_BUFFER_USEC*2, TRUE);
1071 usec = pa_rtclock_usec();
1072 pa_smoother_set_time_offset(u->smoother, usec);
1073 pa_smoother_pause(u->smoother, usec);
1074
1075 snd_config_update_free_global();
1076
1077 b = use_mmap;
1078 d = use_tsched;
1079
1080 if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
1081
1082 if (!(u->pcm_handle = pa_alsa_open_by_device_id(
1083 dev_id,
1084 &u->device_name,
1085 &ss, &map,
1086 SND_PCM_STREAM_PLAYBACK,
1087 &nfrags, &period_frames, tsched_frames,
1088 &b, &d)))
1089
1090 goto fail;
1091
1092 } else {
1093
1094 if (!(u->pcm_handle = pa_alsa_open_by_device_string(
1095 pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
1096 &u->device_name,
1097 &ss, &map,
1098 SND_PCM_STREAM_PLAYBACK,
1099 &nfrags, &period_frames, tsched_frames,
1100 &b, &d)))
1101 goto fail;
1102
1103 }
1104
1105 pa_assert(u->device_name);
1106 pa_log_info("Successfully opened device %s.", u->device_name);
1107
1108 if (use_mmap && !b) {
1109 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1110 u->use_mmap = use_mmap = FALSE;
1111 }
1112
1113 if (use_tsched && (!b || !d)) {
1114 pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1115 u->use_tsched = use_tsched = FALSE;
1116 }
1117
1118 if (u->use_mmap)
1119 pa_log_info("Successfully enabled mmap() mode.");
1120
1121 if (u->use_tsched)
1122 pa_log_info("Successfully enabled timer-based scheduling mode.");
1123
1124 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
1125 pa_log("Error fetching PCM info: %s", snd_strerror(err));
1126 goto fail;
1127 }
1128
1129 /* ALSA might tweak the sample spec, so recalculate the frame size */
1130 frame_size = pa_frame_size(&ss);
1131
1132 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
1133 pa_log_warn("Error opening mixer: %s", snd_strerror(err));
1134 else {
1135 pa_bool_t found = FALSE;
1136
1137 if (pa_alsa_prepare_mixer(u->mixer_handle, u->device_name) >= 0)
1138 found = TRUE;
1139 else {
1140 snd_pcm_info_t *info;
1141
1142 snd_pcm_info_alloca(&info);
1143
1144 if (snd_pcm_info(u->pcm_handle, info) >= 0) {
1145 char *md;
1146 int card;
1147
1148 if ((card = snd_pcm_info_get_card(info)) >= 0) {
1149
1150 md = pa_sprintf_malloc("hw:%i", card);
1151
1152 if (strcmp(u->device_name, md))
1153 if (pa_alsa_prepare_mixer(u->mixer_handle, md) >= 0)
1154 found = TRUE;
1155 pa_xfree(md);
1156 }
1157 }
1158 }
1159
1160 if (found)
1161 if (!(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Master", "PCM")))
1162 found = FALSE;
1163
1164 if (!found) {
1165 snd_mixer_close(u->mixer_handle);
1166 u->mixer_handle = NULL;
1167 }
1168 }
1169
1170 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
1171 namereg_fail = TRUE;
1172 else {
1173 name = name_buf = pa_sprintf_malloc("alsa_output.%s", u->device_name);
1174 namereg_fail = FALSE;
1175 }
1176
1177 pa_sink_new_data_init(&data);
1178 data.driver = __FILE__;
1179 data.module = m;
1180 pa_sink_new_data_set_name(&data, name);
1181 data.namereg_fail = namereg_fail;
1182 pa_sink_new_data_set_sample_spec(&data, &ss);
1183 pa_sink_new_data_set_channel_map(&data, &map);
1184
1185 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
1186 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "alsa");
1187 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, snd_pcm_info_get_name(pcm_info));
1188
1189 if (class_table[snd_pcm_info_get_class(pcm_info)])
1190 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, class_table[snd_pcm_info_get_class(pcm_info)]);
1191
1192 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap_rewrite" : (u->use_mmap ? "mmap" : "serial"));
1193
1194 u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
1195 pa_sink_new_data_done(&data);
1196 pa_xfree(name_buf);
1197
1198 if (!u->sink) {
1199 pa_log("Failed to create sink object");
1200 goto fail;
1201 }
1202
1203 u->sink->parent.process_msg = sink_process_msg;
1204 u->sink->update_requested_latency = sink_update_requested_latency_cb;
1205 u->sink->userdata = u;
1206
1207 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1208 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1209
1210 u->frame_size = frame_size;
1211 u->fragment_size = frag_size = period_frames * frame_size;
1212 u->nfragments = nfrags;
1213 u->hwbuf_size = u->fragment_size * nfrags;
1214 u->hwbuf_unused_frames = 0;
1215 u->tsched_watermark = tsched_watermark;
1216 u->frame_index = 0;
1217 u->hw_dB_supported = FALSE;
1218 u->hw_dB_min = u->hw_dB_max = 0;
1219 u->hw_volume_min = u->hw_volume_max = 0;
1220
1221 u->sink->thread_info.max_rewind = use_tsched ? u->hwbuf_size : 0;
1222
1223 u->sink->max_latency = pa_bytes_to_usec(u->hwbuf_size, &ss);
1224
1225 if (!use_tsched)
1226 u->sink->min_latency = u->sink->max_latency;
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 }