4 This file is part of PulseAudio.
6 PulseAudio 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.
11 PulseAudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/soundcard.h>
27 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_MMAN_H
42 #include <pulse/xmalloc.h>
43 #include <pulse/util.h>
45 #include <pulsecore/core-error.h>
46 #include <pulsecore/iochannel.h>
47 #include <pulsecore/sink.h>
48 #include <pulsecore/source.h>
49 #include <pulsecore/module.h>
50 #include <pulsecore/sample-util.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/modargs.h>
53 #include <pulsecore/log.h>
56 #include "module-oss-mmap-symdef.h"
58 PA_MODULE_AUTHOR("Lennart Poettering")
59 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
60 PA_MODULE_VERSION(PACKAGE_VERSION)
62 "sink_name=<name for the sink> "
63 "source_name=<name for the source> "
64 "device=<OSS device> "
65 "record=<enable source?> "
66 "playback=<enable sink?> "
67 "format=<sample format> "
68 "channels=<number of channels> "
70 "fragments=<number of fragments> "
71 "fragment_size=<fragment size> "
72 "channel_map=<channel map>")
78 pa_sample_spec sample_spec;
80 size_t in_fragment_size, out_fragment_size;
81 unsigned in_fragments, out_fragments;
82 unsigned out_blocks_saved, in_blocks_saved;
86 void *in_mmap, *out_mmap;
87 size_t in_mmap_length, out_mmap_length;
89 pa_io_event *io_event;
91 pa_memblock **in_memblocks, **out_memblocks;
92 unsigned out_current, in_current;
96 static const char* const valid_modargs[] = {
111 #define DEFAULT_DEVICE "/dev/dsp"
112 #define DEFAULT_NFRAGS 12
113 #define DEFAULT_FRAGSIZE 1024
115 static void update_usage(struct userdata *u) {
116 pa_module_set_used(u->module,
117 (u->sink ? pa_sink_used_by(u->sink) : 0) +
118 (u->source ? pa_source_used_by(u->source) : 0));
121 static void clear_up(struct userdata *u) {
125 pa_sink_disconnect(u->sink);
126 pa_sink_unref(u->sink);
131 pa_source_disconnect(u->source);
132 pa_source_unref(u->source);
136 if (u->in_mmap && u->in_mmap != MAP_FAILED) {
137 munmap(u->in_mmap, u->in_mmap_length);
141 if (u->out_mmap && u->out_mmap != MAP_FAILED) {
142 munmap(u->out_mmap, u->out_mmap_length);
147 u->core->mainloop->io_free(u->io_event);
157 static void out_fill_memblocks(struct userdata *u, unsigned n) {
158 assert(u && u->out_memblocks);
163 if (u->out_memblocks[u->out_current])
164 pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
166 chunk.memblock = u->out_memblocks[u->out_current] =
167 pa_memblock_new_fixed(
169 (uint8_t*) u->out_mmap+u->out_fragment_size*u->out_current,
170 u->out_fragment_size,
172 assert(chunk.memblock);
173 chunk.length = chunk.memblock->length;
176 pa_sink_render_into_full(u->sink, &chunk);
179 while (u->out_current >= u->out_fragments)
180 u->out_current -= u->out_fragments;
186 static void do_write(struct userdata *u) {
187 struct count_info info;
188 assert(u && u->sink);
192 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
193 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
196 pa_module_unload_request(u->module);
200 info.blocks += u->out_blocks_saved;
201 u->out_blocks_saved = 0;
206 out_fill_memblocks(u, info.blocks);
209 static void in_post_memblocks(struct userdata *u, unsigned n) {
210 assert(u && u->in_memblocks);
215 if (!u->in_memblocks[u->in_current]) {
216 chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed(u->core->mempool, (uint8_t*) u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size, 1);
217 chunk.length = chunk.memblock->length;
220 pa_source_post(u->source, &chunk);
224 while (u->in_current >= u->in_fragments)
225 u->in_current -= u->in_fragments;
231 static void in_clear_memblocks(struct userdata*u, unsigned n) {
232 unsigned i = u->in_current;
233 assert(u && u->in_memblocks);
235 if (n > u->in_fragments)
239 if (u->in_memblocks[i]) {
240 pa_memblock_unref_fixed(u->in_memblocks[i]);
241 u->in_memblocks[i] = NULL;
245 while (i >= u->in_fragments)
246 i -= u->in_fragments;
252 static void do_read(struct userdata *u) {
253 struct count_info info;
254 assert(u && u->source);
258 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
259 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
262 pa_module_unload_request(u->module);
266 info.blocks += u->in_blocks_saved;
267 u->in_blocks_saved = 0;
272 in_post_memblocks(u, info.blocks);
273 in_clear_memblocks(u, u->in_fragments/2);
276 static void io_callback(pa_mainloop_api *m, pa_io_event *e, PA_GCC_UNUSED int fd, pa_io_event_flags_t f, void *userdata) {
277 struct userdata *u = userdata;
278 assert (u && u->core->mainloop == m && u->io_event == e);
280 if (f & PA_IO_EVENT_ERROR) {
282 pa_module_unload_request(u->module);
286 if (f & PA_IO_EVENT_INPUT)
288 if (f & PA_IO_EVENT_OUTPUT)
292 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
293 struct userdata *u = s->userdata;
294 struct count_info info;
295 size_t bpos, n, total;
298 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
299 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
303 u->out_blocks_saved += info.blocks;
305 total = u->out_fragments * u->out_fragment_size;
306 bpos = ((u->out_current + u->out_blocks_saved) * u->out_fragment_size) % total;
308 if (bpos <= (size_t) info.ptr)
309 n = total - (info.ptr - bpos);
313 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
315 return pa_bytes_to_usec(n, &s->sample_spec);
318 static pa_usec_t source_get_latency_cb(pa_source *s) {
319 struct userdata *u = s->userdata;
320 struct count_info info;
321 size_t bpos, n, total;
324 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
325 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
329 u->in_blocks_saved += info.blocks;
331 total = u->in_fragments * u->in_fragment_size;
332 bpos = ((u->in_current + u->in_blocks_saved) * u->in_fragment_size) % total;
334 if (bpos <= (size_t) info.ptr)
337 n = (u->in_fragments * u->in_fragment_size) - bpos + info.ptr;
339 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
341 return pa_bytes_to_usec(n, &s->sample_spec);
344 static int sink_get_hw_volume(pa_sink *s) {
345 struct userdata *u = s->userdata;
347 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
348 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
349 s->get_hw_volume = NULL;
356 static int sink_set_hw_volume(pa_sink *s) {
357 struct userdata *u = s->userdata;
359 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
360 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
361 s->set_hw_volume = NULL;
368 static int source_get_hw_volume(pa_source *s) {
369 struct userdata *u = s->userdata;
371 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
372 pa_log_info("device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
373 s->get_hw_volume = NULL;
380 static int source_set_hw_volume(pa_source *s) {
381 struct userdata *u = s->userdata;
383 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
384 pa_log_info("device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
385 s->set_hw_volume = NULL;
392 int pa__init(pa_core *c, pa_module*m) {
393 struct audio_buf_info info;
394 struct userdata *u = NULL;
396 int nfrags, frag_size;
398 int enable_bits = 0, zero = 0;
399 int playback = 1, record = 1;
400 pa_modargs *ma = NULL;
404 char *name_buf = NULL;
410 m->userdata = u = pa_xnew0(struct userdata, 1);
415 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
416 pa_log("failed to parse module arguments.");
420 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
421 pa_log("record= and playback= expect numeric arguments.");
425 if (!playback && !record) {
426 pa_log("neither playback nor record enabled for device.");
430 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
432 nfrags = DEFAULT_NFRAGS;
433 frag_size = DEFAULT_FRAGSIZE;
434 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
435 pa_log("failed to parse fragments arguments");
439 u->sample_spec = c->default_sample_spec;
440 if (pa_modargs_get_sample_spec_and_channel_map(ma, &u->sample_spec, &map, PA_CHANNEL_MAP_OSS) < 0) {
441 pa_log("failed to parse sample specification or channel map");
445 if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
448 if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER)) {
449 pa_log("OSS device not mmap capable.");
453 pa_log_info("device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
455 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
456 pa_log_info("hardware name is '%s'.", hwdesc);
460 if (nfrags >= 2 && frag_size >= 1)
461 if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
464 if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
467 if (mode != O_WRONLY) {
468 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
469 pa_log("SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
473 pa_log_info("input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
474 u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
476 if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
477 if (mode == O_RDWR) {
478 pa_log("mmap failed for input. Changing to O_WRONLY mode.");
481 pa_log("mmap(): %s", pa_cstrerror(errno));
485 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
488 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p));
492 if (!(u->source = pa_source_new(c, __FILE__, name, namereg_fail, &u->sample_spec, &map)))
495 u->source->userdata = u;
496 u->source->get_latency = source_get_latency_cb;
497 u->source->get_hw_volume = source_get_hw_volume;
498 u->source->set_hw_volume = source_set_hw_volume;
499 pa_source_set_owner(u->source, m);
500 pa_source_set_description(u->source, t = pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
502 hwdesc[0] ? " (" : "",
503 hwdesc[0] ? hwdesc : "",
504 hwdesc[0] ? ")" : ""));
506 u->source->is_hardware = 1;
508 u->in_memblocks = pa_xnew0(pa_memblock*, u->in_fragments);
510 enable_bits |= PCM_ENABLE_INPUT;
517 if (mode != O_RDONLY) {
518 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
519 pa_log("SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
523 pa_log_info("output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
524 u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
526 if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
527 if (mode == O_RDWR) {
528 pa_log("mmap filed for output. Changing to O_RDONLY mode.");
531 pa_log("mmap(): %s", pa_cstrerror(errno));
535 pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
537 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
540 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p));
544 if (!(u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &u->sample_spec, &map)))
547 u->sink->get_latency = sink_get_latency_cb;
548 u->sink->get_hw_volume = sink_get_hw_volume;
549 u->sink->set_hw_volume = sink_set_hw_volume;
550 u->sink->userdata = u;
551 pa_sink_set_owner(u->sink, m);
552 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("OSS PCM/mmap() on %s%s%s%s",
554 hwdesc[0] ? " (" : "",
555 hwdesc[0] ? hwdesc : "",
556 hwdesc[0] ? ")" : ""));
559 u->sink->is_hardware = 1;
560 u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments);
562 enable_bits |= PCM_ENABLE_OUTPUT;
570 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
571 pa_log("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
575 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
576 pa_log("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
580 assert(u->source || u->sink);
582 u->io_event = c->mainloop->io_new(c->mainloop, u->fd, (u->source ? PA_IO_EVENT_INPUT : 0) | (u->sink ? PA_IO_EVENT_OUTPUT : 0), io_callback, u);
587 /* Read mixer settings */
589 source_get_hw_volume(u->source);
591 sink_get_hw_volume(u->sink);
606 void pa__done(pa_core *c, pa_module*m) {
612 if (!(u = m->userdata))
617 if (u->out_memblocks) {
619 for (i = 0; i < u->out_fragments; i++)
620 if (u->out_memblocks[i])
621 pa_memblock_unref_fixed(u->out_memblocks[i]);
622 pa_xfree(u->out_memblocks);
625 if (u->in_memblocks) {
627 for (i = 0; i < u->in_fragments; i++)
628 if (u->in_memblocks[i])
629 pa_memblock_unref_fixed(u->in_memblocks[i]);
630 pa_xfree(u->in_memblocks);