]> code.delx.au - pulseaudio/blob - polyp/module-oss.c
correct latency calculation
[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
63 int fd;
64 struct pa_module *module;
65 };
66
67 static const char* const valid_modargs[] = {
68 "sink_name",
69 "source_name",
70 "device",
71 "record",
72 "playback",
73 "fragments",
74 "fragment_size",
75 "format",
76 "rate",
77 "channels",
78 NULL
79 };
80
81 #define DEFAULT_SINK_NAME "oss_output"
82 #define DEFAULT_SOURCE_NAME "oss_input"
83 #define DEFAULT_DEVICE "/dev/dsp"
84
85 static void update_usage(struct userdata *u) {
86 pa_module_set_used(u->module,
87 (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) +
88 (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) +
89 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
90 }
91
92 static void do_write(struct userdata *u) {
93 struct pa_memchunk *memchunk;
94 ssize_t r;
95 assert(u);
96
97 if (!u->sink || !pa_iochannel_is_writable(u->io))
98 return;
99
100 update_usage(u);
101
102 memchunk = &u->memchunk;
103
104 if (!memchunk->length)
105 if (pa_sink_render(u->sink, u->out_fragment_size, memchunk) < 0)
106 memchunk = &u->silence;
107
108 assert(memchunk->memblock);
109 assert(memchunk->memblock->data);
110 assert(memchunk->length);
111
112 if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
113 pa_log(__FILE__": write() failed: %s\n", strerror(errno));
114 return;
115 }
116
117 if (memchunk == &u->silence)
118 assert(r % u->sample_size == 0);
119 else {
120 u->memchunk.index += r;
121 u->memchunk.length -= r;
122
123 if (u->memchunk.length <= 0) {
124 pa_memblock_unref(u->memchunk.memblock);
125 u->memchunk.memblock = NULL;
126 }
127 }
128 }
129
130 static void do_read(struct userdata *u) {
131 struct pa_memchunk memchunk;
132 ssize_t r;
133 assert(u);
134
135 if (!u->source || !pa_iochannel_is_readable(u->io))
136 return;
137
138 update_usage(u);
139
140 memchunk.memblock = pa_memblock_new(u->in_fragment_size, u->core->memblock_stat);
141 assert(memchunk.memblock);
142 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
143 pa_memblock_unref(memchunk.memblock);
144 if (errno != EAGAIN)
145 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
146 return;
147 }
148
149 assert(r <= (ssize_t) memchunk.memblock->length);
150 memchunk.length = memchunk.memblock->length = r;
151 memchunk.index = 0;
152
153 pa_source_post(u->source, &memchunk);
154 pa_memblock_unref(memchunk.memblock);
155 }
156
157 static void io_callback(struct pa_iochannel *io, void*userdata) {
158 struct userdata *u = userdata;
159 assert(u);
160 do_write(u);
161 do_read(u);
162 }
163
164 static pa_usec_t sink_get_latency_cb(struct pa_sink *s) {
165 pa_usec_t r = 0;
166 int arg;
167 struct userdata *u = s->userdata;
168 assert(s && u && u->sink);
169
170 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
171 pa_log(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY.\n");
172 s->get_latency = NULL;
173 return 0;
174 }
175
176 r += pa_bytes_to_usec(arg, &s->sample_spec);
177
178 if (u->memchunk.memblock)
179 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
180
181 return r;
182 }
183
184 int pa__init(struct pa_core *c, struct pa_module*m) {
185 struct audio_buf_info info;
186 struct userdata *u = NULL;
187 const char *p;
188 int fd = -1;
189 int nfrags, frag_size, in_frag_size, out_frag_size;
190 int mode;
191 int record = 1, playback = 1;
192 struct pa_sample_spec ss;
193 struct pa_modargs *ma = NULL;
194 assert(c && m);
195
196 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
197 pa_log(__FILE__": failed to parse module arguments.\n");
198 goto fail;
199 }
200
201 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
202 pa_log(__FILE__": record= and playback= expect numeric argument.\n");
203 goto fail;
204 }
205
206 if (!playback && !record) {
207 pa_log(__FILE__": neither playback nor record enabled for device.\n");
208 goto fail;
209 }
210
211 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
212
213 nfrags = 12;
214 frag_size = 1024;
215 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
216 pa_log(__FILE__": failed to parse fragments arguments\n");
217 goto fail;
218 }
219
220 ss = c->default_sample_spec;
221 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
222 pa_log(__FILE__": failed to parse sample specification\n");
223 goto fail;
224 }
225
226 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
227 goto fail;
228
229 pa_log(__FILE__": device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
230
231
232 if (nfrags >= 2 && frag_size >= 1)
233 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
234 goto fail;
235
236 if (pa_oss_auto_format(fd, &ss) < 0)
237 goto fail;
238
239 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
240 pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno));
241 goto fail;
242 }
243 assert(frag_size);
244 in_frag_size = out_frag_size = frag_size;
245
246 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
247 pa_log(__FILE__": input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
248 in_frag_size = info.fragsize;
249 }
250
251 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
252 pa_log(__FILE__": output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
253 out_frag_size = info.fragsize;
254 }
255
256 u = pa_xmalloc(sizeof(struct userdata));
257 u->core = c;
258
259 if (mode != O_WRONLY) {
260 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
261 assert(u->source);
262 u->source->userdata = u;
263 pa_source_set_owner(u->source, m);
264 u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
265 } else
266 u->source = NULL;
267
268 if (mode != O_RDONLY) {
269 u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
270 assert(u->sink);
271 u->sink->get_latency = sink_get_latency_cb;
272 u->sink->userdata = u;
273 pa_sink_set_owner(u->sink, m);
274 u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
275 } else
276 u->sink = NULL;
277
278 assert(u->source || u->sink);
279
280 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
281 assert(u->io);
282 pa_iochannel_set_callback(u->io, io_callback, u);
283 u->fd = fd;
284
285 u->memchunk.memblock = NULL;
286 u->memchunk.length = 0;
287 u->sample_size = pa_frame_size(&ss);
288
289 u->out_fragment_size = out_frag_size;
290 u->in_fragment_size = in_frag_size;
291 u->silence.memblock = pa_memblock_new(u->silence.length = u->out_fragment_size, u->core->memblock_stat);
292 assert(u->silence.memblock);
293 pa_silence_memblock(u->silence.memblock, &ss);
294 u->silence.index = 0;
295
296 u->module = m;
297 m->userdata = u;
298
299 pa_modargs_free(ma);
300
301 return 0;
302
303 fail:
304 if (fd >= 0)
305 close(fd);
306
307 if (ma)
308 pa_modargs_free(ma);
309
310 return -1;
311 }
312
313 void pa__done(struct pa_core *c, struct pa_module*m) {
314 struct userdata *u;
315 assert(c && m);
316
317 if (!(u = m->userdata))
318 return;
319
320 if (u->memchunk.memblock)
321 pa_memblock_unref(u->memchunk.memblock);
322 if (u->silence.memblock)
323 pa_memblock_unref(u->silence.memblock);
324
325 if (u->sink)
326 pa_sink_free(u->sink);
327 if (u->source)
328 pa_source_free(u->source);
329
330 pa_iochannel_free(u->io);
331 pa_xfree(u);
332 }