qtgstreamercapturebackend.cpp 5.67 KB
Newer Older
1
/*
2
 *  Copyright 2013-2014  Andreas Cord-Landwehr <cordlandwehr@kde.org>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 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.), which shall act as a proxy
 *  defined in Section 14 of version 3 of the license.
 *
 *  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
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

21
#include "qtgstreamercapturebackend.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <QGlib/Error>
#include <QGlib/Connect>
#include <QGst/Init>
#include <QGst/ElementFactory>
#include <QGst/ChildProxy>
#include <QGst/Pipeline>
#include <QGst/Pad>
#include <QGst/Event>
#include <QGst/Message>
#include <QGst/Bus>

#include <KDebug>
#include <KLocale>

37
QtGStreamerCaptureBackend::QtGStreamerCaptureBackend()
38
39
40
41
42
43
44
45
46
47
48
49
{
    QGst::init();

    //setup the device list
    QGst::ElementPtr src = QGst::ElementFactory::make("autoaudiosrc");

    if (!src) {
        kError() << "Failed to create element \"autoaudiosrc\". Make sure you have "
                 << "gstreamer-plugins-good installed";
        return;
    }

50
    m_availableDevices.insert("", i18nc("default sound device", "Default"));
51
52
}

53
QtGStreamerCaptureBackend::~QtGStreamerCaptureBackend()
54
55
56
57
{
    m_pipeline.clear();
}

58
CaptureDeviceController::State QtGStreamerCaptureBackend::captureState()
59
{
60
61
62
63
    if (!m_pipeline) {
        return CaptureDeviceController::StoppedState;
    }

64
65
66
67
68
    switch (m_pipeline->currentState()) {
    case QGst::StateNull:
        return CaptureDeviceController::StoppedState;
        break;
    case QGst::StatePaused:
69
        return CaptureDeviceController::RecordingState;
70
71
72
73
74
75
76
77
78
        break;
    case QGst::StatePlaying:
        return CaptureDeviceController::RecordingState;
        break;
    default:
        return CaptureDeviceController::StoppedState;
    }
}

79
QGst::BinPtr QtGStreamerCaptureBackend::createAudioSrcBin()
80
81
82
83
84
85
86
87
88
89
{
    QGst::BinPtr audioBin;

    try {
        audioBin = QGst::Bin::fromDescription("autoaudiosrc name=\"audiosrc\" ! audioconvert ! "
                                              "audioresample ! audiorate ! vorbisenc name=enc quality=0.6 ! queue");
    } catch (const QGlib::Error &error) {
        kError() << "Failed to create audio source bin:" << error;
        return QGst::BinPtr();
    }
90
91
    QGst::ElementPtr src = audioBin->getElementByName("audiosrc");
    //autoaudiosrc creates the actual source in the READY state
92

93
    src->setState(QGst::StateReady);
94
95
96
    return audioBin;
}

97
void QtGStreamerCaptureBackend::onBusMessage(const QGst::MessagePtr & message)
98
99
100
101
{
    switch (message->type()) {
    case QGst::MessageEos:
        //got end-of-stream - stop the pipeline
102
103
        kDebug() << "EOS signal received, stopping pipeline";
        stopPipeline();
104
105
106
107
108
        break;
    case QGst::MessageError:
        //check if the pipeline exists before destroying it,
        //since we could get multiple error messages
        if (m_pipeline) {
109
            stopPipeline();
110
111
112
113
114
115
116
117
118
        }
        kError() << "Pipeline Error:"
                 << message.staticCast<QGst::ErrorMessage>()->error().message();
        break;
    default:
        break;
    }
}

119
void QtGStreamerCaptureBackend::startCapture(const QString &filePath)
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
{
    // clear pipeline if still existing
    if (m_pipeline) {
        kWarning() << "removing forgotten pipeline";
        //send an end-of-stream event to flush metadata and cause an EosMessage to be delivered
        m_pipeline->sendEvent(QGst::EosEvent::create());
    }

    QGst::BinPtr audioSrcBin = createAudioSrcBin();
    QGst::ElementPtr mux = QGst::ElementFactory::make("oggmux");
    QGst::ElementPtr sink = QGst::ElementFactory::make("filesink");

    if (!audioSrcBin || !mux || !sink) {
        kError() << "One or more elements could not be created. "
                 << "Verify that you have all the necessary element plugins installed.";
        return;
    }

    // set output path
    sink->setProperty("location", filePath);

    m_pipeline = QGst::Pipeline::create();
    m_pipeline->add(audioSrcBin, mux, sink);

    //link elements
145
    QGst::PadPtr audioPad = mux->getRequestPad("audio_%u");
146
147
148
149
150
151
    audioSrcBin->getStaticPad("src")->link(audioPad);

    mux->link(sink);

    //connect the bus
    m_pipeline->bus()->addSignalWatch();
152
    QGlib::connect(m_pipeline->bus(), "message", this, &QtGStreamerCaptureBackend::onBusMessage);
153
154
155
    m_pipeline->setState(QGst::StatePlaying);
}

156
void QtGStreamerCaptureBackend::stopCapture()
157
158
159
160
161
162
163
{
    if (m_pipeline) { //pipeline exists - destroy it
        //send an end-of-stream event to flush metadata and cause an EosMessage to be delivered
        m_pipeline->sendEvent(QGst::EosEvent::create());
    }
}

164
void QtGStreamerCaptureBackend::stopPipeline()
165
166
167
168
169
170
171
172
173
{
    if (!m_pipeline) {
        kWarning() << "Stopping non-existing pipeline, aborting";
        return;
    }
    m_pipeline->setState(QGst::StateNull);
    m_pipeline.clear();
}

174
QStringList QtGStreamerCaptureBackend::devices() const
175
{
176
177
178
    //TODO qtgstreamer backend currently only provides access to default backend,
    // reenable selection by using Gst::Device

179
    return m_availableDevices.values();
180
181
}

182
void QtGStreamerCaptureBackend::setDevice(const QString& deviceIdentifier)
183
184
185
186
{
    //TODO add sanity check
    m_device = deviceIdentifier;
}