1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "system.h"
#include "AudioContext.h"
#include "GUIAudioManager.h"
#include "Settings.h"
#include "GUISettings.h"
#include "SystemGlobals.h"
#ifdef _WIN32
#include "WINDirectSound.h"
#endif
extern HWND g_hWnd;
CAudioContext& g_audioContext = g_SystemGlobals.m_audioContext;
#ifdef _WIN32
static GUID g_digitaldevice;
BOOL CALLBACK DSEnumCallback(
LPGUID lpGuid,
LPCSTR lpcstrDescription,
LPCSTR lpcstrModule,
LPVOID lpContext
)
{
if(strstr(lpcstrDescription, "Digital Output") != NULL)
{
g_digitaldevice = *lpGuid;
return false;
}
return true;
}
#endif
CAudioContext::CAudioContext()
{
m_bAC3EncoderActive=false;
m_iDevice=DEFAULT_DEVICE;
m_strDevice.clear();
#ifdef HAS_AUDIO
#ifdef HAS_AUDIO_PASS_THROUGH
m_pAC97Device=NULL;
#endif
m_pDirectSoundDevice=NULL;
#endif
}
CAudioContext::~CAudioContext()
{
}
// \brief Create a new device by type (DEFAULT_DEVICE, DIRECTSOUND_DEVICE, AC97_DEVICE)
void CAudioContext::SetActiveDevice(int iDevice)
{
CStdString strAudioDev = g_guiSettings.GetString("audiooutput.audiodevice");
/* if device is the same, no need to bother */
#ifdef _WIN32
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));
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 (strAudioDev.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
// Create DirectSound
if (FAILED(DirectSoundCreate( guid, &m_pDirectSoundDevice, NULL )))
{
CLog::Log(LOGERROR, "DirectSoundCreate() Failed");
return;
}
if (FAILED(m_pDirectSoundDevice->SetCooperativeLevel(g_hWnd, DSSCL_PRIORITY)))
{
CLog::Log(LOGERROR, "DirectSoundDevice::SetCooperativeLevel() Failed");
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
g_audioManager.Initialize(m_iDevice);
}
// \brief Return the active device type (NONE, DEFAULT_DEVICE, DIRECTSOUND_DEVICE, AC97_DEVICE)
int CAudioContext::GetActiveDevice()
{
return m_iDevice;
}
// \brief Remove the current sound device, eg. to setup new speaker config
void CAudioContext::RemoveActiveDevice()
{
CLog::Log(LOGDEBUG, "%s - Removing device %i", __FUNCTION__, m_iDevice);
g_audioManager.DeInitialize(m_iDevice);
m_iDevice=NONE;
#ifdef HAS_AUDIO
#ifdef HAS_AUDIO_PASS_THROUGH
SAFE_RELEASE(m_pAC97Device);
#endif
SAFE_RELEASE(m_pDirectSoundDevice);
#endif
}
// \brief set a new speaker config
void CAudioContext::SetupSpeakerConfig(int iChannels, bool& bAudioOnAllSpeakers, bool bIsMusic)
{
m_bAC3EncoderActive = false;
bAudioOnAllSpeakers = false;
#ifdef HAS_AUDIO
return; //not implemented
DWORD spconfig = DSSPEAKER_USE_DEFAULT;
if (AUDIO_IS_BITSTREAM(g_guiSettings.GetInt("audiooutput.mode")))
{
if (g_settings.m_currentVideoSettings.m_OutputToAllSpeakers && !bIsMusic)
{
bAudioOnAllSpeakers = true;
m_bAC3EncoderActive = true;
spconfig = DSSPEAKER_USE_DEFAULT; //Allows ac3 encoder should it be enabled
}
else
{
if (iChannels == 1)
spconfig = DSSPEAKER_MONO;
else if (iChannels == 2)
spconfig = DSSPEAKER_STEREO;
else
spconfig = DSSPEAKER_USE_DEFAULT; //Allows ac3 encoder should it be enabled
}
}
else // We don't want to use the Dolby Digital Encoder output. Downmix to surround instead.
{
if (iChannels == 1)
spconfig = DSSPEAKER_MONO;
else
{
// check if surround mode is allowed, if not then use normal stereo
// don't always set it to default as that enabled ac3 encoder if that is allowed in dash
// ruining quality
spconfig = DSSPEAKER_STEREO;
}
}
DWORD spconfig_old = DSSPEAKER_USE_DEFAULT;
if(m_pDirectSoundDevice)
{
m_pDirectSoundDevice->GetSpeakerConfig(&spconfig_old);
spconfig_old = DSSPEAKER_CONFIG(spconfig_old);
}
/* speaker config identical, no need to do anything */
if(spconfig == spconfig_old) return;
CLog::Log(LOGDEBUG, "%s - Speakerconfig changed from %i to %i", __FUNCTION__, spconfig_old, spconfig);
#endif
/* speaker config has changed, caller need to recreate it */
RemoveActiveDevice();
}
bool CAudioContext::IsAC3EncoderActive() const
{
return m_bAC3EncoderActive;
}
bool CAudioContext::IsPassthroughActive() const
{
return (m_iDevice == DIRECTSOUND_DEVICE_DIGITAL);
}
|