-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGStreamerPlayer.cpp
208 lines (181 loc) · 5.57 KB
/
GStreamerPlayer.cpp
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
/*
Copyright (C) 2010 Marco Ballesio <[email protected]>
Copyright (C) 2011-2013 Collabora Ltd.
@author George Kiagiadakis <[email protected]>
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.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "GStreamerPlayer.h"
#include <QUrl>
#include <QDebug>
#include <QGlib/Connect>
#include <QGlib/Error>
#include <QGst/ElementFactory>
#include <QGst/Bus>
#include <QGst/Pad>
#include <QGst/Event>
#include <QGst/Parse>
#include <QtQuick/QQuickView>
#include <QDialog>
GStreamerPlayer::GStreamerPlayer(QObject *parent)
: QObject(parent)
{
m_playing = false;
m_stopped = true;
m_paused = false;
m_stopTimer.setSingleShot(true);
connect(&m_stopTimer, SIGNAL(timeout()), this, SLOT(onStopTimer()));
}
GStreamerPlayer::~GStreamerPlayer()
{
stop();
}
void GStreamerPlayer::setVideoSink(const QGst::ElementPtr & sink)
{
m_videoSink = sink;
}
bool GStreamerPlayer::play()
{
initialize();
if (!m_pipeline.isNull()) {
m_pipeline->setState(QGst::StatePlaying);
return true;
}
return false;
}
void GStreamerPlayer::pause()
{
if (!m_pipeline.isNull()) {
m_pipeline->setState(QGst::StatePaused);
}
}
void GStreamerPlayer::stop()
{
if (!m_pipeline.isNull()) {
m_pipeline->setState(QGst::StateNull);
}
}
void GStreamerPlayer::toggleFullScreen()
{
}
void GStreamerPlayer::initialize()
{
if (!m_pipelineString.isEmpty() && m_pipelineString != m_currentPipelineString)
{
m_currentPipelineString = "";
stop();
if (!m_pipeline.isNull())
{
m_pipeline->remove(m_videoSink);
m_pipeline.clear();
}
QByteArray data = m_pipelineString.toUtf8();
const char *pData = data.constData();
m_pipeline = QGst::Parse::launch(pData).dynamicCast<QGst::Pipeline>();
if (!m_pipeline.isNull())
{
QGst::PadPtr src = m_pipeline->findUnlinkedPad(QGst::PadSrc);
if (!src.isNull())
{
src->parentElement()->setProperty("caps", QGst::Caps::fromString("video/x-raw, format=I420"));
m_videoSink->setProperty("sync", false);
m_pipeline->add(m_videoSink);
src->parentElement()->link(m_videoSink);
//watch the bus for messages
QGst::BusPtr bus = m_pipeline->bus();
bus->addSignalWatch();
QGlib::connect(bus, "message", this, &GStreamerPlayer::onBusMessage);
m_currentPipelineString = m_pipelineString;
}
else
{
QString msg = "The Pipeline command string has no unlinked video source element, cannot link pipeline. String = " + m_pipelineString;
Q_EMIT messageBox(msg);
m_pipeline.clear();
}
}
else
{
QString msg = "Failed to create the pipeline '" + m_pipelineString + "'!";
Q_EMIT messageBox(msg);
}
}
}
void GStreamerPlayer::onBusMessage(const QGst::MessagePtr & message)
{
switch (message->type()) {
case QGst::MessageEos: //End of stream. We reached the end of the file.
stop();
break;
case QGst::MessageError: //Some error occurred.
qCritical() << message.staticCast<QGst::ErrorMessage>()->error();
stop();
break;
case QGst::MessageStateChanged:
if (!m_pipeline.isNull() && message->source() == m_pipeline)
{
handlePipelineStateChange(message.staticCast<QGst::StateChangedMessage>());
}
break;
default:
break;
}
}
void GStreamerPlayer::handlePipelineStateChange(const QGst::StateChangedMessagePtr & scm)
{
switch (scm->newState()) {
case QGst::StatePlaying:
m_playing = true;
Q_EMIT playingChanged(m_playing);
m_paused = false;
Q_EMIT pausedChanged(m_paused);
m_stopped = false;
Q_EMIT stoppedChanged(m_stopped);
break;
case QGst::StatePaused:
m_playing = false;
Q_EMIT playingChanged(m_playing);
m_paused = true;
Q_EMIT pausedChanged(m_paused);
m_stopped = false;
Q_EMIT stoppedChanged(m_stopped);
break;
case QGst::StateNull:
m_playing = false;
Q_EMIT playingChanged(m_playing);
m_paused = false;
Q_EMIT pausedChanged(m_paused);
m_stopped = true;
Q_EMIT stoppedChanged(m_stopped);
break;
case QGst::StateVoidPending:
case QGst::StateReady:
break;
}
}
void GStreamerPlayer::sendEOS()
{
// Need this to save a running stream
QGst::EosEventPtr eos = QGst::EosEvent::create();
if (!m_pipeline.isNull())
m_pipeline->sendEvent(eos);
}
void GStreamerPlayer::onStopTimer()
{
m_stopTimer.stop();
m_stopped = true;
Q_EMIT stoppedChanged(m_stopped);
m_paused = false;
Q_EMIT pausedChanged(m_paused);
m_playing = false;
Q_EMIT playingChanged(m_playing);
this->stop();
}