4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
31 #include <asoundlib.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
35 #include <pulse/timeval.h>
37 #include <pulsecore/core.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/memchunk.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/sample-util.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/macro.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/rtclock.h>
51 #include <pulsecore/time-smoother.h>
53 #include "alsa-util.h"
54 #include "module-alsa-sink-symdef.h"
56 PA_MODULE_AUTHOR("Lennart Poettering");
57 PA_MODULE_DESCRIPTION("ALSA Sink");
58 PA_MODULE_VERSION(PACKAGE_VERSION
);
59 PA_MODULE_LOAD_ONCE(FALSE
);
61 "sink_name=<name for the sink> "
62 "device=<ALSA device> "
63 "device_id=<ALSA device id> "
64 "format=<sample format> "
66 "channels=<number of channels> "
67 "channel_map=<channel map> "
68 "fragments=<number of fragments> "
69 "fragment_size=<fragment size> "
70 "mmap=<enable memory mapping?> "
71 "tsched=<enable system timer based scheduling mode?> "
72 "tsched_buffer_size=<buffer size when using timer based scheduling> "
73 "tsched_buffer_watermark=<lower fill watermark>");
75 #define DEFAULT_DEVICE "default"
76 #define DEFAULT_TSCHED_BUFFER_USEC (2*PA_USEC_PER_SEC)
77 #define DEFAULT_TSCHED_WATERMARK_USEC (10*PA_USEC_PER_MSEC)
85 pa_thread_mq thread_mq
;
88 snd_pcm_t
*pcm_handle
;
90 pa_alsa_fdlist
*mixer_fdl
;
91 snd_mixer_t
*mixer_handle
;
92 snd_mixer_elem_t
*mixer_elem
;
93 long hw_volume_max
, hw_volume_min
;
94 long hw_dB_max
, hw_dB_min
;
95 pa_bool_t hw_dB_supported
;
97 size_t frame_size
, fragment_size
, hwbuf_size
, tsched_watermark
;
103 pa_bool_t use_mmap
, use_tsched
;
107 pa_rtpoll_item
*alsa_rtpoll_item
;
109 snd_mixer_selem_channel_id_t mixer_map
[SND_MIXER_SCHN_LAST
];
111 pa_smoother
*smoother
;
114 snd_pcm_sframes_t hwbuf_unused_frames
;
117 static const char* const valid_modargs
[] = {
129 "tsched_buffer_size",
130 "tsched_buffer_watermark",
134 static int mmap_write(struct userdata
*u
) {
138 pa_sink_assert_ref(u
->sink
);
145 const snd_pcm_channel_area_t
*areas
;
146 snd_pcm_uframes_t offset
, frames
;
148 snd_pcm_hwsync(u
->pcm_handle
);
150 /* First we determine how many samples are missing to fill the
151 * buffer up to 100% */
153 if (PA_UNLIKELY((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0)) {
155 pa_log_debug("snd_pcm_avail_update: %s", snd_strerror(n
));
157 if (err
== -EAGAIN
) {
158 pa_log_debug("EAGAIN");
163 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
165 if ((err
= snd_pcm_recover(u
->pcm_handle
, n
, 1)) == 0) {
170 pa_log("snd_pcm_recover: %s", snd_strerror(err
));
174 /* We only use part of the buffer that matches our
175 * dynamically requested latency */
177 if (PA_UNLIKELY(n
<= u
->hwbuf_unused_frames
))
180 frames
= n
= n
- u
->hwbuf_unused_frames
;
182 if (PA_UNLIKELY((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0)) {
184 pa_log_debug("snd_pcm_mmap_begin: %s", snd_strerror(err
));
186 if (err
== -EAGAIN
) {
187 pa_log_debug("EAGAIN");
192 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
194 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0) {
199 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
203 /* Check these are multiples of 8 bit */
204 pa_assert((areas
[0].first
& 7) == 0);
205 pa_assert((areas
[0].step
& 7)== 0);
207 /* We assume a single interleaved memory buffer */
208 pa_assert((areas
[0].first
>> 3) == 0);
209 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
211 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
213 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, 1);
214 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
217 pa_sink_render_into_full(u
->sink
, &chunk
);
219 /* FIXME: Maybe we can do something to keep this memory block
220 * a little bit longer around? */
221 pa_memblock_unref_fixed(chunk
.memblock
);
223 if (PA_UNLIKELY((err
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0)) {
225 pa_log_debug("snd_pcm_mmap_commit: %s", snd_strerror(err
));
227 if (err
== -EAGAIN
) {
228 pa_log_debug("EAGAIN");
233 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
235 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0) {
240 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
246 u
->frame_index
+= frames
;
248 pa_log_debug("wrote %llu frames", (unsigned long long) frames
);
250 if (PA_LIKELY(frames
>= (snd_pcm_uframes_t
) n
))
255 static int unix_write(struct userdata
*u
) {
256 snd_pcm_status_t
*status
;
259 snd_pcm_status_alloca(&status
);
262 pa_sink_assert_ref(u
->sink
);
266 snd_pcm_sframes_t n
, frames
;
269 snd_pcm_hwsync(u
->pcm_handle
);
271 if (PA_UNLIKELY((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0)) {
272 pa_log("Failed to query DSP status data: %s", snd_strerror(err
));
276 if (PA_UNLIKELY(snd_pcm_status_get_avail_max(status
)*u
->frame_size
>= u
->hwbuf_size
))
277 pa_log_debug("Buffer underrun!");
279 n
= snd_pcm_status_get_avail(status
);
281 /* We only use part of the buffer that matches our
282 * dynamically requested latency */
284 if (PA_UNLIKELY(n
<= u
->hwbuf_unused_frames
))
287 n
-= u
->hwbuf_unused_frames
;
289 if (u
->memchunk
.length
<= 0)
290 pa_sink_render(u
->sink
, n
* u
->frame_size
, &u
->memchunk
);
292 pa_assert(u
->memchunk
.length
> 0);
294 frames
= u
->memchunk
.length
/ u
->frame_size
;
299 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
300 frames
= snd_pcm_writei(u
->pcm_handle
, (const uint8_t*) p
+ u
->memchunk
.index
, frames
);
301 pa_memblock_release(u
->memchunk
.memblock
);
303 pa_assert(frames
!= 0);
305 if (PA_UNLIKELY(frames
< 0)) {
307 if (frames
== -EAGAIN
) {
308 pa_log_debug("EAGAIN");
312 if (frames
== -EPIPE
)
313 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
315 if ((frames
= snd_pcm_recover(u
->pcm_handle
, frames
, 1)) == 0) {
320 pa_log("Failed to write data to DSP: %s", snd_strerror(frames
));
324 u
->memchunk
.index
+= frames
* u
->frame_size
;
325 u
->memchunk
.length
-= frames
* u
->frame_size
;
327 if (u
->memchunk
.length
<= 0) {
328 pa_memblock_unref(u
->memchunk
.memblock
);
329 pa_memchunk_reset(&u
->memchunk
);
334 u
->frame_index
+= frames
;
336 if (PA_LIKELY(frames
>= n
))
341 static void update_smoother(struct userdata
*u
) {
342 snd_pcm_sframes_t delay
= 0;
345 pa_usec_t now1
, now2
;
348 pa_assert(u
->pcm_handle
);
350 /* Let's update the time smoother */
352 snd_pcm_hwsync(u
->pcm_handle
);
353 snd_pcm_avail_update(u
->pcm_handle
);
355 if (PA_UNLIKELY((err
= snd_pcm_delay(u
->pcm_handle
, &delay
)) < 0)) {
356 pa_log_warn("Failed to get delay: %s", snd_strerror(err
));
360 frames
= u
->frame_index
- delay
;
362 pa_log_debug("frame_index = %llu, delay = %llu, p = %llu", (unsigned long long) u
->frame_index
, (unsigned long long) delay
, (unsigned long long) frames
);
364 now1
= pa_rtclock_usec();
365 now2
= pa_bytes_to_usec(frames
* u
->frame_size
, &u
->sink
->sample_spec
);
366 pa_smoother_put(u
->smoother
, now1
, now2
);
369 static pa_usec_t
sink_get_latency(struct userdata
*u
) {
375 delay
= u
->frame_index
- pa_smoother_get(u
->smoother
, pa_rtclock_usec());
378 r
= pa_bytes_to_usec(delay
* u
->frame_size
, &u
->sink
->sample_spec
);
380 if (u
->memchunk
.memblock
)
381 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
386 static int build_pollfd(struct userdata
*u
) {
388 struct pollfd
*pollfd
;
392 pa_assert(u
->pcm_handle
);
394 if ((n
= snd_pcm_poll_descriptors_count(u
->pcm_handle
)) < 0) {
395 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
399 if (u
->alsa_rtpoll_item
)
400 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
402 u
->alsa_rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, n
);
403 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, NULL
);
405 if ((err
= snd_pcm_poll_descriptors(u
->pcm_handle
, pollfd
, n
)) < 0) {
406 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
413 static int suspend(struct userdata
*u
) {
415 pa_assert(u
->pcm_handle
);
417 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
420 snd_pcm_drain(u
->pcm_handle
);
421 snd_pcm_close(u
->pcm_handle
);
422 u
->pcm_handle
= NULL
;
424 if (u
->alsa_rtpoll_item
) {
425 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
426 u
->alsa_rtpoll_item
= NULL
;
429 pa_log_info("Device suspended...");
434 static pa_usec_t
hw_sleep_time(struct userdata
*u
) {
439 usec
= pa_sink_get_requested_latency_within_thread(u
->sink
);
442 usec
= pa_bytes_to_usec(u
->hwbuf_size
, &u
->sink
->sample_spec
);
444 pa_log_debug("hw buffer time: %u ms", (unsigned) (usec
/ PA_USEC_PER_MSEC
));
446 wm
= pa_bytes_to_usec(u
->tsched_watermark
, &u
->sink
->sample_spec
);
453 pa_log_debug("after watermark: %u ms", (unsigned) (usec
/ PA_USEC_PER_MSEC
));
458 static void update_hwbuf_unused_frames(struct userdata
*u
) {
464 if ((usec
= pa_sink_get_requested_latency_within_thread(u
->sink
)) <= 0) {
465 /* Use the full buffer if noone asked us for anything
467 u
->hwbuf_unused_frames
= 0;
471 b
= pa_usec_to_bytes(usec
, &u
->sink
->sample_spec
);
473 /* We need at least one sample in our buffer */
475 if (PA_UNLIKELY(b
< u
->frame_size
))
478 u
->hwbuf_unused_frames
=
479 PA_LIKELY(b
< u
->hwbuf_size
) ?
480 ((u
->hwbuf_size
- b
) / u
->frame_size
) : 0;
483 static int update_sw_params(struct userdata
*u
) {
484 snd_pcm_uframes_t avail_min
;
492 usec
= hw_sleep_time(u
);
494 avail_min
= pa_usec_to_bytes(usec
, &u
->sink
->sample_spec
) / u
->frame_size
;
502 pa_log("setting avail_min=%lu", (unsigned long) avail_min
);
504 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
, avail_min
)) < 0) {
505 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
509 update_hwbuf_unused_frames(u
);
511 pa_log("hwbuf_unused_frames=%lu", (unsigned long) u
->hwbuf_unused_frames
);
516 static int unsuspend(struct userdata
*u
) {
521 snd_pcm_uframes_t period_size
;
524 pa_assert(!u
->pcm_handle
);
526 pa_log_info("Trying resume...");
528 snd_config_update_free_global();
529 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
530 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
534 ss
= u
->sink
->sample_spec
;
535 nfrags
= u
->nfragments
;
536 period_size
= u
->fragment_size
/ u
->frame_size
;
540 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, u
->hwbuf_size
/ u
->frame_size
, &b
, &d
, TRUE
)) < 0) {
541 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
545 if (b
!= u
->use_mmap
|| d
!= u
->use_tsched
) {
546 pa_log_warn("Resume failed, couldn't get original access mode.");
550 if (!pa_sample_spec_equal(&ss
, &u
->sink
->sample_spec
)) {
551 pa_log_warn("Resume failed, couldn't restore original sample settings.");
555 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
556 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
560 if (update_sw_params(u
) < 0)
563 if (build_pollfd(u
) < 0)
566 /* FIXME: We need to reload the volume somehow */
570 pa_log_info("Resumed successfully...");
576 snd_pcm_close(u
->pcm_handle
);
577 u
->pcm_handle
= NULL
;
583 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
584 struct userdata
*u
= PA_SINK(o
)->userdata
;
588 case PA_SINK_MESSAGE_GET_LATENCY
: {
592 r
= sink_get_latency(u
);
594 *((pa_usec_t
*) data
) = r
;
599 case PA_SINK_MESSAGE_SET_STATE
:
601 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
603 case PA_SINK_SUSPENDED
:
604 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
612 case PA_SINK_RUNNING
:
614 if (u
->sink
->thread_info
.state
== PA_SINK_INIT
) {
615 if (build_pollfd(u
) < 0)
619 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
620 if (unsuspend(u
) < 0)
626 case PA_SINK_UNLINKED
:
633 /* case PA_SINK_MESSAGE_ADD_INPUT: */
634 /* case PA_SINK_MESSAGE_REMOVE_INPUT: */
635 /* case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: { */
636 /* int r = pa_sink_process_msg(o, code, data, offset, chunk); */
637 /* update_hwbuf_unused_frames(u); */
642 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
645 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
646 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
649 pa_assert(u
->mixer_handle
);
651 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
654 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
655 pa_sink_get_volume(u
->sink
);
656 pa_sink_get_mute(u
->sink
);
662 static int sink_get_volume_cb(pa_sink
*s
) {
663 struct userdata
*u
= s
->userdata
;
668 pa_assert(u
->mixer_elem
);
670 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
673 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, u
->mixer_map
[i
]));
675 if (u
->hw_dB_supported
) {
677 if ((err
= snd_mixer_selem_get_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) >= 0) {
678 s
->volume
.values
[i
] = pa_sw_volume_from_dB(alsa_vol
/ 100.0);
682 u
->hw_dB_supported
= FALSE
;
685 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) < 0)
688 s
->volume
.values
[i
] = (pa_volume_t
) roundf(((float) (alsa_vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
) / (u
->hw_volume_max
- u
->hw_volume_min
));
694 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
699 static int sink_set_volume_cb(pa_sink
*s
) {
700 struct userdata
*u
= s
->userdata
;
705 pa_assert(u
->mixer_elem
);
707 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
711 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, u
->mixer_map
[i
]));
713 vol
= PA_MIN(s
->volume
.values
[i
], PA_VOLUME_NORM
);
715 if (u
->hw_dB_supported
) {
716 alsa_vol
= (long) (pa_sw_volume_to_dB(vol
) * 100);
717 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_dB_min
, u
->hw_dB_max
);
719 if ((err
= snd_mixer_selem_set_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
, -1)) >= 0) {
721 if (snd_mixer_selem_get_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
) >= 0)
722 s
->volume
.values
[i
] = pa_sw_volume_from_dB(alsa_vol
/ 100.0);
727 u
->hw_dB_supported
= FALSE
;
731 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
732 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_volume_min
, u
->hw_volume_max
);
734 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
)) < 0)
737 if (snd_mixer_selem_get_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
) >= 0)
738 s
->volume
.values
[i
] = (pa_volume_t
) roundf(((float) (alsa_vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
) / (u
->hw_volume_max
- u
->hw_volume_min
));
744 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
749 static int sink_get_mute_cb(pa_sink
*s
) {
750 struct userdata
*u
= s
->userdata
;
754 pa_assert(u
->mixer_elem
);
756 if ((err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
757 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
766 static int sink_set_mute_cb(pa_sink
*s
) {
767 struct userdata
*u
= s
->userdata
;
771 pa_assert(u
->mixer_elem
);
773 if ((err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
774 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
781 static void sink_update_requested_latency_cb(pa_sink
*s
) {
782 struct userdata
*u
= s
->userdata
;
789 static void thread_func(void *userdata
) {
790 struct userdata
*u
= userdata
;
794 pa_log_debug("Thread starting up");
796 if (u
->core
->realtime_scheduling
)
797 pa_make_realtime(u
->core
->realtime_priority
);
799 pa_thread_mq_install(&u
->thread_mq
);
800 pa_rtpoll_install(u
->rtpoll
);
802 /* update_hwbuf_unused_frames(u); */
807 pa_log_debug("loop");
809 /* Render some data and write it to the dsp */
810 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
813 if (u
->sink
->thread_info
.rewind_nbytes
> 0 && u
->use_tsched
) {
814 snd_pcm_sframes_t frames
, limit
, unused
;
816 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) u
->sink
->thread_info
.rewind_nbytes
);
818 frames
= u
->sink
->thread_info
.rewind_nbytes
/ u
->frame_size
;
820 snd_pcm_hwsync(u
->pcm_handle
);
821 if ((unused
= snd_pcm_avail_update(u
->pcm_handle
)) < 0) {
822 pa_log("snd_pcm_avail_update() failed: %s", snd_strerror(unused
));
826 limit
= (u
->hwbuf_size
/ u
->frame_size
) - unused
;
835 pa_log_debug("Limited to %lu bytes.", (unsigned long) frames
* u
->frame_size
);
837 if ((frames
= snd_pcm_rewind(u
->pcm_handle
, frames
)) < 0) {
838 pa_log("snd_pcm_rewind() failed: %s", snd_strerror(frames
));
842 if ((u
->sink
->thread_info
.rewind_nbytes
= frames
* u
->frame_size
) <= 0)
843 pa_log_info("Tried rewind, but was apparently not possible.");
845 u
->frame_index
-= frames
;
846 pa_log_debug("Rewound %lu bytes.", (unsigned long) u
->sink
->thread_info
.rewind_nbytes
);
847 pa_sink_process_rewind(u
->sink
);
850 pa_log_debug("Mhmm, actually there is nothing to rewind.");
855 if ((work_done
= mmap_write(u
)) < 0)
858 if ((work_done
= unix_write(u
)) < 0)
862 pa_log_debug("work_done = %i", work_done
);
867 pa_log_info("Starting playback.");
868 snd_pcm_start(u
->pcm_handle
);
871 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
878 pa_usec_t usec
, cusec
;
880 /* OK, the playback buffer is now full, let's
881 * calculate when to wake up next */
883 usec
= hw_sleep_time(u
);
885 pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) usec
/ PA_USEC_PER_MSEC
);
887 /* Convert from the sound card time domain to the
888 * system time domain */
889 cusec
= pa_smoother_translate(u
->smoother
, pa_rtclock_usec(), usec
);
891 pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec
/ PA_USEC_PER_MSEC
);
893 /* We don't trust the conversion, so we wake up whatever comes first */
894 pa_rtpoll_set_timer_relative(u
->rtpoll
, PA_MIN(usec
, cusec
));
897 } else if (u
->use_tsched
)
899 /* OK, we're in an invalid state, let's disable our timers */
900 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
902 /* Hmm, nothing to do. Let's sleep */
903 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
909 /* Tell ALSA about this and process its response */
910 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
911 struct pollfd
*pollfd
;
912 unsigned short revents
= 0;
916 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
918 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
919 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
923 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
924 snd_pcm_state_t state
;
926 if (revents
& POLLERR
)
927 pa_log_warn("Got POLLERR from ALSA");
928 if (revents
& POLLNVAL
)
929 pa_log_warn("Got POLLNVAL from ALSA");
930 if (revents
& POLLHUP
)
931 pa_log_warn("Got POLLHUP from ALSA");
933 state
= snd_pcm_state(u
->pcm_handle
);
934 pa_log_warn("PCM state is %s", snd_pcm_state_name(state
));
936 /* Try to recover from this error */
940 case SND_PCM_STATE_XRUN
:
941 if ((err
= snd_pcm_recover(u
->pcm_handle
, -EPIPE
, 1)) != 0) {
942 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
947 case SND_PCM_STATE_SUSPENDED
:
948 if ((err
= snd_pcm_recover(u
->pcm_handle
, -ESTRPIPE
, 1)) != 0) {
949 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
956 snd_pcm_drop(u
->pcm_handle
);
958 if ((err
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
959 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
968 pa_log_debug("alsa revents = %i", revents
);
973 /* If this was no regular exit from the loop we have to continue
974 * processing messages until we received PA_MESSAGE_SHUTDOWN */
975 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
976 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
979 pa_log_debug("Thread shutting down");
982 int pa__init(pa_module
*m
) {
984 pa_modargs
*ma
= NULL
;
985 struct userdata
*u
= NULL
;
989 uint32_t nfrags
, hwbuf_size
, frag_size
, tsched_size
, tsched_watermark
;
990 snd_pcm_uframes_t period_frames
, tsched_frames
;
992 snd_pcm_info_t
*pcm_info
= NULL
;
995 char *name_buf
= NULL
;
996 pa_bool_t namereg_fail
;
997 pa_bool_t use_mmap
= TRUE
, b
, use_tsched
= TRUE
, d
;
999 pa_sink_new_data data
;
1000 static const char * const class_table
[SND_PCM_CLASS_LAST
+1] = {
1001 [SND_PCM_CLASS_GENERIC
] = "sound",
1002 [SND_PCM_CLASS_MULTI
] = NULL
,
1003 [SND_PCM_CLASS_MODEM
] = "modem",
1004 [SND_PCM_CLASS_DIGITIZER
] = NULL
1007 snd_pcm_info_alloca(&pcm_info
);
1011 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1012 pa_log("Failed to parse module arguments");
1016 ss
= m
->core
->default_sample_spec
;
1017 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
1018 pa_log("Failed to parse sample specification and channel map");
1022 frame_size
= pa_frame_size(&ss
);
1024 nfrags
= m
->core
->default_n_fragments
;
1025 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*PA_USEC_PER_MSEC
, &ss
);
1027 frag_size
= frame_size
;
1028 tsched_size
= pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC
, &ss
);
1029 tsched_watermark
= pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC
, &ss
);
1031 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 ||
1032 pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0 ||
1033 pa_modargs_get_value_u32(ma
, "tsched_buffer_size", &tsched_size
) < 0 ||
1034 pa_modargs_get_value_u32(ma
, "tsched_buffer_watermark", &tsched_watermark
) < 0) {
1035 pa_log("Failed to parse buffer metrics");
1039 hwbuf_size
= frag_size
* nfrags
;
1040 period_frames
= frag_size
/frame_size
;
1041 tsched_frames
= tsched_size
/frame_size
;
1043 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
1044 pa_log("Failed to parse mmap argument.");
1048 if (pa_modargs_get_value_boolean(ma
, "tsched", &use_tsched
) < 0) {
1049 pa_log("Failed to parse timer_scheduling argument.");
1053 if (use_tsched
&& !pa_rtclock_hrtimer()) {
1054 pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
1058 u
= pa_xnew0(struct userdata
, 1);
1062 u
->use_mmap
= use_mmap
;
1063 u
->use_tsched
= use_tsched
;
1065 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
1066 u
->rtpoll
= pa_rtpoll_new();
1067 u
->alsa_rtpoll_item
= NULL
;
1068 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
1070 u
->smoother
= pa_smoother_new(DEFAULT_TSCHED_BUFFER_USEC
*2, DEFAULT_TSCHED_BUFFER_USEC
*2, TRUE
);
1071 usec
= pa_rtclock_usec();
1072 pa_smoother_set_time_offset(u
->smoother
, usec
);
1073 pa_smoother_pause(u
->smoother
, usec
);
1075 snd_config_update_free_global();
1080 if ((dev_id
= pa_modargs_get_value(ma
, "device_id", NULL
))) {
1082 if (!(u
->pcm_handle
= pa_alsa_open_by_device_id(
1086 SND_PCM_STREAM_PLAYBACK
,
1087 &nfrags
, &period_frames
, tsched_frames
,
1094 if (!(u
->pcm_handle
= pa_alsa_open_by_device_string(
1095 pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
),
1098 SND_PCM_STREAM_PLAYBACK
,
1099 &nfrags
, &period_frames
, tsched_frames
,
1105 pa_assert(u
->device_name
);
1106 pa_log_info("Successfully opened device %s.", u
->device_name
);
1108 if (use_mmap
&& !b
) {
1109 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1110 u
->use_mmap
= use_mmap
= FALSE
;
1113 if (use_tsched
&& (!b
|| !d
)) {
1114 pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1115 u
->use_tsched
= use_tsched
= FALSE
;
1119 pa_log_info("Successfully enabled mmap() mode.");
1122 pa_log_info("Successfully enabled timer-based scheduling mode.");
1124 if ((err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
1125 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
1129 /* ALSA might tweak the sample spec, so recalculate the frame size */
1130 frame_size
= pa_frame_size(&ss
);
1132 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
1133 pa_log_warn("Error opening mixer: %s", snd_strerror(err
));
1135 pa_bool_t found
= FALSE
;
1137 if (pa_alsa_prepare_mixer(u
->mixer_handle
, u
->device_name
) >= 0)
1140 snd_pcm_info_t
*info
;
1142 snd_pcm_info_alloca(&info
);
1144 if (snd_pcm_info(u
->pcm_handle
, info
) >= 0) {
1148 if ((card
= snd_pcm_info_get_card(info
)) >= 0) {
1150 md
= pa_sprintf_malloc("hw:%i", card
);
1152 if (strcmp(u
->device_name
, md
))
1153 if (pa_alsa_prepare_mixer(u
->mixer_handle
, md
) >= 0)
1161 if (!(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Master", "PCM")))
1165 snd_mixer_close(u
->mixer_handle
);
1166 u
->mixer_handle
= NULL
;
1170 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
1171 namereg_fail
= TRUE
;
1173 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", u
->device_name
);
1174 namereg_fail
= FALSE
;
1177 pa_sink_new_data_init(&data
);
1178 data
.driver
= __FILE__
;
1180 pa_sink_new_data_set_name(&data
, name
);
1181 data
.namereg_fail
= namereg_fail
;
1182 pa_sink_new_data_set_sample_spec(&data
, &ss
);
1183 pa_sink_new_data_set_channel_map(&data
, &map
);
1185 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
1186 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "alsa");
1187 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, snd_pcm_info_get_name(pcm_info
));
1189 if (class_table
[snd_pcm_info_get_class(pcm_info
)])
1190 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, class_table
[snd_pcm_info_get_class(pcm_info
)]);
1192 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, u
->use_tsched
? "mmap_rewrite" : (u
->use_mmap
? "mmap" : "serial"));
1194 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1195 pa_sink_new_data_done(&data
);
1199 pa_log("Failed to create sink object");
1203 u
->sink
->parent
.process_msg
= sink_process_msg
;
1204 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1205 u
->sink
->userdata
= u
;
1207 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1208 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1210 u
->frame_size
= frame_size
;
1211 u
->fragment_size
= frag_size
= period_frames
* frame_size
;
1212 u
->nfragments
= nfrags
;
1213 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
1214 u
->hwbuf_unused_frames
= 0;
1215 u
->tsched_watermark
= tsched_watermark
;
1217 u
->hw_dB_supported
= FALSE
;
1218 u
->hw_dB_min
= u
->hw_dB_max
= 0;
1219 u
->hw_volume_min
= u
->hw_volume_max
= 0;
1221 u
->sink
->thread_info
.max_rewind
= use_tsched
? u
->hwbuf_size
: 0;
1223 u
->sink
->max_latency
= pa_bytes_to_usec(u
->hwbuf_size
, &ss
);
1226 u
->sink
->min_latency
= u
->sink
->max_latency
;
1228 pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms",
1229 nfrags
, (long unsigned) u
->fragment_size
,
1230 (double) pa_bytes_to_usec(u
->hwbuf_size
, &ss
) / PA_USEC_PER_MSEC
);
1233 pa_log_info("Time scheduling watermark is %0.2fms",
1234 (double) pa_bytes_to_usec(u
->tsched_watermark
, &ss
) / PA_USEC_PER_MSEC
);
1236 if (update_sw_params(u
) < 0)
1239 pa_memchunk_reset(&u
->memchunk
);
1241 if (u
->mixer_handle
) {
1242 pa_assert(u
->mixer_elem
);
1244 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
))
1246 if (pa_alsa_calc_mixer_map(u
->mixer_elem
, &map
, u
->mixer_map
, TRUE
) >= 0 &&
1247 snd_mixer_selem_get_playback_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
) >= 0) {
1249 pa_bool_t suitable
= TRUE
;
1251 pa_log_info("Volume ranges from %li to %li.", u
->hw_volume_min
, u
->hw_volume_max
);
1253 if (u
->hw_volume_min
> u
->hw_volume_max
) {
1255 pa_log_info("Minimal volume %li larger than maximum volume %li. Strange stuff Falling back to software volume control.", u
->hw_volume_min
, u
->hw_volume_max
);
1258 } else if (u
->hw_volume_max
- u
->hw_volume_min
< 3) {
1260 pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
1263 } else if (snd_mixer_selem_get_playback_dB_range(u
->mixer_elem
, &u
->hw_dB_min
, &u
->hw_dB_max
) >= 0) {
1265 pa_log_info("Volume ranges from %0.2f dB to %0.2f dB.", u
->hw_dB_min
/100.0, u
->hw_dB_max
/100.0);
1267 /* Let's see if this thing actually is useful for muting */
1268 if (u
->hw_dB_min
> -6000) {
1269 pa_log_info("Device cannot attenuate for more than -60 dB (only %0.2f dB supported), falling back to software volume control.", ((double) u
->hw_dB_min
) / 100);
1272 } else if (u
->hw_dB_max
< 0) {
1274 pa_log_info("Device is still attenuated at maximum volume setting (%0.2f dB is maximum). Strange stuff. Falling back to software volume control.", ((double) u
->hw_dB_max
) / 100);
1277 } else if (u
->hw_dB_min
>= u
->hw_dB_max
) {
1279 pa_log_info("Minimal dB (%0.2f) larger or equal to maximum dB (%0.2f). Strange stuff. Falling back to software volume control.", ((double) u
->hw_dB_min
) / 100, ((double) u
->hw_dB_max
) / 100);
1284 if (u
->hw_dB_max
> 0) {
1285 /* dB > 0 means overamplification, and clipping, we don't want that here */
1286 pa_log_info("Device can do overamplification for %0.2f dB. Limiting to 0 db", ((double) u
->hw_dB_max
) / 100);
1290 u
->hw_dB_supported
= TRUE
;
1295 u
->sink
->get_volume
= sink_get_volume_cb
;
1296 u
->sink
->set_volume
= sink_set_volume_cb
;
1297 u
->sink
->flags
|= PA_SINK_HW_VOLUME_CTRL
| (u
->hw_dB_supported
? PA_SINK_DECIBEL_VOLUME
: 0);
1298 pa_log_info("Using hardware volume control. %s dB scale.", u
->hw_dB_supported
? "Using" : "Not using");
1301 pa_log_info("Using software volume control. Trying to reset sound card to 0 dB.");
1302 pa_alsa_0dB_playback(u
->mixer_elem
);
1306 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
1307 u
->sink
->get_mute
= sink_get_mute_cb
;
1308 u
->sink
->set_mute
= sink_set_mute_cb
;
1309 u
->sink
->flags
|= PA_SINK_HW_MUTE_CTRL
;
1312 u
->mixer_fdl
= pa_alsa_fdlist_new();
1314 if (pa_alsa_fdlist_set_mixer(u
->mixer_fdl
, u
->mixer_handle
, m
->core
->mainloop
) < 0) {
1315 pa_log("Failed to initialize file descriptor monitoring");
1319 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
1320 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
1322 u
->mixer_fdl
= NULL
;
1324 pa_alsa_dump(u
->pcm_handle
);
1326 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
1327 pa_log("Failed to create thread.");
1331 /* Get initial mixer settings */
1332 if (data
.volume_is_set
) {
1333 if (u
->sink
->set_volume
)
1334 u
->sink
->set_volume(u
->sink
);
1336 if (u
->sink
->get_volume
)
1337 u
->sink
->get_volume(u
->sink
);
1340 if (data
.muted_is_set
) {
1341 if (u
->sink
->set_mute
)
1342 u
->sink
->set_mute(u
->sink
);
1344 if (u
->sink
->get_mute
)
1345 u
->sink
->get_mute(u
->sink
);
1348 pa_sink_put(u
->sink
);
1350 pa_modargs_free(ma
);
1357 pa_modargs_free(ma
);
1364 void pa__done(pa_module
*m
) {
1369 if (!(u
= m
->userdata
))
1373 pa_sink_unlink(u
->sink
);
1376 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1377 pa_thread_free(u
->thread
);
1380 pa_thread_mq_done(&u
->thread_mq
);
1383 pa_sink_unref(u
->sink
);
1385 if (u
->memchunk
.memblock
)
1386 pa_memblock_unref(u
->memchunk
.memblock
);
1388 if (u
->alsa_rtpoll_item
)
1389 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
1392 pa_rtpoll_free(u
->rtpoll
);
1395 pa_alsa_fdlist_free(u
->mixer_fdl
);
1397 if (u
->mixer_handle
)
1398 snd_mixer_close(u
->mixer_handle
);
1400 if (u
->pcm_handle
) {
1401 snd_pcm_drop(u
->pcm_handle
);
1402 snd_pcm_close(u
->pcm_handle
);
1406 pa_smoother_free(u
->smoother
);
1408 pa_xfree(u
->device_name
);
1411 snd_config_update_free_global();