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