]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-sink.c
clip volume at PA_VOLUME_NORM for alsa devices
[pulseaudio] / src / modules / module-alsa-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdio.h>
28
29 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #else
32 #include "poll.h"
33 #endif
34
35 #include <asoundlib.h>
36
37 #include <polypcore/core.h>
38 #include <polypcore/module.h>
39 #include <polypcore/memchunk.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/modargs.h>
42 #include <polypcore/util.h>
43 #include <polypcore/sample-util.h>
44 #include <polypcore/xmalloc.h>
45 #include <polypcore/log.h>
46
47 #include "alsa-util.h"
48 #include "module-alsa-sink-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("ALSA Sink")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE("sink_name=<name for the sink> device=<ALSA device> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
54
55 struct userdata {
56 snd_pcm_t *pcm_handle;
57 snd_mixer_t *mixer_handle;
58 snd_mixer_elem_t *mixer_elem;
59 pa_sink *sink;
60 struct pa_alsa_fdlist *pcm_fdl;
61 struct pa_alsa_fdlist *mixer_fdl;
62 long hw_volume_max, hw_volume_min;
63
64 size_t frame_size, fragment_size;
65 pa_memchunk memchunk, silence;
66 pa_module *module;
67 };
68
69 static const char* const valid_modargs[] = {
70 "device",
71 "sink_name",
72 "format",
73 "channels",
74 "rate",
75 "fragments",
76 "fragment_size",
77 NULL
78 };
79
80 #define DEFAULT_SINK_NAME "alsa_output"
81 #define DEFAULT_DEVICE "default"
82
83 static void update_usage(struct userdata *u) {
84 pa_module_set_used(u->module,
85 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
86 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0));
87 }
88
89 static void xrun_recovery(struct userdata *u) {
90 assert(u);
91
92 pa_log(__FILE__": *** ALSA-XRUN (playback) ***");
93
94 if (snd_pcm_prepare(u->pcm_handle) < 0)
95 pa_log(__FILE__": snd_pcm_prepare() failed");
96 }
97
98 static void do_write(struct userdata *u) {
99 assert(u);
100
101 update_usage(u);
102
103 for (;;) {
104 pa_memchunk *memchunk = NULL;
105 snd_pcm_sframes_t frames;
106
107 if (u->memchunk.memblock)
108 memchunk = &u->memchunk;
109 else {
110 if (pa_sink_render(u->sink, u->fragment_size, &u->memchunk) < 0)
111 memchunk = &u->silence;
112 else
113 memchunk = &u->memchunk;
114 }
115
116 assert(memchunk->memblock && memchunk->memblock->data && memchunk->length && memchunk->memblock->length && (memchunk->length % u->frame_size) == 0);
117
118 if ((frames = snd_pcm_writei(u->pcm_handle, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length / u->frame_size)) < 0) {
119 if (frames == -EAGAIN)
120 return;
121
122 if (frames == -EPIPE) {
123 xrun_recovery(u);
124 continue;
125 }
126
127 pa_log(__FILE__": snd_pcm_writei() failed: %s", snd_strerror(frames));
128 return;
129 }
130
131 if (memchunk == &u->memchunk) {
132 size_t l = frames * u->frame_size;
133 memchunk->index += l;
134 memchunk->length -= l;
135
136 if (memchunk->length == 0) {
137 pa_memblock_unref(memchunk->memblock);
138 memchunk->memblock = NULL;
139 memchunk->index = memchunk->length = 0;
140 }
141 }
142
143 break;
144 }
145 }
146
147 static void fdl_callback(void *userdata) {
148 struct userdata *u = userdata;
149 assert(u);
150
151 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
152 xrun_recovery(u);
153
154 do_write(u);
155 }
156
157 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
158 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
159
160 assert(u && u->mixer_handle);
161
162 if (mask & SND_CTL_EVENT_MASK_VALUE) {
163 if (u->sink->get_hw_volume)
164 u->sink->get_hw_volume(u->sink);
165 if (u->sink->get_hw_mute)
166 u->sink->get_hw_mute(u->sink);
167 pa_subscription_post(u->sink->core,
168 PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
169 u->sink->index);
170 }
171
172 return 0;
173 }
174
175 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
176 pa_usec_t r = 0;
177 struct userdata *u = s->userdata;
178 snd_pcm_sframes_t frames;
179 int err;
180
181 assert(s && u && u->sink);
182
183 if ((err = snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
184 pa_log(__FILE__": failed to get delay: %s", snd_strerror(err));
185 s->get_latency = NULL;
186 return 0;
187 }
188
189 if (frames < 0)
190 frames = 0;
191
192 r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
193
194 if (u->memchunk.memblock)
195 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
196
197 return r;
198 }
199
200 static int sink_get_hw_volume_cb(pa_sink *s) {
201 struct userdata *u = s->userdata;
202 long vol;
203 int err;
204
205 assert(u && u->mixer_elem);
206
207 if (snd_mixer_selem_has_playback_volume_joined(u->mixer_elem)) {
208 err = snd_mixer_selem_get_playback_volume(u->mixer_elem, 0, &vol);
209 if (err < 0)
210 goto fail;
211 pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
212 (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min));
213 } else {
214 int i;
215
216 for (i = 0;i < s->hw_volume.channels;i++) {
217 err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol);
218 if (err < 0)
219 goto fail;
220 s->hw_volume.values[i] =
221 (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
222 }
223 }
224
225 return 0;
226
227 fail:
228 pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
229 s->get_hw_volume = NULL;
230 s->set_hw_volume = NULL;
231 return -1;
232 }
233
234 static int sink_set_hw_volume_cb(pa_sink *s) {
235 struct userdata *u = s->userdata;
236 int err;
237 pa_volume_t vol;
238
239 assert(u && u->mixer_elem);
240
241 if (snd_mixer_selem_has_playback_volume_joined(u->mixer_elem)) {
242 vol = pa_cvolume_avg(&s->hw_volume);
243
244 if (vol > PA_VOLUME_NORM)
245 vol = PA_VOLUME_NORM;
246
247 vol = (vol * (u->hw_volume_max - u->hw_volume_min)) /
248 PA_VOLUME_NORM + u->hw_volume_min;
249
250 err = snd_mixer_selem_set_playback_volume_all(u->mixer_elem, vol);
251 if (err < 0)
252 goto fail;
253 } else {
254 int i;
255
256 for (i = 0;i < s->hw_volume.channels;i++) {
257 vol = s->hw_volume.values[i];
258
259 if (vol > PA_VOLUME_NORM)
260 vol = PA_VOLUME_NORM;
261
262 vol = (vol * (u->hw_volume_max - u->hw_volume_min)) /
263 PA_VOLUME_NORM + u->hw_volume_min;
264 err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, vol);
265 if (err < 0)
266 goto fail;
267 }
268 }
269
270 return 0;
271
272 fail:
273 pa_log_error(__FILE__": Unable to set volume: %s", snd_strerror(err));
274 s->get_hw_volume = NULL;
275 s->set_hw_volume = NULL;
276 return -1;
277 }
278
279 static int sink_get_hw_mute_cb(pa_sink *s) {
280 struct userdata *u = s->userdata;
281 int err, sw;
282
283 assert(u && u->mixer_elem);
284
285 err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw);
286 if (err) {
287 pa_log_error(__FILE__": Unable to get switch: %s", snd_strerror(err));
288 s->get_hw_mute = NULL;
289 s->set_hw_mute = NULL;
290 return -1;
291 }
292
293 s->hw_muted = !sw;
294
295 return 0;
296 }
297
298 static int sink_set_hw_mute_cb(pa_sink *s) {
299 struct userdata *u = s->userdata;
300 int err;
301
302 assert(u && u->mixer_elem);
303
304 err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->hw_muted);
305 if (err) {
306 pa_log_error(__FILE__": Unable to set switch: %s", snd_strerror(err));
307 s->get_hw_mute = NULL;
308 s->set_hw_mute = NULL;
309 return -1;
310 }
311
312 return 0;
313 }
314
315 int pa__init(pa_core *c, pa_module*m) {
316 pa_modargs *ma = NULL;
317 int ret = -1;
318 struct userdata *u = NULL;
319 const char *dev;
320 pa_sample_spec ss;
321 uint32_t periods, fragsize;
322 snd_pcm_uframes_t period_size;
323 size_t frame_size;
324 snd_pcm_info_t *pcm_info = NULL;
325 int err;
326
327 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
328 pa_log(__FILE__": failed to parse module arguments");
329 goto fail;
330 }
331
332 ss = c->default_sample_spec;
333 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
334 pa_log(__FILE__": failed to parse sample specification");
335 goto fail;
336 }
337 frame_size = pa_frame_size(&ss);
338
339 periods = 8;
340 fragsize = 1024;
341 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
342 pa_log(__FILE__": failed to parse buffer metrics");
343 goto fail;
344 }
345 period_size = fragsize;
346
347 u = pa_xmalloc0(sizeof(struct userdata));
348 m->userdata = u;
349 u->module = m;
350
351 snd_config_update_free_global();
352 if ((err = snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
353 pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
354 goto fail;
355 }
356
357 if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
358 (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
359 pa_log(__FILE__": Error fetching PCM info: %s", snd_strerror(err));
360 goto fail;
361 }
362
363 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
364 pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
365 goto fail;
366 }
367
368 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
369 pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
370 goto fail;
371 }
372
373 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
374 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "PCM"))) {
375 snd_mixer_close(u->mixer_handle);
376 u->mixer_handle = NULL;
377 }
378
379 u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL);
380 assert(u->sink);
381
382 u->sink->get_latency = sink_get_latency_cb;
383 if (u->mixer_handle) {
384 assert(u->mixer_elem);
385 if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
386 u->sink->get_hw_volume = sink_get_hw_volume_cb;
387 u->sink->set_hw_volume = sink_set_hw_volume_cb;
388 snd_mixer_selem_get_playback_volume_range(
389 u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
390 }
391 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
392 u->sink->get_hw_mute = sink_get_hw_mute_cb;
393 u->sink->set_hw_mute = sink_set_hw_mute_cb;
394 }
395 }
396 u->sink->userdata = u;
397 pa_sink_set_owner(u->sink, m);
398 u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev, snd_pcm_info_get_name(pcm_info));
399
400 u->pcm_fdl = pa_alsa_fdlist_new();
401 assert(u->pcm_fdl);
402 if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
403 pa_log(__FILE__": failed to initialise file descriptor monitoring");
404 goto fail;
405 }
406
407 if (u->mixer_handle) {
408 u->mixer_fdl = pa_alsa_fdlist_new();
409 assert(u->mixer_fdl);
410 if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
411 pa_log(__FILE__": failed to initialise file descriptor monitoring");
412 goto fail;
413 }
414 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
415 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
416 }
417
418 u->frame_size = frame_size;
419 u->fragment_size = period_size;
420
421 pa_log_info(__FILE__": using %u fragments of size %lu bytes.", periods, (long unsigned)u->fragment_size);
422
423 u->silence.memblock = pa_memblock_new(u->silence.length = u->fragment_size, c->memblock_stat);
424 assert(u->silence.memblock);
425 pa_silence_memblock(u->silence.memblock, &ss);
426 u->silence.index = 0;
427
428 u->memchunk.memblock = NULL;
429 u->memchunk.index = u->memchunk.length = 0;
430
431 ret = 0;
432
433 /* Get initial mixer settings */
434 if (u->sink->get_hw_volume)
435 u->sink->get_hw_volume(u->sink);
436 if (u->sink->get_hw_mute)
437 u->sink->get_hw_mute(u->sink);
438
439 finish:
440 if (ma)
441 pa_modargs_free(ma);
442
443 if (pcm_info)
444 snd_pcm_info_free(pcm_info);
445
446 return ret;
447
448 fail:
449
450 if (u)
451 pa__done(c, m);
452
453 goto finish;
454 }
455
456 void pa__done(pa_core *c, pa_module*m) {
457 struct userdata *u;
458 assert(c && m);
459
460 if (!(u = m->userdata))
461 return;
462
463 if (u->sink) {
464 pa_sink_disconnect(u->sink);
465 pa_sink_unref(u->sink);
466 }
467
468 if (u->pcm_fdl)
469 pa_alsa_fdlist_free(u->pcm_fdl);
470 if (u->mixer_fdl)
471 pa_alsa_fdlist_free(u->mixer_fdl);
472
473 if (u->mixer_handle)
474 snd_mixer_close(u->mixer_handle);
475
476 if (u->pcm_handle) {
477 snd_pcm_drop(u->pcm_handle);
478 snd_pcm_close(u->pcm_handle);
479 }
480
481 if (u->memchunk.memblock)
482 pa_memblock_unref(u->memchunk.memblock);
483 if (u->silence.memblock)
484 pa_memblock_unref(u->silence.memblock);
485
486 pa_xfree(u);
487 }
488