mirror of
https://github.com/XmengMD/Minixer.git
synced 2026-12-05 08:00:37 +00:00
PluginHost 控制循环(IPC 阻塞读)移入独立后台线程,消息线程进入 runDispatchLoop 驱动插件编辑器 UI;安装与主程序一致的 MixerLookAndFeel,版本号统一取 ProjectInfo。 PluginHostServer 将 wrapper 改为 shared_ptr 并注入编辑器关闭回调;编辑器显示/隐藏经 callAsync 转投消息线程,按消息线程销毁规则释放 GPU 资源;Init 布局设置失败不再回退,插件按默认布局继续出声。 PluginWrapper 编辑器改用独立 PluginEditorWindow(原生标题栏 + 深色背景 + 关闭回调销毁);processBlock 增加常规/回退双路径,按插件实际通道数适配缓冲,解决通道不匹配导致的无声;新增 VST3 外壳插件 name 校验,不符则重建一次。 IPC:命名管道改为字节流模式并将长度头与负载单次写入,消除消息边界错位;共享内存子进程侧映射整个区域,避免映射尺寸不一致;PluginHostClient 通道数钳到至少 2 与子进程 (2,2) 布局对齐,Init 超时放宽至 120s。PluginBridgeNode 统一按立体声总线构建音频图。
307 lines
9.0 KiB
C++
307 lines
9.0 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;
|
|
options.maxFramesPerBlock = static_cast<uint32_t> (bufferSize);
|
|
|
|
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::shutdown()
|
|
{
|
|
isShuttingDown = true;
|
|
|
|
if (crashState != nullptr)
|
|
crashState->alive.store (false);
|
|
|
|
// 1. 先关闭编辑器窗口,避免子进程退出后留下悬空窗口。
|
|
hideEditorWindow();
|
|
|
|
if (client != nullptr)
|
|
{
|
|
client->shutdown();
|
|
client.reset();
|
|
}
|
|
|
|
if (launcher != nullptr)
|
|
{
|
|
if (launcher->isRunning())
|
|
{
|
|
if (! launcher->waitForExit (2000))
|
|
launcher->terminateProcess();
|
|
}
|
|
|
|
launcher.reset();
|
|
}
|
|
|
|
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
|