From cd5b0e74e3641ae6a94d230d5d3bd11bedf691a7 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 19 Dec 2020 21:25:20 -0600 Subject: [PATCH] Draw background in MMAL renderer on X11 Fixes #412 --- app/streaming/video/ffmpeg-renderers/mmal.cpp | 42 ++++++++++++++++++- app/streaming/video/ffmpeg-renderers/mmal.h | 4 ++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/streaming/video/ffmpeg-renderers/mmal.cpp b/app/streaming/video/ffmpeg-renderers/mmal.cpp index 53a7405b..61f09994 100644 --- a/app/streaming/video/ffmpeg-renderers/mmal.cpp +++ b/app/streaming/video/ffmpeg-renderers/mmal.cpp @@ -5,9 +5,12 @@ #include +#include + MmalRenderer::MmalRenderer() : m_Renderer(nullptr), - m_InputPort(nullptr) + m_InputPort(nullptr), + m_BackgroundRenderer(nullptr) { } @@ -20,6 +23,10 @@ MmalRenderer::~MmalRenderer() if (m_Renderer != nullptr) { mmal_component_destroy(m_Renderer); } + + if (m_BackgroundRenderer != nullptr) { + SDL_DestroyRenderer(m_BackgroundRenderer); + } } bool MmalRenderer::prepareDecoderContext(AVCodecContext* context, AVDictionary** options) @@ -43,6 +50,9 @@ bool MmalRenderer::initialize(PDECODER_PARAMETERS params) { MMAL_STATUS_T status; + // Clear the background if possible + setupBackground(params); + status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, &m_Renderer); if (status != MMAL_SUCCESS) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, @@ -134,6 +144,36 @@ bool MmalRenderer::initialize(PDECODER_PARAMETERS params) return true; } +void MmalRenderer::setupBackground(PDECODER_PARAMETERS params) +{ + SDL_SysWMinfo info; + + SDL_VERSION(&info.version); + + if (!SDL_GetWindowWMInfo(params->window, &info)) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "SDL_GetWindowWMInfo() failed: %s", + SDL_GetError()); + return; + } + + // On X11, we can safely create a renderer and draw a black background. + // Due to conflicts with Qt, it's unsafe to do this for KMSDRM. + if (info.subsystem == SDL_SYSWM_X11) { + m_BackgroundRenderer = SDL_CreateRenderer(params->window, -1, SDL_RENDERER_SOFTWARE); + if (m_BackgroundRenderer == nullptr) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "SDL_CreateRenderer() failed: %s", + SDL_GetError()); + return; + } + + SDL_SetRenderDrawColor(m_BackgroundRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderClear(m_BackgroundRenderer); + SDL_RenderPresent(m_BackgroundRenderer); + } +} + void MmalRenderer::InputPortCallback(MMAL_PORT_T*, MMAL_BUFFER_HEADER_T* buffer) { mmal_buffer_header_release(buffer); diff --git a/app/streaming/video/ffmpeg-renderers/mmal.h b/app/streaming/video/ffmpeg-renderers/mmal.h index 2571aa69..fc2b16c2 100644 --- a/app/streaming/video/ffmpeg-renderers/mmal.h +++ b/app/streaming/video/ffmpeg-renderers/mmal.h @@ -21,7 +21,11 @@ public: private: static void InputPortCallback(MMAL_PORT_T* port, MMAL_BUFFER_HEADER_T* buffer); + void setupBackground(PDECODER_PARAMETERS params); + MMAL_COMPONENT_T* m_Renderer; MMAL_PORT_T* m_InputPort; + + SDL_Renderer* m_BackgroundRenderer; };