[r35737]: branches / AudioEngine / xbmc / cores / AudioEngine / Engines / PulseAE.cpp  Maximize  Restore  History

Download this file

229 lines (200 with data), 5.8 kB

  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
/*
* Copyright (C) 2005-2010 Team XBMC
* http://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"
#ifdef HAS_PULSEAUDIO
#include "PulseAE.h"
#include "PulseAEStream.h"
#include "PulseAESound.h"
#include "utils/SingleLock.h"
#include "log.h"
#include "Settings.h"
#include <pulse/pulseaudio.h>
/* Static helpers */
static const char *ContextStateToString(pa_context_state s)
{
switch (s)
{
case PA_CONTEXT_UNCONNECTED:
return "unconnected";
case PA_CONTEXT_CONNECTING:
return "connecting";
case PA_CONTEXT_AUTHORIZING:
return "authorizing";
case PA_CONTEXT_SETTING_NAME:
return "setting name";
case PA_CONTEXT_READY:
return "ready";
case PA_CONTEXT_FAILED:
return "failed";
case PA_CONTEXT_TERMINATED:
return "terminated";
default:
return "none";
}
}
static const char *StreamStateToString(pa_stream_state s)
{
switch(s)
{
case PA_STREAM_UNCONNECTED:
return "unconnected";
case PA_STREAM_CREATING:
return "creating";
case PA_STREAM_READY:
return "ready";
case PA_STREAM_FAILED:
return "failed";
case PA_STREAM_TERMINATED:
return "terminated";
default:
return "none";
}
}
CPulseAE::CPulseAE()
{
m_Context = NULL;
m_MainLoop = NULL;
}
CPulseAE::~CPulseAE()
{
if (m_MainLoop)
pa_threaded_mainloop_stop(m_MainLoop);
if (m_Context)
{
pa_context_disconnect(m_Context);
pa_context_unref(m_Context);
m_Context = NULL;
}
}
bool CPulseAE::Initialize()
{
m_Volume = g_settings.m_fVolumeLevel;
if ((m_MainLoop = pa_threaded_mainloop_new()) == NULL)
{
CLog::Log(LOGERROR, "PulseAudio: Failed to allocate main loop");
return false;
}
if ((m_Context = pa_context_new(pa_threaded_mainloop_get_api(m_MainLoop), "XBMC")) == NULL)
{
CLog::Log(LOGERROR, "PulseAudio: Failed to allocate context");
return false;
}
pa_context_set_state_callback(m_Context, ContextStateCallback, m_MainLoop);
if (pa_context_connect(m_Context, NULL, (pa_context_flags_t)0, NULL) < 0)
{
CLog::Log(LOGERROR, "PulseAudio: Failed to connect context");
return false;
}
pa_threaded_mainloop_lock(m_MainLoop);
if (pa_threaded_mainloop_start(m_MainLoop) < 0)
{
CLog::Log(LOGERROR, "PulseAudio: Failed to start MainLoop");
pa_threaded_mainloop_unlock(m_MainLoop);
return false;
}
/* Wait until the context is ready */
do
{
pa_threaded_mainloop_wait(m_MainLoop);
CLog::Log(LOGDEBUG, "PulseAudio: Context %s", ContextStateToString(pa_context_get_state(m_Context)));
}
while (pa_context_get_state(m_Context) != PA_CONTEXT_READY && pa_context_get_state(m_Context) != PA_CONTEXT_FAILED);
if (pa_context_get_state(m_Context) == PA_CONTEXT_FAILED)
{
CLog::Log(LOGERROR, "PulseAudio: Waited for the Context but it failed");
pa_threaded_mainloop_unlock(m_MainLoop);
return false;
}
pa_threaded_mainloop_unlock(m_MainLoop);
return true;
}
void CPulseAE::OnSettingsChange(CStdString setting)
{
}
float CPulseAE::GetVolume()
{
return m_Volume;
}
void CPulseAE::SetVolume(float volume)
{
CSingleLock lock(m_lock);
m_Volume = volume;
std::list<CPulseAEStream*>::iterator itt;
for(itt = m_streams.begin(); itt != m_streams.end(); ++itt)
(*itt)->UpdateVolume(volume);
}
IAEStream *CPulseAE::GetStream(enum AEDataFormat dataFormat, unsigned int sampleRate, unsigned int channelCount, AEChLayout channelLayout, unsigned int options)
{
CPulseAEStream *st = new CPulseAEStream(m_Context, m_MainLoop, dataFormat, sampleRate, channelCount, channelLayout, options);
CSingleLock lock(m_lock);
m_streams.push_back(st);
return st;
}
IAEStream *CPulseAE::AlterStream(IAEStream *stream, enum AEDataFormat dataFormat, unsigned int sampleRate, unsigned int channelCount, AEChLayout channelLayout, unsigned int options)
{
/* FIXME: Pulse can alter a stream I think */
((CPulseAEStream*)stream)->SetFreeOnDrain();
stream->Drain();
return GetStream(dataFormat, sampleRate, channelCount, channelLayout, options);
}
IAESound *CPulseAE::GetSound(CStdString file)
{
return new CPulseAESound(file);
}
void CPulseAE::FreeSound(IAESound *sound)
{
delete (CPulseAESound*)sound;
}
void CPulseAE::GarbageCollect()
{
CSingleLock lock(m_lock);
std::list<CPulseAEStream*>::iterator itt;
for(itt = m_streams.begin(); itt != m_streams.end();)
{
if ((*itt)->IsDestroyed())
{
delete (*itt);
itt = m_streams.erase(itt);
continue;
}
++itt;
}
}
void CPulseAE::EnumerateOutputDevices(AEDeviceList &devices, bool passthrough)
{
}
void CPulseAE::ContextStateCallback(pa_context *c, void *userdata)
{
pa_threaded_mainloop *m = (pa_threaded_mainloop *)userdata;
switch (pa_context_get_state(c))
{
case PA_CONTEXT_READY:
case PA_CONTEXT_TERMINATED:
case PA_CONTEXT_UNCONNECTED:
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
case PA_CONTEXT_SETTING_NAME:
case PA_CONTEXT_FAILED:
pa_threaded_mainloop_signal(m, 0);
break;
}
}
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks