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