diff --git a/SWYH/App.xaml.cs b/SWYH/App.xaml.cs index a8ead90..0ad40fe 100644 --- a/SWYH/App.xaml.cs +++ b/SWYH/App.xaml.cs @@ -19,8 +19,10 @@ * * You should have received a copy of the GNU General Public License * along with Stream What Your Hear. If not, see . - */ - + */ + +using NAudio.CoreAudioApi; + namespace SWYH { using OpenSource.UPnP.AV.RENDERER.CP; @@ -58,7 +60,7 @@ public partial class App : Application private System.Windows.Forms.NotifyIcon notifyIcon = null; private System.Windows.Forms.ToolStripMenuItem streamToMenu = null; private System.Windows.Forms.ToolStripMenuItem searchingItem = null; - private bool directClose = false; //Skip the statements in Application_Exit function. + private bool directClose = false; //Skip the statements in Application_Exit function. private void Application_Startup(object sender, StartupEventArgs e) { @@ -193,7 +195,8 @@ private void CloseStreamingConnections() if (renderer != null && renderer.Connections.Count > 0) { var connectionAV = renderer.Connections[0] as AVConnection; - connectionAV.Stop(); + connectionAV.Stop(); + ToggleMuteWindowsAudioDevices(false); } } } @@ -257,9 +260,9 @@ private void streamMenu_RendererSelected(object sender, EventArgs e) { var connectionAV = renderer.Connections[0] as AVConnection; if (rendererItem.Checked) - { + { rendererItem.Checked = false; - connectionAV.Stop(); + connectionAV.Stop(); } else { @@ -269,10 +272,36 @@ private void streamMenu_RendererSelected(object sender, EventArgs e) { connectionAV.Stop(); renderer.CreateConnection(media, DateTime.Now.Ticks); - connectionAV.Play(); + connectionAV.Play(); } } } + } + + public static bool ToggleMuteWindowsAudioDevices(bool muted = false) + { + //Instantiate an Enumerator to find audio devices + NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator(); + //Get all the devices, no matter what condition or status + NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All); + //Loop through all devices + foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol) + { + try + { + //Show us the human understandable name of the device + //System.Diagnostics.Debug.Print(dev.FriendlyName); + if (dev.State == DeviceState.Active) + //[Un]Mute it + dev.AudioEndpointVolume.Mute = muted; + } + catch (Exception ex) + { + //Do something with exception when an audio endpoint could not be muted + return false; + } + } + return true; } private void connectionAV_OnPlayStateChanged(AVConnection sender, AVConnection.PlayState NewState) @@ -285,11 +314,13 @@ private void connectionAV_OnPlayStateChanged(AVConnection sender, AVConnection.P var item = this.streamToMenu.DropDownItems[sender.Parent.UniqueDeviceName] as System.Windows.Forms.ToolStripMenuItem; { if (NewState == AVConnection.PlayState.STOPPED) - { + { + ToggleMuteWindowsAudioDevices(false); item.Checked = false; } else if (NewState == AVConnection.PlayState.PLAYING) - { + { + ToggleMuteWindowsAudioDevices(true); item.Checked = true; } } @@ -308,7 +339,8 @@ private void Application_Exit(object sender, ExitEventArgs e) { this.notifyIcon.Dispose(); } - } + } + ToggleMuteWindowsAudioDevices(false); } } } diff --git a/SWYH/Constants.cs b/SWYH/Constants.cs index 3b66e91..d129b41 100644 --- a/SWYH/Constants.cs +++ b/SWYH/Constants.cs @@ -28,7 +28,7 @@ internal class Constants public const string SWYH_PROCESS_NAME = "SWYH"; public const string RESTART_ARGUMENT_NAME = "--restart"; public const string STREAM_TO_ARGUMENT_NAME = "--streamto:"; - public const int NUMBER_OF_RESTART_TEST = 20; + public const int NUMBER_OF_RESTART_TEST = 30; public const string SWYH_CRASHLOG_FILENAME = "SWYH_crash.log"; public const string REGISTRY_START_SUBKEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; diff --git a/SWYH/SWYH.csproj b/SWYH/SWYH.csproj index 911ef4b..c1f6458 100644 --- a/SWYH/SWYH.csproj +++ b/SWYH/SWYH.csproj @@ -1,253 +1,254 @@ - - - - Debug - x86 - 8.0.30703 - 2.0 - {9F7F5728-3DC1-4FA2-8D4B-F91AA3ABA754} - WinExe - Properties - SWYH - SWYH - v4.0 - - - 512 - {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 4 - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - - - - - - - - - - - x86 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - 512 - - - x86 - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - true - - - Resources\Icons\swyh128.ico - - - false - - - - - - - - - - - False - ..\packages\NAudio.1.7.3\lib\net35\NAudio.dll - - - ..\packages\NAudio.Lame.1.0.2\lib\net20\NAudio.Lame.dll - - - - - - - - - - - - - 4.0 - - - - - - - - MSBuild:Compile - Designer - - - - - - - - HTTPLiveStreamWindow.xaml - - - SettingsWindow.xaml - - - RecordWindow.xaml - - - - - - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - App.xaml - Code - - - AboutWindow.xaml - Code - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - - - Code - - - True - True - Resources.resx - - - True - Settings.settings - True - - - ResXFileCodeGenerator - Resources.Designer.cs - - - SWYH.licenseheader - - - - - SettingsSingleFileGenerator - Settings.Designer.cs - - - - - - - - - - - - - - - - Always - - - Always - - - - - - - False - Microsoft .NET Framework 4 %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - false - - - False - Windows Installer 3.1 - true - - - - - {8ea0e595-45ec-40c1-82ce-269f4fa9ebb3} - UPnPAVCDSML - - - {e01b6019-acc6-40a6-a511-48d285d51c40} - UPnPAV_RendererStack - - - {584ed187-8f2a-4b58-a266-85f3f212b092} - UPnP - - - {b2439738-5f46-4e59-95a1-92c3df38696f} - UPnPAV - - - - - - - + + + + Debug + x86 + 8.0.30703 + 2.0 + {9F7F5728-3DC1-4FA2-8D4B-F91AA3ABA754} + WinExe + Properties + SWYH + SWYH + v4.0 + + + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + + + + + + + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + 512 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + true + + + Resources\Icons\swyh128.ico + + + false + + + + + + + + + + + ..\packages\NAudio.1.8.1\lib\net35\NAudio.dll + True + + + ..\packages\NAudio.Lame.1.0.6\lib\net20\NAudio.Lame.dll + True + + + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + + + + + + HTTPLiveStreamWindow.xaml + + + SettingsWindow.xaml + + + RecordWindow.xaml + + + + + + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + AboutWindow.xaml + Code + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + SWYH.licenseheader + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + + + + + + + + + Always + + + Always + + + + + + + False + Microsoft .NET Framework 4 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + + + {8ea0e595-45ec-40c1-82ce-269f4fa9ebb3} + UPnPAVCDSML + + + {e01b6019-acc6-40a6-a511-48d285d51c40} + UPnPAV_RendererStack + + + {584ed187-8f2a-4b58-a266-85f3f212b092} + UPnP + + + {b2439738-5f46-4e59-95a1-92c3df38696f} + UPnPAV + + + + + + + + --> \ No newline at end of file diff --git a/SWYH/UPnP/SwyhDevice.cs b/SWYH/UPnP/SwyhDevice.cs index d5c880e..70e91f0 100644 --- a/SWYH/UPnP/SwyhDevice.cs +++ b/SWYH/UPnP/SwyhDevice.cs @@ -37,7 +37,8 @@ internal class SwyhDevice { private bool isRunning = true; private byte[] bufferMp3 = new byte[2048]; - private byte[] bufferPcm = new byte[2048]; + private byte[] bufferPcm = new byte[2048]; + private static EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.AutoReset); internal ConcurrentDictionary sessionMp3Streams = new ConcurrentDictionary(); internal ConcurrentDictionary sessionPcmStreams = new ConcurrentDictionary(); @@ -123,7 +124,8 @@ public void Start() public void Stop() { this.isRunning = false; - this.Device.StopDevice(); + this.Device.StopDevice(); + App.ToggleMuteWindowsAudioDevices(false); } private void duplicateMp3AudioStream() @@ -154,7 +156,7 @@ private void duplicatePcmAudioStream() { Parallel.ForEach(sessionPcmStreams.Values, (p, s, l) => { - p.Write(bufferPcm, 0, bytesRead); + p.Write(bufferPcm, 0, bytesRead); }); } else @@ -184,7 +186,7 @@ private void PageHandler(OpenSource.UPnP.UPnPDevice sender, OpenSource.UPnP.HTTP } }; PipeStream stream = sessionMp3Streams.GetOrAdd(WebSession.SessionID, new PipeStream()); - App.CurrentInstance.wasapiProvider.UpdateClientsList(); + App.CurrentInstance.wasapiProvider.UpdateClientsList(); WebSession.SendStreamObject(stream, "audio/mpeg"); } else if (VirtualDir.Equals("/stream", StringComparison.InvariantCultureIgnoreCase) && msg.DirectiveObj.Equals("/swyh.wav", StringComparison.InvariantCultureIgnoreCase)) @@ -194,11 +196,11 @@ private void PageHandler(OpenSource.UPnP.UPnPDevice sender, OpenSource.UPnP.HTTP if (sessionPcmStreams.ContainsKey(s.SessionID)) { PipeStream value; - sessionPcmStreams.TryRemove(s.SessionID, out value); + sessionPcmStreams.TryRemove(s.SessionID, out value); App.CurrentInstance.wasapiProvider.UpdateClientsList(); } - }; - PipeStream stream = sessionPcmStreams.GetOrAdd(WebSession.SessionID, new PipeStream()); + }; + PipeStream stream = sessionPcmStreams.GetOrAdd(WebSession.SessionID, new PipeStream()); App.CurrentInstance.wasapiProvider.UpdateClientsList(); var audioFormat = AudioSettings.GetAudioFormat(); WebSession.SendStreamObject(stream, "audio/L16;rate=" + audioFormat.SampleRate + ";channels=" + audioFormat.Channels); diff --git a/SWYH/packages.config b/SWYH/packages.config index 91f9a13..f043872 100644 --- a/SWYH/packages.config +++ b/SWYH/packages.config @@ -1,5 +1,5 @@ - - - - + + + + \ No newline at end of file