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