source: trunk/src/3rdparty/phonon/gstreamer/devicemanager.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 12.1 KB
Line 
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#include "pulsesupport.h"
28
29#ifdef USE_ALSASINK2
30#include "alsasink2.h"
31#endif
32
33/*
34 * This class manages the list of currently
35 * active output devices
36 */
37
38QT_BEGIN_NAMESPACE
39
40namespace Phonon
41{
42namespace Gstreamer
43{
44
45AudioDevice::AudioDevice(DeviceManager *manager, const QByteArray &gstId)
46 : gstId(gstId)
47{
48 // This should never be called when PulseAudio is active.
49 Q_ASSERT(!PulseSupport::getInstance()->isActive());
50
51 id = manager->allocateDeviceId();
52 icon = "audio-card";
53
54 //get name from device
55 if (gstId == "default") {
56 description = "Default audio device";
57 } else {
58 GstElement *aSink= manager->createAudioSink();
59
60 if (aSink) {
61 gchar *deviceDescription = NULL;
62
63 if (GST_IS_PROPERTY_PROBE(aSink) && gst_property_probe_get_property( GST_PROPERTY_PROBE(aSink), "device" ) ) {
64 g_object_set (G_OBJECT(aSink), "device", gstId.constData(), (const char*)NULL);
65 g_object_get (G_OBJECT(aSink), "device-name", &deviceDescription, (const char*)NULL);
66 description = QByteArray(deviceDescription);
67 g_free (deviceDescription);
68 gst_element_set_state(aSink, GST_STATE_NULL);
69 gst_object_unref (aSink);
70 }
71 }
72 }
73}
74
75DeviceManager::DeviceManager(Backend *backend)
76 : QObject(backend)
77 , m_backend(backend)
78 , m_audioDeviceCounter(0)
79{
80 QSettings settings(QLatin1String("Trolltech"));
81 settings.beginGroup(QLatin1String("Qt"));
82
83 PulseSupport *pulse = PulseSupport::getInstance();
84 m_audioSink = qgetenv("PHONON_GST_AUDIOSINK");
85 if (m_audioSink.isEmpty()) {
86 m_audioSink = settings.value(QLatin1String("audiosink"), "Auto").toByteArray().toLower();
87 if (m_audioSink == "auto" && pulse->isActive())
88 m_audioSink = "pulsesink";
89 }
90 if ("pulsesink" != m_audioSink)
91 pulse->enable(false);
92
93 m_videoSinkWidget = qgetenv("PHONON_GST_VIDEOMODE");
94 if (m_videoSinkWidget.isEmpty()) {
95 m_videoSinkWidget = settings.value(QLatin1String("videomode"), "Auto").toByteArray().toLower();
96 }
97
98 if (m_backend->isValid())
99 updateDeviceList();
100}
101
102DeviceManager::~DeviceManager()
103{
104 m_audioDeviceList.clear();
105}
106
107/***
108* Returns a Gst Audiosink based on GNOME configuration settings,
109* or 0 if the element is not available.
110*/
111GstElement *DeviceManager::createGNOMEAudioSink(Category category)
112{
113 GstElement *sink = gst_element_factory_make ("gconfaudiosink", NULL);
114
115 if (sink) {
116
117 // set profile property on the gconfaudiosink to "music and movies"
118 if (g_object_class_find_property (G_OBJECT_GET_CLASS (sink), "profile")) {
119 switch (category) {
120 case NotificationCategory:
121 g_object_set (G_OBJECT (sink), "profile", 0, (const char*)NULL); // 0 = 'sounds'
122 break;
123 case CommunicationCategory:
124 g_object_set (G_OBJECT (sink), "profile", 2, (const char*)NULL); // 2 = 'chat'
125 break;
126 default:
127 g_object_set (G_OBJECT (sink), "profile", 1, (const char*)NULL); // 1 = 'music and movies'
128 break;
129 }
130 }
131 }
132 return sink;
133}
134
135
136bool DeviceManager::canOpenDevice(GstElement *element) const
137{
138 if (!element)
139 return false;
140
141 if (gst_element_set_state(element, GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS)
142 return true;
143
144 const QList<QByteArray> &list = GstHelper::extractProperties(element, "device");
145 foreach (const QByteArray &gstId, list) {
146 GstHelper::setProperty(element, "device", gstId);
147 if (gst_element_set_state(element, GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS) {
148 return true;
149 }
150 }
151 // FIXME: the above can still fail for a valid alsasink because list only contains entries of
152 // the form "hw:X,Y". Would be better to use "default:X" or "dmix:X,Y"
153
154 gst_element_set_state(element, GST_STATE_NULL);
155 return false;
156}
157
158/*
159*
160* Returns a GstElement with a valid audio sink
161* based on the current value of PHONON_GSTREAMER_DRIVER
162*
163* Allowed values are auto (default), alsa, oss, arts and ess
164* does not exist
165*
166* If no real sound sink is available a fakesink will be returned
167*/
168GstElement *DeviceManager::createAudioSink(Category category)
169{
170 GstElement *sink = 0;
171
172 if (m_backend && m_backend->isValid())
173 {
174 if (m_audioSink == "auto") //this is the default value
175 {
176 //### TODO : get equivalent KDE settings here
177
178 if (!qgetenv("GNOME_DESKTOP_SESSION_ID").isEmpty()) {
179 sink = createGNOMEAudioSink(category);
180 if (canOpenDevice(sink))
181 m_backend->logMessage("AudioOutput using gconf audio sink");
182 else if (sink) {
183 gst_object_unref(sink);
184 sink = 0;
185 }
186 }
187
188#ifdef USE_ALSASINK2
189 if (!sink) {
190 sink = gst_element_factory_make ("_k_alsasink", NULL);
191 if (canOpenDevice(sink))
192 m_backend->logMessage("AudioOutput using alsa2 audio sink");
193 else if (sink) {
194 gst_object_unref(sink);
195 sink = 0;
196 }
197 }
198#endif
199
200 if (!sink) {
201 sink = gst_element_factory_make ("alsasink", NULL);
202 if (canOpenDevice(sink))
203 m_backend->logMessage("AudioOutput using alsa audio sink");
204 else if (sink) {
205 gst_object_unref(sink);
206 sink = 0;
207 }
208 }
209
210 if (!sink) {
211 sink = gst_element_factory_make ("autoaudiosink", NULL);
212 if (canOpenDevice(sink))
213 m_backend->logMessage("AudioOutput using auto audio sink");
214 else if (sink) {
215 gst_object_unref(sink);
216 sink = 0;
217 }
218 }
219
220 if (!sink) {
221 sink = gst_element_factory_make ("osssink", NULL);
222 if (canOpenDevice(sink))
223 m_backend->logMessage("AudioOutput using oss audio sink");
224 else if (sink) {
225 gst_object_unref(sink);
226 sink = 0;
227 }
228 }
229 } else if (m_audioSink == "fake") {
230 //do nothing as a fakesink will be created by default
231 } else if (m_audioSink == "artssink") {
232 sink = GST_ELEMENT(g_object_new(arts_sink_get_type(), NULL));
233 } else if (!m_audioSink.isEmpty()) { //Use a custom sink
234 sink = gst_element_factory_make (m_audioSink, NULL);
235 if (canOpenDevice(sink))
236 m_backend->logMessage(QString("AudioOutput using %0").arg(QString::fromUtf8(m_audioSink)));
237 else if (sink) {
238 gst_object_unref(sink);
239 sink = 0;
240 }
241 }
242 }
243
244 if (!sink) { //no suitable sink found so we'll make a fake one
245 sink = gst_element_factory_make("fakesink", NULL);
246 if (sink) {
247 m_backend->logMessage("AudioOutput Using fake audio sink");
248 //without sync the sink will pull the pipeline as fast as the CPU allows
249 g_object_set (G_OBJECT (sink), "sync", TRUE, (const char*)NULL);
250 }
251 }
252 Q_ASSERT(sink);
253 return sink;
254}
255
256#ifndef QT_NO_PHONON_VIDEO
257AbstractRenderer *DeviceManager::createVideoRenderer(VideoWidget *parent)
258{
259#if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES)
260 if (m_videoSinkWidget == "opengl") {
261 return new GLRenderer(parent);
262 } else
263#endif
264 if (m_videoSinkWidget == "software") {
265 return new WidgetRenderer(parent);
266 }
267#ifndef Q_WS_QWS
268 else if (m_videoSinkWidget == "xwindow") {
269 return new X11Renderer(parent);
270 } else {
271 GstElementFactory *srcfactory = gst_element_factory_find("ximagesink");
272 if (srcfactory) {
273 return new X11Renderer(parent);
274 }
275 }
276#endif
277 return new WidgetRenderer(parent);
278}
279#endif //QT_NO_PHONON_VIDEO
280
281/**
282 * Allocate a device id for a new audio device
283 */
284int DeviceManager::allocateDeviceId()
285{
286 return m_audioDeviceCounter++;
287}
288
289
290/**
291 * Returns a positive device id or -1 if device does not exist
292 *
293 * The gstId is typically in the format hw:1,0
294 */
295int DeviceManager::deviceId(const QByteArray &gstId) const
296{
297 for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) {
298 if (m_audioDeviceList[i].gstId == gstId) {
299 return m_audioDeviceList[i].id;
300 }
301 }
302 return -1;
303}
304
305/**
306 * Returns a gstId or "default" if device does not exist
307 *
308 * The gstId is typically in the format hw:1,0
309 */
310const QByteArray DeviceManager::gstId(int deviceId)
311{
312 if (!PulseSupport::getInstance()->isActive()) {
313 AudioDevice *ad = audioDevice(deviceId);
314 if (ad)
315 return QByteArray(ad->gstId);
316 }
317 return QByteArray("default");
318}
319
320/**
321* Get the AudioDevice for a given device id
322*/
323AudioDevice* DeviceManager::audioDevice(int id)
324{
325 for (int i = 0 ; i < m_audioDeviceList.size() ; ++i) {
326 if (m_audioDeviceList[i].id == id)
327 return &m_audioDeviceList[i];
328 }
329 return NULL;
330}
331
332/**
333 * Updates the current list of active devices
334 */
335void DeviceManager::updateDeviceList()
336{
337 //fetch list of current devices
338 GstElement *audioSink= createAudioSink();
339
340 QList<QByteArray> list;
341
342 if (audioSink) {
343 if (!PulseSupport::getInstance()->isActive()) {
344 // If we're using pulse, the PulseSupport class takes care of things for us.
345 list = GstHelper::extractProperties(audioSink, "device");
346 list.prepend("default");
347 }
348
349 for (int i = 0 ; i < list.size() ; ++i) {
350 QByteArray gstId = list.at(i);
351 if (deviceId(gstId) == -1) {
352 // This is a new device, add it
353 m_audioDeviceList.append(AudioDevice(this, gstId));
354 emit deviceAdded(deviceId(gstId));
355 m_backend->logMessage(QString("Found new audio device %0").arg(QString::fromUtf8(gstId)), Backend::Debug, this);
356 }
357 }
358
359 if (list.size() < m_audioDeviceList.size()) {
360 //a device was removed
361 for (int i = m_audioDeviceList.size() -1 ; i >= 0 ; --i) {
362 QByteArray currId = m_audioDeviceList[i].gstId;
363 bool found = false;
364 for (int k = list.size() -1 ; k >= 0 ; --k) {
365 if (currId == list[k]) {
366 found = true;
367 break;
368 }
369 }
370 if (!found) {
371 m_backend->logMessage(QString("Audio device lost %0").arg(QString::fromUtf8(currId)), Backend::Debug, this);
372 emit deviceRemoved(deviceId(currId));
373 m_audioDeviceList.removeAt(i);
374 }
375 }
376 }
377 }
378
379 gst_element_set_state (audioSink, GST_STATE_NULL);
380 gst_object_unref (audioSink);
381}
382
383/**
384 * Returns a list of hardware id usable by gstreamer [i.e hw:1,0]
385 */
386const QList<AudioDevice> DeviceManager::audioOutputDevices() const
387{
388 return m_audioDeviceList;
389}
390
391}
392}
393
394QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.