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 /* First we determine how many samples are missing to fill the
149 * buffer up to 100% */
151 if (PA_UNLIKELY((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0)) {
153 pa_log_debug("snd_pcm_avail_update: %s", snd_strerror(n
));
156 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
160 if ((err
= snd_pcm_recover(u
->pcm_handle
, n
, 1)) == 0)
166 pa_log("snd_pcm_avail_update: %s", snd_strerror(err
));
170 /* We only use part of the buffer that matches our
171 * dynamically requested latency */
173 if (PA_UNLIKELY(n
<= u
->hwbuf_unused_frames
))
176 frames
= n
= n
- u
->hwbuf_unused_frames
;
178 if (PA_UNLIKELY((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0)) {
180 pa_log_debug("snd_pcm_mmap_begin: %s", snd_strerror(err
));
183 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
187 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
193 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
197 /* Check these are multiples of 8 bit */
198 pa_assert((areas
[0].first
& 7) == 0);
199 pa_assert((areas
[0].step
& 7)== 0);
201 /* We assume a single interleaved memory buffer */
202 pa_assert((areas
[0].first
>> 3) == 0);
203 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
205 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
207 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, 1);
208 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
211 pa_sink_render_into_full(u
->sink
, &chunk
);
213 /* FIXME: Maybe we can do something to keep this memory block
214 * a little bit longer around? */
215 pa_memblock_unref_fixed(chunk
.memblock
);
217 if (PA_UNLIKELY((err
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0)) {
219 pa_log_debug("snd_pcm_mmap_commit: %s", snd_strerror(err
));
222 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
226 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
232 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
238 u
->frame_index
+= frames
;
240 pa_log_debug("wrote %llu frames", (unsigned long long) frames
);
242 if (PA_LIKELY(frames
>= (snd_pcm_uframes_t
) n
))
247 static int unix_write(struct userdata
*u
) {
248 snd_pcm_status_t
*status
;
251 snd_pcm_status_alloca(&status
);
254 pa_sink_assert_ref(u
->sink
);
258 snd_pcm_sframes_t n
, frames
;
261 if (PA_UNLIKELY((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0)) {
262 pa_log("Failed to query DSP status data: %s", snd_strerror(err
));
266 if (PA_UNLIKELY(snd_pcm_status_get_avail_max(status
)*u
->frame_size
>= u
->hwbuf_size
))
267 pa_log_debug("Buffer underrun!");
269 n
= snd_pcm_status_get_avail(status
);
271 /* We only use part of the buffer that matches our
272 * dynamically requested latency */
274 if (PA_UNLIKELY(n
<= u
->hwbuf_unused_frames
))
277 n
-= u
->hwbuf_unused_frames
;
279 if (u
->memchunk
.length
<= 0)
280 pa_sink_render(u
->sink
, n
* u
->frame_size
, &u
->memchunk
);
282 pa_assert(u
->memchunk
.length
> 0);
284 frames
= u
->memchunk
.length
/ u
->frame_size
;
289 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
290 frames
= snd_pcm_writei(u
->pcm_handle
, (const uint8_t*) p
+ u
->memchunk
.index
, frames
);
291 pa_memblock_release(u
->memchunk
.memblock
);
293 pa_assert(frames
!= 0);
295 if (PA_UNLIKELY(frames
< 0)) {
297 if (frames
== -EPIPE
)
298 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
300 if ((frames
= snd_pcm_recover(u
->pcm_handle
, frames
, 1)) == 0)
303 if (frames
== -EAGAIN
)
306 pa_log("Failed to write data to DSP: %s", snd_strerror(frames
));
310 u
->memchunk
.index
+= frames
* u
->frame_size
;
311 u
->memchunk
.length
-= frames
* u
->frame_size
;
313 if (u
->memchunk
.length
<= 0) {
314 pa_memblock_unref(u
->memchunk
.memblock
);
315 pa_memchunk_reset(&u
->memchunk
);
320 u
->frame_index
+= frames
;
322 if (PA_LIKELY(frames
>= n
))
327 static int update_smoother(struct userdata
*u
) {
328 snd_pcm_sframes_t delay
= 0;
331 pa_usec_t now1
, now2
;
334 pa_assert(u
->pcm_handle
);
336 /* Let's update the time smoother */
338 snd_pcm_avail_update(u
->pcm_handle
);
340 if (PA_UNLIKELY((err
= snd_pcm_delay(u
->pcm_handle
, &delay
)) < 0)) {
341 pa_log("Failed to get delay: %s", snd_strerror(err
));
345 frames
= u
->frame_index
- delay
;
347 pa_log_debug("frame_index = %llu, delay = %llu, p = %llu", (unsigned long long) u
->frame_index
, (unsigned long long) delay
, (unsigned long long) frames
);
349 now1
= pa_rtclock_usec();
350 now2
= pa_bytes_to_usec(frames
* u
->frame_size
, &u
->sink
->sample_spec
);
351 pa_smoother_put(u
->smoother
, now1
, now2
);
356 static pa_usec_t
sink_get_latency(struct userdata
*u
) {
362 delay
= u
->frame_index
- pa_smoother_get(u
->smoother
, pa_rtclock_usec());
365 r
= pa_bytes_to_usec(delay
* u
->frame_size
, &u
->sink
->sample_spec
);
367 if (u
->memchunk
.memblock
)
368 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
373 static int build_pollfd(struct userdata
*u
) {
375 struct pollfd
*pollfd
;
379 pa_assert(u
->pcm_handle
);
381 if ((n
= snd_pcm_poll_descriptors_count(u
->pcm_handle
)) < 0) {
382 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
386 if (u
->alsa_rtpoll_item
)
387 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
389 u
->alsa_rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, n
);
390 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, NULL
);
392 if ((err
= snd_pcm_poll_descriptors(u
->pcm_handle
, pollfd
, n
)) < 0) {
393 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
400 static int suspend(struct userdata
*u
) {
402 pa_assert(u
->pcm_handle
);
404 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
407 snd_pcm_drain(u
->pcm_handle
);
408 snd_pcm_close(u
->pcm_handle
);
409 u
->pcm_handle
= NULL
;
411 if (u
->alsa_rtpoll_item
) {
412 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
413 u
->alsa_rtpoll_item
= NULL
;
416 pa_log_info("Device suspended...");
421 static pa_usec_t
hw_sleep_time(struct userdata
*u
) {
426 usec
= pa_sink_get_requested_latency(u
->sink
);
429 usec
= pa_bytes_to_usec(u
->hwbuf_size
, &u
->sink
->sample_spec
);
431 pa_log_debug("hw buffer time: %u ms", (unsigned) (usec
/ PA_USEC_PER_MSEC
));
433 wm
= pa_bytes_to_usec(u
->tsched_watermark
, &u
->sink
->sample_spec
);
440 pa_log_debug("after watermark: %u ms", (unsigned) (usec
/ PA_USEC_PER_MSEC
));
445 static void update_hwbuf_unused_frames(struct userdata
*u
) {
451 if ((usec
= pa_sink_get_requested_latency(u
->sink
)) <= 0) {
452 /* Use the full buffer if noone asked us for anything
454 u
->hwbuf_unused_frames
= 0;
458 b
= pa_usec_to_bytes(usec
, &u
->sink
->sample_spec
);
460 /* We need at least one sample in our buffer */
462 if (PA_UNLIKELY(b
< u
->frame_size
))
465 u
->hwbuf_unused_frames
=
466 PA_LIKELY(b
< u
->hwbuf_size
) ?
467 ((u
->hwbuf_size
- b
) / u
->frame_size
) : 0;
470 static int update_sw_params(struct userdata
*u
) {
479 usec
= hw_sleep_time(u
);
481 avail_min
= pa_usec_to_bytes(usec
, &u
->sink
->sample_spec
);
489 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
, avail_min
)) < 0) {
490 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
494 update_hwbuf_unused_frames(u
);
499 static int unsuspend(struct userdata
*u
) {
504 snd_pcm_uframes_t period_size
;
507 pa_assert(!u
->pcm_handle
);
509 pa_log_info("Trying resume...");
511 snd_config_update_free_global();
512 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
513 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
517 ss
= u
->sink
->sample_spec
;
518 nfrags
= u
->nfragments
;
519 period_size
= u
->fragment_size
/ u
->frame_size
;
523 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, u
->hwbuf_size
/ u
->frame_size
, &b
, &d
, TRUE
)) < 0) {
524 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
528 if (b
!= u
->use_mmap
|| d
!= u
->use_tsched
) {
529 pa_log_warn("Resume failed, couldn't get original access mode.");
533 if (!pa_sample_spec_equal(&ss
, &u
->sink
->sample_spec
)) {
534 pa_log_warn("Resume failed, couldn't restore original sample settings.");
538 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
539 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
543 if (update_sw_params(u
) < 0)
546 if (build_pollfd(u
) < 0)
549 /* FIXME: We need to reload the volume somehow */
553 pa_log_info("Resumed successfully...");
559 snd_pcm_close(u
->pcm_handle
);
560 u
->pcm_handle
= NULL
;
566 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
567 struct userdata
*u
= PA_SINK(o
)->userdata
;
571 case PA_SINK_MESSAGE_GET_LATENCY
: {
575 r
= sink_get_latency(u
);
577 *((pa_usec_t
*) data
) = r
;
582 case PA_SINK_MESSAGE_SET_STATE
:
584 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
586 case PA_SINK_SUSPENDED
:
587 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
595 case PA_SINK_RUNNING
:
597 if (u
->sink
->thread_info
.state
== PA_SINK_INIT
) {
598 if (build_pollfd(u
) < 0)
602 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
603 if (unsuspend(u
) < 0)
609 case PA_SINK_UNLINKED
:
616 /* case PA_SINK_MESSAGE_ADD_INPUT: */
617 /* case PA_SINK_MESSAGE_REMOVE_INPUT: */
618 /* case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: { */
619 /* int r = pa_sink_process_msg(o, code, data, offset, chunk); */
620 /* update_hwbuf_unused_frames(u); */
625 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
628 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
629 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
632 pa_assert(u
->mixer_handle
);
634 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
637 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
638 pa_sink_get_volume(u
->sink
);
639 pa_sink_get_mute(u
->sink
);
645 static int sink_get_volume_cb(pa_sink
*s
) {
646 struct userdata
*u
= s
->userdata
;
651 pa_assert(u
->mixer_elem
);
653 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
656 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, u
->mixer_map
[i
]));
658 if (u
->hw_dB_supported
) {
660 if ((err
= snd_mixer_selem_get_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) >= 0) {
661 s
->volume
.values
[i
] = pa_sw_volume_from_dB(alsa_vol
/ 100.0);
665 u
->hw_dB_supported
= FALSE
;
668 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) < 0)
671 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
));
677 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
682 static int sink_set_volume_cb(pa_sink
*s
) {
683 struct userdata
*u
= s
->userdata
;
688 pa_assert(u
->mixer_elem
);
690 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
694 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, u
->mixer_map
[i
]));
696 vol
= PA_MIN(s
->volume
.values
[i
], PA_VOLUME_NORM
);
698 if (u
->hw_dB_supported
) {
699 alsa_vol
= (long) (pa_sw_volume_to_dB(vol
) * 100);
700 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_dB_min
, u
->hw_dB_max
);
702 if ((err
= snd_mixer_selem_set_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
, -1)) >= 0) {
704 if (snd_mixer_selem_get_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
) >= 0)
705 s
->volume
.values
[i
] = pa_sw_volume_from_dB(alsa_vol
/ 100.0);
710 u
->hw_dB_supported
= FALSE
;
714 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
715 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_volume_min
, u
->hw_volume_max
);
717 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
)) < 0)
720 if (snd_mixer_selem_get_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
) >= 0)
721 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
));
727 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
732 static int sink_get_mute_cb(pa_sink
*s
) {
733 struct userdata
*u
= s
->userdata
;
737 pa_assert(u
->mixer_elem
);
739 if ((err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
740 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
749 static int sink_set_mute_cb(pa_sink
*s
) {
750 struct userdata
*u
= s
->userdata
;
754 pa_assert(u
->mixer_elem
);
756 if ((err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
757 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
764 static void sink_update_requested_latency_cb(pa_sink
*s
) {
765 struct userdata
*u
= s
->userdata
;
772 static void thread_func(void *userdata
) {
773 struct userdata
*u
= userdata
;
777 pa_log_debug("Thread starting up");
779 if (u
->core
->realtime_scheduling
)
780 pa_make_realtime(u
->core
->realtime_priority
);
782 pa_thread_mq_install(&u
->thread_mq
);
783 pa_rtpoll_install(u
->rtpoll
);
785 /* update_hwbuf_unused_frames(u); */
790 pa_log_debug("loop");
792 /* Render some data and write it to the dsp */
793 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
796 if (u
->sink
->thread_info
.rewind_nbytes
> 0 && u
->use_tsched
) {
797 snd_pcm_sframes_t frames
, limit
, unused
;
799 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) u
->sink
->thread_info
.rewind_nbytes
);
801 frames
= u
->sink
->thread_info
.rewind_nbytes
/ u
->frame_size
;
803 if ((unused
= snd_pcm_avail_update(u
->pcm_handle
)) < 0) {
804 pa_log("snd_pcm_avail_update() failed: %s", snd_strerror(unused
));
808 limit
= (u
->hwbuf_size
/ u
->frame_size
) - unused
;
813 pa_log_debug("Limited to %lu bytes.", (unsigned long) frames
* u
->frame_size
);
815 if ((frames
= snd_pcm_rewind(u
->pcm_handle
, frames
)) < 0) {
816 pa_log("snd_pcm_rewind() failed: %s", snd_strerror(frames
));
820 if ((u
->sink
->thread_info
.rewind_nbytes
= frames
* u
->frame_size
) <= 0)
821 pa_log_info("Tried rewind, but was apparently not possible.");
823 u
->frame_index
-= frames
;
824 pa_log_debug("Rewound %lu bytes.", (unsigned long) u
->sink
->thread_info
.rewind_nbytes
);
825 pa_sink_process_rewind(u
->sink
);
830 if ((work_done
= mmap_write(u
)) < 0)
833 if ((work_done
= unix_write(u
)) < 0)
837 pa_log_debug("work_done = %i", work_done
);
842 pa_log_info("Starting playback.");
843 snd_pcm_start(u
->pcm_handle
);
846 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
849 if (update_smoother(u
) < 0)
854 pa_usec_t usec
, cusec
;
856 /* OK, the playback buffer is now full, let's
857 * calculate when to wake up next */
859 usec
= hw_sleep_time(u
);
861 pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) usec
/ PA_USEC_PER_MSEC
);
863 /* Convert from the sound card time domain to the
864 * system time domain */
865 cusec
= pa_smoother_translate(u
->smoother
, pa_rtclock_usec(), usec
);
867 pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec
/ PA_USEC_PER_MSEC
);
869 /* We don't trust the conversion, so we wake up whatever comes first */
870 pa_rtpoll_set_timer_relative(u
->rtpoll
, PA_MIN(usec
, cusec
));
873 } else if (u
->use_tsched
) {
875 /* OK, we're in an invalid state, let's disable our timers */
876 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
879 /* Hmm, nothing to do. Let's sleep */
880 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
886 /* Tell ALSA about this and process its response */
887 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
888 struct pollfd
*pollfd
;
889 unsigned short revents
= 0;
893 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
895 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
896 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
900 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
902 if (revents
& POLLERR
)
903 pa_log_warn("Got POLLERR from ALSA");
904 if (revents
& POLLNVAL
)
905 pa_log_warn("Got POLLNVAL from ALSA");
906 if (revents
& POLLHUP
)
907 pa_log_warn("Got POLLHUP from ALSA");
909 /* Try to recover from this error */
911 switch (snd_pcm_state(u
->pcm_handle
)) {
913 case SND_PCM_STATE_XRUN
:
914 if ((err
= snd_pcm_recover(u
->pcm_handle
, -EPIPE
, 1)) != 0) {
915 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
920 case SND_PCM_STATE_SUSPENDED
:
921 if ((err
= snd_pcm_recover(u
->pcm_handle
, -ESTRPIPE
, 1)) != 0) {
922 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
929 snd_pcm_drop(u
->pcm_handle
);
931 if ((err
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
932 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
939 pa_log_debug("alsa revents = %i", revents
);
944 /* If this was no regular exit from the loop we have to continue
945 * processing messages until we received PA_MESSAGE_SHUTDOWN */
946 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
947 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
950 pa_log_debug("Thread shutting down");
953 int pa__init(pa_module
*m
) {
955 pa_modargs
*ma
= NULL
;
956 struct userdata
*u
= NULL
;
960 uint32_t nfrags
, hwbuf_size
, frag_size
, tsched_size
, tsched_watermark
;
961 snd_pcm_uframes_t period_frames
, tsched_frames
;
963 snd_pcm_info_t
*pcm_info
= NULL
;
966 char *name_buf
= NULL
;
967 pa_bool_t namereg_fail
;
968 pa_bool_t use_mmap
= TRUE
, b
, use_tsched
= TRUE
, d
;
970 pa_sink_new_data data
;
971 static const char * const class_table
[SND_PCM_CLASS_LAST
+1] = {
972 [SND_PCM_CLASS_GENERIC
] = "sound",
973 [SND_PCM_CLASS_MULTI
] = NULL
,
974 [SND_PCM_CLASS_MODEM
] = "modem",
975 [SND_PCM_CLASS_DIGITIZER
] = NULL
978 snd_pcm_info_alloca(&pcm_info
);
982 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
983 pa_log("Failed to parse module arguments");
987 ss
= m
->core
->default_sample_spec
;
988 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
989 pa_log("Failed to parse sample specification and channel map");
993 frame_size
= pa_frame_size(&ss
);
995 nfrags
= m
->core
->default_n_fragments
;
996 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*PA_USEC_PER_MSEC
, &ss
);
998 frag_size
= frame_size
;
999 tsched_size
= pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC
, &ss
);
1000 tsched_watermark
= pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC
, &ss
);
1002 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 ||
1003 pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0 ||
1004 pa_modargs_get_value_u32(ma
, "tsched_buffer_size", &tsched_size
) < 0 ||
1005 pa_modargs_get_value_u32(ma
, "tsched_buffer_watermark", &tsched_watermark
) < 0) {
1006 pa_log("Failed to parse buffer metrics");
1010 hwbuf_size
= frag_size
* nfrags
;
1011 period_frames
= frag_size
/frame_size
;
1012 tsched_frames
= tsched_size
/frame_size
;
1014 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
1015 pa_log("Failed to parse mmap argument.");
1019 if (pa_modargs_get_value_boolean(ma
, "tsched", &use_tsched
) < 0) {
1020 pa_log("Failed to parse timer_scheduling argument.");
1024 if (use_tsched
&& !pa_rtclock_hrtimer()) {
1025 pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
1029 u
= pa_xnew0(struct userdata
, 1);
1033 u
->use_mmap
= use_mmap
;
1034 u
->use_tsched
= use_tsched
;
1036 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
1037 u
->rtpoll
= pa_rtpoll_new();
1038 u
->alsa_rtpoll_item
= NULL
;
1039 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
1041 u
->smoother
= pa_smoother_new(DEFAULT_TSCHED_BUFFER_USEC
*2, DEFAULT_TSCHED_BUFFER_USEC
*2, TRUE
);
1042 usec
= pa_rtclock_usec();
1043 pa_smoother_set_time_offset(u
->smoother
, usec
);
1044 pa_smoother_pause(u
->smoother
, usec
);
1046 snd_config_update_free_global();
1051 if ((dev_id
= pa_modargs_get_value(ma
, "device_id", NULL
))) {
1053 if (!(u
->pcm_handle
= pa_alsa_open_by_device_id(
1057 SND_PCM_STREAM_PLAYBACK
,
1058 &nfrags
, &period_frames
, tsched_frames
,
1065 if (!(u
->pcm_handle
= pa_alsa_open_by_device_string(
1066 pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
),
1069 SND_PCM_STREAM_PLAYBACK
,
1070 &nfrags
, &period_frames
, tsched_frames
,
1076 pa_assert(u
->device_name
);
1077 pa_log_info("Successfully opened device %s.", u
->device_name
);
1079 if (use_mmap
&& !b
) {
1080 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1081 u
->use_mmap
= use_mmap
= FALSE
;
1084 if (use_tsched
&& (!b
|| !d
)) {
1085 pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1086 u
->use_tsched
= use_tsched
= FALSE
;
1090 pa_log_info("Successfully enabled mmap() mode.");
1093 pa_log_info("Successfully enabled timer-based scheduling mode.");
1095 if ((err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
1096 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
1100 /* ALSA might tweak the sample spec, so recalculate the frame size */
1101 frame_size
= pa_frame_size(&ss
);
1103 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
1104 pa_log_warn("Error opening mixer: %s", snd_strerror(err
));
1106 pa_bool_t found
= FALSE
;
1108 if (pa_alsa_prepare_mixer(u
->mixer_handle
, u
->device_name
) >= 0)
1111 char *md
= pa_sprintf_malloc("hw:%s", dev_id
);
1113 if (strcmp(u
->device_name
, md
))
1114 if (pa_alsa_prepare_mixer(u
->mixer_handle
, md
) >= 0)
1121 if (!(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Master", "PCM")))
1125 snd_mixer_close(u
->mixer_handle
);
1126 u
->mixer_handle
= NULL
;
1130 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
1131 namereg_fail
= TRUE
;
1133 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", u
->device_name
);
1134 namereg_fail
= FALSE
;
1137 pa_sink_new_data_init(&data
);
1138 data
.driver
= __FILE__
;
1140 pa_sink_new_data_set_name(&data
, name
);
1141 data
.namereg_fail
= namereg_fail
;
1142 pa_sink_new_data_set_sample_spec(&data
, &ss
);
1143 pa_sink_new_data_set_channel_map(&data
, &map
);
1145 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
1146 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "alsa");
1147 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, snd_pcm_info_get_name(pcm_info
));
1149 if (class_table
[snd_pcm_info_get_class(pcm_info
)])
1150 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, class_table
[snd_pcm_info_get_class(pcm_info
)]);
1152 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, u
->use_tsched
? "mmap_rewrite" : (u
->use_mmap
? "mmap" : "serial"));
1154 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1155 pa_sink_new_data_done(&data
);
1159 pa_log("Failed to create sink object");
1163 u
->sink
->parent
.process_msg
= sink_process_msg
;
1164 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1165 u
->sink
->userdata
= u
;
1167 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1168 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1170 u
->frame_size
= frame_size
;
1171 u
->fragment_size
= frag_size
= period_frames
* frame_size
;
1172 u
->nfragments
= nfrags
;
1173 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
1174 u
->hwbuf_unused_frames
= 0;
1175 u
->tsched_watermark
= tsched_watermark
;
1177 u
->hw_dB_supported
= FALSE
;
1178 u
->hw_dB_min
= u
->hw_dB_max
= 0;
1179 u
->hw_volume_min
= u
->hw_volume_max
= 0;
1181 u
->sink
->thread_info
.max_rewind
= use_tsched
? u
->hwbuf_size
: 0;
1184 u
->sink
->min_latency
= pa_bytes_to_usec(u
->hwbuf_size
, &ss
);
1186 pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms",
1187 nfrags
, (long unsigned) u
->fragment_size
,
1188 (double) pa_bytes_to_usec(u
->hwbuf_size
, &ss
) / PA_USEC_PER_MSEC
);
1191 pa_log_info("Time scheduling watermark is %0.2fms",
1192 (double) pa_bytes_to_usec(u
->tsched_watermark
, &ss
) / PA_USEC_PER_MSEC
);
1194 if (update_sw_params(u
) < 0)
1197 pa_memchunk_reset(&u
->memchunk
);
1199 if (u
->mixer_handle
) {
1200 pa_assert(u
->mixer_elem
);
1202 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
))
1204 if (pa_alsa_calc_mixer_map(u
->mixer_elem
, &map
, u
->mixer_map
, TRUE
) >= 0 &&
1205 snd_mixer_selem_get_playback_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
) >= 0) {
1207 pa_bool_t suitable
= TRUE
;
1209 pa_log_info("Volume ranges from %li to %li.", u
->hw_volume_min
, u
->hw_volume_max
);
1211 if (u
->hw_volume_min
> u
->hw_volume_max
) {
1213 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
);
1216 } else if (u
->hw_volume_max
- u
->hw_volume_min
< 3) {
1218 pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
1221 } else if (snd_mixer_selem_get_playback_dB_range(u
->mixer_elem
, &u
->hw_dB_min
, &u
->hw_dB_max
) >= 0) {
1223 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);
1225 /* Let's see if this thing actually is useful for muting */
1226 if (u
->hw_dB_min
> -6000) {
1227 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);
1230 } else if (u
->hw_dB_max
< 0) {
1232 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);
1235 } else if (u
->hw_dB_min
>= u
->hw_dB_max
) {
1237 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);
1242 if (u
->hw_dB_max
> 0) {
1243 /* dB > 0 means overamplification, and clipping, we don't want that here */
1244 pa_log_info("Device can do overamplification for %0.2f dB. Limiting to 0 db", ((double) u
->hw_dB_max
) / 100);
1248 u
->hw_dB_supported
= TRUE
;
1253 u
->sink
->get_volume
= sink_get_volume_cb
;
1254 u
->sink
->set_volume
= sink_set_volume_cb
;
1255 u
->sink
->flags
|= PA_SINK_HW_VOLUME_CTRL
| (u
->hw_dB_supported
? PA_SINK_DECIBEL_VOLUME
: 0);
1256 pa_log_info("Using hardware volume control. %s dB scale.", u
->hw_dB_supported
? "Using" : "Not using");
1259 pa_log_info("Using software volume control. Trying to reset sound card to 0 dB.");
1260 pa_alsa_0dB_playback(u
->mixer_elem
);
1264 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
1265 u
->sink
->get_mute
= sink_get_mute_cb
;
1266 u
->sink
->set_mute
= sink_set_mute_cb
;
1267 u
->sink
->flags
|= PA_SINK_HW_MUTE_CTRL
;
1270 u
->mixer_fdl
= pa_alsa_fdlist_new();
1272 if (pa_alsa_fdlist_set_mixer(u
->mixer_fdl
, u
->mixer_handle
, m
->core
->mainloop
) < 0) {
1273 pa_log("Failed to initialize file descriptor monitoring");
1277 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
1278 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
1280 u
->mixer_fdl
= NULL
;
1282 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
1283 pa_log("Failed to create thread.");
1287 /* Get initial mixer settings */
1288 if (data
.volume_is_set
) {
1289 if (u
->sink
->set_volume
)
1290 u
->sink
->set_volume(u
->sink
);
1292 if (u
->sink
->get_volume
)
1293 u
->sink
->get_volume(u
->sink
);
1296 if (data
.muted_is_set
) {
1297 if (u
->sink
->set_mute
)
1298 u
->sink
->set_mute(u
->sink
);
1300 if (u
->sink
->get_mute
)
1301 u
->sink
->get_mute(u
->sink
);
1304 pa_sink_put(u
->sink
);
1306 pa_modargs_free(ma
);
1313 pa_modargs_free(ma
);
1320 void pa__done(pa_module
*m
) {
1325 if (!(u
= m
->userdata
))
1329 pa_sink_unlink(u
->sink
);
1332 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1333 pa_thread_free(u
->thread
);
1336 pa_thread_mq_done(&u
->thread_mq
);
1339 pa_sink_unref(u
->sink
);
1341 if (u
->memchunk
.memblock
)
1342 pa_memblock_unref(u
->memchunk
.memblock
);
1344 if (u
->alsa_rtpoll_item
)
1345 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
1348 pa_rtpoll_free(u
->rtpoll
);
1351 pa_alsa_fdlist_free(u
->mixer_fdl
);
1353 if (u
->mixer_handle
)
1354 snd_mixer_close(u
->mixer_handle
);
1356 if (u
->pcm_handle
) {
1357 snd_pcm_drop(u
->pcm_handle
);
1358 snd_pcm_close(u
->pcm_handle
);
1362 pa_smoother_free(u
->smoother
);
1364 pa_xfree(u
->device_name
);
1367 snd_config_update_free_global();