]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-sink.c
Reorganised the source tree. We now have src/ with a couple of subdirs:
[pulseaudio] / src / modules / module-alsa-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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.
10
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.
15
16 You should have received a copy of the GNU Lesser 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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdio.h>
28
29 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #else
32 #include "poll.h"
33 #endif
34
35 #include <asoundlib.h>
36
37 #include <polypcore/core.h>
38 #include <polypcore/module.h>
39 #include <polypcore/memchunk.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/modargs.h>
42 #include <polypcore/util.h>
43 #include <polypcore/sample-util.h>
44 #include <polypcore/alsa-util.h>
45 #include <polypcore/xmalloc.h>
46 #include <polypcore/log.h>
47
48 #include "module-alsa-sink-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("ALSA Sink")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE("sink_name=<name for the sink> device=<ALSA device> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
54
55 struct userdata {
56 snd_pcm_t *pcm_handle;
57 pa_sink *sink;
58 pa_io_event **io_events;
59 unsigned n_io_events;
60
61 size_t frame_size, fragment_size;
62 pa_memchunk memchunk, silence;
63 pa_module *module;
64 };
65
66 static const char* const valid_modargs[] = {
67 "device",
68 "sink_name",
69 "format",
70 "channels",
71 "rate",
72 "fragments",
73 "fragment_size",
74 NULL
75 };
76
77 #define DEFAULT_SINK_NAME "alsa_output"
78 #define DEFAULT_DEVICE "default"
79
80 static void update_usage(struct userdata *u) {
81 pa_module_set_used(u->module,
82 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
83 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0));
84 }
85
86 static void xrun_recovery(struct userdata *u) {
87 assert(u);
88
89 pa_log(__FILE__": *** ALSA-XRUN (playback) ***\n");
90
91 if (snd_pcm_prepare(u->pcm_handle) < 0)
92 pa_log(__FILE__": snd_pcm_prepare() failed\n");
93 }
94
95 static void do_write(struct userdata *u) {
96 assert(u);
97
98 update_usage(u);
99
100 for (;;) {
101 pa_memchunk *memchunk = NULL;
102 snd_pcm_sframes_t frames;
103
104 if (u->memchunk.memblock)
105 memchunk = &u->memchunk;
106 else {
107 if (pa_sink_render(u->sink, u->fragment_size, &u->memchunk) < 0)
108 memchunk = &u->silence;
109 else
110 memchunk = &u->memchunk;
111 }
112
113 assert(memchunk->memblock && memchunk->memblock->data && memchunk->length && memchunk->memblock->length && (memchunk->length % u->frame_size) == 0);
114
115 if ((frames = snd_pcm_writei(u->pcm_handle, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length / u->frame_size)) < 0) {
116 if (frames == -EAGAIN)
117 return;
118
119 if (frames == -EPIPE) {
120 xrun_recovery(u);
121 continue;
122 }
123
124 pa_log(__FILE__": snd_pcm_writei() failed\n");
125 return;
126 }
127
128 if (memchunk == &u->memchunk) {
129 size_t l = frames * u->frame_size;
130 memchunk->index += l;
131 memchunk->length -= l;
132
133 if (memchunk->length == 0) {
134 pa_memblock_unref(memchunk->memblock);
135 memchunk->memblock = NULL;
136 memchunk->index = memchunk->length = 0;
137 }
138 }
139
140 break;
141 }
142 }
143
144 static void io_callback(pa_mainloop_api*a, pa_io_event *e, PA_GCC_UNUSED int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
145 struct userdata *u = userdata;
146 assert(u && a && e);
147
148 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
149 xrun_recovery(u);
150
151 do_write(u);
152 }
153
154 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
155 pa_usec_t r = 0;
156 struct userdata *u = s->userdata;
157 snd_pcm_sframes_t frames;
158 assert(s && u && u->sink);
159
160 if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
161 pa_log(__FILE__": failed to get delay\n");
162 s->get_latency = NULL;
163 return 0;
164 }
165
166 if (frames < 0)
167 frames = 0;
168
169 r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
170
171 if (u->memchunk.memblock)
172 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
173
174 return r;
175 }
176
177 int pa__init(pa_core *c, pa_module*m) {
178 pa_modargs *ma = NULL;
179 int ret = -1;
180 struct userdata *u = NULL;
181 const char *dev;
182 pa_sample_spec ss;
183 uint32_t periods, fragsize;
184 snd_pcm_uframes_t period_size;
185 size_t frame_size;
186 int err;
187
188 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
189 pa_log(__FILE__": failed to parse module arguments\n");
190 goto fail;
191 }
192
193 ss = c->default_sample_spec;
194 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
195 pa_log(__FILE__": failed to parse sample specification\n");
196 goto fail;
197 }
198 frame_size = pa_frame_size(&ss);
199
200 periods = 8;
201 fragsize = 1024;
202 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
203 pa_log(__FILE__": failed to parse buffer metrics\n");
204 goto fail;
205 }
206 period_size = fragsize;
207
208 u = pa_xmalloc0(sizeof(struct userdata));
209 m->userdata = u;
210 u->module = m;
211
212 snd_config_update_free_global();
213 if ((err = snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
214 pa_log(__FILE__": Error opening PCM device %s: %s\n", dev, snd_strerror(err));
215 goto fail;
216 }
217
218 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
219 pa_log(__FILE__": Failed to set hardware parameters: %s\n", snd_strerror(err));
220 goto fail;
221 }
222
223 u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL);
224 assert(u->sink);
225
226 u->sink->get_latency = sink_get_latency_cb;
227 u->sink->userdata = u;
228 pa_sink_set_owner(u->sink, m);
229 u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
230
231 if (pa_create_io_events(u->pcm_handle, c->mainloop, &u->io_events, &u->n_io_events, io_callback, u) < 0) {
232 pa_log(__FILE__": failed to obtain file descriptors\n");
233 goto fail;
234 }
235
236 u->frame_size = frame_size;
237 u->fragment_size = period_size;
238
239 pa_log_info(__FILE__": using %u fragments of size %u bytes.\n", periods, u->fragment_size);
240
241 u->silence.memblock = pa_memblock_new(u->silence.length = u->fragment_size, c->memblock_stat);
242 assert(u->silence.memblock);
243 pa_silence_memblock(u->silence.memblock, &ss);
244 u->silence.index = 0;
245
246 u->memchunk.memblock = NULL;
247 u->memchunk.index = u->memchunk.length = 0;
248
249 ret = 0;
250
251 finish:
252 if (ma)
253 pa_modargs_free(ma);
254
255 return ret;
256
257 fail:
258
259 if (u)
260 pa__done(c, m);
261
262 goto finish;
263 }
264
265 void pa__done(pa_core *c, pa_module*m) {
266 struct userdata *u;
267 assert(c && m);
268
269 if (!(u = m->userdata))
270 return;
271
272 if (u->sink) {
273 pa_sink_disconnect(u->sink);
274 pa_sink_unref(u->sink);
275 }
276
277 if (u->io_events)
278 pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
279
280 if (u->pcm_handle) {
281 snd_pcm_drop(u->pcm_handle);
282 snd_pcm_close(u->pcm_handle);
283 }
284
285 if (u->memchunk.memblock)
286 pa_memblock_unref(u->memchunk.memblock);
287 if (u->silence.memblock)
288 pa_memblock_unref(u->silence.memblock);
289
290 pa_xfree(u);
291 }
292