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.

589 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. // -------------------------------------------------------------------------------------------------------------------
  110. BridgeRtClientControl::BridgeRtClientControl() noexcept
  111. : data(nullptr),
  112. filename(),
  113. needsSemDestroy(false),
  114. isServer(false)
  115. {
  116. carla_zeroChars(shm, 64);
  117. jackbridge_shm_init(shm);
  118. }
  119. BridgeRtClientControl::~BridgeRtClientControl() noexcept
  120. {
  121. // should be cleared by now
  122. CARLA_SAFE_ASSERT(data == nullptr);
  123. clear();
  124. }
  125. bool BridgeRtClientControl::initializeServer() noexcept
  126. {
  127. char tmpFileBase[64];
  128. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "XXXXXX");
  129. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  130. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  131. void* const shmptr = shm;
  132. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  133. carla_copyStruct(shm1, shm2);
  134. // NEW
  135. if (! mapData())
  136. {
  137. jackbridge_shm_close(shm);
  138. jackbridge_shm_init(shm);
  139. return false;
  140. }
  141. CARLA_SAFE_ASSERT(data != nullptr);
  142. if (! jackbridge_sem_init(&data->sem.server))
  143. {
  144. unmapData();
  145. jackbridge_shm_close(shm);
  146. jackbridge_shm_init(shm);
  147. return false;
  148. }
  149. if (! jackbridge_sem_init(&data->sem.client))
  150. {
  151. jackbridge_sem_destroy(&data->sem.server);
  152. unmapData();
  153. jackbridge_shm_close(shm);
  154. jackbridge_shm_init(shm);
  155. return false;
  156. }
  157. // NEW
  158. filename = tmpFileBase;
  159. isServer = true;
  160. needsSemDestroy = true;
  161. return true;
  162. }
  163. bool BridgeRtClientControl::attachClient(const char* const basename) noexcept
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  166. // must be invalid right now
  167. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  168. filename = PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT;
  169. filename += basename;
  170. jackbridge_shm_attach(shm, filename);
  171. return jackbridge_shm_is_valid(shm);
  172. }
  173. void BridgeRtClientControl::clear() noexcept
  174. {
  175. filename.clear();
  176. if (needsSemDestroy)
  177. {
  178. jackbridge_sem_destroy(&data->sem.client);
  179. jackbridge_sem_destroy(&data->sem.server);
  180. needsSemDestroy = false;
  181. }
  182. if (data != nullptr)
  183. unmapData();
  184. if (! jackbridge_shm_is_valid(shm))
  185. return;
  186. jackbridge_shm_close(shm);
  187. jackbridge_shm_init(shm);
  188. }
  189. bool BridgeRtClientControl::mapData() noexcept
  190. {
  191. CARLA_SAFE_ASSERT(data == nullptr);
  192. if (! jackbridge_shm_map2<BridgeRtClientData>(shm, data))
  193. return false;
  194. if (isServer)
  195. {
  196. std::memset(data, 0, sizeof(BridgeRtClientData));
  197. setRingBuffer(&data->ringBuffer, true);
  198. }
  199. else
  200. {
  201. CARLA_SAFE_ASSERT(data->midiOut[0] == 0);
  202. setRingBuffer(&data->ringBuffer, false);
  203. CARLA_SAFE_ASSERT_RETURN(jackbridge_sem_connect(&data->sem.server), false);
  204. CARLA_SAFE_ASSERT_RETURN(jackbridge_sem_connect(&data->sem.client), false);
  205. }
  206. return true;
  207. }
  208. void BridgeRtClientControl::unmapData() noexcept
  209. {
  210. if (isServer)
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  213. jackbridge_shm_unmap(shm, data);
  214. }
  215. data = nullptr;
  216. setRingBuffer(nullptr, false);
  217. }
  218. bool BridgeRtClientControl::waitForClient(const uint msecs) noexcept
  219. {
  220. CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);
  221. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  222. CARLA_SAFE_ASSERT_RETURN(isServer, false);
  223. jackbridge_sem_post(&data->sem.server, true);
  224. return jackbridge_sem_timedwait(&data->sem.client, msecs, true);
  225. return false;
  226. }
  227. void BridgeRtClientControl::writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept
  228. {
  229. writeUInt(static_cast<uint32_t>(opcode));
  230. }
  231. PluginBridgeRtClientOpcode BridgeRtClientControl::readOpcode() noexcept
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(! isServer, kPluginBridgeRtClientNull);
  234. return static_cast<PluginBridgeRtClientOpcode>(readUInt());
  235. }
  236. BridgeRtClientControl::WaitHelper::WaitHelper(BridgeRtClientControl& c) noexcept
  237. : data(c.data),
  238. ok(jackbridge_sem_timedwait(&data->sem.server, 5000, false)) {}
  239. BridgeRtClientControl::WaitHelper::~WaitHelper() noexcept
  240. {
  241. if (ok)
  242. jackbridge_sem_post(&data->sem.client, false);
  243. }
  244. // -------------------------------------------------------------------------------------------------------------------
  245. BridgeNonRtClientControl::BridgeNonRtClientControl() noexcept
  246. : data(nullptr),
  247. filename(),
  248. mutex(),
  249. isServer(false)
  250. {
  251. carla_zeroChars(shm, 64);
  252. jackbridge_shm_init(shm);
  253. }
  254. BridgeNonRtClientControl::~BridgeNonRtClientControl() noexcept
  255. {
  256. // should be cleared by now
  257. CARLA_SAFE_ASSERT(data == nullptr);
  258. clear();
  259. }
  260. bool BridgeNonRtClientControl::initializeServer() noexcept
  261. {
  262. char tmpFileBase[64];
  263. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "XXXXXX");
  264. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  265. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  266. void* const shmptr = shm;
  267. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  268. carla_copyStruct(shm1, shm2);
  269. if (! mapData())
  270. {
  271. jackbridge_shm_close(shm);
  272. jackbridge_shm_init(shm);
  273. return false;
  274. }
  275. CARLA_SAFE_ASSERT(data != nullptr);
  276. filename = tmpFileBase;
  277. isServer = true;
  278. return true;
  279. }
  280. bool BridgeNonRtClientControl::attachClient(const char* const basename) noexcept
  281. {
  282. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  283. // must be invalid right now
  284. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  285. filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT;
  286. filename += basename;
  287. jackbridge_shm_attach(shm, filename);
  288. return jackbridge_shm_is_valid(shm);
  289. }
  290. void BridgeNonRtClientControl::clear() noexcept
  291. {
  292. filename.clear();
  293. if (data != nullptr)
  294. unmapData();
  295. if (! jackbridge_shm_is_valid(shm))
  296. {
  297. if (! isServer) {
  298. CARLA_SAFE_ASSERT(data == nullptr);
  299. }
  300. return;
  301. }
  302. jackbridge_shm_close(shm);
  303. jackbridge_shm_init(shm);
  304. }
  305. bool BridgeNonRtClientControl::mapData() noexcept
  306. {
  307. CARLA_SAFE_ASSERT(data == nullptr);
  308. if (jackbridge_shm_map2<BridgeNonRtClientData>(shm, data))
  309. {
  310. setRingBuffer(&data->ringBuffer, isServer);
  311. return true;
  312. }
  313. return false;
  314. }
  315. void BridgeNonRtClientControl::unmapData() noexcept
  316. {
  317. if (isServer)
  318. {
  319. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  320. jackbridge_shm_unmap(shm, data);
  321. }
  322. data = nullptr;
  323. setRingBuffer(nullptr, false);
  324. }
  325. void BridgeNonRtClientControl::waitIfDataIsReachingLimit() noexcept
  326. {
  327. CARLA_SAFE_ASSERT_RETURN(isServer,);
  328. if (getAvailableDataSize() < BigStackBuffer::size/4)
  329. return;
  330. for (int i=50; --i >= 0;)
  331. {
  332. if (getAvailableDataSize() >= BigStackBuffer::size*3/4)
  333. {
  334. writeOpcode(kPluginBridgeNonRtClientPing);
  335. commitWrite();
  336. return;
  337. }
  338. carla_msleep(20);
  339. }
  340. carla_stderr("Server waitIfDataIsReachingLimit() reached and failed");
  341. }
  342. void BridgeNonRtClientControl::writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept
  343. {
  344. CARLA_SAFE_ASSERT_RETURN(isServer,);
  345. writeUInt(static_cast<uint32_t>(opcode));
  346. }
  347. PluginBridgeNonRtClientOpcode BridgeNonRtClientControl::readOpcode() noexcept
  348. {
  349. CARLA_SAFE_ASSERT_RETURN(! isServer, kPluginBridgeNonRtClientNull);
  350. return static_cast<PluginBridgeNonRtClientOpcode>(readUInt());
  351. }
  352. // -------------------------------------------------------------------------------------------------------------------
  353. BridgeNonRtServerControl::BridgeNonRtServerControl() noexcept
  354. : data(nullptr),
  355. filename(),
  356. mutex(),
  357. isServer(false)
  358. {
  359. carla_zeroChars(shm, 64);
  360. jackbridge_shm_init(shm);
  361. }
  362. BridgeNonRtServerControl::~BridgeNonRtServerControl() noexcept
  363. {
  364. // should be cleared by now
  365. CARLA_SAFE_ASSERT(data == nullptr);
  366. clear();
  367. }
  368. bool BridgeNonRtServerControl::initializeServer() noexcept
  369. {
  370. char tmpFileBase[64];
  371. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "XXXXXX");
  372. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  373. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  374. void* const shmptr = shm;
  375. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  376. carla_copyStruct(shm1, shm2);
  377. if (! mapData())
  378. {
  379. jackbridge_shm_close(shm);
  380. jackbridge_shm_init(shm);
  381. return false;
  382. }
  383. CARLA_SAFE_ASSERT(data != nullptr);
  384. filename = tmpFileBase;
  385. isServer = true;
  386. return true;
  387. }
  388. bool BridgeNonRtServerControl::attachClient(const char* const basename) noexcept
  389. {
  390. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  391. // must be invalid right now
  392. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  393. filename = PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER;
  394. filename += basename;
  395. jackbridge_shm_attach(shm, filename);
  396. return jackbridge_shm_is_valid(shm);
  397. }
  398. void BridgeNonRtServerControl::clear() noexcept
  399. {
  400. filename.clear();
  401. if (data != nullptr)
  402. unmapData();
  403. if (! jackbridge_shm_is_valid(shm))
  404. {
  405. CARLA_SAFE_ASSERT(data == nullptr);
  406. return;
  407. }
  408. jackbridge_shm_close(shm);
  409. jackbridge_shm_init(shm);
  410. }
  411. bool BridgeNonRtServerControl::mapData() noexcept
  412. {
  413. CARLA_SAFE_ASSERT(data == nullptr);
  414. if (jackbridge_shm_map2<BridgeNonRtServerData>(shm, data))
  415. {
  416. setRingBuffer(&data->ringBuffer, isServer);
  417. return true;
  418. }
  419. return false;
  420. }
  421. void BridgeNonRtServerControl::unmapData() noexcept
  422. {
  423. if (isServer)
  424. {
  425. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  426. jackbridge_shm_unmap(shm, data);
  427. }
  428. data = nullptr;
  429. setRingBuffer(nullptr, false);
  430. }
  431. PluginBridgeNonRtServerOpcode BridgeNonRtServerControl::readOpcode() noexcept
  432. {
  433. CARLA_SAFE_ASSERT_RETURN(isServer, kPluginBridgeNonRtServerNull);
  434. return static_cast<PluginBridgeNonRtServerOpcode>(readUInt());
  435. }
  436. void BridgeNonRtServerControl::waitIfDataIsReachingLimit() noexcept
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(! isServer,);
  439. if (getAvailableDataSize() < HugeStackBuffer::size/4)
  440. return;
  441. for (int i=50; --i >= 0;)
  442. {
  443. if (getAvailableDataSize() >= HugeStackBuffer::size*3/4)
  444. {
  445. writeOpcode(kPluginBridgeNonRtServerPong);
  446. commitWrite();
  447. return;
  448. }
  449. carla_msleep(20);
  450. }
  451. carla_stderr("Client waitIfDataIsReachingLimit() reached and failed");
  452. }
  453. void BridgeNonRtServerControl::writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept
  454. {
  455. CARLA_SAFE_ASSERT_RETURN(! isServer,);
  456. writeUInt(static_cast<uint32_t>(opcode));
  457. }
  458. // -------------------------------------------------------------------------------------------------------------------