4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/soundcard.h>
30 #include <sys/ioctl.h>
41 #include <pulse/xmalloc.h>
42 #include <pulse/util.h>
44 #include <pulsecore/core-error.h>
45 #include <pulsecore/iochannel.h>
46 #include <pulsecore/sink.h>
47 #include <pulsecore/source.h>
48 #include <pulsecore/module.h>
49 #include <pulsecore/sample-util.h>
50 #include <pulsecore/core-util.h>
51 #include <pulsecore/modargs.h>
52 #include <pulsecore/log.h>
55 #include "module-oss-symdef.h"
57 PA_MODULE_AUTHOR("Lennart Poettering")
58 PA_MODULE_DESCRIPTION("OSS Sink/Source")
59 PA_MODULE_VERSION(PACKAGE_VERSION)
61 "sink_name=<name for the sink> "
62 "source_name=<name for the source> "
63 "device=<OSS device> "
64 "record=<enable source?> "
65 "playback=<enable sink?> "
66 "format=<sample format> "
67 "channels=<number of channels> "
69 "fragments=<number of fragments> "
70 "fragment_size=<fragment size> "
71 "channel_map=<channel map>")
79 pa_memchunk memchunk, silence;
81 uint32_t in_fragment_size, out_fragment_size, sample_size;
82 int use_getospace, use_getispace;
88 static const char* const valid_modargs[] = {
103 #define DEFAULT_DEVICE "/dev/dsp"
105 static void update_usage(struct userdata *u) {
106 pa_module_set_used(u->module,
107 (u->sink ? pa_sink_used_by(u->sink) : 0) +
108 (u->source ? pa_source_used_by(u->source) : 0));
111 static void clear_up(struct userdata *u) {
115 pa_sink_disconnect(u->sink);
116 pa_sink_unref(u->sink);
121 pa_source_disconnect(u->source);
122 pa_source_unref(u->source);
127 pa_iochannel_free(u->io);
132 static void do_write(struct userdata *u) {
133 pa_memchunk *memchunk;
140 if (!u->sink || !pa_iochannel_is_writable(u->io))
145 l = u->out_fragment_size;
147 if (u->use_getospace) {
150 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
151 u->use_getospace = 0;
153 if (info.bytes/l > 0) {
154 l = (info.bytes/l)*l;
162 memchunk = &u->memchunk;
164 if (!memchunk->length)
165 if (pa_sink_render(u->sink, l, memchunk) < 0)
166 memchunk = &u->silence;
168 assert(memchunk->memblock);
169 assert(memchunk->length);
171 p = pa_memblock_acquire(memchunk->memblock);
172 if ((r = pa_iochannel_write(u->io, (uint8_t*) p + memchunk->index, memchunk->length)) < 0) {
173 pa_memblock_release(memchunk->memblock);
175 if (errno != EAGAIN) {
176 pa_log("write() failed: %s", pa_cstrerror(errno));
179 pa_module_unload_request(u->module);
185 pa_memblock_release(memchunk->memblock);
187 if (memchunk == &u->silence)
188 assert(r % u->sample_size == 0);
190 u->memchunk.index += r;
191 u->memchunk.length -= r;
193 if (u->memchunk.length <= 0) {
194 pa_memblock_unref(u->memchunk.memblock);
195 u->memchunk.memblock = NULL;
199 l = l > (size_t) r ? l - r : 0;
200 } while (loop && l > 0);
203 static void do_read(struct userdata *u) {
204 pa_memchunk memchunk;
210 if (!u->source || !pa_iochannel_is_readable(u->io) || !pa_idxset_size(u->source->outputs))
215 l = u->in_fragment_size;
217 if (u->use_getispace) {
220 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0)
221 u->use_getispace = 0;
223 if (info.bytes/l > 0) {
224 l = (info.bytes/l)*l;
232 memchunk.memblock = pa_memblock_new(u->core->mempool, l);
234 p = pa_memblock_acquire(memchunk.memblock);
236 if ((r = pa_iochannel_read(u->io, p, pa_memblock_get_length(memchunk.memblock))) < 0) {
237 pa_memblock_release(memchunk.memblock);
238 pa_memblock_unref(memchunk.memblock);
240 if (errno != EAGAIN) {
241 pa_log("read() failed: %s", pa_cstrerror(errno));
244 pa_module_unload_request(u->module);
250 pa_memblock_release(memchunk.memblock);
252 assert(r <= (ssize_t) pa_memblock_get_length(memchunk.memblock));
256 pa_source_post(u->source, &memchunk);
257 pa_memblock_unref(memchunk.memblock);
259 l = l > (size_t) r ? l - r : 0;
260 } while (loop && l > 0);
263 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
264 struct userdata *u = userdata;
270 static void source_notify_cb(pa_source *s) {
271 struct userdata *u = s->userdata;
276 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
279 struct userdata *u = s->userdata;
280 assert(s && u && u->sink);
282 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
283 pa_log_info("device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
284 s->get_latency = NULL;
288 r += pa_bytes_to_usec(arg, &s->sample_spec);
290 if (u->memchunk.memblock)
291 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
296 static pa_usec_t source_get_latency_cb(pa_source *s) {
297 struct userdata *u = s->userdata;
299 assert(s && u && u->source);
301 if (!u->use_getispace)
304 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
305 u->use_getispace = 0;
312 return pa_bytes_to_usec(info.bytes, &s->sample_spec);
315 static int sink_get_hw_volume(pa_sink *s) {
316 struct userdata *u = s->userdata;
318 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
319 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
320 s->get_hw_volume = NULL;
327 static int sink_set_hw_volume(pa_sink *s) {
328 struct userdata *u = s->userdata;
330 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
331 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
332 s->set_hw_volume = NULL;
339 static int source_get_hw_volume(pa_source *s) {
340 struct userdata *u = s->userdata;
342 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
343 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
344 s->get_hw_volume = NULL;
351 static int source_set_hw_volume(pa_source *s) {
352 struct userdata *u = s->userdata;
354 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
355 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
356 s->set_hw_volume = NULL;
363 int pa__init(pa_core *c, pa_module*m) {
364 struct audio_buf_info info;
365 struct userdata *u = NULL;
368 int nfrags, frag_size, in_frag_size, out_frag_size;
370 int record = 1, playback = 1;
373 pa_modargs *ma = NULL;
376 char *name_buf = NULL;
382 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
383 pa_log("failed to parse module arguments.");
387 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
388 pa_log("record= and playback= expect numeric argument.");
392 if (!playback && !record) {
393 pa_log("neither playback nor record enabled for device.");
397 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
399 ss = c->default_sample_spec;
400 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
401 pa_log("failed to parse sample specification or channel map");
405 /* Fix latency to 100ms */
407 frag_size = pa_bytes_per_second(&ss)/128;
409 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
410 pa_log("failed to parse fragments arguments");
414 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
417 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
418 pa_log_info("hardware name is '%s'.", hwdesc);
422 pa_log_info("device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
424 if (nfrags >= 2 && frag_size >= 1)
425 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
428 if (pa_oss_auto_format(fd, &ss) < 0)
431 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
432 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
436 in_frag_size = out_frag_size = frag_size;
438 u = pa_xmalloc(sizeof(struct userdata));
440 u->use_getospace = u->use_getispace = 0;
442 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
443 pa_log_info("input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
444 in_frag_size = info.fragsize;
445 u->use_getispace = 1;
448 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
449 pa_log_info("output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
450 out_frag_size = info.fragsize;
451 u->use_getospace = 1;
454 if (mode != O_WRONLY) {
455 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
458 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p));
462 if (!(u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map)))
465 u->source->userdata = u;
466 u->source->notify = source_notify_cb;
467 u->source->get_latency = source_get_latency_cb;
468 u->source->get_hw_volume = source_get_hw_volume;
469 u->source->set_hw_volume = source_set_hw_volume;
470 pa_source_set_owner(u->source, m);
471 pa_source_set_description(u->source, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s",
473 hwdesc[0] ? " (" : "",
474 hwdesc[0] ? hwdesc : "",
475 hwdesc[0] ? ")" : ""));
477 u->source->is_hardware = 1;
484 if (mode != O_RDONLY) {
485 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
488 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p));
492 if (!(u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map)))
495 u->sink->get_latency = sink_get_latency_cb;
496 u->sink->get_hw_volume = sink_get_hw_volume;
497 u->sink->set_hw_volume = sink_set_hw_volume;
498 u->sink->userdata = u;
499 pa_sink_set_owner(u->sink, m);
500 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s",
502 hwdesc[0] ? " (" : "",
503 hwdesc[0] ? hwdesc : "",
504 hwdesc[0] ? ")" : ""));
506 u->sink->is_hardware = 1;
513 assert(u->source || u->sink);
515 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : -1);
517 pa_iochannel_set_callback(u->io, io_callback, u);
520 u->memchunk.memblock = NULL;
521 u->memchunk.length = 0;
522 u->sample_size = pa_frame_size(&ss);
524 u->out_fragment_size = out_frag_size;
525 u->in_fragment_size = in_frag_size;
526 u->silence.memblock = pa_memblock_new(u->core->mempool, u->silence.length = u->out_fragment_size);
527 assert(u->silence.memblock);
528 pa_silence_memblock(u->silence.memblock, &ss);
529 u->silence.index = 0;
537 * Some crappy drivers do not start the recording until we read something.
538 * Without this snippet, poll will never register the fd as ready.
541 char *buf = pa_xnew(char, u->sample_size);
542 pa_read(u->fd, buf, u->sample_size, NULL);
546 /* Read mixer settings */
548 source_get_hw_volume(u->source);
550 sink_get_hw_volume(u->sink);
566 void pa__done(pa_core *c, pa_module*m) {
572 if (!(u = m->userdata))
577 if (u->memchunk.memblock)
578 pa_memblock_unref(u->memchunk.memblock);
579 if (u->silence.memblock)
580 pa_memblock_unref(u->silence.memblock);