]> code.delx.au - pulseaudio/blob - polyp/module-alsa-source.c
Merge Pierre's changes
[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 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
29 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #else
32 #include "poll.h"
33 #endif
34
35 #include <asoundlib.h>
36
37 #include "module.h"
38 #include "core.h"
39 #include "memchunk.h"
40 #include "sink.h"
41 #include "modargs.h"
42 #include "util.h"
43 #include "sample-util.h"
44 #include "alsa-util.h"
45 #include "xmalloc.h"
46 #include "log.h"
47 #include "module-alsa-source-symdef.h"
48
49 PA_MODULE_AUTHOR("Lennart Poettering")
50 PA_MODULE_DESCRIPTION("ALSA Source")
51 PA_MODULE_VERSION(PACKAGE_VERSION)
52 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>")
53
54 #define PA_TYPEID_ALSA PA_TYPEID_MAKE('A', 'L', 'S', 'A')
55
56 struct userdata {
57 snd_pcm_t *pcm_handle;
58 struct pa_source *source;
59 struct pa_io_event **io_events;
60 unsigned n_io_events;
61
62 size_t frame_size, fragment_size;
63 struct pa_memchunk memchunk;
64 struct pa_module *module;
65 };
66
67 static const char* const valid_modargs[] = {
68 "device",
69 "source_name",
70 "channels",
71 "rate",
72 "format",
73 "fragments",
74 "fragment_size",
75 NULL
76 };
77
78 #define DEFAULT_SOURCE_NAME "alsa_input"
79 #define DEFAULT_DEVICE "hw:0,0"
80
81 static void update_usage(struct userdata *u) {
82 pa_module_set_used(u->module,
83 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
84 }
85
86 static void xrun_recovery(struct userdata *u) {
87 assert(u);
88
89 pa_log(__FILE__": *** ALSA-XRUN (capture) ***\n");
90
91 if (snd_pcm_prepare(u->pcm_handle) < 0)
92 pa_log(__FILE__": snd_pcm_prepare() failed\n");
93 }
94
95 static void do_read(struct userdata *u) {
96 assert(u);
97
98 update_usage(u);
99
100 for (;;) {
101 struct pa_memchunk post_memchunk;
102 snd_pcm_sframes_t frames;
103 size_t l;
104
105 if (!u->memchunk.memblock) {
106 u->memchunk.memblock = pa_memblock_new(u->memchunk.length = u->fragment_size, u->source->core->memblock_stat);
107 u->memchunk.index = 0;
108 }
109
110 assert(u->memchunk.memblock && u->memchunk.memblock->data && u->memchunk.length && u->memchunk.memblock->length && (u->memchunk.length % u->frame_size) == 0);
111
112 if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
113 if (frames == -EAGAIN)
114 return;
115
116 if (frames == -EPIPE) {
117 xrun_recovery(u);
118 continue;
119 }
120
121 pa_log(__FILE__": snd_pcm_readi() failed: %s\n", strerror(-frames));
122 return;
123 }
124
125 l = frames * u->frame_size;
126
127 post_memchunk = u->memchunk;
128 post_memchunk.length = l;
129
130 pa_source_post(u->source, &post_memchunk);
131
132 u->memchunk.index += l;
133 u->memchunk.length -= l;
134
135 if (u->memchunk.length == 0) {
136 pa_memblock_unref(u->memchunk.memblock);
137 u->memchunk.memblock = NULL;
138 u->memchunk.index = u->memchunk.length = 0;
139 }
140
141 break;
142 }
143 }
144
145 static void io_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
146 struct userdata *u = userdata;
147 assert(u && a && e);
148
149 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
150 xrun_recovery(u);
151
152 do_read(u);
153 }
154
155 static pa_usec_t source_get_latency_cb(struct pa_source *s) {
156 struct userdata *u = s->userdata;
157 snd_pcm_sframes_t frames;
158 assert(s && u && u->source);
159
160 if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
161 pa_log(__FILE__": failed to get delay\n");
162 s->get_latency = NULL;
163 return 0;
164 }
165
166 return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
167 }
168
169 int pa__init(struct pa_core *c, struct pa_module*m) {
170 struct pa_modargs *ma = NULL;
171 int ret = -1;
172 struct userdata *u = NULL;
173 const char *dev;
174 struct pa_sample_spec ss;
175 unsigned periods, fragsize;
176 snd_pcm_uframes_t period_size;
177 size_t frame_size;
178
179 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
180 pa_log(__FILE__": failed to parse module arguments\n");
181 goto fail;
182 }
183
184 ss = c->default_sample_spec;
185 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
186 pa_log(__FILE__": failed to parse sample specification\n");
187 goto fail;
188 }
189 frame_size = pa_frame_size(&ss);
190
191 periods = 12;
192 fragsize = 1024;
193 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
194 pa_log(__FILE__": failed to parse buffer metrics\n");
195 goto fail;
196 }
197 period_size = fragsize;
198
199 u = pa_xmalloc0(sizeof(struct userdata));
200 m->userdata = u;
201 u->module = m;
202
203 snd_config_update_free_global();
204 if (snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, 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, &period_size) < 0) {
210 pa_log(__FILE__": Failed to set hardware parameters\n");
211 goto fail;
212 }
213
214 u->source = pa_source_new(c, PA_TYPEID_ALSA, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
215 assert(u->source);
216
217 u->source->userdata = u;
218 u->source->get_latency = source_get_latency_cb;
219 pa_source_set_owner(u->source, m);
220 u->source->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 = period_size;
229
230 pa_log(__FILE__": using %u fragments of size %u bytes.\n", periods, u->fragment_size);
231
232 u->memchunk.memblock = NULL;
233 u->memchunk.index = u->memchunk.length = 0;
234
235 snd_pcm_start(u->pcm_handle);
236
237 ret = 0;
238
239 finish:
240 if (ma)
241 pa_modargs_free(ma);
242
243 return ret;
244
245 fail:
246
247 if (u)
248 pa__done(c, m);
249
250 goto finish;
251 }
252
253 void pa__done(struct pa_core *c, struct pa_module*m) {
254 struct userdata *u;
255 assert(c && m);
256
257 if (!(u = m->userdata))
258 return;
259
260 if (u->source) {
261 pa_source_disconnect(u->source);
262 pa_source_unref(u->source);
263 }
264
265 if (u->io_events)
266 pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
267
268 if (u->pcm_handle) {
269 snd_pcm_drop(u->pcm_handle);
270 snd_pcm_close(u->pcm_handle);
271 }
272
273 if (u->memchunk.memblock)
274 pa_memblock_unref(u->memchunk.memblock);
275
276 pa_xfree(u);
277 }
278