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