]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-sink.c
minor cleanups
[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
36 #include <pulsecore/core.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/memchunk.h>
39 #include <pulsecore/sink.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/sample-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/thread-mq.h>
48 #include <pulsecore/rtpoll.h>
49
50 #include "alsa-util.h"
51 #include "module-alsa-sink-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering")
54 PA_MODULE_DESCRIPTION("ALSA Sink")
55 PA_MODULE_VERSION(PACKAGE_VERSION)
56 PA_MODULE_USAGE(
57 "sink_name=<name for the sink> "
58 "device=<ALSA device> "
59 "format=<sample format> "
60 "channels=<number of channels> "
61 "rate=<sample rate> "
62 "fragments=<number of fragments> "
63 "fragment_size=<fragment size> "
64 "channel_map=<channel map> "
65 "mmap=<enable memory mapping?>")
66
67 #define DEFAULT_DEVICE "default"
68
69 struct userdata {
70 pa_core *core;
71 pa_module *module;
72 pa_sink *sink;
73
74 pa_thread *thread;
75 pa_thread_mq thread_mq;
76 pa_rtpoll *rtpoll;
77
78 snd_pcm_t *pcm_handle;
79
80 pa_alsa_fdlist *mixer_fdl;
81 snd_mixer_t *mixer_handle;
82 snd_mixer_elem_t *mixer_elem;
83 long hw_volume_max, hw_volume_min;
84
85 size_t frame_size, fragment_size, hwbuf_size;
86 unsigned nfragments;
87 pa_memchunk memchunk;
88
89 char *device_name;
90
91 int use_mmap;
92
93 int first;
94
95 pa_rtpoll_item *alsa_rtpoll_item;
96 };
97
98 static const char* const valid_modargs[] = {
99 "device",
100 "sink_name",
101 "format",
102 "channels",
103 "rate",
104 "fragments",
105 "fragment_size",
106 "channel_map",
107 "mmap",
108 NULL
109 };
110
111 static int mmap_write(struct userdata *u) {
112 int work_done = 0;
113
114 pa_assert(u);
115 pa_sink_assert_ref(u->sink);
116
117 for (;;) {
118 pa_memchunk chunk;
119 void *p;
120 snd_pcm_sframes_t n;
121 int err;
122 const snd_pcm_channel_area_t *areas;
123 snd_pcm_uframes_t offset, frames;
124
125 if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
126
127 if (n == -EPIPE) {
128 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
129 u->first = 1;
130 }
131
132 if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0)
133 continue;
134
135 if (err == -EAGAIN)
136 return work_done;
137
138 pa_log("snd_pcm_avail_update: %s", snd_strerror(n));
139 return -1;
140 }
141
142 /* pa_log("Got request for %i samples", (int) n); */
143
144 if (n <= 0)
145 return work_done;
146
147 frames = n;
148
149 if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
150
151 if (err == -EPIPE) {
152 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
153 u->first = 1;
154 }
155
156 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
157 continue;
158
159 if (err == -EAGAIN)
160 return work_done;
161
162 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
163 return -1;
164 }
165
166 /* Check these are multiples of 8 bit */
167 pa_assert((areas[0].first & 7) == 0);
168 pa_assert((areas[0].step & 7)== 0);
169
170 /* We assume a single interleaved memory buffer */
171 pa_assert((areas[0].first >> 3) == 0);
172 pa_assert((areas[0].step >> 3) == u->frame_size);
173
174 p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
175
176 chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, 1);
177 chunk.length = pa_memblock_get_length(chunk.memblock);
178 chunk.index = 0;
179
180 pa_sink_render_into_full(u->sink, &chunk);
181
182 /* FIXME: Maybe we can do something to keep this memory block
183 * a little bit longer around? */
184 pa_memblock_unref_fixed(chunk.memblock);
185
186 if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
187
188 if (err == -EPIPE) {
189 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
190 u->first = 1;
191 }
192
193 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
194 continue;
195
196 if (err == -EAGAIN)
197 return work_done;
198
199 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
200 return -1;
201 }
202
203 work_done = 1;
204
205 if (frames >= (snd_pcm_uframes_t) n)
206 return work_done;
207
208 /* pa_log("wrote %i samples", (int) frames); */
209 }
210 }
211
212 static int unix_write(struct userdata *u) {
213 snd_pcm_status_t *status;
214 int work_done = 0;
215
216 snd_pcm_status_alloca(&status);
217
218 pa_assert(u);
219 pa_sink_assert_ref(u->sink);
220
221 for (;;) {
222 void *p;
223 snd_pcm_sframes_t t;
224 ssize_t l;
225 int err;
226
227 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
228 pa_log("Failed to query DSP status data: %s", snd_strerror(t));
229 return -1;
230 }
231
232 if (snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size)
233 pa_log_debug("Buffer underrun!");
234
235 l = snd_pcm_status_get_avail(status) * u->frame_size;
236
237 /* pa_log("%u bytes to write", l); */
238
239 if (l <= 0)
240 return work_done;
241
242 if (u->memchunk.length <= 0)
243 pa_sink_render(u->sink, l, &u->memchunk);
244
245 pa_assert(u->memchunk.length > 0);
246
247 p = pa_memblock_acquire(u->memchunk.memblock);
248 t = snd_pcm_writei(u->pcm_handle, (const uint8_t*) p + u->memchunk.index, u->memchunk.length / u->frame_size);
249 pa_memblock_release(u->memchunk.memblock);
250
251 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
252
253 pa_assert(t != 0);
254
255 if (t < 0) {
256
257 if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
258 continue;
259
260 if (t == -EAGAIN) {
261 pa_log_debug("EAGAIN");
262 return work_done;
263 } else {
264 pa_log("Failed to write data to DSP: %s", snd_strerror(t));
265 return -1;
266 }
267 }
268
269 u->memchunk.index += t * u->frame_size;
270 u->memchunk.length -= t * u->frame_size;
271
272 if (u->memchunk.length <= 0) {
273 pa_memblock_unref(u->memchunk.memblock);
274 pa_memchunk_reset(&u->memchunk);
275 }
276
277 work_done = 1;
278
279 if (t * u->frame_size >= (unsigned) l)
280 return work_done;
281 }
282 }
283
284 static pa_usec_t sink_get_latency(struct userdata *u) {
285 pa_usec_t r = 0;
286 snd_pcm_status_t *status;
287 snd_pcm_sframes_t frames = 0;
288 int err;
289
290 snd_pcm_status_alloca(&status);
291
292 pa_assert(u);
293 pa_assert(u->pcm_handle);
294
295 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0)
296 pa_log("Failed to get delay: %s", snd_strerror(err));
297 else
298 frames = snd_pcm_status_get_delay(status);
299
300 if (frames > 0)
301 r = pa_bytes_to_usec(frames * u->frame_size, &u->sink->sample_spec);
302
303 if (u->memchunk.memblock)
304 r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
305
306 return r;
307 }
308
309 static int build_pollfd(struct userdata *u) {
310 int err;
311 struct pollfd *pollfd;
312 int n;
313
314 pa_assert(u);
315 pa_assert(u->pcm_handle);
316
317 if ((n = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
318 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
319 return -1;
320 }
321
322 if (u->alsa_rtpoll_item)
323 pa_rtpoll_item_free(u->alsa_rtpoll_item);
324
325 u->alsa_rtpoll_item = pa_rtpoll_item_new(u->rtpoll, n);
326 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, NULL);
327
328 if ((err = snd_pcm_poll_descriptors(u->pcm_handle, pollfd, n)) < 0) {
329 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
330 return -1;
331 }
332
333 return 0;
334 }
335
336 static int suspend(struct userdata *u) {
337 pa_assert(u);
338 pa_assert(u->pcm_handle);
339
340 /* Let's suspend */
341 snd_pcm_drain(u->pcm_handle);
342 snd_pcm_close(u->pcm_handle);
343 u->pcm_handle = NULL;
344
345 if (u->alsa_rtpoll_item) {
346 pa_rtpoll_item_free(u->alsa_rtpoll_item);
347 u->alsa_rtpoll_item = NULL;
348 }
349
350 pa_log_debug("Device suspended...");
351
352 return 0;
353 }
354
355 static int unsuspend(struct userdata *u) {
356 pa_sample_spec ss;
357 int err, b;
358 unsigned nfrags;
359 snd_pcm_uframes_t period_size;
360
361 pa_assert(u);
362 pa_assert(!u->pcm_handle);
363
364 pa_log_debug("Trying resume...");
365
366 snd_config_update_free_global();
367 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
368 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
369 goto fail;
370 }
371
372 ss = u->sink->sample_spec;
373 nfrags = u->nfragments;
374 period_size = u->fragment_size / u->frame_size;
375 b = u->use_mmap;
376
377 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
378 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
379 goto fail;
380 }
381
382 if (b != u->use_mmap) {
383 pa_log_warn("Resume failed, couldn't get original access mode.");
384 goto fail;
385 }
386
387 if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
388 pa_log_warn("Resume failed, couldn't restore original sample settings.");
389 goto fail;
390 }
391
392 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
393 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
394 goto fail;
395 }
396
397 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
398 pa_log("Failed to set software parameters: %s", snd_strerror(err));
399 goto fail;
400 }
401
402 if (build_pollfd(u) < 0)
403 goto fail;
404
405 /* FIXME: We need to reload the volume somehow */
406
407 u->first = 1;
408
409 pa_log_debug("Resumed successfully...");
410
411 return 0;
412
413 fail:
414 if (u->pcm_handle) {
415 snd_pcm_close(u->pcm_handle);
416 u->pcm_handle = NULL;
417 }
418
419 return -1;
420 }
421
422 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
423 struct userdata *u = PA_SINK(o)->userdata;
424
425 switch (code) {
426
427 case PA_SINK_MESSAGE_GET_LATENCY: {
428 pa_usec_t r = 0;
429
430 if (u->pcm_handle)
431 r = sink_get_latency(u);
432
433 *((pa_usec_t*) data) = r;
434
435 return 0;
436 }
437
438 case PA_SINK_MESSAGE_SET_STATE:
439
440 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
441
442 case PA_SINK_SUSPENDED:
443 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
444
445 if (suspend(u) < 0)
446 return -1;
447
448 break;
449
450 case PA_SINK_IDLE:
451 case PA_SINK_RUNNING:
452
453 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
454 if (unsuspend(u) < 0)
455 return -1;
456 }
457
458 break;
459
460 case PA_SINK_DISCONNECTED:
461 ;
462 }
463
464 break;
465 }
466
467 return pa_sink_process_msg(o, code, data, offset, chunk);
468 }
469
470 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
471 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
472
473 pa_assert(u);
474 pa_assert(u->mixer_handle);
475
476 if (mask == SND_CTL_EVENT_MASK_REMOVE)
477 return 0;
478
479 if (mask & SND_CTL_EVENT_MASK_VALUE) {
480 pa_sink_get_volume(u->sink);
481 pa_sink_get_mute(u->sink);
482 }
483
484 return 0;
485 }
486
487 static int sink_get_volume_cb(pa_sink *s) {
488 struct userdata *u = s->userdata;
489 int err;
490 int i;
491
492 pa_assert(u);
493 pa_assert(u->mixer_elem);
494
495 for (i = 0; i < s->sample_spec.channels; i++) {
496 long set_vol, vol;
497
498 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
499
500 if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol)) < 0)
501 goto fail;
502
503 set_vol = (long) roundf(((float) s->volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
504
505 /* Try to avoid superfluous volume changes */
506 if (set_vol != vol)
507 s->volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
508 }
509
510 return 0;
511
512 fail:
513 pa_log_error("Unable to read volume: %s", snd_strerror(err));
514
515 s->get_volume = NULL;
516 s->set_volume = NULL;
517 return -1;
518 }
519
520 static int sink_set_volume_cb(pa_sink *s) {
521 struct userdata *u = s->userdata;
522 int err;
523 int i;
524
525 pa_assert(u);
526 pa_assert(u->mixer_elem);
527
528 for (i = 0; i < s->sample_spec.channels; i++) {
529 long alsa_vol;
530 pa_volume_t vol;
531
532 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
533
534 vol = s->volume.values[i];
535
536 if (vol > PA_VOLUME_NORM)
537 vol = PA_VOLUME_NORM;
538
539 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
540
541 if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, alsa_vol)) < 0)
542 goto fail;
543 }
544
545 return 0;
546
547 fail:
548 pa_log_error("Unable to set volume: %s", snd_strerror(err));
549
550 s->get_volume = NULL;
551 s->set_volume = NULL;
552 return -1;
553 }
554
555 static int sink_get_mute_cb(pa_sink *s) {
556 struct userdata *u = s->userdata;
557 int err, sw;
558
559 pa_assert(u);
560 pa_assert(u->mixer_elem);
561
562 if ((err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw)) < 0) {
563 pa_log_error("Unable to get switch: %s", snd_strerror(err));
564
565 s->get_mute = NULL;
566 s->set_mute = NULL;
567 return -1;
568 }
569
570 s->muted = !sw;
571
572 return 0;
573 }
574
575 static int sink_set_mute_cb(pa_sink *s) {
576 struct userdata *u = s->userdata;
577 int err;
578
579 pa_assert(u);
580 pa_assert(u->mixer_elem);
581
582 if ((err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->muted)) < 0) {
583 pa_log_error("Unable to set switch: %s", snd_strerror(err));
584
585 s->get_mute = NULL;
586 s->set_mute = NULL;
587 return -1;
588 }
589
590 return 0;
591 }
592
593 static void thread_func(void *userdata) {
594 struct userdata *u = userdata;
595
596 pa_assert(u);
597
598 pa_log_debug("Thread starting up");
599
600 if (u->core->high_priority)
601 pa_make_realtime();
602
603 pa_thread_mq_install(&u->thread_mq);
604 pa_rtpoll_install(u->rtpoll);
605
606 if (build_pollfd(u) < 0)
607 goto fail;
608
609 for (;;) {
610 pa_msgobject *object;
611 int code;
612 void *data;
613 pa_memchunk chunk;
614 int64_t offset;
615
616 /* pa_log("loop"); */
617
618 /* Render some data and write it to the dsp */
619 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
620 int work_done = 0;
621
622 if (u->use_mmap) {
623 if ((work_done = mmap_write(u)) < 0)
624 goto fail;
625 } else {
626 if ((work_done = unix_write(u)) < 0)
627 goto fail;
628 }
629
630 if (work_done && u->first) {
631 pa_log_info("Starting playback.");
632 snd_pcm_start(u->pcm_handle);
633 u->first = 0;
634 continue;
635 }
636 }
637
638 /* pa_log("loop2"); */
639
640 /* Check whether there is a message for us to process */
641 if (pa_asyncmsgq_get(u->thread_mq.inq, &object, &code, &data, &offset, &chunk, 0) == 0) {
642 int ret;
643
644 if (!object && code == PA_MESSAGE_SHUTDOWN) {
645 pa_asyncmsgq_done(u->thread_mq.inq, 0);
646 goto finish;
647 }
648
649 ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
650 pa_asyncmsgq_done(u->thread_mq.inq, ret);
651 continue;
652 }
653
654 /* Hmm, nothing to do. Let's sleep */
655 if (pa_rtpoll_run(u->rtpoll) < 0) {
656 pa_log("poll() failed: %s", pa_cstrerror(errno));
657 goto fail;
658 }
659
660 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
661 struct pollfd *pollfd;
662 unsigned short revents = 0;
663 int err;
664 unsigned n;
665
666 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
667
668 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
669 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
670 goto fail;
671 }
672
673 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
674 if (revents & POLLERR)
675 pa_log_warn("Got POLLERR from ALSA");
676 if (revents & POLLNVAL)
677 pa_log_warn("Got POLLNVAL from ALSA");
678 if (revents & POLLHUP)
679 pa_log_warn("Got POLLHUP from ALSA");
680
681 goto fail;
682 }
683 /* pa_log("got alsa event"); */
684 }
685
686 }
687
688 fail:
689 /* We have to continue processing messages until we receive the
690 * SHUTDOWN message */
691 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
692 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
693
694 finish:
695 pa_log_debug("Thread shutting down");
696 }
697
698 int pa__init(pa_module*m) {
699
700 pa_modargs *ma = NULL;
701 struct userdata *u = NULL;
702 const char *dev;
703 pa_sample_spec ss;
704 pa_channel_map map;
705 uint32_t nfrags, frag_size;
706 snd_pcm_uframes_t period_size;
707 size_t frame_size;
708 snd_pcm_info_t *pcm_info = NULL;
709 int err;
710 char *t;
711 const char *name;
712 char *name_buf = NULL;
713 int namereg_fail;
714 int use_mmap = 1, b;
715
716 snd_pcm_info_alloca(&pcm_info);
717
718 pa_assert(m);
719
720 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
721 pa_log("Failed to parse module arguments");
722 goto fail;
723 }
724
725 ss = m->core->default_sample_spec;
726 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
727 pa_log("Failed to parse sample specification and channel map");
728 goto fail;
729 }
730
731 frame_size = pa_frame_size(&ss);
732
733 nfrags = m->core->default_n_fragments;
734 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
735 if (frag_size <= 0)
736 frag_size = frame_size;
737
738 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
739 pa_log("Failed to parse buffer metrics");
740 goto fail;
741 }
742 period_size = frag_size/frame_size;
743
744 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
745 pa_log("Failed to parse mmap argument.");
746 goto fail;
747 }
748
749 u = pa_xnew0(struct userdata, 1);
750 u->core = m->core;
751 u->module = m;
752 m->userdata = u;
753 u->use_mmap = use_mmap;
754 u->first = 1;
755 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
756 u->rtpoll = pa_rtpoll_new();
757 u->alsa_rtpoll_item = NULL;
758 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, u->thread_mq.inq);
759
760 snd_config_update_free_global();
761 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) {
762 pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
763 goto fail;
764 }
765
766 u->device_name = pa_xstrdup(dev);
767
768 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
769 pa_log("Error fetching PCM info: %s", snd_strerror(err));
770 goto fail;
771 }
772
773 b = use_mmap;
774 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
775 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
776 goto fail;
777 }
778
779 if (use_mmap && !b) {
780 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
781 u->use_mmap = use_mmap = b;
782 }
783
784 if (u->use_mmap)
785 pa_log_info("Successfully enabled mmap() mode.");
786
787 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
788 pa_log("Failed to set software parameters: %s", snd_strerror(err));
789 goto fail;
790 }
791
792 /* ALSA might tweak the sample spec, so recalculate the frame size */
793 frame_size = pa_frame_size(&ss);
794
795 if (ss.channels != map.channels)
796 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
797 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
798
799 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
800 pa_log_warn("Error opening mixer: %s", snd_strerror(err));
801 else {
802
803 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
804 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Master", "PCM"))) {
805
806 snd_mixer_close(u->mixer_handle);
807 u->mixer_handle = NULL;
808 }
809 }
810
811 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
812 namereg_fail = 1;
813 else {
814 name = name_buf = pa_sprintf_malloc("alsa_output.%s", dev);
815 namereg_fail = 0;
816 }
817
818 u->sink = pa_sink_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
819 pa_xfree(name_buf);
820
821 if (!u->sink) {
822 pa_log("Failed to create sink object");
823 goto fail;
824 }
825
826 u->sink->parent.process_msg = sink_process_msg;
827 u->sink->userdata = u;
828
829 pa_sink_set_module(u->sink, m);
830 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
831 pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
832 "ALSA PCM on %s (%s)%s",
833 dev,
834 snd_pcm_info_get_name(pcm_info),
835 use_mmap ? " via DMA" : ""));
836 pa_xfree(t);
837
838 u->sink->is_hardware = 1;
839
840 u->frame_size = frame_size;
841 u->fragment_size = frag_size = period_size * frame_size;
842 u->nfragments = nfrags;
843 u->hwbuf_size = u->fragment_size * nfrags;
844
845 pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
846
847 pa_memchunk_reset(&u->memchunk);
848
849 if (u->mixer_handle) {
850 /* Initialize mixer code */
851
852 pa_assert(u->mixer_elem);
853
854 if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
855 int i;
856
857 for (i = 0;i < ss.channels; i++) {
858 if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i))
859 break;
860 }
861
862 if (i == ss.channels) {
863 u->sink->get_volume = sink_get_volume_cb;
864 u->sink->set_volume = sink_set_volume_cb;
865 snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
866 }
867 }
868
869 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
870 u->sink->get_mute = sink_get_mute_cb;
871 u->sink->set_mute = sink_set_mute_cb;
872 }
873
874 u->mixer_fdl = pa_alsa_fdlist_new();
875
876 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
877 pa_log("failed to initialise file descriptor monitoring");
878 goto fail;
879 }
880
881 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
882 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
883 } else
884 u->mixer_fdl = NULL;
885
886 if (!(u->thread = pa_thread_new(thread_func, u))) {
887 pa_log("Failed to create thread.");
888 goto fail;
889 }
890
891 /* Get initial mixer settings */
892 if (u->sink->get_volume)
893 u->sink->get_volume(u->sink);
894 if (u->sink->get_mute)
895 u->sink->get_mute(u->sink);
896
897 pa_modargs_free(ma);
898
899 return 0;
900
901 fail:
902
903 if (ma)
904 pa_modargs_free(ma);
905
906 pa__done(m);
907
908 return -1;
909 }
910
911 void pa__done(pa_module*m) {
912 struct userdata *u;
913
914 pa_assert(m);
915
916 if (!(u = m->userdata))
917 return;
918
919 if (u->sink)
920 pa_sink_disconnect(u->sink);
921
922 if (u->thread) {
923 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
924 pa_thread_free(u->thread);
925 }
926
927 pa_thread_mq_done(&u->thread_mq);
928
929 if (u->sink)
930 pa_sink_unref(u->sink);
931
932 if (u->memchunk.memblock)
933 pa_memblock_unref(u->memchunk.memblock);
934
935 if (u->alsa_rtpoll_item)
936 pa_rtpoll_item_free(u->alsa_rtpoll_item);
937
938 if (u->rtpoll)
939 pa_rtpoll_free(u->rtpoll);
940
941 if (u->mixer_fdl)
942 pa_alsa_fdlist_free(u->mixer_fdl);
943
944 if (u->mixer_handle)
945 snd_mixer_close(u->mixer_handle);
946
947 if (u->pcm_handle) {
948 snd_pcm_drop(u->pcm_handle);
949 snd_pcm_close(u->pcm_handle);
950 }
951
952 pa_xfree(u->device_name);
953 pa_xfree(u);
954
955 snd_config_update_free_global();
956 }
957