Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Improve lock for effect object
Browse files Browse the repository at this point in the history
lockEffect is only necessary for these situations:
1. access effect object outside of UI thread
2. close/delete effect object anywhere
  • Loading branch information
walker-WSH authored and WizardCM committed Jan 13, 2022
1 parent 566c213 commit 106868e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
45 changes: 22 additions & 23 deletions VSTPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,28 @@ void VSTPlugin::cleanupChannelBuffers()

void VSTPlugin::loadEffectFromPath(std::string path)
{
std::lock_guard<std::recursive_mutex> lock(lockEffect);

if (this->pluginPath.compare(path) != 0) {
closeEditor();
unloadEffect();
blog(LOG_INFO, "User selected new VST plugin: '%s'", path.c_str());
}

if (!effect) {
// TODO: alert user of error if VST is not available.

pluginPath = path;
effect = loadEffect();

if (!effect) {
// TODO: alert user of error
blog(LOG_WARNING,
"VST Plug-in: Can't load "
"effect!");
AEffect *effectTemp = loadEffect();
if (!effectTemp) {
blog(LOG_WARNING, "VST Plug-in: Can't load effect!");
return;
}

{
std::lock_guard<std::recursive_mutex> lock(lockEffect);
effect = effectTemp;
}

// Check plug-in's magic number
// If incorrect, then the file either was not loaded properly,
// is not a real VST plug-in, or is otherwise corrupt.
Expand Down Expand Up @@ -221,6 +223,8 @@ void silenceChannel(float **channelData, int numChannels, long numFrames)

obs_audio_data *VSTPlugin::process(struct obs_audio_data *audio)
{
// Here we check the status firstly,
// which help avoid waiting for lock while unloadEffect() is running.
bool effectValid = (effect && effectReady && numChannels > 0);
if (!effectValid)
return audio;
Expand Down Expand Up @@ -260,16 +264,19 @@ obs_audio_data *VSTPlugin::process(struct obs_audio_data *audio)

void VSTPlugin::unloadEffect()
{
std::lock_guard<std::recursive_mutex> lock(lockEffect);
{
std::lock_guard<std::recursive_mutex> lock(lockEffect);

effectReady = false;
// Reset the status firstly to avoid VSTPlugin::process is blocked
effectReady = false;

if (effect) {
effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0);
effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
}
if (effect) {
effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0);
effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
}

effect = nullptr;
effect = nullptr;
}

unloadLibrary();
}
Expand All @@ -287,7 +294,6 @@ void VSTPlugin::onEditorClosed()
editorWidget->deleteLater();
editorWidget = nullptr;

std::lock_guard<std::recursive_mutex> lock(lockEffect);
if (effect && editorOpened) {
editorOpened = false;
effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0);
Expand All @@ -296,8 +302,6 @@ void VSTPlugin::onEditorClosed()

void VSTPlugin::openEditor()
{
std::lock_guard<std::recursive_mutex> lock(lockEffect);

if (effect && !editorWidget) {
// This check logic is refer to open source project : Audacity
if (!(effect->flags & effFlagsHasEditor)) {
Expand Down Expand Up @@ -363,8 +367,6 @@ std::string VSTPlugin::getChunk()

void VSTPlugin::setChunk(std::string data)
{
std::lock_guard<std::recursive_mutex> lock(lockEffect);

if (!effect) {
return;
}
Expand Down Expand Up @@ -398,8 +400,6 @@ void VSTPlugin::setChunk(std::string data)

void VSTPlugin::setProgram(const int programNumber)
{
std::lock_guard<std::recursive_mutex> lock(lockEffect);

if (programNumber < effect->numPrograms) {
effect->dispatcher(effect, effSetProgram, 0, programNumber, NULL, 0.0f);
} else {
Expand All @@ -409,7 +409,6 @@ void VSTPlugin::setProgram(const int programNumber)

int VSTPlugin::getProgram()
{
std::lock_guard<std::recursive_mutex> lock(lockEffect);
return effect->dispatcher(effect, effGetProgram, 0, 0, NULL, 0.0f);
}

Expand Down
3 changes: 3 additions & 0 deletions headers/VSTPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class EditorWidget;
class VSTPlugin : public QObject {
Q_OBJECT

/* Because effect is always changed in UI thread, so lockEffect is only necessary for these situations:
1. access effect object outside of UI thread;
2. close/delete effect object any where. */
std::recursive_mutex lockEffect;
AEffect * effect = nullptr;
obs_source_t * sourceContext;
Expand Down

0 comments on commit 106868e

Please sign in to comment.