https://gitlab.freedesktop.org/pipewire/wireplumber/-/commit/392cce2136e81ac3146078eacbbae85b694c917a

From 392cce2136e81ac3146078eacbbae85b694c917a Mon Sep 17 00:00:00 2001
From: Julian Bouzas <julian.bouzas@collabora.com>
Date: Fri, 20 May 2022 07:38:24 -0400
Subject: [PATCH] m-default-nodes: don't check if all device nodes are ready
 when finding default node

This check was originally added to avoid a small audio glitch when changing
default nodes while also changing the device profile (eg Gnome Sound Settings).
The check is removed because it causes issues when disabling alsa nodes. There
are plans to fix the audio glitch issue in the future with the planned
event-dispatcher architecture.

Fixes #279
---
 modules/module-default-nodes.c | 136 ---------------------------------
 1 file changed, 136 deletions(-)

diff --git a/modules/module-default-nodes.c b/modules/module-default-nodes.c
index 0fdaed5..577f9bb 100644
--- a/modules/module-default-nodes.c
+++ b/modules/module-default-nodes.c
@@ -345,135 +345,6 @@ reevaluate_default_node (WpDefaultNodes * self, WpMetadata *m, gint node_t)
   }
 }
 
-static guint
-get_device_total_nodes (WpPipewireObject * proxy)
-{
-  g_autoptr (WpIterator) profiles = NULL;
-  g_auto (GValue) item = G_VALUE_INIT;
-
-  profiles = wp_pipewire_object_enum_params_sync (proxy, "Profile", NULL);
-  if (!profiles)
-    return 0;
-
-  for (; wp_iterator_next (profiles, &item); g_value_unset (&item)) {
-    WpSpaPod *pod = g_value_get_boxed (&item);
-    gint idx = -1;
-    const gchar *name = NULL;
-    g_autoptr (WpSpaPod) classes = NULL;
-
-    /* Parse */
-    if (!wp_spa_pod_get_object (pod, NULL,
-        "index", "i", &idx,
-        "name", "s", &name,
-        "classes", "?P", &classes,
-        NULL))
-      continue;
-    if (!classes)
-      continue;
-
-    /* Parse profile classes */
-    {
-      g_autoptr (WpIterator) it = wp_spa_pod_new_iterator (classes);
-      g_auto (GValue) v = G_VALUE_INIT;
-      gint total_nodes = 0;
-      for (; wp_iterator_next (it, &v); g_value_unset (&v)) {
-        WpSpaPod *entry = g_value_get_boxed (&v);
-        g_autoptr (WpSpaPodParser) pp = NULL;
-        const gchar *media_class = NULL;
-        gint n_nodes = 0;
-        g_return_val_if_fail (entry, 0);
-        if (!wp_spa_pod_is_struct (entry))
-          continue;
-        pp = wp_spa_pod_parser_new_struct (entry);
-        g_return_val_if_fail (pp, 0);
-        g_return_val_if_fail (wp_spa_pod_parser_get_string (pp, &media_class), 0);
-        g_return_val_if_fail (wp_spa_pod_parser_get_int (pp, &n_nodes), 0);
-        wp_spa_pod_parser_end (pp);
-
-        total_nodes += n_nodes;
-      }
-
-      if (total_nodes > 0)
-        return total_nodes;
-    }
-  }
-
-  return 0;
-}
-
-static gboolean
-nodes_ready (WpDefaultNodes * self)
-{
-  g_autoptr (WpIterator) it = NULL;
-  g_auto (GValue) val = G_VALUE_INIT;
-
-  /* Get the total number of nodes for each device and make sure they exist
-   * and have at least 1 port */
-  it = wp_object_manager_new_filtered_iterator (self->rescan_om,
-      WP_TYPE_DEVICE, NULL);
-  for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
-    WpPipewireObject *device = g_value_get_object (&val);
-    guint total_nodes = get_device_total_nodes (device);
-    if (total_nodes > 0) {
-      guint32 device_id = wp_proxy_get_bound_id (WP_PROXY (device));
-      g_autoptr (WpIterator) node_it = NULL;
-      g_auto (GValue) node_val = G_VALUE_INIT;
-      guint ready_nodes = 0;
-
-      node_it = wp_object_manager_new_filtered_iterator (self->rescan_om,
-          WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY,
-          PW_KEY_DEVICE_ID, "=i", device_id, NULL);
-      for (; wp_iterator_next (node_it, &node_val); g_value_unset (&node_val)) {
-        WpPipewireObject *node = g_value_get_object (&node_val);
-        g_autoptr (WpPort) port =
-              wp_object_manager_lookup (self->rescan_om,
-              WP_TYPE_PORT, WP_CONSTRAINT_TYPE_PW_PROPERTY,
-              PW_KEY_NODE_ID, "=u", wp_proxy_get_bound_id (WP_PROXY (node)),
-              NULL);
-        if (port)
-          ready_nodes++;
-      }
-
-      if (ready_nodes < total_nodes) {
-        const gchar *device_name = wp_pipewire_object_get_property (
-            WP_PIPEWIRE_OBJECT (device), PW_KEY_DEVICE_NAME);
-        wp_debug_object (self, "device '%s' is not ready (%d/%d)", device_name,
-            ready_nodes, total_nodes);
-        return FALSE;
-      }
-    }
-  }
-
-  /* Make sure Audio and Video virtual sources have ports */
-  {
-    g_autoptr (WpIterator) node_it = NULL;
-    g_auto (GValue) node_val = G_VALUE_INIT;
-    node_it = wp_object_manager_new_filtered_iterator (self->rescan_om,
-        WP_TYPE_NODE, WP_CONSTRAINT_TYPE_PW_PROPERTY, PW_KEY_DEVICE_ID, "-",
-        NULL);
-    for (; wp_iterator_next (node_it, &node_val); g_value_unset (&node_val)) {
-      WpPipewireObject *node = g_value_get_object (&node_val);
-      const gchar *media_class = wp_pipewire_object_get_property (
-          WP_PIPEWIRE_OBJECT (node), PW_KEY_MEDIA_CLASS);
-      g_autoptr (WpPort) port =
-          wp_object_manager_lookup (self->rescan_om,
-          WP_TYPE_PORT, WP_CONSTRAINT_TYPE_PW_PROPERTY,
-          PW_KEY_NODE_ID, "=u", wp_proxy_get_bound_id (WP_PROXY (node)),
-          NULL);
-      if (!port &&
-          (g_strcmp0 ("Audio/Source/Virtual", media_class) == 0 ||
-           g_strcmp0 ("Video/Source/Virtual", media_class) == 0)) {
-        const gchar *node_name = wp_pipewire_object_get_property (
-            WP_PIPEWIRE_OBJECT (node), PW_KEY_NODE_NAME);
-        wp_debug_object (self, "virtual node '%s' is not ready", node_name);
-        return FALSE;
-      }
-    }
-  }
-
-  return TRUE;
-}
-
 static void
 sync_rescan (WpCore * core, GAsyncResult * res, WpDefaultNodes * self)
 {
@@ -491,10 +362,6 @@ sync_rescan (WpCore * core, GAsyncResult * res, WpDefaultNodes * self)
   if (!metadata)
     return;
 
-  /* Make sure nodes are ready for current profile */
-  if (!nodes_ready (self))
-    return;
-
   wp_trace_object (self, "re-evaluating defaults");
   reevaluate_default_node (self, metadata, AUDIO_SINK);
   reevaluate_default_node (self, metadata, AUDIO_SOURCE);
@@ -584,13 +451,10 @@ on_metadata_added (WpObjectManager *om, WpMetadata *metadata, gpointer d)
   self->rescan_om = wp_object_manager_new ();
   wp_object_manager_add_interest (self->rescan_om, WP_TYPE_DEVICE, NULL);
   wp_object_manager_add_interest (self->rescan_om, WP_TYPE_NODE, NULL);
-  wp_object_manager_add_interest (self->rescan_om, WP_TYPE_PORT, NULL);
   wp_object_manager_request_object_features (self->rescan_om, WP_TYPE_DEVICE,
       WP_OBJECT_FEATURES_ALL);
   wp_object_manager_request_object_features (self->rescan_om, WP_TYPE_NODE,
       WP_OBJECT_FEATURES_ALL);
-  wp_object_manager_request_object_features (self->rescan_om, WP_TYPE_PORT,
-      WP_OBJECT_FEATURES_ALL);
   g_signal_connect_object (self->rescan_om, "objects-changed",
       G_CALLBACK (schedule_rescan), self, G_CONNECT_SWAPPED);
   g_signal_connect_object (self->rescan_om, "object-added",
-- 
2.35.1

