From 10270ca0f6f3f8b8a7e9853e451027caf64ea0c7 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 29 Oct 2026 22:54:19 -0500 Subject: [PATCH] Move ownership of the overlay callback registration to Pacer This allows it to synchronize with the lifetime of the rendering context. --- .../video/ffmpeg-renderers/pacer/pacer.cpp | 13 +++++++++++ app/streaming/video/ffmpeg.cpp | 23 ++++--------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/streaming/video/ffmpeg-renderers/pacer/pacer.cpp b/app/streaming/video/ffmpeg-renderers/pacer/pacer.cpp index 2985df82..05ffebe3 100644 --- a/app/streaming/video/ffmpeg-renderers/pacer/pacer.cpp +++ b/app/streaming/video/ffmpeg-renderers/pacer/pacer.cpp @@ -1,5 +1,6 @@ #include "pacer.h" #include "streaming/streamutils.h" +#include "streaming/session.h" #ifdef Q_OS_WIN32 #define WIN32_LEAN_AND_MEAN @@ -64,6 +65,9 @@ Pacer::~Pacer() SDL_WaitThread(m_RenderThread, nullptr); } else { + // Stop any further overlay callbacks + Session::get()->getOverlayManager().setOverlayRenderer(nullptr); + // Notify the renderer that it is being destroyed soon // NB: This must happen on the same thread that calls renderFrame(). m_VsyncRenderer->cleanupRenderContext(); @@ -177,6 +181,9 @@ int Pacer::renderThread(void* context) me->renderFrame(frame); } + // Stop any further overlay callbacks + Session::get()->getOverlayManager().setOverlayRenderer(nullptr); + // Notify the renderer that it is being destroyed soon // NB: This must happen on the same thread that calls renderFrame(). me->m_VsyncRenderer->cleanupRenderContext(); @@ -325,6 +332,12 @@ bool Pacer::initialize(SDL_Window* window, int maxVideoFps, bool enablePacing) m_VsyncThread = SDL_CreateThread(Pacer::vsyncThread, "PacerVsync", this); } + // Allow the renderer to perform final preparations for rendering + m_VsyncRenderer->prepareToRender(); + + // Tell overlay manager to use this frontend renderer + Session::get()->getOverlayManager().setOverlayRenderer(m_VsyncRenderer); + if (m_VsyncRenderer->isRenderThreadSupported()) { m_RenderThread = SDL_CreateThread(Pacer::renderThread, "PacerRender", this); } diff --git a/app/streaming/video/ffmpeg.cpp b/app/streaming/video/ffmpeg.cpp index fab14b97..c8c292d7 100644 --- a/app/streaming/video/ffmpeg.cpp +++ b/app/streaming/video/ffmpeg.cpp @@ -288,10 +288,6 @@ void FFmpegVideoDecoder::reset() // need to delete in the renderer destructor. avcodec_free_context(&m_VideoDecoderCtx); - if (m_CurrentTestMode != TestMode::TestFrameOnly) { - Session::get()->getOverlayManager().setOverlayRenderer(nullptr); - } - // If we have a separate frontend renderer, free that first if (m_FrontendRenderer != m_BackendRenderer) { delete m_FrontendRenderer; @@ -494,15 +490,6 @@ bool FFmpegVideoDecoder::completeInitialization(const AVCodec* decoder, enum AVP m_VideoFormat = params->videoFormat; m_CurrentTestMode = testMode; - // Don't bother initializing Pacer if we're not actually going to render - if (testMode != TestMode::TestFrameOnly) { - m_Pacer = new Pacer(m_FrontendRenderer, &m_ActiveWndVideoStats); - if (!m_Pacer->initialize(params->window, params->frameRate, - params->enableFramePacing || (params->enableVsync && (m_FrontendRenderer->getRendererAttributes() & RENDERER_ATTRIBUTE_FORCE_PACING)))) { - return false; - } - } - m_VideoDecoderCtx = avcodec_alloc_context3(decoder); if (!m_VideoDecoderCtx) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, @@ -723,11 +710,11 @@ bool FFmpegVideoDecoder::completeInitialization(const AVCodec* decoder, enum AVP m_NeedsSpsFixup = false; } - // Tell overlay manager to use this frontend renderer - Session::get()->getOverlayManager().setOverlayRenderer(m_FrontendRenderer); - - // Allow the renderer to perform final preparations for rendering - m_FrontendRenderer->prepareToRender(); + m_Pacer = new Pacer(m_FrontendRenderer, &m_ActiveWndVideoStats); + if (!m_Pacer->initialize(params->window, params->frameRate, + params->enableFramePacing || (params->enableVsync && (m_FrontendRenderer->getRendererAttributes() & RENDERER_ATTRIBUTE_FORCE_PACING)))) { + return false; + } // Only create the decoder thread when instantiating the decoder for real. It will use APIs from // moonlight-common-c that can only be legally called with an established connection.