Files
Minixer/Source/Plugin/PluginBridgeNode.cpp
XmengMD a9bbf9771f 修复插件槽加载/卸载交互缺陷,并解决删除插件、加载预设阻塞界面
问题
1. 插件加载中只在底部状态栏提示 Loading,槽位自身没有加载态;且加载中仍可再次点开
   load plugin,可对同一槽位重复发起加载。
2. 删除插件会阻塞界面(大型插件约 2 秒),加载 .minixer 预设时更严重——预设里 N 个
   插件会导致数秒至十几秒完全无响应;而加载单个插件却不卡。

根因
- 槽位只有“有/无插件”两种显示状态,而加载期间 pluginSlotNodes 必为 nullptr,被误判
  成空槽,于是所有空槽分支(可点击加载、显示 “+”、空槽右键菜单、可拖拽、不可复制)
  全部串错。
- 删除阻塞的根因不在槽位层:AudioProcessorGraph 的渲染序列
  GraphRenderSequence::NodeOp 持有 Node::Ptr 强引用(juce_AudioProcessorGraph.cpp:889),
  节点从图中摘除后仍被旧渲染序列保活;而 RenderSequenceExchange::set() 与其 500ms
  timerCallback() 都在消息线程销毁被淘汰的序列,因此
  ~PluginBridgeNode() -> shutdown() -> launcher->waitForExit(2000) 依旧在消息线程执行。
  预设加载会一次摘除 N 个节点、旧序列一次性析构,故冻结 N×2s。

修改
一、槽位进行中状态(PluginSlotComponent / ChannelStripComponent / MainComponent)
- 新增 PluginSlotBusyState{none, loading, removing} 取代原 bool loading;槽位自身显示
  “Loading xxx…/Removing xxx…”,加淡蓝底 + 强调色描边,进行中屏蔽加载、打开编辑器、
  拖拽、旁通等交互。
- 加载中右侧按钮改为“取消加载”(新增 pluginSlotLoadCancelRequested 回调),右键仅提供
  Cancel loading;卸载中不可取消,右键仅提示当前状态。
- 替换插件时不再先移除旧插件:加载期间旧插件继续工作,待新插件在后台就绪后再原子替换;
  加载失败或取消时保留原插件,不再出现替换瞬间断音与槽位清空。
- “卸载中”状态改由子进程真正退出后的回调解除,状态栏同步从 Removing… 更新为
  Slot N cleared(仅用户主动删除时回写,预设批量清空不刷屏)。

二、删除/替换/预设加载/退出不再阻塞(PluginHostLauncher / PluginBridgeNode)
- 新增 PluginHostProcessReaper:把子进程交给后台线程池完成“等待退出(宽限 3s)→ 超时
  强杀”,绝不在调用线程上等待;reapAsync 支持完成后在消息线程回调。
- PluginBridgeNode::shutdown() 不再同步 waitForExit,改为把 launcher 交给收割器;新增
  setShutdownCompletionCallback 供槽位解除“卸载中”状态。
- PluginHostLauncher 析构时强杀仍在运行的子进程(juce::ChildProcess 析构并不会结束进程),
  避免残留孤儿进程;程序退出时调用 drain(2000) 清场。

三、其他同源问题
- 槽位内容整体移位时,取消移位区间内所有在途加载,避免加载完成后写回错位槽位并产生
  孤立节点。
- 空槽与进行中的槽位不再可拖拽(mouseDrag 与 isInterestedInDragSource 双重判断)。
- 复制空槽/进行中槽位给出状态栏提示,不再静默无反馈。
- 跨实例粘贴改为按最后一个 ':' 拆分,插件名含 ':' 不再被截断。
- 右键 Move down 的硬编码槽位数 11 改为按实际槽位数判断。
2026-09-24 21:34:44 +08:00

323 lines
10 KiB
C++

/*
==============================================================================
PluginBridgeNode.cpp
==============================================================================
*/
#include "PluginBridgeNode.h"
namespace minixer
{
//==============================================================================
namespace
{
// (桥梁节点现统一按立体声总线构建,见构造函数注释。)
} // anonymous namespace
PluginBridgeNode::PluginBridgeNode (const juce::PluginDescription& description,
PluginArchitecture arch)
: juce::AudioProcessor (juce::AudioProcessor::BusesProperties()
.withInput ("Input", juce::AudioChannelSet::stereo(), true)
.withOutput ("Output", juce::AudioChannelSet::stereo(), true)),
pluginDescription (description),
pluginName (description.name),
architecture (arch),
// 音频缓冲按立体声(与本机混音器/子进程 2 通道布局一致)。
// 不能使用扫描描述中的通道数:VST3 扫描结果往往为 0/0(外壳与多数插件
// 不报告有效通道数),若按 0 构建总线会导致节点无通道、图无法连接而全程无声。
currentInputChannels (2),
currentOutputChannels (2),
crashState (std::make_shared<CrashState>())
{
}
PluginBridgeNode::~PluginBridgeNode()
{
shutdown();
}
//==============================================================================
void PluginBridgeNode::addListener (Listener* listener)
{
listeners.add (listener);
}
void PluginBridgeNode::removeListener (Listener* listener)
{
listeners.remove (listener);
}
//==============================================================================
bool PluginBridgeNode::initialize (double sampleRate, int bufferSize, juce::String& errorMessage)
{
shutdown();
isShuttingDown = false;
crashed.store (false);
crashReason.clear();
currentSampleRate = sampleRate;
currentBufferSize = bufferSize;
auto ipcKey = juce::Uuid().toString();
launcher = std::make_unique<PluginHostLauncher>();
PluginHostLaunchOptions options;
options.pluginId = ipcKey;
options.pluginPath = pluginDescription.fileOrIdentifier;
options.ipcKey = ipcKey;
options.mode = "runtime";
options.architecture = architecture;
// 共享音频缓冲按硬上限 kMaxAudioBufferFrames 一次性分配(见 PluginHostClient::connect),
// 而非加载时的 bufferSize:之后任何时机(含 ASIO 设备内置 Control Panel 内)
// 改变 Buffer Size,都不会让音频线程的拷贝越过共享内存边界。
options.maxFramesPerBlock = kMaxAudioBufferFrames;
if (auto xml = pluginDescription.createXml())
options.pluginDescriptionXmlB64 = juce::Base64::toBase64 (xml->toString ());
if (! launcher->launch (options))
{
errorMessage = launcher->getLastError();
launcher.reset();
return false;
}
client = std::make_unique<PluginHostClient>();
if (! client->connect (ipcKey, options.maxFramesPerBlock, currentInputChannels, currentOutputChannels))
{
errorMessage = client->getLastError();
client.reset();
launcher.reset();
return false;
}
// 等待子进程连接并初始化插件
if (! client->initPlugin (sampleRate, bufferSize))
{
errorMessage = client->getLastError();
if (launcher != nullptr && launcher->didCrash())
PluginBlacklist::getInstance().recordCrash (pluginDescription.fileOrIdentifier,
launcher->getExitCode());
shutdown();
return false;
}
initialized = true;
return true;
}
//==============================================================================
void PluginBridgeNode::setShutdownCompletionCallback (std::function<void()> callback)
{
shutdownCompletionCallback = std::move (callback);
}
//==============================================================================
void PluginBridgeNode::shutdown()
{
isShuttingDown = true;
if (crashState != nullptr)
crashState->alive.store (false);
// 1. 先关闭编辑器窗口,避免子进程退出后留下悬空窗口。
hideEditorWindow();
if (client != nullptr)
{
client->shutdown();
client.reset();
}
// 2. 子进程的退出(卸载 DLL、释放采样库)可能耗时数秒,且本函数可能在
// AudioProcessorGraph 回收渲染序列时的消息线程上被调用,因此绝不能在
// 调用线程上等待:交给后台收割器完成「等待退出 → 超时强杀」。
auto completionCallback = std::move (shutdownCompletionCallback);
shutdownCompletionCallback = nullptr;
if (launcher != nullptr)
{
PluginHostProcessReaper::getInstance().reapAsync (
std::shared_ptr<PluginHostLauncher> (std::move (launcher)),
std::move (completionCallback));
}
else if (completionCallback != nullptr)
{
// 没有子进程可等待:立即回调,避免槽位一直停留在“卸载中”
juce::MessageManager::callAsync (std::move (completionCallback));
}
initialized = false;
crashed.store (false);
crashReason.clear();
}
//==============================================================================
void PluginBridgeNode::prepareToPlay (double sampleRate, int samplesPerBlockExpected)
{
currentSampleRate = sampleRate;
currentBufferSize = samplesPerBlockExpected;
if (client != nullptr && initialized)
client->prepareToPlay (sampleRate, samplesPerBlockExpected);
}
//==============================================================================
void PluginBridgeNode::releaseResources()
{
if (client != nullptr && initialized)
client->releaseResources();
}
//==============================================================================
void PluginBridgeNode::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& /*midiMessages*/)
{
if (crashed.load() || ! initialized || client == nullptr || ! client->isConnected())
{
buffer.clear();
return;
}
const int numSamples = buffer.getNumSamples();
client->writeInput (buffer, numSamples);
if (! client->processBlock (numSamples))
{
buffer.clear();
// processBlock 失败可能是子进程崩溃或挂起。
if (launcher != nullptr && ! launcher->isRunning())
handleProcessFailure ("crash");
else
handleProcessFailure ("hang");
return;
}
client->readOutput (buffer, numSamples);
}
//==============================================================================
void PluginBridgeNode::getStateInformation (juce::MemoryBlock& destData)
{
if (client != nullptr && initialized)
client->getState (destData);
}
//==============================================================================
void PluginBridgeNode::setStateInformation (const void* data, int sizeInBytes)
{
if (client != nullptr && initialized)
{
juce::MemoryBlock state (data, static_cast<size_t> (sizeInBytes));
client->setState (state);
}
}
//==============================================================================
bool PluginBridgeNode::isBusesLayoutSupported (const BusesLayout& layouts) const
{
return layouts.getMainInputChannelSet() == juce::AudioChannelSet::stereo()
&& layouts.getMainOutputChannelSet() == juce::AudioChannelSet::stereo();
}
//==============================================================================
bool PluginBridgeNode::isPluginRunning() const
{
return initialized && client != nullptr && client->isConnected()
&& launcher != nullptr && launcher->isRunning();
}
//==============================================================================
juce::String PluginBridgeNode::getLastError() const
{
if (client != nullptr)
return client->getLastError();
if (launcher != nullptr)
return launcher->getLastError();
return {};
}
//==============================================================================
void PluginBridgeNode::showEditorWindow (juce::Component* parent)
{
if (client != nullptr && initialized)
{
void* handle = nullptr;
if (parent != nullptr)
handle = reinterpret_cast<void*> (parent->getWindowHandle());
client->showEditor (handle);
}
}
//==============================================================================
void PluginBridgeNode::hideEditorWindow()
{
if (client != nullptr && initialized)
client->hideEditor();
}
//==============================================================================
void PluginBridgeNode::handleProcessFailure (const juce::String& reason)
{
// 避免在主动关闭或已经报告过崩溃时重复记录。
bool expected = false;
if (! crashed.compare_exchange_strong (expected, true))
return;
crashReason = reason;
const auto filePath = pluginDescription.fileOrIdentifier;
const int exitCode = launcher != nullptr ? launcher->getExitCode() : 0;
if (reason == "crash")
PluginBlacklist::getInstance().recordCrash (filePath, exitCode);
else
PluginBlacklist::getInstance().recordScanFailure (filePath, reason);
// 确保子进程不再继续运行,避免音频线程反复超时。
if (launcher != nullptr && launcher->isRunning())
launcher->terminateProcess();
notifyCrashAsync();
}
//==============================================================================
void PluginBridgeNode::notifyCrashAsync()
{
// 从音频线程安全地切换到消息线程通知监听器。
auto weakState = std::weak_ptr<CrashState> (crashState);
auto* self = this;
juce::MessageManager::callAsync ([self, weakState]()
{
auto state = weakState.lock();
if (state == nullptr || ! state->alive.load())
return;
self->listeners.call ([self] (Listener& l)
{
l.pluginBridgeNodeCrashed (self);
});
});
}
} // namespace minixer