From 9ecd7f803b0d1e5afe9de3a09b51be48428288f3 Mon Sep 17 00:00:00 2001
From: sebastiii <sebastiii@team-mediaportal.com>
Date: Sat, 11 Jul 2015 15:17:58 +0200
Subject: [PATCH] Master : Rework audio device detection to always have an
 available audio device when a new one is added or removed. (based on
 mm1352000 idea and code)

---
 mediaportal/Core/DShowNET/Helper/DirectShowUtil.cs |  2 +-
 mediaportal/Core/Player/VolumeHandler.cs           |  4 +--
 mediaportal/Core/guilib/GraphicContext.cs          | 16 ++++-----
 mediaportal/MediaPortal.Application/MediaPortal.cs | 39 ++++++++++++++++++++--
 4 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/mediaportal/Core/DShowNET/Helper/DirectShowUtil.cs b/mediaportal/Core/DShowNET/Helper/DirectShowUtil.cs
index 7e73290..45a0856 100644
--- a/mediaportal/Core/DShowNET/Helper/DirectShowUtil.cs
+++ b/mediaportal/Core/DShowNET/Helper/DirectShowUtil.cs
@@ -945,7 +945,7 @@ namespace DShowNET.Helper
                       VolumeHandler.Dispose();
                       GUIGraphicsContext.VolumeHandler = VolumeHandler.Instance;
                     }
-                    if (GUIGraphicsContext.VolumeHandler != null && GUIGraphicsContext.VolumeHandler.Volume != 19660500 && GUIGraphicsContext.DeviceAudioConnected)
+                    if (GUIGraphicsContext.VolumeHandler != null && GUIGraphicsContext.VolumeHandler.Volume != 19660500 && GUIGraphicsContext.DeviceAudioConnected > 0)
                     {
                       Log.Debug("DirectShowUtil: volume handler value {0}", GUIGraphicsContext.VolumeHandler.Volume);
                       Log.Debug("DirectShowUtil: build the graph for PIN : {0}", pinName);
diff --git a/mediaportal/Core/Player/VolumeHandler.cs b/mediaportal/Core/Player/VolumeHandler.cs
index 968e33c..1d1d2c5 100644
--- a/mediaportal/Core/Player/VolumeHandler.cs
+++ b/mediaportal/Core/Player/VolumeHandler.cs
@@ -42,7 +42,7 @@ namespace MediaPortal.Player
 
     public VolumeHandler(int[] volumeTable)
     {
-      if (GUIGraphicsContext.DeviceAudioConnected)
+      if (GUIGraphicsContext.DeviceAudioConnected > 0)
       {
         bool isDigital;
 
@@ -93,7 +93,7 @@ namespace MediaPortal.Player
 
     private static VolumeHandler CreateInstance()
     {
-      if (GUIGraphicsContext.DeviceAudioConnected)
+      if (GUIGraphicsContext.DeviceAudioConnected > 0)
       {
         using (Settings reader = new MPSettings())
         {
diff --git a/mediaportal/Core/guilib/GraphicContext.cs b/mediaportal/Core/guilib/GraphicContext.cs
index 9e292cf..4cfdc7c 100644
--- a/mediaportal/Core/guilib/GraphicContext.cs
+++ b/mediaportal/Core/guilib/GraphicContext.cs
@@ -130,7 +130,7 @@ namespace MediaPortal.GUI.Library
     private static float _currentFPS;
     private static long _lasttime;
     private static bool _blankScreen;
-    private static bool _deviceAudioConnected;
+    private static int _deviceAudioConnected = 0;
     private static VolumeHandler _initVolumeHandler;
     private static bool _deviceVideoConnected;
     private static bool _idleTimePowerSaving;
@@ -253,20 +253,20 @@ namespace MediaPortal.GUI.Library
     /// <summary>
     /// Set/get audio device connected or removed
     /// </summary>
-    public static bool DeviceAudioConnected
+    public static int DeviceAudioConnected
     {
       get { return _deviceAudioConnected; }
       set
       {
-        if (value == false)
+        if (value > _deviceAudioConnected)
         {
-          _deviceAudioConnected = false;
-          Log.Debug("GraphicContext: device audio removed");
+          _deviceAudioConnected = value;
+          Log.Debug("GraphicContext: device audio connected - Count {0}", _deviceAudioConnected);
         }
-        else
+        else if (value < _deviceAudioConnected)
         {
-          _deviceAudioConnected = true;
-          Log.Debug("GraphicContext: device audio connected");
+          _deviceAudioConnected = value < 0 ? 0 : value;
+          Log.Debug("GraphicContext: device audio removed - Count {0}", _deviceAudioConnected);
         }
       }
     }
diff --git a/mediaportal/MediaPortal.Application/MediaPortal.cs b/mediaportal/MediaPortal.Application/MediaPortal.cs
index 790fb22..684ae62 100644
--- a/mediaportal/MediaPortal.Application/MediaPortal.cs
+++ b/mediaportal/MediaPortal.Application/MediaPortal.cs
@@ -18,6 +18,8 @@
 
 #endregion
 
+using DirectShowLib;
+
 #region usings
 
 using System;
@@ -1138,7 +1140,7 @@ public class MediaPortalApp : D3D, IRender
     GUIGraphicsContext.graphics = null;
     GUIGraphicsContext.RenderGUI = this;
     GUIGraphicsContext.Render3DMode = GUIGraphicsContext.eRender3DMode.None;
-    GUIGraphicsContext.DeviceAudioConnected = true;
+    GUIGraphicsContext.DeviceAudioConnected = 0;
     GUIGraphicsContext.DeviceVideoConnected = true;
 
     using (Settings xmlreader = new MPSettings())
@@ -2076,7 +2078,7 @@ public class MediaPortalApp : D3D, IRender
               Log.Info("Main: Audio Renderer {0} removed", deviceName);
               try
               {
-                GUIGraphicsContext.DeviceAudioConnected = false;
+                GUIGraphicsContext.DeviceAudioConnected = GUIGraphicsContext.DeviceAudioConnected - 1;
                 if (_stopOnLostAudioRenderer)
                 {
                   g_Player.Stop();
@@ -2096,7 +2098,7 @@ public class MediaPortalApp : D3D, IRender
               Log.Info("Main: Audio Renderer {0} connected", deviceName);
               try
               {
-                GUIGraphicsContext.DeviceAudioConnected = true;
+                GUIGraphicsContext.DeviceAudioConnected = GUIGraphicsContext.DeviceAudioConnected + 1;
                 if (_stopOnLostAudioRenderer)
                 {
                   g_Player.Stop();
@@ -2888,6 +2890,37 @@ public class MediaPortalApp : D3D, IRender
     Log.Debug("Main: Auto play start listening");
     AutoPlay.StartListening();
 
+    GUIGraphicsContext.DeviceAudioConnected = 0;
+    DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSAudio);    // KSCATEGORY_AUDIO
+    if (devices != null)
+    {
+      GUIGraphicsContext.DeviceAudioConnected += devices.Length;
+      foreach (DsDevice d in devices)
+      {
+        d.Dispose();
+      }
+    }
+    devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSRender);    // KSCATEGORY_RENDER
+    if (devices != null)
+    {
+      GUIGraphicsContext.DeviceAudioConnected += devices.Length;
+      foreach (DsDevice d in devices)
+      {
+        d.Dispose();
+      }
+    }
+    devices = DsDevice.GetDevicesOfCat(RDP_REMOTE_AUDIO);
+    if (devices != null)
+    {
+      GUIGraphicsContext.DeviceAudioConnected += devices.Length;
+      foreach (DsDevice d in devices)
+      {
+        d.Dispose();
+      }
+    }
+
+    Log.Debug("Main: audio renderer count at startup = {0}", GUIGraphicsContext.DeviceAudioConnected);
+
     Log.Info("Main: Initializing volume handler");
     #pragma warning disable 168
     GUIGraphicsContext.VolumeHandler = VolumeHandler.Instance;
-- 
1.9.5.msysgit.0

