feat: embed native VST GUI in mastering + fx rack panels (bridge --fx-gui frame capture + input relay)
Linux parts (0L1-4L1 per TASKS_LINUX_WINDOWS.md): - native_bridge: FxGuiServer.cpp HTTP server (Winsock) — /frame BitBlt + PrintWindow fallback -> JPEG (libjpeg-turbo q70), /input PostMessage relay (mouse/wheel/key, coords scaled), /close, /ping; RenderFxJob fxGuiSetup + run_fx_gui_server (offscreen); main.cpp --fx-gui dispatch; vcpkg + CMakeLists WIN32-only + JPEG. - api: open_fx_gui embed flag spawn --fx-gui, parse SF_FXGUI_PORT -> embed_url, fx-gui/close endpoint; 4 unit tests. - ui: VstGuiEmbed component (frame refresh 100ms, ping 2s, input relay, scale coords) shared by MasteringModal + FXRackModal; swap graph<->GUI, cleanup on unmount, dead-bridge toast fallback. - docs: TASKS_LINUX_WINDOWS.md, WALKTHROUGH_WINDOWS.md, PLAN status. Windows tasks 0W1-4W1 remain (build + E2E real VST).
This commit is contained in:
@@ -0,0 +1,318 @@
|
||||
// native_bridge/src/FxGuiServer.cpp
|
||||
// Embedded VST GUI server (Phase 0 of PLAN_MASTERBUS_VST_GUI_EMBED.md):
|
||||
// `daw_vst_bridge --fx-gui <job.json>` loads ONE VST3 effect plugin, attaches
|
||||
// its native editor to an off-screen host window, and serves the window as
|
||||
// JPEG frames over a tiny HTTP server so the web frontend can embed it in a
|
||||
// panel (frame-capture + input relay, VNC-style). Windows only.
|
||||
//
|
||||
// Routes:
|
||||
// GET /ping -> {"ok":true}
|
||||
// GET /frame -> image/jpeg (BitBlt, fallback PrintWindow)
|
||||
// POST /input -> {type:"mousedown|mousemove|mouseup|wheel|keydown|keyup",
|
||||
// x,y,button,deltaY,keyCode} -> PostMessage to HWND
|
||||
// POST /close -> detach + exit 0
|
||||
//
|
||||
// The HTTP server runs on a worker thread; the calling (STA) thread runs the
|
||||
// message pump — VST3 editors need COM apartment + pump on the same thread.
|
||||
// The listen port is random and printed to stdout as `SF_FXGUI_PORT=<port>`.
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
#include <jpeglib.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
// sheredom/json.h (vendored with the VST3 SDK) — same include as RenderFxJob.cpp.
|
||||
#include "vst3sdk/public.sdk/source/vst/moduleinfo/json.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// --- tiny JSON helpers (same API as RenderFxJob.cpp) ------------------------
|
||||
const json_value_s* memberValue(const json_object_s* o, const char* key) {
|
||||
const json_object_element_s* m = member(o, key);
|
||||
return m ? m->value : nullptr;
|
||||
}
|
||||
std::string memberString(const json_object_s* o, const char* key, const std::string& def) {
|
||||
const json_value_s* v = memberValue(o, key);
|
||||
if (v && v->type == json_type_string) {
|
||||
const auto* s = static_cast<const json_string_s*>(v->payload);
|
||||
return std::string(s->string, s->string_size);
|
||||
}
|
||||
return def;
|
||||
}
|
||||
int64_t memberInt(const json_object_s* o, const char* key, int64_t def) {
|
||||
const json_value_s* v = memberValue(o, key);
|
||||
if (v && v->type == json_type_number)
|
||||
return (int64_t)std::atof(static_cast<const json_number_s*>(v->payload)->number);
|
||||
return def;
|
||||
}
|
||||
|
||||
// --- HTTP plumbing -----------------------------------------------------------
|
||||
struct HttpReq {
|
||||
std::string method;
|
||||
std::string path;
|
||||
std::string body;
|
||||
};
|
||||
|
||||
// Read one request (headers up to \r\n\r\n + Content-Length body). Returns
|
||||
// false on socket error or malformed request line.
|
||||
bool readRequest(SOCKET c, HttpReq& out) {
|
||||
std::string raw;
|
||||
char buf[4096];
|
||||
while (raw.find("\r\n\r\n") == std::string::npos) {
|
||||
if (raw.size() > 16384) return false;
|
||||
int n = recv(c, buf, (int)sizeof(buf), 0);
|
||||
if (n <= 0) return false;
|
||||
raw.append(buf, (size_t)n);
|
||||
}
|
||||
const size_t headEnd = raw.find("\r\n\r\n");
|
||||
const std::string head = raw.substr(0, headEnd);
|
||||
const size_t sp1 = head.find(' ');
|
||||
const size_t sp2 = head.find(' ', sp1 + 1);
|
||||
if (sp1 == std::string::npos || sp2 == std::string::npos) return false;
|
||||
out.method = head.substr(0, sp1);
|
||||
out.path = head.substr(sp1 + 1, sp2 - sp1 - 1);
|
||||
|
||||
long contentLength = 0;
|
||||
size_t pos = 0;
|
||||
while ((pos = head.find("\r\n", pos)) != std::string::npos) {
|
||||
const size_t lineEnd = head.find("\r\n", pos + 2);
|
||||
const std::string line = head.substr(pos + 2, lineEnd == std::string::npos ? std::string::npos : lineEnd - pos - 2);
|
||||
pos = lineEnd == std::string::npos ? std::string::npos : lineEnd;
|
||||
if (line.rfind("Content-Length:", 0) == 0 || line.rfind("content-length:", 0) == 0) {
|
||||
contentLength = std::atol(line.c_str() + 15);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (contentLength > 0) {
|
||||
while (raw.size() < headEnd + 4 + (size_t)contentLength) {
|
||||
int n = recv(c, buf, (int)sizeof(buf), 0);
|
||||
if (n <= 0) return false;
|
||||
raw.append(buf, (size_t)n);
|
||||
}
|
||||
out.body = raw.substr(headEnd + 4, (size_t)contentLength);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void sendResponse(SOCKET c, int code, const char* contentType,
|
||||
const void* data, size_t len) {
|
||||
char hdr[512];
|
||||
std::snprintf(hdr, sizeof(hdr),
|
||||
"HTTP/1.1 %d %s\r\nContent-Type: %s\r\n"
|
||||
"Content-Length: %zu\r\nAccess-Control-Allow-Origin: *\r\n"
|
||||
"Connection: close\r\n\r\n",
|
||||
code, code == 200 ? "OK" : "Error", contentType, len);
|
||||
send(c, hdr, (int)std::strlen(hdr), 0);
|
||||
if (len > 0) send(c, (const char*)data, (int)len, 0);
|
||||
}
|
||||
|
||||
// --- frame capture -----------------------------------------------------------
|
||||
// BitBlt the window client rect; if the result is all-black (plugin uses a
|
||||
// layered/DirectX surface GDI cannot see), fall back to PrintWindow with
|
||||
// PW_RENDERFULLCONTENT. Returns JPEG bytes in `out`.
|
||||
bool captureHwndToJpeg(HWND hwnd, std::vector<unsigned char>& out, int quality) {
|
||||
RECT rc;
|
||||
if (!GetClientRect(hwnd, &rc)) return false;
|
||||
const int w = rc.right - rc.left;
|
||||
const int h = rc.bottom - rc.top;
|
||||
if (w <= 0 || h <= 0) return false;
|
||||
|
||||
HDC win = GetDC(hwnd);
|
||||
HDC mem = CreateCompatibleDC(win);
|
||||
HBITMAP bmp = CreateCompatibleBitmap(win, w, h);
|
||||
HGDIOBJ oldBmp = SelectObject(mem, bmp);
|
||||
|
||||
std::vector<unsigned char> px((size_t)w * h * 4);
|
||||
auto grab = [&]() -> bool {
|
||||
BITMAPINFO bi = {};
|
||||
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bi.bmiHeader.biWidth = w;
|
||||
bi.bmiHeader.biHeight = -h; // top-down
|
||||
bi.bmiHeader.biPlanes = 1;
|
||||
bi.bmiHeader.biBitCount = 32;
|
||||
bi.bmiHeader.biCompression = BI_RGB;
|
||||
return GetDIBits(mem, bmp, 0, h, px.data(), &bi, DIB_RGB_COLORS) == h;
|
||||
};
|
||||
auto isAllBlack = [&]() -> bool {
|
||||
// Sample a coarse grid of pixels; all zero => invisible to GDI.
|
||||
const int step = 8;
|
||||
for (int y = 0; y < h; y += step)
|
||||
for (int x = 0; x < w; x += step) {
|
||||
const size_t i = ((size_t)y * w + x) * 4;
|
||||
if (px[i] || px[i + 1] || px[i + 2]) return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
bool ok = BitBlt(mem, 0, 0, w, h, win, 0, 0, SRCCOPY | CAPTUREBLT) != 0 && grab();
|
||||
if (ok && isAllBlack()) {
|
||||
ok = PrintWindow(hwnd, mem, PW_RENDERFULLCONTENT) != 0 && grab();
|
||||
}
|
||||
|
||||
SelectObject(mem, oldBmp);
|
||||
DeleteObject(bmp);
|
||||
DeleteDC(mem);
|
||||
ReleaseDC(hwnd, win);
|
||||
if (!ok) return false;
|
||||
|
||||
// BGRA (GDI) -> RGB, then JPEG.
|
||||
std::vector<unsigned char> rgb((size_t)w * h * 3);
|
||||
for (int i = 0; i < w * h; ++i) {
|
||||
rgb[(size_t)i * 3 + 0] = px[(size_t)i * 4 + 2];
|
||||
rgb[(size_t)i * 3 + 1] = px[(size_t)i * 4 + 1];
|
||||
rgb[(size_t)i * 3 + 2] = px[(size_t)i * 4 + 0];
|
||||
}
|
||||
|
||||
jpeg_compress_struct cinfo = {};
|
||||
jpeg_error_mgr jerr;
|
||||
cinfo.err = jpeg_std_error(&jerr);
|
||||
jpeg_create_compress(&cinfo);
|
||||
unsigned char* memBuf = nullptr;
|
||||
unsigned long memSize = 0;
|
||||
jpeg_mem_dest(&cinfo, &memBuf, &memSize);
|
||||
cinfo.image_width = (JDIMENSION)w;
|
||||
cinfo.image_height = (JDIMENSION)h;
|
||||
cinfo.input_components = 3;
|
||||
cinfo.in_color_space = JCS_RGB;
|
||||
jpeg_set_defaults(&cinfo);
|
||||
jpeg_set_quality(&cinfo, quality, TRUE);
|
||||
jpeg_start_compress(&cinfo, TRUE);
|
||||
JSAMPROW row = rgb.data();
|
||||
while (cinfo.next_scanline < cinfo.image_height)
|
||||
jpeg_write_scanlines(&cinfo, &row, 1);
|
||||
jpeg_finish_compress(&cinfo);
|
||||
if (memBuf && memSize > 0) out.assign(memBuf, memBuf + memSize);
|
||||
std::free(memBuf);
|
||||
jpeg_destroy_compress(&cinfo);
|
||||
return !out.empty();
|
||||
}
|
||||
|
||||
// --- input relay -------------------------------------------------------------
|
||||
void relayInput(HWND hwnd, const json_object_s* o) {
|
||||
const std::string type = memberString(o, "type", "");
|
||||
const int x = (int)memberInt(o, "x", 0);
|
||||
const int y = (int)memberInt(o, "y", 0);
|
||||
const int button = (int)memberInt(o, "button", 0);
|
||||
const int deltaY = (int)memberInt(o, "deltaY", 0);
|
||||
const WPARAM keyCode = (WPARAM)memberInt(o, "keyCode", 0);
|
||||
const LPARAM lp = MAKELPARAM(x, y);
|
||||
|
||||
if (type == "mousemove") {
|
||||
PostMessageA(hwnd, WM_MOUSEMOVE, 0, lp);
|
||||
} else if (type == "mousedown") {
|
||||
UINT m = button == 2 ? WM_RBUTTONDOWN : button == 1 ? WM_MBUTTONDOWN : WM_LBUTTONDOWN;
|
||||
PostMessageA(hwnd, m, MAKEWPARAM(0, button == 2 ? MK_RBUTTON : button == 1 ? MK_MBUTTON : MK_LBUTTON), lp);
|
||||
PostMessageA(hwnd, WM_MOUSEMOVE, 0, lp); // plugins tracking absolute pos
|
||||
} else if (type == "mouseup") {
|
||||
UINT m = button == 2 ? WM_RBUTTONUP : button == 1 ? WM_MBUTTONUP : WM_LBUTTONUP;
|
||||
PostMessageA(hwnd, m, 0, lp);
|
||||
} else if (type == "wheel") {
|
||||
PostMessageA(hwnd, WM_MOUSEWHEEL, MAKEWPARAM(0, (short)(-(int64_t)deltaY * 120)), lp);
|
||||
} else if (type == "keydown") {
|
||||
SetFocus(hwnd);
|
||||
PostMessageA(hwnd, WM_KEYDOWN, keyCode, 0);
|
||||
} else if (type == "keyup") {
|
||||
PostMessageA(hwnd, WM_KEYUP, keyCode, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// --- HTTP server loop (worker thread) ---------------------------------------
|
||||
void runHttpServer(HWND hwnd, std::atomic<bool>& stop) {
|
||||
WSADATA wsa;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { std::cerr << "[FxGuiServer] WSAStartup failed" << std::endl; return; }
|
||||
SOCKET lsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (lsock == INVALID_SOCKET) { WSACleanup(); return; }
|
||||
sockaddr_in addr = {};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = 0; // random free port
|
||||
if (bind(lsock, (sockaddr*)&addr, sizeof(addr)) != 0 || listen(lsock, 4) != 0) {
|
||||
closesocket(lsock);
|
||||
WSACleanup();
|
||||
std::cerr << "[FxGuiServer] bind/listen failed" << std::endl;
|
||||
return;
|
||||
}
|
||||
sockaddr_in bound = {};
|
||||
int blen = sizeof(bound);
|
||||
getsockname(lsock, (sockaddr*)&bound, &blen);
|
||||
std::cout << "SF_FXGUI_PORT=" << ntohs(bound.sin_port) << std::endl;
|
||||
std::cout.flush();
|
||||
|
||||
while (!stop.load()) {
|
||||
fd_set rf;
|
||||
FD_ZERO(&rf);
|
||||
FD_SET(lsock, &rf);
|
||||
timeval tv = { 0, 100000 }; // 100ms poll so /close can break out
|
||||
int sel = select(0, &rf, nullptr, nullptr, &tv);
|
||||
if (sel <= 0) continue;
|
||||
SOCKET c = accept(lsock, nullptr, nullptr);
|
||||
if (c == INVALID_SOCKET) continue;
|
||||
|
||||
HttpReq req;
|
||||
if (!readRequest(c, req)) { closesocket(c); continue; }
|
||||
if (req.path == "/ping") {
|
||||
sendResponse(c, 200, "application/json", "{\"ok\":true}", 11);
|
||||
} else if (req.path == "/frame") {
|
||||
std::vector<unsigned char> jpg;
|
||||
if (captureHwndToJpeg(hwnd, jpg, 70))
|
||||
sendResponse(c, 200, "image/jpeg", jpg.data(), jpg.size());
|
||||
else
|
||||
sendResponse(c, 500, "application/json", "{\"error\":\"capture failed\"}", 25);
|
||||
} else if (req.path == "/input") {
|
||||
json_parse_result_s pres = {};
|
||||
json_value_s* root = json_parse_ex(req.body.data(), req.body.size(),
|
||||
json_parse_flags_default, nullptr, nullptr, &pres);
|
||||
if (root && root->type == json_type_object) {
|
||||
relayInput(hwnd, static_cast<const json_object_s*>(root->payload));
|
||||
std::free(root);
|
||||
sendResponse(c, 200, "application/json", "{\"ok\":true}", 11);
|
||||
} else {
|
||||
if (root) std::free(root);
|
||||
sendResponse(c, 400, "application/json", "{\"error\":\"bad json\"}", 20);
|
||||
}
|
||||
} else if (req.path == "/close") {
|
||||
sendResponse(c, 200, "application/json", "{\"ok\":true}", 11);
|
||||
stop.store(true);
|
||||
PostMessageA(hwnd, WM_CLOSE); // pump exits -> process returns
|
||||
} else {
|
||||
sendResponse(c, 404, "application/json", "{\"error\":\"not found\"}", 22);
|
||||
}
|
||||
closesocket(c);
|
||||
}
|
||||
closesocket(lsock);
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Runs the embedded GUI server: starts the HTTP worker thread, then pumps
|
||||
// messages on this (STA) thread until the host window is destroyed (user
|
||||
// closed it or POST /close). Returns 0.
|
||||
int fxGuiServerLoop(HWND hwnd) {
|
||||
std::atomic<bool> stop{false};
|
||||
std::thread http(runHttpServer, hwnd, std::ref(stop));
|
||||
MSG msg;
|
||||
while (IsWindow(hwnd)) {
|
||||
BOOL r = GetMessageA(&msg, nullptr, 0, 0);
|
||||
if (r <= 0) break;
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessageA(&msg);
|
||||
}
|
||||
stop.store(true);
|
||||
if (http.joinable()) http.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
||||
Reference in New Issue
Block a user