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 61KB

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