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(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(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 avail_min
= (snd_pcm_uframes_t
) -1;
504 pa_log("setting avail_min=%lu", (unsigned long) avail_min
);
506 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
, avail_min
)) < 0) {
507 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
511 update_hwbuf_unused_frames(u
);
513 pa_log("hwbuf_unused_frames=%lu", (unsigned long) u
->hwbuf_unused_frames
);
518 static int unsuspend(struct userdata
*u
) {
523 snd_pcm_uframes_t period_size
;
526 pa_assert(!u
->pcm_handle
);
528 pa_log_info("Trying resume...");
530 snd_config_update_free_global();
531 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
532 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
536 ss
= u
->sink
->sample_spec
;
537 nfrags
= u
->nfragments
;
538 period_size
= u
->fragment_size
/ u
->frame_size
;
542 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, u
->hwbuf_size
/ u
->frame_size
, &b
, &d
, TRUE
)) < 0) {
543 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
547 if (b
!= u
->use_mmap
|| d
!= u
->use_tsched
) {
548 pa_log_warn("Resume failed, couldn't get original access mode.");
552 if (!pa_sample_spec_equal(&ss
, &u
->sink
->sample_spec
)) {
553 pa_log_warn("Resume failed, couldn't restore original sample settings.");
557 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
558 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
562 if (update_sw_params(u
) < 0)
565 if (build_pollfd(u
) < 0)
568 /* FIXME: We need to reload the volume somehow */
572 pa_log_info("Resumed successfully...");
578 snd_pcm_close(u
->pcm_handle
);
579 u
->pcm_handle
= NULL
;
585 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
586 struct userdata
*u
= PA_SINK(o
)->userdata
;
590 case PA_SINK_MESSAGE_GET_LATENCY
: {
594 r
= sink_get_latency(u
);
596 *((pa_usec_t
*) data
) = r
;
601 case PA_SINK_MESSAGE_SET_STATE
:
603 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
605 case PA_SINK_SUSPENDED
:
606 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
614 case PA_SINK_RUNNING
:
616 if (u
->sink
->thread_info
.state
== PA_SINK_INIT
) {
617 if (build_pollfd(u
) < 0)
621 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
622 if (unsuspend(u
) < 0)
628 case PA_SINK_UNLINKED
:
635 /* case PA_SINK_MESSAGE_ADD_INPUT: */
636 /* case PA_SINK_MESSAGE_REMOVE_INPUT: */
637 /* case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: { */
638 /* int r = pa_sink_process_msg(o, code, data, offset, chunk); */
639 /* update_hwbuf_unused_frames(u); */
644 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
647 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
648 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
651 pa_assert(u
->mixer_handle
);
653 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
656 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
657 pa_sink_get_volume(u
->sink
);
658 pa_sink_get_mute(u
->sink
);
664 static int sink_get_volume_cb(pa_sink
*s
) {
665 struct userdata
*u
= s
->userdata
;
670 pa_assert(u
->mixer_elem
);
672 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
675 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, u
->mixer_map
[i
]));
677 if (u
->hw_dB_supported
) {
679 if ((err
= snd_mixer_selem_get_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) >= 0) {
680 s
->volume
.values
[i
] = pa_sw_volume_from_dB(alsa_vol
/ 100.0);
684 u
->hw_dB_supported
= FALSE
;
687 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) < 0)
690 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
));
696 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
701 static int sink_set_volume_cb(pa_sink
*s
) {
702 struct userdata
*u
= s
->userdata
;
707 pa_assert(u
->mixer_elem
);
709 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
713 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, u
->mixer_map
[i
]));
715 vol
= PA_MIN(s
->volume
.values
[i
], PA_VOLUME_NORM
);
717 if (u
->hw_dB_supported
) {
718 alsa_vol
= (long) (pa_sw_volume_to_dB(vol
) * 100);
719 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_dB_min
, u
->hw_dB_max
);
721 if ((err
= snd_mixer_selem_set_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
, -1)) >= 0) {
723 if (snd_mixer_selem_get_playback_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
) >= 0)
724 s
->volume
.values
[i
] = pa_sw_volume_from_dB(alsa_vol
/ 100.0);
729 u
->hw_dB_supported
= FALSE
;
733 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
734 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_volume_min
, u
->hw_volume_max
);
736 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
)) < 0)
739 if (snd_mixer_selem_get_playback_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
) >= 0)
740 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
));
746 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
751 static int sink_get_mute_cb(pa_sink
*s
) {
752 struct userdata
*u
= s
->userdata
;
756 pa_assert(u
->mixer_elem
);
758 if ((err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
759 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
768 static int sink_set_mute_cb(pa_sink
*s
) {
769 struct userdata
*u
= s
->userdata
;
773 pa_assert(u
->mixer_elem
);
775 if ((err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
776 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
783 static void sink_update_requested_latency_cb(pa_sink
*s
) {
784 struct userdata
*u
= s
->userdata
;
791 static void thread_func(void *userdata
) {
792 struct userdata
*u
= userdata
;
796 pa_log_debug("Thread starting up");
798 if (u
->core
->realtime_scheduling
)
799 pa_make_realtime(u
->core
->realtime_priority
);
801 pa_thread_mq_install(&u
->thread_mq
);
802 pa_rtpoll_install(u
->rtpoll
);
804 /* update_hwbuf_unused_frames(u); */
809 pa_log_debug("loop");
811 /* Render some data and write it to the dsp */
812 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
815 if (u
->sink
->thread_info
.rewind_nbytes
> 0 && u
->use_tsched
) {
816 snd_pcm_sframes_t frames
, limit
, unused
;
818 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) u
->sink
->thread_info
.rewind_nbytes
);
820 frames
= u
->sink
->thread_info
.rewind_nbytes
/ u
->frame_size
;
822 snd_pcm_hwsync(u
->pcm_handle
);
823 if ((unused
= snd_pcm_avail_update(u
->pcm_handle
)) < 0) {
824 pa_log("snd_pcm_avail_update() failed: %s", snd_strerror(unused
));
828 limit
= (u
->hwbuf_size
/ u
->frame_size
) - unused
;
837 pa_log_debug("Limited to %lu bytes.", (unsigned long) frames
* u
->frame_size
);
839 if ((frames
= snd_pcm_rewind(u
->pcm_handle
, frames
)) < 0) {
840 pa_log("snd_pcm_rewind() failed: %s", snd_strerror(frames
));
844 if ((u
->sink
->thread_info
.rewind_nbytes
= frames
* u
->frame_size
) <= 0)
845 pa_log_info("Tried rewind, but was apparently not possible.");
847 u
->frame_index
-= frames
;
848 pa_log_debug("Rewound %lu bytes.", (unsigned long) u
->sink
->thread_info
.rewind_nbytes
);
849 pa_sink_process_rewind(u
->sink
);
852 pa_log_debug("Mhmm, actually there is nothing to rewind.");
857 if ((work_done
= mmap_write(u
)) < 0)
860 if ((work_done
= unix_write(u
)) < 0)
864 pa_log_debug("work_done = %i", work_done
);
869 pa_log_info("Starting playback.");
870 snd_pcm_start(u
->pcm_handle
);
873 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
880 pa_usec_t usec
, cusec
;
882 /* OK, the playback buffer is now full, let's
883 * calculate when to wake up next */
885 usec
= hw_sleep_time(u
);
887 pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) usec
/ PA_USEC_PER_MSEC
);
889 /* Convert from the sound card time domain to the
890 * system time domain */
891 cusec
= pa_smoother_translate(u
->smoother
, pa_rtclock_usec(), usec
);
893 pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec
/ PA_USEC_PER_MSEC
);
895 /* We don't trust the conversion, so we wake up whatever comes first */
896 pa_rtpoll_set_timer_relative(u
->rtpoll
, PA_MIN(usec
, cusec
));
899 } else if (u
->use_tsched
)
901 /* OK, we're in an invalid state, let's disable our timers */
902 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
904 /* Hmm, nothing to do. Let's sleep */
905 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
911 /* Tell ALSA about this and process its response */
912 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
913 struct pollfd
*pollfd
;
914 unsigned short revents
= 0;
918 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
920 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
921 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
925 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
926 snd_pcm_state_t state
;
928 if (revents
& POLLERR
)
929 pa_log_warn("Got POLLERR from ALSA");
930 if (revents
& POLLNVAL
)
931 pa_log_warn("Got POLLNVAL from ALSA");
932 if (revents
& POLLHUP
)
933 pa_log_warn("Got POLLHUP from ALSA");
935 state
= snd_pcm_state(u
->pcm_handle
);
936 pa_log_warn("PCM state is %s", snd_pcm_state_name(state
));
938 /* Try to recover from this error */
942 case SND_PCM_STATE_XRUN
:
943 if ((err
= snd_pcm_recover(u
->pcm_handle
, -EPIPE
, 1)) != 0) {
944 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
949 case SND_PCM_STATE_SUSPENDED
:
950 if ((err
= snd_pcm_recover(u
->pcm_handle
, -ESTRPIPE
, 1)) != 0) {
951 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
958 snd_pcm_drop(u
->pcm_handle
);
960 if ((err
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
961 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
970 pa_log_debug("alsa revents = %i", revents
);
975 /* If this was no regular exit from the loop we have to continue
976 * processing messages until we received PA_MESSAGE_SHUTDOWN */
977 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
978 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
981 pa_log_debug("Thread shutting down");
984 int pa__init(pa_module
*m
) {
986 pa_modargs
*ma
= NULL
;
987 struct userdata
*u
= NULL
;
991 uint32_t nfrags
, hwbuf_size
, frag_size
, tsched_size
, tsched_watermark
;
992 snd_pcm_uframes_t period_frames
, tsched_frames
;
994 snd_pcm_info_t
*pcm_info
= NULL
;
997 char *name_buf
= NULL
;
998 pa_bool_t namereg_fail
;
999 pa_bool_t use_mmap
= TRUE
, b
, use_tsched
= TRUE
, d
;
1001 pa_sink_new_data data
;
1002 static const char * const class_table
[SND_PCM_CLASS_LAST
+1] = {
1003 [SND_PCM_CLASS_GENERIC
] = "sound",
1004 [SND_PCM_CLASS_MULTI
] = NULL
,
1005 [SND_PCM_CLASS_MODEM
] = "modem",
1006 [SND_PCM_CLASS_DIGITIZER
] = NULL
1009 snd_pcm_info_alloca(&pcm_info
);
1013 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1014 pa_log("Failed to parse module arguments");
1018 ss
= m
->core
->default_sample_spec
;
1019 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
1020 pa_log("Failed to parse sample specification and channel map");
1024 frame_size
= pa_frame_size(&ss
);
1026 nfrags
= m
->core
->default_n_fragments
;
1027 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*PA_USEC_PER_MSEC
, &ss
);
1029 frag_size
= frame_size
;
1030 tsched_size
= pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC
, &ss
);
1031 tsched_watermark
= pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC
, &ss
);
1033 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 ||
1034 pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0 ||
1035 pa_modargs_get_value_u32(ma
, "tsched_buffer_size", &tsched_size
) < 0 ||
1036 pa_modargs_get_value_u32(ma
, "tsched_buffer_watermark", &tsched_watermark
) < 0) {
1037 pa_log("Failed to parse buffer metrics");
1041 hwbuf_size
= frag_size
* nfrags
;
1042 period_frames
= frag_size
/frame_size
;
1043 tsched_frames
= tsched_size
/frame_size
;
1045 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
1046 pa_log("Failed to parse mmap argument.");
1050 if (pa_modargs_get_value_boolean(ma
, "tsched", &use_tsched
) < 0) {
1051 pa_log("Failed to parse timer_scheduling argument.");
1055 if (use_tsched
&& !pa_rtclock_hrtimer()) {
1056 pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
1060 u
= pa_xnew0(struct userdata
, 1);
1064 u
->use_mmap
= use_mmap
;
1065 u
->use_tsched
= use_tsched
;
1067 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
1068 u
->rtpoll
= pa_rtpoll_new();
1069 u
->alsa_rtpoll_item
= NULL
;
1070 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
1072 u
->smoother
= pa_smoother_new(DEFAULT_TSCHED_BUFFER_USEC
*2, DEFAULT_TSCHED_BUFFER_USEC
*2, TRUE
);
1073 usec
= pa_rtclock_usec();
1074 pa_smoother_set_time_offset(u
->smoother
, usec
);
1075 pa_smoother_pause(u
->smoother
, usec
);
1077 snd_config_update_free_global();
1082 if ((dev_id
= pa_modargs_get_value(ma
, "device_id", NULL
))) {
1084 if (!(u
->pcm_handle
= pa_alsa_open_by_device_id(
1088 SND_PCM_STREAM_PLAYBACK
,
1089 &nfrags
, &period_frames
, tsched_frames
,
1096 if (!(u
->pcm_handle
= pa_alsa_open_by_device_string(
1097 pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
),
1100 SND_PCM_STREAM_PLAYBACK
,
1101 &nfrags
, &period_frames
, tsched_frames
,
1107 pa_assert(u
->device_name
);
1108 pa_log_info("Successfully opened device %s.", u
->device_name
);
1110 if (use_mmap
&& !b
) {
1111 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1112 u
->use_mmap
= use_mmap
= FALSE
;
1115 if (use_tsched
&& (!b
|| !d
)) {
1116 pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1117 u
->use_tsched
= use_tsched
= FALSE
;
1121 pa_log_info("Successfully enabled mmap() mode.");
1124 pa_log_info("Successfully enabled timer-based scheduling mode.");
1126 if ((err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
1127 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
1131 /* ALSA might tweak the sample spec, so recalculate the frame size */
1132 frame_size
= pa_frame_size(&ss
);
1134 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
1135 pa_log_warn("Error opening mixer: %s", snd_strerror(err
));
1137 pa_bool_t found
= FALSE
;
1139 if (pa_alsa_prepare_mixer(u
->mixer_handle
, u
->device_name
) >= 0)
1142 snd_pcm_info_t
*info
;
1144 snd_pcm_info_alloca(&info
);
1146 if (snd_pcm_info(u
->pcm_handle
, info
) >= 0) {
1150 if ((card
= snd_pcm_info_get_card(info
)) >= 0) {
1152 md
= pa_sprintf_malloc("hw:%i", card
);
1154 if (strcmp(u
->device_name
, md
))
1155 if (pa_alsa_prepare_mixer(u
->mixer_handle
, md
) >= 0)
1163 if (!(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Master", "PCM")))
1167 snd_mixer_close(u
->mixer_handle
);
1168 u
->mixer_handle
= NULL
;
1172 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
1173 namereg_fail
= TRUE
;
1175 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", u
->device_name
);
1176 namereg_fail
= FALSE
;
1179 pa_sink_new_data_init(&data
);
1180 data
.driver
= __FILE__
;
1182 pa_sink_new_data_set_name(&data
, name
);
1183 data
.namereg_fail
= namereg_fail
;
1184 pa_sink_new_data_set_sample_spec(&data
, &ss
);
1185 pa_sink_new_data_set_channel_map(&data
, &map
);
1187 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
1188 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_API
, "alsa");
1189 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, snd_pcm_info_get_name(pcm_info
));
1191 if (class_table
[snd_pcm_info_get_class(pcm_info
)])
1192 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, class_table
[snd_pcm_info_get_class(pcm_info
)]);
1194 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, u
->use_tsched
? "mmap_rewrite" : (u
->use_mmap
? "mmap" : "serial"));
1196 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_HARDWARE
|PA_SINK_LATENCY
);
1197 pa_sink_new_data_done(&data
);
1201 pa_log("Failed to create sink object");
1205 u
->sink
->parent
.process_msg
= sink_process_msg
;
1206 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1207 u
->sink
->userdata
= u
;
1209 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1210 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1212 u
->frame_size
= frame_size
;
1213 u
->fragment_size
= frag_size
= period_frames
* frame_size
;
1214 u
->nfragments
= nfrags
;
1215 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
1216 u
->hwbuf_unused_frames
= 0;
1217 u
->tsched_watermark
= tsched_watermark
;
1219 u
->hw_dB_supported
= FALSE
;
1220 u
->hw_dB_min
= u
->hw_dB_max
= 0;
1221 u
->hw_volume_min
= u
->hw_volume_max
= 0;
1223 u
->sink
->thread_info
.max_rewind
= use_tsched
? u
->hwbuf_size
: 0;
1226 u
->sink
->min_latency
= pa_bytes_to_usec(u
->hwbuf_size
, &ss
);
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();