-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathMediaDeviceList.cs
204 lines (179 loc) · 6.42 KB
/
MediaDeviceList.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Devices.Enumeration;
using Windows.Media.Devices;
namespace Telegram.Common
{
public partial class MediaDeviceList
{
private readonly Dictionary<string, MediaDevice2> _cache = new();
private readonly object _lock = new();
private readonly MediaDeviceClass _class;
private readonly DeviceWatcher _watcher;
private MediaDeviceId _tracked;
public MediaDeviceList(MediaDeviceClass deviceClass)
{
_class = deviceClass;
_tracked = GetDefaultId(deviceClass);
try
{
var nativeClass = deviceClass switch
{
MediaDeviceClass.VideoInput => DeviceClass.VideoCapture,
MediaDeviceClass.AudioInput => DeviceClass.AudioCapture,
MediaDeviceClass.AudioOutput => DeviceClass.AudioRender,
_ => DeviceClass.AudioRender
};
_watcher = DeviceInformation.CreateWatcher(nativeClass);
_watcher.Added += OnAdded;
_watcher.Removed += OnRemoved;
_watcher.Start();
if (deviceClass == MediaDeviceClass.AudioInput)
{
MediaDevice.DefaultAudioCaptureDeviceChanged += OnDefaultAudioCaptureDeviceChanged;
}
else if (deviceClass == MediaDeviceClass.AudioOutput)
{
MediaDevice.DefaultAudioRenderDeviceChanged += OnDefaultAudioRenderDeviceChanged;
}
}
catch
{
// All the remote procedure calls must be wrapped in a try-catch block
}
}
public void Stop()
{
try
{
_watcher.Added -= OnAdded;
_watcher.Removed -= OnRemoved;
_watcher.Stop();
}
catch
{
// All the remote procedure calls must be wrapped in a try-catch block
}
}
private void OnDefaultAudioCaptureDeviceChanged(object sender, DefaultAudioCaptureDeviceChangedEventArgs args)
{
if (args.Role == AudioDeviceRole.Communications)
{
OnDefaultDeviceChanged(args.Id);
}
}
private void OnDefaultAudioRenderDeviceChanged(object sender, DefaultAudioRenderDeviceChangedEventArgs args)
{
if (args.Role == AudioDeviceRole.Communications)
{
OnDefaultDeviceChanged(args.Id);
}
}
private void OnDefaultDeviceChanged(string defaultId)
{
lock (_lock)
{
if (_tracked.IsDefault && _tracked.Id != defaultId && _cache.ContainsKey(defaultId))
{
_tracked = new MediaDeviceId(defaultId, true);
Changed?.Invoke(this, new MediaDeviceChangedEventArgs(_class, Constants.DefaultDeviceId));
}
}
}
public event EventHandler<MediaDeviceChangedEventArgs> Changed;
public bool? HasValues
{
get
{
lock (_cache)
{
return _cache.Count > 0 ? true : _watcher?.Status == DeviceWatcherStatus.EnumerationCompleted ? false : null;
}
}
}
public IList<MediaDevice2> GetValues()
{
lock (_lock)
{
var devices = _cache.Values
.OrderBy(x => x.Name)
.ToList();
if (_class != MediaDeviceClass.VideoInput)
{
devices.Insert(0, new MediaDevice2(string.Empty, Strings.Default, _class));
}
return devices;
}
}
public void Track(string deviceId)
{
lock (_lock)
{
if (string.IsNullOrEmpty(deviceId))
{
_tracked = GetDefaultId(_class);
}
else
{
_tracked = new MediaDeviceId(deviceId, false);
}
}
}
private void OnAdded(DeviceWatcher sender, DeviceInformation args)
{
lock (_lock)
{
var item = new MediaDevice2(args.Id, args.Name, _class);
_cache[item.Id] = item;
if (_tracked.IsDefault && _class != MediaDeviceClass.VideoInput)
{
var defaultId = GetDefaultId(_class);
if (defaultId.Id == item.Id && defaultId.Id != _tracked.Id)
{
_tracked = defaultId;
Changed?.Invoke(this, new MediaDeviceChangedEventArgs(_class, Constants.DefaultDeviceId));
}
}
}
}
private void OnRemoved(DeviceWatcher sender, DeviceInformationUpdate args)
{
lock (_lock)
{
if (_cache.TryGetValue(args.Id, out var item))
{
_cache.Remove(item.Id);
}
if (_tracked.Id == args.Id)
{
_tracked = GetDefaultId(_class);
Changed?.Invoke(this, new MediaDeviceChangedEventArgs(_class, Constants.DefaultDeviceId));
}
}
}
class MediaDeviceComparer : IComparer<MediaDevice2>
{
public int Compare(MediaDevice2 x, MediaDevice2 y)
{
return x.Name.CompareTo(y.Name);
}
}
private static MediaDeviceId GetDefaultId(MediaDeviceClass deviceClass)
{
try
{
return deviceClass switch
{
MediaDeviceClass.AudioInput => new MediaDeviceId(MediaDevice.GetDefaultAudioCaptureId(AudioDeviceRole.Communications), true),
MediaDeviceClass.AudioOutput => new MediaDeviceId(MediaDevice.GetDefaultAudioRenderId(AudioDeviceRole.Communications), true),
_ => new MediaDeviceId(Constants.DefaultDeviceId, true)
};
}
catch
{
return new MediaDeviceId(Constants.DefaultDeviceId, true);
}
}
}
}