From e1bacd75b630f7961511a2223bd2816027f22e71 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Tue, 1 Mar 2022 21:32:30 +0100 Subject: [PATCH] Move audio fade threads to generic _helperThreads of macro This will allow waiting for and stopping audio fades when calling Stop() for a particular macro --- src/headers/macro-action-audio.hpp | 2 -- src/headers/macro.hpp | 4 +++- src/macro-action-audio.cpp | 23 ++++---------------- src/macro.cpp | 34 ++++++++++++++++++++---------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/headers/macro-action-audio.hpp b/src/headers/macro-action-audio.hpp index b48daea3..afa81685 100644 --- a/src/headers/macro-action-audio.hpp +++ b/src/headers/macro-action-audio.hpp @@ -16,7 +16,6 @@ enum class AudioAction { class MacroActionAudio : public MacroAction { public: MacroActionAudio(Macro *m) : MacroAction(m) {} - virtual ~MacroActionAudio(); bool PerformAction(); void LogAction(); bool Save(obs_data_t *obj); @@ -40,7 +39,6 @@ private: void StartMasterFade(); void FadeSourceVolume(); void FadeMasterVolume(); - std::thread _fadeThread; static bool _registered; static const std::string id; diff --git a/src/headers/macro.hpp b/src/headers/macro.hpp index ed9bbbb9..a4fa0ada 100644 --- a/src/headers/macro.hpp +++ b/src/headers/macro.hpp @@ -88,6 +88,7 @@ public: bool MatchOnChange() { return _matchOnChange; } int GetCount() { return _count; }; void ResetCount() { _count = 0; }; + void AddHelperThread(std::thread &&); bool GetStop() { return _stop; } void Stop(); std::deque> &Conditions() @@ -136,7 +137,8 @@ private: bool _die = false; bool _stop = false; bool _done = true; - std::thread _thread; + std::thread _backgroundThread; + std::vector _helperThreads; }; Macro *GetMacroByName(const char *name); diff --git a/src/macro-action-audio.cpp b/src/macro-action-audio.cpp index 30681b26..52177e11 100644 --- a/src/macro-action-audio.cpp +++ b/src/macro-action-audio.cpp @@ -18,13 +18,6 @@ const static std::map actionTypes = { "AdvSceneSwitcher.action.audio.type.masterVolume"}, }; -MacroActionAudio::~MacroActionAudio() -{ - if (_fadeThread.joinable()) { - _fadeThread.join(); - } -} - constexpr auto fadeInterval = std::chrono::milliseconds(100); constexpr float minFade = 0.000001f; @@ -103,12 +96,8 @@ void MacroActionAudio::StartSourceFade() if (_wait) { FadeSourceVolume(); } else { - if (_fadeThread.joinable()) { - return; - } - _fadeThread = - std::thread(&MacroActionAudio::FadeSourceVolume, this); - _fadeThread.detach(); + GetMacro()->AddHelperThread( + std::thread(&MacroActionAudio::FadeSourceVolume, this)); } } @@ -124,12 +113,8 @@ void MacroActionAudio::StartMasterFade() if (_wait) { FadeMasterVolume(); } else { - if (_fadeThread.joinable()) { - return; - } - _fadeThread = - std::thread(&MacroActionAudio::FadeMasterVolume, this); - _fadeThread.detach(); + GetMacro()->AddHelperThread( + std::thread(&MacroActionAudio::FadeMasterVolume, this)); } } diff --git a/src/macro.cpp b/src/macro.cpp index b43c0935..214271fe 100644 --- a/src/macro.cpp +++ b/src/macro.cpp @@ -30,10 +30,7 @@ Macro::Macro(const std::string &name) Macro::~Macro() { _die = true; - _stop = true; - if (_thread.joinable()) { - _thread.join(); - } + Stop(); ClearHotkeys(); } @@ -126,10 +123,10 @@ bool Macro::PerformActions(bool forceParallel, bool ignorePause) _done = false; bool ret = true; if (_runInParallel || forceParallel) { - if (_thread.joinable()) { - _thread.join(); + if (_backgroundThread.joinable()) { + _backgroundThread.join(); } - _thread = std::thread( + _backgroundThread = std::thread( [this, ignorePause] { RunActions(ignorePause); }); } else { RunActions(ret, ignorePause); @@ -159,8 +156,7 @@ void Macro::RunActions(bool &retVal, bool ignorePause) ret = ret && a->PerformAction(); if (!ret || (_paused && !ignorePause) || _stop || _die) { retVal = ret; - _done = true; - return; + break; } } _done = true; @@ -180,12 +176,28 @@ void Macro::SetPaused(bool pause) _paused = pause; } +void Macro::AddHelperThread(std::thread &&newThread) +{ + for (int i = 0; i < _helperThreads.size(); i++) { + if (!_helperThreads[i].joinable()) { + _helperThreads[i] = std::move(newThread); + return; + } + } + _helperThreads.push_back(std::move(newThread)); +} + void Macro::Stop() { _stop = true; switcher->macroWaitCv.notify_all(); - if (_thread.joinable()) { - _thread.join(); + for (auto &t : _helperThreads) { + if (t.joinable()) { + t.join(); + } + } + if (_backgroundThread.joinable()) { + _backgroundThread.join(); } }