4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <asoundlib.h>
38 #include "sample-util.h"
39 #include "alsa-util.h"
43 PA_MODULE_AUTHOR("Lennart Poettering")
44 PA_MODULE_DESCRIPTION("ALSA Sink")
45 PA_MODULE_VERSION(PACKAGE_VERSION
)
48 snd_pcm_t
*pcm_handle
;
50 struct pa_io_event
**io_events
;
53 size_t frame_size
, fragment_size
;
54 struct pa_memchunk memchunk
, silence
;
55 struct pa_module
*module
;
58 static const char* const valid_modargs
[] = {
69 #define DEFAULT_SINK_NAME "alsa_output"
70 #define DEFAULT_DEVICE "plughw:0,0"
72 static void update_usage(struct userdata
*u
) {
73 pa_module_set_used(u
->module
,
74 (u
->sink
? pa_idxset_ncontents(u
->sink
->inputs
) : 0) +
75 (u
->sink
? pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
) : 0));
78 static void xrun_recovery(struct userdata
*u
) {
81 pa_log(__FILE__
": *** ALSA-XRUN (playback) ***\n");
83 if (snd_pcm_prepare(u
->pcm_handle
) < 0)
84 pa_log(__FILE__
": snd_pcm_prepare() failed\n");
87 static void do_write(struct userdata
*u
) {
93 struct pa_memchunk
*memchunk
= NULL
;
94 snd_pcm_sframes_t frames
;
96 if (u
->memchunk
.memblock
)
97 memchunk
= &u
->memchunk
;
99 if (pa_sink_render(u
->sink
, u
->fragment_size
, &u
->memchunk
) < 0)
100 memchunk
= &u
->silence
;
102 memchunk
= &u
->memchunk
;
105 assert(memchunk
->memblock
&& memchunk
->memblock
->data
&& memchunk
->length
&& memchunk
->memblock
->length
&& (memchunk
->length
% u
->frame_size
) == 0);
107 if ((frames
= snd_pcm_writei(u
->pcm_handle
, (uint8_t*) memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
/ u
->frame_size
)) < 0) {
108 if (frames
== -EAGAIN
)
111 if (frames
== -EPIPE
) {
116 pa_log(__FILE__
": snd_pcm_writei() failed\n");
120 if (memchunk
== &u
->memchunk
) {
121 size_t l
= frames
* u
->frame_size
;
122 memchunk
->index
+= l
;
123 memchunk
->length
-= l
;
125 if (memchunk
->length
== 0) {
126 pa_memblock_unref(memchunk
->memblock
);
127 memchunk
->memblock
= NULL
;
128 memchunk
->index
= memchunk
->length
= 0;
136 static void io_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
137 struct userdata
*u
= userdata
;
140 if (snd_pcm_state(u
->pcm_handle
) == SND_PCM_STATE_XRUN
)
146 static pa_usec_t
sink_get_latency_cb(struct pa_sink
*s
) {
148 struct userdata
*u
= s
->userdata
;
149 snd_pcm_sframes_t frames
;
150 assert(s
&& u
&& u
->sink
);
152 if (snd_pcm_delay(u
->pcm_handle
, &frames
) < 0) {
153 pa_log(__FILE__
": failed to get delay\n");
154 s
->get_latency
= NULL
;
161 r
+= pa_bytes_to_usec(frames
* u
->frame_size
, &s
->sample_spec
);
163 if (u
->memchunk
.memblock
)
164 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
);
169 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
170 struct pa_modargs
*ma
= NULL
;
172 struct userdata
*u
= NULL
;
174 struct pa_sample_spec ss
;
175 unsigned periods
, fragsize
;
176 snd_pcm_uframes_t buffer_size
;
179 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
180 pa_log(__FILE__
": failed to parse module arguments\n");
184 ss
= c
->default_sample_spec
;
185 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
186 pa_log(__FILE__
": failed to parse sample specification\n");
189 frame_size
= pa_frame_size(&ss
);
193 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
194 pa_log(__FILE__
": failed to parse buffer metrics\n");
197 buffer_size
= fragsize
/frame_size
*periods
;
199 u
= pa_xmalloc0(sizeof(struct userdata
));
203 if (snd_pcm_open(&u
->pcm_handle
, dev
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
) < 0) {
204 pa_log(__FILE__
": Error opening PCM device %s\n", dev
);
208 if (pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &buffer_size
) < 0) {
209 pa_log(__FILE__
": Failed to set hardware parameters\n");
213 u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
);
216 u
->sink
->get_latency
= sink_get_latency_cb
;
217 u
->sink
->userdata
= u
;
218 pa_sink_set_owner(u
->sink
, m
);
219 u
->sink
->description
= pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev
);
221 if (pa_create_io_events(u
->pcm_handle
, c
->mainloop
, &u
->io_events
, &u
->n_io_events
, io_callback
, u
) < 0) {
222 pa_log(__FILE__
": failed to obtain file descriptors\n");
226 u
->frame_size
= frame_size
;
227 u
->fragment_size
= buffer_size
*u
->frame_size
/periods
;
229 pa_log(__FILE__
": using %u fragments of size %u bytes.\n", periods
, u
->fragment_size
);
231 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->fragment_size
, c
->memblock_stat
);
232 assert(u
->silence
.memblock
);
233 pa_silence_memblock(u
->silence
.memblock
, &ss
);
234 u
->silence
.index
= 0;
236 u
->memchunk
.memblock
= NULL
;
237 u
->memchunk
.index
= u
->memchunk
.length
= 0;
255 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
259 if (!(u
= m
->userdata
))
263 pa_sink_disconnect(u
->sink
);
264 pa_sink_unref(u
->sink
);
268 pa_free_io_events(c
->mainloop
, u
->io_events
, u
->n_io_events
);
271 snd_pcm_drop(u
->pcm_handle
);
272 snd_pcm_close(u
->pcm_handle
);
275 if (u
->memchunk
.memblock
)
276 pa_memblock_unref(u
->memchunk
.memblock
);
277 if (u
->silence
.memblock
)
278 pa_memblock_unref(u
->silence
.memblock
);