4 This file is part of polypaudio.
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.
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.
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
26 #include <sys/soundcard.h>
27 #include <sys/ioctl.h>
39 #include <polyp/xmalloc.h>
41 #include <polypcore/iochannel.h>
42 #include <polypcore/sink.h>
43 #include <polypcore/source.h>
44 #include <polypcore/module.h>
45 #include <polypcore/sample-util.h>
46 #include <polypcore/util.h>
47 #include <polypcore/modargs.h>
48 #include <polypcore/log.h>
51 #include "module-oss-mmap-symdef.h"
53 PA_MODULE_AUTHOR("Lennart Poettering")
54 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
55 PA_MODULE_VERSION(PACKAGE_VERSION)
57 "sink_name=<name for the sink> "
58 "source_name=<name for the source> "
59 "device=<OSS device> "
60 "record=<enable source?> "
61 "playback=<enable sink?> "
62 "format=<sample format> "
63 "channels=<number of channels> "
65 "fragments=<number of fragments> "
66 "fragment_size=<fragment size> "
67 "channel_map=<channel map>")
73 pa_sample_spec sample_spec;
75 size_t in_fragment_size, out_fragment_size;
76 unsigned in_fragments, out_fragments;
77 unsigned out_blocks_saved, in_blocks_saved;
81 void *in_mmap, *out_mmap;
82 size_t in_mmap_length, out_mmap_length;
84 pa_io_event *io_event;
86 pa_memblock **in_memblocks, **out_memblocks;
87 unsigned out_current, in_current;
91 static const char* const valid_modargs[] = {
106 #define DEFAULT_SINK_NAME "oss_output"
107 #define DEFAULT_SOURCE_NAME "oss_input"
108 #define DEFAULT_DEVICE "/dev/dsp"
109 #define DEFAULT_NFRAGS 12
110 #define DEFAULT_FRAGSIZE 1024
112 static void update_usage(struct userdata *u) {
113 pa_module_set_used(u->module,
114 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
115 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
116 (u->source ? pa_idxset_size(u->source->outputs) : 0));
119 static void out_fill_memblocks(struct userdata *u, unsigned n) {
120 assert(u && u->out_memblocks);
125 if (u->out_memblocks[u->out_current])
126 pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
128 chunk.memblock = u->out_memblocks[u->out_current] =
129 pa_memblock_new_fixed(
130 (uint8_t*) u->out_mmap+u->out_fragment_size*u->out_current,
131 u->out_fragment_size,
133 u->core->memblock_stat);
134 assert(chunk.memblock);
135 chunk.length = chunk.memblock->length;
138 pa_sink_render_into_full(u->sink, &chunk);
141 while (u->out_current >= u->out_fragments)
142 u->out_current -= u->out_fragments;
148 static void do_write(struct userdata *u) {
149 struct count_info info;
150 assert(u && u->sink);
154 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
155 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", strerror(errno));
159 info.blocks += u->out_blocks_saved;
160 u->out_blocks_saved = 0;
165 out_fill_memblocks(u, info.blocks);
168 static void in_post_memblocks(struct userdata *u, unsigned n) {
169 assert(u && u->in_memblocks);
174 if (!u->in_memblocks[u->in_current]) {
175 chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed((uint8_t*) u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size, 1, u->core->memblock_stat);
176 chunk.length = chunk.memblock->length;
179 pa_source_post(u->source, &chunk);
183 while (u->in_current >= u->in_fragments)
184 u->in_current -= u->in_fragments;
190 static void in_clear_memblocks(struct userdata*u, unsigned n) {
191 unsigned i = u->in_current;
192 assert(u && u->in_memblocks);
194 if (n > u->in_fragments)
198 if (u->in_memblocks[i]) {
199 pa_memblock_unref_fixed(u->in_memblocks[i]);
200 u->in_memblocks[i] = NULL;
204 while (i >= u->in_fragments)
205 i -= u->in_fragments;
211 static void do_read(struct userdata *u) {
212 struct count_info info;
213 assert(u && u->source);
217 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
218 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", strerror(errno));
222 info.blocks += u->in_blocks_saved;
223 u->in_blocks_saved = 0;
228 in_post_memblocks(u, info.blocks);
229 in_clear_memblocks(u, u->in_fragments/2);
232 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) {
233 struct userdata *u = userdata;
234 assert (u && u->core->mainloop == m && u->io_event == e);
236 if (f & PA_IO_EVENT_INPUT)
238 if (f & PA_IO_EVENT_OUTPUT)
242 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
243 struct userdata *u = s->userdata;
244 struct count_info info;
245 size_t bpos, n, total;
248 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
249 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", strerror(errno));
253 u->out_blocks_saved += info.blocks;
255 total = u->out_fragments * u->out_fragment_size;
256 bpos = ((u->out_current + u->out_blocks_saved) * u->out_fragment_size) % total;
258 if (bpos <= (size_t) info.ptr)
259 n = total - (info.ptr - bpos);
263 /* 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); */
265 return pa_bytes_to_usec(n, &s->sample_spec);
268 static pa_usec_t source_get_latency_cb(pa_source *s) {
269 struct userdata *u = s->userdata;
270 struct count_info info;
271 size_t bpos, n, total;
274 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
275 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", strerror(errno));
279 u->in_blocks_saved += info.blocks;
281 total = u->in_fragments * u->in_fragment_size;
282 bpos = ((u->in_current + u->in_blocks_saved) * u->in_fragment_size) % total;
284 if (bpos <= (size_t) info.ptr)
287 n = (u->in_fragments * u->in_fragment_size) - bpos + info.ptr;
289 /* 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); */
291 return pa_bytes_to_usec(n, &s->sample_spec);
294 static int sink_get_hw_volume(pa_sink *s) {
295 struct userdata *u = s->userdata;
297 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
298 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
299 s->get_hw_volume = NULL;
306 static int sink_set_hw_volume(pa_sink *s) {
307 struct userdata *u = s->userdata;
309 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
310 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
311 s->set_hw_volume = NULL;
318 static int source_get_hw_volume(pa_source *s) {
319 struct userdata *u = s->userdata;
321 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
322 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
323 s->get_hw_volume = NULL;
330 static int source_set_hw_volume(pa_source *s) {
331 struct userdata *u = s->userdata;
333 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
334 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
335 s->set_hw_volume = NULL;
342 int pa__init(pa_core *c, pa_module*m) {
343 struct audio_buf_info info;
344 struct userdata *u = NULL;
346 int nfrags, frag_size;
348 int enable_bits = 0, zero = 0;
349 int playback = 1, record = 1;
350 pa_modargs *ma = NULL;
357 m->userdata = u = pa_xnew0(struct userdata, 1);
362 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
363 pa_log(__FILE__": failed to parse module arguments.");
367 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
368 pa_log(__FILE__": record= and playback= expect numeric arguments.");
372 if (!playback && !record) {
373 pa_log(__FILE__": neither playback nor record enabled for device.");
377 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
379 nfrags = DEFAULT_NFRAGS;
380 frag_size = DEFAULT_FRAGSIZE;
381 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
382 pa_log(__FILE__": failed to parse fragments arguments");
386 u->sample_spec = c->default_sample_spec;
387 if (pa_modargs_get_sample_spec_and_channel_map(ma, &u->sample_spec, &map, PA_CHANNEL_MAP_OSS) < 0) {
388 pa_log(__FILE__": failed to parse sample specification or channel map");
392 if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
395 if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
396 pa_log(__FILE__": OSS device not mmap capable.");
400 pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
402 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
403 pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
407 if (nfrags >= 2 && frag_size >= 1)
408 if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
411 if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
414 if (mode != O_WRONLY) {
415 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
416 pa_log(__FILE__": SNDCTL_DSP_GETISPACE: %s", strerror(errno));
420 pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
421 u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
423 if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
424 if (mode == O_RDWR) {
425 pa_log(__FILE__": mmap failed for input. Changing to O_WRONLY mode.");
428 pa_log(__FILE__": mmap(): %s", strerror(errno));
433 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec, &map)))
436 u->source->userdata = u;
437 u->source->get_latency = source_get_latency_cb;
438 u->source->get_hw_volume = source_get_hw_volume;
439 u->source->set_hw_volume = source_set_hw_volume;
440 pa_source_set_owner(u->source, m);
441 u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
443 hwdesc[0] ? " (" : "",
444 hwdesc[0] ? hwdesc : "",
445 hwdesc[0] ? ")" : "");
447 u->in_memblocks = pa_xnew0(pa_memblock*, u->in_fragments);
449 enable_bits |= PCM_ENABLE_INPUT;
453 if (mode != O_RDONLY) {
454 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
455 pa_log(__FILE__": SNDCTL_DSP_GETOSPACE: %s", strerror(errno));
459 pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
460 u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
462 if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
463 if (mode == O_RDWR) {
464 pa_log(__FILE__": mmap filed for output. Changing to O_RDONLY mode.");
467 pa_log(__FILE__": mmap(): %s", strerror(errno));
471 pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
473 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec, &map)))
476 u->sink->get_latency = sink_get_latency_cb;
477 u->sink->get_hw_volume = sink_get_hw_volume;
478 u->sink->set_hw_volume = sink_set_hw_volume;
479 u->sink->userdata = u;
480 pa_sink_set_owner(u->sink, m);
481 u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
483 hwdesc[0] ? " (" : "",
484 hwdesc[0] ? hwdesc : "",
485 hwdesc[0] ? ")" : "");
487 u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments);
489 enable_bits |= PCM_ENABLE_OUTPUT;
494 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
495 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno));
499 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
500 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno));
504 assert(u->source || u->sink);
506 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);
511 /* Read mixer settings */
513 source_get_hw_volume(u->source);
515 sink_get_hw_volume(u->sink);
528 void pa__done(pa_core *c, pa_module*m) {
534 if (!(u = m->userdata))
537 if (u->out_memblocks) {
539 for (i = 0; i < u->out_fragments; i++)
540 if (u->out_memblocks[i])
541 pa_memblock_unref_fixed(u->out_memblocks[i]);
542 pa_xfree(u->out_memblocks);
545 if (u->in_memblocks) {
547 for (i = 0; i < u->in_fragments; i++)
548 if (u->in_memblocks[i])
549 pa_memblock_unref_fixed(u->in_memblocks[i]);
550 pa_xfree(u->in_memblocks);
553 if (u->in_mmap && u->in_mmap != MAP_FAILED)
554 munmap(u->in_mmap, u->in_mmap_length);
556 if (u->out_mmap && u->out_mmap != MAP_FAILED)
557 munmap(u->out_mmap, u->out_mmap_length);
560 pa_sink_disconnect(u->sink);
561 pa_sink_unref(u->sink);
565 pa_source_disconnect(u->source);
566 pa_source_unref(u->source);
570 u->core->mainloop->io_free(u->io_event);