6#include <QtCore/QCoreApplication>
7#include <QtCore/QThread>
8#include <QtCore/QTimer>
17constexpr std::chrono::milliseconds kWatchdogInterval{1000};
19constexpr int kHwStatsLogIntervalTicks = 10;
25 , _watchdogTimer(new QTimer(this))
27 , _statsSampler(*_supervisor)
29 _watchdogTimer->setInterval(kWatchdogInterval);
30 (void) connect(_watchdogTimer, &QTimer::timeout,
this, [
this]() { _onWatchdogTick(); });
34 qCDebug(GstVideoReceiverLog) <<
"Reconnecting (attempt" << _supervisor->reconnectAttempts() <<
")"
47 const Request previous = std::exchange(_requested, request);
53 if (_requested.active) {
58 _onConfigChanged(previous.
config);
61 _session->setVideoSink(_requested.videoSink.get());
64 _supervisor->reconnectNow();
70 if (recordingId != previousRecordingId) {
79void GstReceiverWorker::_onActiveChanged()
92 if (!_requested.active) {
95 const VideoReceiver::Config& config = _requested.config;
96 VideoReceiver::Config sameStream = previous;
98 if (sameStream != config) {
104 switch (_supervisor->autoReconnectChanged(config.
autoReconnect)) {
106 _onHalted(_supervisor->lastFailure());
116void GstReceiverWorker::_startRecording(
const RecordingRequest& request)
118 if (!_requested.active) {
121 if (_supervisor->halted()) {
122 const QString message =
123 QCoreApplication::translate(
"GstVideoReceiver",
"Unable to record video: the video stream has stopped.");
128 _recording.emplace(request.file, request.format, _maxRecordingPartDuration);
131 _session->setRecording(_recordingRequest());
135void GstReceiverWorker::_stopRecording()
142 _session->setRecording(std::nullopt);
148 _requested.active =
false;
154void GstReceiverWorker::_reconcile()
161 if (_session || _supervisor->halted() || _supervisor->reconnectPending()) {
167void GstReceiverWorker::_restartStream()
169 _supervisor->startOver();
174void GstReceiverWorker::_startSession()
176 const PipelineSession::Settings settings{
177 .uri = _requested.config.uri,
178 .redactedUri = _redactedUri(),
179 .lowLatency = _requested.config.lowLatency,
180 .rtpJitterLatencyMs = _requested.config.rtpJitterLatencyMs,
182 _session =
new PipelineSession(settings, *
this, _watchdog, _dotDumper,
this);
185 if (
const std::optional<PipelineSession::Failure> failure = _session->start()) {
191 _supervisor->sessionStarted();
192 _session->setVideoSink(_requested.videoSink.get());
193 _session->setRecording(_recordingRequest());
195 _watchdogTimer->start();
198void GstReceiverWorker::_stopSession()
203 _watchdogTimer->stop();
204 if (_statsSampler.reset()) {
207 PipelineSession*
const session = std::exchange(_session,
nullptr);
210 session->deleteLater();
213 _recording->nextPart();
219 const StreamSupervisor::FailureOutcome outcome = _supervisor->sessionFailed(
222 _requested.config.autoReconnect);
227 qCDebug(GstVideoReceiverLog) <<
"Scheduling reconnect #" << _supervisor->reconnectAttempts() <<
"in"
231void GstReceiverWorker::_onHalted(
const QString& message)
233 qCWarning(GstVideoReceiverLog) <<
"Video stream stopped:" << message;
238void GstReceiverWorker::_endRecording()
247std::optional<RecordingBranch::Request> GstReceiverWorker::_recordingRequest()
const
252 return RecordingBranch::Request{.file = _recording->currentFile(), .format = _recording->format()};
255void GstReceiverWorker::_onWatchdogTick()
261 if (++_statsTicks >= kHwStatsLogIntervalTicks) {
263 if (
const GstQgc::PathStats hwStats = _session->sinkPathStats(); hwStats.totalDelivered > 0) {
264 qCDebug(GstVideoReceiverLog).noquote() <<
"GPU path live" << _redactedUri() << hwStats.line;
269 if (_recording && _recording->partDue()) {
270 _recording->nextPart();
271 qCDebug(GstVideoReceiverLog) <<
"Recording continues in" << _recording->currentFile();
272 _session->setRecording(_recordingRequest());
275 const StreamWatchdog::Result result = _watchdog.check(
StreamWatchdog::now(), _session->displaying());
276 switch (result.
stall) {
280 qCDebug(GstVideoReceiverLog) <<
"Video decoder timeout, no frames for" << result.
silentMs <<
"ms"
282 _session->snapshot(QStringLiteral(
"pipeline-decoder-timeout"));
283 _session->restartDecoder(
"decoder watchdog");
286 qCDebug(GstVideoReceiverLog) <<
"Stream timeout, no frames for" << result.
silentMs <<
"ms"
288 _session->snapshot(QStringLiteral(
"pipeline-watchdog-timeout"));
290 _recover({.reason = QStringLiteral(
"source watchdog"),
291 .message = QCoreApplication::translate(
"GstVideoReceiver",
"No video received for %1 s.")
293 .permanent =
false});
298void GstReceiverWorker::_sampleStats()
304 VideoStreamStatsMeter::Counters counters;
305 VideoStreamStats stats = _statsSampler.stats();
306 _session->sampleStats(counters, stats);
307 if (_statsSampler.sample(counters, stats, _recording ? &*_recording :
nullptr)) {
312QString GstReceiverWorker::_redactedUri()
const
319 _supervisor->sessionStreaming();
329 qCDebug(GstVideoReceiverLog) <<
"Stream failed:" << failure.
reason << _redactedUri();
334void GstReceiverWorker::onDecodingChanged(
bool decoding)
339void GstReceiverWorker::onDecoderNameChanged(
const QString& name)
346 onSessionFailed({.reason = QStringLiteral(
"decoder failing"),
347 .message = QCoreApplication::translate(
"GstVideoReceiver",
"The video decoder keeps failing."),
348 .permanent =
false});
351void GstReceiverWorker::onRecordingFileOpened(
const QString& file)
354 (void) _recording->onFileOpened(file);
359void GstReceiverWorker::onRecordingFileClosed(
const QString& file,
const QString& error)
362 _recording->onFileClosed(file);
367void GstReceiverWorker::onRecordingFailed(
const QString& file,
const QString& error,
bool fileOpened)
369 if (_recording && (file == _recording->currentFile())) {
374 const QString message =
375 QCoreApplication::translate(
"GstVideoReceiver",
"Video recording failed: %1").arg(error);
380void GstReceiverWorker::onRecordingNeedsNewFile(
const QString& file)
382 if (!_session || !_recording || (file != _recording->currentFile())) {
385 _recording->nextPart();
386 qCDebug(GstVideoReceiverLog) <<
"Recording continues in" << _recording->currentFile();
387 _session->setRecording(_recordingRequest());
virtual void onDecoderFailing()=0
void errorOccurred(const QString &message)
GstReceiverRequest Request
void apply(const Request &request)
Steers the stream towards request, acting on what differs from the previous one.
GstReceiverWorker(QObject *parent=nullptr)
void recordingStarted(const QString &file)
void shutdown()
Stops everything and quits the thread.
~GstReceiverWorker() override
void graphicsDeviceLost()
void recordingFinished(const QString &file, const QString &error)
void setReceiverName(const QString &receiverName)
virtual void onSessionStreaming()=0
The first frame reached the tee.
virtual void onSessionGraphicsDeviceLost()=0
The reconnect policy both receivers share: the phase a stream is in, when a failed one connects again...
@ Halted
Turned off while a reconnect was pending: the stream halted.
@ Resumable
Turned on while halted after a failure that wasn't permanent: the halt is lifted.
void reconnectDue()
A failure's backoff has passed, or reconnectNow() cut it short: the receiver starts the next session.
void stop()
The stream isn't wanted (anymore): Idle, without a reconnect.
@ Permanent
It can't: an unsupported stream, a missing plugin.
@ Transient
It may, so reconnecting is worth it.
@ Streaming
The session delivers.
void stateChanged(VideoReceiver::State state)
@ Decoder
Displaying, but no frame reached the video sink within twice the timeout.
@ Source
No frame reached the tee within the timeout.
std::optional< std::chrono::milliseconds > connectGracePeriod(const QString &uri)
QString redactedUrlForLogging(const QUrl &url)
VideoReceiver::Config config
quint64 restarts
Counts restart() calls: a new count restarts the stream, with videoSink.
quint64 reconnects
Counts reconnectNow() calls: a new count cuts a reconnect's backoff short.
std::shared_ptr< GstElement > videoSink
std::optional< GstRecordingRequest > recording
VideoReceiver::State state
VideoStreamStats stats
About once a second while streaming, and invalid once the session stops.
QString message
For the user.
bool permanent
Retrying can't help: unsupported URI, missing plugin, refused credentials.
QString reason
For the log.
std::chrono::milliseconds reconnectDelay
Until reconnectDue(), when not halted.
qint64 silentMs
How long the stalled side has gone without a frame.
bool autoReconnect
Reconnect with backoff after a failure. Applies at once; changing any other field restarts the stream...