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
This commit is contained in:
WarmUpTill
2022-03-01 21:32:30 +01:00
committed by WarmUpTill
parent 7095f4668c
commit e1bacd75b6
4 changed files with 30 additions and 33 deletions

View File

@@ -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;

View File

@@ -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<std::shared_ptr<MacroCondition>> &Conditions()
@@ -136,7 +137,8 @@ private:
bool _die = false;
bool _stop = false;
bool _done = true;
std::thread _thread;
std::thread _backgroundThread;
std::vector<std::thread> _helperThreads;
};
Macro *GetMacroByName(const char *name);

View File

@@ -18,13 +18,6 @@ const static std::map<AudioAction, std::string> 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));
}
}

View File

@@ -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();
}
}