Skip to content

Commit f1aef5d

Browse files
committed
fix(Device::Selection): Fix issue with recording and playback device having the same name not appearing in selected devices.
Fixes #1070
1 parent 428aedd commit f1aef5d

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

SoundSwitch.Common/Framework/Audio/Collection/DeviceCollection.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ public DeviceCollection(IEnumerable<T> deviceInfos)
2121
}
2222

2323
_byId[item.Id] = item;
24-
_byName[item.NameClean] = item;
24+
_byName[GetNameKey(item)] = item;
2525
}
2626
}
2727

28+
/// <summary>
29+
/// Generate a key that merge together the type of device and their name.
30+
/// </summary>
31+
/// <remarks>Recording and Playback device can share the same name, this is done to avoid conflict between the two</remarks>
32+
/// <param name="item"></param>
33+
/// <returns></returns>
34+
private string GetNameKey(T item) => $"{item.Type}-{item.NameClean}";
35+
2836
public IEnumerator<T> GetEnumerator()
2937
{
3038
return _byId.Values.OrderBy(info => info.DiscoveredAt).GetEnumerator();
@@ -43,7 +51,7 @@ public void Add(T item)
4351
}
4452

4553
_byId.TryAdd(item.Id, item);
46-
_byName.TryAdd(item.NameClean, item);
54+
_byName.TryAdd(GetNameKey(item), item);
4755
}
4856

4957
public void Clear()
@@ -59,8 +67,7 @@ public bool Contains(T item)
5967
throw new ArgumentNullException();
6068
}
6169

62-
return _byId.ContainsKey(item.Id) || _byName.ContainsKey(item.NameClean);
63-
70+
return _byId.ContainsKey(item.Id) || _byName.ContainsKey(GetNameKey(item));
6471
}
6572

6673
public void CopyTo(T[] array, int arrayIndex)
@@ -76,12 +83,13 @@ public bool Remove(T item)
7683
}
7784

7885
var removeId = _byId.Remove(item.Id);
79-
var removeName = _byName.Remove(item.NameClean, out var removedByName);
86+
var removeName = _byName.Remove(GetNameKey(item), out var removedByName);
8087

8188
//If we found it by name, remove it also with it's id
8289
//this avoid the case that a device is removed because of name matching
8390
//but it's still used since we iterate the _byId
84-
if(removeName) {
91+
if (removeName)
92+
{
8593
_byId.Remove(removedByName.Id);
8694
}
8795

SoundSwitch.Common/Framework/Audio/Device/DeviceInfo.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public DeviceInfo(MMDevice device)
5151
Id = device.ID;
5252
Type = device.DataFlow;
5353
var deviceProperties = device.Properties;
54-
var enumerator = deviceProperties.Contains(PropertyKeys.DEVPKEY_Device_EnumeratorName) ? (string) deviceProperties[PropertyKeys.DEVPKEY_Device_EnumeratorName].Value : "";
54+
var enumerator = deviceProperties.Contains(PropertyKeys.DEVPKEY_Device_EnumeratorName) ? (string)deviceProperties[PropertyKeys.DEVPKEY_Device_EnumeratorName].Value : "";
5555
IsUsb = enumerator == "USB";
5656
}
5757

@@ -94,7 +94,7 @@ public bool Equals(DeviceInfo other)
9494
//help recognizing the same device
9595
return NameClean == other.NameClean;
9696
}
97-
97+
9898
public override bool Equals(object obj)
9999
{
100100
if (ReferenceEquals(null, obj))
@@ -117,7 +117,7 @@ public override bool Equals(object obj)
117117

118118
public override int GetHashCode()
119119
{
120-
return HashCode.Combine(NameClean, Id);
120+
return HashCode.Combine(Type, NameClean, Id);
121121
}
122122
}
123123
}

0 commit comments

Comments
 (0)