]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-sink.c
Tried to get the volume information even upon init failure.
[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");
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 assert(s && u && u->sink);
180
181 if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
182 pa_log(__FILE__": failed to get delay");
183 s->get_latency = NULL;
184 return 0;
185 }
186
187 if (frames < 0)
188 frames = 0;
189
190 r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
191
192 if (u->memchunk.memblock)
193 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
194
195 return r;
196 }
197
198 static int sink_get_hw_volume_cb(pa_sink *s) {
199 struct userdata *u = s->userdata;
200 long vol;
201 int err;
202
203 assert(u && u->mixer_elem);
204
205 if (snd_mixer_selem_has_playback_volume_joined(u->mixer_elem)) {
206 err = snd_mixer_selem_get_playback_volume(u->mixer_elem, 0, &vol);
207 if (err < 0)
208 goto fail;
209 pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
210 (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min));
211 } else {
212 int i;
213
214 for (i = 0;i < s->hw_volume.channels;i++) {
215 err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol);
216 if (err < 0)
217 goto fail;
218 s->hw_volume.values[i] =
219 (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
220 }
221 }
222
223 return 0;
224
225 fail:
226 pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
227 s->get_hw_volume = NULL;
228 s->set_hw_volume = NULL;
229 return -1;
230 }
231
232 static int sink_set_hw_volume_cb(pa_sink *s) {
233 struct userdata *u = s->userdata;
234 int err;
235 pa_volume_t vol;
236
237 assert(u && u->mixer_elem);
238
239 if (snd_mixer_selem_has_playback_volume_joined(u->mixer_elem)) {
240 vol = pa_cvolume_avg(&s->hw_volume) * (u->hw_volume_max - u->hw_volume_min) /
241 PA_VOLUME_NORM + u->hw_volume_min;
242 err = snd_mixer_selem_set_playback_volume_all(u->mixer_elem, vol);
243 if (err < 0)
244 goto fail;
245 } else {
246 int i;
247
248 for (i = 0;i < s->hw_volume.channels;i++) {
249 vol = s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min) /
250 PA_VOLUME_NORM + u->hw_volume_min;
251 err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, vol);
252 if (err < 0)
253 goto fail;
254 }
255 }
256
257 return 0;
258
259 fail:
260 pa_log_error(__FILE__": Unable to set volume: %s", snd_strerror(err));
261 s->get_hw_volume = NULL;
262 s->set_hw_volume = NULL;
263 return -1;
264 }
265
266 static int sink_get_hw_mute_cb(pa_sink *s) {
267 struct userdata *u = s->userdata;
268 int err, sw;
269
270 assert(u && u->mixer_elem);
271
272 err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw);
273 if (err) {
274 pa_log_error(__FILE__": Unable to get switch: %s", snd_strerror(err));
275 s->get_hw_mute = NULL;
276 s->set_hw_mute = NULL;
277 return -1;
278 }
279
280 s->hw_muted = !sw;
281
282 return 0;
283 }
284
285 static int sink_set_hw_mute_cb(pa_sink *s) {
286 struct userdata *u = s->userdata;
287 int err;
288
289 assert(u && u->mixer_elem);
290
291 err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->hw_muted);
292 if (err) {
293 pa_log_error(__FILE__": Unable to set switch: %s", snd_strerror(err));
294 s->get_hw_mute = NULL;
295 s->set_hw_mute = NULL;
296 return -1;
297 }
298
299 return 0;
300 }
301
302 int pa__init(pa_core *c, pa_module*m) {
303 pa_modargs *ma = NULL;
304 int ret = -1;
305 struct userdata *u = NULL;
306 const char *dev;
307 pa_sample_spec ss;
308 uint32_t periods, fragsize;
309 snd_pcm_uframes_t period_size;
310 size_t frame_size;
311 int err;
312
313 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
314 pa_log(__FILE__": failed to parse module arguments");
315 goto fail;
316 }
317
318 ss = c->default_sample_spec;
319 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
320 pa_log(__FILE__": failed to parse sample specification");
321 goto fail;
322 }
323 frame_size = pa_frame_size(&ss);
324
325 periods = 8;
326 fragsize = 1024;
327 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
328 pa_log(__FILE__": failed to parse buffer metrics");
329 goto fail;
330 }
331 period_size = fragsize;
332
333 u = pa_xmalloc0(sizeof(struct userdata));
334 m->userdata = u;
335 u->module = m;
336
337 snd_config_update_free_global();
338 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) {
339 pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
340 goto fail;
341 }
342
343 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
344 pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
345 goto fail;
346 }
347
348 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
349 pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
350 goto fail;
351 }
352
353 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
354 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "PCM"))) {
355 snd_mixer_close(u->mixer_handle);
356 u->mixer_handle = NULL;
357 }
358
359 u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL);
360 assert(u->sink);
361
362 u->sink->get_latency = sink_get_latency_cb;
363 if (u->mixer_handle) {
364 assert(u->mixer_elem);
365 if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
366 u->sink->get_hw_volume = sink_get_hw_volume_cb;
367 u->sink->set_hw_volume = sink_set_hw_volume_cb;
368 snd_mixer_selem_get_playback_volume_range(
369 u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
370 }
371 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
372 u->sink->get_hw_mute = sink_get_hw_mute_cb;
373 u->sink->set_hw_mute = sink_set_hw_mute_cb;
374 }
375 }
376 u->sink->userdata = u;
377 pa_sink_set_owner(u->sink, m);
378 u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
379
380 u->pcm_fdl = pa_alsa_fdlist_new();
381 assert(u->pcm_fdl);
382 if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
383 pa_log(__FILE__": failed to initialise file descriptor monitoring");
384 goto fail;
385 }
386
387 if (u->mixer_handle) {
388 u->mixer_fdl = pa_alsa_fdlist_new();
389 assert(u->mixer_fdl);
390 if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
391 pa_log(__FILE__": failed to initialise file descriptor monitoring");
392 goto fail;
393 }
394 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
395 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
396 }
397
398 u->frame_size = frame_size;
399 u->fragment_size = period_size;
400
401 pa_log_info(__FILE__": using %u fragments of size %u bytes.", periods, u->fragment_size);
402
403 u->silence.memblock = pa_memblock_new(u->silence.length = u->fragment_size, c->memblock_stat);
404 assert(u->silence.memblock);
405 pa_silence_memblock(u->silence.memblock, &ss);
406 u->silence.index = 0;
407
408 u->memchunk.memblock = NULL;
409 u->memchunk.index = u->memchunk.length = 0;
410
411 ret = 0;
412
413 /* Get initial mixer settings */
414 if (u->sink->get_hw_volume)
415 u->sink->get_hw_volume(u->sink);
416 if (u->sink->get_hw_mute)
417 u->sink->get_hw_mute(u->sink);
418
419 finish:
420 if (ma)
421 pa_modargs_free(ma);
422
423 return ret;
424
425 fail:
426
427 if (u)
428 pa__done(c, m);
429
430 goto finish;
431 }
432
433 void pa__done(pa_core *c, pa_module*m) {
434 struct userdata *u;
435 assert(c && m);
436
437 if (!(u = m->userdata))
438 return;
439
440 if (u->sink) {
441 pa_sink_disconnect(u->sink);
442 pa_sink_unref(u->sink);
443 }
444
445 if (u->pcm_fdl)
446 pa_alsa_fdlist_free(u->pcm_fdl);
447 if (u->mixer_fdl)
448 pa_alsa_fdlist_free(u->mixer_fdl);
449
450 if (u->mixer_handle)
451 snd_mixer_close(u->mixer_handle);
452
453 if (u->pcm_handle) {
454 snd_pcm_drop(u->pcm_handle);
455 snd_pcm_close(u->pcm_handle);
456 }
457
458 if (u->memchunk.memblock)
459 pa_memblock_unref(u->memchunk.memblock);
460 if (u->silence.memblock)
461 pa_memblock_unref(u->silence.memblock);
462
463 pa_xfree(u);
464 }
465