[pulseaudio-discuss] [PATCH 2/3] protocol-native: Add signal receiving capability

Georg Chini georg at chini.tk
Fri Aug 4 13:37:28 UTC 2017


This patch extends the client subscription API, so that signals sent from
PulseAudio can be processed. Within PulseAudio, a signal can be emitted
using pa_signal_post(). The interface can be used to notify the client of
events that are not covered by the subscription API (for example a button
press event on a bluetooth headset).

Setting up signal notification is very similar to using subscriptions.
First the client needs to subscribe with pa_context_subscribe_signals()
and then sets up a signal handler using pa_context_set_signal_callback().

The signal handler will receive three arguments in addition to the usual
context and userdata:
sender - string that specifies the origin of the signal
signal - string that specifies the type of the signal
signal_info - optional string for additional information
---
 PROTOCOL                        |  5 ++++
 configure.ac                    |  2 +-
 src/map-file                    |  2 ++
 src/pulse/def.h                 |  4 +++
 src/pulse/internal.h            |  2 ++
 src/pulse/subscribe.c           | 66 +++++++++++++++++++++++++++++++++++++----
 src/pulse/subscribe.h           |  9 ++++++
 src/pulsecore/core-messages.c   | 25 ++++++++++++++++
 src/pulsecore/core-messages.h   | 11 +++++++
 src/pulsecore/native-common.h   |  1 +
 src/pulsecore/pdispatch.c       |  1 +
 src/pulsecore/protocol-native.c | 66 +++++++++++++++++++++++++++++++++++++++++
 12 files changed, 187 insertions(+), 7 deletions(-)

diff --git a/PROTOCOL b/PROTOCOL
index 546998b7..e11aaa01 100644
--- a/PROTOCOL
+++ b/PROTOCOL
@@ -420,6 +420,11 @@ memfd support only to 10.0+ clients.
 
 Check commit 451d1d676237c81 for further details.
 
+## v33, implemented by > 11.0
+
+Added signal subscription functions. Clients can now subscribe to signals
+that PulseAudio sends.
+
 #### If you just changed the protocol, read this
 ## module-tunnel depends on the sink/source/sink-input/source-input protocol
 ## internals, so if you changed these, you might have broken module-tunnel.
diff --git a/configure.ac b/configure.ac
index 77b5ff5d..e28755b0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,7 +40,7 @@ AC_SUBST(PA_MINOR, pa_minor)
 AC_SUBST(PA_MAJORMINOR, pa_major.pa_minor)
 
 AC_SUBST(PA_API_VERSION, 12)
-AC_SUBST(PA_PROTOCOL_VERSION, 32)
+AC_SUBST(PA_PROTOCOL_VERSION, 33)
 
 # The stable ABI for client applications, for the version info x:y:z
 # always will hold y=z
diff --git a/src/map-file b/src/map-file
index 93a62b86..d6cc06c1 100644
--- a/src/map-file
+++ b/src/map-file
@@ -111,8 +111,10 @@ pa_context_set_source_volume_by_index;
 pa_context_set_source_volume_by_name;
 pa_context_set_state_callback;
 pa_context_set_subscribe_callback;
+pa_context_set_signal_callback;
 pa_context_stat;
 pa_context_subscribe;
+pa_context_subscribe_signals;
 pa_context_suspend_sink_by_index;
 pa_context_suspend_sink_by_name;
 pa_context_suspend_source_by_index;
diff --git a/src/pulse/def.h b/src/pulse/def.h
index 680bdc98..a26f2385 100644
--- a/src/pulse/def.h
+++ b/src/pulse/def.h
@@ -599,6 +599,9 @@ typedef enum pa_subscription_event_type {
     PA_SUBSCRIPTION_EVENT_REMOVE = 0x0020U,
     /**< An object was removed */
 
+    PA_SUBSCRIPTION_EVENT_SIGNAL = 0x0040U,
+    /** A signal was issued */
+
     PA_SUBSCRIPTION_EVENT_TYPE_MASK = 0x0030U
     /**< A mask to extract the event operation from an event value */
 
@@ -634,6 +637,7 @@ typedef enum pa_subscription_event_type {
 #define PA_SUBSCRIPTION_EVENT_NEW PA_SUBSCRIPTION_EVENT_NEW
 #define PA_SUBSCRIPTION_EVENT_CHANGE PA_SUBSCRIPTION_EVENT_CHANGE
 #define PA_SUBSCRIPTION_EVENT_REMOVE PA_SUBSCRIPTION_EVENT_REMOVE
+#define PA_SUBSCRIPTION_EVENT_SIGNAL PA_SUBSCRIPTION_EVENT_SIGNAL
 #define PA_SUBSCRIPTION_EVENT_TYPE_MASK PA_SUBSCRIPTION_EVENT_TYPE_MASK
 /** \endcond */
 
diff --git a/src/pulse/internal.h b/src/pulse/internal.h
index 01d2b6e4..7d024cb1 100644
--- a/src/pulse/internal.h
+++ b/src/pulse/internal.h
@@ -89,6 +89,8 @@ struct pa_context {
     void *subscribe_userdata;
     pa_context_event_cb_t event_callback;
     void *event_userdata;
+    pa_context_signal_cb_t signal_callback;
+    void *signal_userdata;
 
     pa_mempool *mempool;
 
diff --git a/src/pulse/subscribe.c b/src/pulse/subscribe.c
index e7cce2e3..d9fd2e65 100644
--- a/src/pulse/subscribe.c
+++ b/src/pulse/subscribe.c
@@ -32,7 +32,6 @@
 void pa_command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
     pa_context *c = userdata;
     pa_subscription_event_type_t e;
-    uint32_t idx;
 
     pa_assert(pd);
     pa_assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
@@ -42,15 +41,39 @@ void pa_command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag
 
     pa_context_ref(c);
 
-    if (pa_tagstruct_getu32(t, &e) < 0 ||
-        pa_tagstruct_getu32(t, &idx) < 0 ||
-        !pa_tagstruct_eof(t)) {
+    if (pa_tagstruct_getu32(t, &e) < 0) {
         pa_context_fail(c, PA_ERR_PROTOCOL);
         goto finish;
     }
 
-    if (c->subscribe_callback)
-        c->subscribe_callback(c, e, idx, c->subscribe_userdata);
+    if (e != PA_SUBSCRIPTION_EVENT_SIGNAL) {
+        uint32_t idx;
+
+        if (pa_tagstruct_getu32(t, &idx) < 0 ||
+            !pa_tagstruct_eof(t)) {
+            pa_context_fail(c, PA_ERR_PROTOCOL);
+            goto finish;
+        }
+
+        if (c->subscribe_callback)
+            c->subscribe_callback(c, e, idx, c->subscribe_userdata);
+
+    } else {
+        const char *sender;
+        const char *signal;
+        const char *signal_info;
+
+        if (pa_tagstruct_gets(t, &sender) < 0 ||
+            pa_tagstruct_gets(t, &signal) < 0 ||
+            pa_tagstruct_gets(t, &signal_info) < 0 ||
+            !pa_tagstruct_eof(t)) {
+            pa_context_fail(c, PA_ERR_PROTOCOL);
+            goto finish;
+        }
+
+        if (c->signal_callback)
+            c->signal_callback(c, sender, signal, signal_info, c->signal_userdata);
+    }
 
 finish:
     pa_context_unref(c);
@@ -86,3 +109,34 @@ void pa_context_set_subscribe_callback(pa_context *c, pa_context_subscribe_cb_t
     c->subscribe_callback = cb;
     c->subscribe_userdata = userdata;
 }
+
+void pa_context_set_signal_callback(pa_context *c, pa_context_signal_cb_t cb, void *userdata) {
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
+    if (c->state == PA_CONTEXT_TERMINATED || c->state == PA_CONTEXT_FAILED)
+        return;
+
+    c->signal_callback = cb;
+    c->signal_userdata = userdata;
+}
+
+pa_operation* pa_context_subscribe_signals(pa_context *c, uint64_t signal_mask, pa_context_success_cb_t cb, void *userdata) {
+    pa_operation *o;
+    pa_tagstruct *t;
+    uint32_t tag;
+
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
+    PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
+
+    o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
+
+    t = pa_tagstruct_command(c, PA_COMMAND_SUBSCRIBE_SIGNALS, &tag);
+    pa_tagstruct_putu64(t, signal_mask);
+    pa_pstream_send_tagstruct(c->pstream, t);
+    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
+
+    return o;
+}
diff --git a/src/pulse/subscribe.h b/src/pulse/subscribe.h
index b43c8ea4..d4e03411 100644
--- a/src/pulse/subscribe.h
+++ b/src/pulse/subscribe.h
@@ -72,12 +72,21 @@ PA_C_DECL_BEGIN
 /** Subscription event callback prototype */
 typedef void (*pa_context_subscribe_cb_t)(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata);
 
+/** Signal event callback prototype */
+typedef void (*pa_context_signal_cb_t)(pa_context *c, const char *sender, const char *signal, const char *signal_info, void *userdata);
+
 /** Enable event notification */
 pa_operation* pa_context_subscribe(pa_context *c, pa_subscription_mask_t m, pa_context_success_cb_t cb, void *userdata);
 
+/** Enable signal notification */
+pa_operation* pa_context_subscribe_signals(pa_context *c, uint64_t signal_mask, pa_context_success_cb_t cb, void *userdata);
+
 /** Set the context specific call back function that is called whenever the state of the daemon changes */
 void pa_context_set_subscribe_callback(pa_context *c, pa_context_subscribe_cb_t cb, void *userdata);
 
+/** Set the context specific call back function that is called whenever pulseaudio sends a signal */
+void pa_context_set_signal_callback(pa_context *c, pa_context_signal_cb_t cb, void *userdata);
+
 PA_C_DECL_END
 
 #endif
diff --git a/src/pulsecore/core-messages.c b/src/pulsecore/core-messages.c
index 9ebaab24..cc224363 100644
--- a/src/pulsecore/core-messages.c
+++ b/src/pulsecore/core-messages.c
@@ -418,3 +418,28 @@ int pa_core_message_handler_group_unsubscribe(pa_core *c, const char *recipient_
     pa_xfree(real_group_name);
     return 0;
 }
+
+/* Send a signal */
+void pa_signal_post(pa_core *c, const char *sender, uint64_t facility, const char *signal, const char *signal_parameters) {
+    pa_signal_descriptor *sd;
+    char *response;
+
+    pa_assert(sender);
+    pa_assert(facility);
+    pa_assert(signal);
+
+    sd = pa_xnew(pa_signal_descriptor, 1);
+    sd->sender = pa_xstrdup(sender);
+    sd->facility = facility;
+    sd->signal = pa_xstrdup(signal);
+    sd->parameters = pa_xstrdup(signal_parameters);
+
+    if (pa_core_send_message(c, "signals", "send-signal", NULL, sd, &response) != PA_CORE_SEND_OK)
+        pa_log_warn("Sending signal %s from %s failed", signal, sender);
+
+    pa_xfree(response);
+    pa_xfree(sd->sender);
+    pa_xfree(sd->signal);
+    pa_xfree(sd->parameters);
+    pa_xfree(sd);
+}
diff --git a/src/pulsecore/core-messages.h b/src/pulsecore/core-messages.h
index d85f69f4..89fa57fd 100644
--- a/src/pulsecore/core-messages.h
+++ b/src/pulsecore/core-messages.h
@@ -47,6 +47,17 @@ typedef int (*pa_core_message_handler_cb_t)(
         char **response,
         void *userdata);
 
+/* Structure to pass signal information */
+typedef struct pa_signal_descriptor {
+    char *sender;
+    char *signal;
+    char *parameters;
+    uint64_t facility;
+} pa_signal_descriptor;
+
+/* Send a signal */
+void pa_signal_post(pa_core *c, const char *sender, uint64_t facility, const char *signal, const char *signal_parameters);
+
 /* Handler registration */
 void pa_core_message_handler_register(pa_core *c, const char *recipient_name, const char *description, bool is_private, pa_core_message_handler_cb_t cb, void *userdata);
 void pa_core_message_handler_unregister(pa_core *c, const char *recipient_name);
diff --git a/src/pulsecore/native-common.h b/src/pulsecore/native-common.h
index 70338b9f..5b9d5f0c 100644
--- a/src/pulsecore/native-common.h
+++ b/src/pulsecore/native-common.h
@@ -71,6 +71,7 @@ enum {
     PA_COMMAND_GET_SAMPLE_INFO,
     PA_COMMAND_GET_SAMPLE_INFO_LIST,
     PA_COMMAND_SUBSCRIBE,
+    PA_COMMAND_SUBSCRIBE_SIGNALS,
 
     PA_COMMAND_SET_SINK_VOLUME,
     PA_COMMAND_SET_SINK_INPUT_VOLUME,
diff --git a/src/pulsecore/pdispatch.c b/src/pulsecore/pdispatch.c
index ab632a5a..f5aa5a24 100644
--- a/src/pulsecore/pdispatch.c
+++ b/src/pulsecore/pdispatch.c
@@ -85,6 +85,7 @@ static const char *command_names[PA_COMMAND_MAX] = {
     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO] = "GET_SOURCE_OUTPUT_INFO",
     [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST] = "GET_SOURCE_OUTPUT_INFO_LIST",
     [PA_COMMAND_SUBSCRIBE] = "SUBSCRIBE",
+    [PA_COMMAND_SUBSCRIBE_SIGNALS] = "SUBSCRIBE_SIGNALS",
 
     [PA_COMMAND_SET_SINK_VOLUME] = "SET_SINK_VOLUME",
     [PA_COMMAND_SET_SINK_INPUT_VOLUME] = "SET_SINK_INPUT_VOLUME",
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index 866e2c64..e31562cf 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -54,6 +54,7 @@
 #include <pulsecore/sample-util.h>
 #include <pulsecore/creds.h>
 #include <pulsecore/core-util.h>
+#include <pulsecore/core-messages.h>
 #include <pulsecore/ipacl.h>
 #include <pulsecore/thread-mq.h>
 #include <pulsecore/mem.h>
@@ -186,6 +187,7 @@ struct pa_native_connection {
     pa_idxset *record_streams, *output_streams;
     uint32_t rrobin_index;
     pa_subscription *subscription;
+    uint64_t signal_mask;
     pa_time_event *auth_timeout_event;
     pa_srbchannel *srbpending;
 };
@@ -3721,6 +3723,26 @@ static void command_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag,
     pa_pstream_send_simple_ack(c->pstream, tag);
 }
 
+static void command_signal_subscribe(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+    pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
+    uint64_t m;
+
+    pa_native_connection_assert_ref(c);
+    pa_assert(t);
+
+    if (pa_tagstruct_getu64(t, &m) < 0 ||
+        !pa_tagstruct_eof(t)) {
+        protocol_error(c);
+        return;
+    }
+
+    CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
+
+    c->signal_mask = m;
+
+    pa_pstream_send_simple_ack(c->pstream, tag);
+}
+
 static void command_set_volume(
         pa_pdispatch *pd,
         uint32_t command,
@@ -4871,6 +4893,7 @@ static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
     [PA_COMMAND_GET_SAMPLE_INFO_LIST] = command_get_info_list,
     [PA_COMMAND_GET_SERVER_INFO] = command_get_server_info,
     [PA_COMMAND_SUBSCRIBE] = command_subscribe,
+    [PA_COMMAND_SUBSCRIBE_SIGNALS] = command_signal_subscribe,
 
     [PA_COMMAND_SET_SINK_VOLUME] = command_set_volume,
     [PA_COMMAND_SET_SINK_INPUT_VOLUME] = command_set_volume,
@@ -5217,6 +5240,44 @@ void pa_native_protocol_disconnect(pa_native_protocol *p, pa_module *m) {
             native_connection_unlink(c);
 }
 
+static int native_protocol_message_handler(const char *recipient, const char *message, const char *message_parameters, void *message_data, char **response, void *userdata) {
+    pa_native_protocol *p = (pa_native_protocol *) userdata;
+
+    pa_assert(p);
+    pa_assert(pa_streq(recipient, "/protocol-native"));
+    pa_assert(response);
+    pa_assert(message);
+
+    if (pa_streq(message, "send-signal")){
+        pa_native_connection *c;
+        uint32_t idx;
+        pa_signal_descriptor *sd = (pa_signal_descriptor *) message_data;
+
+        PA_IDXSET_FOREACH(c, p->connections, idx) {
+            if (sd->facility & c->signal_mask) {
+                pa_tagstruct *t;
+
+                pa_native_connection_assert_ref(c);
+
+                t = pa_tagstruct_new();
+                pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE_EVENT);
+                pa_tagstruct_putu32(t, (uint32_t) -1);
+                pa_tagstruct_putu32(t, PA_SUBSCRIPTION_EVENT_SIGNAL);
+                pa_tagstruct_puts(t, sd->sender);
+                pa_tagstruct_puts(t, sd->signal);
+                pa_tagstruct_puts(t, sd->parameters);
+                pa_pstream_send_tagstruct(c->pstream, t);
+            }
+        }
+
+        *response = pa_xstrdup("OK");
+
+    } else
+        *response = pa_xstrdup("Message not implemented");
+
+    return 0;
+}
+
 static pa_native_protocol* native_protocol_new(pa_core *c) {
     pa_native_protocol *p;
     pa_native_hook_t h;
@@ -5235,6 +5296,9 @@ static pa_native_protocol* native_protocol_new(pa_core *c) {
     for (h = 0; h < PA_NATIVE_HOOK_MAX; h++)
         pa_hook_init(&p->hooks[h], p);
 
+    pa_core_message_handler_register(c, "/protocol-native", "Message handler used for signals", true, native_protocol_message_handler, p);
+    pa_core_message_handler_group_subscribe(c, "/protocol-native", "signals", 0);
+
     pa_assert_se(pa_shared_set(c, "native-protocol", p) >= 0);
 
     return p;
@@ -5275,6 +5339,8 @@ void pa_native_protocol_unref(pa_native_protocol *p) {
 
     pa_strlist_free(p->servers);
 
+    pa_core_message_handler_unregister(p->core, "/protocol-native");
+
     for (h = 0; h < PA_NATIVE_HOOK_MAX; h++)
         pa_hook_done(&p->hooks[h]);
 
-- 
2.11.0



More information about the pulseaudio-discuss mailing list