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