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;
161 memchunk = &u->memchunk;
163 if (!memchunk->length)
164 if (pa_sink_render(u->sink, l, memchunk) < 0)
165 memchunk = &u->silence;
167 assert(memchunk->memblock);
168 assert(memchunk->memblock->data);
169 assert(memchunk->length);
171 if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
172 pa_log("write() failed: %s", pa_cstrerror(errno));
175 pa_module_unload_request(u->module);
179 if (memchunk == &u->silence)
180 assert(r % u->sample_size == 0);
182 u->memchunk.index += r;
183 u->memchunk.length -= r;
185 if (u->memchunk.length <= 0) {
186 pa_memblock_unref(u->memchunk.memblock);
187 u->memchunk.memblock = NULL;
191 l = l > (size_t) r ? l - r : 0;
192 } while (loop && l > 0);
195 static void do_read(struct userdata *u) {
196 pa_memchunk memchunk;
202 if (!u->source || !pa_iochannel_is_readable(u->io) || !pa_idxset_size(u->source->outputs))
207 l = u->in_fragment_size;
209 if (u->use_getispace) {
212 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0)
213 u->use_getispace = 0;
215 if (info.bytes/l > 0) {
216 l = (info.bytes/l)*l;
223 memchunk.memblock = pa_memblock_new(u->core->mempool, l);
224 assert(memchunk.memblock);
225 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
226 pa_memblock_unref(memchunk.memblock);
227 if (errno != EAGAIN) {
228 pa_log("read() failed: %s", pa_cstrerror(errno));
230 pa_module_unload_request(u->module);
235 assert(r <= (ssize_t) memchunk.memblock->length);
236 memchunk.length = memchunk.memblock->length = r;
239 pa_source_post(u->source, &memchunk);
240 pa_memblock_unref(memchunk.memblock);
242 l = l > (size_t) r ? l - r : 0;
243 } while (loop && l > 0);
246 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
247 struct userdata *u = userdata;
253 static void source_notify_cb(pa_source *s) {
254 struct userdata *u = s->userdata;
259 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
262 struct userdata *u = s->userdata;
263 assert(s && u && u->sink);
265 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
266 pa_log_info("device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
267 s->get_latency = NULL;
271 r += pa_bytes_to_usec(arg, &s->sample_spec);
273 if (u->memchunk.memblock)
274 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
279 static pa_usec_t source_get_latency_cb(pa_source *s) {
280 struct userdata *u = s->userdata;
282 assert(s && u && u->source);
284 if (!u->use_getispace)
287 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
288 u->use_getispace = 0;
295 return pa_bytes_to_usec(info.bytes, &s->sample_spec);
298 static int sink_get_hw_volume(pa_sink *s) {
299 struct userdata *u = s->userdata;
301 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
302 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
303 s->get_hw_volume = NULL;
310 static int sink_set_hw_volume(pa_sink *s) {
311 struct userdata *u = s->userdata;
313 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
314 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
315 s->set_hw_volume = NULL;
322 static int source_get_hw_volume(pa_source *s) {
323 struct userdata *u = s->userdata;
325 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
326 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
327 s->get_hw_volume = NULL;
334 static int source_set_hw_volume(pa_source *s) {
335 struct userdata *u = s->userdata;
337 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
338 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
339 s->set_hw_volume = NULL;
346 int pa__init(pa_core *c, pa_module*m) {
347 struct audio_buf_info info;
348 struct userdata *u = NULL;
351 int nfrags, frag_size, in_frag_size, out_frag_size;
353 int record = 1, playback = 1;
356 pa_modargs *ma = NULL;
359 char *name_buf = NULL;
365 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
366 pa_log("failed to parse module arguments.");
370 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
371 pa_log("record= and playback= expect numeric argument.");
375 if (!playback && !record) {
376 pa_log("neither playback nor record enabled for device.");
380 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
382 ss = c->default_sample_spec;
383 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
384 pa_log("failed to parse sample specification or channel map");
388 /* Fix latency to 100ms */
390 frag_size = pa_bytes_per_second(&ss)/128;
392 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
393 pa_log("failed to parse fragments arguments");
397 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
400 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
401 pa_log_info("hardware name is '%s'.", hwdesc);
405 pa_log_info("device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
407 if (nfrags >= 2 && frag_size >= 1)
408 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
411 if (pa_oss_auto_format(fd, &ss) < 0)
414 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
415 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
419 in_frag_size = out_frag_size = frag_size;
421 u = pa_xmalloc(sizeof(struct userdata));
423 u->use_getospace = u->use_getispace = 0;
425 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
426 pa_log_info("input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
427 in_frag_size = info.fragsize;
428 u->use_getispace = 1;
431 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
432 pa_log_info("output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
433 out_frag_size = info.fragsize;
434 u->use_getospace = 1;
437 if (mode != O_WRONLY) {
438 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
441 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p));
445 if (!(u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map)))
448 u->source->userdata = u;
449 u->source->notify = source_notify_cb;
450 u->source->get_latency = source_get_latency_cb;
451 u->source->get_hw_volume = source_get_hw_volume;
452 u->source->set_hw_volume = source_set_hw_volume;
453 pa_source_set_owner(u->source, m);
454 pa_source_set_description(u->source, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s",
456 hwdesc[0] ? " (" : "",
457 hwdesc[0] ? hwdesc : "",
458 hwdesc[0] ? ")" : ""));
460 u->source->is_hardware = 1;
467 if (mode != O_RDONLY) {
468 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
471 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p));
475 if (!(u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map)))
478 u->sink->get_latency = sink_get_latency_cb;
479 u->sink->get_hw_volume = sink_get_hw_volume;
480 u->sink->set_hw_volume = sink_set_hw_volume;
481 u->sink->userdata = u;
482 pa_sink_set_owner(u->sink, m);
483 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s",
485 hwdesc[0] ? " (" : "",
486 hwdesc[0] ? hwdesc : "",
487 hwdesc[0] ? ")" : ""));
489 u->sink->is_hardware = 1;
496 assert(u->source || u->sink);
498 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : -1);
500 pa_iochannel_set_callback(u->io, io_callback, u);
503 u->memchunk.memblock = NULL;
504 u->memchunk.length = 0;
505 u->sample_size = pa_frame_size(&ss);
507 u->out_fragment_size = out_frag_size;
508 u->in_fragment_size = in_frag_size;
509 u->silence.memblock = pa_memblock_new(u->core->mempool, u->silence.length = u->out_fragment_size);
510 assert(u->silence.memblock);
511 pa_silence_memblock(u->silence.memblock, &ss);
512 u->silence.index = 0;
520 * Some crappy drivers do not start the recording until we read something.
521 * Without this snippet, poll will never register the fd as ready.
524 char *buf = pa_xnew(char, u->sample_size);
525 pa_read(u->fd, buf, u->sample_size, NULL);
529 /* Read mixer settings */
531 source_get_hw_volume(u->source);
533 sink_get_hw_volume(u->sink);
549 void pa__done(pa_core *c, pa_module*m) {
555 if (!(u = m->userdata))
560 if (u->memchunk.memblock)
561 pa_memblock_unref(u->memchunk.memblock);
562 if (u->silence.memblock)
563 pa_memblock_unref(u->silence.memblock);