Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1720 lines
68KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef BUILD_BRIDGE
  18. # error This file should not be compiled if not building bridge
  19. #endif
  20. // must be first so we can undef CARLA_SAFE_*_RETURN_ERR* macros
  21. #include "CarlaPluginInternal.hpp"
  22. #undef CARLA_SAFE_ASSERT_RETURN_ERR
  23. #undef CARLA_SAFE_ASSERT_RETURN_ERRN
  24. #undef CARLA_SAFE_EXCEPTION_RETURN_ERR
  25. #undef CARLA_SAFE_EXCEPTION_RETURN_ERRN
  26. #include "CarlaEngineClient.hpp"
  27. #include "CarlaEngineInit.hpp"
  28. #include "CarlaBackendUtils.hpp"
  29. #include "CarlaBase64Utils.hpp"
  30. #include "CarlaBridgeUtils.hpp"
  31. #include "CarlaTimeUtils.hpp"
  32. #include "CarlaMIDI.h"
  33. #ifdef __SSE2_MATH__
  34. # include <xmmintrin.h>
  35. #endif
  36. #include "water/files/File.h"
  37. #include "water/misc/Time.h"
  38. // must be last
  39. #include "jackbridge/JackBridge.hpp"
  40. using water::File;
  41. using water::MemoryBlock;
  42. using water::String;
  43. CARLA_BACKEND_START_NAMESPACE
  44. // -----------------------------------------------------------------------
  45. // Bridge Engine client
  46. struct LatencyChangedCallback {
  47. virtual ~LatencyChangedCallback() noexcept {}
  48. virtual void latencyChanged(const uint32_t samples) noexcept = 0;
  49. };
  50. class CarlaEngineBridgeClient : public CarlaEngineClientForSubclassing
  51. {
  52. public:
  53. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  54. CarlaEngineBridgeClient(const CarlaEngine& engine,
  55. EngineInternalGraph& egraph,
  56. const CarlaPluginPtr plugin,
  57. LatencyChangedCallback* const cb)
  58. : CarlaEngineClientForSubclassing(engine, egraph, plugin),
  59. fLatencyCallback(cb) {}
  60. #else
  61. CarlaEngineBridgeClient(const CarlaEngine& engine, LatencyChangedCallback* const cb)
  62. : CarlaEngineClientForSubclassing(engine),
  63. fLatencyCallback(cb) {}
  64. #endif
  65. protected:
  66. void setLatency(const uint32_t samples) noexcept override
  67. {
  68. if (getLatency() == samples)
  69. return;
  70. fLatencyCallback->latencyChanged(samples);
  71. CarlaEngineClient::setLatency(samples);
  72. }
  73. private:
  74. LatencyChangedCallback* const fLatencyCallback;
  75. CARLA_DECLARE_NON_COPYABLE(CarlaEngineBridgeClient)
  76. };
  77. // -------------------------------------------------------------------
  78. struct BridgeTextReader {
  79. char* text;
  80. BridgeTextReader(BridgeNonRtClientControl& nonRtClientCtrl)
  81. : text(nullptr)
  82. {
  83. const uint32_t size = nonRtClientCtrl.readUInt();
  84. CARLA_SAFE_ASSERT_RETURN(size != 0,);
  85. text = new char[size + 1];
  86. nonRtClientCtrl.readCustomData(text, size);
  87. text[size] = '\0';
  88. }
  89. BridgeTextReader(BridgeNonRtClientControl& nonRtClientCtrl, const uint32_t size)
  90. : text(nullptr)
  91. {
  92. text = new char[size + 1];
  93. if (size != 0)
  94. nonRtClientCtrl.readCustomData(text, size);
  95. text[size] = '\0';
  96. }
  97. ~BridgeTextReader() noexcept
  98. {
  99. delete[] text;
  100. }
  101. CARLA_DECLARE_NON_COPYABLE(BridgeTextReader)
  102. };
  103. // -------------------------------------------------------------------
  104. class CarlaEngineBridge : public CarlaEngine,
  105. private CarlaThread,
  106. private LatencyChangedCallback
  107. {
  108. public:
  109. CarlaEngineBridge(const char* const audioPoolBaseName, const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  110. : CarlaEngine(),
  111. CarlaThread("CarlaEngineBridge"),
  112. fShmAudioPool(),
  113. fShmRtClientControl(),
  114. fShmNonRtClientControl(),
  115. fShmNonRtServerControl(),
  116. fBaseNameAudioPool(audioPoolBaseName),
  117. fBaseNameRtClientControl(rtClientBaseName),
  118. fBaseNameNonRtClientControl(nonRtClientBaseName),
  119. fBaseNameNonRtServerControl(nonRtServerBaseName),
  120. fClosingDown(false),
  121. fIsOffline(false),
  122. fFirstIdle(true),
  123. fBridgeVersion(0),
  124. fLastPingTime(-1)
  125. {
  126. carla_debug("CarlaEngineBridge::CarlaEngineBridge(\"%s\", \"%s\", \"%s\", \"%s\")", audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  127. }
  128. ~CarlaEngineBridge() noexcept override
  129. {
  130. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  131. clear();
  132. }
  133. // -------------------------------------
  134. // CarlaEngine virtual calls
  135. bool init(const char* const clientName) override
  136. {
  137. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  138. if (! pData->init(clientName))
  139. {
  140. setLastError("Failed to init internal data");
  141. return false;
  142. }
  143. if (! fShmAudioPool.attachClient(fBaseNameAudioPool))
  144. {
  145. pData->close();
  146. setLastError("Failed to attach to audio pool shared memory");
  147. return false;
  148. }
  149. if (! fShmRtClientControl.attachClient(fBaseNameRtClientControl))
  150. {
  151. pData->close();
  152. clear();
  153. setLastError("Failed to attach to rt client control shared memory");
  154. return false;
  155. }
  156. if (! fShmRtClientControl.mapData())
  157. {
  158. pData->close();
  159. clear();
  160. setLastError("Failed to map rt client control shared memory");
  161. return false;
  162. }
  163. if (! fShmNonRtClientControl.attachClient(fBaseNameNonRtClientControl))
  164. {
  165. pData->close();
  166. clear();
  167. setLastError("Failed to attach to non-rt client control shared memory");
  168. return false;
  169. }
  170. if (! fShmNonRtClientControl.mapData())
  171. {
  172. pData->close();
  173. clear();
  174. setLastError("Failed to map non-rt control client shared memory");
  175. return false;
  176. }
  177. if (! fShmNonRtServerControl.attachClient(fBaseNameNonRtServerControl))
  178. {
  179. pData->close();
  180. clear();
  181. setLastError("Failed to attach to non-rt server control shared memory");
  182. return false;
  183. }
  184. if (! fShmNonRtServerControl.mapData())
  185. {
  186. pData->close();
  187. clear();
  188. setLastError("Failed to map non-rt control server shared memory");
  189. return false;
  190. }
  191. PluginBridgeNonRtClientOpcode opcode;
  192. opcode = fShmNonRtClientControl.readOpcode();
  193. CARLA_SAFE_ASSERT_RETURN(opcode == kPluginBridgeNonRtClientVersion, false);
  194. const uint32_t apiVersion = fShmNonRtClientControl.readUInt();
  195. CARLA_SAFE_ASSERT_RETURN(apiVersion >= CARLA_PLUGIN_BRIDGE_API_VERSION_MINIMUM, false);
  196. const uint32_t shmRtClientDataSize = fShmNonRtClientControl.readUInt();
  197. CARLA_SAFE_ASSERT_INT2(shmRtClientDataSize == sizeof(BridgeRtClientData), shmRtClientDataSize, sizeof(BridgeRtClientData));
  198. const uint32_t shmNonRtClientDataSize = fShmNonRtClientControl.readUInt();
  199. CARLA_SAFE_ASSERT_INT2(shmNonRtClientDataSize == sizeof(BridgeNonRtClientData), shmNonRtClientDataSize, sizeof(BridgeNonRtClientData));
  200. const uint32_t shmNonRtServerDataSize = fShmNonRtClientControl.readUInt();
  201. CARLA_SAFE_ASSERT_INT2(shmNonRtServerDataSize == sizeof(BridgeNonRtServerData), shmNonRtServerDataSize, sizeof(BridgeNonRtServerData));
  202. if (shmRtClientDataSize != sizeof(BridgeRtClientData) ||
  203. shmNonRtClientDataSize != sizeof(BridgeNonRtClientData) ||
  204. shmNonRtServerDataSize != sizeof(BridgeNonRtServerData))
  205. {
  206. pData->close();
  207. clear();
  208. setLastError("Shared memory data size mismatch");
  209. return false;
  210. }
  211. opcode = fShmNonRtClientControl.readOpcode();
  212. CARLA_SAFE_ASSERT_RETURN(opcode == kPluginBridgeNonRtClientInitialSetup, false);
  213. pData->bufferSize = fShmNonRtClientControl.readUInt();
  214. pData->sampleRate = fShmNonRtClientControl.readDouble();
  215. if (pData->bufferSize == 0 || carla_isZero(pData->sampleRate))
  216. {
  217. pData->close();
  218. clear();
  219. setLastError("Shared memory has invalid data");
  220. return false;
  221. }
  222. pData->initTime(nullptr);
  223. // tell backend we're live
  224. {
  225. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  226. // kPluginBridgeNonRtServerVersion was added in API 7
  227. if (apiVersion >= 7)
  228. {
  229. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerVersion);
  230. fShmNonRtServerControl.writeUInt(CARLA_PLUGIN_BRIDGE_API_VERSION_CURRENT);
  231. }
  232. else
  233. {
  234. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPong);
  235. }
  236. fShmNonRtServerControl.commitWrite();
  237. }
  238. startThread(true);
  239. return true;
  240. }
  241. bool close() override
  242. {
  243. carla_debug("CarlaEngineBridge::close()");
  244. fLastPingTime = -1;
  245. CarlaEngine::close();
  246. stopThread(5000);
  247. clear();
  248. return true;
  249. }
  250. bool hasIdleOnMainThread() const noexcept override
  251. {
  252. return true;
  253. }
  254. bool isRunning() const noexcept override
  255. {
  256. if (fClosingDown)
  257. return false;
  258. return isThreadRunning() || ! fFirstIdle;
  259. }
  260. bool isOffline() const noexcept override
  261. {
  262. return fIsOffline;
  263. }
  264. EngineType getType() const noexcept override
  265. {
  266. return kEngineTypeBridge;
  267. }
  268. const char* getCurrentDriverName() const noexcept override
  269. {
  270. return "Bridge";
  271. }
  272. void touchPluginParameter(const uint id, const uint32_t parameterId, const bool touch) noexcept override
  273. {
  274. CARLA_SAFE_ASSERT_RETURN(id == 0,);
  275. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  276. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterTouch);
  277. fShmNonRtServerControl.writeUInt(parameterId);
  278. fShmNonRtServerControl.writeBool(touch);
  279. fShmNonRtServerControl.commitWrite();
  280. }
  281. CarlaEngineClient* addClient(const CarlaPluginPtr plugin) override
  282. {
  283. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  284. return new CarlaEngineBridgeClient(*this, pData->graph, plugin, this);
  285. #else
  286. return new CarlaEngineBridgeClient(*this, this);
  287. // unused
  288. (void)plugin;
  289. #endif
  290. }
  291. void idle() noexcept override
  292. {
  293. const CarlaPluginPtr plugin = pData->plugins[0].plugin;
  294. if (plugin.get() == nullptr)
  295. {
  296. if (const uint32_t length = static_cast<uint32_t>(pData->lastError.length()))
  297. {
  298. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  299. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerError);
  300. fShmNonRtServerControl.writeUInt(length);
  301. fShmNonRtServerControl.writeCustomData(pData->lastError.buffer(), length);
  302. fShmNonRtServerControl.commitWrite();
  303. }
  304. signalThreadShouldExit();
  305. callback(true, true, ENGINE_CALLBACK_QUIT, 0, 0, 0, 0, 0.0f, nullptr);
  306. return;
  307. }
  308. const bool wasFirstIdle = fFirstIdle;
  309. if (wasFirstIdle)
  310. {
  311. fFirstIdle = false;
  312. fLastPingTime = carla_gettime_ms();
  313. CARLA_SAFE_ASSERT(fLastPingTime > 0);
  314. char bufStr[STR_MAX+1];
  315. carla_zeroChars(bufStr, STR_MAX+1);
  316. uint32_t bufStrSize;
  317. const CarlaEngineClient* const client(plugin->getEngineClient());
  318. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  319. // kPluginBridgeNonRtServerPluginInfo1
  320. {
  321. // uint/category, uint/hints, uint/optionsAvailable, uint/optionsEnabled, long/uniqueId
  322. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPluginInfo1);
  323. fShmNonRtServerControl.writeUInt(plugin->getCategory());
  324. fShmNonRtServerControl.writeUInt(plugin->getHints());
  325. fShmNonRtServerControl.writeUInt(plugin->getOptionsAvailable());
  326. fShmNonRtServerControl.writeUInt(plugin->getOptionsEnabled());
  327. fShmNonRtServerControl.writeLong(plugin->getUniqueId());
  328. fShmNonRtServerControl.commitWrite();
  329. }
  330. // kPluginBridgeNonRtServerPluginInfo2
  331. {
  332. // uint/size, str[] (realName), uint/size, str[] (label), uint/size, str[] (maker), uint/size, str[] (copyright)
  333. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPluginInfo2);
  334. if (! plugin->getRealName(bufStr))
  335. bufStr[0] = '\0';
  336. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  337. fShmNonRtServerControl.writeUInt(bufStrSize);
  338. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  339. if (! plugin->getLabel(bufStr))
  340. bufStr[0] = '\0';
  341. bufStrSize = carla_fixedValue(1U, 256U, static_cast<uint32_t>(std::strlen(bufStr)));
  342. fShmNonRtServerControl.writeUInt(bufStrSize);
  343. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  344. if (! plugin->getMaker(bufStr))
  345. bufStr[0] = '\0';
  346. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  347. fShmNonRtServerControl.writeUInt(bufStrSize);
  348. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  349. if (! plugin->getCopyright(bufStr))
  350. bufStr[0] = '\0';
  351. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  352. fShmNonRtServerControl.writeUInt(bufStrSize);
  353. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  354. fShmNonRtServerControl.commitWrite();
  355. }
  356. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  357. // kPluginBridgeNonRtServerAudioCount
  358. {
  359. const uint32_t aIns = plugin->getAudioInCount();
  360. const uint32_t aOuts = plugin->getAudioOutCount();
  361. // uint/ins, uint/outs
  362. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerAudioCount);
  363. fShmNonRtServerControl.writeUInt(aIns);
  364. fShmNonRtServerControl.writeUInt(aOuts);
  365. fShmNonRtServerControl.commitWrite();
  366. // kPluginBridgeNonRtServerPortName
  367. for (uint32_t i=0; i<aIns; ++i)
  368. {
  369. const char* const portName(client->getAudioPortName(true, i));
  370. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr && portName[0] != '\0');
  371. // byte/type, uint/index, uint/size, str[] (name)
  372. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPortName);
  373. fShmNonRtServerControl.writeByte(kPluginBridgePortAudioInput);
  374. fShmNonRtServerControl.writeUInt(i);
  375. bufStrSize = static_cast<uint32_t>(std::strlen(portName));
  376. fShmNonRtServerControl.writeUInt(bufStrSize);
  377. fShmNonRtServerControl.writeCustomData(portName, bufStrSize);
  378. }
  379. // kPluginBridgeNonRtServerPortName
  380. for (uint32_t i=0; i<aOuts; ++i)
  381. {
  382. const char* const portName(client->getAudioPortName(false, i));
  383. CARLA_SAFE_ASSERT_CONTINUE(portName != nullptr && portName[0] != '\0');
  384. // byte/type, uint/index, uint/size, str[] (name)
  385. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPortName);
  386. fShmNonRtServerControl.writeByte(kPluginBridgePortAudioOutput);
  387. fShmNonRtServerControl.writeUInt(i);
  388. bufStrSize = static_cast<uint32_t>(std::strlen(portName));
  389. fShmNonRtServerControl.writeUInt(bufStrSize);
  390. fShmNonRtServerControl.writeCustomData(portName, bufStrSize);
  391. }
  392. }
  393. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  394. // kPluginBridgeNonRtServerMidiCount
  395. {
  396. // uint/ins, uint/outs
  397. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiCount);
  398. fShmNonRtServerControl.writeUInt(plugin->getMidiInCount());
  399. fShmNonRtServerControl.writeUInt(plugin->getMidiOutCount());
  400. fShmNonRtServerControl.commitWrite();
  401. }
  402. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  403. // kPluginBridgeNonRtServerCvCount
  404. {
  405. // uint/ins, uint/outs
  406. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCvCount);
  407. fShmNonRtServerControl.writeUInt(plugin->getCVInCount());
  408. fShmNonRtServerControl.writeUInt(plugin->getCVOutCount());
  409. fShmNonRtServerControl.commitWrite();
  410. }
  411. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  412. // kPluginBridgeNonRtServerParameter*
  413. if (const uint32_t count = plugin->getParameterCount())
  414. {
  415. // uint/count
  416. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterCount);
  417. fShmNonRtServerControl.writeUInt(count);
  418. fShmNonRtServerControl.commitWrite();
  419. for (uint32_t i=0; i<count; ++i)
  420. {
  421. const ParameterData& paramData(plugin->getParameterData(i));
  422. if (paramData.type != PARAMETER_INPUT && paramData.type != PARAMETER_OUTPUT)
  423. continue;
  424. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  425. continue;
  426. // kPluginBridgeNonRtServerParameterData1
  427. {
  428. // uint/index, int/rindex, uint/type, uint/hints, short/cc
  429. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterData1);
  430. fShmNonRtServerControl.writeUInt(i);
  431. fShmNonRtServerControl.writeInt(paramData.rindex);
  432. fShmNonRtServerControl.writeUInt(paramData.type);
  433. fShmNonRtServerControl.writeUInt(paramData.hints);
  434. fShmNonRtServerControl.writeShort(paramData.mappedControlIndex);
  435. fShmNonRtServerControl.commitWrite();
  436. }
  437. // kPluginBridgeNonRtServerParameterData2
  438. {
  439. // uint/index, uint/size, str[] (name), uint/size, str[] (symbol), uint/size, str[] (unit)
  440. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterData2);
  441. fShmNonRtServerControl.writeUInt(i);
  442. if (! plugin->getParameterName(i, bufStr))
  443. std::snprintf(bufStr, STR_MAX, "Param %u", i+1);
  444. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  445. fShmNonRtServerControl.writeUInt(bufStrSize);
  446. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  447. if (! plugin->getParameterSymbol(i, bufStr))
  448. bufStr[0] = '\0';
  449. bufStrSize = carla_fixedValue(1U, 64U, static_cast<uint32_t>(std::strlen(bufStr)));
  450. fShmNonRtServerControl.writeUInt(bufStrSize);
  451. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  452. if (! plugin->getParameterUnit(i, bufStr))
  453. bufStr[0] = '\0';
  454. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  455. fShmNonRtServerControl.writeUInt(bufStrSize);
  456. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  457. fShmNonRtServerControl.commitWrite();
  458. }
  459. // kPluginBridgeNonRtServerParameterRanges
  460. {
  461. const ParameterRanges& paramRanges(plugin->getParameterRanges(i));
  462. // uint/index, float/def, float/min, float/max, float/step, float/stepSmall, float/stepLarge
  463. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterRanges);
  464. fShmNonRtServerControl.writeUInt(i);
  465. fShmNonRtServerControl.writeFloat(paramRanges.def);
  466. fShmNonRtServerControl.writeFloat(paramRanges.min);
  467. fShmNonRtServerControl.writeFloat(paramRanges.max);
  468. fShmNonRtServerControl.writeFloat(paramRanges.step);
  469. fShmNonRtServerControl.writeFloat(paramRanges.stepSmall);
  470. fShmNonRtServerControl.writeFloat(paramRanges.stepLarge);
  471. fShmNonRtServerControl.commitWrite();
  472. }
  473. // kPluginBridgeNonRtServerParameterValue2
  474. {
  475. // uint/index float/value (used for init/output parameters only, don't resend values)
  476. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue2);
  477. fShmNonRtServerControl.writeUInt(i);
  478. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  479. fShmNonRtServerControl.commitWrite();
  480. }
  481. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  482. }
  483. }
  484. // kPluginBridgeNonRtServerProgram*
  485. if (const uint32_t count = plugin->getProgramCount())
  486. {
  487. // uint/count
  488. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerProgramCount);
  489. fShmNonRtServerControl.writeUInt(count);
  490. fShmNonRtServerControl.commitWrite();
  491. for (uint32_t i=0; i < count; ++i)
  492. {
  493. // uint/index, uint/size, str[] (name)
  494. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerProgramName);
  495. fShmNonRtServerControl.writeUInt(i);
  496. if (! plugin->getProgramName(i, bufStr))
  497. bufStr[0] = '\0';
  498. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(bufStr)));
  499. fShmNonRtServerControl.writeUInt(bufStrSize);
  500. fShmNonRtServerControl.writeCustomData(bufStr, bufStrSize);
  501. fShmNonRtServerControl.commitWrite();
  502. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  503. }
  504. }
  505. // kPluginBridgeNonRtServerMidiProgram*
  506. if (const uint32_t count = plugin->getMidiProgramCount())
  507. {
  508. // uint/count
  509. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiProgramCount);
  510. fShmNonRtServerControl.writeUInt(count);
  511. fShmNonRtServerControl.commitWrite();
  512. for (uint32_t i=0; i < count; ++i)
  513. {
  514. const MidiProgramData& mpData(plugin->getMidiProgramData(i));
  515. CARLA_SAFE_ASSERT_CONTINUE(mpData.name != nullptr);
  516. // uint/index, uint/bank, uint/program, uint/size, str[] (name)
  517. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerMidiProgramData);
  518. fShmNonRtServerControl.writeUInt(i);
  519. fShmNonRtServerControl.writeUInt(mpData.bank);
  520. fShmNonRtServerControl.writeUInt(mpData.program);
  521. bufStrSize = carla_fixedValue(1U, 32U, static_cast<uint32_t>(std::strlen(mpData.name)));
  522. fShmNonRtServerControl.writeUInt(bufStrSize);
  523. fShmNonRtServerControl.writeCustomData(mpData.name, bufStrSize);
  524. fShmNonRtServerControl.commitWrite();
  525. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  526. }
  527. }
  528. if (const uint32_t latency = plugin->getLatencyInFrames())
  529. {
  530. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetLatency);
  531. fShmNonRtServerControl.writeUInt(latency);
  532. fShmNonRtServerControl.commitWrite();
  533. }
  534. // ready!
  535. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerReady);
  536. fShmNonRtServerControl.commitWrite();
  537. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  538. carla_stdout("Carla Bridge Ready!");
  539. fLastPingTime = carla_gettime_ms();
  540. }
  541. // send parameter outputs
  542. if (const uint32_t count = plugin->getParameterCount())
  543. {
  544. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  545. for (uint32_t i=0; i < count; ++i)
  546. {
  547. if (! plugin->isParameterOutput(i))
  548. continue;
  549. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue2);
  550. fShmNonRtServerControl.writeUInt(i);
  551. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  552. // parameter outputs are not that important, we can skip some
  553. if (! fShmNonRtServerControl.commitWrite())
  554. break;
  555. }
  556. }
  557. CarlaEngine::idle();
  558. try {
  559. handleNonRtData();
  560. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  561. if (fLastPingTime > 0 && carla_gettime_ms() > fLastPingTime + 30000 && ! wasFirstIdle)
  562. {
  563. carla_stderr("Did not receive ping message from server for 30 secs, closing...");
  564. signalThreadShouldExit();
  565. callback(true, true, ENGINE_CALLBACK_QUIT, 0, 0, 0, 0, 0.0f, nullptr);
  566. }
  567. }
  568. void callback(const bool sendHost, const bool sendOsc,
  569. const EngineCallbackOpcode action, const uint pluginId,
  570. const int value1, const int value2, const int value3,
  571. const float valuef, const char* const valueStr) noexcept override
  572. {
  573. CarlaEngine::callback(sendHost, sendOsc, action, pluginId, value1, value2, value3, valuef, valueStr);
  574. if (fClosingDown || ! sendHost)
  575. return;
  576. switch (action)
  577. {
  578. // uint/index float/value
  579. case ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED: {
  580. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  581. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  582. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue);
  583. fShmNonRtServerControl.writeUInt(static_cast<uint>(value1));
  584. fShmNonRtServerControl.writeFloat(valuef);
  585. fShmNonRtServerControl.commitWrite();
  586. } break;
  587. // uint/index float/value
  588. case ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED: {
  589. CARLA_SAFE_ASSERT_BREAK(value1 >= 0);
  590. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  591. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerDefaultValue);
  592. fShmNonRtServerControl.writeUInt(static_cast<uint>(value1));
  593. fShmNonRtServerControl.writeFloat(valuef);
  594. fShmNonRtServerControl.commitWrite();
  595. } break;
  596. // int/index
  597. case ENGINE_CALLBACK_PROGRAM_CHANGED: {
  598. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  599. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  600. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCurrentProgram);
  601. fShmNonRtServerControl.writeInt(value1);
  602. fShmNonRtServerControl.commitWrite();
  603. } break;
  604. // int/index
  605. case ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED: {
  606. CARLA_SAFE_ASSERT_BREAK(value1 >= -1);
  607. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  608. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerCurrentMidiProgram);
  609. fShmNonRtServerControl.writeInt(value1);
  610. fShmNonRtServerControl.commitWrite();
  611. } break;
  612. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  613. if (value1 != 1)
  614. {
  615. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  616. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerUiClosed);
  617. fShmNonRtServerControl.commitWrite();
  618. }
  619. break;
  620. case ENGINE_CALLBACK_EMBED_UI_RESIZED: {
  621. CARLA_SAFE_ASSERT_BREAK(value1 > 1);
  622. CARLA_SAFE_ASSERT_BREAK(value2 > 1);
  623. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  624. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerResizeEmbedUI);
  625. fShmNonRtServerControl.writeUInt(static_cast<uint>(value1));
  626. fShmNonRtServerControl.writeUInt(static_cast<uint>(value2));
  627. fShmNonRtServerControl.commitWrite();
  628. } break;
  629. case ENGINE_CALLBACK_RELOAD_PARAMETERS:
  630. if (const CarlaPluginPtr plugin = pData->plugins[0].plugin)
  631. {
  632. if (const uint32_t count = plugin->getParameterCount())
  633. {
  634. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  635. for (uint32_t i=0; i<count; ++i)
  636. {
  637. const ParameterData& paramData(plugin->getParameterData(i));
  638. if (paramData.type != PARAMETER_INPUT && paramData.type != PARAMETER_OUTPUT)
  639. continue;
  640. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  641. continue;
  642. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerParameterValue);
  643. fShmNonRtServerControl.writeUInt(i);
  644. fShmNonRtServerControl.writeFloat(plugin->getParameterValue(i));
  645. fShmNonRtServerControl.commitWrite();
  646. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  647. }
  648. }
  649. }
  650. break;
  651. default:
  652. break;
  653. }
  654. }
  655. // -------------------------------------------------------------------
  656. void clear() noexcept
  657. {
  658. fShmAudioPool.clear();
  659. fShmRtClientControl.clear();
  660. fShmNonRtClientControl.clear();
  661. fShmNonRtServerControl.clear();
  662. }
  663. void handleNonRtData()
  664. {
  665. const CarlaPluginPtr plugin = pData->plugins[0].plugin;
  666. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  667. for (; fShmNonRtClientControl.isDataAvailableForReading();)
  668. {
  669. const PluginBridgeNonRtClientOpcode opcode = fShmNonRtClientControl.readOpcode();
  670. #ifdef DEBUG
  671. if (opcode != kPluginBridgeNonRtClientPing) {
  672. carla_debug("CarlaEngineBridge::handleNonRtData() - got opcode: %i:%s",
  673. opcode, PluginBridgeNonRtClientOpcode2str(opcode));
  674. }
  675. #endif
  676. if (opcode != kPluginBridgeNonRtClientNull && opcode != kPluginBridgeNonRtClientPingOnOff && fLastPingTime > 0)
  677. fLastPingTime = carla_gettime_ms();
  678. switch (opcode)
  679. {
  680. case kPluginBridgeNonRtClientNull:
  681. break;
  682. case kPluginBridgeNonRtClientVersion: {
  683. fBridgeVersion = fShmNonRtServerControl.readUInt();
  684. CARLA_SAFE_ASSERT_UINT2(fBridgeVersion >= CARLA_PLUGIN_BRIDGE_API_VERSION_MINIMUM,
  685. fBridgeVersion, CARLA_PLUGIN_BRIDGE_API_VERSION_MINIMUM);
  686. } break;
  687. case kPluginBridgeNonRtClientPing: {
  688. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  689. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPong);
  690. fShmNonRtServerControl.commitWrite();
  691. } break;
  692. case kPluginBridgeNonRtClientPingOnOff: {
  693. const uint32_t onOff(fShmNonRtClientControl.readBool());
  694. fLastPingTime = onOff ? carla_gettime_ms() : -1;
  695. } break;
  696. case kPluginBridgeNonRtClientActivate:
  697. if (plugin->isEnabled())
  698. plugin->setActive(true, false, false);
  699. break;
  700. case kPluginBridgeNonRtClientDeactivate:
  701. if (plugin->isEnabled())
  702. plugin->setActive(false, false, false);
  703. break;
  704. case kPluginBridgeNonRtClientInitialSetup:
  705. // should never happen!!
  706. fShmNonRtServerControl.readUInt();
  707. fShmNonRtServerControl.readDouble();
  708. break;
  709. case kPluginBridgeNonRtClientSetParameterValue: {
  710. const uint32_t index(fShmNonRtClientControl.readUInt());
  711. const float value(fShmNonRtClientControl.readFloat());
  712. if (plugin->isEnabled())
  713. plugin->setParameterValue(index, value, false, false, false);
  714. break;
  715. }
  716. case kPluginBridgeNonRtClientSetParameterMidiChannel: {
  717. const uint32_t index(fShmNonRtClientControl.readUInt());
  718. const uint8_t channel(fShmNonRtClientControl.readByte());
  719. if (plugin->isEnabled())
  720. plugin->setParameterMidiChannel(index, channel, false, false);
  721. break;
  722. }
  723. case kPluginBridgeNonRtClientSetParameterMappedControlIndex: {
  724. const uint32_t index(fShmNonRtClientControl.readUInt());
  725. const int16_t ctrl(fShmNonRtClientControl.readShort());
  726. if (plugin->isEnabled())
  727. plugin->setParameterMappedControlIndex(index, ctrl, false, false, true);
  728. break;
  729. }
  730. case kPluginBridgeNonRtClientSetParameterMappedRange: {
  731. const uint32_t index = fShmNonRtClientControl.readUInt();
  732. const float minimum = fShmNonRtClientControl.readFloat();
  733. const float maximum = fShmNonRtClientControl.readFloat();
  734. if (plugin->isEnabled())
  735. plugin->setParameterMappedRange(index, minimum, maximum, false, false);
  736. break;
  737. }
  738. case kPluginBridgeNonRtClientSetProgram: {
  739. const int32_t index(fShmNonRtClientControl.readInt());
  740. if (plugin->isEnabled())
  741. plugin->setProgram(index, true, false, false);
  742. break;
  743. }
  744. case kPluginBridgeNonRtClientSetMidiProgram: {
  745. const int32_t index(fShmNonRtClientControl.readInt());
  746. if (plugin->isEnabled())
  747. plugin->setMidiProgram(index, true, false, false);
  748. break;
  749. }
  750. case kPluginBridgeNonRtClientSetCustomData: {
  751. const uint32_t maxLocalValueLen = fBridgeVersion >= 10 ? 4096 : 16384;
  752. // type
  753. const BridgeTextReader type(fShmNonRtClientControl);
  754. // key
  755. const BridgeTextReader key(fShmNonRtClientControl);
  756. // value
  757. const uint32_t valueSize = fShmNonRtClientControl.readUInt();
  758. if (valueSize > 0)
  759. {
  760. if (valueSize > maxLocalValueLen)
  761. {
  762. const BridgeTextReader bigValueFilePathTry(fShmNonRtClientControl, valueSize);
  763. CARLA_SAFE_ASSERT_BREAK(bigValueFilePathTry.text[0] != '\0');
  764. if (! plugin->isEnabled()) break;
  765. String bigValueFilePath(bigValueFilePathTry.text);
  766. #ifdef CARLA_OS_WIN
  767. // check if running under Wine
  768. if (bigValueFilePath.startsWith("/"))
  769. bigValueFilePath = bigValueFilePath.replaceSection(0, 1, "Z:\\").replace("/", "\\");
  770. #endif
  771. File bigValueFile(bigValueFilePath);
  772. CARLA_SAFE_ASSERT_BREAK(bigValueFile.existsAsFile());
  773. plugin->setCustomData(type.text, key.text, bigValueFile.loadFileAsString().toRawUTF8(), true);
  774. bigValueFile.deleteFile();
  775. }
  776. else
  777. {
  778. const BridgeTextReader value(fShmNonRtClientControl, valueSize);
  779. if (plugin->isEnabled())
  780. plugin->setCustomData(type.text, key.text, value.text, true);
  781. }
  782. }
  783. else
  784. {
  785. if (plugin->isEnabled())
  786. plugin->setCustomData(type.text, key.text, "", true);
  787. }
  788. break;
  789. }
  790. case kPluginBridgeNonRtClientSetChunkDataFile: {
  791. const uint32_t size = fShmNonRtClientControl.readUInt();
  792. CARLA_SAFE_ASSERT_BREAK(size > 0);
  793. const BridgeTextReader chunkFilePathTry(fShmNonRtClientControl, size);
  794. CARLA_SAFE_ASSERT_BREAK(chunkFilePathTry.text[0] != '\0');
  795. if (! plugin->isEnabled()) break;
  796. String chunkFilePath(chunkFilePathTry.text);
  797. #ifdef CARLA_OS_WIN
  798. // check if running under Wine
  799. if (chunkFilePath.startsWith("/"))
  800. chunkFilePath = chunkFilePath.replaceSection(0, 1, "Z:\\").replace("/", "\\");
  801. #endif
  802. File chunkFile(chunkFilePath);
  803. CARLA_SAFE_ASSERT_BREAK(chunkFile.existsAsFile());
  804. String chunkDataBase64(chunkFile.loadFileAsString());
  805. chunkFile.deleteFile();
  806. CARLA_SAFE_ASSERT_BREAK(chunkDataBase64.isNotEmpty());
  807. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(chunkDataBase64.toRawUTF8()));
  808. #ifdef CARLA_PROPER_CPP11_SUPPORT
  809. plugin->setChunkData(chunk.data(), chunk.size());
  810. #else
  811. plugin->setChunkData(&chunk.front(), chunk.size());
  812. #endif
  813. break;
  814. }
  815. case kPluginBridgeNonRtClientSetCtrlChannel: {
  816. const int16_t channel(fShmNonRtClientControl.readShort());
  817. CARLA_SAFE_ASSERT_BREAK(channel >= -1 && channel < MAX_MIDI_CHANNELS);
  818. if (plugin->isEnabled())
  819. plugin->setCtrlChannel(static_cast<int8_t>(channel), false, false);
  820. break;
  821. }
  822. case kPluginBridgeNonRtClientSetOption: {
  823. const uint32_t option(fShmNonRtClientControl.readUInt());
  824. const bool yesNo(fShmNonRtClientControl.readBool());
  825. if (plugin->isEnabled())
  826. plugin->setOption(option, yesNo, false);
  827. break;
  828. }
  829. case kPluginBridgeNonRtClientSetOptions: {
  830. const uint32_t options(fShmNonRtClientControl.readUInt());
  831. plugin->pData->options = options;
  832. break;
  833. }
  834. case kPluginBridgeNonRtClientSetWindowTitle: {
  835. const BridgeTextReader title(fShmNonRtClientControl);
  836. plugin->setCustomUITitle(title.text);
  837. break;
  838. }
  839. case kPluginBridgeNonRtClientGetParameterText: {
  840. const int32_t index = fShmNonRtClientControl.readInt();
  841. if (index >= 0 && plugin->isEnabled())
  842. {
  843. char bufStr[STR_MAX+1];
  844. carla_zeroChars(bufStr, STR_MAX+1);
  845. if (! plugin->getParameterText(static_cast<uint32_t>(index), bufStr))
  846. bufStr[0] = '\0';
  847. const uint32_t bufStrLen = static_cast<uint32_t>(std::strlen(bufStr));
  848. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  849. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetParameterText);
  850. fShmNonRtServerControl.writeInt(index);
  851. fShmNonRtServerControl.writeUInt(bufStrLen);
  852. fShmNonRtServerControl.writeCustomData(bufStr, bufStrLen);
  853. fShmNonRtServerControl.commitWrite();
  854. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  855. }
  856. else
  857. {
  858. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  859. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetParameterText);
  860. fShmNonRtServerControl.writeInt(index);
  861. fShmNonRtServerControl.writeUInt(0);
  862. fShmNonRtServerControl.commitWrite();
  863. }
  864. break;
  865. }
  866. case kPluginBridgeNonRtClientPrepareForSave: {
  867. if (! plugin->isEnabled())
  868. {
  869. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  870. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSaved);
  871. fShmNonRtServerControl.commitWrite();
  872. return;
  873. }
  874. plugin->prepareForSave(false);
  875. const uint32_t maxLocalValueLen = fBridgeVersion >= 10 ? 4096 : 16384;
  876. for (uint32_t i=0, count=plugin->getCustomDataCount(); i<count; ++i)
  877. {
  878. const CustomData& cdata(plugin->getCustomData(i));
  879. if (std::strcmp(cdata.type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  880. continue;
  881. const uint32_t typeLen = static_cast<uint32_t>(std::strlen(cdata.type));
  882. const uint32_t keyLen = static_cast<uint32_t>(std::strlen(cdata.key));
  883. const uint32_t valueLen = static_cast<uint32_t>(std::strlen(cdata.value));
  884. {
  885. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  886. if (valueLen > maxLocalValueLen)
  887. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  888. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetCustomData);
  889. fShmNonRtServerControl.writeUInt(typeLen);
  890. fShmNonRtServerControl.writeCustomData(cdata.type, typeLen);
  891. fShmNonRtServerControl.writeUInt(keyLen);
  892. fShmNonRtServerControl.writeCustomData(cdata.key, keyLen);
  893. fShmNonRtServerControl.writeUInt(valueLen);
  894. if (valueLen > 0)
  895. {
  896. if (valueLen > maxLocalValueLen)
  897. {
  898. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  899. filePath += CARLA_OS_SEP_STR ".CarlaCustomData_";
  900. filePath += fShmAudioPool.getFilenameSuffix();
  901. if (File(filePath).replaceWithText(cdata.value))
  902. {
  903. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  904. fShmNonRtServerControl.writeUInt(ulength);
  905. fShmNonRtServerControl.writeCustomData(filePath.toRawUTF8(), ulength);
  906. }
  907. else
  908. {
  909. fShmNonRtServerControl.writeUInt(0);
  910. }
  911. }
  912. else
  913. {
  914. fShmNonRtServerControl.writeCustomData(cdata.value, valueLen);
  915. }
  916. }
  917. fShmNonRtServerControl.commitWrite();
  918. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  919. }
  920. }
  921. if (plugin->getOptionsEnabled() & PLUGIN_OPTION_USE_CHUNKS)
  922. {
  923. void* data = nullptr;
  924. if (const std::size_t dataSize = plugin->getChunkData(&data))
  925. {
  926. CARLA_SAFE_ASSERT_BREAK(data != nullptr);
  927. CarlaString dataBase64 = CarlaString::asBase64(data, dataSize);
  928. CARLA_SAFE_ASSERT_BREAK(dataBase64.length() > 0);
  929. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  930. filePath += CARLA_OS_SEP_STR ".CarlaChunk_";
  931. filePath += fShmAudioPool.getFilenameSuffix();
  932. if (File(filePath).replaceWithText(dataBase64.buffer()))
  933. {
  934. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  935. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  936. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetChunkDataFile);
  937. fShmNonRtServerControl.writeUInt(ulength);
  938. fShmNonRtServerControl.writeCustomData(filePath.toRawUTF8(), ulength);
  939. fShmNonRtServerControl.commitWrite();
  940. }
  941. }
  942. }
  943. {
  944. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  945. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSaved);
  946. fShmNonRtServerControl.commitWrite();
  947. }
  948. break;
  949. }
  950. case kPluginBridgeNonRtClientRestoreLV2State:
  951. if (plugin->isEnabled())
  952. plugin->restoreLV2State(false);
  953. break;
  954. case kPluginBridgeNonRtClientShowUI:
  955. if (plugin->isEnabled())
  956. plugin->showCustomUI(true);
  957. break;
  958. case kPluginBridgeNonRtClientHideUI:
  959. if (plugin->isEnabled())
  960. plugin->showCustomUI(false);
  961. break;
  962. case kPluginBridgeNonRtClientEmbedUI: {
  963. const uint64_t winId = fShmNonRtClientControl.readULong();
  964. uint64_t resp = 0;
  965. if (plugin->isEnabled())
  966. resp = reinterpret_cast<uint64_t>(plugin->embedCustomUI(reinterpret_cast<void*>(winId)));
  967. if (resp == 0)
  968. resp = 1;
  969. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  970. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerRespEmbedUI);
  971. fShmNonRtServerControl.writeULong(resp);
  972. fShmNonRtServerControl.commitWrite();
  973. break;
  974. }
  975. case kPluginBridgeNonRtClientUiParameterChange: {
  976. const uint32_t index = fShmNonRtClientControl.readUInt();
  977. const float value = fShmNonRtClientControl.readFloat();
  978. if (plugin->isEnabled())
  979. plugin->uiParameterChange(index, value);
  980. break;
  981. }
  982. case kPluginBridgeNonRtClientUiProgramChange: {
  983. const uint32_t index(fShmNonRtClientControl.readUInt());
  984. if (plugin->isEnabled())
  985. plugin->uiProgramChange(index);
  986. break;
  987. }
  988. case kPluginBridgeNonRtClientUiMidiProgramChange: {
  989. const uint32_t index(fShmNonRtClientControl.readUInt());
  990. if (plugin->isEnabled())
  991. plugin->uiMidiProgramChange(index);
  992. break;
  993. }
  994. case kPluginBridgeNonRtClientUiNoteOn: {
  995. const uint8_t chnl(fShmNonRtClientControl.readByte());
  996. const uint8_t note(fShmNonRtClientControl.readByte());
  997. const uint8_t velo(fShmNonRtClientControl.readByte());
  998. if (plugin->isEnabled())
  999. plugin->uiNoteOn(chnl, note, velo);
  1000. break;
  1001. }
  1002. case kPluginBridgeNonRtClientUiNoteOff: {
  1003. const uint8_t chnl(fShmNonRtClientControl.readByte());
  1004. const uint8_t note(fShmNonRtClientControl.readByte());
  1005. if (plugin->isEnabled())
  1006. plugin->uiNoteOff(chnl, note);
  1007. break;
  1008. }
  1009. case kPluginBridgeNonRtClientQuit:
  1010. fClosingDown = true;
  1011. signalThreadShouldExit();
  1012. callback(true, true, ENGINE_CALLBACK_QUIT, 0, 0, 0, 0, 0.0f, nullptr);
  1013. break;
  1014. case kPluginBridgeNonRtClientReload:
  1015. fFirstIdle = true;
  1016. break;
  1017. }
  1018. }
  1019. }
  1020. // -------------------------------------------------------------------
  1021. protected:
  1022. void run() override
  1023. {
  1024. #ifdef __SSE2_MATH__
  1025. // Set FTZ and DAZ flags
  1026. _mm_setcsr(_mm_getcsr() | 0x8040);
  1027. #endif
  1028. bool quitReceived = false;
  1029. for (; ! shouldThreadExit();)
  1030. {
  1031. const BridgeRtClientControl::WaitHelper helper(fShmRtClientControl);
  1032. if (! helper.ok)
  1033. continue;
  1034. for (; fShmRtClientControl.isDataAvailableForReading();)
  1035. {
  1036. const PluginBridgeRtClientOpcode opcode(fShmRtClientControl.readOpcode());
  1037. const CarlaPluginPtr plugin = pData->plugins[0].plugin;
  1038. #ifdef DEBUG
  1039. if (opcode != kPluginBridgeRtClientProcess && opcode != kPluginBridgeRtClientMidiEvent) {
  1040. carla_debug("CarlaEngineBridgeRtThread::run() - got opcode: %s", PluginBridgeRtClientOpcode2str(opcode));
  1041. }
  1042. #endif
  1043. switch (opcode)
  1044. {
  1045. case kPluginBridgeRtClientNull:
  1046. break;
  1047. case kPluginBridgeRtClientSetAudioPool: {
  1048. if (fShmAudioPool.data != nullptr)
  1049. {
  1050. jackbridge_shm_unmap(fShmAudioPool.shm, fShmAudioPool.data);
  1051. fShmAudioPool.data = nullptr;
  1052. }
  1053. const uint64_t poolSize(fShmRtClientControl.readULong());
  1054. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  1055. fShmAudioPool.data = (float*)jackbridge_shm_map(fShmAudioPool.shm, static_cast<size_t>(poolSize));
  1056. break;
  1057. }
  1058. case kPluginBridgeRtClientSetBufferSize: {
  1059. const uint32_t bufferSize(fShmRtClientControl.readUInt());
  1060. pData->bufferSize = bufferSize;
  1061. bufferSizeChanged(bufferSize);
  1062. break;
  1063. }
  1064. case kPluginBridgeRtClientSetSampleRate: {
  1065. const double sampleRate(fShmRtClientControl.readDouble());
  1066. pData->sampleRate = sampleRate;
  1067. sampleRateChanged(sampleRate);
  1068. break;
  1069. }
  1070. case kPluginBridgeRtClientSetOnline:
  1071. fIsOffline = fShmRtClientControl.readBool();
  1072. offlineModeChanged(fIsOffline);
  1073. break;
  1074. // NOTE this is never used
  1075. case kPluginBridgeRtClientControlEventParameter: {
  1076. const uint32_t time(fShmRtClientControl.readUInt());
  1077. const uint8_t channel(fShmRtClientControl.readByte());
  1078. const uint16_t param(fShmRtClientControl.readUShort());
  1079. const float value(fShmRtClientControl.readFloat());
  1080. if (EngineEvent* const event = getNextFreeInputEvent())
  1081. {
  1082. event->type = kEngineEventTypeControl;
  1083. event->time = time;
  1084. event->channel = channel;
  1085. event->ctrl.type = kEngineControlEventTypeParameter;
  1086. event->ctrl.param = param;
  1087. event->ctrl.midiValue = -1;
  1088. event->ctrl.normalizedValue = value;
  1089. event->ctrl.handled = true;
  1090. }
  1091. break;
  1092. }
  1093. case kPluginBridgeRtClientControlEventMidiBank: {
  1094. const uint32_t time(fShmRtClientControl.readUInt());
  1095. const uint8_t channel(fShmRtClientControl.readByte());
  1096. const uint16_t index(fShmRtClientControl.readUShort());
  1097. if (EngineEvent* const event = getNextFreeInputEvent())
  1098. {
  1099. event->type = kEngineEventTypeControl;
  1100. event->time = time;
  1101. event->channel = channel;
  1102. event->ctrl.type = kEngineControlEventTypeMidiBank;
  1103. event->ctrl.param = index;
  1104. event->ctrl.midiValue = -1;
  1105. event->ctrl.normalizedValue = 0.0f;
  1106. event->ctrl.handled = true;
  1107. }
  1108. break;
  1109. }
  1110. case kPluginBridgeRtClientControlEventMidiProgram: {
  1111. const uint32_t time(fShmRtClientControl.readUInt());
  1112. const uint8_t channel(fShmRtClientControl.readByte());
  1113. const uint16_t index(fShmRtClientControl.readUShort());
  1114. if (EngineEvent* const event = getNextFreeInputEvent())
  1115. {
  1116. event->type = kEngineEventTypeControl;
  1117. event->time = time;
  1118. event->channel = channel;
  1119. event->ctrl.type = kEngineControlEventTypeMidiProgram;
  1120. event->ctrl.param = index;
  1121. event->ctrl.midiValue = -1;
  1122. event->ctrl.normalizedValue = 0.0f;
  1123. event->ctrl.handled = true;
  1124. }
  1125. break;
  1126. }
  1127. case kPluginBridgeRtClientControlEventAllSoundOff: {
  1128. const uint32_t time(fShmRtClientControl.readUInt());
  1129. const uint8_t channel(fShmRtClientControl.readByte());
  1130. if (EngineEvent* const event = getNextFreeInputEvent())
  1131. {
  1132. event->type = kEngineEventTypeControl;
  1133. event->time = time;
  1134. event->channel = channel;
  1135. event->ctrl.type = kEngineControlEventTypeAllSoundOff;
  1136. event->ctrl.param = 0;
  1137. event->ctrl.midiValue = -1;
  1138. event->ctrl.normalizedValue = 0.0f;
  1139. event->ctrl.handled = true;
  1140. }
  1141. } break;
  1142. case kPluginBridgeRtClientControlEventAllNotesOff: {
  1143. const uint32_t time(fShmRtClientControl.readUInt());
  1144. const uint8_t channel(fShmRtClientControl.readByte());
  1145. if (EngineEvent* const event = getNextFreeInputEvent())
  1146. {
  1147. event->type = kEngineEventTypeControl;
  1148. event->time = time;
  1149. event->channel = channel;
  1150. event->ctrl.type = kEngineControlEventTypeAllNotesOff;
  1151. event->ctrl.param = 0;
  1152. event->ctrl.midiValue = -1;
  1153. event->ctrl.normalizedValue = 0.0f;
  1154. event->ctrl.handled = true;
  1155. }
  1156. } break;
  1157. case kPluginBridgeRtClientMidiEvent: {
  1158. const uint32_t time(fShmRtClientControl.readUInt());
  1159. const uint8_t port(fShmRtClientControl.readByte());
  1160. const uint8_t size(fShmRtClientControl.readByte());
  1161. CARLA_SAFE_ASSERT_BREAK(size > 0);
  1162. // FIXME variable-size stack
  1163. uint8_t data[4];
  1164. {
  1165. uint8_t i=0;
  1166. for (; i<size && i<4; ++i)
  1167. data[i] = fShmRtClientControl.readByte();
  1168. for (; i<size; ++i)
  1169. fShmRtClientControl.readByte();
  1170. }
  1171. if (size > 4)
  1172. continue;
  1173. if (EngineEvent* const event = getNextFreeInputEvent())
  1174. {
  1175. event->type = kEngineEventTypeMidi;
  1176. event->time = time;
  1177. event->channel = MIDI_GET_CHANNEL_FROM_DATA(data);
  1178. event->midi.port = port;
  1179. event->midi.size = size;
  1180. if (size > EngineMidiEvent::kDataSize)
  1181. {
  1182. event->midi.dataExt = data;
  1183. std::memset(event->midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
  1184. }
  1185. else
  1186. {
  1187. event->midi.data[0] = MIDI_GET_STATUS_FROM_DATA(data);
  1188. uint8_t i=1;
  1189. for (; i < size; ++i)
  1190. event->midi.data[i] = data[i];
  1191. for (; i < EngineMidiEvent::kDataSize; ++i)
  1192. event->midi.data[i] = 0;
  1193. event->midi.dataExt = nullptr;
  1194. }
  1195. }
  1196. break;
  1197. }
  1198. case kPluginBridgeRtClientProcess: {
  1199. const uint32_t frames(fShmRtClientControl.readUInt());
  1200. CARLA_SAFE_ASSERT_BREAK(fShmAudioPool.data != nullptr);
  1201. if (plugin.get() != nullptr && plugin->isEnabled() && plugin->tryLock(fIsOffline))
  1202. {
  1203. const BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  1204. const uint32_t audioInCount = plugin->getAudioInCount();
  1205. const uint32_t audioOutCount = plugin->getAudioOutCount();
  1206. const uint32_t cvInCount = plugin->getCVInCount();
  1207. const uint32_t cvOutCount = plugin->getCVOutCount();
  1208. const float* audioIn[64];
  1209. /* */ float* audioOut[64];
  1210. const float* cvIn[32];
  1211. /* */ float* cvOut[32];
  1212. float* fdata = fShmAudioPool.data;
  1213. for (uint32_t i=0; i < audioInCount; ++i, fdata += pData->bufferSize)
  1214. audioIn[i] = fdata;
  1215. for (uint32_t i=0; i < audioOutCount; ++i, fdata += pData->bufferSize)
  1216. audioOut[i] = fdata;
  1217. for (uint32_t i=0; i < cvInCount; ++i, fdata += pData->bufferSize)
  1218. cvIn[i] = fdata;
  1219. for (uint32_t i=0; i < cvOutCount; ++i, fdata += pData->bufferSize)
  1220. cvOut[i] = fdata;
  1221. EngineTimeInfo& timeInfo(pData->timeInfo);
  1222. timeInfo.playing = bridgeTimeInfo.playing;
  1223. timeInfo.frame = bridgeTimeInfo.frame;
  1224. timeInfo.usecs = bridgeTimeInfo.usecs;
  1225. timeInfo.bbt.valid = (bridgeTimeInfo.validFlags & kPluginBridgeTimeInfoValidBBT) != 0;
  1226. if (timeInfo.bbt.valid)
  1227. {
  1228. timeInfo.bbt.bar = bridgeTimeInfo.bar;
  1229. timeInfo.bbt.beat = bridgeTimeInfo.beat;
  1230. timeInfo.bbt.tick = bridgeTimeInfo.tick;
  1231. timeInfo.bbt.beatsPerBar = bridgeTimeInfo.beatsPerBar;
  1232. timeInfo.bbt.beatType = bridgeTimeInfo.beatType;
  1233. timeInfo.bbt.ticksPerBeat = bridgeTimeInfo.ticksPerBeat;
  1234. timeInfo.bbt.beatsPerMinute = bridgeTimeInfo.beatsPerMinute;
  1235. timeInfo.bbt.barStartTick = bridgeTimeInfo.barStartTick;
  1236. }
  1237. plugin->initBuffers();
  1238. plugin->process(audioIn, audioOut, cvIn, cvOut, frames);
  1239. plugin->unlock();
  1240. }
  1241. uint8_t* midiData(fShmRtClientControl.data->midiOut);
  1242. carla_zeroBytes(midiData, kBridgeBaseMidiOutHeaderSize);
  1243. std::size_t curMidiDataPos = 0;
  1244. if (pData->events.in[0].type != kEngineEventTypeNull)
  1245. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  1246. if (pData->events.out[0].type != kEngineEventTypeNull)
  1247. {
  1248. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  1249. {
  1250. const EngineEvent& event(pData->events.out[i]);
  1251. if (event.type == kEngineEventTypeNull)
  1252. break;
  1253. if (event.type == kEngineEventTypeControl)
  1254. {
  1255. uint8_t data[3];
  1256. const uint8_t size = event.ctrl.convertToMidiData(event.channel, data);
  1257. CARLA_SAFE_ASSERT_CONTINUE(size > 0 && size <= 3);
  1258. if (curMidiDataPos + kBridgeBaseMidiOutHeaderSize + size >= kBridgeRtClientDataMidiOutSize)
  1259. break;
  1260. // set time
  1261. *(uint32_t*)midiData = event.time;
  1262. midiData = midiData + 4;
  1263. curMidiDataPos += 4;
  1264. // set port
  1265. *midiData++ = 0;
  1266. ++curMidiDataPos;
  1267. // set size
  1268. *midiData++ = size;
  1269. ++curMidiDataPos;
  1270. // set data
  1271. for (uint8_t j=0; j<size; ++j)
  1272. *midiData++ = data[j];
  1273. curMidiDataPos += size;
  1274. }
  1275. else if (event.type == kEngineEventTypeMidi)
  1276. {
  1277. const EngineMidiEvent& _midiEvent(event.midi);
  1278. if (curMidiDataPos + kBridgeBaseMidiOutHeaderSize + _midiEvent.size >= kBridgeRtClientDataMidiOutSize)
  1279. break;
  1280. const uint8_t* const _midiData(_midiEvent.dataExt != nullptr ? _midiEvent.dataExt : _midiEvent.data);
  1281. // set time
  1282. *(uint32_t*)midiData = event.time;
  1283. midiData += 4;
  1284. curMidiDataPos += 4;
  1285. // set port
  1286. *midiData++ = _midiEvent.port;
  1287. ++curMidiDataPos;
  1288. // set size
  1289. *midiData++ = _midiEvent.size;
  1290. ++curMidiDataPos;
  1291. // set data
  1292. *midiData++ = uint8_t(_midiData[0] | (event.channel & MIDI_CHANNEL_BIT));
  1293. for (uint8_t j=1; j<_midiEvent.size; ++j)
  1294. *midiData++ = _midiData[j];
  1295. curMidiDataPos += _midiEvent.size;
  1296. }
  1297. }
  1298. if (curMidiDataPos != 0 &&
  1299. curMidiDataPos + kBridgeBaseMidiOutHeaderSize < kBridgeRtClientDataMidiOutSize)
  1300. carla_zeroBytes(midiData, kBridgeBaseMidiOutHeaderSize);
  1301. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  1302. }
  1303. } break;
  1304. case kPluginBridgeRtClientQuit: {
  1305. quitReceived = true;
  1306. fClosingDown = true;
  1307. signalThreadShouldExit();
  1308. } break;
  1309. }
  1310. }
  1311. }
  1312. callback(true, true, ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0, 0.0f, nullptr);
  1313. if (! quitReceived)
  1314. {
  1315. const char* const message("Plugin bridge error, process thread has stopped");
  1316. const std::size_t messageSize(std::strlen(message));
  1317. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  1318. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerError);
  1319. fShmNonRtServerControl.writeUInt(messageSize);
  1320. fShmNonRtServerControl.writeCustomData(message, messageSize);
  1321. fShmNonRtServerControl.commitWrite();
  1322. }
  1323. }
  1324. // called from process thread above
  1325. EngineEvent* getNextFreeInputEvent() const noexcept
  1326. {
  1327. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  1328. {
  1329. EngineEvent* const event(&pData->events.in[i]);
  1330. if (event->type == kEngineEventTypeNull)
  1331. return event;
  1332. }
  1333. return nullptr;
  1334. }
  1335. void latencyChanged(const uint32_t samples) noexcept override
  1336. {
  1337. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  1338. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSetLatency);
  1339. fShmNonRtServerControl.writeUInt(samples);
  1340. fShmNonRtServerControl.commitWrite();
  1341. }
  1342. // -------------------------------------------------------------------
  1343. private:
  1344. BridgeAudioPool fShmAudioPool;
  1345. BridgeRtClientControl fShmRtClientControl;
  1346. BridgeNonRtClientControl fShmNonRtClientControl;
  1347. BridgeNonRtServerControl fShmNonRtServerControl;
  1348. CarlaString fBaseNameAudioPool;
  1349. CarlaString fBaseNameRtClientControl;
  1350. CarlaString fBaseNameNonRtClientControl;
  1351. CarlaString fBaseNameNonRtServerControl;
  1352. bool fClosingDown;
  1353. bool fIsOffline;
  1354. bool fFirstIdle;
  1355. uint32_t fBridgeVersion;
  1356. int64_t fLastPingTime;
  1357. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  1358. };
  1359. // -----------------------------------------------------------------------
  1360. namespace EngineInit {
  1361. CarlaEngine* newBridge(const char* const audioPoolBaseName,
  1362. const char* const rtClientBaseName,
  1363. const char* const nonRtClientBaseName,
  1364. const char* const nonRtServerBaseName)
  1365. {
  1366. return new CarlaEngineBridge(audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  1367. }
  1368. }
  1369. // -----------------------------------------------------------------------
  1370. CARLA_BACKEND_END_NAMESPACE
  1371. // -----------------------------------------------------------------------
  1372. #include "CarlaBridgeUtils.cpp"
  1373. // -----------------------------------------------------------------------