From c404c76a8b8149e130375eab89e3c2d32a49f2e4 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Tue, 23 Apr 2024 11:44:39 +0300 Subject: [PATCH] plugins/screencast: Use steady clock to generate last sent timestamps The steady clock is more preferred for generating timestamps because it ensures that timestamps increase monotonically. This fixes screencasts freezing in obs after changing the system time. --- src/plugins/screencast/screencaststream.cpp | 11 ++++++----- src/plugins/screencast/screencaststream.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/plugins/screencast/screencaststream.cpp b/src/plugins/screencast/screencaststream.cpp index 44fc7d71f8..f5e7ca40e7 100644 --- a/src/plugins/screencast/screencaststream.cpp +++ b/src/plugins/screencast/screencaststream.cpp @@ -90,7 +90,7 @@ void ScreenCastStream::onStreamStateChanged(pw_stream_state old, pw_stream_state break; case PW_STREAM_STATE_STREAMING: m_streaming = true; - m_lastSent = QDateTime(); + m_lastSent.reset(); m_source->resume(); break; case PW_STREAM_STATE_CONNECTING: @@ -436,9 +436,10 @@ void ScreenCastStream::recordFrame(const QRegion &_damagedRegion) return; } - if (m_videoFormat.max_framerate.num != 0 && !m_lastSent.isNull()) { - auto frameInterval = (1000 * m_videoFormat.max_framerate.denom / m_videoFormat.max_framerate.num); - auto lastSentAgo = m_lastSent.msecsTo(QDateTime::currentDateTimeUtc()); + if (m_videoFormat.max_framerate.num != 0 && m_lastSent.has_value()) { + const auto now = std::chrono::steady_clock::now(); + const auto frameInterval = std::chrono::milliseconds(1000 * m_videoFormat.max_framerate.denom / m_videoFormat.max_framerate.num); + const auto lastSentAgo = std::chrono::duration_cast(now - m_lastSent.value()); if (lastSentAgo < frameInterval) { m_pendingDamages += damagedRegion; if (!m_pendingFrame.isActive()) { @@ -654,7 +655,7 @@ void ScreenCastStream::enqueue(pw_buffer *pwBuffer) pw_stream_queue_buffer(m_pwStream, pwBuffer); if (pwBuffer->buffer->datas[0].chunk->flags != SPA_CHUNK_FLAG_CORRUPTED) { - m_lastSent = QDateTime::currentDateTimeUtc(); + m_lastSent = std::chrono::steady_clock::now(); } } diff --git a/src/plugins/screencast/screencaststream.h b/src/plugins/screencast/screencaststream.h index 6ebd4187d7..0a929c2dad 100644 --- a/src/plugins/screencast/screencaststream.h +++ b/src/plugins/screencast/screencaststream.h @@ -135,7 +135,7 @@ private: bool m_waitForNewBuffers = false; quint32 m_drmFormat = 0; - QDateTime m_lastSent; + std::optional m_lastSent; QRegion m_pendingDamages; QTimer m_pendingFrame; };