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