[pulseaudio-discuss] [PATCH 16/30] sink, source: Pass the port object instead of the port name to pa_sink/source_set_port()

Tanu Kaskinen tanu.kaskinen at linux.intel.com
Thu Jan 16 07:02:42 PST 2014


A similar change was made for pa_card_set_profile() in
ce304d62087b51ce592e9489381ba49750392f60.
---
 src/modules/dbus/iface-device-port.c          |  6 ++++++
 src/modules/dbus/iface-device-port.h          |  1 +
 src/modules/dbus/iface-device.c               |  4 ++--
 src/modules/module-switch-on-port-available.c |  8 ++++----
 src/pulsecore/cli-command.c                   | 16 ++++++++++++++--
 src/pulsecore/protocol-native.c               | 11 ++++++++---
 src/pulsecore/sink.c                          | 10 ++--------
 src/pulsecore/sink.h                          |  2 +-
 src/pulsecore/source.c                        | 10 ++--------
 src/pulsecore/source.h                        |  2 +-
 10 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/src/modules/dbus/iface-device-port.c b/src/modules/dbus/iface-device-port.c
index d403b6a..e8d87cc 100644
--- a/src/modules/dbus/iface-device-port.c
+++ b/src/modules/dbus/iface-device-port.c
@@ -188,3 +188,9 @@ const char *pa_dbusiface_device_port_get_name(pa_dbusiface_device_port *p) {
 
     return p->port->name;
 }
+
+pa_device_port *pa_dbusiface_device_port_get_port(pa_dbusiface_device_port *p) {
+    pa_assert(p);
+
+    return p->port;
+}
diff --git a/src/modules/dbus/iface-device-port.h b/src/modules/dbus/iface-device-port.h
index 0461e2f..1d300c2 100644
--- a/src/modules/dbus/iface-device-port.h
+++ b/src/modules/dbus/iface-device-port.h
@@ -46,5 +46,6 @@ void pa_dbusiface_device_port_free(pa_dbusiface_device_port *p);
 
 const char *pa_dbusiface_device_port_get_path(pa_dbusiface_device_port *p);
 const char *pa_dbusiface_device_port_get_name(pa_dbusiface_device_port *p);
+pa_device_port *pa_dbusiface_device_port_get_port(pa_dbusiface_device_port *p);
 
 #endif
diff --git a/src/modules/dbus/iface-device.c b/src/modules/dbus/iface-device.c
index 34b370b..c2e1fd7 100644
--- a/src/modules/dbus/iface-device.c
+++ b/src/modules/dbus/iface-device.c
@@ -753,13 +753,13 @@ static void handle_set_active_port(DBusConnection *conn, DBusMessage *msg, DBusM
     }
 
     if (d->type == PA_DEVICE_TYPE_SINK) {
-        if ((r = pa_sink_set_port(d->sink, pa_dbusiface_device_port_get_name(new_active), true)) < 0) {
+        if ((r = pa_sink_set_port(d->sink, pa_dbusiface_device_port_get_port(new_active), true)) < 0) {
             pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
                                "Internal error in PulseAudio: pa_sink_set_port() failed with error code %i.", r);
             return;
         }
     } else {
-        if ((r = pa_source_set_port(d->source, pa_dbusiface_device_port_get_name(new_active), true)) < 0) {
+        if ((r = pa_source_set_port(d->source, pa_dbusiface_device_port_get_port(new_active), true)) < 0) {
             pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED,
                                "Internal error in PulseAudio: pa_source_set_port() failed with error code %i.", r);
             return;
diff --git a/src/modules/module-switch-on-port-available.c b/src/modules/module-switch-on-port-available.c
index 92730fe..05d88e6 100644
--- a/src/modules/module-switch-on-port-available.c
+++ b/src/modules/module-switch-on-port-available.c
@@ -196,9 +196,9 @@ static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port
         }
 
         if (source)
-            pa_source_set_port(source, port->name, false);
+            pa_source_set_port(source, port, false);
         if (sink)
-            pa_sink_set_port(sink, port->name, false);
+            pa_sink_set_port(sink, port, false);
     }
 
     if (port->available == PA_AVAILABLE_NO) {
@@ -206,7 +206,7 @@ static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port
             pa_device_port *p2 = find_best_port(sink->ports);
 
             if (p2 && p2->available != PA_AVAILABLE_NO)
-                pa_sink_set_port(sink, p2->name, false);
+                pa_sink_set_port(sink, p2, false);
             else {
                 /* Maybe try to switch to another profile? */
             }
@@ -216,7 +216,7 @@ static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port
             pa_device_port *p2 = find_best_port(source->ports);
 
             if (p2 && p2->available != PA_AVAILABLE_NO)
-                pa_source_set_port(source, p2->name, false);
+                pa_source_set_port(source, p2, false);
             else {
                 /* Maybe try to switch to another profile? */
             }
diff --git a/src/pulsecore/cli-command.c b/src/pulsecore/cli-command.c
index d390a74..03512b7 100644
--- a/src/pulsecore/cli-command.c
+++ b/src/pulsecore/cli-command.c
@@ -1672,6 +1672,7 @@ static int pa_cli_command_card_profile(pa_core *c, pa_tokenizer *t, pa_strbuf *b
 static int pa_cli_command_sink_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail) {
     const char *n, *p;
     pa_sink *sink;
+    pa_device_port *port;
 
     pa_core_assert_ref(c);
     pa_assert(t);
@@ -1693,7 +1694,12 @@ static int pa_cli_command_sink_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf,
         return -1;
     }
 
-    if (pa_sink_set_port(sink, p, true) < 0) {
+    if (!(port = pa_hashmap_get(sink->ports, p))) {
+        pa_strbuf_printf(buf, "No such port: %s\n", p);
+        return -1;
+    }
+
+    if (pa_sink_set_port(sink, port, true) < 0) {
         pa_strbuf_printf(buf, "Failed to set sink port to '%s'.\n", p);
         return -1;
     }
@@ -1704,6 +1710,7 @@ static int pa_cli_command_sink_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf,
 static int pa_cli_command_source_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail) {
     const char *n, *p;
     pa_source *source;
+    pa_device_port *port;
 
     pa_core_assert_ref(c);
     pa_assert(t);
@@ -1725,7 +1732,12 @@ static int pa_cli_command_source_port(pa_core *c, pa_tokenizer *t, pa_strbuf *bu
         return -1;
     }
 
-    if (pa_source_set_port(source, p, true) < 0) {
+    if (!(port = pa_hashmap_get(source->ports, p))) {
+        pa_strbuf_printf(buf, "No such port: %s\n", p);
+        return -1;
+    }
+
+    if (pa_source_set_port(source, port, true) < 0) {
         pa_strbuf_printf(buf, "Failed to set source port to '%s'.\n", p);
         return -1;
     }
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index 88ebcdc..379c4b3 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -4747,7 +4747,8 @@ static void command_set_card_profile(pa_pdispatch *pd, uint32_t command, uint32_
 static void command_set_sink_or_source_port(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
     pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
     uint32_t idx = PA_INVALID_INDEX;
-    const char *name = NULL, *port = NULL;
+    const char *name = NULL, *port_name = NULL;
+    pa_device_port *port;
     int ret;
 
     pa_native_connection_assert_ref(c);
@@ -4755,7 +4756,7 @@ static void command_set_sink_or_source_port(pa_pdispatch *pd, uint32_t command,
 
     if (pa_tagstruct_getu32(t, &idx) < 0 ||
         pa_tagstruct_gets(t, &name) < 0 ||
-        pa_tagstruct_gets(t, &port) < 0 ||
+        pa_tagstruct_gets(t, &port_name) < 0 ||
         !pa_tagstruct_eof(t)) {
         protocol_error(c);
         return;
@@ -4764,7 +4765,7 @@ static void command_set_sink_or_source_port(pa_pdispatch *pd, uint32_t command,
     CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
     CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name_or_wildcard(name, command == PA_COMMAND_SET_SINK_PORT ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE), tag, PA_ERR_INVALID);
     CHECK_VALIDITY(c->pstream, (idx != PA_INVALID_INDEX) ^ (name != NULL), tag, PA_ERR_INVALID);
-    CHECK_VALIDITY(c->pstream, port, tag, PA_ERR_INVALID);
+    CHECK_VALIDITY(c->pstream, port_name, tag, PA_ERR_INVALID);
 
     if (command == PA_COMMAND_SET_SINK_PORT) {
         pa_sink *sink;
@@ -4775,6 +4776,8 @@ static void command_set_sink_or_source_port(pa_pdispatch *pd, uint32_t command,
             sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK);
 
         CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
+        port = pa_hashmap_get(sink->ports, port_name);
+        CHECK_VALIDITY(c->pstream, port, tag, PA_ERR_NOENTITY);
 
         if ((ret = pa_sink_set_port(sink, port, true)) < 0) {
             pa_pstream_send_error(c->pstream, tag, -ret);
@@ -4791,6 +4794,8 @@ static void command_set_sink_or_source_port(pa_pdispatch *pd, uint32_t command,
             source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE);
 
         CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
+        port = pa_hashmap_get(source->ports, port_name);
+        CHECK_VALIDITY(c->pstream, port, tag, PA_ERR_NOENTITY);
 
         if ((ret = pa_source_set_port(source, port, true)) < 0) {
             pa_pstream_send_error(c->pstream, tag, -ret);
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c
index 1792538..45dc618 100644
--- a/src/pulsecore/sink.c
+++ b/src/pulsecore/sink.c
@@ -3424,12 +3424,12 @@ size_t pa_sink_get_max_request(pa_sink *s) {
 }
 
 /* Called from main context */
-int pa_sink_set_port(pa_sink *s, const char *name, bool save) {
-    pa_device_port *port;
+int pa_sink_set_port(pa_sink *s, pa_device_port *port, bool save) {
     pa_device_port *old_port;
     int ret;
 
     pa_sink_assert_ref(s);
+    pa_assert(port);
     pa_assert_ctl_context();
 
     if (!s->set_port) {
@@ -3437,12 +3437,6 @@ int pa_sink_set_port(pa_sink *s, const char *name, bool save) {
         return -PA_ERR_NOTIMPLEMENTED;
     }
 
-    if (!name)
-        return -PA_ERR_NOENTITY;
-
-    if (!(port = pa_hashmap_get(s->ports, name)))
-        return -PA_ERR_NOENTITY;
-
     old_port = s->active_port;
 
     if (port == old_port) {
diff --git a/src/pulsecore/sink.h b/src/pulsecore/sink.h
index 020ee3d..a3bd24b 100644
--- a/src/pulsecore/sink.h
+++ b/src/pulsecore/sink.h
@@ -461,7 +461,7 @@ bool pa_sink_get_mute(pa_sink *sink, bool force_refresh);
 
 bool pa_sink_update_proplist(pa_sink *s, pa_update_mode_t mode, pa_proplist *p);
 
-int pa_sink_set_port(pa_sink *s, const char *name, bool save);
+int pa_sink_set_port(pa_sink *s, pa_device_port *port, bool save);
 void pa_sink_set_mixer_dirty(pa_sink *s, bool is_dirty);
 
 unsigned pa_sink_linked_by(pa_sink *s); /* Number of connected streams */
diff --git a/src/pulsecore/source.c b/src/pulsecore/source.c
index 0843795..d836ae2 100644
--- a/src/pulsecore/source.c
+++ b/src/pulsecore/source.c
@@ -2717,12 +2717,12 @@ size_t pa_source_get_max_rewind(pa_source *s) {
 }
 
 /* Called from main context */
-int pa_source_set_port(pa_source *s, const char *name, bool save) {
-    pa_device_port *port;
+int pa_source_set_port(pa_source *s, pa_device_port *port, bool save) {
     pa_device_port *old_port;
     int ret;
 
     pa_source_assert_ref(s);
+    pa_assert(port);
     pa_assert_ctl_context();
 
     if (!s->set_port) {
@@ -2730,12 +2730,6 @@ int pa_source_set_port(pa_source *s, const char *name, bool save) {
         return -PA_ERR_NOTIMPLEMENTED;
     }
 
-    if (!name)
-        return -PA_ERR_NOENTITY;
-
-    if (!(port = pa_hashmap_get(s->ports, name)))
-        return -PA_ERR_NOENTITY;
-
     old_port = s->active_port;
 
     if (port == old_port) {
diff --git a/src/pulsecore/source.h b/src/pulsecore/source.h
index fcb52a7..cb8f900 100644
--- a/src/pulsecore/source.h
+++ b/src/pulsecore/source.h
@@ -395,7 +395,7 @@ bool pa_source_get_mute(pa_source *source, bool force_refresh);
 
 bool pa_source_update_proplist(pa_source *s, pa_update_mode_t mode, pa_proplist *p);
 
-int pa_source_set_port(pa_source *s, const char *name, bool save);
+int pa_source_set_port(pa_source *s, pa_device_port *port, bool save);
 void pa_source_set_mixer_dirty(pa_source *s, bool is_dirty);
 
 int pa_source_update_rate(pa_source *s, uint32_t rate, bool passthrough);
-- 
1.8.3.1



More information about the pulseaudio-discuss mailing list