-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApps.cs
More file actions
209 lines (181 loc) · 5.93 KB
/
Copy pathApps.cs
File metadata and controls
209 lines (181 loc) · 5.93 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using CSCore.CoreAudioAPI;
namespace AutoVolumeControl
{
class Apps : IDisposable
{
private readonly List<string> apps;
private MMDevice defaultPlaybackDevice;
private readonly object lockObj = new object();
public event EventHandler AppsUpdated;
private readonly AppSettings appSettings;
public Apps()
{
apps = new List<string>();
appSettings = new AppSettings();
InitializeDefaultDevice();
}
public List<string> GetApps()
{
lock (lockObj)
{
return new List<string>(apps);
}
}
public void AddRange(IEnumerable<string> newApps)
{
lock (lockObj)
{
apps.AddRange(newApps);
}
}
public void Refresh()
{
List<string> sessionApps = new List<string>();
var task = Task.Run(() =>
{
try
{
InitializeCom();
var sessions = GetActiveSessions();
sessionApps.AddRange(sessions);
}
catch (Exception ex)
{
Console.WriteLine($"Error in Refresh Task: {ex.Message}\n{ex.StackTrace}");
}
finally
{
ComHelper.CoUninitialize();
}
});
try
{
task.Wait();
}
catch (AggregateException aggEx)
{
foreach (var ex in aggEx.InnerExceptions)
{
Console.WriteLine($"Exception in task: {ex.Message}\n{ex.StackTrace}");
}
}
bool isUpdated = false;
lock (lockObj)
{
foreach (var app in sessionApps)
{
if (!appSettings.Exists(app))
{
appSettings.Set(app, "True");
}
if (!apps.Contains(app))
{
apps.Add(app);
isUpdated = true;
}
}
int originalCount = apps.Count;
apps.RemoveAll(a => !sessionApps.Contains(a));
if (apps.Count != originalCount)
{
isUpdated = true;
}
}
if (isUpdated)
{
AppsUpdated?.Invoke(this, EventArgs.Empty);
}
}
private void InitializeDefaultDevice()
{
Task.Run(() =>
{
try
{
InitializeCom();
using var enumerator = new MMDeviceEnumerator();
defaultPlaybackDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
}
catch (Exception ex)
{
Console.WriteLine($"Error in InitializeDefaultDevice: {ex.Message}");
}
finally
{
ComHelper.CoUninitialize();
}
}).Wait();
}
private void InitializeCom()
{
int hr = ComHelper.CoInitializeEx(IntPtr.Zero, ComHelper.COINIT.COINIT_MULTITHREADED);
if (hr == (int)HResult.RPC_E_CHANGED_MODE)
{
Console.WriteLine("COM already initialized with a different threading model");
}
else if (hr < 0)
{
throw new COMException("Failed to initialize COM library", hr);
}
}
private IEnumerable<string> GetActiveSessions()
{
var sessionNames = new List<string>();
using var sessionManager = AudioSessionManager2.FromMMDevice(defaultPlaybackDevice);
var sessionEnumerator = sessionManager.GetSessionEnumerator();
foreach (var session in sessionEnumerator)
{
using var audioSessionControl = session.QueryInterface<AudioSessionControl2>();
if (audioSessionControl.IsSystemSoundSession)
continue;
string name = GetSessionDisplayName(audioSessionControl);
if (!string.IsNullOrEmpty(name) && !sessionNames.Contains(name))
{
sessionNames.Add(name);
Console.WriteLine($"Found audio session: {name}");
}
}
Console.WriteLine($"Total sessions found: {sessionNames.Count}");
return sessionNames;
}
private string GetSessionDisplayName(AudioSessionControl2 audioSession)
{
string name = null;
name = GetProcessName(audioSession.Process);
if (!string.IsNullOrEmpty(name))
return name.Trim();
name = audioSession.DisplayName;
if (!string.IsNullOrEmpty(name))
return name.Trim();
name = audioSession.SessionIdentifier;
return name?.Trim();
}
private string GetProcessName(Process process)
{
try
{
return process?.ProcessName;
}
catch (Exception ex)
{
Console.WriteLine($"Error getting process name: {ex.Message}");
return null;
}
}
public void Dispose()
{
defaultPlaybackDevice?.Dispose();
}
public enum HResult : int
{
S_OK = 0,
S_FALSE = 1,
RPC_E_CHANGED_MODE = unchecked((int)0x80010106)
}
}
}