[WIN32][DirectSound] added fake entry default and make sink understand it.
authorwsoltys <wiso@no.way>
Thu, 18 Jul 2013 18:35:08 +0000 (20:35 +0200)
committerRainer Hochecker <fernetmenta@online.de>
Thu, 1 Aug 2013 14:28:33 +0000 (16:28 +0200)
xbmc/cores/AudioEngine/Sinks/AESinkDirectSound.cpp

index 5c5e015..da98e1f 100644 (file)
@@ -35,6 +35,7 @@
 #include <Functiondiscoverykeys_devpkey.h>
 #include <Rpc.h>
 #include "cores/AudioEngine/Utils/AEUtil.h"
+#include "utils/StringUtils.h"
 #pragma comment(lib, "Rpcrt4.lib")
 
 extern HWND g_hWnd;
@@ -143,10 +144,14 @@ bool CAESinkDirectSound::Initialize(AEAudioFormat &format, std::string &device)
   LPGUID deviceGUID = NULL;
   RPC_CSTR wszUuid  = NULL;
   HRESULT hr = E_FAIL;
+  std::string strDeviceGUID = device;
   std::list<DSDevice> DSDeviceList;
   std::string deviceFriendlyName;
   DirectSoundEnumerate(DSEnumCallback, &DSDeviceList);
 
+  if(StringUtils::EndsWith(device, std::string("default")))
+    strDeviceGUID = GetDefaultDevice();
+
   for (std::list<DSDevice>::iterator itt = DSDeviceList.begin(); itt != DSDeviceList.end(); ++itt)
   {
     if ((*itt).lpGuid)
@@ -154,23 +159,27 @@ bool CAESinkDirectSound::Initialize(AEAudioFormat &format, std::string &device)
       hr = (UuidToString((*itt).lpGuid, &wszUuid));
       std::string sztmp = (char*)wszUuid;
       std::string szGUID = "{" + std::string(sztmp.begin(), sztmp.end()) + "}";
-      if (strcasecmp(szGUID.c_str(), device.c_str()) == 0)
+      if (strcasecmp(szGUID.c_str(), strDeviceGUID.c_str()) == 0)
       {
         deviceGUID = (*itt).lpGuid;
         deviceFriendlyName = (*itt).name.c_str();
         break;
       }
     }
-  if (hr == RPC_S_OK) RpcStringFree(&wszUuid);
+    if (hr == RPC_S_OK) RpcStringFree(&wszUuid);
   }
 
   hr = DirectSoundCreate(deviceGUID, &m_pDSound, NULL);
 
   if (FAILED(hr))
   {
-    CLog::Log(LOGERROR, __FUNCTION__": Failed to create the DirectSound device.");
-    CLog::Log(LOGERROR, __FUNCTION__": DSErr: %s", dserr2str(hr));
-    return false;
+    CLog::Log(LOGERROR, __FUNCTION__": Failed to create the DirectSound device %s with error %s, trying the default device.", deviceFriendlyName.c_str(), dserr2str(hr));
+    hr = DirectSoundCreate(NULL, &m_pDSound, NULL);
+    if (FAILED(hr))
+    {
+      CLog::Log(LOGERROR, __FUNCTION__": Failed to create the default DirectSound device with error %s.", dserr2str(hr));
+      return false;
+    }
   }
 
   HWND tmp_hWnd;
@@ -501,6 +510,8 @@ void CAESinkDirectSound::EnumerateDevicesEx(AEDeviceInfoList &deviceInfoList, bo
 
   HRESULT                hr;
 
+  std::string strDD = GetDefaultDevice();
+
   /* See if we are on Windows XP */
   if (!g_sysinfo.IsWindowsVersionAtLeast(CSysInfo::WindowsVersionVista))
   {
@@ -535,6 +546,15 @@ void CAESinkDirectSound::EnumerateDevicesEx(AEDeviceInfoList &deviceInfoList, bo
       deviceInfo.m_sampleRates.push_back((DWORD) 96000);
 
       deviceInfoList.push_back(deviceInfo);
+
+      // add the default device with m_deviceName = default
+      if(strDD == deviceInfo.m_deviceName)
+      {
+        deviceInfo.m_deviceName = std::string("default");
+        deviceInfo.m_displayName = std::string("default");
+        deviceInfo.m_displayNameExtra = std::string("");
+        deviceInfoList.push_back(deviceInfo);
+      }
     }
 
     RpcStringFree(&cszGUID);
@@ -650,22 +670,14 @@ void CAESinkDirectSound::EnumerateDevicesEx(AEDeviceInfoList &deviceInfoList, bo
     deviceInfo.m_deviceType       = aeDeviceType;
 
     deviceInfoList.push_back(deviceInfo);
-  }
 
-  // since AE takes the first device in deviceInfoList as default audio device we need
-  // to sort it in order to use the real default device
-  if(deviceInfoList.size() > 1)
-  {
-    std::string strDD = GetDefaultDevice();
-    for (AEDeviceInfoList::iterator itt = deviceInfoList.begin(); itt != deviceInfoList.end(); ++itt)
+    // add the default device with m_deviceName = default
+    if(strDD == strDevName)
     {
-      CAEDeviceInfo devInfo = *itt;
-      if(devInfo.m_deviceName == strDD)
-      {
-        deviceInfoList.erase(itt);
-        deviceInfoList.insert(deviceInfoList.begin(), devInfo);
-        break;
-      }
+      deviceInfo.m_deviceName = std::string("default");
+      deviceInfo.m_displayName = std::string("default");
+      deviceInfo.m_displayNameExtra = std::string("");
+      deviceInfoList.push_back(deviceInfo);
     }
   }