-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmjpegstreamerclient.cpp
320 lines (263 loc) · 8.56 KB
/
mjpegstreamerclient.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
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
/****************************************************************************
**
** Copyright (C) 2014 Alexander Rössler
** License: LGPL version 2.1
**
** This file is part of QtQuickVcp.
**
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the GNU Lesser General Public License
** (LGPL) version 2.1 which accompanies this distribution, and is available at
** http://www.gnu.org/licenses/lgpl-2.1.html
**
** 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.
**
** Contributors:
** Alexander Rössler @ The Cool Tool GmbH <mail DOT aroessler AT gmail DOT com>
**
****************************************************************************/
#include "mjpegstreamerclient.h"
#include <QPainter>
#include <QNetworkAccessManager>
#include <QTimer>
#include <QNetworkReply>
#include <QElapsedTimer>
#include <google/protobuf/text_format.h>
using namespace nzmqt;
namespace qtquickvcp {
/*!
\qmltype MjpegStreamerClient
\instantiates qtquickvcp::MjpegStreamerClient
\inqmlmodule Machinekit.HalRemote
\brief Client for a MJPEG video stream
\ingroup videoview
The MjpegStreamerClient component can display MJPG video streams captured
by the application mjpeg-streamer. For that purpose mjpeg-streamer needs
to be run with the ZeroMQ output plugin.
\qml
import Machinekit.HalRemote 1.0
import Machinekit.HalRemote.Controls 1.0
HalApplicationWindow {
services: [
Service {
id: videoService
type: "video"
filter: ServiceDiscoveryFilter {
name: "Webcam1"
}
}
]
MjpegStreamerClient {
id: mjpegStreamerClient
anchors.fill: parent
videoUri: videoService.uri
ready: videoService.ready
}
}
\endqml
*/
/*! \qmlproperty string MjpegStreamerClient::videoUri
This property holds the video service uri.
*/
/*! \qmlproperty bool MjpegStreamerClient::ready
This property holds whether the component is ready or not.
If the property is set to \c true the component will try to connect. If the
property is set to \c false all connections will be closed.
The default value is \c{false}.
*/
/*! \qmlproperty int MjpegStreamerClient::fps
This property holds the current framerate of the video in fps.
*/
/*! \qmlproperty double MjpegStreamerClient::timestamp
This property holds the timestamp in ms of the current displayed video frame.
*/
/*! \qmlproperty time MjpegStreamerClient::time
This property holds the time the current displayed video frame was taken.
*/
/* \qmlproperty enumeration MjpegStreamerClient::aspectRatioMode
*
* This property specifies how the video image should be scaled.
*
* The default value is \c{Qt::KeepAspectRatio}
*/
MjpegStreamerClient::MjpegStreamerClient(QQuickPaintedItem *parent) :
QQuickPaintedItem(parent),
m_componentCompleted(false),
m_framerateTimer(new QTimer(this)),
m_streamBufferTimer(new QTimer(this)),
m_context(nullptr),
m_updateSocket(nullptr),
m_videoUri(""),
m_running(false),
m_fps(0.0),
m_frameCount(0),
m_timestamp(0.0),
m_time(QTime()),
m_aspectRatioMode(Qt::KeepAspectRatio)
{
this->setRenderTarget(QQuickPaintedItem::FramebufferObject);
this->setPerformanceHint(QQuickPaintedItem::FastFBOResizing, true);
this->setAntialiasing(false);
this->setOpaquePainting(true);
connect(m_framerateTimer, &QTimer::timeout,
this, &MjpegStreamerClient::updateFramerate);
m_framerateTimer->setInterval(1000);
connect(m_streamBufferTimer, &QTimer::timeout,
this, &MjpegStreamerClient::updateStreamBuffer);
m_streamBufferTimer->setSingleShot(true);
}
MjpegStreamerClient::~MjpegStreamerClient()
{
disconnectSocket();
}
/** componentComplete is executed when the QML component is fully loaded */
void MjpegStreamerClient::componentComplete()
{
m_componentCompleted = true;
if (m_running == true) // the component was set to ready before it was completed
{
start();
}
QQuickPaintedItem::componentComplete();
}
void MjpegStreamerClient::paint(QPainter *painter)
{
QRect boundingRect = this->boundingRect().toRect();
// Show view finder
if(!m_frameImg.isNull())
{
QImage scaledImage = m_frameImg.scaled(boundingRect.size(), m_aspectRatioMode);
QRect drawRect(scaledImage.rect());
drawRect.moveCenter(boundingRect.center());
painter->drawImage(drawRect, scaledImage, scaledImage.rect());
}
}
/** If the running property has a rising edge we try to connect
* if it is has a falling edge we disconnect and cleanup
*/
void MjpegStreamerClient::setReady(bool arg)
{
if (m_running != arg) {
m_running = arg;
emit readyChanged(arg);
if (m_componentCompleted == false)
{
return;
}
if (m_running)
{
start();
}
else
{
stop();
}
}
}
void MjpegStreamerClient::start()
{
m_framerateTimer->start();
connectSocket();
}
void MjpegStreamerClient::stop()
{
m_framerateTimer->stop();
disconnectSocket();
}
void MjpegStreamerClient::connectSocket()
{
m_context = createDefaultContext(this, 1);
m_context->start();
m_updateSocket = m_context->createSocket(ZMQSocket::TYP_SUB, this);
m_updateSocket->setLinger(0);
m_updateSocket->connectTo(m_videoUri);
m_updateSocket->subscribeTo("frames");
connect(m_updateSocket, &ZMQSocket::messageReceived,
this, &MjpegStreamerClient::updateMessageReceived);
}
void MjpegStreamerClient::disconnectSocket()
{
if (m_updateSocket != nullptr)
{
m_updateSocket->close();
m_updateSocket->deleteLater();
m_updateSocket = nullptr;
}
if (m_context != nullptr)
{
m_context->stop();
m_context->deleteLater();
m_context = nullptr;
}
}
void MjpegStreamerClient::updateMessageReceived(const QList<QByteArray> &messageList)
{
QByteArray topic;
topic = messageList.at(0);
m_rx.ParseFromArray(messageList.at(1).data(), messageList.at(1).size());
m_streamBuffer.clear();
m_streamBufferTimer->stop();
m_currentStreamBufferItem.timestamp = 0;
for (int i = 0; i < m_rx.frame_size(); ++i)
{
pb::Package_Frame frame;
QByteArray data;
QDateTime dateTime;
QTime time;
StreamBufferItem streamBufferItem;
frame = m_rx.frame(i);
data = QByteArray(frame.blob().data(), static_cast<int>(frame.blob().size()));
#ifdef QT_DEBUG
qDebug() << "received frame" << topic;
qDebug() << "timestamp: " << frame.timestamp_unix() << frame.timestamp_s() << frame.timestamp_us();
#endif
streamBufferItem.image = QImage::fromData(data, "JPG");
streamBufferItem.timestamp = static_cast<double>(frame.timestamp_s())*1000.0 + static_cast<double>(frame.timestamp_us()) / 1000.0;
dateTime.setMSecsSinceEpoch(static_cast<qint64>(frame.timestamp_unix()) * 1000);
dateTime = dateTime.toLocalTime();
time = dateTime.time();
streamBufferItem.time = time.addMSecs(static_cast<int>(frame.timestamp_us() / 1000));
#ifdef QT_DEBUG
qDebug() << "time: " << streamBufferItem.time;
#endif
m_streamBuffer.enqueue(streamBufferItem);
}
m_currentStreamBufferItem = m_streamBuffer.dequeue();
updateStreamBuffer();
}
void MjpegStreamerClient::updateFramerate()
{
m_fps = m_frameCount;
m_frameCount = 0;
emit fpsChanged(m_fps);
#ifdef QT_DEBUG
qDebug() << "fps: " << m_fps;
#endif
}
void MjpegStreamerClient::updateStreamBuffer()
{
updateStreamBufferItem();
if (!m_streamBuffer.isEmpty())
{
double timestamp;
double interval;
timestamp = m_currentStreamBufferItem.timestamp;
m_currentStreamBufferItem = m_streamBuffer.dequeue();
interval = m_currentStreamBufferItem.timestamp - timestamp;
m_streamBufferTimer->start(qMax(static_cast<int>(interval), 0));
}
}
void MjpegStreamerClient::updateStreamBufferItem()
{
m_frameCount++;
m_frameImg = m_currentStreamBufferItem.image;
m_timestamp = m_currentStreamBufferItem.timestamp;
m_time = m_currentStreamBufferItem.time;
update();
emit timestampChanged(m_timestamp);
emit timeChanged(m_time);
}
} // namespace qtquickvcp