https://gitlab.freedesktop.org/pipewire/wireplumber/-/commit/22fd4292ab35786b09561508778ab49cc6488dc6

From 22fd4292ab35786b09561508778ab49cc6488dc6 Mon Sep 17 00:00:00 2001
From: Julian Bouzas <julian.bouzas@collabora.com>
Date: Tue, 7 Dec 2021 14:10:32 -0500
Subject: [PATCH] default-routes.lua: reevaluate current profile only for its
 own routes

Only check the current active profile when some routes changed that are part of
the profile. If some other route changed, just ignore it.
---
 src/scripts/default-routes.lua | 39 +++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/src/scripts/default-routes.lua b/src/scripts/default-routes.lua
index 1840989f..953cba33 100644
--- a/src/scripts/default-routes.lua
+++ b/src/scripts/default-routes.lua
@@ -352,6 +352,12 @@ function handleDevice(device)
   local dev_info = dev_infos[device["bound-id"]]
   local new_route_infos = {}
   local avail_routes_changed = false
+  local profile = nil
+
+  -- get current profile
+  for p in device:iterate_params("Profile") do
+    profile = parseParam(p, "Profile")
+  end
 
   -- look at all the routes and update/reset cached information
   for p in device:iterate_params("EnumRoute") do
@@ -370,7 +376,9 @@ function handleDevice(device)
       Log.info(device, "route " .. route.name .. " available changed " ..
                        route_info.available .. " -> " .. route.available)
       route_info.available = route.available
-      avail_routes_changed = true
+      if profile and arrayContains(route.profiles, profile.index) then
+        avail_routes_changed = true
+      end
     end
     route_info.prev_active = route_info.active
     route_info.active = false
@@ -419,23 +427,20 @@ function handleDevice(device)
     ::skip_route::
   end
 
-  -- now get the profile and restore routes for it
-  for p in device:iterate_params("Profile") do
-    local profile = parseParam(p, "Profile")
-    if profile then
-      local profile_changed = (dev_info.active_profile ~= profile.index)
-
-      -- if the profile changed, restore routes for that profile
-      -- if any of the routes of the current profile changed in availability,
-      -- then try to select a new "best" route for each device and ignore
-      -- what was stored
-      if profile_changed or avail_routes_changed then
-        dev_info.active_profile = profile.index
-        restoreProfileRoutes(device, dev_info, profile, profile_changed)
-      end
-
-      saveProfile(dev_info, profile.name)
+  -- restore routes for profile
+  if profile then
+    local profile_changed = (dev_info.active_profile ~= profile.index)
+
+    -- if the profile changed, restore routes for that profile
+    -- if any of the routes of the current profile changed in availability,
+    -- then try to select a new "best" route for each device and ignore
+    -- what was stored
+    if profile_changed or avail_routes_changed then
+      dev_info.active_profile = profile.index
+      restoreProfileRoutes(device, dev_info, profile, profile_changed)
     end
+
+    saveProfile(dev_info, profile.name)
   end
 end
 
-- 
GitLab
