]> code.delx.au - pulseaudio/blob - polyp/module-alsa-source.c
sample cache work
[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
41 struct userdata {
42 snd_pcm_t *pcm_handle;
43 struct pa_source *source;
44 void **io_sources;
45 unsigned n_io_sources;
46
47 size_t frame_size, fragment_size;
48 struct pa_memchunk memchunk;
49 };
50
51 static const char* const valid_modargs[] = {
52 "device",
53 "source_name",
54 "format",
55 "channels",
56 "rate",
57 "fragments",
58 "fragment_size",
59 NULL
60 };
61
62 #define DEFAULT_SOURCE_NAME "alsa_input"
63 #define DEFAULT_DEVICE "hw:0,0"
64
65 static void xrun_recovery(struct userdata *u) {
66 assert(u);
67
68 fprintf(stderr, "*** ALSA-XRUN (capture) ***\n");
69
70 if (snd_pcm_prepare(u->pcm_handle) < 0)
71 fprintf(stderr, "snd_pcm_prepare() failed\n");
72 }
73
74 static void do_read(struct userdata *u) {
75 assert(u);
76
77 for (;;) {
78 struct pa_memchunk post_memchunk;
79 snd_pcm_sframes_t frames;
80 size_t l;
81
82 if (!u->memchunk.memblock) {
83 u->memchunk.memblock = pa_memblock_new(u->memchunk.length = u->fragment_size);
84 u->memchunk.index = 0;
85 }
86
87 assert(u->memchunk.memblock && u->memchunk.memblock->data && u->memchunk.length && u->memchunk.memblock->length && (u->memchunk.length % u->frame_size) == 0);
88
89 if ((frames = snd_pcm_readi(u->pcm_handle, u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
90 if (frames == -EAGAIN)
91 return;
92
93 if (frames == -EPIPE) {
94 xrun_recovery(u);
95 continue;
96 }
97
98 fprintf(stderr, "snd_pcm_readi() failed: %s\n", strerror(-frames));
99 return;
100 }
101
102 l = frames * u->frame_size;
103
104 post_memchunk = u->memchunk;
105 post_memchunk.length = l;
106
107 pa_source_post(u->source, &post_memchunk);
108
109 u->memchunk.index += l;
110 u->memchunk.length -= l;
111
112 if (u->memchunk.length == 0) {
113 pa_memblock_unref(u->memchunk.memblock);
114 u->memchunk.memblock = NULL;
115 u->memchunk.index = u->memchunk.length = 0;
116 }
117
118 break;
119 }
120 }
121
122 static void io_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
123 struct userdata *u = userdata;
124 assert(u && a && id);
125
126 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
127 xrun_recovery(u);
128
129 do_read(u);
130 }
131
132 int pa_module_init(struct pa_core *c, struct pa_module*m) {
133 struct pa_modargs *ma = NULL;
134 int ret = -1;
135 struct userdata *u = NULL;
136 const char *dev;
137 struct pa_sample_spec ss;
138 unsigned periods, fragsize;
139 snd_pcm_uframes_t buffer_size;
140 size_t frame_size;
141
142 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
143 fprintf(stderr, __FILE__": failed to parse module arguments\n");
144 goto fail;
145 }
146
147 ss = c->default_sample_spec;
148 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
149 fprintf(stderr, __FILE__": failed to parse sample specification\n");
150 goto fail;
151 }
152 frame_size = pa_frame_size(&ss);
153
154 periods = 12;
155 fragsize = 1024;
156 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
157 fprintf(stderr, __FILE__": failed to parse buffer metrics\n");
158 goto fail;
159 }
160 buffer_size = fragsize/frame_size*periods;
161
162 u = malloc(sizeof(struct userdata));
163 assert(u);
164 memset(u, 0, sizeof(struct userdata));
165 m->userdata = u;
166
167 if (snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) < 0) {
168 fprintf(stderr, __FILE__": Error opening PCM device %s\n", dev);
169 goto fail;
170 }
171
172 if (pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &buffer_size) < 0) {
173 fprintf(stderr, __FILE__": Failed to set hardware parameters\n");
174 goto fail;
175 }
176
177 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
178 assert(u->source);
179
180 u->source->userdata = u;
181 pa_source_set_owner(u->source, m);
182 u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
183
184 if (pa_create_io_sources(u->pcm_handle, c->mainloop, &u->io_sources, &u->n_io_sources, io_callback, u) < 0) {
185 fprintf(stderr, __FILE__": failed to obtain file descriptors\n");
186 goto fail;
187 }
188
189 u->frame_size = frame_size;
190 u->fragment_size = buffer_size*u->frame_size/periods;
191
192 fprintf(stderr, __FILE__": using %u fragments of size %u bytes.\n", periods, u->fragment_size);
193
194 u->memchunk.memblock = NULL;
195 u->memchunk.index = u->memchunk.length = 0;
196
197 snd_pcm_start(u->pcm_handle);
198
199 ret = 0;
200
201 finish:
202 if (ma)
203 pa_modargs_free(ma);
204
205 return ret;
206
207 fail:
208
209 if (u)
210 pa_module_done(c, m);
211
212 goto finish;
213 }
214
215 void pa_module_done(struct pa_core *c, struct pa_module*m) {
216 struct userdata *u;
217 assert(c && m);
218
219 if ((u = m->userdata)) {
220 if (u->source)
221 pa_source_free(u->source);
222
223 if (u->io_sources)
224 pa_free_io_sources(c->mainloop, u->io_sources, u->n_io_sources);
225
226 if (u->pcm_handle) {
227 snd_pcm_drop(u->pcm_handle);
228 snd_pcm_close(u->pcm_handle);
229 }
230
231 if (u->memchunk.memblock)
232 pa_memblock_unref(u->memchunk.memblock);
233
234 free(u);
235 }
236 }
237