author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/* This file is part of the KDE project. |
2 |
||
3 |
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
||
5 |
This library is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Lesser General Public License as published by |
|
7 |
the Free Software Foundation, either version 2.1 or 3 of the License. |
|
8 |
||
9 |
This library is distributed in the hope that it will be useful, |
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
GNU Lesser General Public License for more details. |
|
13 |
||
14 |
You should have received a copy of the GNU Lesser General Public License |
|
15 |
along with this library. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
18 |
#include <gst/interfaces/propertyprobe.h> |
|
19 |
#include "devicemanager.h" |
|
20 |
#include "backend.h" |
|
21 |
#include "gsthelper.h" |
|
22 |
#include "videowidget.h" |
|
23 |
#include "glrenderer.h" |
|
24 |
#include "widgetrenderer.h" |
|
25 |
#include "x11renderer.h" |
|
26 |
#include "artssink.h" |
|
27 |
||
28 |
#ifdef USE_ALSASINK2 |
|
29 |
#include "alsasink2.h" |
|
30 |
#endif |
|
31 |
||
32 |
/* |
|
33 |
* This class manages the list of currently |
|
34 |
* active output devices |
|
35 |
*/ |
|
36 |
||
37 |
QT_BEGIN_NAMESPACE |
|
38 |
||
39 |
namespace Phonon |
|
40 |
{ |
|
41 |
namespace Gstreamer |
|
42 |
{ |
|
43 |
||
44 |
AudioDevice::AudioDevice(DeviceManager *manager, const QByteArray &gstId) |
|
45 |
: gstId(gstId) |
|
46 |
{ |
|
47 |
//get an id |
|
48 |
static int counter = 0; |
|
49 |
id = counter++; |
|
50 |
//get name from device |
|
51 |
if (gstId == "default") { |
|
52 |
description = "Default audio device"; |
|
53 |
} else { |
|
54 |
GstElement *aSink= manager->createAudioSink(); |
|
55 |
||
56 |
if (aSink) { |
|
57 |
gchar *deviceDescription = NULL; |
|
58 |
||
59 |
if (GST_IS_PROPERTY_PROBE(aSink) && gst_property_probe_get_property( GST_PROPERTY_PROBE(aSink), "device" ) ) { |
|
60 |
g_object_set (G_OBJECT(aSink), "device", gstId.constData(), (const char*)NULL); |
|
61 |
g_object_get (G_OBJECT(aSink), "device-name", &deviceDescription, (const char*)NULL); |
|
62 |
description = QByteArray(deviceDescription); |
|
63 |
g_free (deviceDescription); |
|
64 |
gst_element_set_state(aSink, GST_STATE_NULL); |
|
65 |
gst_object_unref (aSink); |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
DeviceManager::DeviceManager(Backend *backend) |
|
72 |
: QObject(backend) |
|
73 |
, m_backend(backend) |
|
74 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
75 |
m_audioSink = qgetenv("PHONON_GST_AUDIOSINK"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
76 |
m_videoSinkWidget = qgetenv("PHONON_GST_VIDEOMODE"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
#ifndef QT_NO_SETTINGS |
0 | 79 |
QSettings settings(QLatin1String("Trolltech")); |
80 |
settings.beginGroup(QLatin1String("Qt")); |
|
81 |
||
82 |
if (m_audioSink.isEmpty()) { |
|
83 |
m_audioSink = settings.value(QLatin1String("audiosink"), "Auto").toByteArray().toLower(); |
|
84 |
} |
|
85 |
||
86 |
if (m_videoSinkWidget.isEmpty()) { |
|
87 |
m_videoSinkWidget = settings.value(QLatin1String("videomode"), "Auto").toByteArray().toLower(); |
|
88 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
#endif //QT_NO_SETTINGS |
0 | 90 |
|
91 |
if (m_backend->isValid()) |
|
92 |
updateDeviceList(); |
|
93 |
} |
|
94 |
||
95 |
DeviceManager::~DeviceManager() |
|
96 |
{ |
|
97 |
m_audioDeviceList.clear(); |
|
98 |
} |
|
99 |
||
100 |
/*** |
|
101 |
* Returns a Gst Audiosink based on GNOME configuration settings, |
|
102 |
* or 0 if the element is not available. |
|
103 |
*/ |
|
104 |
GstElement *DeviceManager::createGNOMEAudioSink(Category category) |
|
105 |
{ |
|
106 |
GstElement *sink = gst_element_factory_make ("gconfaudiosink", NULL); |
|
107 |
||
108 |
if (sink) { |
|
109 |
||
110 |
// set profile property on the gconfaudiosink to "music and movies" |
|
111 |
if (g_object_class_find_property (G_OBJECT_GET_CLASS (sink), "profile")) { |
|
112 |
switch (category) { |
|
113 |
case NotificationCategory: |
|
114 |
g_object_set (G_OBJECT (sink), "profile", 0, (const char*)NULL); // 0 = 'sounds' |
|
115 |
break; |
|
116 |
case CommunicationCategory: |
|
117 |
g_object_set (G_OBJECT (sink), "profile", 2, (const char*)NULL); // 2 = 'chat' |
|
118 |
break; |
|
119 |
default: |
|
120 |
g_object_set (G_OBJECT (sink), "profile", 1, (const char*)NULL); // 1 = 'music and movies' |
|
121 |
break; |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
return sink; |
|
126 |
} |
|
127 |
||
128 |
||
129 |
bool DeviceManager::canOpenDevice(GstElement *element) const |
|
130 |
{ |
|
131 |
if (!element) |
|
132 |
return false; |
|
133 |
||
134 |
if (gst_element_set_state(element, GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS) |
|
135 |
return true; |
|
136 |
||
137 |
const QList<QByteArray> &list = GstHelper::extractProperties(element, "device"); |
|
138 |
foreach (const QByteArray &gstId, list) { |
|
139 |
GstHelper::setProperty(element, "device", gstId); |
|
140 |
if (gst_element_set_state(element, GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS) { |
|
141 |
return true; |
|
142 |
} |
|
143 |
} |
|
144 |
// FIXME: the above can still fail for a valid alsasink because list only contains entries of |
|
145 |
// the form "hw:X,Y". Would be better to use "default:X" or "dmix:X,Y" |
|
146 |
||
147 |
gst_element_set_state(element, GST_STATE_NULL); |
|
148 |
return false; |
|
149 |
} |
|
150 |
||
151 |
/* |
|
152 |
* |
|
153 |
* Returns a GstElement with a valid audio sink |
|
154 |
* based on the current value of PHONON_GSTREAMER_DRIVER |
|
155 |
* |
|
156 |
* Allowed values are auto (default), alsa, oss, arts and ess |
|
157 |
* does not exist |
|
158 |
* |
|
159 |
* If no real sound sink is available a fakesink will be returned |
|
160 |
*/ |
|
161 |
GstElement *DeviceManager::createAudioSink(Category category) |
|
162 |
{ |
|
163 |
GstElement *sink = 0; |
|
164 |
||
165 |
if (m_backend && m_backend->isValid()) |
|
166 |
{ |
|
167 |
if (m_audioSink == "auto") //this is the default value |
|
168 |
{ |
|
169 |
//### TODO : get equivalent KDE settings here |
|
170 |
||
171 |
if (!qgetenv("GNOME_DESKTOP_SESSION_ID").isEmpty()) { |
|
172 |
sink = createGNOMEAudioSink(category); |
|
173 |
if (canOpenDevice(sink)) |
|
174 |
m_backend->logMessage("AudioOutput using gconf audio sink"); |
|
175 |
else if (sink) { |
|
176 |
gst_object_unref(sink); |
|
177 |
sink = 0; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
#ifdef USE_ALSASINK2 |
|
182 |
if (!sink) { |
|
183 |
sink = gst_element_factory_make ("_k_alsasink", NULL); |
|
184 |
if (canOpenDevice(sink)) |
|
185 |
m_backend->logMessage("AudioOutput using alsa2 audio sink"); |
|
186 |
else if (sink) { |
|
187 |
gst_object_unref(sink); |
|
188 |
sink = 0; |
|
189 |
} |
|
190 |
} |
|
191 |
#endif |
|
192 |
||
193 |
if (!sink) { |
|
194 |
sink = gst_element_factory_make ("alsasink", NULL); |
|
195 |
if (canOpenDevice(sink)) |
|
196 |
m_backend->logMessage("AudioOutput using alsa audio sink"); |
|
197 |
else if (sink) { |
|
198 |
gst_object_unref(sink); |
|
199 |
sink = 0; |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
if (!sink) { |
|
204 |
sink = gst_element_factory_make ("autoaudiosink", NULL); |
|
205 |
if (canOpenDevice(sink)) |
|
206 |
m_backend->logMessage("AudioOutput using auto audio sink"); |
|
207 |
else if (sink) { |
|
208 |
gst_object_unref(sink); |
|
209 |
sink = 0; |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
if (!sink) { |
|
214 |
sink = gst_element_factory_make ("osssink", NULL); |
|
215 |
if (canOpenDevice(sink)) |
|
216 |
m_backend->logMessage("AudioOutput using oss audio sink"); |
|
217 |
else if (sink) { |
|
218 |
gst_object_unref(sink); |
|
219 |
sink = 0; |
|
220 |
} |
|
221 |
} |
|
222 |
} else if (m_audioSink == "fake") { |
|
223 |
//do nothing as a fakesink will be created by default |
|
224 |
} else if (m_audioSink == "artssink") { |
|
225 |
sink = GST_ELEMENT(g_object_new(arts_sink_get_type(), NULL)); |
|
226 |
} else if (!m_audioSink.isEmpty()) { //Use a custom sink |
|
227 |
sink = gst_element_factory_make (m_audioSink, NULL); |
|
228 |
if (canOpenDevice(sink)) |
|
229 |
m_backend->logMessage(QString("AudioOutput using %0").arg(QString::fromUtf8(m_audioSink))); |
|
230 |
else if (sink) { |
|
231 |
gst_object_unref(sink); |
|
232 |
sink = 0; |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
if (!sink) { //no suitable sink found so we'll make a fake one |
|
238 |
sink = gst_element_factory_make("fakesink", NULL); |
|
239 |
if (sink) { |
|
240 |
m_backend->logMessage("AudioOutput Using fake audio sink"); |
|
241 |
//without sync the sink will pull the pipeline as fast as the CPU allows |
|
242 |
g_object_set (G_OBJECT (sink), "sync", TRUE, (const char*)NULL); |
|
243 |
} |
|
244 |
} |
|
245 |
Q_ASSERT(sink); |
|
246 |
return sink; |
|
247 |
} |
|
248 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
249 |
#ifndef QT_NO_PHONON_VIDEO |
0 | 250 |
AbstractRenderer *DeviceManager::createVideoRenderer(VideoWidget *parent) |
251 |
{ |
|
252 |
#if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES) |
|
253 |
if (m_videoSinkWidget == "opengl") { |
|
254 |
return new GLRenderer(parent); |
|
255 |
} else |
|
256 |
#endif |
|
257 |
if (m_videoSinkWidget == "software") { |
|
258 |
return new WidgetRenderer(parent); |
|
259 |
} |
|
260 |
#ifndef Q_WS_QWS |
|
261 |
else if (m_videoSinkWidget == "xwindow") { |
|
262 |
return new X11Renderer(parent); |
|
263 |
} else { |
|
264 |
GstElementFactory *srcfactory = gst_element_factory_find("ximagesink"); |
|
265 |
if (srcfactory) { |
|
266 |
return new X11Renderer(parent); |
|
267 |
} |
|
268 |
} |
|
269 |
#endif |
|
270 |
return new WidgetRenderer(parent); |
|
271 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
272 |
#endif //QT_NO_PHONON_VIDEO |
0 | 273 |
|
274 |
/* |
|
275 |
* Returns a positive device id or -1 if device |
|
276 |
* does not exist |
|
277 |
* |
|
278 |
* The gstId is typically in the format hw:1,0 |
|
279 |
*/ |
|
280 |
int DeviceManager::deviceId(const QByteArray &gstId) const |
|
281 |
{ |
|
282 |
for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) { |
|
283 |
if (m_audioDeviceList[i].gstId == gstId) { |
|
284 |
return m_audioDeviceList[i].id; |
|
285 |
} |
|
286 |
} |
|
287 |
return -1; |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Get a human-readable description from a device id |
|
292 |
*/ |
|
293 |
QByteArray DeviceManager::deviceDescription(int id) const |
|
294 |
{ |
|
295 |
for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) { |
|
296 |
if (m_audioDeviceList[i].id == id) { |
|
297 |
return m_audioDeviceList[i].description; |
|
298 |
} |
|
299 |
} |
|
300 |
return QByteArray(); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Updates the current list of active devices |
|
305 |
*/ |
|
306 |
void DeviceManager::updateDeviceList() |
|
307 |
{ |
|
308 |
//fetch list of current devices |
|
309 |
GstElement *audioSink= createAudioSink(); |
|
310 |
||
311 |
QList<QByteArray> list; |
|
312 |
||
313 |
if (audioSink) { |
|
314 |
list = GstHelper::extractProperties(audioSink, "device"); |
|
315 |
list.prepend("default"); |
|
316 |
||
317 |
for (int i = 0 ; i < list.size() ; ++i) { |
|
318 |
QByteArray gstId = list.at(i); |
|
319 |
if (deviceId(gstId) == -1) { |
|
320 |
// This is a new device, add it |
|
321 |
m_audioDeviceList.append(AudioDevice(this, gstId)); |
|
322 |
emit deviceAdded(deviceId(gstId)); |
|
323 |
m_backend->logMessage(QString("Found new audio device %0").arg(QString::fromUtf8(gstId)), Backend::Debug, this); |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
if (list.size() < m_audioDeviceList.size()) { |
|
328 |
//a device was removed |
|
329 |
for (int i = m_audioDeviceList.size() -1 ; i >= 0 ; --i) { |
|
330 |
QByteArray currId = m_audioDeviceList[i].gstId; |
|
331 |
bool found = false; |
|
332 |
for (int k = list.size() -1 ; k >= 0 ; --k) { |
|
333 |
if (currId == list[k]) { |
|
334 |
found = true; |
|
335 |
break; |
|
336 |
} |
|
337 |
} |
|
338 |
if (!found) { |
|
339 |
m_backend->logMessage(QString("Audio device lost %0").arg(QString::fromUtf8(currId)), Backend::Debug, this); |
|
340 |
emit deviceRemoved(deviceId(currId)); |
|
341 |
m_audioDeviceList.removeAt(i); |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
} |
|
346 |
||
347 |
gst_element_set_state (audioSink, GST_STATE_NULL); |
|
348 |
gst_object_unref (audioSink); |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Returns a list of hardware id usable by gstreamer [i.e hw:1,0] |
|
353 |
*/ |
|
354 |
const QList<AudioDevice> DeviceManager::audioOutputDevices() const |
|
355 |
{ |
|
356 |
return m_audioDeviceList; |
|
357 |
} |
|
358 |
||
359 |
} |
|
360 |
} |
|
361 |
||
362 |
QT_END_NAMESPACE |