6#include <QtMultimedia/QCamera>
7#include <QtMultimedia/QVideoFrame>
8#include <QtMultimedia/QVideoSink>
18constexpr std::chrono::milliseconds kSampleInterval{1000};
27 _sampleTimer.setInterval(kSampleInterval);
28 (void) connect(&_sampleTimer, &QTimer::timeout,
this, &UVCStreamMonitor::_sample);
34 qCWarning(UVCStreamMonitorLog) <<
"attach without a video sink";
37 qCDebug(UVCStreamMonitorLog) <<
"attach" << camera << videoSink;
39 _videoSink = videoSink;
41 _connections.append(connect(videoSink, &QVideoSink::videoFrameChanged,
this, &UVCStreamMonitor::_onVideoFrame));
43 _connections.append(connect(camera, &QCamera::activeChanged,
this, &UVCStreamMonitor::_restart));
51 qCDebug(UVCStreamMonitorLog) <<
"detach" << _videoSink.data();
56void UVCStreamMonitor::_disconnect()
58 for (
const QMetaObject::Connection& connection : std::as_const(_connections)) {
59 (void) disconnect(connection);
66void UVCStreamMonitor::_onVideoFrame(
const QVideoFrame& frame)
69 if (!
frame.isValid()) {
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);
80void UVCStreamMonitor::_sample()
88 const FrameCounters& frameCounters = _frameCounters;
90 if (frameCounters.frames == 0) {
93 const qint64
nowMs = _clock();
94 if (!_streamingSinceMs) {
95 _streamingSinceMs =
nowMs;
98 VideoStreamStats
stats = _stats;
100 stats.gpuPath = frameCounters.gpu ? QStringLiteral(
"GPU") : QStringLiteral(
"CPU");
102 VideoStreamStatsMeter::Counters counters;
104 if (_meter.update(counters,
stats)) {
105 _publish(stats, frameCounters.size, frameCounters.streamFrameRate);
109void UVCStreamMonitor::_restart()
111 _frameCounters = FrameCounters();
113 _streamingSinceMs.reset();
114 _publish(VideoStreamStats(), QSize(), 0.0);
117void UVCStreamMonitor::_publish(
const VideoStreamStats& stats, QSize videoSize,
double cameraFrameRate)
#define QGC_LOGGING_CATEGORY(name, categoryStr)
double cameraFrameRate() const
The frame rate the camera was set up for; 0 while unknown.
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.
std::optional< Frame > frame(std::string_view text)
QString pixelFormatName(QVideoFrameFormat::PixelFormat pixelFormat)
"MJPEG", "YUYV", "NV12", ...; empty for an invalid format.