Files
Minixer/Source/Plugin/PluginHostClient.cpp
T
XmengMD 01eade3e8a PluginHost 隔离进程增强:编辑器窗口托管、IPC 字节流协议与音频通道适配
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 统一按立体声总线构建音频图。
2026-09-17 17:16:14 +08:00

369 lines
12 KiB
C++

/*
==============================================================================
PluginHostClient.cpp
==============================================================================
*/
#include "PluginHostClient.h"
namespace minixer
{
//==============================================================================
// Init 握手等待子进程加载插件(含 license 校验)完成的最长时间。
// UI 加载已异步化不阻塞,此值只作为防挂死兜底,因而取较宽松的 120 秒。
static constexpr int kInitResultTimeoutMs = 120000;
//==============================================================================
PluginHostClient::PluginHostClient() = default;
PluginHostClient::~PluginHostClient()
{
disconnect();
}
//==============================================================================
bool PluginHostClient::connect (const juce::String& ipcKey,
uint32_t maxFrames,
uint32_t numInputs,
uint32_t numOutputs)
{
disconnect();
maxFramesPerBlock = maxFrames;
// 共享内存尺寸必须以两侧一致的最小通道数计算:子进程固定按 (2,2) 打开映射,
// 而插件描述里扫描得到的通道数可能为 0 / 不准确。这里统一钳到至少 2 通道,
// 保证主进程创建的映射 ≥ 子进程打开所需尺寸(MapViewOfFile 不允许超尺寸视图)。
numInputChannels = juce::jmax (2u, numInputs);
numOutputChannels = juce::jmax (2u, numOutputs);
sharedMemory = createDefaultSharedMemoryRegion();
if (sharedMemory == nullptr)
{
lastError = "Shared memory implementation not available";
return false;
}
const size_t audioShmSize = AudioSharedMemoryLayout::getTotalSize (maxFramesPerBlock,
numInputChannels,
numOutputChannels);
if (! sharedMemory->create (ipcKey, audioShmSize))
{
lastError = "Failed to create shared memory";
return false;
}
audioLayout = static_cast<AudioSharedMemoryLayout*> (sharedMemory->getAddress());
if (audioLayout == nullptr)
{
lastError = "Failed to map shared memory";
return false;
}
audioLayout->maxFramesPerBlock = maxFramesPerBlock;
audioLayout->numInputChannels = numInputChannels;
audioLayout->numOutputChannels = numOutputChannels;
transport = createDefaultIpcTransport();
if (transport == nullptr)
{
lastError = "IPC transport implementation not available";
return false;
}
if (! transport->connect (ipcKey))
{
lastError = "Failed to connect IPC transport";
return false;
}
return true;
}
//==============================================================================
void PluginHostClient::disconnect()
{
if (transport != nullptr && transport->isConnected())
{
shutdown();
transport->close();
}
transport.reset();
audioLayout = nullptr;
sharedMemory.reset();
}
//==============================================================================
bool PluginHostClient::initPlugin (double sampleRate, int bufferSize)
{
MessageBuilder payload;
payload.writeUInt32 (static_cast<uint32_t> (sampleRate));
payload.writeUInt32 (static_cast<uint32_t> (bufferSize));
payload.writeUInt32 (numInputChannels);
payload.writeUInt32 (numOutputChannels);
const auto requestId = nextRequestId();
if (! sendCommand (ControlMessageType::Init, payload.getData(), requestId))
return false;
juce::MemoryBlock response;
if (! readResponse (response, ControlMessageType::InitResult, requestId, kInitResultTimeoutMs))
return false;
MessageReader reader (response);
bool success = false;
reader.readBool (success);
return success;
}
//==============================================================================
bool PluginHostClient::prepareToPlay (double sampleRate, int bufferSize)
{
MessageBuilder payload;
payload.writeUInt32 (static_cast<uint32_t> (sampleRate));
payload.writeUInt32 (static_cast<uint32_t> (bufferSize));
return sendCommand (ControlMessageType::PrepareToPlay, payload.getData(), nextRequestId());
}
//==============================================================================
bool PluginHostClient::releaseResources()
{
return sendCommand (ControlMessageType::ReleaseResources, {}, nextRequestId());
}
//==============================================================================
bool PluginHostClient::processBlock (int numSamples)
{
if (audioLayout == nullptr)
return false;
const uint32_t currentSeq = audioLayout->hostWriteSeq.load (std::memory_order_relaxed) + 1;
audioLayout->hostWriteSeq.store (currentSeq, std::memory_order_release);
MessageBuilder payload;
payload.writeUInt32 (static_cast<uint32_t> (numSamples));
const auto requestId = nextRequestId();
if (! sendCommand (ControlMessageType::ProcessBlock, payload.getData(), requestId))
return false;
// 等待子进程完成:pluginWriteSeq 等于 currentSeq 表示处理完成。
const auto timeout = juce::Time::getCurrentTime() + juce::RelativeTime::milliseconds (5000);
while (audioLayout->pluginWriteSeq.load (std::memory_order_acquire) != currentSeq)
{
if (juce::Time::getCurrentTime() > timeout)
{
lastError = "PluginHost process block timeout";
return false;
}
juce::Thread::sleep (1);
}
return true;
}
//==============================================================================
void PluginHostClient::writeInput (const juce::AudioBuffer<float>& inputBuffer, int numSamples)
{
if (audioLayout == nullptr)
return;
const auto inputChans = static_cast<uint32_t> (inputBuffer.getNumChannels());
const auto chansToWrite = juce::jmin (numInputChannels, inputChans);
for (uint32_t ch = 0; ch < chansToWrite; ++ch)
{
auto* dst = audioLayout->getInputChannelData (ch, maxFramesPerBlock,
numInputChannels, numOutputChannels);
std::memcpy (dst, inputBuffer.getReadPointer (static_cast<int> (ch)),
static_cast<size_t> (numSamples) * sizeof (float));
}
}
//==============================================================================
void PluginHostClient::readOutput (juce::AudioBuffer<float>& outputBuffer, int numSamples)
{
if (audioLayout == nullptr)
return;
const auto outputChans = static_cast<uint32_t> (outputBuffer.getNumChannels());
const auto chansToRead = juce::jmin (numOutputChannels, outputChans);
for (uint32_t ch = 0; ch < chansToRead; ++ch)
{
auto* src = audioLayout->getOutputChannelData (ch, maxFramesPerBlock,
numInputChannels, numOutputChannels);
std::memcpy (outputBuffer.getWritePointer (static_cast<int> (ch)),
src,
static_cast<size_t> (numSamples) * sizeof (float));
}
}
//==============================================================================
bool PluginHostClient::setState (const juce::MemoryBlock& state)
{
MessageBuilder payload;
payload.writeMemoryBlock (state);
return sendCommand (ControlMessageType::SetState, payload.getData(), nextRequestId());
}
//==============================================================================
bool PluginHostClient::getState (juce::MemoryBlock& state)
{
const auto requestId = nextRequestId();
if (! sendCommand (ControlMessageType::GetState, {}, requestId))
return false;
juce::MemoryBlock response;
if (! readResponse (response, ControlMessageType::StateData, requestId, 30000))
return false;
MessageReader reader (response);
return reader.readMemoryBlock (state);
}
//==============================================================================
bool PluginHostClient::setParameter (int index, float value)
{
MessageBuilder payload;
payload.writeUInt32 (static_cast<uint32_t> (index));
payload.writeFloat (value);
return sendCommand (ControlMessageType::SetParameter, payload.getData(), nextRequestId());
}
//==============================================================================
int PluginHostClient::getLatencySamples()
{
const auto requestId = nextRequestId();
if (! sendCommand (ControlMessageType::GetLatency, {}, requestId))
return 0;
juce::MemoryBlock response;
if (! readResponse (response, ControlMessageType::LatencyInfo, requestId, 30000))
return 0;
MessageReader reader (response);
uint32_t latency = 0;
reader.readUInt32 (latency);
return static_cast<int> (latency);
}
//==============================================================================
bool PluginHostClient::showEditor (void* parentWindowHandle)
{
MessageBuilder payload;
payload.writeUInt64 (reinterpret_cast<uintptr_t> (parentWindowHandle));
return sendCommand (ControlMessageType::ShowEditor, payload.getData(), nextRequestId());
}
//==============================================================================
bool PluginHostClient::hideEditor()
{
return sendCommand (ControlMessageType::HideEditor, {}, nextRequestId());
}
//==============================================================================
bool PluginHostClient::shutdown()
{
if (transport == nullptr || ! transport->isConnected())
return false;
return sendCommand (ControlMessageType::Shutdown, {}, nextRequestId());
}
//==============================================================================
bool PluginHostClient::isConnected() const
{
return transport != nullptr && transport->isConnected();
}
//==============================================================================
bool PluginHostClient::sendCommand (ControlMessageType type,
const juce::MemoryBlock& payload,
uint64_t requestId)
{
if (transport == nullptr)
{
lastError = "Transport not connected";
return false;
}
MessageBuilder builder;
builder.getData() = payload;
auto frame = builder.buildWithHeader (type, requestId);
return transport->sendMessage (frame);
}
//==============================================================================
bool PluginHostClient::readResponse (juce::MemoryBlock& payload,
ControlMessageType expectedType,
uint64_t requestId,
int timeoutMs)
{
if (transport == nullptr)
return false;
const auto deadline = juce::Time::getCurrentTime() + juce::RelativeTime::milliseconds (timeoutMs);
while (true)
{
juce::MemoryBlock frame;
const int remainingMs = juce::jmax (0, static_cast<int> ((deadline - juce::Time::getCurrentTime()).inMilliseconds()));
if (! transport->readMessage (frame, remainingMs))
{
lastError = "Timeout waiting for response";
return false;
}
if (frame.getSize() < ControlHeader::size)
continue;
ControlHeader header;
std::memcpy (&header, frame.getData(), ControlHeader::size);
if (! header.isValid())
continue;
payload.replaceAll (static_cast<const uint8_t*> (frame.getData()) + ControlHeader::size,
frame.getSize() - ControlHeader::size);
if (header.requestId == requestId && static_cast<ControlMessageType> (header.type) == expectedType)
return true;
// 异步通知(日志、错误、编辑器关闭)暂不处理,继续等待目标响应。
if (static_cast<ControlMessageType> (header.type) == ControlMessageType::Error)
{
MessageReader reader (payload);
uint32_t code = 0;
juce::String msg;
reader.readUInt32 (code);
reader.readString (msg);
lastError = msg;
}
}
}
//==============================================================================
uint64_t PluginHostClient::nextRequestId()
{
return currentRequestId++;
}
} // namespace minixer