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