File: audiooutput.cpp

package info (click to toggle)
phonon 4%3A4.8.0-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,164 kB
  • ctags: 2,519
  • sloc: cpp: 15,935; sh: 62; makefile: 49; xml: 32; awk: 22
file content (558 lines) | stat: -rw-r--r-- 19,890 bytes parent folder | download
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*  This file is part of the KDE project
    Copyright (C) 2005-2006 Matthias Kretz <kretz@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), Nokia Corporation
    (or its successors, if any) and the KDE Free Qt Foundation, which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "audiooutput.h"
#include "audiooutput_p.h"

#include "audiooutputadaptor_p.h"
#include "audiooutputinterface.h"
#include "factory_p.h"
#include "globalconfig.h"
#include "objectdescription.h"
#include "phononconfig_p.h"
#include "phononnamespace_p.h"
#include "platform_p.h"
#include "pulsesupport.h"
#ifdef HAVE_PULSEAUDIO
#  include "pulsestream_p.h"
#endif

#include <QtCore/QUuid>
#include <QtCore/qmath.h>

#define PHONON_CLASSNAME AudioOutput
#define IFECES7 AudioOutputInterface47
#define IFACES2 AudioOutputInterface42
#define IFACES1 IFACES2
#define IFACES0 AudioOutputInterface40, IFACES1, IFECES7
#define PHONON_INTERFACENAME IFACES0

namespace Phonon
{

static inline bool callSetOutputDevice(AudioOutputPrivate *const d, const AudioOutputDevice &dev)
{
    PulseSupport *pulse = PulseSupport::getInstance();
    if (pulse->isActive())
        return pulse->setOutputDevice(d->getStreamUuid(), dev.index());

    if (!d->backendObject())
        return false;

    Iface<IFACES2> iface(d);
    if (iface) {
        return iface->setOutputDevice(dev);
    }
    return Iface<IFACES0>::cast(d)->setOutputDevice(dev.index());
}

AudioOutput::AudioOutput(Phonon::Category category, QObject *parent)
    : AbstractAudioOutput(*new AudioOutputPrivate, parent)
{
    P_D(AudioOutput);
    d->init(category);
}

AudioOutput::AudioOutput(QObject *parent)
    : AbstractAudioOutput(*new AudioOutputPrivate, parent)
{
    P_D(AudioOutput);
    d->init(NoCategory);
}

void AudioOutputPrivate::init(Phonon::Category c)
{
    P_Q(AudioOutput);
#ifndef PHONON_NO_DBUS
    adaptor = new AudioOutputAdaptor(q);
    static unsigned int number = 0;
    const QString &path = QLatin1String("/AudioOutputs/") + QString::number(number++);
    QDBusConnection con = QDBusConnection::sessionBus();
    con.registerObject(path, q);
    emit adaptor->newOutputAvailable(con.baseService(), path);
    q->connect(q, SIGNAL(volumeChanged(qreal)), adaptor, SIGNAL(volumeChanged(qreal)));
    q->connect(q, SIGNAL(mutedChanged(bool)), adaptor, SIGNAL(mutedChanged(bool)));
#endif

    category = c;
#ifndef QT_NO_QUUID_STRING
    streamUuid = QUuid::createUuid().toString();
#endif

    createBackendObject();

#ifdef HAVE_PULSEAUDIO
    PulseSupport *pulse = PulseSupport::getInstance();
    if (pulse->isActive()) {
        PulseStream *stream = pulse->registerOutputStream(streamUuid, category);
        if (stream) {
            q->connect(stream, SIGNAL(usingDevice(int)), SLOT(_k_deviceChanged(int)));
            q->connect(stream, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal)));
            q->connect(stream, SIGNAL(muteChanged(bool)), SLOT(_k_mutedChanged(bool)));

            AudioOutputInterface47 *iface = Iface<AudioOutputInterface47>::cast(this);
            if (iface)
                iface->setStreamUuid(streamUuid);
            else
                pulse->setupStreamEnvironment(streamUuid);
        }
    }
#endif

    q->connect(Factory::sender(), SIGNAL(availableAudioOutputDevicesChanged()), SLOT(_k_deviceListChanged()));
}

QString AudioOutputPrivate::getStreamUuid()
{
    return streamUuid;
}

void AudioOutputPrivate::createBackendObject()
{
    if (m_backendObject)
        return;
    P_Q(AudioOutput);
    m_backendObject = Factory::createAudioOutput(q);
    // (cg) Is it possible that PulseAudio initialisation means that the device here is not valid?
    // User reports seem to suggest this possibility but I can't see how :s.
    // See other comment and check for isValid() in handleAutomaticDeviceChange()
    device = AudioOutputDevice::fromIndex(GlobalConfig().audioOutputDeviceFor(category, GlobalConfig::AdvancedDevicesFromSettings | GlobalConfig::HideUnavailableDevices));
    if (m_backendObject) {
        setupBackendObject();
    }
}

QString AudioOutput::name() const
{
    P_D(const AudioOutput);
    return d->name;
}

void AudioOutput::setName(const QString &newName)
{
    P_D(AudioOutput);
    if (d->name == newName) {
        return;
    }
    d->name = newName;
    PulseSupport *pulse = PulseSupport::getInstance();
    if (pulse->isActive())
        pulse->setOutputName(d->getStreamUuid(), newName);
    else
        setVolume(Platform::loadVolume(newName));
#ifndef PHONON_NO_DBUS
    if (d->adaptor) {
        emit d->adaptor->nameChanged(newName);
    }
#endif
}

static const qreal LOUDNESS_TO_VOLTAGE_EXPONENT = qreal(0.67);
static const qreal VOLTAGE_TO_LOUDNESS_EXPONENT = qreal(1.0/LOUDNESS_TO_VOLTAGE_EXPONENT);

void AudioOutput::setVolume(qreal volume)
{
    P_D(AudioOutput);
    d->volume = volume;
    PulseSupport *pulse = PulseSupport::getInstance();
    if (k_ptr->backendObject()) {
        if (pulse->isActive()) {
            pulse->setOutputVolume(d->getStreamUuid(), volume);
        } else if (!d->muted) {
            // using Stevens' power law loudness is proportional to (sound pressure)^0.67
            // sound pressure is proportional to voltage:
            // p² \prop P \prop V²
            // => if a factor for loudness of x is requested
            INTERFACE_CALL(setVolume(pow(volume, VOLTAGE_TO_LOUDNESS_EXPONENT)));
      } else {
          emit volumeChanged(volume);
      }
    } else {
        emit volumeChanged(volume);
    }
    if (!pulse->isActive())
        Platform::saveVolume(d->name, volume);
}

qreal AudioOutput::volume() const
{
    P_D(const AudioOutput);
    if (d->muted || !d->m_backendObject || PulseSupport::getInstance()->isActive())
        return d->volume;
    return pow(INTERFACE_CALL(volume()), LOUDNESS_TO_VOLTAGE_EXPONENT);
}

#ifndef PHONON_LOG10OVER20
#define PHONON_LOG10OVER20
static const qreal log10over20 = qreal(0.1151292546497022842); // ln(10) / 20
#endif // PHONON_LOG10OVER20

qreal AudioOutput::volumeDecibel() const
{
    P_D(const AudioOutput);
    if (d->muted || !d->m_backendObject || PulseSupport::getInstance()->isActive())
        return log(d->volume) / log10over20;
    return 0.67 * log(INTERFACE_CALL(volume())) / log10over20;
}

void AudioOutput::setVolumeDecibel(qreal newVolumeDecibel)
{
    setVolume(exp(newVolumeDecibel * log10over20));
}

bool AudioOutput::isMuted() const
{
    P_D(const AudioOutput);
    return d->muted;
}

void AudioOutput::setMuted(bool mute)
{
    P_D(AudioOutput);
    if (d->muted != mute) {
        PulseSupport *pulse = PulseSupport::getInstance();
        if (mute) {
            d->muted = mute;
            if (k_ptr->backendObject()) {
                if (pulse->isActive())
                    pulse->setOutputMute(d->getStreamUuid(), mute);
                else
                    INTERFACE_CALL(setVolume(0.0));
            }
        } else {
            if (k_ptr->backendObject()) {
                if (pulse->isActive())
                    pulse->setOutputMute(d->getStreamUuid(), mute);
                else
                    INTERFACE_CALL(setVolume(pow(d->volume, VOLTAGE_TO_LOUDNESS_EXPONENT)));
            }
            d->muted = mute;
        }
        emit mutedChanged(mute);
    }
}

Category AudioOutput::category() const
{
    P_D(const AudioOutput);
    return d->category;
}

AudioOutputDevice AudioOutput::outputDevice() const
{
    P_D(const AudioOutput);
    return d->device;
}

bool AudioOutput::setOutputDevice(const AudioOutputDevice &newAudioOutputDevice)
{
    P_D(AudioOutput);
    if (!newAudioOutputDevice.isValid()) {
        d->outputDeviceOverridden = d->forceMove = false;
        const int newIndex = GlobalConfig().audioOutputDeviceFor(d->category);
        if (newIndex == d->device.index()) {
            return true;
        }
        d->device = AudioOutputDevice::fromIndex(newIndex);
    } else {
        d->outputDeviceOverridden = d->forceMove = true;
        if (d->device == newAudioOutputDevice) {
            return true;
        }
        d->device = newAudioOutputDevice;
    }
    if (k_ptr->backendObject()) {
        return callSetOutputDevice(d, d->device);
    }
    return true;
}

bool AudioOutputPrivate::aboutToDeleteBackendObject()
{
    if (m_backendObject) {
        volume = pINTERFACE_CALL(volume());
    }
    return AbstractAudioOutputPrivate::aboutToDeleteBackendObject();
}

void AudioOutputPrivate::setupBackendObject()
{
    P_Q(AudioOutput);
    Q_ASSERT(m_backendObject);
    AbstractAudioOutputPrivate::setupBackendObject();

    QObject::connect(m_backendObject, SIGNAL(volumeChanged(qreal)), q, SLOT(_k_volumeChanged(qreal)));
    QObject::connect(m_backendObject, SIGNAL(audioDeviceFailed()), q, SLOT(_k_audioDeviceFailed()));

    if (!PulseSupport::getInstance()->isActive()) {
        // set up attributes
        pINTERFACE_CALL(setVolume(pow(volume, VOLTAGE_TO_LOUDNESS_EXPONENT)));

#ifndef QT_NO_PHONON_SETTINGSGROUP
        // if the output device is not available and the device was not explicitly set
        // There is no need to set the output device initially if PA is used as
        // we know it will not work (stream doesn't exist yet) and that this will be
        // handled by _k_deviceChanged()
        if (!callSetOutputDevice(this, device) && !outputDeviceOverridden) {
            // fall back in the preference list of output devices
            QList<int> deviceList = GlobalConfig().audioOutputDeviceListFor(category, GlobalConfig::AdvancedDevicesFromSettings | GlobalConfig::HideUnavailableDevices);
            if (deviceList.isEmpty()) {
                return;
            }
            for (int i = 0; i < deviceList.count(); ++i) {
                const AudioOutputDevice &dev = AudioOutputDevice::fromIndex(deviceList.at(i));
                if (callSetOutputDevice(this, dev)) {
                    handleAutomaticDeviceChange(dev, AudioOutputPrivate::FallbackChange);
                    return; // found one that works
                }
            }
            // if we get here there is no working output device. Tell the backend.
            const AudioOutputDevice none;
            callSetOutputDevice(this, none);
            handleAutomaticDeviceChange(none, FallbackChange);
        }
#endif //QT_NO_PHONON_SETTINGSGROUP
    }
}

void AudioOutputPrivate::_k_volumeChanged(qreal newVolume)
{
    volume = pow(newVolume, LOUDNESS_TO_VOLTAGE_EXPONENT);
    if (!muted) {
        P_Q(AudioOutput);
        emit q->volumeChanged(volume);
    }
}

void AudioOutputPrivate::_k_mutedChanged(bool newMuted)
{
    muted = newMuted;
    P_Q(AudioOutput);
    emit q->mutedChanged(newMuted);
}

void AudioOutputPrivate::_k_revertFallback()
{
    if (deviceBeforeFallback == -1) {
        return;
    }
    device = AudioOutputDevice::fromIndex(deviceBeforeFallback);
    callSetOutputDevice(this, device);
    P_Q(AudioOutput);
    emit q->outputDeviceChanged(device);
#ifndef PHONON_NO_DBUS
    emit adaptor->outputDeviceIndexChanged(device.index());
#endif
}

void AudioOutputPrivate::_k_audioDeviceFailed()
{
    if (PulseSupport::getInstance()->isActive())
        return;

#ifndef QT_NO_PHONON_SETTINGSGROUP

    pDebug() << Q_FUNC_INFO;
    // outputDeviceIndex identifies a failing device
    // fall back in the preference list of output devices
    const QList<int> deviceList = GlobalConfig().audioOutputDeviceListFor(category, GlobalConfig::AdvancedDevicesFromSettings | GlobalConfig::HideUnavailableDevices);
    for (int i = 0; i < deviceList.count(); ++i) {
        const int devIndex = deviceList.at(i);
        // if it's the same device as the one that failed, ignore it
        if (device.index() != devIndex) {
            const AudioOutputDevice &info = AudioOutputDevice::fromIndex(devIndex);
            if (callSetOutputDevice(this, info)) {
                handleAutomaticDeviceChange(info, FallbackChange);
                return; // found one that works
            }
        }
    }
#endif //QT_NO_PHONON_SETTINGSGROUP
    // if we get here there is no working output device. Tell the backend.
    const AudioOutputDevice none;
    callSetOutputDevice(this, none);
    handleAutomaticDeviceChange(none, FallbackChange);
}

void AudioOutputPrivate::_k_deviceListChanged()
{
    if (PulseSupport::getInstance()->isActive())
        return;

#ifndef QT_NO_PHONON_SETTINGSGROUP
    pDebug() << Q_FUNC_INFO;
    // Check to see if we have an override and do not change to a higher priority device if the overridden device is still present.
    if (outputDeviceOverridden && device.property("available").toBool()) {
        return;
    }
    // let's see if there's a usable device higher in the preference list
    const QList<int> deviceList = GlobalConfig().audioOutputDeviceListFor(category, GlobalConfig::AdvancedDevicesFromSettings);
    DeviceChangeType changeType = HigherPreferenceChange;
    for (int i = 0; i < deviceList.count(); ++i) {
        const int devIndex = deviceList.at(i);
        const AudioOutputDevice &info = AudioOutputDevice::fromIndex(devIndex);
        if (!info.property("available").toBool()) {
            if (device.index() == devIndex) {
                // we've reached the currently used device and it's not available anymore, so we
                // fallback to the next available device
                changeType = FallbackChange;
            }
            pDebug() << devIndex << "is not available";
            continue;
        }
        pDebug() << devIndex << "is available";
        if (device.index() == devIndex) {
            // we've reached the currently used device, nothing to change
            break;
        }
        if (callSetOutputDevice(this, info)) {
            handleAutomaticDeviceChange(info, changeType);
            break; // found one with higher preference that works
        }
    }
#endif //QT_NO_PHONON_SETTINGSGROUP
}

void AudioOutputPrivate::_k_deviceChanged(int deviceIndex)
{
    // NB that this method is only used by PulseAudio at present.

    // 1. Check to see if we are overridden. If we are, and devices do not match,
    //    then try and apply our own device as the output device.
    //    We only do this the first time
    if (outputDeviceOverridden && forceMove) {
        forceMove = false;
        const AudioOutputDevice &currentDevice = AudioOutputDevice::fromIndex(deviceIndex);
        if (currentDevice != device) {
            if (!callSetOutputDevice(this, device)) {
                // What to do if we are overridden and cannot change to our preferred device?
            }
        }
    }
    // 2. If we are not overridden, then we need to update our perception of what
    //    device we are using. If the devices do not match, something lower in the
    //    stack is overriding our preferences (e.g. a per-application stream preference,
    //    specific application move, priority list changed etc. etc.)
    else if (!outputDeviceOverridden) {
        const AudioOutputDevice &currentDevice = AudioOutputDevice::fromIndex(deviceIndex);
        if (currentDevice != device) {
            // The device is not what we think it is, so lets say what is happening.
            handleAutomaticDeviceChange(currentDevice, SoundSystemChange);
        }
    }
}

static struct
{
    int first;
    int second;
} g_lastFallback = { 0, 0 };

void AudioOutputPrivate::handleAutomaticDeviceChange(const AudioOutputDevice &device2, DeviceChangeType type)
{
    P_Q(AudioOutput);
    deviceBeforeFallback = device.index();
    device = device2;
    emit q->outputDeviceChanged(device2);
#ifndef PHONON_NO_DBUS
    emit adaptor->outputDeviceIndexChanged(device.index());
#endif
    const AudioOutputDevice &device1 = AudioOutputDevice::fromIndex(deviceBeforeFallback);
    switch (type) {
    case FallbackChange:
        if (g_lastFallback.first != device1.index() || g_lastFallback.second != device2.index()) {
#ifndef QT_NO_PHONON_PLATFORMPLUGIN
            const QString &text = //device2.isValid() ?
                AudioOutput::tr("<html>The audio playback device <b>%1</b> does not work.<br/>"
                        "Falling back to <b>%2</b>.</html>").arg(device1.name()).arg(device2.name()) /*:
                AudioOutput::tr("<html>The audio playback device <b>%1</b> does not work.<br/>"
                        "No other device available.</html>").arg(device1.name())*/;
            Platform::notification("AudioDeviceFallback", text);
#endif //QT_NO_PHONON_PLATFORMPLUGIN
            g_lastFallback.first = device1.index();
            g_lastFallback.second = device2.index();
        }
        break;
    case HigherPreferenceChange:
        {
#ifndef QT_NO_PHONON_PLATFORMPLUGIN
        const QString text = AudioOutput::tr("<html>Switching to the audio playback device <b>%1</b><br/>"
                "which just became available and has higher preference.</html>").arg(device2.name());
        Platform::notification("AudioDeviceFallback", text,
                QStringList(AudioOutput::tr("Revert back to device '%1'").arg(device1.name())),
                q, SLOT(_k_revertFallback()));
#endif //QT_NO_PHONON_PLATFORMPLUGIN
        g_lastFallback.first = 0;
        g_lastFallback.second = 0;
        }
        break;
    case SoundSystemChange:
        {
#ifndef QT_NO_PHONON_PLATFORMPLUGIN
        // If device1 is not "valid" this indicates that the preferences used to select
        // a device was perhaps not available when this object was created (although
        // I can't quite work out how that would be....)
        if (device1.isValid()) {
            if (device1.property("available").toBool()) {
                const QString text = AudioOutput::tr("<html>Switching to the audio playback device <b>%1</b><br/>"
                        "which has higher preference or is specifically configured for this stream.</html>").arg(device2.name());
                Platform::notification("AudioDeviceFallback", text,
                        QStringList(AudioOutput::tr("Revert back to device '%1'").arg(device1.name())),
                        q, SLOT(_k_revertFallback()));
            } else {
                const QString &text =
                    AudioOutput::tr("<html>The audio playback device <b>%1</b> does not work.<br/>"
                            "Falling back to <b>%2</b>.</html>").arg(device1.name()).arg(device2.name());
                Platform::notification("AudioDeviceFallback", text);
            }
        }
#endif //QT_NO_PHONON_PLATFORMPLUGIN
        //outputDeviceOverridden = true;
        g_lastFallback.first = 0;
        g_lastFallback.second = 0;
        }
        break;
    }
}

AudioOutputPrivate::~AudioOutputPrivate()
{
    PulseSupport *pulse = PulseSupport::getInstanceOrNull(true);
    if (pulse) {
        pulse->clearStreamCache(streamUuid);
    }
#ifndef PHONON_NO_DBUS
    if (adaptor) {
        emit adaptor->outputDestroyed();
    }
#endif
}

} //namespace Phonon

#include "moc_audiooutput.cpp"

#undef PHONON_CLASSNAME
#undef PHONON_INTERFACENAME
#undef IFECES7
#undef IFACES2
#undef IFACES1
#undef IFACES0