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