1- using CSCore . CoreAudioAPI ;
2- using System ;
3- using System . Collections . ObjectModel ;
1+ using System ;
42using System . Diagnostics ;
5- using System . Runtime . InteropServices ;
63using System . Threading ;
74using System . Windows ;
8- using System . Web . Script . Serialization ;
95using Newtonsoft . Json ;
10- using CSCore . Win32 ;
116using System . Collections . Generic ;
127using System . Net . Sockets ;
138using System . Net ;
149using AudioSwitcher . AudioApi . CoreAudio ;
15- using AudioSwitcher . AudioApi . Observables ;
1610using AudioSwitcher . AudioApi ;
1711using System . Reactive . Linq ;
18- using System . Text . RegularExpressions ;
1912using System . Reactive . Subjects ;
20- using System . Reactive . Concurrency ;
2113using System . Windows . Navigation ;
2214using AudioSwitcher . AudioApi . Session ;
2315
@@ -35,7 +27,10 @@ public partial class MainWindow : Window, ClientListener
3527 private UpdateListener m_updateListener ;
3628 private Dictionary < string , AudioSessionKeeper > m_sessions = new Dictionary < string , AudioSessionKeeper > ( ) ;
3729
38- Subject < bool > m_updateSubject = new Subject < bool > ( ) ;
30+ private AudioSessionVolumeListener m_sessionVolumeListener ;
31+ private AudioSessionMuteListener m_sessionMuteListener ;
32+
33+ private Subject < bool > m_updateSubject = new Subject < bool > ( ) ;
3934
4035 JsonSerializerSettings m_jsonsettings = new JsonSerializerSettings
4136 {
@@ -60,6 +55,9 @@ public MainWindow()
6055 server_ip . Content = ipAddress ;
6156 Console . WriteLine ( "ipAddress: " + ipAddress ) ;
6257
58+ m_sessionVolumeListener = new AudioSessionVolumeListener ( this ) ;
59+ m_sessionMuteListener = new AudioSessionMuteListener ( this ) ;
60+
6361 updateConnectionStatus ( ) ;
6462
6563 m_coreAudioController = new CoreAudioController ( ) ;
@@ -71,11 +69,11 @@ public MainWindow()
7169 MasterVolumeListener masterVolumeListener = new MasterVolumeListener ( this ) ;
7270
7371 m_coreAudioController . DefaultPlaybackDevice . VolumeChanged
74- . Throttle ( TimeSpan . FromMilliseconds ( 100 ) )
72+ // .Throttle(TimeSpan.FromMilliseconds(10 ))
7573 . Subscribe ( masterVolumeListener ) ;
7674
7775 m_coreAudioController . DefaultPlaybackDevice . MuteChanged
78- . Throttle ( TimeSpan . FromMilliseconds ( 100 ) )
76+ // .Throttle(TimeSpan.FromMilliseconds(10 ))
7977 . Subscribe ( masterVolumeListener ) ;
8078
8179 new Thread ( ( ) =>
@@ -99,7 +97,7 @@ public void requestUpdate()
9997
10098 private void updateConnectionStatus ( )
10199 {
102- Application . Current . Dispatcher . Invoke ( new Action ( ( ) =>
100+ Application . Current . Dispatcher . BeginInvoke ( new Action ( ( ) =>
103101 {
104102 if ( m_server == null || ! m_server . isRunning ( ) )
105103 {
@@ -193,13 +191,11 @@ private void updateState(PcAudio audioUpdate)
193191 PcAudio audioState = new PcAudio ( ) ;
194192 audioState . version = VERSION ;
195193
196- Console . WriteLine ( "Scrapping device IDs" ) ;
197-
198194 cleanUpSessionKeepers ( ) ;
199195
200196 var defaultDevice = m_coreAudioController . GetDefaultDevice ( DeviceType . Playback , AudioSwitcher . AudioApi . Role . Multimedia ) ;
201197 string defaultDeviceId = defaultDevice . Id . ToString ( ) ;
202-
198+
203199 // Add all avalible audio devices to our list of device IDs
204200 IEnumerable < CoreAudioDevice > devices = m_coreAudioController . GetPlaybackDevices ( ) ;
205201 foreach ( var device in devices )
@@ -210,20 +206,13 @@ private void updateState(PcAudio audioUpdate)
210206 }
211207 }
212208
213- Console . WriteLine ( "Done scrapping device IDs" ) ;
214-
215209 // Master device updates
216210 if ( audioUpdate != null && audioUpdate . defaultDevice != null )
217211 {
218- Console . WriteLine ( "Has default device" ) ;
219-
220212 if ( ! audioUpdate . defaultDevice . deviceId . Equals ( defaultDeviceId ) )
221213 {
222- Console . WriteLine ( "Audio update default device change request" ) ;
223-
224214 Guid deviceId = Guid . Parse ( audioUpdate . defaultDevice . deviceId ) ;
225215
226- Console . WriteLine ( "About to get new default device" ) ;
227216 CoreAudioDevice newDefaultAudioDevice = m_coreAudioController . GetDevice ( deviceId ) ;
228217 if ( newDefaultAudioDevice != null )
229218 {
@@ -244,7 +233,6 @@ private void updateState(PcAudio audioUpdate)
244233 {
245234 if ( audioUpdate . defaultDevice . masterMuted != null )
246235 {
247- Console . WriteLine ( "Getting current mute state" ) ;
248236 bool muted = audioUpdate . defaultDevice . masterMuted ?? m_coreAudioController . DefaultPlaybackDevice . IsMuted ;
249237 Console . WriteLine ( "Updating master mute: " + muted ) ;
250238
@@ -253,32 +241,25 @@ private void updateState(PcAudio audioUpdate)
253241
254242 if ( audioUpdate . defaultDevice . masterVolume != null )
255243 {
256- Console . WriteLine ( "Getting current volume" ) ;
257244 float volume = audioUpdate . defaultDevice . masterVolume ?? ( float ) m_coreAudioController . DefaultPlaybackDevice . Volume ;
258245 Console . WriteLine ( "Updating master volume: " + volume ) ;
259246
260- m_coreAudioController . DefaultPlaybackDevice . Volume = volume * 100 ;
247+ m_coreAudioController . DefaultPlaybackDevice . Volume = volume ;
261248 }
262249
263250 return ;
264251 }
265252 }
266253 }
267254
268- Console . WriteLine ( "Creating new device data" ) ;
269-
270255 // Create our default audio device and populate it's volume and mute status
271256 AudioDevice audioDevice = new AudioDevice ( defaultDevice . FullName , defaultDeviceId ) ;
272257 audioState . defaultDevice = audioDevice ;
273258
274- Console . WriteLine ( "Creating new device data" ) ;
275-
276259 CoreAudioDevice defaultPlaybackDevice = m_coreAudioController . DefaultPlaybackDevice ;
277260 audioDevice . masterVolume = ( float ) defaultPlaybackDevice . Volume ;
278261 audioDevice . masterMuted = defaultPlaybackDevice . IsMuted ;
279262
280- Console . WriteLine ( "Iterating sessions" ) ;
281-
282263 // Go through all audio sessions
283264 foreach ( var session in defaultDevice . SessionController . All ( ) )
284265 {
@@ -288,9 +269,9 @@ private void updateState(PcAudio audioUpdate)
288269 string sessionId = session . Id . ToString ( ) ;
289270 if ( ! m_sessions . ContainsKey ( sessionId ) )
290271 {
291- Console . WriteLine ( "Found new audio session" ) ;
272+ // Console.WriteLine("Found new audio session");
292273
293- AudioSessionKeeper sessionKeeper = new AudioSessionKeeper ( session , this ) ;
274+ AudioSessionKeeper sessionKeeper = new AudioSessionKeeper ( session , m_sessionVolumeListener , m_sessionMuteListener ) ;
294275 m_sessions . Add ( session . Id , sessionKeeper ) ;
295276 }
296277
@@ -325,8 +306,10 @@ private void updateState(PcAudio audioUpdate)
325306 string sessionName = session . DisplayName ;
326307 if ( sessionName == null || sessionName . Trim ( ) == "" )
327308 {
328- var process = Process . GetProcessById ( session . ProcessId ) ;
329- sessionName = process . ProcessName ;
309+ using ( var process = Process . GetProcessById ( session . ProcessId ) )
310+ {
311+ sessionName = process . ProcessName ;
312+ }
330313 }
331314
332315 AudioSession audioSession = new AudioSession ( sessionName , ( float ) session . Volume , session . IsMuted ) ;
@@ -341,8 +324,6 @@ private void updateState(PcAudio audioUpdate)
341324 AudioSessionKeeper sessionKeeper = m_sessions [ session . Id ] ;
342325 m_sessions . Remove ( session . Id ) ;
343326 sessionKeeper . Dispose ( ) ;
344-
345- Console . WriteLine ( "Done cleaning up" ) ;
346327 }
347328 }
348329 }
0 commit comments