// \brief Create a new device by type (DEFAULT_DEVICE, DIRECTSOUND_DEVICE, AC97_DEVICE) void CAudioContext::SetActiveDevice(int iDevice) { CStdString strAudioDev; if(!m_bAudio2) { strAudioDev = g_guiSettings.GetString("audiooutput.audiodevice"); } else { strAudioDev = g_guiSettings.GetString("audiooutput2.audiodevice"); } /* if device is the same, no need to bother */ #ifdef _WIN32 HRESULT hr; int iPos = strAudioDev.Find(':'); if(iPos != CStdString::npos) strAudioDev.erase(0, iPos+1); if (iDevice == DEFAULT_DEVICE) iDevice = DIRECTSOUND_DEVICE; // default device on win32 is directsound device if(m_iDevice == iDevice && strAudioDev.Equals(m_strDevice)) { if (iDevice != NONE && m_pDirectSoundDevice) { DSCAPS devCaps = {0}; devCaps.dwSize = sizeof(devCaps); HRESULT hr = m_pDirectSoundDevice->GetCaps(&devCaps); if (SUCCEEDED(hr)) // Make sure the DirectSound interface is still valid. return; CLog::Log(LOGDEBUG, "%s - DirectSoundDevice no longer valid and is going to be recreated (0x%08x)", __FUNCTION__, hr); } } #else if(m_iDevice == iDevice) return; #endif CLog::Log(LOGDEBUG, "%s - SetActiveDevice from %i to %i", __FUNCTION__, m_iDevice, iDevice); /* deinit current device */ RemoveActiveDevice(); m_iDevice=iDevice; m_strDevice=strAudioDev; #ifdef HAS_AUDIO memset(&g_digitaldevice, 0, sizeof(GUID)); hr = DirectSoundEnumerate(DSEnumCallback, this); if (FAILED(hr)) CLog::Log(LOGERROR, "%s - failed to enumerate output devices (0x%08X)", __FUNCTION__, hr); if (iDevice==DIRECTSOUND_DEVICE || iDevice==DIRECTSOUND_DEVICE_DIGITAL) { LPGUID guid = NULL; #ifdef _WIN32 CWDSound p_dsound; std::vector<DSDeviceInfo > deviceList = p_dsound.GetSoundDevices(); if (deviceList.size() == 0) CLog::Log(LOGDEBUG, "%s - no output devices found.", __FUNCTION__); std::vector<DSDeviceInfo >::const_iterator iter = deviceList.begin(); for (int i=0; iter != deviceList.end(); i++) { DSDeviceInfo dev = *iter; if (strAudioDev.Equals(dev.strDescription)) { guid = dev.lpGuid; CLog::Log(LOGDEBUG, "%s - selecting %s as output devices", __FUNCTION__, dev.strDescription.c_str()); break; } ++iter; } if (guid == NULL) CLog::Log(LOGDEBUG, "%s - (default playback device).", __FUNCTION__); #else if(iDevice == DIRECTSOUND_DEVICE_DIGITAL && ( g_digitaldevice.Data1 || g_digitaldevice.Data2 || g_digitaldevice.Data3 || g_digitaldevice.Data4 )) guid = &g_digitaldevice; #endif // Create DirectSound hr = DirectSoundCreate( guid, &m_pDirectSoundDevice, NULL ); if (FAILED(hr)) { CLog::Log(LOGERROR, "DirectSoundCreate() Failed (0x%08X)", hr); return; } hr = m_pDirectSoundDevice->SetCooperativeLevel(g_hWnd, DSSCL_PRIORITY); if (FAILED(hr)) { CLog::Log(LOGERROR, "DirectSoundDevice::SetCooperativeLevel() Failed (0x%08X)", hr); return; } } else if (iDevice == DIRECTSOUND_DEVICE_DIGITAL) { } #ifdef HAS_AUDIO_PASS_THROUGH else if (iDevice==AC97_DEVICE) { // Create AC97 Device if (FAILED(Ac97CreateMediaObject(DSAC97_CHANNEL_DIGITAL, NULL, NULL, &m_pAC97Device))) { CLog::Log(LOGERROR, "Failed to create digital Ac97CreateMediaObject()"); return; } } #endif // Don't log an error if the caller specifically asked for no device // externalplayer does this to ensure all audio devices are closed // during external playback else if (iDevice != NONE) { CLog::Log(LOGERROR, "Failed to create audio device"); return; } #endif if (!m_bAudio2) { g_audioManager.Initialize(m_iDevice); } }
// \brief Create a new device by type (DEFAULT_DEVICE, DIRECTSOUND_DEVICE, AC97_DEVICE) // Note: DEFAULT_DEVICE is created by the IAudioDeviceChangedCallback void CAudioContext::SetActiveDevice(int iDevice) { /* if device is the same, no need to bother */ #ifdef _WIN32 if (iDevice == DEFAULT_DEVICE) iDevice = DIRECTSOUND_DEVICE; // default device on win32 is directsound device if(m_iDevice == iDevice && g_guiSettings.GetString("audiooutput.audiodevice").Equals(m_strDevice)) { if (iDevice != NONE && m_pDirectSoundDevice) { DSCAPS devCaps = {0}; devCaps.dwSize = sizeof(devCaps); if (SUCCEEDED(m_pDirectSoundDevice->GetCaps(&devCaps))) // Make sure the DirectSound interface is still valid. return; } } #else if(m_iDevice == iDevice) return; #endif if (iDevice==DEFAULT_DEVICE) { /* we just tell callbacks to init, it will setup audio */ g_audioManager.Initialize(iDevice); return; } /* deinit current device */ RemoveActiveDevice(); m_iDevice=iDevice; if (g_guiSettings.HasSetting("audiooutput.audiodevice")) { m_strDevice=g_guiSettings.GetString("audiooutput.audiodevice"); } else { m_strDevice="audiodevice"; } #ifdef HAS_AUDIO memset(&g_digitaldevice, 0, sizeof(GUID)); if (FAILED(DirectSoundEnumerate(DSEnumCallback, this))) CLog::Log(LOGERROR, "%s - failed to enumerate output devices", __FUNCTION__); if (iDevice==DIRECTSOUND_DEVICE || iDevice==DIRECTSOUND_DEVICE_DIGITAL) { LPGUID guid = NULL; #ifdef _WIN32 CWDSound p_dsound; std::vector<DSDeviceInfo > deviceList = p_dsound.GetSoundDevices(); std::vector<DSDeviceInfo >::const_iterator iter = deviceList.begin(); for (int i=0; iter != deviceList.end(); i++) { DSDeviceInfo dev = *iter; if (g_guiSettings.GetString("audiooutput.audiodevice").Equals(dev.strDescription)) { guid = dev.lpGuid; CLog::Log(LOGDEBUG, "%s - selecting %s as output devices", __FUNCTION__, dev.strDescription.c_str()); break; } ++iter; } #else if(iDevice == DIRECTSOUND_DEVICE_DIGITAL && ( g_digitaldevice.Data1 || g_digitaldevice.Data2 || g_digitaldevice.Data3 || g_digitaldevice.Data4 )) guid = &g_digitaldevice; #endif #ifdef _WIN32 //If default device was already created, use it (default device on win32 is directsound device) if (iDevice == DIRECTSOUND_DEVICE && m_pDefaultDirectSoundDevice != NULL) { m_pDirectSoundDevice = m_pDefaultDirectSoundDevice; m_pDirectSoundDevice->AddRef(); } #endif // Create DirectSound if (m_pDirectSoundDevice == NULL && FAILED(DirectSoundCreate( guid, &m_pDirectSoundDevice, NULL ))) { CLog::Log(LOGERROR, "DirectSoundCreate() Failed"); return; } #ifdef _WIN32 //If default device (default device on win32 is directsound device), save it if (iDevice == DIRECTSOUND_DEVICE && m_pDefaultDirectSoundDevice == NULL) { m_pDefaultDirectSoundDevice = m_pDirectSoundDevice; m_pDefaultDirectSoundDevice->AddRef(); } #endif #ifndef _XBOX if (FAILED(m_pDirectSoundDevice->SetCooperativeLevel(g_hWnd, DSSCL_PRIORITY))) { CLog::Log(LOGERROR, "DirectSoundDevice::SetCooperativeLevel() Failed"); return; } #endif } else if (iDevice == DIRECTSOUND_DEVICE_DIGITAL) { } #ifdef HAS_AUDIO_PASS_THROUGH else if (iDevice==AC97_DEVICE) { // Create AC97 Device if (FAILED(Ac97CreateMediaObject(DSAC97_CHANNEL_DIGITAL, NULL, NULL, &m_pAC97Device))) { CLog::Log(LOGERROR, "Failed to create digital Ac97CreateMediaObject()"); return; } } #endif // Don't log an error if the caller specifically asked for no device // externalplayer does this to ensure all audio devices are closed // during external playback else if (iDevice != NONE) { CLog::Log(LOGERROR, "Failed to create audio device"); return; } #endif g_audioManager.Initialize(m_iDevice); }