From 7041feaea37f912944e9d6e1ddc3735a6e8ee218 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Mon, 25 Dec 2023 21:34:37 +0100 Subject: [PATCH] Move handling of stream start / stop time to stream condition --- src/advanced-scene-switcher.cpp | 18 ---------- src/macro-core/macro-condition-streaming.cpp | 37 ++++++++++++++++---- src/switcher-data.hpp | 2 -- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/advanced-scene-switcher.cpp b/src/advanced-scene-switcher.cpp index 4463762f..ab72870b 100644 --- a/src/advanced-scene-switcher.cpp +++ b/src/advanced-scene-switcher.cpp @@ -623,18 +623,6 @@ static void setReplayBufferSaved() switcher->replayBufferSaved = true; } -static void setStreamStarting() -{ - switcher->lastStreamStartingTime = - std::chrono::high_resolution_clock::now(); -} - -static void setStreamStopping() -{ - switcher->lastStreamStoppingTime = - std::chrono::high_resolution_clock::now(); -} - static void handleTransitionEnd() { GetMacroTransitionCV().notify_all(); @@ -714,12 +702,6 @@ static void OBSEvent(enum obs_frontend_event event, void *switcher) case OBS_FRONTEND_EVENT_TRANSITION_STOPPED: handleTransitionEnd(); break; - case OBS_FRONTEND_EVENT_STREAMING_STARTING: - setStreamStarting(); - break; - case OBS_FRONTEND_EVENT_STREAMING_STOPPING: - setStreamStopping(); - break; #if LIBOBS_API_VER >= MAKE_SEMANTIC_VERSION(27, 2, 0) case OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING: handleSceneCollectionChanging(); diff --git a/src/macro-core/macro-condition-streaming.cpp b/src/macro-core/macro-condition-streaming.cpp index 52894d61..46b72275 100644 --- a/src/macro-core/macro-condition-streaming.cpp +++ b/src/macro-core/macro-condition-streaming.cpp @@ -1,5 +1,4 @@ #include "macro-condition-streaming.hpp" -#include "switcher-data.hpp" #include "utility.hpp" namespace advss { @@ -25,6 +24,32 @@ const static std::map "AdvSceneSwitcher.condition.stream.state.keyFrameInterval"}, }; +static bool setupStreamingEventHandler(); +static bool steamingEventHandlerIsSetup = setupStreamingEventHandler(); +static std::chrono::high_resolution_clock::time_point streamStartTime{}; +static std::chrono::high_resolution_clock::time_point streamStopTime{}; + +bool setupStreamingEventHandler() +{ + static auto handleStreamingEvents = [](enum obs_frontend_event event, + void *) { + switch (event) { + case OBS_FRONTEND_EVENT_STREAMING_STARTING: + streamStartTime = + std::chrono::high_resolution_clock::now(); + break; + case OBS_FRONTEND_EVENT_STREAMING_STOPPING: + streamStopTime = + std::chrono::high_resolution_clock::now(); + break; + default: + break; + }; + }; + obs_frontend_add_event_callback(handleStreamingEvents, nullptr); + return true; +} + int MacroConditionStream::GetKeyFrameInterval() { const auto configPath = GetPathInProfileDir("streamEncoder.json"); @@ -42,10 +67,8 @@ bool MacroConditionStream::CheckCondition() { bool match = false; - bool streamStarting = switcher->lastStreamStartingTime != - _lastStreamStartingTime; - bool streamStopping = switcher->lastStreamStoppingTime != - _lastStreamStoppingTime; + bool streamStarting = streamStartTime != _lastStreamStartingTime; + bool streamStopping = streamStopTime != _lastStreamStoppingTime; switch (_condition) { case Condition::STOP: @@ -68,10 +91,10 @@ bool MacroConditionStream::CheckCondition() } if (streamStarting) { - _lastStreamStartingTime = switcher->lastStreamStartingTime; + _lastStreamStartingTime = streamStartTime; } if (streamStopping) { - _lastStreamStoppingTime = switcher->lastStreamStoppingTime; + _lastStreamStoppingTime = streamStopTime; } return match; } diff --git a/src/switcher-data.hpp b/src/switcher-data.hpp index 0bbc2066..f5e02a2c 100644 --- a/src/switcher-data.hpp +++ b/src/switcher-data.hpp @@ -120,8 +120,6 @@ public: obs_source_t *waitScene = nullptr; OBSWeakSource currentScene = nullptr; OBSWeakSource previousScene = nullptr; - std::chrono::high_resolution_clock::time_point lastStreamStartingTime{}; - std::chrono::high_resolution_clock::time_point lastStreamStoppingTime{}; /* --- Start of General tab section --- */