]> code.delx.au - pulseaudio/blob - polyp/module-alsa-source.c
34f08f95a0372d05edd3d746ff6b6f89e41c518e
[pulseaudio] / polyp / module-alsa-source.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-source-symdef.h"
43
44 PA_MODULE_AUTHOR("Lennart Poettering")
45 PA_MODULE_DESCRIPTION("ALSA Source")
46 PA_MODULE_VERSION(PACKAGE_VERSION)
47 PA_MODULE_USAGE("source_name=<name for the source> 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_source *source;
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;
57 struct pa_module *module;
58 };
59
60 static const char* const valid_modargs[] = {
61 "device",
62 "source_name",
63 "channels",
64 "rate",
65 "format",
66 "fragments",
67 "fragment_size",
68 NULL
69 };
70
71 #define DEFAULT_SOURCE_NAME "alsa_input"
72 #define DEFAULT_DEVICE "hw:0,0"
73
74 static void update_usage(struct userdata *u) {
75 pa_module_set_used(u->module,
76 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
77 }
78
79 static void xrun_recovery(struct userdata *u) {
80 assert(u);
81
82 pa_log(__FILE__": *** ALSA-XRUN (capture) ***\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_read(struct userdata *u) {
89 assert(u);
90
91 update_usage(u);
92
93 for (;;) {
94 struct pa_memchunk post_memchunk;
95 snd_pcm_sframes_t frames;
96 size_t l;
97
98 if (!u->memchunk.memblock) {
99 u->memchunk.memblock = pa_memblock_new(u->memchunk.length = u->fragment_size, u->source->core->memblock_stat);
100 u->memchunk.index = 0;
101 }
102
103 assert(u->memchunk.memblock && u->memchunk.memblock->data && u->memchunk.length && u->memchunk.memblock->length && (u->memchunk.length % u->frame_size) == 0);
104
105 if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
106 if (frames == -EAGAIN)
107 return;
108
109 if (frames == -EPIPE) {
110 xrun_recovery(u);
111 continue;
112 }
113
114 pa_log(__FILE__": snd_pcm_readi() failed: %s\n", strerror(-frames));
115 return;
116 }
117
118 l = frames * u->frame_size;
119
120 post_memchunk = u->memchunk;
121 post_memchunk.length = l;
122
123 pa_source_post(u->source, &post_memchunk);
124
125 u->memchunk.index += l;
126 u->memchunk.length -= l;
127
128 if (u->memchunk.length == 0) {
129 pa_memblock_unref(u->memchunk.memblock);
130 u->memchunk.memblock = NULL;
131 u->memchunk.index = u->memchunk.length = 0;
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_read(u);
146 }
147
148 static pa_usec_t source_get_latency_cb(struct pa_source *s) {
149 struct userdata *u = s->userdata;
150 snd_pcm_sframes_t frames;
151 assert(s && u && u->source);
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 return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
160 }
161
162 int pa__init(struct pa_core *c, struct pa_module*m) {
163 struct pa_modargs *ma = NULL;
164 int ret = -1;
165 struct userdata *u = NULL;
166 const char *dev;
167 struct pa_sample_spec ss;
168 unsigned periods, fragsize;
169 snd_pcm_uframes_t buffer_size;
170 size_t frame_size;
171
172 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
173 pa_log(__FILE__": failed to parse module arguments\n");
174 goto fail;
175 }
176
177 ss = c->default_sample_spec;
178 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
179 pa_log(__FILE__": failed to parse sample specification\n");
180 goto fail;
181 }
182 frame_size = pa_frame_size(&ss);
183
184 periods = 12;
185 fragsize = 1024;
186 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
187 pa_log(__FILE__": failed to parse buffer metrics\n");
188 goto fail;
189 }
190 buffer_size = fragsize/frame_size*periods;
191
192 u = pa_xmalloc0(sizeof(struct userdata));
193 m->userdata = u;
194 u->module = m;
195
196 if (snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) < 0) {
197 pa_log(__FILE__": Error opening PCM device %s\n", dev);
198 goto fail;
199 }
200
201 if (pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &buffer_size) < 0) {
202 pa_log(__FILE__": Failed to set hardware parameters\n");
203 goto fail;
204 }
205
206 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
207 assert(u->source);
208
209 u->source->userdata = u;
210 u->source->get_latency = source_get_latency_cb;
211 pa_source_set_owner(u->source, m);
212 u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
213
214 if (pa_create_io_events(u->pcm_handle, c->mainloop, &u->io_events, &u->n_io_events, io_callback, u) < 0) {
215 pa_log(__FILE__": failed to obtain file descriptors\n");
216 goto fail;
217 }
218
219 u->frame_size = frame_size;
220 u->fragment_size = buffer_size*u->frame_size/periods;
221
222 pa_log(__FILE__": using %u fragments of size %u bytes.\n", periods, u->fragment_size);
223
224 u->memchunk.memblock = NULL;
225 u->memchunk.index = u->memchunk.length = 0;
226
227 snd_pcm_start(u->pcm_handle);
228
229 ret = 0;
230
231 finish:
232 if (ma)
233 pa_modargs_free(ma);
234
235 return ret;
236
237 fail:
238
239 if (u)
240 pa__done(c, m);
241
242 goto finish;
243 }
244
245 void pa__done(struct pa_core *c, struct pa_module*m) {
246 struct userdata *u;
247 assert(c && m);
248
249 if (!(u = m->userdata))
250 return;
251
252 if (u->source) {
253 pa_source_disconnect(u->source);
254 pa_source_unref(u->source);
255 }
256
257 if (u->io_events)
258 pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
259
260 if (u->pcm_handle) {
261 snd_pcm_drop(u->pcm_handle);
262 snd_pcm_close(u->pcm_handle);
263 }
264
265 if (u->memchunk.memblock)
266 pa_memblock_unref(u->memchunk.memblock);
267
268 pa_xfree(u);
269 }
270