]> code.delx.au - pulseaudio/blobdiff - src/modules/module-oss-mmap.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[pulseaudio] / src / modules / module-oss-mmap.c
index 0ed2af93897db1cb44c0f26a8721945bb4ebb1a2..5cf6228f26a631f9a13bb6c9c119ed9608765d9a 100644 (file)
 #include <limits.h>
 #include <sys/mman.h>
 
+#include <polyp/xmalloc.h>
+
+#include <polypcore/core-error.h>
 #include <polypcore/iochannel.h>
 #include <polypcore/sink.h>
 #include <polypcore/source.h>
 #include <polypcore/module.h>
 #include <polypcore/sample-util.h>
-#include <polypcore/util.h>
+#include <polypcore/core-util.h>
 #include <polypcore/modargs.h>
-#include <polypcore/xmalloc.h>
 #include <polypcore/log.h>
 
 #include "oss-util.h"
 PA_MODULE_AUTHOR("Lennart Poettering")
 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
 PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("sink_name=<name for the sink> source_name=<name for the source> device=<OSS device> record=<enable source?> playback=<enable sink?> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
+PA_MODULE_USAGE(
+        "sink_name=<name for the sink> "
+        "source_name=<name for the source> "
+        "device=<OSS device> "
+        "record=<enable source?> "
+        "playback=<enable sink?> "
+        "format=<sample format> "
+        "channels=<number of channels> "
+        "rate=<sample rate> "
+        "fragments=<number of fragments> "
+        "fragment_size=<fragment size> "
+        "channel_map=<channel map>")
 
 struct userdata {
     pa_sink *sink;
@@ -60,7 +73,9 @@ struct userdata {
     pa_core *core;
     pa_sample_spec sample_spec;
 
-    size_t in_fragment_size, out_fragment_size, in_fragments, out_fragments, out_fill;
+    size_t in_fragment_size, out_fragment_size;
+    unsigned in_fragments, out_fragments;
+    unsigned out_blocks_saved, in_blocks_saved;
 
     int fd;
 
@@ -85,12 +100,15 @@ static const char* const valid_modargs[] = {
     "format",
     "rate",
     "channels",
+    "channel_map",
     NULL
 };
 
 #define DEFAULT_SINK_NAME "oss_output"
 #define DEFAULT_SOURCE_NAME "oss_input"
 #define DEFAULT_DEVICE "/dev/dsp"
+#define DEFAULT_NFRAGS 12
+#define DEFAULT_FRAGSIZE 1024
 
 static void update_usage(struct userdata *u) {
    pa_module_set_used(u->module,
@@ -108,7 +126,12 @@ static void out_fill_memblocks(struct userdata *u, unsigned n) {
         if (u->out_memblocks[u->out_current])
             pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
             
-        chunk.memblock = u->out_memblocks[u->out_current] = pa_memblock_new_fixed((uint8_t*)u->out_mmap+u->out_fragment_size*u->out_current, u->out_fragment_size, 1, u->core->memblock_stat);
+        chunk.memblock = u->out_memblocks[u->out_current] =
+            pa_memblock_new_fixed(
+                    (uint8_t*) u->out_mmap+u->out_fragment_size*u->out_current,
+                    u->out_fragment_size,
+                    1,
+                    u->core->memblock_stat);
         assert(chunk.memblock);
         chunk.length = chunk.memblock->length;
         chunk.index = 0;
@@ -130,12 +153,13 @@ static void do_write(struct userdata *u) {
     update_usage(u);
     
     if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
-        pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", strerror(errno));
+        pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
         return;
     }
 
-    u->out_fill = (u->out_fragment_size * u->out_fragments) - (info.ptr % u->out_fragment_size);
-
+    info.blocks += u->out_blocks_saved;
+    u->out_blocks_saved = 0;
+    
     if (!info.blocks)
         return;
     
@@ -192,10 +216,13 @@ static void do_read(struct userdata *u) {
     update_usage(u);
     
     if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
-        pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", strerror(errno));
+        pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
         return;
     }
 
+    info.blocks += u->in_blocks_saved;
+    u->in_blocks_saved = 0;
+        
     if (!info.blocks)
         return;
     
@@ -215,17 +242,61 @@ static void io_callback(pa_mainloop_api *m, pa_io_event *e, PA_GCC_UNUSED int fd
 
 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
     struct userdata *u = s->userdata;
+    struct count_info info;
+    size_t bpos, n, total;
+    assert(s && u);
+
+    if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
+        pa_log(__FILE__": SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
+        return 0;
+    }
+
+    u->out_blocks_saved += info.blocks;
+
+    total = u->out_fragments * u->out_fragment_size;
+    bpos = ((u->out_current + u->out_blocks_saved) * u->out_fragment_size) % total;
+
+    if (bpos <= (size_t) info.ptr)
+        n = total - (info.ptr - bpos);
+    else
+        n = bpos - info.ptr;
+
+/*     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); */
+    
+    return pa_bytes_to_usec(n, &s->sample_spec);
+}
+
+static pa_usec_t source_get_latency_cb(pa_source *s) {
+    struct userdata *u = s->userdata;
+    struct count_info info;
+    size_t bpos, n, total;
     assert(s && u);
 
-    do_write(u);
-    return pa_bytes_to_usec(u->out_fill, &s->sample_spec);
+    if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
+        pa_log(__FILE__": SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
+        return 0;
+    }
+
+    u->in_blocks_saved += info.blocks;
+
+    total = u->in_fragments * u->in_fragment_size;
+    bpos = ((u->in_current + u->in_blocks_saved) * u->in_fragment_size) % total;
+
+    if (bpos <= (size_t) info.ptr)
+        n = info.ptr - bpos;
+    else
+        n = (u->in_fragments * u->in_fragment_size) - bpos + info.ptr;
+
+/*     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);  */
+    
+    return pa_bytes_to_usec(n, &s->sample_spec);
 }
 
 static int sink_get_hw_volume(pa_sink *s) {
     struct userdata *u = s->userdata;
 
     if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
+        pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
         s->get_hw_volume = NULL;
         return -1;
     }
@@ -237,7 +308,7 @@ static int sink_set_hw_volume(pa_sink *s) {
     struct userdata *u = s->userdata;
 
     if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
+        pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
         s->set_hw_volume = NULL;
         return -1;
     }
@@ -249,7 +320,7 @@ static int source_get_hw_volume(pa_source *s) {
     struct userdata *u = s->userdata;
 
     if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", strerror(errno));
+        pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
         s->get_hw_volume = NULL;
         return -1;
     }
@@ -261,7 +332,7 @@ static int source_set_hw_volume(pa_source *s) {
     struct userdata *u = s->userdata;
 
     if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", strerror(errno));
+        pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
         s->set_hw_volume = NULL;
         return -1;
     }
@@ -279,11 +350,12 @@ int pa__init(pa_core *c, pa_module*m) {
     int playback = 1, record = 1;
     pa_modargs *ma = NULL;
     char hwdesc[64];
-    
+    pa_channel_map map;
+
     assert(c);
     assert(m);
 
-    m->userdata = u = pa_xmalloc0(sizeof(struct userdata));
+    m->userdata = u = pa_xnew0(struct userdata, 1);
     u->module = m;
     u->fd = -1;
     u->core = c;
@@ -305,27 +377,22 @@ int pa__init(pa_core *c, pa_module*m) {
 
     mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
 
-    nfrags = 12;
-    frag_size = 1024;
+    nfrags = DEFAULT_NFRAGS;
+    frag_size = DEFAULT_FRAGSIZE;
     if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
         pa_log(__FILE__": failed to parse fragments arguments");
         goto fail;
     }
 
     u->sample_spec = c->default_sample_spec;
-    if (pa_modargs_get_sample_spec(ma, &u->sample_spec) < 0) {
-        pa_log(__FILE__": failed to parse sample specification");
+    if (pa_modargs_get_sample_spec_and_channel_map(ma, &u->sample_spec, &map, PA_CHANNEL_MAP_OSS) < 0) {
+        pa_log(__FILE__": failed to parse sample specification or channel map");
         goto fail;
     }
 
     if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
         goto fail;
 
-    if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
-        pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
-    else
-        hwdesc[0] = 0;
-
     if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
         pa_log(__FILE__": OSS device not mmap capable.");
         goto fail;
@@ -333,6 +400,11 @@ int pa__init(pa_core *c, pa_module*m) {
 
     pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
 
+    if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
+        pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
+    else
+        hwdesc[0] = 0;
+
     if (nfrags >= 2 && frag_size >= 1)
         if (pa_oss_set_fragments(u->fd, nfrags, frag_size) < 0)
             goto fail;
@@ -342,7 +414,7 @@ int pa__init(pa_core *c, pa_module*m) {
 
     if (mode != O_WRONLY) {
         if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
-            pa_log(__FILE__": SNDCTL_DSP_GETISPACE: %s", strerror(errno));
+            pa_log(__FILE__": SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
             goto fail;
         }
 
@@ -354,16 +426,18 @@ int pa__init(pa_core *c, pa_module*m) {
                 pa_log(__FILE__": mmap failed for input. Changing to O_WRONLY mode.");
                 mode = O_WRONLY;
             } else {
-                pa_log(__FILE__": mmap(): %s", strerror(errno));
+                pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
                 goto fail;
             }
         } else {
         
-            u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec, NULL);
-            assert(u->source);
+            if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec, &map)))
+                goto fail;
+            
+            u->source->userdata = u;
+            u->source->get_latency = source_get_latency_cb;
             u->source->get_hw_volume = source_get_hw_volume;
             u->source->set_hw_volume = source_set_hw_volume;
-            u->source->userdata = u;
             pa_source_set_owner(u->source, m);
             u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'%s%s%s",
                                                        p,
@@ -371,7 +445,7 @@ int pa__init(pa_core *c, pa_module*m) {
                                                        hwdesc[0] ? hwdesc : "",
                                                        hwdesc[0] ? ")" : "");
             
-            u->in_memblocks = pa_xmalloc0(sizeof(pa_memblock *)*u->in_fragments);
+            u->in_memblocks = pa_xnew0(pa_memblock*, u->in_fragments);
             
             enable_bits |= PCM_ENABLE_INPUT;
         }
@@ -379,7 +453,7 @@ int pa__init(pa_core *c, pa_module*m) {
 
     if (mode != O_RDONLY) {
         if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
-            pa_log(__FILE__": SNDCTL_DSP_GETOSPACE: %s", strerror(errno));
+            pa_log(__FILE__": SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
             goto fail;
         }
         
@@ -391,14 +465,15 @@ int pa__init(pa_core *c, pa_module*m) {
                 pa_log(__FILE__": mmap filed for output. Changing to O_RDONLY mode.");
                 mode = O_RDONLY;
             } else {
-                pa_log(__FILE__": mmap(): %s", strerror(errno));
+                pa_log(__FILE__": mmap(): %s", pa_cstrerror(errno));
                 goto fail;
             }
         } else {
             pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
             
-            u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec, NULL);
-            assert(u->sink);
+            if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec, &map)))
+                goto fail;
+
             u->sink->get_latency = sink_get_latency_cb;
             u->sink->get_hw_volume = sink_get_hw_volume;
             u->sink->set_hw_volume = sink_set_hw_volume;
@@ -418,12 +493,12 @@ int pa__init(pa_core *c, pa_module*m) {
 
     zero = 0;
     if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
-        pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno));
+        pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
         goto fail;
     }
     
     if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
-        pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", strerror(errno));
+        pa_log(__FILE__": SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
         goto fail;
     }
         
@@ -453,7 +528,9 @@ fail:
 
 void pa__done(pa_core *c, pa_module*m) {
     struct userdata *u;
-    assert(c && m);
+    
+    assert(c);
+    assert(m);
 
     if (!(u = m->userdata))
         return;