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.

CarlaBridgeUtils.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Carla Bridge utils
  3. * Copyright (C) 2013-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. #include "CarlaBridgeUtils.hpp"
  18. #include "CarlaShmUtils.hpp"
  19. // must be last
  20. #include "jackbridge/JackBridge.hpp"
  21. #if defined(CARLA_OS_WIN) && !defined(BUILDING_CARLA_FOR_WINE)
  22. # define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "Local\\carla-bridge_shm_ap_"
  23. # define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "Local\\carla-bridge_shm_rtC_"
  24. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "Local\\carla-bridge_shm_nonrtC_"
  25. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "Local\\carla-bridge_shm_nonrtS_"
  26. #else
  27. # define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "/crlbrdg_shm_ap_"
  28. # define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "/crlbrdg_shm_rtC_"
  29. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "/crlbrdg_shm_nonrtC_"
  30. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "/crlbrdg_shm_nonrtS_"
  31. #endif
  32. // -------------------------------------------------------------------------------------------------------------------
  33. template<typename T>
  34. bool jackbridge_shm_map2(void* shm, T*& value) noexcept
  35. {
  36. value = (T*)jackbridge_shm_map(shm, sizeof(T));
  37. return (value != nullptr);
  38. }
  39. // -------------------------------------------------------------------------------------------------------------------
  40. BridgeAudioPool::BridgeAudioPool() noexcept
  41. : data(nullptr),
  42. dataSize(0),
  43. filename(),
  44. isServer(false)
  45. {
  46. carla_zeroChars(shm, 64);
  47. jackbridge_shm_init(shm);
  48. }
  49. BridgeAudioPool::~BridgeAudioPool() noexcept
  50. {
  51. // should be cleared by now
  52. CARLA_SAFE_ASSERT(data == nullptr);
  53. clear();
  54. }
  55. bool BridgeAudioPool::initializeServer() noexcept
  56. {
  57. char tmpFileBase[64] = {};
  58. std::snprintf(tmpFileBase, sizeof(tmpFileBase)-1, PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "XXXXXX");
  59. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  60. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  61. void* const shmptr = shm;
  62. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  63. carla_copyStruct(shm1, shm2);
  64. filename = tmpFileBase;
  65. isServer = true;
  66. return true;
  67. }
  68. bool BridgeAudioPool::attachClient(const char* const basename) noexcept
  69. {
  70. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  71. // must be invalid right now
  72. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  73. filename = PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL;
  74. filename += basename;
  75. jackbridge_shm_attach(shm, filename);
  76. return jackbridge_shm_is_valid(shm);
  77. }
  78. void BridgeAudioPool::clear() noexcept
  79. {
  80. filename.clear();
  81. if (! jackbridge_shm_is_valid(shm))
  82. {
  83. CARLA_SAFE_ASSERT(data == nullptr);
  84. return;
  85. }
  86. if (data != nullptr)
  87. {
  88. if (isServer)
  89. jackbridge_shm_unmap(shm, data);
  90. data = nullptr;
  91. }
  92. dataSize = 0;
  93. jackbridge_shm_close(shm);
  94. jackbridge_shm_init(shm);
  95. }
  96. void BridgeAudioPool::resize(const uint32_t bufferSize, const uint32_t audioPortCount, const uint32_t cvPortCount) noexcept
  97. {
  98. CARLA_SAFE_ASSERT_RETURN(jackbridge_shm_is_valid(shm),);
  99. CARLA_SAFE_ASSERT_RETURN(isServer,);
  100. if (data != nullptr)
  101. jackbridge_shm_unmap(shm, data);
  102. dataSize = (audioPortCount+cvPortCount)*bufferSize*sizeof(float);
  103. if (dataSize == 0)
  104. dataSize = sizeof(float);
  105. data = (float*)jackbridge_shm_map(shm, dataSize);
  106. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  107. std::memset(data, 0, dataSize);
  108. }
  109. const char* BridgeAudioPool::getFilenameSuffix() const noexcept
  110. {
  111. CARLA_SAFE_ASSERT_RETURN(filename.isNotEmpty(), nullptr);
  112. const std::size_t prefixLength(std::strlen(PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL));
  113. CARLA_SAFE_ASSERT_RETURN(filename.length() > prefixLength, nullptr);
  114. return filename.buffer() + prefixLength;
  115. }
  116. // -------------------------------------------------------------------------------------------------------------------
  117. BridgeRtClientControl::BridgeRtClientControl() noexcept
  118. : data(nullptr),
  119. filename(),
  120. needsSemDestroy(false),
  121. isServer(false)
  122. {
  123. carla_zeroChars(shm, 64);
  124. jackbridge_shm_init(shm);
  125. }
  126. BridgeRtClientControl::~BridgeRtClientControl() noexcept
  127. {
  128. // should be cleared by now
  129. CARLA_SAFE_ASSERT(data == nullptr);
  130. clear();
  131. }
  132. bool BridgeRtClientControl::initializeServer() noexcept
  133. {
  134. char tmpFileBase[64] = {};
  135. std::snprintf(tmpFileBase, sizeof(tmpFileBase)-1, PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "XXXXXX");
  136. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  137. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  138. void* const shmptr = shm;
  139. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  140. carla_copyStruct(shm1, shm2);
  141. filename = tmpFileBase;
  142. isServer = true;
  143. if (! mapData())
  144. {
  145. jackbridge_shm_close(shm);
  146. jackbridge_shm_init(shm);
  147. return false;
  148. }
  149. CARLA_SAFE_ASSERT(data != nullptr);
  150. if (! jackbridge_sem_init(&data->sem.server))
  151. {
  152. unmapData();
  153. jackbridge_shm_close(shm);
  154. jackbridge_shm_init(shm);
  155. return false;
  156. }
  157. if (! jackbridge_sem_init(&data->sem.client))
  158. {
  159. jackbridge_sem_destroy(&data->sem.server);
  160. unmapData();
  161. jackbridge_shm_close(shm);
  162. jackbridge_shm_init(shm);
  163. return false;
  164. }
  165. needsSemDestroy = true;
  166. return true;
  167. }
  168. bool BridgeRtClientControl::attachClient(const char* const basename) noexcept
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  171. // must be invalid right now
  172. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  173. filename = PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT;
  174. filename += basename;
  175. jackbridge_shm_attach(shm, filename);
  176. return jackbridge_shm_is_valid(shm);
  177. }
  178. void BridgeRtClientControl::clear() noexcept
  179. {
  180. filename.clear();
  181. if (needsSemDestroy)
  182. {
  183. jackbridge_sem_destroy(&data->sem.client);
  184. jackbridge_sem_destroy(&data->sem.server);
  185. needsSemDestroy = false;
  186. }
  187. if (data != nullptr)
  188. unmapData();
  189. if (! jackbridge_shm_is_valid(shm))
  190. return;
  191. jackbridge_shm_close(shm);
  192. jackbridge_shm_init(shm);
  193. }
  194. bool BridgeRtClientControl::mapData() noexcept
  195. {
  196. CARLA_SAFE_ASSERT(data == nullptr);
  197. if (! jackbridge_shm_map2<BridgeRtClientData>(shm, data))
  198. return false;
  199. if (isServer)
  200. {
  201. std::memset(data, 0, sizeof(BridgeRtClientData));
  202. setRingBuffer(&data->ringBuffer, true);
  203. }
  204. else
  205. {
  206. CARLA_SAFE_ASSERT(data->midiOut[0] == 0);
  207. setRingBuffer(&data->ringBuffer, false);
  208. CARLA_SAFE_ASSERT_RETURN(jackbridge_sem_connect(&data->sem.server), false);
  209. CARLA_SAFE_ASSERT_RETURN(jackbridge_sem_connect(&data->sem.client), false);
  210. }
  211. return true;
  212. }
  213. void BridgeRtClientControl::unmapData() noexcept
  214. {
  215. if (isServer)
  216. {
  217. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  218. jackbridge_shm_unmap(shm, data);
  219. }
  220. data = nullptr;
  221. setRingBuffer(nullptr, false);
  222. }
  223. bool BridgeRtClientControl::waitForClient(const uint msecs) noexcept
  224. {
  225. CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);
  226. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  227. CARLA_SAFE_ASSERT_RETURN(isServer, false);
  228. jackbridge_sem_post(&data->sem.server, true);
  229. return jackbridge_sem_timedwait(&data->sem.client, msecs, true);
  230. }
  231. bool BridgeRtClientControl::writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept
  232. {
  233. return writeUInt(static_cast<uint32_t>(opcode));
  234. }
  235. PluginBridgeRtClientOpcode BridgeRtClientControl::readOpcode() noexcept
  236. {
  237. CARLA_SAFE_ASSERT_RETURN(! isServer, kPluginBridgeRtClientNull);
  238. return static_cast<PluginBridgeRtClientOpcode>(readUInt());
  239. }
  240. BridgeRtClientControl::WaitHelper::WaitHelper(BridgeRtClientControl& c) noexcept
  241. : data(c.data),
  242. ok(jackbridge_sem_timedwait(&data->sem.server, 5000, false)) {}
  243. BridgeRtClientControl::WaitHelper::~WaitHelper() noexcept
  244. {
  245. if (ok)
  246. jackbridge_sem_post(&data->sem.client, false);
  247. }
  248. // -------------------------------------------------------------------------------------------------------------------
  249. BridgeNonRtClientControl::BridgeNonRtClientControl() noexcept
  250. : data(nullptr),
  251. filename(),
  252. mutex(),
  253. isServer(false)
  254. {
  255. carla_zeroChars(shm, 64);
  256. jackbridge_shm_init(shm);
  257. }
  258. BridgeNonRtClientControl::~BridgeNonRtClientControl() noexcept
  259. {
  260. // should be cleared by now
  261. CARLA_SAFE_ASSERT(data == nullptr);
  262. clear();
  263. }
  264. bool BridgeNonRtClientControl::initializeServer() noexcept
  265. {
  266. char tmpFileBase[64] = {};
  267. std::snprintf(tmpFileBase, sizeof(tmpFileBase)-1, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "XXXXXX");
  268. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  269. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  270. void* const shmptr = shm;
  271. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  272. carla_copyStruct(shm1, shm2);
  273. filename = tmpFileBase;
  274. isServer = true;
  275. if (! mapData())
  276. {
  277. jackbridge_shm_close(shm);
  278. jackbridge_shm_init(shm);
  279. return false;
  280. }
  281. CARLA_SAFE_ASSERT(data != nullptr);
  282. return true;
  283. }
  284. bool BridgeNonRtClientControl::attachClient(const char* const basename) noexcept
  285. {
  286. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  287. // must be invalid right now
  288. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  289. filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT;
  290. filename += basename;
  291. jackbridge_shm_attach(shm, filename);
  292. return jackbridge_shm_is_valid(shm);
  293. }
  294. void BridgeNonRtClientControl::clear() noexcept
  295. {
  296. filename.clear();
  297. if (data != nullptr)
  298. unmapData();
  299. if (! jackbridge_shm_is_valid(shm))
  300. {
  301. if (! isServer) {
  302. CARLA_SAFE_ASSERT(data == nullptr);
  303. }
  304. return;
  305. }
  306. jackbridge_shm_close(shm);
  307. jackbridge_shm_init(shm);
  308. }
  309. bool BridgeNonRtClientControl::mapData() noexcept
  310. {
  311. CARLA_SAFE_ASSERT(data == nullptr);
  312. if (jackbridge_shm_map2<BridgeNonRtClientData>(shm, data))
  313. {
  314. setRingBuffer(&data->ringBuffer, isServer);
  315. return true;
  316. }
  317. return false;
  318. }
  319. void BridgeNonRtClientControl::unmapData() noexcept
  320. {
  321. if (isServer)
  322. {
  323. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  324. jackbridge_shm_unmap(shm, data);
  325. }
  326. data = nullptr;
  327. setRingBuffer(nullptr, false);
  328. }
  329. void BridgeNonRtClientControl::waitIfDataIsReachingLimit() noexcept
  330. {
  331. CARLA_SAFE_ASSERT_RETURN(isServer,);
  332. if (getAvailableDataSize() < BigStackBuffer::size/4)
  333. return;
  334. for (int i=50; --i >= 0;)
  335. {
  336. if (getAvailableDataSize() >= BigStackBuffer::size*3/4)
  337. {
  338. writeOpcode(kPluginBridgeNonRtClientPing);
  339. commitWrite();
  340. return;
  341. }
  342. carla_msleep(20);
  343. }
  344. carla_stderr("Server waitIfDataIsReachingLimit() reached and failed");
  345. }
  346. bool BridgeNonRtClientControl::writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept
  347. {
  348. CARLA_SAFE_ASSERT_RETURN(isServer, false);
  349. return writeUInt(static_cast<uint32_t>(opcode));
  350. }
  351. PluginBridgeNonRtClientOpcode BridgeNonRtClientControl::readOpcode() noexcept
  352. {
  353. CARLA_SAFE_ASSERT_RETURN(! isServer, kPluginBridgeNonRtClientNull);
  354. return static_cast<PluginBridgeNonRtClientOpcode>(readUInt());
  355. }
  356. // -------------------------------------------------------------------------------------------------------------------
  357. BridgeNonRtServerControl::BridgeNonRtServerControl() noexcept
  358. : data(nullptr),
  359. filename(),
  360. mutex(),
  361. isServer(false)
  362. {
  363. carla_zeroChars(shm, 64);
  364. jackbridge_shm_init(shm);
  365. }
  366. BridgeNonRtServerControl::~BridgeNonRtServerControl() noexcept
  367. {
  368. // should be cleared by now
  369. CARLA_SAFE_ASSERT(data == nullptr);
  370. clear();
  371. }
  372. bool BridgeNonRtServerControl::initializeServer() noexcept
  373. {
  374. char tmpFileBase[64] = {};
  375. std::snprintf(tmpFileBase, sizeof(tmpFileBase)-1, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "XXXXXX");
  376. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  377. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  378. void* const shmptr = shm;
  379. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  380. carla_copyStruct(shm1, shm2);
  381. filename = tmpFileBase;
  382. isServer = true;
  383. if (! mapData())
  384. {
  385. jackbridge_shm_close(shm);
  386. jackbridge_shm_init(shm);
  387. return false;
  388. }
  389. CARLA_SAFE_ASSERT(data != nullptr);
  390. return true;
  391. }
  392. bool BridgeNonRtServerControl::attachClient(const char* const basename) noexcept
  393. {
  394. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  395. // must be invalid right now
  396. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  397. filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER;
  398. filename += basename;
  399. jackbridge_shm_attach(shm, filename);
  400. return jackbridge_shm_is_valid(shm);
  401. }
  402. void BridgeNonRtServerControl::clear() noexcept
  403. {
  404. filename.clear();
  405. if (data != nullptr)
  406. unmapData();
  407. if (! jackbridge_shm_is_valid(shm))
  408. {
  409. CARLA_SAFE_ASSERT(data == nullptr);
  410. return;
  411. }
  412. jackbridge_shm_close(shm);
  413. jackbridge_shm_init(shm);
  414. }
  415. bool BridgeNonRtServerControl::mapData() noexcept
  416. {
  417. CARLA_SAFE_ASSERT(data == nullptr);
  418. if (jackbridge_shm_map2<BridgeNonRtServerData>(shm, data))
  419. {
  420. setRingBuffer(&data->ringBuffer, isServer);
  421. return true;
  422. }
  423. return false;
  424. }
  425. void BridgeNonRtServerControl::unmapData() noexcept
  426. {
  427. if (isServer)
  428. {
  429. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  430. jackbridge_shm_unmap(shm, data);
  431. }
  432. data = nullptr;
  433. setRingBuffer(nullptr, false);
  434. }
  435. PluginBridgeNonRtServerOpcode BridgeNonRtServerControl::readOpcode() noexcept
  436. {
  437. CARLA_SAFE_ASSERT_RETURN(isServer, kPluginBridgeNonRtServerNull);
  438. return static_cast<PluginBridgeNonRtServerOpcode>(readUInt());
  439. }
  440. void BridgeNonRtServerControl::waitIfDataIsReachingLimit() noexcept
  441. {
  442. CARLA_SAFE_ASSERT_RETURN(! isServer,);
  443. if (getAvailableDataSize() < HugeStackBuffer::size/4)
  444. return;
  445. for (int i=50; --i >= 0;)
  446. {
  447. if (getAvailableDataSize() >= HugeStackBuffer::size*3/4)
  448. {
  449. writeOpcode(kPluginBridgeNonRtServerPong);
  450. commitWrite();
  451. return;
  452. }
  453. carla_msleep(20);
  454. }
  455. carla_stderr("Client waitIfDataIsReachingLimit() reached and failed");
  456. }
  457. bool BridgeNonRtServerControl::writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept
  458. {
  459. CARLA_SAFE_ASSERT_RETURN(! isServer, false);
  460. return writeUInt(static_cast<uint32_t>(opcode));
  461. }
  462. // -------------------------------------------------------------------------------------------------------------------