From c7140f7c9f07ef585de2fa7c936bbf46df8a9bea Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Sun, 3 May 2026 14:05:05 +0200 Subject: [PATCH] Add helper to keep sources active To be used for e.g. the video condition / game capture condition, which can only get the relevant information while a given source is "active". A source can become active if it is not part of the currently selected scene. --- lib/utils/source-helpers.cpp | 43 ++++++++++++++++++++++++++++++++++++ lib/utils/source-helpers.hpp | 21 ++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/lib/utils/source-helpers.cpp b/lib/utils/source-helpers.cpp index 2059d7ca..89c0fa5c 100644 --- a/lib/utils/source-helpers.cpp +++ b/lib/utils/source-helpers.cpp @@ -162,4 +162,47 @@ bool IsMediaSource(obs_source_t *source) return (flags & OBS_SOURCE_CONTROLLABLE_MEDIA) != 0; } +SourceActiveKeeper::~SourceActiveKeeper() +{ + ReleaseRef(); +} + +void SourceActiveKeeper::SetActive(bool active) +{ + if (_active == active) { + return; + } + _active = active; + if (_active) { + AcquireRef(); + } else { + ReleaseRef(); + } +} + +void SourceActiveKeeper::SetSource(obs_source_t *source) +{ + if (_source == source) { + return; + } + ReleaseRef(); + _source = source; + AcquireRef(); +} + +void SourceActiveKeeper::AcquireRef() +{ + if (_active && _source) { + obs_source_inc_active(_source); + } +} + +void SourceActiveKeeper::ReleaseRef() +{ + if (_active && _source) { + obs_source_dec_active(_source); + _source = nullptr; + } +} + } // namespace advss diff --git a/lib/utils/source-helpers.hpp b/lib/utils/source-helpers.hpp index f43cd23d..a7c35a97 100644 --- a/lib/utils/source-helpers.hpp +++ b/lib/utils/source-helpers.hpp @@ -20,4 +20,25 @@ EXPORT OBSWeakSource GetWeakFilterByQString(OBSWeakSource source, EXPORT int GetSceneItemCount(const OBSWeakSource &); EXPORT bool IsMediaSource(obs_source_t *source); +// RAII helper that keeps an OBS source active (via obs_source_inc_active / +// obs_source_dec_active) for as long as the keeper is enabled. +class EXPORT SourceActiveKeeper { +public: + SourceActiveKeeper() = default; + ~SourceActiveKeeper(); + + SourceActiveKeeper(const SourceActiveKeeper &) = delete; + SourceActiveKeeper &operator=(const SourceActiveKeeper &) = delete; + + void SetActive(bool active); + void SetSource(obs_source_t *source); + +private: + void AcquireRef(); + void ReleaseRef(); + + OBSSource _source; + bool _active = false; +}; + } // namespace advss