1
0

BridgeCommon.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * BridgeCommon.h
  3. * --------------
  4. * Purpose: Declarations of stuff that's common between the VST bridge and bridge wrapper.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include <vector>
  12. #if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
  13. #include <intrin.h>
  14. #endif
  15. OPENMPT_NAMESPACE_BEGIN
  16. // Insert some object at the end of a char vector.
  17. template <typename T>
  18. static void PushToVector(std::vector<char> &data, const T &obj, size_t writeSize = sizeof(T))
  19. {
  20. static_assert(!std::is_pointer<T>::value, "Won't push pointers to data vectors.");
  21. const char *objC = reinterpret_cast<const char *>(&obj);
  22. data.insert(data.end(), objC, objC + writeSize);
  23. }
  24. static void PushZStringToVector(std::vector<char> &data, const char *str)
  25. {
  26. if(str != nullptr)
  27. data.insert(data.end(), str, str + strlen(str));
  28. PushToVector(data, char(0));
  29. }
  30. OPENMPT_NAMESPACE_END
  31. #include "AEffectWrapper.h"
  32. #include "BridgeOpCodes.h"
  33. OPENMPT_NAMESPACE_BEGIN
  34. // Internal data structures
  35. // Event to notify other threads
  36. class Event
  37. {
  38. private:
  39. HANDLE handle = nullptr;
  40. public:
  41. Event() = default;
  42. ~Event() { Close(); }
  43. // Create a new event
  44. bool Create(bool manual = false, const wchar_t *name = nullptr)
  45. {
  46. Close();
  47. handle = CreateEventW(nullptr, manual ? TRUE : FALSE, FALSE, name);
  48. return handle != nullptr;
  49. }
  50. // Duplicate a local event
  51. bool DuplicateFrom(HANDLE source)
  52. {
  53. Close();
  54. return DuplicateHandle(GetCurrentProcess(), source, GetCurrentProcess(), &handle, 0, FALSE, DUPLICATE_SAME_ACCESS) != FALSE;
  55. }
  56. void Close()
  57. {
  58. CloseHandle(handle);
  59. }
  60. void Trigger()
  61. {
  62. SetEvent(handle);
  63. }
  64. void Reset()
  65. {
  66. ResetEvent(handle);
  67. }
  68. void operator=(const HANDLE other) { handle = other; }
  69. operator const HANDLE() const { return handle; }
  70. };
  71. // Two-way event
  72. class Signal
  73. {
  74. public:
  75. Event send, confirm;
  76. // Create new (local) signal
  77. bool Create()
  78. {
  79. return send.Create() && confirm.Create();
  80. }
  81. // Create signal from name (for inter-process communication)
  82. bool Create(const wchar_t *name, const wchar_t *addendum)
  83. {
  84. wchar_t fullName[64 + 1];
  85. wcscpy(fullName, name);
  86. wcscat(fullName, addendum);
  87. fullName[std::size(fullName) - 1] = L'\0';
  88. size_t nameLen = wcslen(fullName);
  89. wcscpy(fullName + nameLen, L"-s");
  90. bool success = send.Create(false, fullName);
  91. wcscpy(fullName + nameLen, L"-a");
  92. return success && confirm.Create(false, fullName);
  93. }
  94. // Create signal from other signal
  95. bool DuplicateFrom(const Signal &other)
  96. {
  97. return send.DuplicateFrom(other.send)
  98. && confirm.DuplicateFrom(other.confirm);
  99. }
  100. void Send()
  101. {
  102. send.Trigger();
  103. }
  104. void Confirm()
  105. {
  106. confirm.Trigger();
  107. }
  108. };
  109. // Memory that can be shared between processes
  110. class MappedMemory
  111. {
  112. protected:
  113. struct Header
  114. {
  115. uint32 size;
  116. };
  117. HANDLE mapFile = nullptr;
  118. Header *view = nullptr;
  119. public:
  120. MappedMemory() = default;
  121. ~MappedMemory() { Close(); }
  122. // Create a shared memory object.
  123. bool Create(const wchar_t *name, uint32 size)
  124. {
  125. Close();
  126. mapFile = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, size + sizeof(Header), name);
  127. if(!mapFile)
  128. {
  129. return false;
  130. }
  131. view = static_cast<Header *>(MapViewOfFile(mapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0));
  132. if(!view)
  133. {
  134. return false;
  135. }
  136. view->size = size;
  137. return Good();
  138. }
  139. // Open an existing shared memory object.
  140. bool Open(const wchar_t *name)
  141. {
  142. Close();
  143. mapFile = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, name);
  144. if(!mapFile)
  145. {
  146. return false;
  147. }
  148. view = static_cast<Header *>(MapViewOfFile(mapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0));
  149. if(!view)
  150. {
  151. return false;
  152. }
  153. return Good();
  154. }
  155. // Close this shared memory object.
  156. void Close()
  157. {
  158. if(mapFile)
  159. {
  160. if(view)
  161. {
  162. UnmapViewOfFile(view);
  163. view = nullptr;
  164. }
  165. CloseHandle(mapFile);
  166. mapFile = nullptr;
  167. }
  168. }
  169. template <typename T = void>
  170. T *Data() const
  171. {
  172. if(view == nullptr)
  173. return nullptr;
  174. else
  175. return reinterpret_cast<T *>(view + 1);
  176. }
  177. size_t Size() const
  178. {
  179. if(!view)
  180. return 0;
  181. else
  182. return view->size;
  183. }
  184. bool Good() const { return view != nullptr; }
  185. // Make a copy and detach it from the other object
  186. void CopyFrom(MappedMemory &other)
  187. {
  188. Close();
  189. mapFile = other.mapFile;
  190. view = other.view;
  191. other.mapFile = nullptr;
  192. other.view = nullptr;
  193. }
  194. };
  195. // Bridge communication data
  196. #pragma pack(push, 8)
  197. // Simple atomic value that has a guaranteed size and layout for the shared memory
  198. template <typename T>
  199. struct BridgeAtomic
  200. {
  201. public:
  202. BridgeAtomic() = default;
  203. BridgeAtomic<T> &operator=(const T value)
  204. {
  205. static_assert(sizeof(m_value) >= sizeof(T));
  206. MPT_ASSERT((intptr_t(&m_value) & 3) == 0);
  207. #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  208. InterlockedExchange(&m_value, static_cast<LONG>(value));
  209. #else
  210. _InterlockedExchange(&m_value, static_cast<LONG>(value));
  211. #endif
  212. return *this;
  213. }
  214. operator T() const
  215. {
  216. #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  217. return static_cast<T>(InterlockedAdd(&m_value, 0));
  218. #else
  219. return static_cast<T>(_InterlockedExchangeAdd(&m_value, 0));
  220. #endif
  221. }
  222. T exchange(T desired)
  223. {
  224. #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  225. return static_cast<T>(InterlockedExchange(&m_value, static_cast<LONG>(desired)));
  226. #else
  227. return static_cast<T>(_InterlockedExchange(&m_value, static_cast<LONG>(desired)));
  228. #endif
  229. }
  230. T fetch_add(T arg)
  231. {
  232. #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  233. return static_cast<T>(InterlockedExchangeAdd(&m_value, static_cast<LONG>(arg)));
  234. #else
  235. return static_cast<T>(_InterlockedExchangeAdd(&m_value, static_cast<LONG>(arg)));
  236. #endif
  237. }
  238. bool compare_exchange_strong(T &expected, T desired)
  239. {
  240. #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  241. return InterlockedCompareExchange(&m_value, static_cast<LONG>(desired), static_cast<LONG>(expected)) == static_cast<LONG>(expected);
  242. #else
  243. return _InterlockedCompareExchange(&m_value, static_cast<LONG>(desired), static_cast<LONG>(expected)) == static_cast<LONG>(expected);
  244. #endif
  245. }
  246. private:
  247. mutable LONG m_value;
  248. };
  249. // Host-to-bridge parameter automation message
  250. struct AutomationQueue
  251. {
  252. struct Parameter
  253. {
  254. uint32 index;
  255. float value;
  256. };
  257. BridgeAtomic<int32> pendingEvents; // Number of pending automation events
  258. Parameter params[64]; // Automation events
  259. };
  260. // Host-to-bridge message to initiate a process call.
  261. struct ProcessMsg
  262. {
  263. enum ProcessType : int32
  264. {
  265. process = 0,
  266. processReplacing,
  267. processDoubleReplacing,
  268. };
  269. int32 processType;
  270. int32 numInputs;
  271. int32 numOutputs;
  272. int32 sampleFrames;
  273. // Input and output buffers follow
  274. };
  275. // General message header
  276. struct MsgHeader
  277. {
  278. enum BridgeMessageType : uint32
  279. {
  280. // Management messages, host to bridge
  281. newInstance,
  282. init,
  283. // Management messages, bridge to host
  284. errorMsg,
  285. exceptionMsg,
  286. // VST messages, common
  287. dispatch,
  288. // VST messages, host to bridge
  289. setParameter,
  290. getParameter,
  291. automate,
  292. };
  293. BridgeAtomic<bool> isUsed;
  294. uint32 size; // Size of complete message, including this header
  295. uint32 type; // See BridgeMessageType
  296. };
  297. // Host-to-bridge new instance message
  298. struct NewInstanceMsg : public MsgHeader
  299. {
  300. wchar_t memName[64]; // Shared memory object name;
  301. };
  302. // Host-to-bridge initialization message
  303. struct InitMsg : public MsgHeader
  304. {
  305. int32 result;
  306. int32 hostPtrSize; // Size of VstIntPtr in host
  307. uint32 mixBufSize; // Interal mix buffer size (for shared memory audio buffers)
  308. int32 pluginID; // ID to use when sending messages to host
  309. uint32 fullMemDump; // When crashing, create full memory dumps instead of stack dumps
  310. wchar_t str[_MAX_PATH]; // Plugin file to load. Out: Error message if result != 0.
  311. };
  312. // Host-to-bridge or bridge-to-host VST dispatch message
  313. struct DispatchMsg : public MsgHeader
  314. {
  315. int32 opcode;
  316. int32 index;
  317. int64 value;
  318. int64 ptr; // Usually, this will be the size of whatever ptr points to. In that case, the data itself is stored after this struct.
  319. float opt;
  320. int32 result;
  321. };
  322. // Host-to-bridge VST setParameter / getParameter message
  323. struct ParameterMsg : public MsgHeader
  324. {
  325. int32 index; // Parameter ID
  326. float value; // Parameter value (in/out)
  327. };
  328. // Bridge-to-host error message
  329. struct ErrorMsg : public MsgHeader
  330. {
  331. wchar_t str[64];
  332. };
  333. // Universal bridge message format
  334. union BridgeMessage
  335. {
  336. MsgHeader header;
  337. NewInstanceMsg newInstance;
  338. InitMsg init;
  339. DispatchMsg dispatch;
  340. ParameterMsg parameter;
  341. ErrorMsg error;
  342. uint8 dummy[2048]; // Enough space for most default structs, e.g. 2x speaker negotiation struct
  343. void SetType(MsgHeader::BridgeMessageType msgType, uint32 size)
  344. {
  345. header.isUsed = true;
  346. header.size = size;
  347. header.type = msgType;
  348. }
  349. void NewInstance(const wchar_t *memName)
  350. {
  351. SetType(MsgHeader::newInstance, sizeof(NewInstanceMsg));
  352. wcsncpy(newInstance.memName, memName, std::size(newInstance.memName) - 1);
  353. }
  354. void Init(const wchar_t *pluginPath, uint32 mixBufSize, int32 pluginID, bool fullMemDump)
  355. {
  356. SetType(MsgHeader::init, sizeof(InitMsg));
  357. init.result = 0;
  358. init.hostPtrSize = sizeof(intptr_t);
  359. init.mixBufSize = mixBufSize;
  360. init.pluginID = pluginID;
  361. init.fullMemDump = fullMemDump;
  362. wcsncpy(init.str, pluginPath, std::size(init.str) - 1);
  363. }
  364. void Dispatch(int32 opcode, int32 index, int64 value, int64 ptr, float opt, uint32 extraDataSize)
  365. {
  366. SetType(MsgHeader::dispatch, sizeof(DispatchMsg) + extraDataSize);
  367. dispatch.result = 0;
  368. dispatch.opcode = opcode;
  369. dispatch.index = index;
  370. dispatch.value = value;
  371. dispatch.ptr = ptr;
  372. dispatch.opt = opt;
  373. }
  374. void Dispatch(Vst::VstOpcodeToHost opcode, int32 index, int64 value, int64 ptr, float opt, uint32 extraDataSize)
  375. {
  376. Dispatch(static_cast<int32>(opcode), index, value, ptr, opt, extraDataSize);
  377. }
  378. void Dispatch(Vst::VstOpcodeToPlugin opcode, int32 index, int64 value, int64 ptr, float opt, uint32 extraDataSize)
  379. {
  380. Dispatch(static_cast<int32>(opcode), index, value, ptr, opt, extraDataSize);
  381. }
  382. void SetParameter(int32 index, float value)
  383. {
  384. SetType(MsgHeader::setParameter, sizeof(ParameterMsg));
  385. parameter.index = index;
  386. parameter.value = value;
  387. }
  388. void GetParameter(int32 index)
  389. {
  390. SetType(MsgHeader::getParameter, sizeof(ParameterMsg));
  391. parameter.index = index;
  392. parameter.value = 0.0f;
  393. }
  394. void Automate()
  395. {
  396. // Dummy message
  397. SetType(MsgHeader::automate, sizeof(MsgHeader));
  398. }
  399. void Error(const wchar_t *text)
  400. {
  401. SetType(MsgHeader::errorMsg, sizeof(ErrorMsg));
  402. wcsncpy(error.str, text, std::size(error.str) - 1);
  403. error.str[std::size(error.str) - 1] = 0;
  404. }
  405. // Copy message to target and clear delivery status
  406. void CopyTo(BridgeMessage &target)
  407. {
  408. std::memcpy(&target, this, std::min(static_cast<size_t>(header.size), sizeof(BridgeMessage)));
  409. header.isUsed = false;
  410. }
  411. };
  412. // This is the maximum size of dispatch data that can be sent in a message. If you want to dispatch more data, use a secondary medium for sending it along.
  413. inline constexpr size_t DISPATCH_DATA_SIZE = (sizeof(BridgeMessage) - sizeof(DispatchMsg));
  414. static_assert(DISPATCH_DATA_SIZE >= 256, "There should be room for at least 256 bytes of dispatch data!");
  415. // The array size should be more than enough for any realistic scenario with nested and simultaneous dispatch calls
  416. inline constexpr int MSG_STACK_SIZE = 16;
  417. using MsgStack = std::array<BridgeMessage, MSG_STACK_SIZE>;
  418. // Ensuring that our HWND looks the same to both 32-bit and 64-bit processes
  419. struct BridgeHWND
  420. {
  421. public:
  422. void operator=(HWND handle) { m_handle = static_cast<int32>(reinterpret_cast<intptr_t>(handle)); }
  423. operator HWND() const { return reinterpret_cast<HWND>(static_cast<intptr_t>(m_handle)); }
  424. protected:
  425. BridgeAtomic<int32> m_handle;
  426. };
  427. // Layout of the shared memory chunk
  428. struct SharedMemLayout
  429. {
  430. union
  431. {
  432. Vst::AEffect effect; // Native layout from host perspective
  433. AEffect32 effect32;
  434. AEffect64 effect64;
  435. };
  436. MsgStack ipcMessages;
  437. AutomationQueue automationQueue;
  438. Vst::VstTimeInfo timeInfo;
  439. BridgeHWND hostCommWindow;
  440. BridgeHWND bridgeCommWindow;
  441. int32 bridgePluginID;
  442. BridgeAtomic<int32> tailSize;
  443. BridgeAtomic<int32> audioThreadToHostMsgID;
  444. BridgeAtomic<int32> audioThreadToBridgeMsgID;
  445. };
  446. static_assert(sizeof(Vst::AEffect) <= sizeof(AEffect64), "Something's going very wrong here.");
  447. // For caching parameter information
  448. struct ParameterInfo
  449. {
  450. Vst::VstParameterProperties props;
  451. char name[64];
  452. char label[64];
  453. char display[64];
  454. };
  455. #pragma pack(pop)
  456. // Common variables that we will find in both the host and plugin side of the bridge (this is not shared memory)
  457. class BridgeCommon
  458. {
  459. public:
  460. BridgeCommon()
  461. {
  462. m_instanceCount++;
  463. }
  464. ~BridgeCommon()
  465. {
  466. m_instanceCount--;
  467. }
  468. protected:
  469. enum WindowMessage : UINT
  470. {
  471. WM_BRIDGE_KEYFIRST = WM_USER + 4000, // Must be consistent with VSTEditor.cpp!
  472. WM_BRIDGE_KEYLAST = WM_BRIDGE_KEYFIRST + WM_KEYLAST - WM_KEYFIRST,
  473. WM_BRIDGE_MESSAGE_TO_BRIDGE,
  474. WM_BRIDGE_MESSAGE_TO_HOST,
  475. WM_BRIDGE_DELETE_PLUGIN,
  476. WM_BRIDGE_SUCCESS = 1337,
  477. };
  478. static std::vector<BridgeCommon *> m_plugins;
  479. static HWND m_communicationWindow;
  480. static int m_instanceCount;
  481. static thread_local bool m_isAudioThread;
  482. // Signals for host <-> bridge communication
  483. Signal m_sigToHostAudio, m_sigToBridgeAudio;
  484. Signal m_sigProcessAudio;
  485. Event m_sigBridgeReady;
  486. Event m_otherProcess; // Handle of "other" process (host handle in the bridge and vice versa)
  487. // Shared memory segments
  488. MappedMemory m_queueMem; // AEffect, message, some fixed size VST structures
  489. MappedMemory m_processMem; // Process message + sample buffer
  490. MappedMemory m_getChunkMem; // effGetChunk temporary memory
  491. MappedMemory m_eventMem; // VstEvents memory
  492. // Pointer into shared memory
  493. SharedMemLayout /*volatile*/ *m_sharedMem = nullptr;
  494. // Pointer size of the "other" side of the bridge, in bytes
  495. int32 m_otherPtrSize = 0;
  496. int32 m_thisPluginID = 0;
  497. int32 m_otherPluginID = 0;
  498. static void CreateCommunicationWindow(WNDPROC windowProc)
  499. {
  500. static constexpr TCHAR windowClassName[] = _T("OpenMPTPluginBridgeCommunication");
  501. static bool registered = false;
  502. if(!registered)
  503. {
  504. registered = true;
  505. WNDCLASSEX wndClass;
  506. wndClass.cbSize = sizeof(WNDCLASSEX);
  507. wndClass.style = CS_HREDRAW | CS_VREDRAW;
  508. wndClass.lpfnWndProc = windowProc;
  509. wndClass.cbClsExtra = 0;
  510. wndClass.cbWndExtra = 0;
  511. wndClass.hInstance = GetModuleHandle(nullptr);
  512. wndClass.hIcon = nullptr;
  513. wndClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
  514. wndClass.hbrBackground = nullptr;
  515. wndClass.lpszMenuName = nullptr;
  516. wndClass.lpszClassName = windowClassName;
  517. wndClass.hIconSm = nullptr;
  518. RegisterClassEx(&wndClass);
  519. }
  520. m_communicationWindow = CreateWindow(
  521. windowClassName,
  522. _T("OpenMPT Plugin Bridge Communication"),
  523. WS_POPUP,
  524. CW_USEDEFAULT,
  525. CW_USEDEFAULT,
  526. 1,
  527. 1,
  528. HWND_MESSAGE,
  529. nullptr,
  530. GetModuleHandle(nullptr),
  531. nullptr);
  532. }
  533. bool CreateSignals(const wchar_t *mapName)
  534. {
  535. wchar_t readyName[64];
  536. wcscpy(readyName, mapName);
  537. wcscat(readyName, L"rdy");
  538. return m_sigToHostAudio.Create(mapName, L"sha")
  539. && m_sigToBridgeAudio.Create(mapName, L"sba")
  540. && m_sigProcessAudio.Create(mapName, L"prc")
  541. && m_sigBridgeReady.Create(false, readyName);
  542. }
  543. // Copy a message to shared memory and return relative position.
  544. int CopyToSharedMemory(const BridgeMessage &msg, MsgStack &stack)
  545. {
  546. MPT_ASSERT(msg.header.isUsed);
  547. for(int i = 0; i < MSG_STACK_SIZE; i++)
  548. {
  549. BridgeMessage &targetMsg = stack[i];
  550. bool expected = false;
  551. if(targetMsg.header.isUsed.compare_exchange_strong(expected, true))
  552. {
  553. std::memcpy(&targetMsg, &msg, std::min(sizeof(BridgeMessage), size_t(msg.header.size)));
  554. return i;
  555. }
  556. }
  557. return -1;
  558. }
  559. };
  560. OPENMPT_NAMESPACE_END