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