summaryrefslogtreecommitdiff
path: root/media-video/wireplumber/files/wireplumber-0.4.10-alsa.lua-fix-device-name-deduplication-when-reserva.patch
blob: 610db0e11e89721e504978021f24048c397e8c3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
https://gitlab.freedesktop.org/pipewire/wireplumber/-/commit/c00c5a6675b6640db13111c808eaa3251917c412

From c00c5a6675b6640db13111c808eaa3251917c412 Mon Sep 17 00:00:00 2001
From: Julian Bouzas <julian.bouzas@collabora.com>
Date: Wed, 18 May 2022 10:51:41 -0400
Subject: [PATCH] alsa.lua: fix device name deduplication when reservation is
 enabled

Fixes #241
---
 src/scripts/monitors/alsa.lua | 47 +++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/src/scripts/monitors/alsa.lua b/src/scripts/monitors/alsa.lua
index 01d241db..7beed3a8 100644
--- a/src/scripts/monitors/alsa.lua
+++ b/src/scripts/monitors/alsa.lua
@@ -11,6 +11,10 @@ local config = ... or {}
 -- ensure config.properties is not nil
 config.properties = config.properties or {}
 
+-- unique device/node name tables
+device_names_table = nil
+node_names_table = nil
+
 -- preprocess rules and create Interest objects
 for _, r in ipairs(config.rules or {}) do
   r.interests = {}
@@ -41,16 +45,6 @@ function rulesApplyProperties(properties)
   end
 end
 
-function findDuplicate(parent, id, property, value)
-  for i = 0, id - 1, 1 do
-    local obj = parent:get_managed_object(i)
-    if obj and obj.properties[property] == value then
-      return true
-    end
-  end
-  return false
-end
-
 function nonempty(str)
   return str ~= "" and str or nil
 end
@@ -125,11 +119,11 @@ function createNode(parent, id, type, factory, properties)
 
     -- deduplicate nodes with the same name
     for counter = 2, 99, 1 do
-      if findDuplicate(parent, id, "node.name", properties["node.name"]) then
-        properties["node.name"] = name .. "." .. counter
-      else
+      if node_names_table[properties["node.name"]] ~= true then
+        node_names_table[properties["node.name"]] = true
         break
       end
+      properties["node.name"] = name .. "." .. counter
     end
   end
 
@@ -186,6 +180,10 @@ function createDevice(parent, id, factory, properties)
   local device = SpaDevice(factory, properties)
   if device then
     device:connect("create-object", createNode)
+    device:connect("object-removed", function (parent, id)
+      local node = parent:get_managed_object(id)
+      node_names_table[node.properties["node.name"]] = nil
+    end)
     device:activate(Feature.SpaDevice.ENABLED | Feature.Proxy.BOUND)
     parent:store_managed_object(id, device)
   else
@@ -205,11 +203,11 @@ function prepareDevice(parent, id, type, factory, properties)
 
   -- deduplicate devices with the same name
   for counter = 2, 99, 1 do
-    if findDuplicate(parent, id, "device.name", properties["device.name"]) then
-      properties["device.name"] = name .. "." .. counter
-    else
+    if device_names_table[properties["device.name"]] ~= true then
+      device_names_table[properties["device.name"]] = true
       break
     end
+    properties["device.name"] = name .. "." .. counter
   end
 
   -- ensure the device has a description
@@ -337,16 +335,21 @@ function createMonitor ()
   -- handle create-object to prepare device
   m:connect("create-object", prepareDevice)
 
-  -- if dbus reservation, handle object-removed to destroy device reservations
-  if rd_plugin then
-    m:connect("object-removed", function (parent, id)
-      local device = parent:get_managed_object(id)
+  -- handle object-removed to destroy device reservations and recycle device name
+  m:connect("object-removed", function (parent, id)
+    local device = parent:get_managed_object(id)
+    if rd_plugin then
       local rd_name = device.properties["api.dbus.ReserveDevice1"]
       if rd_name then
         rd_plugin:call("destroy-reservation", rd_name)
       end
-    end)
-  end
+    end
+    device_names_table[device.properties["device.name"]] = nil
+  end)
+
+  -- reset the name tables to make sure names are recycled
+  device_names_table = {}
+  node_names_table = {}
 
   -- activate monitor
   Log.info("Activating ALSA monitor")
-- 
GitLab