]> code.delx.au - pulseaudio/blobdiff - src/modules/module-alsa-source.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[pulseaudio] / src / modules / module-alsa-source.c
index e2dbc8bf182ba7770bea4f0cef31c2c493baa1f1..414efda882abd8922437d5483e72c9a958c83a9f 100644 (file)
 
 #include <asoundlib.h>
 
+#include <polyp/xmalloc.h>
+
+#include <polypcore/core-error.h>
 #include <polypcore/core.h>
 #include <polypcore/module.h>
 #include <polypcore/memchunk.h>
 #include <polypcore/sink.h>
 #include <polypcore/modargs.h>
-#include <polypcore/util.h>
+#include <polypcore/core-util.h>
 #include <polypcore/sample-util.h>
-#include <polypcore/xmalloc.h>
 #include <polypcore/log.h>
 
 #include "alsa-util.h"
 PA_MODULE_AUTHOR("Lennart Poettering")
 PA_MODULE_DESCRIPTION("ALSA Source")
 PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("source_name=<name for the source> device=<ALSA device> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
+PA_MODULE_USAGE(
+        "source_name=<name for the source> "
+        "device=<ALSA device> "
+        "format=<sample format> "
+        "channels=<number of channels> "
+        "rate=<sample rate> "
+        "fragments=<number of fragments> "
+        "fragment_size=<fragment size> "
+        "channel_map=<channel map>")
 
 struct userdata {
     snd_pcm_t *pcm_handle;
     snd_mixer_t *mixer_handle;
     snd_mixer_elem_t *mixer_elem;
     pa_source *source;
-    pa_io_event **io_events;
-    unsigned n_io_events;
+    struct pa_alsa_fdlist *pcm_fdl;
+    struct pa_alsa_fdlist *mixer_fdl;
     long hw_volume_max, hw_volume_min;
 
     size_t frame_size, fragment_size;
@@ -74,11 +84,12 @@ static const char* const valid_modargs[] = {
     "format",
     "fragments",
     "fragment_size",
+    "channel_map",
     NULL
 };
 
 #define DEFAULT_SOURCE_NAME "alsa_input"
-#define DEFAULT_DEVICE "hw:0,0"
+#define DEFAULT_DEVICE "default"
 
 static void update_usage(struct userdata *u) {
    pa_module_set_used(u->module,
@@ -109,7 +120,11 @@ static void do_read(struct userdata *u) {
             u->memchunk.index = 0;
         }
             
-        assert(u->memchunk.memblock && u->memchunk.memblock->data && u->memchunk.length && u->memchunk.memblock->length && (u->memchunk.length % u->frame_size) == 0);
+        assert(u->memchunk.memblock);
+        assert(u->memchunk.length);
+        assert(u->memchunk.memblock->data);
+        assert(u->memchunk.memblock->length);
+        assert(u->memchunk.length % u->frame_size == 0);
 
         if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
             if (frames == -EAGAIN)
@@ -120,7 +135,7 @@ static void do_read(struct userdata *u) {
                 continue;
             }
 
-            pa_log(__FILE__": snd_pcm_readi() failed: %s", strerror(-frames));
+            pa_log(__FILE__": snd_pcm_readi() failed: %s", pa_cstrerror(-frames));
             return;
         }
 
@@ -144,9 +159,9 @@ static void do_read(struct userdata *u) {
     }
 }
 
-static void io_callback(pa_mainloop_api*a, pa_io_event *e, PA_GCC_UNUSED int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
+static void fdl_callback(void *userdata) {
     struct userdata *u = userdata;
-    assert(u && a && e);
+    assert(u);
 
     if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
         xrun_recovery(u);
@@ -154,6 +169,27 @@ static void io_callback(pa_mainloop_api*a, pa_io_event *e, PA_GCC_UNUSED int fd,
     do_read(u);
 }
 
+static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
+    struct userdata *u = snd_mixer_elem_get_callback_private(elem);
+
+    assert(u && u->mixer_handle);
+
+    if (mask == SND_CTL_EVENT_MASK_REMOVE)
+        return 0;
+
+    if (mask & SND_CTL_EVENT_MASK_VALUE) {
+        if (u->source->get_hw_volume)
+            u->source->get_hw_volume(u->source);
+        if (u->source->get_hw_mute)
+            u->source->get_hw_mute(u->source);
+        pa_subscription_post(u->source->core,
+            PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
+            u->source->index);
+    }
+
+    return 0;
+}
+
 static pa_usec_t source_get_latency_cb(pa_source *s) {
     struct userdata *u = s->userdata;
     snd_pcm_sframes_t frames;
@@ -172,25 +208,18 @@ static int source_get_hw_volume_cb(pa_source *s) {
     struct userdata *u = s->userdata;
     long vol;
     int err;
+    int i;
 
     assert(u && u->mixer_elem);
 
-    if (snd_mixer_selem_has_capture_volume_joined(u->mixer_elem)) {
-        err = snd_mixer_selem_get_capture_volume(u->mixer_elem, 0, &vol);
+    for (i = 0;i < s->hw_volume.channels;i++) {
+        assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
+
+        err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol);
         if (err < 0)
             goto fail;
-        pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
-            (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min));
-    } else {
-        int i;
-
-        for (i = 0;i < s->hw_volume.channels;i++) {
-            err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol);
-            if (err < 0)
-                goto fail;
-            s->hw_volume.values[i] =
-                (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
-        }
+        s->hw_volume.values[i] =
+            (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
     }
 
     return 0;
@@ -206,25 +235,23 @@ static int source_set_hw_volume_cb(pa_source *s) {
     struct userdata *u = s->userdata;
     int err;
     pa_volume_t vol;
+    int i;
 
     assert(u && u->mixer_elem);
 
-    if (snd_mixer_selem_has_capture_volume_joined(u->mixer_elem)) {
-        vol = pa_cvolume_avg(&s->hw_volume) * (u->hw_volume_max - u->hw_volume_min) /
+    for (i = 0;i < s->hw_volume.channels;i++) {
+        assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
+
+        vol = s->hw_volume.values[i];
+
+        if (vol > PA_VOLUME_NORM)
+            vol = PA_VOLUME_NORM;
+
+        vol = vol * (u->hw_volume_max - u->hw_volume_min) /
             PA_VOLUME_NORM + u->hw_volume_min;
-        err = snd_mixer_selem_set_capture_volume_all(u->mixer_elem, vol);
+        err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol);
         if (err < 0)
             goto fail;
-    } else {
-        int i;
-
-        for (i = 0;i < s->hw_volume.channels;i++) {
-            vol = s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min) /
-                PA_VOLUME_NORM + u->hw_volume_min;
-            err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol);
-            if (err < 0)
-                goto fail;
-        }
     }
 
     return 0;
@@ -278,9 +305,11 @@ int pa__init(pa_core *c, pa_module*m) {
     struct userdata *u = NULL;
     const char *dev;
     pa_sample_spec ss;
+    pa_channel_map map;
     unsigned periods, fragsize;
     snd_pcm_uframes_t period_size;
     size_t frame_size;
+    snd_pcm_info_t *pcm_info = NULL;
     int err;
     
     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
@@ -289,19 +318,22 @@ int pa__init(pa_core *c, pa_module*m) {
     }
 
     ss = c->default_sample_spec;
-    if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
+    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
         pa_log(__FILE__": failed to parse sample specification");
         goto fail;
     }
+
     frame_size = pa_frame_size(&ss);
-    
+
+    /* Fix latency to 100ms */
     periods = 12;
-    fragsize = 1024;
+    fragsize = pa_bytes_per_second(&ss)/128;
+    
     if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
         pa_log(__FILE__": failed to parse buffer metrics");
         goto fail;
     }
-    period_size = fragsize;
+    period_size = fragsize/frame_size;
     
     u = pa_xmalloc0(sizeof(struct userdata));
     m->userdata = u;
@@ -313,6 +345,12 @@ int pa__init(pa_core *c, pa_module*m) {
         goto fail;
     }
 
+    if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
+        (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
+        pa_log(__FILE__": Error fetching PCM info: %s", snd_strerror(err));
+        goto fail;
+    }
+
     if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
         pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
         goto fail;
@@ -329,7 +367,7 @@ int pa__init(pa_core *c, pa_module*m) {
         u->mixer_handle = NULL;
     }
 
-    u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, NULL);
+    u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
     assert(u->source);
 
     u->source->userdata = u;
@@ -337,10 +375,19 @@ int pa__init(pa_core *c, pa_module*m) {
     if (u->mixer_handle) {
         assert(u->mixer_elem);
         if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
-            u->source->get_hw_volume = source_get_hw_volume_cb;
-            u->source->set_hw_volume = source_set_hw_volume_cb;
-            snd_mixer_selem_get_capture_volume_range(
-                u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
+            int i;
+
+            for (i = 0;i < ss.channels;i++) {
+                if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
+                    break;
+            }
+
+            if (i == ss.channels) {
+                u->source->get_hw_volume = source_get_hw_volume_cb;
+                u->source->set_hw_volume = source_set_hw_volume_cb;
+                snd_mixer_selem_get_capture_volume_range(
+                    u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
+            }
         }
         if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
             u->source->get_hw_mute = source_get_hw_mute_cb;
@@ -348,17 +395,30 @@ int pa__init(pa_core *c, pa_module*m) {
         }
     }
     pa_source_set_owner(u->source, m);
-    u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
+    u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev, snd_pcm_info_get_name(pcm_info));
 
-    if (pa_create_io_events(u->pcm_handle, c->mainloop, &u->io_events, &u->n_io_events, io_callback, u) < 0) {
-        pa_log(__FILE__": failed to obtain file descriptors");
+    u->pcm_fdl = pa_alsa_fdlist_new();
+    assert(u->pcm_fdl);
+    if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
+        pa_log(__FILE__": failed to initialise file descriptor monitoring");
         goto fail;
     }
 
+    if (u->mixer_handle) {
+        u->mixer_fdl = pa_alsa_fdlist_new();
+        assert(u->mixer_fdl);
+        if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
+            pa_log(__FILE__": failed to initialise file descriptor monitoring");
+            goto fail;
+        }
+        snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
+        snd_mixer_elem_set_callback_private(u->mixer_elem, u);
+    }
+
     u->frame_size = frame_size;
-    u->fragment_size = period_size;
+    u->fragment_size = period_size * frame_size;
 
-    pa_log(__FILE__": using %u fragments of size %u bytes.", periods, u->fragment_size);
+    pa_log_info(__FILE__": using %u fragments of size %lu bytes.", periods, (long unsigned) u->fragment_size);
 
     u->memchunk.memblock = NULL;
     u->memchunk.index = u->memchunk.length = 0;
@@ -367,15 +427,18 @@ int pa__init(pa_core *c, pa_module*m) {
     
     ret = 0;
 
-finish:
-     if (ma)
-         pa_modargs_free(ma);
-
     /* Get initial mixer settings */
     if (u->source->get_hw_volume)
         u->source->get_hw_volume(u->source);
     if (u->source->get_hw_mute)
         u->source->get_hw_mute(u->source);
+
+finish:
+     if (ma)
+         pa_modargs_free(ma);
+
+    if (pcm_info)
+        snd_pcm_info_free(pcm_info);
     
     return ret;
 
@@ -399,8 +462,10 @@ void pa__done(pa_core *c, pa_module*m) {
         pa_source_unref(u->source);
     }
     
-    if (u->io_events)
-        pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
+    if (u->pcm_fdl)
+        pa_alsa_fdlist_free(u->pcm_fdl);
+    if (u->mixer_fdl)
+        pa_alsa_fdlist_free(u->mixer_fdl);
 
     if (u->mixer_handle)
         snd_mixer_close(u->mixer_handle);