This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
/
AudioRendererFactory.cpp
215 lines (193 loc) · 6.7 KB
/
AudioRendererFactory.cpp
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
/*
* 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 "AudioRendererFactory.h"
#include "settings/GUISettings.h"
#include "utils/log.h"
#include "NullDirectSound.h"
#ifdef HAS_PULSEAUDIO
#include "PulseAudioDirectSound.h"
#endif
#ifdef _WIN32
#include "Win32WASAPI.h"
#include "Win32DirectSound.h"
#endif
#if defined(__APPLE__)
#if defined(__arm__)
#include "IOSAudioRenderer.h"
#else
#include "CoreAudioRenderer.h"
#endif
#elif defined(USE_ALSA)
#include "ALSADirectSound.h"
#endif
#define ReturnOnValidInitialize(rendererName) \
{ \
if (audioSink->Initialize(pCallback, device, iChannels, channelMap, uiSamplesPerSec, uiBitsPerSample, bResample, bIsMusic, bPassthrough)) \
{ \
CLog::Log(LOGDEBUG, "%s::Initialize" \
" - Channels: %i" \
" - SampleRate: %i" \
" - SampleBit: %i" \
" - Resample %s" \
" - IsMusic %s" \
" - IsPassthrough %s" \
" - audioDevice: %s", \
rendererName, \
iChannels, \
uiSamplesPerSec, \
uiBitsPerSample, \
bResample ? "true" : "false", \
bIsMusic ? "true" : "false", \
bPassthrough ? "true" : "false", \
device.c_str() \
); \
return audioSink; \
} \
else \
{ \
audioSink->Deinitialize(); \
delete audioSink; \
audioSink = NULL; \
} \
}
#define CreateAndReturnOnValidInitialize(rendererClass) \
{ \
audioSink = new rendererClass(); \
ReturnOnValidInitialize(#rendererClass); \
}
#define ReturnNewRenderer(rendererClass) \
{ \
renderer = #rendererClass; \
return new rendererClass(); \
}
IAudioRenderer* CAudioRendererFactory::Create(IAudioCallback* pCallback, int iChannels, enum PCMChannels *channelMap, unsigned int uiSamplesPerSec, unsigned int uiBitsPerSample, bool bResample, bool bIsMusic, bool bPassthrough)
{
IAudioRenderer* audioSink = NULL;
CStdString renderer;
CStdString deviceString, device;
if (bPassthrough)
{
#if defined(_LINUX) && !defined(__APPLE__)
deviceString = g_guiSettings.GetString("audiooutput.passthroughdevice");
if (deviceString.Equals("custom"))
deviceString = g_guiSettings.GetString("audiooutput.custompassthrough");
#else
// osx/win platforms do not have an "audiooutput.passthroughdevice" setting but can do passthrough
deviceString = g_guiSettings.GetString("audiooutput.audiodevice");
#endif
}
else
{
deviceString = g_guiSettings.GetString("audiooutput.audiodevice");
if (deviceString.Equals("custom"))
deviceString = g_guiSettings.GetString("audiooutput.customdevice");
}
int iPos = deviceString.Find(":");
if (iPos > 0)
{
audioSink = CreateFromUri(deviceString.Left(iPos), renderer);
if (audioSink)
{
device = deviceString.Right(deviceString.length() - iPos - 1);
ReturnOnValidInitialize(renderer.c_str());
#ifdef _WIN32
//If WASAPI failed try DirectSound.
if(deviceString.Left(iPos).Equals("wasapi"))
{
audioSink = CreateFromUri("directsound", renderer);
ReturnOnValidInitialize(renderer.c_str());
}
#endif
CreateAndReturnOnValidInitialize(CNullDirectSound);
/* should never get here */
assert(false);
}
}
CLog::Log(LOGINFO, "AudioRendererFactory: %s not a explicit device, trying to autodetect.", device.c_str());
device = deviceString;
/* First pass creation */
#ifdef HAS_PULSEAUDIO
CreateAndReturnOnValidInitialize(CPulseAudioDirectSound);
#endif
/* incase none in the first pass was able to be created, fall back to os specific */
#ifdef WIN32
CreateAndReturnOnValidInitialize(CWin32DirectSound);
#endif
#if defined(__APPLE__)
#if defined(__arm__)
CreateAndReturnOnValidInitialize(CIOSAudioRenderer);
#else
CreateAndReturnOnValidInitialize(CCoreAudioRenderer);
#endif
#elif defined(USE_ALSA)
CreateAndReturnOnValidInitialize(CALSADirectSound);
#endif
CreateAndReturnOnValidInitialize(CNullDirectSound);
/* should never get here */
assert(false);
return NULL;
}
void CAudioRendererFactory::EnumerateAudioSinks(AudioSinkList& vAudioSinks, bool passthrough)
{
#ifdef HAS_PULSEAUDIO
CPulseAudioDirectSound::EnumerateAudioSinks(vAudioSinks, passthrough);
#endif
#ifdef WIN32
CWin32DirectSound::EnumerateAudioSinks(vAudioSinks, passthrough);
CWin32WASAPI::EnumerateAudioSinks(vAudioSinks, passthrough);
#endif
#if defined(__APPLE__)
#if !defined(__arm__)
CCoreAudioRenderer::EnumerateAudioSinks(vAudioSinks, passthrough);
#endif
#elif defined(USE_ALSA)
CALSADirectSound::EnumerateAudioSinks(vAudioSinks, passthrough);
#endif
}
IAudioRenderer *CAudioRendererFactory::CreateFromUri(const CStdString &soundsystem, CStdString &renderer)
{
#ifdef HAS_PULSEAUDIO
if (soundsystem.Equals("pulse"))
ReturnNewRenderer(CPulseAudioDirectSound);
#endif
#ifdef WIN32
if (soundsystem.Equals("wasapi"))
ReturnNewRenderer(CWin32WASAPI)
else if (soundsystem.Equals("directsound"))
ReturnNewRenderer(CWin32DirectSound);
#endif
#if defined(__APPLE__)
#if defined(__arm__)
if (soundsystem.Equals("ioscoreaudio"))
ReturnNewRenderer(CIOSAudioRenderer);
#else
if (soundsystem.Equals("coreaudio"))
ReturnNewRenderer(CCoreAudioRenderer);
#endif
#elif defined(USE_ALSA)
if (soundsystem.Equals("alsa"))
ReturnNewRenderer(CALSADirectSound);
#endif
if (soundsystem.Equals("null"))
ReturnNewRenderer(CNullDirectSound);
return NULL;
}