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