void audioseslist_VolumeChanged(object sender, VolumeEventArgs e) { AudioSessionListener asl = (AudioSessionListener)sender; try { Console.WriteLine(Process.GetProcessById(asl.ProcessID).ProcessName + ": " + e.NewVolume); } catch { } }
void AudioSessionTimer_Tick(object sender, EventArgs e) { if (lastDevice != null && Client != null && Client.CurrentPage == Constants.Device_Pages.LEVELS) { lastDevice = DevEnum.GetDevice(lastDeviceID); SessionCollection sescol = lastDevice.AudioSessionManager.Sessions; List <AudioSessionControl> newsess = new List <AudioSessionControl>(); for (int i = 0; i < sescol.Count; i++) { newsess.Add(sescol[i]); if (listeners.Where(value => value.ASC.ProcessID == sescol[i].ProcessID).Count() < 1) { if (!deadSessionProcessIDs.Contains((int)sescol[i].ProcessID)) { string output = ""; try { output = "{\"" + Constants.Commands.REQUEST_AUDIO_SESSIONS + "\":{\"" + lastDevice.ID + "\": {"; } catch { } string name = ""; if (sescol[i].DisplayName == "@%SystemRoot%\\System32\\AudioSrv.Dll,-202") { name = "System Sounds"; } else { try { name = FileVersionInfo.GetVersionInfo(Process.GetProcessById((int)sescol[i].ProcessID).MainModule.FileName).FileDescription.Trim(); if (name.Length > 20) { name = Process.GetProcessById((int)sescol[i].ProcessID).ProcessName; name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1)); } } catch { Console.WriteLine("Catch 1: " + sescol[i].DisplayName); try { name = Process.GetProcessById((int)sescol[i].ProcessID).ProcessName; name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1)); } catch { Console.WriteLine("Catch 2: " + sescol[i].DisplayName); name = sescol[i].DisplayName; } } } if (name != "") { output += "\"" + sescol[i].ProcessID + "\": [\"" + name + "\", " + sescol[i].SimpleAudioVolume.MasterVolume * 100 + "]"; output += "}}}"; Console.WriteLine("Added audio session detected..."); AudioSessionListener t = new AudioSessionListener((int)sescol[i].ProcessID, sescol[i], name); sescol[i].RegisterAudioSessionNotification(t); listeners.Add(t); SendMessage(output, true); } } } } List <AudioSessionListener> sess2remov = new List <AudioSessionListener>(); try { foreach (AudioSessionListener asl in listeners) { if (!deadSessionProcessIDs.Contains(asl.ProcessID)) { bool remove = false; string name = ""; try { if (Process.GetProcessById((int)asl.ProcessID) == null) { remove = true; } else if (Process.GetProcessById((int)asl.ProcessID).MainModule == null) { remove = true; } else if (Process.GetProcessById((int)asl.ProcessID).MainModule.FileName == null) { remove = true; } } catch { remove = true; } if (asl.ASC.DisplayName == "@%SystemRoot%\\System32\\AudioSrv.Dll,-202") { name = "System Sounds"; } else if (!remove) { try { name = FileVersionInfo.GetVersionInfo(Process.GetProcessById((int)asl.ProcessID).MainModule.FileName).FileDescription.Trim(); if (name.Length > 20) { name = Process.GetProcessById((int)asl.ProcessID).ProcessName; name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1)); } } catch { try { name = Process.GetProcessById((int)asl.ProcessID).ProcessName; name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1)); } catch { name = asl.ASC.DisplayName; } } } if (name == "") { remove = true; } if (remove && name != "System Sounds") { deadSessionProcessIDs.Add(asl.ProcessID); Console.WriteLine("Removed audio session detected..."); SendMessage("{\"" + Constants.Commands.REMOVE_SESSION + "\": \"" + asl.ProcessID + "\"}", true); asl.ASC.UnregisterAudioSessionNotification(asl); asl.Dispose(); sess2remov.Add(asl); } /*if (newsess.Where(value => value.ProcessID == asl.ProcessID).Count() < 1) * { * SendMessage("{\"" + Constants.Commands.REMOVE_SESSION + "\": \"" + asl.ProcessID + "\"}"); * asl.ASC.UnregisterAudioSessionNotification(asl); * asl.Dispose(); * listeners.Remove(asl); * }*/ } } } catch { } foreach (AudioSessionListener asl in sess2remov) { listeners.Remove(asl); } sess2remov.Clear(); sess2remov = null; } }
void ParseJSONPacket(string json) { Console.WriteLine("Parsing: " + json); dynamic data = DynamicJson.Parse(json); string command = ""; try { command = data.c; } catch { Console.WriteLine("Command identifier not found..."); } switch (command) { case Constants.Commands.DISCONNECT: DisconnectClient(); break; case Constants.Commands.UPDATE_OBJECT: break; case Constants.Commands.UPDATE_PAGE: Client.CurrentPage = data.d; Notif("Changed client's current page to: " + Client.CurrentPage); break; case Constants.Commands.PING: SendMessage("{\"" + Constants.Commands.PONG + "\": \"\"}", true); break; case Constants.Commands.PONG: if (stopwatch.IsRunning) { stopwatch.Stop(); Console.WriteLine("Ping to client: " + stopwatch.ElapsedMilliseconds + "ms"); } else { Error(Constants.ErrorType.InvalidlyTimedCommand, "Unable to output client response time due to the timer not running."); } break; case Constants.Commands.REQUEST_OUTPUT_DEVICES: MMDeviceCollection coll = DevEnum.EnumerateAudioEndPoints(EDataFlow.eRender, EDeviceState.DEVICE_STATE_ACTIVE); MMDevice defoutd = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); string output = "{\"" + Constants.Commands.REQUEST_OUTPUT_DEVICES + "\":["; for (int i = 0; i < coll.Count; i++) { output += "[\"" + coll[i].ID + "\",\"" + coll[i].FriendlyName + "\", " + (coll[i].ID == defoutd.ID).ToString().ToLower() + "],"; } output = output.Remove(output.Length - 1, 1); output += "]}"; SendMessage(output, true); break; case Constants.Commands.REQUEST_INPUT_DEVICES: foreach (KeyValuePair <int, string> pair in InputDevices) { DevEnum.GetDevice(pair.Value).AudioEndpointVolume.OnVolumeNotification -= InputDeviceVolChange; } InputDevices.Clear(); MMDeviceCollection coll2 = DevEnum.EnumerateAudioEndPoints(EDataFlow.eCapture, EDeviceState.DEVICE_STATE_ACTIVE); string output2 = "{\"" + Constants.Commands.REQUEST_INPUT_DEVICES + "\":["; for (int i = 0; i < coll2.Count; i++) { InputDevices.Add(i, coll2[i].ID); coll2[i].AudioEndpointVolume.OnVolumeNotification += InputDeviceVolChange; output2 += "[\"" + i.ToString() + "\",\"" + coll2[i].FriendlyName + "\", " + coll2[i].AudioEndpointVolume.MasterVolumeLevelScalar * 100 + "],"; } output2 = output2.Remove(output2.Length - 1, 1); output2 += "]}"; SendMessage(output2, true); break; case Constants.Commands.REQUEST_AUDIO_SESSIONS: foreach (AudioSessionListener asl in listeners) { asl.ASC.UnregisterAudioSessionNotification(asl); asl.Dispose(); } listeners.Clear(); SessionCollection scol = null; string output3 = ""; try { lastDevice = DevEnum.GetDevice(data.d); lastDeviceID = lastDevice.ID; lastDevice.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification; scol = lastDevice.AudioSessionManager.Sessions; output3 = "{\"" + Constants.Commands.REQUEST_AUDIO_SESSIONS + "\":{\"" + lastDevice.ID + "\": {"; output3 += "\"master\": " + lastDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100 + ","; } catch { } for (int i = 0; i < scol.Count; i++) { if (!deadSessionProcessIDs.Contains((int)scol[i].ProcessID)) { string name = ""; try { name = FileVersionInfo.GetVersionInfo(Process.GetProcessById((int)scol[i].ProcessID).MainModule.FileName).FileDescription.Trim(); if (name.Length > 20) { name = Process.GetProcessById((int)scol[i].ProcessID).ProcessName; name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1)); } } catch { try { name = Process.GetProcessById((int)scol[i].ProcessID).ProcessName; name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1)); } catch { name = scol[i].DisplayName; } } if (name == "Idle") { name = "System Sounds"; } AudioSessionListener t = new AudioSessionListener((int)scol[i].ProcessID, scol[i], name); scol[i].RegisterAudioSessionNotification(t); listeners.Add(t); output3 += "\"" + scol[i].ProcessID + "\": [\"" + name + "\", " + scol[i].SimpleAudioVolume.MasterVolume * 100 + "],"; } } output3 = output3.Remove(output3.Length - 1, 1); output3 += "} } }"; SendMessage(output3, true); break; case Constants.Commands.CHANGE_SESSION_LEVEL: if (data.d.pid == "master") { try { lastDevice = DevEnum.GetDevice(lastDeviceID); lastDevice.AudioEndpointVolume.MasterVolumeLevelScalar = (float)(data.d.vol / 100); if (lastDevice.AudioEndpointVolume.MasterVolumeLevelScalar == 0) { lastDevice.AudioEndpointVolume.Mute = true; } else { lastDevice.AudioEndpointVolume.Mute = false; } lastChangeSessionLevelTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; lastChangedVol = new ProcessVolumeInt(-69001, (int)data.d.vol, ((int)data.d.vol == 0)); } catch { } } else { List <AudioSessionListener> asls = listeners.Where(value => value.ProcessID.ToString() == data.d.pid).ToList(); foreach (AudioSessionListener asl in asls) { asl.ASC.SimpleAudioVolume.MasterVolume = (float)(data.d.vol / 100); if ((int)data.d.vol == 0) { asl.ASC.SimpleAudioVolume.Mute = true; } else { asl.ASC.SimpleAudioVolume.Mute = false; } lastChangedVol = new ProcessVolumeInt(asl.ProcessID, (int)data.d.vol, ((int)data.d.vol == 0)); } lastChangeSessionLevelTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; /*foreach (AudioSessionListener asl2 in listeners) * { * if (asl2.ProcessID.ToString() == data.d.pid) * { * asl2.ASC.SimpleAudioVolume.MasterVolume = (float)(data.d.vol / 100); * break; * } * }*/ } break; case Constants.Commands.CHANGE_INPUT_LEVEL: MMDevice dev = DevEnum.GetDevice(InputDevices[Convert.ToInt32(data.d[0])]); dev.AudioEndpointVolume.MasterVolumeLevelScalar = (float)(data.d[1] / 100); lastChangedVolD = new DeviceVolumeInt(InputDevices[Convert.ToInt32(data.d[0])], Convert.ToInt32(data.d[1]), (bool)(Convert.ToInt32(data.d[1]) == 0)); lastChangeSessionLevelTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; break; case Constants.Commands.REQUEST_TIME_ZONE: SendMessage("{\"" + Constants.Commands.REQUEST_TIME_ZONE + "\": \"" + olsonmap.Find(System.TimeZoneInfo.Local.Id) + "\"}", true); break; } }