X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/13248fd8e6cb44e489bd2d77d5ec3491287a1f4d..fa499dad06ba6558111cdef64c18f2401e803cff:/polyp/module-oss.c diff --git a/polyp/module-oss.c b/polyp/module-oss.c index 95deca9c..fe12b0bb 100644 --- a/polyp/module-oss.c +++ b/polyp/module-oss.c @@ -4,7 +4,7 @@ This file is part of polypaudio. polypaudio is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published + it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -13,7 +13,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with polypaudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. @@ -45,6 +45,12 @@ #include "modargs.h" #include "xmalloc.h" #include "log.h" +#include "module-oss-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering") +PA_MODULE_DESCRIPTION("OSS Sink/Source") +PA_MODULE_VERSION(PACKAGE_VERSION) +PA_MODULE_USAGE("sink_name= source_name= device= record= playback= format= channels= rate= fragments= fragment_size=") struct userdata { struct pa_sink *sink; @@ -55,6 +61,7 @@ struct userdata { struct pa_memchunk memchunk, silence; uint32_t in_fragment_size, out_fragment_size, sample_size; + int use_getospace, use_getispace; int fd; struct pa_module *module; @@ -88,6 +95,9 @@ static void update_usage(struct userdata *u) { static void do_write(struct userdata *u) { struct pa_memchunk *memchunk; ssize_t r; + size_t l; + int loop = 0; + assert(u); if (!u->sink || !pa_iochannel_is_writable(u->io)) @@ -95,37 +105,58 @@ static void do_write(struct userdata *u) { update_usage(u); - memchunk = &u->memchunk; - - if (!memchunk->length) - if (pa_sink_render(u->sink, u->out_fragment_size, memchunk) < 0) - memchunk = &u->silence; + l = u->out_fragment_size; - assert(memchunk->memblock); - assert(memchunk->memblock->data); - assert(memchunk->length); - - if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) { - pa_log(__FILE__": write() failed: %s\n", strerror(errno)); - return; + if (u->use_getospace) { + audio_buf_info info; + + if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) + u->use_getospace = 0; + else { + if (info.bytes/l > 0) { + l = (info.bytes/l)*l; + loop = 1; + } + } } + + do { + memchunk = &u->memchunk; + + if (!memchunk->length) + if (pa_sink_render(u->sink, l, memchunk) < 0) + memchunk = &u->silence; - if (memchunk == &u->silence) - assert(r % u->sample_size == 0); - else { - u->memchunk.index += r; - u->memchunk.length -= r; + assert(memchunk->memblock); + assert(memchunk->memblock->data); + assert(memchunk->length); - if (u->memchunk.length <= 0) { - pa_memblock_unref(u->memchunk.memblock); - u->memchunk.memblock = NULL; + if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) { + pa_log(__FILE__": write() failed: %s\n", strerror(errno)); + break; } - } + + if (memchunk == &u->silence) + assert(r % u->sample_size == 0); + else { + u->memchunk.index += r; + u->memchunk.length -= r; + + if (u->memchunk.length <= 0) { + pa_memblock_unref(u->memchunk.memblock); + u->memchunk.memblock = NULL; + } + } + + l = l > (size_t) r ? l - r : 0; + } while (loop && l > 0); } static void do_read(struct userdata *u) { struct pa_memchunk memchunk; ssize_t r; + size_t l; + int loop = 0; assert(u); if (!u->source || !pa_iochannel_is_readable(u->io)) @@ -133,21 +164,40 @@ static void do_read(struct userdata *u) { update_usage(u); - memchunk.memblock = pa_memblock_new(u->in_fragment_size, u->core->memblock_stat); - assert(memchunk.memblock); - if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) { - pa_memblock_unref(memchunk.memblock); - if (errno != EAGAIN) - pa_log(__FILE__": read() failed: %s\n", strerror(errno)); - return; - } + l = u->in_fragment_size; - assert(r <= (ssize_t) memchunk.memblock->length); - memchunk.length = memchunk.memblock->length = r; - memchunk.index = 0; + if (u->use_getispace) { + audio_buf_info info; + + if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) + u->use_getispace = 0; + else { + if (info.bytes/l > 0) { + l = (info.bytes/l)*l; + loop = 1; + } + } + } + + do { + memchunk.memblock = pa_memblock_new(l, u->core->memblock_stat); + assert(memchunk.memblock); + if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) { + pa_memblock_unref(memchunk.memblock); + if (errno != EAGAIN) + pa_log(__FILE__": read() failed: %s\n", strerror(errno)); + break; + } + + assert(r <= (ssize_t) memchunk.memblock->length); + memchunk.length = memchunk.memblock->length = r; + memchunk.index = 0; + + pa_source_post(u->source, &memchunk); + pa_memblock_unref(memchunk.memblock); - pa_source_post(u->source, &memchunk); - pa_memblock_unref(memchunk.memblock); + l = l > (size_t) r ? l - r : 0; + } while (loop && l > 0); } static void io_callback(struct pa_iochannel *io, void*userdata) { @@ -157,7 +207,8 @@ static void io_callback(struct pa_iochannel *io, void*userdata) { do_read(u); } -static uint32_t sink_get_latency_cb(struct pa_sink *s) { +static pa_usec_t sink_get_latency_cb(struct pa_sink *s) { + pa_usec_t r = 0; int arg; struct userdata *u = s->userdata; assert(s && u && u->sink); @@ -168,10 +219,34 @@ static uint32_t sink_get_latency_cb(struct pa_sink *s) { return 0; } - return pa_bytes_to_usec(arg, &s->sample_spec); + r += pa_bytes_to_usec(arg, &s->sample_spec); + + if (u->memchunk.memblock) + r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec); + + return r; +} + +static pa_usec_t source_get_latency_cb(struct pa_source *s) { + struct userdata *u = s->userdata; + audio_buf_info info; + assert(s && u && u->sink); + + if (!u->use_getispace) + return 0; + + if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) { + u->use_getispace = 0; + return 0; + } + + if (info.bytes <= 0) + return 0; + + return pa_bytes_to_usec(info.bytes, &s->sample_spec); } -int pa_module_init(struct pa_core *c, struct pa_module*m) { +int pa__init(struct pa_core *c, struct pa_module*m) { struct audio_buf_info info; struct userdata *u = NULL; const char *p; @@ -233,23 +308,27 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { assert(frag_size); in_frag_size = out_frag_size = frag_size; + u = pa_xmalloc(sizeof(struct userdata)); + u->core = c; + u->use_getospace = u->use_getispace = 0; + if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) { pa_log(__FILE__": input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize); in_frag_size = info.fragsize; + u->use_getispace = 1; } if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) { pa_log(__FILE__": output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize); out_frag_size = info.fragsize; + u->use_getospace = 1; } - u = pa_xmalloc(sizeof(struct userdata)); - u->core = c; - if (mode != O_WRONLY) { u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss); assert(u->source); u->source->userdata = u; + u->source->get_latency = source_get_latency_cb; pa_source_set_owner(u->source, m); u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p); } else @@ -300,7 +379,7 @@ fail: return -1; } -void pa_module_done(struct pa_core *c, struct pa_module*m) { +void pa__done(struct pa_core *c, struct pa_module*m) { struct userdata *u; assert(c && m); @@ -312,10 +391,15 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { if (u->silence.memblock) pa_memblock_unref(u->silence.memblock); - if (u->sink) - pa_sink_free(u->sink); - if (u->source) - pa_source_free(u->source); + if (u->sink) { + pa_sink_disconnect(u->sink); + pa_sink_unref(u->sink); + } + + if (u->source) { + pa_source_disconnect(u->source); + pa_source_unref(u->source); + } pa_iochannel_free(u->io); pa_xfree(u);