-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
346 lines (328 loc) · 15.2 KB
/
Program.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using CSCore.CoreAudioAPI;
using Microsoft.Win32;
using System.Text.Json;
namespace WASAPI_Audio_Mirror
{
internal static class Program
{
private static NotifyIcon trayIcon = null;
private static ContextMenuStrip trayMenu = null;
private static ToolStripMenuItem inputItem = null;
private static ToolStripMenuItem outputItem = null;
private static ToolStripMenuItem pauseItem = null;
private static ToolStripMenuItem restartItem = null;
private static ToolStripMenuItem exitItem = null;
private static CancellationTokenSource cts = new CancellationTokenSource();
private static ConcurrentExclusiveSchedulerPair scheduler = new ConcurrentExclusiveSchedulerPair();
private static TaskFactory exclusiveFactory = new TaskFactory(scheduler.ExclusiveScheduler);
private static volatile bool paused = false;
private static volatile bool updateDevices = false;
private static MMDeviceEnumerator deviceEnumerator = new MMDeviceEnumerator();
private static MMNotificationClient deviceNotification = new MMNotificationClient(deviceEnumerator);
private static Settings settings = new Settings();
private static AudioMirror audioMirror = new AudioMirror(settings, deviceEnumerator, cts.Token);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
readSettings();
trayMenu = new ContextMenuStrip();
trayMenu.Opening += (s, e) => { if (updateDevices) { updateDevices = false; findAudioDevices(); }; };
inputItem = new ToolStripMenuItem("Input Audio Device");
inputItem.Name = "Input Audio Device";
trayMenu.Items.Add(inputItem);
outputItem = new ToolStripMenuItem("Output Audio Device");
outputItem.Name = "Output Audio Device";
findAudioDevices();
if (checkReady())
{
exclusiveFactory.StartNew(() => audioMirror.startMirror());
exclusiveFactory.StartNew(() => audioMirror.updateOutputs());
}
deviceNotification.DeviceAdded += (s, e) => { updateDevices = true; exclusiveFactory.StartNew(() => audioMirror.updateOutputs()); };
deviceNotification.DeviceRemoved += (s, e) => { updateDevices = true; exclusiveFactory.StartNew(() => audioMirror.updateOutputs()); };
deviceNotification.DeviceStateChanged += (s, e) => { updateDevices = true; exclusiveFactory.StartNew(() => audioMirror.updateOutputs()); };
deviceNotification.DevicePropertyChanged += (s, e) => { updateDevices = true; exclusiveFactory.StartNew(() => audioMirror.updateOutputs()); };
trayMenu.Items.Add(outputItem);
trayMenu.Items.Add(new ToolStripSeparator());
pauseItem = new ToolStripMenuItem("Pause");
pauseItem.Click += new EventHandler(onPause);
pauseItem.Name = "Pause";
trayMenu.Items.Add(pauseItem);
restartItem = new ToolStripMenuItem("Restart Mirror");
restartItem.Click += new EventHandler(onRestart);
restartItem.Name = "Restart Mirror";
trayMenu.Items.Add(restartItem);
exitItem = new ToolStripMenuItem("Exit");
exitItem.Click += new EventHandler(onExit);
exitItem.Name = "Exit";
trayMenu.Items.Add(exitItem);
trayIcon = new NotifyIcon();
trayIcon.Text = "WASAPI-Audio-Mirror";
trayIcon.Icon = Properties.Resources.icon;
trayIcon.Visible = true;
trayIcon.ContextMenuStrip = trayMenu;
SystemEvents.PowerModeChanged += onPowerModeChange;
Application.ApplicationExit += new EventHandler(onExit);
Application.Run();
}
private static bool checkReady()
{
int tries = 0;
bool foundInput = false;
bool foundOutput = false;
while (true)
{
tries++;
try
{
MMDevice device = deviceEnumerator.GetDevice(settings.selectedInputDeviceId);
if (device.DeviceState == DeviceState.Active)
{
foundInput = true;
break;
}
}
catch (Exception) { }
if (tries >= 10)
{
break;
}
Thread.Sleep(5000);
}
if (foundInput)
{
tries = 0;
while (true)
{
tries++;
foreach (string id in settings.selectedOutputDeviceIds)
{
try
{
MMDevice device = deviceEnumerator.GetDevice(id);
if (device.DeviceState == DeviceState.Active)
{
foundOutput = true;
break;
}
}
catch (Exception) { }
}
if (foundOutput || tries >= 10)
{
break;
}
Thread.Sleep(5000);
}
if (foundOutput)
{
return true;
}
}
return false;
}
private static void onPowerModeChange(object? sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Resume)
{
System.Diagnostics.Debug.WriteLine("Detecting system resume from sleep, restarting mirror");
exclusiveFactory.StartNew(() => audioMirror.stopMirror());
if (checkReady())
{
exclusiveFactory.StartNew(() => audioMirror.startMirror());
exclusiveFactory.StartNew(() => audioMirror.updateOutputs());
}
}
}
private static void onPause(object? sender, EventArgs e)
{
if (paused)
{
System.Diagnostics.Debug.WriteLine("Resuming mirror");
paused = false;
pauseItem.Name = "Pause";
pauseItem.Text = "Pause";
pauseItem.CheckState = CheckState.Unchecked;
exclusiveFactory.StartNew(() => audioMirror.resumeMirror());
}
else
{
System.Diagnostics.Debug.WriteLine("Pausing mirror");
paused = true;
pauseItem.Name = "Resume";
pauseItem.Text = "Resume";
pauseItem.CheckState = CheckState.Checked;
exclusiveFactory.StartNew(() => audioMirror.pauseMirror());
}
}
private static void onRestart(object? sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Restarting mirror");
exclusiveFactory.StartNew(() => audioMirror.stopMirror());
exclusiveFactory.StartNew(() => audioMirror.startMirror());
exclusiveFactory.StartNew(() => audioMirror.updateOutputs());
}
private static void onExit(object? sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Shutting down!");
deviceNotification.Dispose();
cts.Cancel();
Application.Exit();
if (trayIcon != null)
{
trayIcon.Visible = false;
trayIcon.Dispose();
}
}
private static void selectDevice(object? sender, EventArgs e, DeviceType type, string deviceId, ToolStripMenuItem item)
{
if (type == DeviceType.INPUT)
{
if (settings.selectedInputDeviceId != null && settings.selectedInputDeviceId.Equals(deviceId))
{
System.Diagnostics.Debug.WriteLine("Input " + item.Text + " already selected, unselecting");
settings.selectedInputDeviceId = null;
if (settings.selectedInputDeviceItem != null)
{
settings.selectedInputDeviceItem.CheckState = CheckState.Unchecked;
}
settings.selectedInputDeviceItem = null;
exclusiveFactory.StartNew(() => audioMirror.stopMirror());
}
else
{
System.Diagnostics.Debug.WriteLine("Input " + item.Text + " selected");
settings.selectedInputDeviceId = deviceId;
if (settings.selectedInputDeviceItem != null)
{
System.Diagnostics.Debug.WriteLine("Input " + settings.selectedInputDeviceItem.Name + " unselected");
settings.selectedInputDeviceItem.CheckState = CheckState.Unchecked;
}
settings.selectedInputDeviceItem = item;
item.CheckState = CheckState.Checked;
if (settings.selectedOutputDeviceIds.Contains(deviceId))
{
System.Diagnostics.Debug.WriteLine("Output " + item.Text + " already selected, unselecting");
settings.selectedOutputDeviceIds.Remove(deviceId);
settings.selectedOutputDeviceItems.RemoveWhere(item => { if (item.Name.Equals(deviceId)) { item.CheckState = CheckState.Unchecked; return true; } return false; });
}
exclusiveFactory.StartNew(() => audioMirror.stopMirror());
exclusiveFactory.StartNew(() => audioMirror.startMirror());
exclusiveFactory.StartNew(() => audioMirror.updateOutputs());
}
}
else
{
if (settings.selectedOutputDeviceIds.Contains(deviceId))
{
System.Diagnostics.Debug.WriteLine("Output " + item.Text + " already selected, unselecting");
settings.selectedOutputDeviceIds.Remove(deviceId);
settings.selectedOutputDeviceItems.RemoveWhere(item => { if (item.Name.Equals(deviceId)) { item.CheckState = CheckState.Unchecked; return true; } return false; });
if (settings.selectedOutputDeviceIds.Count > 0)
{
exclusiveFactory.StartNew(() => audioMirror.updateOutputs());
}
else
{
exclusiveFactory.StartNew(() => audioMirror.stopMirror());
}
}
else
{
System.Diagnostics.Debug.WriteLine("Output " + item.Text + " selected");
settings.selectedOutputDeviceIds.Add(deviceId);
settings.selectedOutputDeviceItems.Add(item);
item.CheckState = CheckState.Checked;
if (settings.selectedInputDeviceId != null && settings.selectedInputDeviceId.Equals(deviceId))
{
System.Diagnostics.Debug.WriteLine("Input " + item.Text + " already selected, unselecting");
settings.selectedInputDeviceId = null;
if (settings.selectedInputDeviceItem != null)
{
settings.selectedInputDeviceItem.CheckState = CheckState.Unchecked;
}
settings.selectedInputDeviceItem = null;
exclusiveFactory.StartNew(() => audioMirror.stopMirror());
}
else
{
exclusiveFactory.StartNew(() => audioMirror.startMirror());
exclusiveFactory.StartNew(() => audioMirror.updateOutputs());
}
}
}
saveSettings();
}
private static void findAudioDevices()
{
settings.selectedOutputDeviceItems.Clear();
inputItem.DropDownItems.Clear();
outputItem.DropDownItems.Clear();
settings.selectedOutputDeviceIds.Remove(settings.selectedInputDeviceId);
foreach (MMDevice device in MMDeviceEnumerator.EnumerateDevices(DataFlow.Render, DeviceState.Active))
{
System.Diagnostics.Debug.WriteLine("Found device " + device.FriendlyName + " ID: " + device.DeviceID);
ToolStripMenuItem itemInput = new ToolStripMenuItem();
itemInput.Text = device.FriendlyName;
itemInput.Name = device.DeviceID;
itemInput.Click += new EventHandler((sender, EventArgs) => { selectDevice(sender, EventArgs, DeviceType.INPUT, device.DeviceID, itemInput); });
ToolStripMenuItem itemOutput = new ToolStripMenuItem();
itemOutput.Text = device.FriendlyName;
itemOutput.Name = device.DeviceID;
itemOutput.Click += new EventHandler((sender, EventArgs) => { selectDevice(sender, EventArgs, DeviceType.OUTPUT, device.DeviceID, itemOutput); });
if (settings.selectedInputDeviceId != null)
{
if (settings.selectedInputDeviceId.Equals(device.DeviceID))
{
System.Diagnostics.Debug.WriteLine("Found previously selected input device: " + device.FriendlyName);
itemInput.CheckState = CheckState.Checked;
settings.selectedInputDeviceItem = itemInput;
}
}
if (settings.selectedOutputDeviceIds.Contains(device.DeviceID))
{
System.Diagnostics.Debug.WriteLine("Found previously selected output device: " + device.FriendlyName);
itemOutput.CheckState = CheckState.Checked;
settings.selectedOutputDeviceItems.Add(itemOutput);
}
inputItem.DropDownItems.Add(itemInput);
outputItem.DropDownItems.Add(itemOutput);
}
}
private static void saveSettings()
{
try
{
using (Stream stream = File.Open("audio-mirror.json", FileMode.Create))
{
JsonSerializer.Serialize(stream, settings);
}
}
catch (Exception) { }
}
private static void readSettings()
{
try
{
using (Stream stream = File.Open("audio-mirror.json", FileMode.Open))
{
settings = JsonSerializer.Deserialize<Settings>(stream);
if (settings == null)
{
settings = new Settings();
}
audioMirror.updateSettings(settings);
}
}
catch (Exception) { }
}
private enum DeviceType
{
INPUT,
OUTPUT
}
}
}