QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
UVCStreamMonitor.cc
Go to the documentation of this file.
1#include "UVCStreamMonitor.h"
2
3#include <chrono>
4#include <utility>
5
6#include <QtMultimedia/QCamera>
7#include <QtMultimedia/QVideoFrame>
8#include <QtMultimedia/QVideoSink>
9
10#include "MonotonicClock.h"
11#include "QGCLoggingCategory.h"
12#include "UVCDevices.h"
13
14QGC_LOGGING_CATEGORY(UVCStreamMonitorLog, "Video.UVCStreamMonitor")
15
16namespace {
17
18constexpr std::chrono::milliseconds kSampleInterval{1000};
19
20} // namespace
21
23 : QObject(parent)
24 , _clock(clock ? std::move(clock) : VideoStreamStatsMeter::Clock(MonotonicClock::nowMs))
25 , _meter(_clock)
26{
27 _sampleTimer.setInterval(kSampleInterval);
28 (void) connect(&_sampleTimer, &QTimer::timeout, this, &UVCStreamMonitor::_sample);
29}
30
31void UVCStreamMonitor::attach(QCamera* camera, QVideoSink* videoSink)
32{
33 if (!videoSink) {
34 qCWarning(UVCStreamMonitorLog) << "attach without a video sink";
35 return;
36 }
37 qCDebug(UVCStreamMonitorLog) << "attach" << camera << videoSink;
38 _disconnect();
39 _videoSink = videoSink;
40 // Qt's capture session queues the camera's frames to the sink's thread, which is this monitor's.
41 _connections.append(connect(videoSink, &QVideoSink::videoFrameChanged, this, &UVCStreamMonitor::_onVideoFrame));
42 if (camera) {
43 _connections.append(connect(camera, &QCamera::activeChanged, this, &UVCStreamMonitor::_restart));
44 }
45 _restart();
46 _sampleTimer.start();
47}
48
50{
51 qCDebug(UVCStreamMonitorLog) << "detach" << _videoSink.data();
52 _disconnect();
53 _restart();
54}
55
56void UVCStreamMonitor::_disconnect()
57{
58 for (const QMetaObject::Connection& connection : std::as_const(_connections)) {
59 (void) disconnect(connection);
60 }
61 _connections.clear();
62 _videoSink.clear();
63 _sampleTimer.stop();
64}
65
66void UVCStreamMonitor::_onVideoFrame(const QVideoFrame& frame)
67{
68 // The sink clears the display with an invalid frame when the camera stops.
69 if (!frame.isValid()) {
70 return;
71 }
72 const QVideoFrameFormat format = frame.surfaceFormat();
73 ++_frameCounters.frames;
74 _frameCounters.size = frame.size();
75 _frameCounters.pixelFormat = format.pixelFormat();
76 _frameCounters.streamFrameRate = format.streamFrameRate();
77 _frameCounters.gpu = (frame.handleType() != QVideoFrame::NoHandle);
78}
79
80void UVCStreamMonitor::_sample()
81{
82 if (!_videoSink) {
83 // The sink went away without detaching.
84 _disconnect();
85 _restart();
86 return;
87 }
88 const FrameCounters& frameCounters = _frameCounters;
89 // Rates from before the first frame would only measure the camera's startup.
90 if (frameCounters.frames == 0) {
91 return;
92 }
93 const qint64 nowMs = _clock();
94 if (!_streamingSinceMs) {
95 _streamingSinceMs = nowMs;
96 }
97
98 VideoStreamStats stats = _stats;
99 stats.codec = UVCDevices::pixelFormatName(frameCounters.pixelFormat);
100 stats.gpuPath = frameCounters.gpu ? QStringLiteral("GPU") : QStringLiteral("CPU");
101 stats.uptimeMs = nowMs - *_streamingSinceMs;
102 VideoStreamStatsMeter::Counters counters;
103 counters.framesDisplayed = frameCounters.frames;
104 if (_meter.update(counters, stats)) {
105 _publish(stats, frameCounters.size, frameCounters.streamFrameRate);
106 }
107}
108
109void UVCStreamMonitor::_restart()
110{
111 _frameCounters = FrameCounters();
112 _meter.reset();
113 _streamingSinceMs.reset();
114 _publish(VideoStreamStats(), QSize(), 0.0);
115}
116
117void UVCStreamMonitor::_publish(const VideoStreamStats& stats, QSize videoSize, double cameraFrameRate)
118{
119 if ((stats == _stats) && (videoSize == _videoSize) && (cameraFrameRate == _cameraFrameRate)) {
120 return;
121 }
122 _stats = stats;
123 _videoSize = videoSize;
124 _cameraFrameRate = cameraFrameRate;
125 emit statsChanged();
126}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
double cameraFrameRate() const
The frame rate the camera was set up for; 0 while unknown.
QSize videoSize() const
void detach()
Stops measuring.
void attach(QCamera *camera, QVideoSink *videoSink)
Measures the frames videoSink receives from camera (optional), in place of any other sink attached.
UVCStreamMonitor(QObject *parent=nullptr, VideoStreamStatsMeter::Clock clock={})
clock defaults to the steady clock.
VideoStreamStats stats() const
std::function< qint64()> Clock
Monotonic milliseconds.
QByteArray format(const QList< LogEntry > &entries, int fmt)
std::optional< Frame > frame(std::string_view text)
QString pixelFormatName(QVideoFrameFormat::PixelFormat pixelFormat)
"MJPEG", "YUYV", "NV12", ...; empty for an invalid format.
Definition UVCDevices.cc:38