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