]> code.delx.au - pulseaudio/blobdiff - src/modules/module-intended-roles.c
alsa: Allow channel count probe on open by mapping
[pulseaudio] / src / modules / module-intended-roles.c
index 38b8f4842f47576301e8f9e6fb03ad8fec53a777..d1e9b818e8d26e6c8ad48305f491d32e2f419713 100644 (file)
 #include <config.h>
 #endif
 
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-
 #include <pulse/xmalloc.h>
-#include <pulse/volume.h>
-#include <pulse/timeval.h>
-#include <pulse/util.h>
 
-#include <pulsecore/core-error.h>
 #include <pulsecore/module.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/modargs.h>
 #include <pulsecore/log.h>
-#include <pulsecore/core-subscribe.h>
 #include <pulsecore/sink-input.h>
 #include <pulsecore/source-output.h>
 #include <pulsecore/namereg.h>
-#include <pulsecore/protocol-native.h>
-#include <pulsecore/pstream.h>
-#include <pulsecore/pstream-util.h>
-#include <pulsecore/database.h>
 
 #include "module-intended-roles-symdef.h"
 
@@ -69,6 +52,7 @@ static const char* const valid_modargs[] = {
 struct userdata {
     pa_core *core;
     pa_module *module;
+
     pa_hook_slot
         *sink_input_new_hook_slot,
         *source_output_new_hook_slot,
@@ -82,24 +66,7 @@ struct userdata {
 };
 
 static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
-    const char *ir;
-    char *r;
-    const char *state = NULL;
-
-    if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES)))
-        return FALSE;
-
-    while ((r = pa_split_spaces(ir, &state))) {
-
-        if (pa_streq(role, r)) {
-            pa_xfree(r);
-            return TRUE;
-        }
-
-        pa_xfree(r);
-    }
-
-    return FALSE;
+    return pa_str_in_list_spaces(pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES), role);
 }
 
 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
@@ -128,21 +95,19 @@ static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_n
 
     /* Prefer the default sink over any other sink, just in case... */
     if ((def = pa_namereg_get_default_sink(c)))
-        if (role_match(def->proplist, role)) {
-            new_data->sink = def;
-            new_data->save_sink = FALSE;
+        if (role_match(def->proplist, role) && pa_sink_input_new_data_set_sink(new_data, def, FALSE))
             return PA_HOOK_OK;
-        }
 
+    /* @todo: favour the highest priority device, not the first one we find? */
     PA_IDXSET_FOREACH(s, c->sinks, idx) {
         if (s == def)
             continue;
 
-        if (role_match(s->proplist, role)) {
-            new_data->sink = s;
-            new_data->save_sink = FALSE;
+        if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+            continue;
+
+        if (role_match(s->proplist, role) && pa_sink_input_new_data_set_sink(new_data, s, FALSE))
             return PA_HOOK_OK;
-        }
     }
 
     return PA_HOOK_OK;
@@ -175,18 +140,23 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou
     /* Prefer the default source over any other source, just in case... */
     if ((def = pa_namereg_get_default_source(c)))
         if (role_match(def->proplist, role)) {
-            new_data->source = def;
-            new_data->save_source = FALSE;
+            pa_source_output_new_data_set_source(new_data, def, FALSE);
             return PA_HOOK_OK;
         }
 
     PA_IDXSET_FOREACH(s, c->sources, idx) {
+        if (s->monitor_of)
+            continue;
+
         if (s == def)
             continue;
 
+        if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
+            continue;
+
+        /* @todo: favour the highest priority device, not the first one we find? */
         if (role_match(s->proplist, role)) {
-            new_data->source = s;
-            new_data->save_source = FALSE;
+            pa_source_output_new_data_set_source(new_data, s, FALSE);
             return PA_HOOK_OK;
         }
     }
@@ -212,6 +182,17 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct
         if (si->save_sink)
             continue;
 
+        /* Skip this if it is already in the process of being moved
+         * anyway */
+        if (!si->sink)
+            continue;
+
+        /* It might happen that a stream and a sink are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+            continue;
+
         if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
             continue;
 
@@ -236,6 +217,9 @@ static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source,
     pa_assert(u);
     pa_assert(u->on_hotplug);
 
+    if (source->monitor_of)
+        return PA_HOOK_OK;
+
     PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
         const char *role;
 
@@ -248,6 +232,17 @@ static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source,
         if (so->direct_on_input)
             continue;
 
+        /* Skip this if it is already in the process of being moved
+         * anyway */
+        if (!so->source)
+            continue;
+
+        /* It might happen that a stream and a source are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+            continue;
+
         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
             continue;
 
@@ -286,24 +281,29 @@ static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, str
         uint32_t jdx;
         pa_sink *d;
 
+        if (!si->sink)
+            continue;
+
         if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
             continue;
 
         /* Would the default sink fit? If so, let's use it */
-        if (def != sink && role_match(def->proplist, role)) {
-            pa_sink_input_move_to(si, def, FALSE);
-            continue;
-        }
+        if (def != sink && role_match(def->proplist, role))
+            if (pa_sink_input_move_to(si, def, FALSE) >= 0)
+                continue;
 
         /* Try to find some other fitting sink */
+        /* @todo: favour the highest priority device, not the first one we find? */
         PA_IDXSET_FOREACH(d, c->sinks, jdx) {
             if (d == def || d == sink)
                 continue;
 
-            if (role_match(d->proplist, role)) {
-                pa_sink_input_move_to(si, d, FALSE);
-                break;
-            }
+            if (!PA_SINK_IS_LINKED(pa_sink_get_state(d)))
+                continue;
+
+            if (role_match(d->proplist, role))
+                if (pa_sink_input_move_to(si, d, FALSE) >= 0)
+                    break;
         }
     }
 
@@ -336,6 +336,9 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
         if (so->direct_on_input)
             continue;
 
+        if (!so->source)
+            continue;
+
         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
             continue;
 
@@ -346,11 +349,16 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
         }
 
         /* Try to find some other fitting source */
+        /* @todo: favour the highest priority device, not the first one we find? */
         PA_IDXSET_FOREACH(d, c->sources, jdx) {
             if (d == def || d == source)
                 continue;
 
-            if (role_match(d->proplist, role) && !source->monitor_of == !d->monitor_of) {
+            if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
+                continue;
+
+            /* If moving from a monitor, move to another monitor */
+            if (!source->monitor_of == !d->monitor_of && role_match(d->proplist, role)) {
                 pa_source_output_move_to(so, d, FALSE);
                 break;
             }
@@ -396,8 +404,8 @@ int pa__init(pa_module*m) {
 
     if (on_rescue) {
         /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
-        u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_unlink_hook_callback, NULL);
-        u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) source_unlink_hook_callback, NULL);
+        u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+        u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) source_unlink_hook_callback, u);
     }
 
     pa_modargs_free(ma);
@@ -409,7 +417,7 @@ fail:
     if (ma)
         pa_modargs_free(ma);
 
-    return  -1;
+    return -1;
 }
 
 void pa__done(pa_module*m) {