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.

CarlaEngineBridge.cpp 63KB

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