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) {
173 if (errno != EAGAIN) {
174 pa_log("write() failed: %s", pa_cstrerror(errno));
177 pa_module_unload_request(u->module);
183 if (memchunk == &u->silence)
184 assert(r % u->sample_size == 0);
186 u->memchunk.index += r;
187 u->memchunk.length -= r;
189 if (u->memchunk.length <= 0) {
190 pa_memblock_unref(u->memchunk.memblock);
191 u->memchunk.memblock = NULL;
195 l = l > (size_t) r ? l - r : 0;
196 } while (loop && l > 0);
199 static void do_read(struct userdata *u) {
200 pa_memchunk memchunk;
206 if (!u->source || !pa_iochannel_is_readable(u->io) || !pa_idxset_size(u->source->outputs))
211 l = u->in_fragment_size;
213 if (u->use_getispace) {
216 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0)
217 u->use_getispace = 0;
219 if (info.bytes/l > 0) {
220 l = (info.bytes/l)*l;
227 memchunk.memblock = pa_memblock_new(u->core->mempool, l);
228 assert(memchunk.memblock);
229 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
230 pa_memblock_unref(memchunk.memblock);
232 if (errno != EAGAIN) {
233 pa_log("read() failed: %s", pa_cstrerror(errno));
236 pa_module_unload_request(u->module);
242 assert(r <= (ssize_t) memchunk.memblock->length);
243 memchunk.length = memchunk.memblock->length = r;
246 pa_source_post(u->source, &memchunk);
247 pa_memblock_unref(memchunk.memblock);
249 l = l > (size_t) r ? l - r : 0;
250 } while (loop && l > 0);
253 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
254 struct userdata *u = userdata;
260 static void source_notify_cb(pa_source *s) {
261 struct userdata *u = s->userdata;
266 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
269 struct userdata *u = s->userdata;
270 assert(s && u && u->sink);
272 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
273 pa_log_info("device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
274 s->get_latency = NULL;
278 r += pa_bytes_to_usec(arg, &s->sample_spec);
280 if (u->memchunk.memblock)
281 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
286 static pa_usec_t source_get_latency_cb(pa_source *s) {
287 struct userdata *u = s->userdata;
289 assert(s && u && u->source);
291 if (!u->use_getispace)
294 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
295 u->use_getispace = 0;
302 return pa_bytes_to_usec(info.bytes, &s->sample_spec);
305 static int sink_get_hw_volume(pa_sink *s) {
306 struct userdata *u = s->userdata;
308 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
309 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
310 s->get_hw_volume = NULL;
317 static int sink_set_hw_volume(pa_sink *s) {
318 struct userdata *u = s->userdata;
320 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
321 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
322 s->set_hw_volume = NULL;
329 static int source_get_hw_volume(pa_source *s) {
330 struct userdata *u = s->userdata;
332 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
333 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
334 s->get_hw_volume = NULL;
341 static int source_set_hw_volume(pa_source *s) {
342 struct userdata *u = s->userdata;
344 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
345 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
346 s->set_hw_volume = NULL;
353 int pa__init(pa_core *c, pa_module*m) {
354 struct audio_buf_info info;
355 struct userdata *u = NULL;
358 int nfrags, frag_size, in_frag_size, out_frag_size;
360 int record = 1, playback = 1;
363 pa_modargs *ma = NULL;
366 char *name_buf = NULL;
372 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
373 pa_log("failed to parse module arguments.");
377 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
378 pa_log("record= and playback= expect numeric argument.");
382 if (!playback && !record) {
383 pa_log("neither playback nor record enabled for device.");
387 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
389 ss = c->default_sample_spec;
390 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
391 pa_log("failed to parse sample specification or channel map");
395 /* Fix latency to 100ms */
397 frag_size = pa_bytes_per_second(&ss)/128;
399 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
400 pa_log("failed to parse fragments arguments");
404 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
407 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
408 pa_log_info("hardware name is '%s'.", hwdesc);
412 pa_log_info("device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
414 if (nfrags >= 2 && frag_size >= 1)
415 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
418 if (pa_oss_auto_format(fd, &ss) < 0)
421 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
422 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
426 in_frag_size = out_frag_size = frag_size;
428 u = pa_xmalloc(sizeof(struct userdata));
430 u->use_getospace = u->use_getispace = 0;
432 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
433 pa_log_info("input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
434 in_frag_size = info.fragsize;
435 u->use_getispace = 1;
438 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
439 pa_log_info("output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
440 out_frag_size = info.fragsize;
441 u->use_getospace = 1;
444 if (mode != O_WRONLY) {
445 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
448 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p));
452 if (!(u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map)))
455 u->source->userdata = u;
456 u->source->notify = source_notify_cb;
457 u->source->get_latency = source_get_latency_cb;
458 u->source->get_hw_volume = source_get_hw_volume;
459 u->source->set_hw_volume = source_set_hw_volume;
460 pa_source_set_owner(u->source, m);
461 pa_source_set_description(u->source, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s",
463 hwdesc[0] ? " (" : "",
464 hwdesc[0] ? hwdesc : "",
465 hwdesc[0] ? ")" : ""));
467 u->source->is_hardware = 1;
474 if (mode != O_RDONLY) {
475 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
478 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p));
482 if (!(u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map)))
485 u->sink->get_latency = sink_get_latency_cb;
486 u->sink->get_hw_volume = sink_get_hw_volume;
487 u->sink->set_hw_volume = sink_set_hw_volume;
488 u->sink->userdata = u;
489 pa_sink_set_owner(u->sink, m);
490 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s",
492 hwdesc[0] ? " (" : "",
493 hwdesc[0] ? hwdesc : "",
494 hwdesc[0] ? ")" : ""));
496 u->sink->is_hardware = 1;
503 assert(u->source || u->sink);
505 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : -1);
507 pa_iochannel_set_callback(u->io, io_callback, u);
510 u->memchunk.memblock = NULL;
511 u->memchunk.length = 0;
512 u->sample_size = pa_frame_size(&ss);
514 u->out_fragment_size = out_frag_size;
515 u->in_fragment_size = in_frag_size;
516 u->silence.memblock = pa_memblock_new(u->core->mempool, u->silence.length = u->out_fragment_size);
517 assert(u->silence.memblock);
518 pa_silence_memblock(u->silence.memblock, &ss);
519 u->silence.index = 0;
527 * Some crappy drivers do not start the recording until we read something.
528 * Without this snippet, poll will never register the fd as ready.
531 char *buf = pa_xnew(char, u->sample_size);
532 pa_read(u->fd, buf, u->sample_size, NULL);
536 /* Read mixer settings */
538 source_get_hw_volume(u->source);
540 sink_get_hw_volume(u->sink);
556 void pa__done(pa_core *c, pa_module*m) {
562 if (!(u = m->userdata))
567 if (u->memchunk.memblock)
568 pa_memblock_unref(u->memchunk.memblock);
569 if (u->silence.memblock)
570 pa_memblock_unref(u->silence.memblock);