make module-alsa-card move streams between the old and new sink/source, allowing...
[profile/ivi/pulseaudio-panda.git] / src / modules / alsa / module-alsa-card.c
index 2cc8a15..d8a37fe 100644 (file)
 #include <pulse/xmalloc.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/modargs.h>
+#include <pulsecore/queue.h>
 
 #include "alsa-util.h"
+#include "alsa-sink.h"
+#include "alsa-source.h"
 #include "module-alsa-card-symdef.h"
 
 PA_MODULE_AUTHOR("Lennart Poettering");
@@ -35,7 +38,10 @@ PA_MODULE_DESCRIPTION("ALSA Card");
 PA_MODULE_VERSION(PACKAGE_VERSION);
 PA_MODULE_LOAD_ONCE(FALSE);
 PA_MODULE_USAGE(
-        "name=<name for the sink/source> "
+        "name=<name for the card/sink/source, to be prefixed> "
+        "card_name=<name for card> "
+        "sink_name=<name for sink> "
+        "source_name=<name for source> "
         "device_id=<ALSA card index> "
         "format=<sample format> "
         "rate=<sample rate> "
@@ -48,19 +54,20 @@ PA_MODULE_USAGE(
         "profile=<profile name>");
 
 static const char* const valid_modargs[] = {
+    "name",
+    "card_name",
     "sink_name",
-    "device",
+    "source_name",
     "device_id",
     "format",
     "rate",
-    "channels",
-    "channel_map",
     "fragments",
     "fragment_size",
     "mmap",
     "tsched",
     "tsched_buffer_size",
     "tsched_buffer_watermark",
+    "profile",
     NULL
 };
 
@@ -73,10 +80,14 @@ struct userdata {
     char *device_id;
 
     pa_card *card;
+    pa_sink *sink;
+    pa_source *source;
+
+    pa_modargs *modargs;
 };
 
 struct profile_data {
-    const pa_alsa_profile_info *sink, *source;
+    const pa_alsa_profile_info *sink_profile, *source_profile;
 };
 
 static void enumerate_cb(
@@ -90,14 +101,14 @@ static void enumerate_cb(
     struct profile_data *d;
 
     if (sink && source) {
-        n = pa_sprintf_malloc("%s+%s", sink->name, source->name);
+        n = pa_sprintf_malloc("output-%s+input-%s", sink->name, source->name);
         t = pa_sprintf_malloc("Output %s + Input %s", sink->description, source->description);
     } else if (sink) {
-        n = pa_xstrdup(sink->name);
+        n = pa_sprintf_malloc("output-%s", sink->name);
         t = pa_sprintf_malloc("Output %s", sink->description);
     } else {
         pa_assert(source);
-        n = pa_xstrdup(source->name);
+        n = pa_sprintf_malloc("input-%s", source->name);
         t = pa_sprintf_malloc("Input %s", source->description);
     }
 
@@ -108,6 +119,7 @@ static void enumerate_cb(
     pa_xfree(t);
     pa_xfree(n);
 
+    p->priority = (sink ? sink->priority : 0)*100 + (source ? source->priority : 0);
     p->n_sinks = !!sink;
     p->n_sources = !!source;
 
@@ -118,12 +130,124 @@ static void enumerate_cb(
 
     d = PA_CARD_PROFILE_DATA(p);
 
-    d->sink = sink;
-    d->source = source;
+    d->sink_profile = sink;
+    d->source_profile = source;
 
     pa_hashmap_put(profiles, p->name, p);
 }
 
+static void add_disabled_profile(pa_hashmap *profiles) {
+    pa_card_profile *p;
+    struct profile_data *d;
+
+    p = pa_card_profile_new("off", "Off", sizeof(struct profile_data));
+
+    d = PA_CARD_PROFILE_DATA(p);
+    d->sink_profile = d->source_profile = NULL;
+
+    pa_hashmap_put(profiles, p->name, p);
+}
+
+static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
+    struct userdata *u;
+    struct profile_data *nd, *od;
+
+    pa_assert(c);
+    pa_assert(new_profile);
+    pa_assert_se(u = c->userdata);
+
+    nd = PA_CARD_PROFILE_DATA(new_profile);
+    od = PA_CARD_PROFILE_DATA(c->active_profile);
+
+    if (od->sink_profile != nd->sink_profile) {
+        pa_queue *inputs = NULL;
+
+        if (u->sink) {
+            if (nd->sink_profile)
+                inputs = pa_sink_move_all_start(u->sink);
+
+            pa_alsa_sink_free(u->sink);
+            u->sink = NULL;
+        }
+
+        if (nd->sink_profile) {
+            u->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, nd->sink_profile);
+
+            if (inputs) {
+                if (u->sink)
+                    pa_sink_move_all_finish(u->sink, inputs);
+                else
+                    pa_sink_move_all_fail(inputs);
+            }
+        }
+    }
+
+    if (od->source_profile != nd->source_profile) {
+        pa_queue *outputs = NULL;
+
+        if (u->source) {
+            if (nd->source_profile)
+                outputs = pa_source_move_all_start(u->source);
+
+            pa_alsa_source_free(u->source);
+            u->source = NULL;
+        }
+
+        if (nd->source_profile) {
+            u->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, nd->source_profile);
+
+            if (outputs) {
+                if (u->source)
+                    pa_source_move_all_finish(u->source, outputs);
+                else
+                    pa_source_move_all_fail(outputs);
+            }
+        }
+    }
+
+    return 0;
+}
+
+static void init_profile(struct userdata *u) {
+    struct profile_data *d;
+
+    pa_assert(u);
+
+    d = PA_CARD_PROFILE_DATA(u->card->active_profile);
+
+    if (d->sink_profile)
+        u->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, d->sink_profile);
+
+    if (d->source_profile)
+        u->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, d->source_profile);
+}
+
+static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) {
+    char *t;
+    const char *n;
+
+    pa_assert(data);
+    pa_assert(ma);
+    pa_assert(device_id);
+
+    if ((n = pa_modargs_get_value(ma, "card_name", NULL))) {
+        pa_card_new_data_set_name(data, n);
+        data->namereg_fail = TRUE;
+        return;
+    }
+
+    if ((n = pa_modargs_get_value(ma, "name", NULL)))
+        data->namereg_fail = TRUE;
+    else {
+        n = device_id;
+        data->namereg_fail = FALSE;
+    }
+
+    t = pa_sprintf_malloc("alsa_card.%s", n);
+    pa_card_new_data_set_name(data, t);
+    pa_xfree(t);
+}
+
 int pa__init(pa_module*m) {
     pa_card_new_data data;
     pa_modargs *ma;
@@ -131,6 +255,7 @@ int pa__init(pa_module*m) {
     struct userdata *u;
 
     pa_alsa_redirect_errors_inc();
+    snd_config_update_free_global();
 
     pa_assert(m);
 
@@ -139,10 +264,14 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    m->userdata = u = pa_xnew0(struct userdata, 1);
+    m->userdata = u = pa_xnew(struct userdata, 1);
     u->core = m->core;
     u->module = m;
     u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID));
+    u->card = NULL;
+    u->sink = NULL;
+    u->source = NULL;
+    u->modargs = ma;
 
     if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) {
         pa_log("Card '%s' doesn't exist: %s", u->device_id, snd_strerror(alsa_card_index));
@@ -154,7 +283,7 @@ int pa__init(pa_module*m) {
     data.module = m;
     pa_alsa_init_proplist_card(data.proplist, alsa_card_index);
     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id);
-    pa_card_new_data_set_name(&data, pa_modargs_get_value(ma, "name", u->device_id));
+    set_card_name(&data, ma, u->device_id);
 
     data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, data.profiles) < 0) {
@@ -162,20 +291,44 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    if (pa_hashmap_isempty(data.profiles)) {
+        pa_log("Failed to find a working profile.");
+        pa_card_new_data_done(&data);
+        goto fail;
+    }
+
+    add_disabled_profile(data.profiles);
+
     u->card = pa_card_new(m->core, &data);
     pa_card_new_data_done(&data);
 
+    if (!u->card)
+        goto fail;
+
+    u->card->userdata = u;
+    u->card->set_profile = card_set_profile;
+
+    init_profile(u);
+
     return 0;
 
 fail:
 
-    if (ma)
-        pa_modargs_free(ma);
-
     pa__done(m);
     return -1;
 }
 
+int pa__get_n_used(pa_module *m) {
+    struct userdata *u;
+
+    pa_assert(m);
+    pa_assert_se(u = m->userdata);
+
+    return
+        (u->sink ? pa_sink_linked_by(u->sink) : 0) +
+        (u->source ? pa_source_linked_by(u->source) : 0);
+}
+
 void pa__done(pa_module*m) {
     struct userdata *u;
 
@@ -184,9 +337,18 @@ void pa__done(pa_module*m) {
     if (!(u = m->userdata))
         goto finish;
 
+    if (u->sink)
+        pa_alsa_sink_free(u->sink);
+
+    if (u->source)
+        pa_alsa_source_free(u->source);
+
     if (u->card)
         pa_card_free(u->card);
 
+    if (u->modargs)
+        pa_modargs_free(u->modargs);
+
     pa_xfree(u->device_id);
     pa_xfree(u);