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