]> code.delx.au - pulseaudio/blob - polyp/module-oss.c
add input latency measurement
[pulseaudio] / polyp / module-oss.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 <sys/soundcard.h>
27 #include <sys/ioctl.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include "iochannel.h"
39 #include "sink.h"
40 #include "source.h"
41 #include "module.h"
42 #include "oss-util.h"
43 #include "sample-util.h"
44 #include "util.h"
45 #include "modargs.h"
46 #include "xmalloc.h"
47 #include "log.h"
48
49 PA_MODULE_AUTHOR("Lennart Poettering")
50 PA_MODULE_DESCRIPTION("OSS Sink/Source")
51 PA_MODULE_VERSION(PACKAGE_VERSION)
52
53 struct userdata {
54 struct pa_sink *sink;
55 struct pa_source *source;
56 struct pa_iochannel *io;
57 struct pa_core *core;
58
59 struct pa_memchunk memchunk, silence;
60
61 uint32_t in_fragment_size, out_fragment_size, sample_size;
62 int use_getospace, use_getispace;
63
64 int fd;
65 struct pa_module *module;
66 };
67
68 static const char* const valid_modargs[] = {
69 "sink_name",
70 "source_name",
71 "device",
72 "record",
73 "playback",
74 "fragments",
75 "fragment_size",
76 "format",
77 "rate",
78 "channels",
79 NULL
80 };
81
82 #define DEFAULT_SINK_NAME "oss_output"
83 #define DEFAULT_SOURCE_NAME "oss_input"
84 #define DEFAULT_DEVICE "/dev/dsp"
85
86 static void update_usage(struct userdata *u) {
87 pa_module_set_used(u->module,
88 (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) +
89 (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) +
90 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
91 }
92
93 static void do_write(struct userdata *u) {
94 struct pa_memchunk *memchunk;
95 ssize_t r;
96 size_t l;
97 int loop = 0;
98
99 assert(u);
100
101 if (!u->sink || !pa_iochannel_is_writable(u->io))
102 return;
103
104 update_usage(u);
105
106 l = u->out_fragment_size;
107
108 if (u->use_getospace) {
109 audio_buf_info info;
110
111 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
112 u->use_getospace = 0;
113 else {
114 if (info.bytes/l > 0) {
115 l = (info.bytes/l)*l;
116 loop = 1;
117 }
118 }
119 }
120
121 do {
122 memchunk = &u->memchunk;
123
124 if (!memchunk->length)
125 if (pa_sink_render(u->sink, l, memchunk) < 0)
126 memchunk = &u->silence;
127
128 assert(memchunk->memblock);
129 assert(memchunk->memblock->data);
130 assert(memchunk->length);
131
132 if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
133 pa_log(__FILE__": write() failed: %s\n", strerror(errno));
134 break;
135 }
136
137 if (memchunk == &u->silence)
138 assert(r % u->sample_size == 0);
139 else {
140 u->memchunk.index += r;
141 u->memchunk.length -= r;
142
143 if (u->memchunk.length <= 0) {
144 pa_memblock_unref(u->memchunk.memblock);
145 u->memchunk.memblock = NULL;
146 }
147 }
148
149 l = l > (size_t) r ? l - r : 0;
150 } while (loop && l > 0);
151 }
152
153 static void do_read(struct userdata *u) {
154 struct pa_memchunk memchunk;
155 ssize_t r;
156 size_t l;
157 int loop = 0;
158 assert(u);
159
160 if (!u->source || !pa_iochannel_is_readable(u->io))
161 return;
162
163 update_usage(u);
164
165 l = u->in_fragment_size;
166
167 if (u->use_getispace) {
168 audio_buf_info info;
169
170 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0)
171 u->use_getispace = 0;
172 else {
173 if (info.bytes/l > 0) {
174 l = (info.bytes/l)*l;
175 loop = 1;
176 }
177 }
178 }
179
180 do {
181 memchunk.memblock = pa_memblock_new(l, u->core->memblock_stat);
182 assert(memchunk.memblock);
183 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
184 pa_memblock_unref(memchunk.memblock);
185 if (errno != EAGAIN)
186 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
187 break;
188 }
189
190 assert(r <= (ssize_t) memchunk.memblock->length);
191 memchunk.length = memchunk.memblock->length = r;
192 memchunk.index = 0;
193
194 pa_source_post(u->source, &memchunk);
195 pa_memblock_unref(memchunk.memblock);
196
197 l = l > (size_t) r ? l - r : 0;
198 } while (loop && l > 0);
199 }
200
201 static void io_callback(struct pa_iochannel *io, void*userdata) {
202 struct userdata *u = userdata;
203 assert(u);
204 do_write(u);
205 do_read(u);
206 }
207
208 static pa_usec_t sink_get_latency_cb(struct pa_sink *s) {
209 pa_usec_t r = 0;
210 int arg;
211 struct userdata *u = s->userdata;
212 assert(s && u && u->sink);
213
214 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
215 pa_log(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY.\n");
216 s->get_latency = NULL;
217 return 0;
218 }
219
220 r += pa_bytes_to_usec(arg, &s->sample_spec);
221
222 if (u->memchunk.memblock)
223 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
224
225 return r;
226 }
227
228 static pa_usec_t source_get_latency_cb(struct pa_source *s) {
229 struct userdata *u = s->userdata;
230 audio_buf_info info;
231 assert(s && u && u->sink);
232
233 if (!u->use_getispace)
234 return 0;
235
236 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
237 u->use_getispace = 0;
238 return 0;
239 }
240
241 if (info.bytes <= 0)
242 return 0;
243
244 return pa_bytes_to_usec(info.bytes, &s->sample_spec);
245 }
246
247 int pa__init(struct pa_core *c, struct pa_module*m) {
248 struct audio_buf_info info;
249 struct userdata *u = NULL;
250 const char *p;
251 int fd = -1;
252 int nfrags, frag_size, in_frag_size, out_frag_size;
253 int mode;
254 int record = 1, playback = 1;
255 struct pa_sample_spec ss;
256 struct pa_modargs *ma = NULL;
257 assert(c && m);
258
259 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
260 pa_log(__FILE__": failed to parse module arguments.\n");
261 goto fail;
262 }
263
264 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
265 pa_log(__FILE__": record= and playback= expect numeric argument.\n");
266 goto fail;
267 }
268
269 if (!playback && !record) {
270 pa_log(__FILE__": neither playback nor record enabled for device.\n");
271 goto fail;
272 }
273
274 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
275
276 nfrags = 12;
277 frag_size = 1024;
278 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
279 pa_log(__FILE__": failed to parse fragments arguments\n");
280 goto fail;
281 }
282
283 ss = c->default_sample_spec;
284 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
285 pa_log(__FILE__": failed to parse sample specification\n");
286 goto fail;
287 }
288
289 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
290 goto fail;
291
292 pa_log(__FILE__": device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
293
294
295 if (nfrags >= 2 && frag_size >= 1)
296 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
297 goto fail;
298
299 if (pa_oss_auto_format(fd, &ss) < 0)
300 goto fail;
301
302 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
303 pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno));
304 goto fail;
305 }
306 assert(frag_size);
307 in_frag_size = out_frag_size = frag_size;
308
309 u = pa_xmalloc(sizeof(struct userdata));
310 u->core = c;
311 u->use_getospace = u->use_getispace = 0;
312
313 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
314 pa_log(__FILE__": input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
315 in_frag_size = info.fragsize;
316 u->use_getispace = 1;
317 }
318
319 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
320 pa_log(__FILE__": output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
321 out_frag_size = info.fragsize;
322 u->use_getospace = 1;
323 }
324
325 if (mode != O_WRONLY) {
326 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
327 assert(u->source);
328 u->source->userdata = u;
329 u->source->get_latency = source_get_latency_cb;
330 pa_source_set_owner(u->source, m);
331 u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
332 } else
333 u->source = NULL;
334
335 if (mode != O_RDONLY) {
336 u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
337 assert(u->sink);
338 u->sink->get_latency = sink_get_latency_cb;
339 u->sink->userdata = u;
340 pa_sink_set_owner(u->sink, m);
341 u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
342 } else
343 u->sink = NULL;
344
345 assert(u->source || u->sink);
346
347 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
348 assert(u->io);
349 pa_iochannel_set_callback(u->io, io_callback, u);
350 u->fd = fd;
351
352 u->memchunk.memblock = NULL;
353 u->memchunk.length = 0;
354 u->sample_size = pa_frame_size(&ss);
355
356 u->out_fragment_size = out_frag_size;
357 u->in_fragment_size = in_frag_size;
358 u->silence.memblock = pa_memblock_new(u->silence.length = u->out_fragment_size, u->core->memblock_stat);
359 assert(u->silence.memblock);
360 pa_silence_memblock(u->silence.memblock, &ss);
361 u->silence.index = 0;
362
363 u->module = m;
364 m->userdata = u;
365
366 pa_modargs_free(ma);
367
368 return 0;
369
370 fail:
371 if (fd >= 0)
372 close(fd);
373
374 if (ma)
375 pa_modargs_free(ma);
376
377 return -1;
378 }
379
380 void pa__done(struct pa_core *c, struct pa_module*m) {
381 struct userdata *u;
382 assert(c && m);
383
384 if (!(u = m->userdata))
385 return;
386
387 if (u->memchunk.memblock)
388 pa_memblock_unref(u->memchunk.memblock);
389 if (u->silence.memblock)
390 pa_memblock_unref(u->silence.memblock);
391
392 if (u->sink) {
393 pa_sink_disconnect(u->sink);
394 pa_sink_unref(u->sink);
395 }
396
397 if (u->source) {
398 pa_source_disconnect(u->source);
399 pa_source_unref(u->source);
400 }
401
402 pa_iochannel_free(u->io);
403 pa_xfree(u);
404 }