Compare commits

..

6 Commits

Author SHA1 Message Date
WarmUpTill
8dd2170dfa Update locale 2023-01-31 20:23:14 +01:00
WarmUpTill
5f3a3dd5cf Dynamically increase delay between screenshots up to one second
Otherwise users with a very low switcher interval might not be able to
use the preview dialog at all
2023-01-31 11:20:18 -08:00
WarmUpTill
8ad3563963 Always call obs_remove_tick_callback() in destructor
Seems like in some scenarios a screenshot thread would still be active
while the ScreenshotHelper object was deleted
2023-01-31 11:20:18 -08:00
WarmUpTill
492128ef86 Avoid deadlocks while opening settings window and calling frontend API 2023-01-31 11:20:18 -08:00
WarmUpTill
b4936274f2 Synchronize re-ordering and grouping of macros 2023-01-31 11:20:18 -08:00
WarmUpTill
64ccd5ba53 Detect invalid group setups and attempt to clean them up 2023-01-31 11:20:18 -08:00
11 changed files with 93 additions and 44 deletions

View File

@@ -17,7 +17,7 @@ AdvSceneSwitcher.generalTab.status.autoStart.never="Never"
AdvSceneSwitcher.generalTab.status.autoStart.recording="Recording"
AdvSceneSwitcher.generalTab.status.autoStart.streaming="Streaming"
AdvSceneSwitcher.generalTab.status.autoStart.recordingAndStreaming="Recording or Streaming"
AdvSceneSwitcher.generalTab.status.checkInterval="Check switch conditions every"
AdvSceneSwitcher.generalTab.status.checkInterval="Check conditions every"
AdvSceneSwitcher.generalTab.generalBehavior="General behavior"
AdvSceneSwitcher.generalTab.generalBehavior.onNoMet="If no actions are performed for"
AdvSceneSwitcher.generalTab.generalBehavior.onNoMetDelayTooltip="Will only ever be as accurate as the configured check interval."

View File

@@ -244,6 +244,7 @@ void SwitcherData::Thread()
switcher->firstIntervalAfterStop = false;
}
mainLoopLock = nullptr;
blog(LOG_INFO, "stopped");
}

View File

@@ -106,25 +106,12 @@ bool MacroActionSwitchScene::WaitForTransition(OBSWeakSource &scene,
scene, transition, _duration.seconds);
switcher->abortMacroWait = false;
bool isInMainLoop = QThread::currentThread() == switcher->th;
if (isInMainLoop) {
if (expectedTransitionDuration < 0) {
waitForTransitionChange(transition, switcher->GetLock(),
GetMacro());
} else {
waitForTransitionChangeFixedDuration(
expectedTransitionDuration, switcher->GetLock(),
GetMacro());
}
std::unique_lock<std::mutex> lock(switcher->m);
if (expectedTransitionDuration < 0) {
waitForTransitionChange(transition, &lock, GetMacro());
} else {
std::mutex temp;
std::unique_lock<std::mutex> lock(temp);
if (expectedTransitionDuration < 0) {
waitForTransitionChange(transition, &lock, GetMacro());
} else {
waitForTransitionChangeFixedDuration(
expectedTransitionDuration, &lock, GetMacro());
}
waitForTransitionChangeFixedDuration(expectedTransitionDuration,
&lock, GetMacro());
}
return !switcher->abortMacroWait;

View File

@@ -52,14 +52,8 @@ bool MacroActionWait::PerformAction()
std::chrono::milliseconds((int)(sleepDuration * 1000));
switcher->abortMacroWait = false;
bool isInMainLoop = QThread::currentThread() == switcher->th;
if (isInMainLoop) {
waitHelper(switcher->GetLock(), GetMacro(), time);
} else {
std::mutex temp;
std::unique_lock<std::mutex> lock(temp);
waitHelper(&lock, GetMacro(), time);
}
std::unique_lock<std::mutex> lock(switcher->m);
waitHelper(&lock, GetMacro(), time);
return !switcher->abortMacroWait;
}

View File

@@ -1,6 +1,7 @@
#include "macro-tree.hpp"
#include "macro.hpp"
#include "utility.hpp"
#include "switcher-data-structs.hpp"
#include <obs.h>
#include <string>
@@ -563,6 +564,7 @@ void MacroTreeModel::GroupSelectedItems(QModelIndexList &indices)
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
QString name = GetNewGroupName();
std::vector<std::shared_ptr<Macro>> items;
items.reserve(indices.size());
@@ -606,6 +608,7 @@ void MacroTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
for (int i = indices.count() - 1; i >= 0; i--) {
std::shared_ptr<Macro> item = _macros[ModelIndexToMacroIndex(
indices[i].row(), _macros)];
@@ -966,6 +969,7 @@ void MacroTree::dropEvent(QDropEvent *event)
}
// Move items in backend
std::lock_guard<std::mutex> lock(switcher->m);
int to = row;
try {
to = ModelIndexToMacroIndex(row, items);

View File

@@ -34,7 +34,7 @@ Macro::CreateGroup(const std::string &name,
{
auto group = std::make_shared<Macro>(name, false);
for (auto &c : children) {
c->SetParent(group.get());
c->SetParent(group);
}
group->_isGroup = true;
group->_groupSize = children.size();
@@ -82,7 +82,7 @@ void Macro::PrepareMoveToGroup(std::shared_ptr<Macro> group,
oldGroup->_groupSize--;
}
item->SetParent(group.get());
item->SetParent(group);
if (group) {
group->_groupSize++;
}
@@ -204,8 +204,9 @@ bool Macro::PerformActions(bool forceParallel, bool ignorePause)
RunActions(ret, ignorePause);
}
_wasExecutedRecently = true;
if (_parent) {
_parent->_wasExecutedRecently = true;
auto group = _parent.lock();
if (group) {
group->_wasExecutedRecently = true;
}
return ret;
}
@@ -313,6 +314,12 @@ void Macro::UpdateConditionIndices()
}
}
Macro *Macro::Parent()
{
auto p = _parent.lock();
return p.get();
}
bool Macro::Save(obs_data_t *obj) const
{
obs_data_set_string(obj, "name", _name.c_str());
@@ -709,18 +716,41 @@ void SwitcherData::loadMacros(obs_data_t *obj)
obs_data_array_release(macroArray);
int groupCount = 0;
Macro *group = nullptr;
std::shared_ptr<Macro> group;
std::vector<std::shared_ptr<Macro>> invalidGroups;
for (auto &m : macros) {
if (groupCount && m->IsGroup()) {
blog(LOG_ERROR,
"nested group detected - will delete \"%s\"",
m->Name().c_str());
invalidGroups.emplace_back(m);
continue;
}
if (groupCount) {
m->SetParent(group);
groupCount--;
}
if (m->IsGroup()) {
groupCount = m->GroupSize();
group = m.get();
group = m;
}
m->PostLoad();
}
if (groupCount) {
blog(LOG_ERROR,
"invalid group size detected - will delete \"%s\"",
group->Name().c_str());
invalidGroups.emplace_back(group);
}
for (auto &m : invalidGroups) {
auto it = std::find(macros.begin(), macros.end(), m);
if (it == macros.end()) {
continue;
}
macros.erase(it);
}
}
bool SwitcherData::checkMacros()
@@ -741,8 +771,28 @@ bool SwitcherData::checkMacros()
bool SwitcherData::runMacros()
{
for (auto m : macros) {
if (m->Matched()) {
// Create copy of macor list as elements might be removed, inserted, or
// reordered while macros are currently being executed.
// For example, this can happen if a macro is performing a wait action,
// as the main lock will be unlocked during this time.
auto runPhaseMacros = macros;
// Avoid deadlocks when opening settings window and calling frontend
// API functions at the same time.
//
// If the timing is just right, the frontend API call will call
// QMetaObject::invokeMethod(...) with Qt::BlockingQueuedConnection
// while holding the main switcher mutex.
// But this invokeMethod call itself will be blocked as it is waiting
// the constructor of AdvSceneSwitcher() to complete.
// The constructor of AdvSceneSwitcher() cannot continue however as it
// cannot lock the main switcher mutex.
if (GetLock()) {
GetLock()->unlock();
}
for (auto &m : runPhaseMacros) {
if (m && m->Matched()) {
vblog(LOG_INFO, "running macro: %s", m->Name().c_str());
if (!m->PerformActions()) {
blog(LOG_WARNING, "abort macro: %s",
@@ -750,6 +800,9 @@ bool SwitcherData::runMacros()
}
}
}
if (GetLock()) {
GetLock()->lock();
}
return true;
}

View File

@@ -55,11 +55,11 @@ public:
std::shared_ptr<Macro> item);
bool IsGroup() { return _isGroup; }
uint32_t GroupSize() { return _groupSize; }
bool IsSubitem() { return !!_parent; }
bool IsSubitem() { return !_parent.expired(); }
void SetCollapsed(bool val) { _isCollapsed = val; }
bool IsCollapsed() { return _isCollapsed; }
void SetParent(Macro *m) { _parent = m; }
Macro *Parent() { return _parent; }
void SetParent(std::shared_ptr<Macro> m) { _parent = m; }
Macro *Parent();
bool Save(obs_data_t *obj) const;
bool Load(obs_data_t *obj);
@@ -93,7 +93,7 @@ private:
std::deque<std::shared_ptr<MacroCondition>> _conditions;
std::deque<std::shared_ptr<MacroAction>> _actions;
Macro *_parent = nullptr;
std::weak_ptr<Macro> _parent;
uint32_t _groupSize = 0;
bool _runInParallel = false;

View File

@@ -198,12 +198,21 @@ void PreviewDialog::CheckForMatchLoop()
if (_stop || isHidden()) {
return;
}
if (!screenshot.done || !_video.ValidSelection()) {
if (!_video.ValidSelection()) {
_statusLabel->setText(obs_module_text(
"AdvSceneSwitcher.condition.video.screenshotFail"));
_imageLabel->setPixmap(QPixmap());
continue;
}
if (!screenshot.done) {
_statusLabel->setText(obs_module_text(
"AdvSceneSwitcher.condition.video.screenshotFail"));
_imageLabel->setPixmap(QPixmap());
if (_delay < 1000) {
_delay += 50;
}
continue;
}
if (screenshot.image.width() == 0 ||
screenshot.image.height() == 0) {
_statusLabel->setText(obs_module_text(
@@ -220,6 +229,8 @@ void PreviewDialog::CheckForMatchLoop()
_areaParams.area.height);
}
MarkMatch(screenshot.image);
} else {
_statusLabel->setText("");
}
_imageLabel->setPixmap(QPixmap::fromImage(screenshot.image));
}

View File

@@ -90,7 +90,7 @@ struct SwitcherData {
std::condition_variable cv;
std::mutex m;
std::unique_lock<std::mutex> *mainLoopLock;
std::unique_lock<std::mutex> *mainLoopLock = nullptr;
bool transitionActive = false;
bool waitForTransition = false;

View File

@@ -39,9 +39,8 @@ ScreenshotHelper::~ScreenshotHelper()
gs_stagesurface_destroy(stagesurf);
gs_texrender_destroy(texrender);
obs_leave_graphics();
obs_remove_tick_callback(ScreenshotTick, this);
}
obs_remove_tick_callback(ScreenshotTick, this);
if (_saveThread.joinable()) {
_saveThread.join();
}

View File

@@ -36,7 +36,7 @@ public:
std::chrono::high_resolution_clock::time_point time;
private:
bool _initDone = false;
std::atomic_bool _initDone = false;
bool _blocking = false;
std::thread _saveThread;
bool _saveToFile = false;