outputdevicecontroller.cpp 5.15 KB
Newer Older
1
/*
2
 *  Copyright 2013-2014  Andreas Cord-Landwehr <cordlandwehr@kde.org>
3
 *
4
5
6
7
 *  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) any later version.
8
9
10
11
 *
 *  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
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
16
17
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "outputdevicecontroller.h"
19
#include "outputbackendinterface.h"
20
21
22
23
#include "backendinterface.h"

#include <QCoreApplication>
#include <QPluginLoader>
Andreas Cord-Landwehr's avatar
Andreas Cord-Landwehr committed
24
#include <QUrl>
25
26
27
28
29

#include <KPluginLoader>
#include <KPluginFactory>
#include <KPluginMetaData>

30
#include "libsound_debug.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

/**
 * \class OutputDeviceControllerPrivate
 * \internal
 *
 * This is the private data class for \see OutputDeviceController.
 * Note that -- even if the OutputDeviceController is contructed before its first call -- all
 * devices get only configured by first instantiation of OutputDeviceController with a call
 * to OutputDeviceControllerPrivate::lazyInit(), called in OutputDeviceController::self().
 */
class OutputDeviceControllerPrivate
{
public:
    OutputDeviceControllerPrivate(OutputDeviceController *parent)
        : m_parent(parent)
46
        , m_backend(nullptr)
47
        , m_volume(0)
48
        , m_initialized(false)
49
    {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        QStringList dirsToCheck;
        foreach (const QString &directory, QCoreApplication::libraryPaths()) {
            dirsToCheck << directory + "/artikulate/libsound";
        }
        // load plugins
        QPluginLoader loader;
        foreach (const QString &dir, dirsToCheck) {
            QVector<KPluginMetaData> metadataList = KPluginLoader::findPlugins(dir,
                [=](const KPluginMetaData &data)
            {
                return data.serviceTypes().contains("artikulate/libsound/backend");
            });

            foreach (const auto &metadata, metadataList) {
                loader.setFileName(metadata.fileName());
                qCDebug(LIBSOUND_LOG) << "Load Plugin: " << metadata.name();
                if (!loader.load()) {
                    qCCritical(LIBSOUND_LOG) << "Error while loading plugin: " << metadata.name();
                }
                KPluginFactory *factory = KPluginLoader(loader.fileName()).factory();
70
71
72
73
                if (!factory) {
                    qCCritical(LIBSOUND_LOG) << "Could not load plugin:" << metadata.name();
                    continue;
                }
74
75
76
77
78
79
80
81
82
                BackendInterface *plugin = factory->create<BackendInterface>(parent, QList< QVariant >());
                if (plugin->outputBackend()) {
                    m_backendList.append(plugin->outputBackend());
                }
            }
        }
        if (!m_backend && !m_backendList.isEmpty()) {
            m_backend = m_backendList.first();
        }
83
84
85
86
    }

    ~OutputDeviceControllerPrivate()
    {
87
        delete m_backend;
88
89
    }

90
    void lazyInit()
91
92
    {
        if (m_initialized) {
93
            return;
94
        }
95
        m_parent->connect(m_backend, &OutputBackendInterface::stateChanged,
Andreas Cord-Landwehr's avatar
Andreas Cord-Landwehr committed
96
                          m_parent, &OutputDeviceController::emitChangedState);
97
        m_volume = m_backend->volume();
98
        m_initialized = true;
99
100
    }

101
    OutputBackendInterface * backend() const
102
103
104
    {
        Q_ASSERT(m_backend);
        return m_backend;
105
106
107
    }

    OutputDeviceController *m_parent;
108
    OutputBackendInterface *m_backend;
109
    QList<OutputBackendInterface *> m_backendList;
110
    int m_volume; // volume as cubic value
111
    bool m_initialized;
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
};

OutputDeviceController::OutputDeviceController()
    : d(new OutputDeviceControllerPrivate(this))
{

}

OutputDeviceController::~OutputDeviceController()
{
}

OutputDeviceController & OutputDeviceController::self()
{
    static OutputDeviceController instance;
127
    instance.d->lazyInit();
128
129
130
    return instance;
}

131
132
void OutputDeviceController::play(const QString& filePath)
{
133
    d->backend()->setUri(filePath);
134
    d->backend()->setVolume(d->m_volume);
135
136
    d->backend()->play();
    emit started();
137
138
}

Andreas Cord-Landwehr's avatar
Andreas Cord-Landwehr committed
139
void OutputDeviceController::play(const QUrl &filePath)
140
{
141
    play(filePath.toLocalFile());
142
143
144
145
}

void OutputDeviceController::stop()
{
146
147
    d->backend()->stop();
    emit stopped();
148
149
}

150
OutputDeviceController::State OutputDeviceController::state() const
151
{
152
    return d->backend()->state();
153
154
}

155
void OutputDeviceController::setVolume(int volume)
156
{
157
158
    // backend only accepts volume, when there is a pipeline
    // store value here and set it when playing
159
    d->backend()->setVolume(volume);
160
161
162
163
164
165
    d->m_volume = volume;
}

int OutputDeviceController::volume() const
{
    return d->backend()->volume();
166
}
167
168
169
170
171
172
173
174
175
176
177
178

void OutputDeviceController::emitChangedState()
{
    if (state() == OutputDeviceController::StoppedState) {
        emit stopped();
        return;
    }
    if (state() == OutputDeviceController::PlayingState) {
        emit started();
        return;
    }
}