]> code.delx.au - pulseaudio/blobdiff - src/modules/module-oss.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[pulseaudio] / src / modules / module-oss.c
index 34743a1d4efc983724a8419876a7e16fc911b35b..887246733f3678da97ee0b68a0fc12525d33a9ad 100644 (file)
 #include <unistd.h>
 #include <limits.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")
 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;
@@ -79,6 +92,7 @@ static const char* const valid_modargs[] = {
     "format",
     "rate",
     "channels",
+    "channel_map",
     NULL
 };
 
@@ -133,7 +147,7 @@ static void do_write(struct userdata *u) {
         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));
+            pa_log(__FILE__": write() failed: %s", pa_cstrerror(errno));
             break;
         }
         
@@ -186,7 +200,7 @@ static void do_read(struct userdata *u) {
         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));
+                pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno));
             break;
         }
         
@@ -221,7 +235,7 @@ static pa_usec_t sink_get_latency_cb(pa_sink *s) {
     assert(s && u && u->sink);
 
     if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
-        pa_log_info(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY: %s\n", strerror(errno));
+        pa_log_info(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
         s->get_latency = NULL;
         return 0;
     }
@@ -256,8 +270,8 @@ static pa_usec_t source_get_latency_cb(pa_source *s) {
 static int sink_get_hw_volume(pa_sink *s) {
     struct userdata *u = s->userdata;
 
-    if (pa_oss_get_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info(__FILE__": device doesn't support reading mixer settings: %s\n", strerror(errno));
+    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", pa_cstrerror(errno));
         s->get_hw_volume = NULL;
         return -1;
     }
@@ -268,8 +282,32 @@ static int sink_get_hw_volume(pa_sink *s) {
 static int sink_set_hw_volume(pa_sink *s) {
     struct userdata *u = s->userdata;
 
-    if (pa_oss_set_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info(__FILE__": device doesn't support writing mixer settings: %s\n", strerror(errno));
+    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", pa_cstrerror(errno));
+        s->set_hw_volume = NULL;
+        return -1;
+    }
+
+    return 0;
+}
+
+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", pa_cstrerror(errno));
+        s->get_hw_volume = NULL;
+        return -1;
+    }
+
+    return 0;
+}
+
+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", pa_cstrerror(errno));
         s->set_hw_volume = NULL;
         return -1;
     }
@@ -286,43 +324,54 @@ int pa__init(pa_core *c, pa_module*m) {
     int mode;
     int record = 1, playback = 1;
     pa_sample_spec ss;
+    pa_channel_map map;
     pa_modargs *ma = NULL;
-    assert(c && m);
+    char hwdesc[64];
+    
+    assert(c);
+    assert(m);
 
     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
-        pa_log(__FILE__": failed to parse module arguments.\n");
+        pa_log(__FILE__": failed to parse module arguments.");
         goto fail;
     }
     
     if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
-        pa_log(__FILE__": record= and playback= expect numeric argument.\n");
+        pa_log(__FILE__": record= and playback= expect numeric argument.");
         goto fail;
     }
 
     if (!playback && !record) {
-        pa_log(__FILE__": neither playback nor record enabled for device.\n");
+        pa_log(__FILE__": neither playback nor record enabled for device.");
         goto fail;
     }
 
     mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
+
+    ss = c->default_sample_spec;
+    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
+        pa_log(__FILE__": failed to parse sample specification or channel map");
+        goto fail;
+    }
     
+    /* Fix latency to 100ms */
     nfrags = 12;
-    frag_size = 1024;
+    frag_size = pa_bytes_per_second(&ss)/128;
+    
     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\n");
+        pa_log(__FILE__": failed to parse fragments arguments");
         goto fail;
     }
 
-    ss = c->default_sample_spec;
-    if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
-        pa_log(__FILE__": failed to parse sample specification\n");
-        goto fail;
-    }
-    
     if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
         goto fail;
 
-    pa_log_info(__FILE__": device opened in %s mode.\n", 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;
+    
+    pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
 
     if (nfrags >= 2 && frag_size >= 1)
         if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)   
@@ -332,7 +381,7 @@ int pa__init(pa_core *c, pa_module*m) {
         goto fail;
 
     if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
-        pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno));
+        pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
         goto fail;
     }
     assert(frag_size);
@@ -343,37 +392,49 @@ int pa__init(pa_core *c, pa_module*m) {
     u->use_getospace = u->use_getispace = 0;
     
     if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
-        pa_log_info(__FILE__": input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
+        pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
         in_frag_size = info.fragsize;
         u->use_getispace = 1;
     }
 
     if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
-        pa_log_info(__FILE__": output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
+        pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
         out_frag_size = info.fragsize;
         u->use_getospace = 1;
     }
 
     if (mode != O_WRONLY) {
-        u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, NULL);
-        assert(u->source);
+        if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map)))
+            goto fail;
+
         u->source->userdata = u;
         u->source->notify = source_notify_cb;
         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;
         pa_source_set_owner(u->source, m);
-        u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
+        u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
+                                                   p,
+                                                   hwdesc[0] ? " (" : "",
+                                                   hwdesc[0] ? hwdesc : "",
+                                                   hwdesc[0] ? ")" : "");
     } else
         u->source = NULL;
 
     if (mode != O_RDONLY) {
-        u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL);
-        assert(u->sink);
+        if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &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;
         u->sink->userdata = u;
         pa_sink_set_owner(u->sink, m);
-        u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
+        u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
+                                                 p,
+                                                 hwdesc[0] ? " (" : "",
+                                                 hwdesc[0] ? hwdesc : "",
+                                                 hwdesc[0] ? ")" : "");
     } else
         u->sink = NULL;
 
@@ -405,11 +466,14 @@ int pa__init(pa_core *c, pa_module*m) {
      * Without this snippet, poll will never register the fd as ready.
      */
     if (u->source) {
-        char buf[u->sample_size];
+        char *buf = pa_xnew(char, u->sample_size);
         read(u->fd, buf, u->sample_size);
+        pa_xfree(buf);
     }
 
     /* Read mixer settings */
+    if (u->source)
+        source_get_hw_volume(u->source);
     if (u->sink)
         sink_get_hw_volume(u->sink);
 
@@ -427,7 +491,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;