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.

598 lines
15KB

  1. /*
  2. * Carla Bridge utils
  3. * Copyright (C) 2013-2017 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_WINDOWS)
  22. # define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "Global\\carla-bridge_shm_ap_"
  23. # define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "Global\\carla-bridge_shm_rtC_"
  24. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "Global\\carla-bridge_shm_nonrtC_"
  25. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "Global\\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::sprintf(tmpFileBase, 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::sprintf(tmpFileBase, 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. needsSemDestroy = true;
  144. if (! mapData())
  145. {
  146. jackbridge_shm_close(shm);
  147. jackbridge_shm_init(shm);
  148. return false;
  149. }
  150. CARLA_SAFE_ASSERT(data != nullptr);
  151. if (! jackbridge_sem_init(&data->sem.server))
  152. {
  153. unmapData();
  154. jackbridge_shm_close(shm);
  155. jackbridge_shm_init(shm);
  156. return false;
  157. }
  158. if (! jackbridge_sem_init(&data->sem.client))
  159. {
  160. jackbridge_sem_destroy(&data->sem.server);
  161. unmapData();
  162. jackbridge_shm_close(shm);
  163. jackbridge_shm_init(shm);
  164. return false;
  165. }
  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. return false;
  231. }
  232. bool BridgeRtClientControl::writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept
  233. {
  234. return writeUInt(static_cast<uint32_t>(opcode));
  235. }
  236. PluginBridgeRtClientOpcode BridgeRtClientControl::readOpcode() noexcept
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(! isServer, kPluginBridgeRtClientNull);
  239. return static_cast<PluginBridgeRtClientOpcode>(readUInt());
  240. }
  241. BridgeRtClientControl::WaitHelper::WaitHelper(BridgeRtClientControl& c) noexcept
  242. : data(c.data),
  243. ok(jackbridge_sem_timedwait(&data->sem.server, 5000, false)) {}
  244. BridgeRtClientControl::WaitHelper::~WaitHelper() noexcept
  245. {
  246. if (ok)
  247. jackbridge_sem_post(&data->sem.client, false);
  248. }
  249. // -------------------------------------------------------------------------------------------------------------------
  250. BridgeNonRtClientControl::BridgeNonRtClientControl() noexcept
  251. : data(nullptr),
  252. filename(),
  253. mutex(),
  254. isServer(false)
  255. {
  256. carla_zeroChars(shm, 64);
  257. jackbridge_shm_init(shm);
  258. }
  259. BridgeNonRtClientControl::~BridgeNonRtClientControl() noexcept
  260. {
  261. // should be cleared by now
  262. CARLA_SAFE_ASSERT(data == nullptr);
  263. clear();
  264. }
  265. bool BridgeNonRtClientControl::initializeServer() noexcept
  266. {
  267. char tmpFileBase[64];
  268. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "XXXXXX");
  269. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  270. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  271. void* const shmptr = shm;
  272. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  273. carla_copyStruct(shm1, shm2);
  274. filename = tmpFileBase;
  275. isServer = true;
  276. if (! mapData())
  277. {
  278. jackbridge_shm_close(shm);
  279. jackbridge_shm_init(shm);
  280. return false;
  281. }
  282. CARLA_SAFE_ASSERT(data != nullptr);
  283. return true;
  284. }
  285. bool BridgeNonRtClientControl::attachClient(const char* const basename) noexcept
  286. {
  287. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  288. // must be invalid right now
  289. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  290. filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT;
  291. filename += basename;
  292. jackbridge_shm_attach(shm, filename);
  293. return jackbridge_shm_is_valid(shm);
  294. }
  295. void BridgeNonRtClientControl::clear() noexcept
  296. {
  297. filename.clear();
  298. if (data != nullptr)
  299. unmapData();
  300. if (! jackbridge_shm_is_valid(shm))
  301. {
  302. if (! isServer) {
  303. CARLA_SAFE_ASSERT(data == nullptr);
  304. }
  305. return;
  306. }
  307. jackbridge_shm_close(shm);
  308. jackbridge_shm_init(shm);
  309. }
  310. bool BridgeNonRtClientControl::mapData() noexcept
  311. {
  312. CARLA_SAFE_ASSERT(data == nullptr);
  313. if (jackbridge_shm_map2<BridgeNonRtClientData>(shm, data))
  314. {
  315. setRingBuffer(&data->ringBuffer, isServer);
  316. return true;
  317. }
  318. return false;
  319. }
  320. void BridgeNonRtClientControl::unmapData() noexcept
  321. {
  322. if (isServer)
  323. {
  324. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  325. jackbridge_shm_unmap(shm, data);
  326. }
  327. data = nullptr;
  328. setRingBuffer(nullptr, false);
  329. }
  330. void BridgeNonRtClientControl::waitIfDataIsReachingLimit() noexcept
  331. {
  332. CARLA_SAFE_ASSERT_RETURN(isServer,);
  333. if (getAvailableDataSize() < BigStackBuffer::size/4)
  334. return;
  335. for (int i=50; --i >= 0;)
  336. {
  337. if (getAvailableDataSize() >= BigStackBuffer::size*3/4)
  338. {
  339. writeOpcode(kPluginBridgeNonRtClientPing);
  340. commitWrite();
  341. return;
  342. }
  343. carla_msleep(20);
  344. }
  345. carla_stderr("Server waitIfDataIsReachingLimit() reached and failed");
  346. }
  347. bool BridgeNonRtClientControl::writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept
  348. {
  349. CARLA_SAFE_ASSERT_RETURN(isServer, false);
  350. return writeUInt(static_cast<uint32_t>(opcode));
  351. }
  352. PluginBridgeNonRtClientOpcode BridgeNonRtClientControl::readOpcode() noexcept
  353. {
  354. CARLA_SAFE_ASSERT_RETURN(! isServer, kPluginBridgeNonRtClientNull);
  355. return static_cast<PluginBridgeNonRtClientOpcode>(readUInt());
  356. }
  357. // -------------------------------------------------------------------------------------------------------------------
  358. BridgeNonRtServerControl::BridgeNonRtServerControl() noexcept
  359. : data(nullptr),
  360. filename(),
  361. mutex(),
  362. isServer(false)
  363. {
  364. carla_zeroChars(shm, 64);
  365. jackbridge_shm_init(shm);
  366. }
  367. BridgeNonRtServerControl::~BridgeNonRtServerControl() noexcept
  368. {
  369. // should be cleared by now
  370. CARLA_SAFE_ASSERT(data == nullptr);
  371. clear();
  372. }
  373. bool BridgeNonRtServerControl::initializeServer() noexcept
  374. {
  375. char tmpFileBase[64];
  376. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "XXXXXX");
  377. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  378. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  379. void* const shmptr = shm;
  380. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  381. carla_copyStruct(shm1, shm2);
  382. filename = tmpFileBase;
  383. isServer = true;
  384. if (! mapData())
  385. {
  386. jackbridge_shm_close(shm);
  387. jackbridge_shm_init(shm);
  388. return false;
  389. }
  390. CARLA_SAFE_ASSERT(data != nullptr);
  391. return true;
  392. }
  393. bool BridgeNonRtServerControl::attachClient(const char* const basename) noexcept
  394. {
  395. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  396. // must be invalid right now
  397. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  398. filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER;
  399. filename += basename;
  400. jackbridge_shm_attach(shm, filename);
  401. return jackbridge_shm_is_valid(shm);
  402. }
  403. void BridgeNonRtServerControl::clear() noexcept
  404. {
  405. filename.clear();
  406. if (data != nullptr)
  407. unmapData();
  408. if (! jackbridge_shm_is_valid(shm))
  409. {
  410. CARLA_SAFE_ASSERT(data == nullptr);
  411. return;
  412. }
  413. jackbridge_shm_close(shm);
  414. jackbridge_shm_init(shm);
  415. }
  416. bool BridgeNonRtServerControl::mapData() noexcept
  417. {
  418. CARLA_SAFE_ASSERT(data == nullptr);
  419. if (jackbridge_shm_map2<BridgeNonRtServerData>(shm, data))
  420. {
  421. setRingBuffer(&data->ringBuffer, isServer);
  422. return true;
  423. }
  424. return false;
  425. }
  426. void BridgeNonRtServerControl::unmapData() noexcept
  427. {
  428. if (isServer)
  429. {
  430. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  431. jackbridge_shm_unmap(shm, data);
  432. }
  433. data = nullptr;
  434. setRingBuffer(nullptr, false);
  435. }
  436. PluginBridgeNonRtServerOpcode BridgeNonRtServerControl::readOpcode() noexcept
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(isServer, kPluginBridgeNonRtServerNull);
  439. return static_cast<PluginBridgeNonRtServerOpcode>(readUInt());
  440. }
  441. void BridgeNonRtServerControl::waitIfDataIsReachingLimit() noexcept
  442. {
  443. CARLA_SAFE_ASSERT_RETURN(! isServer,);
  444. if (getAvailableDataSize() < HugeStackBuffer::size/4)
  445. return;
  446. for (int i=50; --i >= 0;)
  447. {
  448. if (getAvailableDataSize() >= HugeStackBuffer::size*3/4)
  449. {
  450. writeOpcode(kPluginBridgeNonRtServerPong);
  451. commitWrite();
  452. return;
  453. }
  454. carla_msleep(20);
  455. }
  456. carla_stderr("Client waitIfDataIsReachingLimit() reached and failed");
  457. }
  458. bool BridgeNonRtServerControl::writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept
  459. {
  460. CARLA_SAFE_ASSERT_RETURN(! isServer, false);
  461. return writeUInt(static_cast<uint32_t>(opcode));
  462. }
  463. // -------------------------------------------------------------------------------------------------------------------