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