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 <polypcore/iochannel.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/source.h>
42 #include <polypcore/module.h>
43 #include <polypcore/sample-util.h>
44 #include <polypcore/util.h>
45 #include <polypcore/modargs.h>
46 #include <polypcore/xmalloc.h>
47 #include <polypcore/log.h>
50 #include "module-oss-mmap-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
54 PA_MODULE_VERSION(PACKAGE_VERSION)
56 "sink_name=<name for the sink> "
57 "source_name=<name for the source> "
58 "device=<OSS device> "
59 "record=<enable source?> "
60 "playback=<enable sink?> "
61 "format=<sample format> "
62 "channels=<number of channels> "
64 "fragments=<number of fragments> "
65 "fragment_size=<fragment size> "
66 "channel_map=<channel map>")
72 pa_sample_spec sample_spec;
74 size_t in_fragment_size, out_fragment_size;
75 unsigned in_fragments, out_fragments;
76 unsigned out_blocks_saved, in_blocks_saved;
80 void *in_mmap, *out_mmap;
81 size_t in_mmap_length, out_mmap_length;
83 pa_io_event *io_event;
85 pa_memblock **in_memblocks, **out_memblocks;
86 unsigned out_current, in_current;
90 static const char* const valid_modargs[] = {
105 #define DEFAULT_SINK_NAME "oss_output"
106 #define DEFAULT_SOURCE_NAME "oss_input"
107 #define DEFAULT_DEVICE "/dev/dsp"
108 #define DEFAULT_NFRAGS 12
109 #define DEFAULT_FRAGSIZE 1024
111 static void update_usage(struct userdata *u) {
112 pa_module_set_used(u->module,
113 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
114 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
115 (u->source ? pa_idxset_size(u->source->outputs) : 0));
118 static void out_fill_memblocks(struct userdata *u, unsigned n) {
119 assert(u && u->out_memblocks);
124 if (u->out_memblocks[u->out_current])
125 pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
127 chunk.memblock = u->out_memblocks[u->out_current] =
128 pa_memblock_new_fixed(
129 (uint8_t*) u->out_mmap+u->out_fragment_size*u->out_current,
130 u->out_fragment_size,
132 u->core->memblock_stat);
133 assert(chunk.memblock);
134 chunk.length = chunk.memblock->length;
137 pa_sink_render_into_full(u->sink, &chunk);
140 while (u->out_current >= u->out_fragments)
141 u->out_current -= u->out_fragments;
147 static void do_write(struct userdata *u) {
148 struct count_info info;
149 assert(u && u->sink);
153 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
154 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", strerror(errno));
158 info.blocks += u->out_blocks_saved;
159 u->out_blocks_saved = 0;
164 out_fill_memblocks(u, info.blocks);
167 static void in_post_memblocks(struct userdata *u, unsigned n) {
168 assert(u && u->in_memblocks);
173 if (!u->in_memblocks[u->in_current]) {
174 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);
175 chunk.length = chunk.memblock->length;
178 pa_source_post(u->source, &chunk);
182 while (u->in_current >= u->in_fragments)
183 u->in_current -= u->in_fragments;
189 static void in_clear_memblocks(struct userdata*u, unsigned n) {
190 unsigned i = u->in_current;
191 assert(u && u->in_memblocks);
193 if (n > u->in_fragments)
197 if (u->in_memblocks[i]) {
198 pa_memblock_unref_fixed(u->in_memblocks[i]);
199 u->in_memblocks[i] = NULL;
203 while (i >= u->in_fragments)
204 i -= u->in_fragments;
210 static void do_read(struct userdata *u) {
211 struct count_info info;
212 assert(u && u->source);
216 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
217 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", strerror(errno));
221 info.blocks += u->in_blocks_saved;
222 u->in_blocks_saved = 0;
227 in_post_memblocks(u, info.blocks);
228 in_clear_memblocks(u, u->in_fragments/2);
231 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) {
232 struct userdata *u = userdata;
233 assert (u && u->core->mainloop == m && u->io_event == e);
235 if (f & PA_IO_EVENT_INPUT)
237 if (f & PA_IO_EVENT_OUTPUT)
241 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
242 struct userdata *u = s->userdata;
243 struct count_info info;
244 size_t bpos, n, total;
247 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
248 pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", strerror(errno));
252 u->out_blocks_saved += info.blocks;
254 total = u->out_fragments * u->out_fragment_size;
255 bpos = ((u->out_current + u->out_blocks_saved) * u->out_fragment_size) % total;
257 if (bpos <= (size_t) info.ptr)
258 n = total - (info.ptr - bpos);
262 /* 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); */
264 return pa_bytes_to_usec(n, &s->sample_spec);
267 static pa_usec_t source_get_latency_cb(pa_source *s) {
268 struct userdata *u = s->userdata;
269 struct count_info info;
270 size_t bpos, n, total;
273 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
274 pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", strerror(errno));
278 u->in_blocks_saved += info.blocks;
280 total = u->in_fragments * u->in_fragment_size;
281 bpos = ((u->in_current + u->in_blocks_saved) * u->in_fragment_size) % total;
283 if (bpos <= (size_t) info.ptr)
286 n = (u->in_fragments * u->in_fragment_size) - bpos + info.ptr;
288 /* 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); */
290 return pa_bytes_to_usec(n, &s->sample_spec);
293 static int sink_get_hw_volume(pa_sink *s) {
294 struct userdata *u = s->userdata;
296 if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
297 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
298 s->get_hw_volume = NULL;
305 static int sink_set_hw_volume(pa_sink *s) {
306 struct userdata *u = s->userdata;
308 if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
309 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
310 s->set_hw_volume = NULL;
317 static int source_get_hw_volume(pa_source *s) {
318 struct userdata *u = s->userdata;
320 if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
321 pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
322 s->get_hw_volume = NULL;
329 static int source_set_hw_volume(pa_source *s) {
330 struct userdata *u = s->userdata;
332 if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
333 pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
334 s->set_hw_volume = NULL;
341 int pa__init(pa_core *c, pa_module*m) {
342 struct audio_buf_info info;
343 struct userdata *u = NULL;
345 int nfrags, frag_size;
347 int enable_bits = 0, zero = 0;
348 int playback = 1, record = 1;
349 pa_modargs *ma = NULL;
356 m->userdata = u = pa_xnew0(struct userdata, 1);
361 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
362 pa_log(__FILE__": failed to parse module arguments.");
366 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
367 pa_log(__FILE__": record= and playback= expect numeric arguments.");
371 if (!playback && !record) {
372 pa_log(__FILE__": neither playback nor record enabled for device.");
376 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
378 nfrags = DEFAULT_NFRAGS;
379 frag_size = DEFAULT_FRAGSIZE;
380 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
381 pa_log(__FILE__": failed to parse fragments arguments");
385 u->sample_spec = c->default_sample_spec;
386 if (pa_modargs_get_sample_spec_and_channel_map(ma, &u->sample_spec, &map) < 0) {
387 pa_log(__FILE__": failed to parse sample specification or channel map");
391 if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
394 if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
395 pa_log(__FILE__": OSS device not mmap capable.");
399 pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
401 if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
402 pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
406 if (nfrags >= 2 && frag_size >= 1)
407 if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
410 if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
413 if (mode != O_WRONLY) {
414 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
415 pa_log(__FILE__": SNDCTL_DSP_GETISPACE: %s", strerror(errno));
419 pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
420 u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
422 if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
423 if (mode == O_RDWR) {
424 pa_log(__FILE__": mmap failed for input. Changing to O_WRONLY mode.");
427 pa_log(__FILE__": mmap(): %s", strerror(errno));
432 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec, &map)))
435 u->source->userdata = u;
436 u->source->get_latency = source_get_latency_cb;
437 u->source->get_hw_volume = source_get_hw_volume;
438 u->source->set_hw_volume = source_set_hw_volume;
439 pa_source_set_owner(u->source, m);
440 u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
442 hwdesc[0] ? " (" : "",
443 hwdesc[0] ? hwdesc : "",
444 hwdesc[0] ? ")" : "");
446 u->in_memblocks = pa_xnew0(pa_memblock*, u->in_fragments);
448 enable_bits |= PCM_ENABLE_INPUT;
452 if (mode != O_RDONLY) {
453 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
454 pa_log(__FILE__": SNDCTL_DSP_GETOSPACE: %s", strerror(errno));
458 pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
459 u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
461 if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
462 if (mode == O_RDWR) {
463 pa_log(__FILE__": mmap filed for output. Changing to O_RDONLY mode.");
466 pa_log(__FILE__": mmap(): %s", strerror(errno));
470 pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
472 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec, &map)))
475 u->sink->get_latency = sink_get_latency_cb;
476 u->sink->get_hw_volume = sink_get_hw_volume;
477 u->sink->set_hw_volume = sink_set_hw_volume;
478 u->sink->userdata = u;
479 pa_sink_set_owner(u->sink, m);
480 u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
482 hwdesc[0] ? " (" : "",
483 hwdesc[0] ? hwdesc : "",
484 hwdesc[0] ? ")" : "");
486 u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments);
488 enable_bits |= PCM_ENABLE_OUTPUT;
493 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
494 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno));
498 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
499 pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno));
503 assert(u->source || u->sink);
505 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);
510 /* Read mixer settings */
512 source_get_hw_volume(u->source);
514 sink_get_hw_volume(u->sink);
527 void pa__done(pa_core *c, pa_module*m) {
533 if (!(u = m->userdata))
536 if (u->out_memblocks) {
538 for (i = 0; i < u->out_fragments; i++)
539 if (u->out_memblocks[i])
540 pa_memblock_unref_fixed(u->out_memblocks[i]);
541 pa_xfree(u->out_memblocks);
544 if (u->in_memblocks) {
546 for (i = 0; i < u->in_fragments; i++)
547 if (u->in_memblocks[i])
548 pa_memblock_unref_fixed(u->in_memblocks[i]);
549 pa_xfree(u->in_memblocks);
552 if (u->in_mmap && u->in_mmap != MAP_FAILED)
553 munmap(u->in_mmap, u->in_mmap_length);
555 if (u->out_mmap && u->out_mmap != MAP_FAILED)
556 munmap(u->out_mmap, u->out_mmap_length);
559 pa_sink_disconnect(u->sink);
560 pa_sink_unref(u->sink);
564 pa_source_disconnect(u->source);
565 pa_source_unref(u->source);
569 u->core->mainloop->io_free(u->io_event);