[pulseaudio-discuss] [PATCH 14/19] dbus: Use hooks for sample cache new and removed events

Juho Hämäläinen juho.hamalainen at tieto.com
Thu Mar 19 04:50:55 PDT 2015


---
 src/modules/dbus/iface-core.c | 96 +++++++++++++++++++++++++++----------------
 1 file changed, 61 insertions(+), 35 deletions(-)

diff --git a/src/modules/dbus/iface-core.c b/src/modules/dbus/iface-core.c
index 9555703..90c8d13 100644
--- a/src/modules/dbus/iface-core.c
+++ b/src/modules/dbus/iface-core.c
@@ -111,6 +111,8 @@ struct pa_dbusiface_core {
 
     pa_hook_slot *default_sink_changed_slot;
     pa_hook_slot *default_source_changed_slot;
+    pa_hook_slot *sample_cache_new_slot;
+    pa_hook_slot *sample_cache_removed_slot;
     pa_hook_slot *card_put_slot;
     pa_hook_slot *card_unlink_slot;
     pa_hook_slot *sink_input_put_slot;
@@ -1578,7 +1580,6 @@ static void handle_stop_listening_for_signal(DBusConnection *conn, DBusMessage *
 
 static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
     pa_dbusiface_core *c = userdata;
-    pa_dbusiface_sample *sample_iface = NULL;
     pa_dbusiface_module *module_iface = NULL;
     DBusMessage *signal_msg = NULL;
     const char *object_path = NULL;
@@ -1586,40 +1587,6 @@ static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint3
     pa_assert(c);
 
     switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
-        case PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE:
-            if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
-                pa_scache_entry *sample = NULL;
-
-                if (!(sample = pa_idxset_get_by_index(core->scache, idx)))
-                    return; /* The sample was removed immediately after creation. */
-
-                if (!(sample_iface = pa_hashmap_get(c->samples, PA_UINT32_TO_PTR(idx)))) {
-                    sample_iface = pa_dbusiface_sample_new(c, sample);
-                    pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(idx), sample_iface);
-                }
-
-                object_path = pa_dbusiface_sample_get_path(sample_iface);
-
-                pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
-                                                                   PA_DBUS_CORE_INTERFACE,
-                                                                   signals[SIGNAL_NEW_SAMPLE].name)));
-                pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
-
-            } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
-                if (!(sample_iface = pa_hashmap_remove(c->samples, PA_UINT32_TO_PTR(idx))))
-                    return;
-
-                object_path = pa_dbusiface_sample_get_path(sample_iface);
-
-                pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
-                                                                   PA_DBUS_CORE_INTERFACE,
-                                                                   signals[SIGNAL_SAMPLE_REMOVED].name)));
-                pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
-
-                pa_dbusiface_sample_free(sample_iface);
-            }
-            break;
-
         case PA_SUBSCRIPTION_EVENT_MODULE:
             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
                 pa_module *module = NULL;
@@ -1661,6 +1628,59 @@ static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint3
     }
 }
 
+static pa_hook_result_t sample_cache_new_cb(void *hook_data, void *call_data, void *slot_data) {
+    pa_dbusiface_core *c = slot_data;
+    pa_scache_entry *sample = call_data;
+    pa_dbusiface_sample *sample_iface;
+    const char *object_path;
+    DBusMessage *signal_msg = NULL;
+
+    pa_assert(c);
+    pa_assert(sample);
+
+    sample_iface = pa_dbusiface_sample_new(c, sample);
+    pa_assert_se(pa_hashmap_put(c->samples, PA_UINT32_TO_PTR(sample->index), sample_iface) >= 0);
+
+    object_path = pa_dbusiface_sample_get_path(sample_iface);
+
+    pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+                                                       PA_DBUS_CORE_INTERFACE,
+                                                       signals[SIGNAL_NEW_SAMPLE].name)));
+    pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+    pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+    dbus_message_unref(signal_msg);
+
+    return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sample_cache_removed_cb(void *hook_data, void *call_data, void *slot_data) {
+    pa_dbusiface_core *c = slot_data;
+    pa_scache_entry *sample = call_data;
+    pa_dbusiface_sample *sample_iface;
+    const char *object_path;
+    DBusMessage *signal_msg = NULL;
+
+    pa_assert(c);
+    pa_assert(sample);
+
+    pa_assert_se((sample_iface = pa_hashmap_remove(c->samples, PA_UINT32_TO_PTR(sample->index))));
+
+    object_path = pa_dbusiface_sample_get_path(sample_iface);
+
+    pa_assert_se((signal_msg = dbus_message_new_signal(PA_DBUS_CORE_OBJECT_PATH,
+                                                       PA_DBUS_CORE_INTERFACE,
+                                                       signals[SIGNAL_SAMPLE_REMOVED].name)));
+    pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+    pa_dbusiface_sample_free(sample_iface);
+
+    pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+    dbus_message_unref(signal_msg);
+
+    return PA_HOOK_OK;
+}
+
 static pa_hook_result_t default_sink_changed_cb(void *hook_data, void *call_data, void *slot_data) {
     pa_dbusiface_core *c = slot_data;
     pa_sink *new_fallback_sink = call_data;
@@ -2138,6 +2158,10 @@ pa_dbusiface_core *pa_dbusiface_core_new(pa_core *core) {
                                                    PA_HOOK_NORMAL, default_sink_changed_cb, c);
     c->default_source_changed_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_DEFAULT_SOURCE_CHANGED],
                                                      PA_HOOK_NORMAL, default_source_changed_cb, c);
+    c->sample_cache_new_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_SAMPLE_CACHE_NEW],
+                                               PA_HOOK_NORMAL, sample_cache_new_cb, c);
+    c->sample_cache_removed_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_SAMPLE_CACHE_REMOVED],
+                                                   PA_HOOK_NORMAL, sample_cache_removed_cb, c);
     c->card_put_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_CARD_PUT],
                                        PA_HOOK_NORMAL, card_put_cb, c);
     c->card_unlink_slot = pa_hook_connect(&core->hooks[PA_CORE_HOOK_CARD_UNLINK],
@@ -2230,6 +2254,8 @@ void pa_dbusiface_core_free(pa_dbusiface_core *c) {
     pa_hashmap_free(c->clients);
     pa_hook_slot_free(c->default_sink_changed_slot);
     pa_hook_slot_free(c->default_source_changed_slot);
+    pa_hook_slot_free(c->sample_cache_new_slot);
+    pa_hook_slot_free(c->sample_cache_removed_slot);
     pa_hook_slot_free(c->card_put_slot);
     pa_hook_slot_free(c->card_unlink_slot);
     pa_hook_slot_free(c->sink_input_put_slot);
-- 
1.9.1



More information about the pulseaudio-discuss mailing list