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.

libjack.cpp 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2019 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 "libjack.hpp"
  18. #include "CarlaThread.hpp"
  19. #include <signal.h>
  20. #include <sys/prctl.h>
  21. #include <sys/time.h>
  22. // ---------------------------------------------------------------------------------------------------------------------
  23. CARLA_EXPORT
  24. int jack_carla_interposed_action(uint, uint, void*)
  25. {
  26. static bool printWarning = true;
  27. if (printWarning)
  28. {
  29. printWarning = false;
  30. carla_stderr2("Non-exported jack_carla_interposed_action called, this should not happen!!");
  31. carla_stderr("Printing some info:");
  32. carla_stderr("\tLD_LIBRARY_PATH: '%s'", std::getenv("LD_LIBRARY_PATH"));
  33. carla_stderr("\tLD_PRELOAD: '%s'", std::getenv("LD_PRELOAD"));
  34. std::fflush(stderr);
  35. }
  36. // ::kill(::getpid(), SIGKILL);
  37. return 1337;
  38. }
  39. // ---------------------------------------------------------------------------------------------------------------------
  40. CARLA_BACKEND_START_NAMESPACE
  41. // ---------------------------------------------------------------------------------------------------------------------
  42. static int64_t getCurrentTimeMilliseconds() noexcept
  43. {
  44. struct timeval tv;
  45. gettimeofday (&tv, nullptr);
  46. return ((int64_t) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  47. }
  48. static int carla_interposed_callback(int, void*);
  49. // ---------------------------------------------------------------------------------------------------------------------
  50. class CarlaJackRealtimeThread : public CarlaThread
  51. {
  52. public:
  53. struct Callback {
  54. Callback() {}
  55. virtual ~Callback() {};
  56. virtual void runRealtimeThread() = 0;
  57. };
  58. CarlaJackRealtimeThread(Callback* const callback)
  59. : CarlaThread("CarlaJackRealtimeThread"),
  60. fCallback(callback) {}
  61. protected:
  62. void run() override
  63. {
  64. fCallback->runRealtimeThread();
  65. }
  66. private:
  67. Callback* const fCallback;
  68. CARLA_DECLARE_NON_COPY_CLASS(CarlaJackRealtimeThread)
  69. };
  70. // --------------------------------------------------------------------------------------------------------------------
  71. class CarlaJackNonRealtimeThread : public CarlaThread
  72. {
  73. public:
  74. struct Callback {
  75. Callback() {}
  76. virtual ~Callback() {};
  77. virtual void runNonRealtimeThread() = 0;
  78. };
  79. CarlaJackNonRealtimeThread(Callback* const callback)
  80. : CarlaThread("CarlaJackNonRealtimeThread"),
  81. fCallback(callback) {}
  82. protected:
  83. void run() override
  84. {
  85. fCallback->runNonRealtimeThread();
  86. }
  87. private:
  88. Callback* const fCallback;
  89. CARLA_DECLARE_NON_COPY_CLASS(CarlaJackNonRealtimeThread)
  90. };
  91. // ---------------------------------------------------------------------------------------------------------------------
  92. class CarlaJackAppClient : public CarlaJackRealtimeThread::Callback,
  93. public CarlaJackNonRealtimeThread::Callback
  94. {
  95. public:
  96. JackServerState fServer;
  97. LinkedList<JackClientState*> fClients;
  98. LinkedList<JackClientState*> fNewClients;
  99. CarlaJackAppClient()
  100. : fServer(this),
  101. fClients(),
  102. fNewClients(),
  103. fShmAudioPool(),
  104. fShmRtClientControl(),
  105. fShmNonRtClientControl(),
  106. fShmNonRtServerControl(),
  107. fAudioPoolCopy(nullptr),
  108. fAudioTmpBuf(nullptr),
  109. fDummyMidiInBuffer(true),
  110. fDummyMidiOutBuffer(false),
  111. fMidiInBuffers(nullptr),
  112. fMidiOutBuffers(nullptr),
  113. fIsOffline(false),
  114. fIsReady(false),
  115. fLastPingTime(-1),
  116. fSessionManager(0),
  117. fSetupHints(0),
  118. fRealtimeThread(this),
  119. fNonRealtimeThread(this),
  120. fRealtimeThreadMutex()
  121. #ifdef DEBUG
  122. ,leakDetector_CarlaJackAppClient()
  123. #endif
  124. {
  125. carla_debug("CarlaJackAppClient::CarlaJackAppClient()");
  126. const char* const shmIds(std::getenv("CARLA_SHM_IDS"));
  127. CARLA_SAFE_ASSERT_INT2_RETURN(shmIds != nullptr && std::strlen(shmIds) == 6*4, std::strlen(shmIds), 6*4,);
  128. const char* const libjackSetup(std::getenv("CARLA_LIBJACK_SETUP"));
  129. CARLA_SAFE_ASSERT_RETURN(libjackSetup != nullptr && std::strlen(libjackSetup) >= 6,);
  130. // make sure we don't get loaded again
  131. carla_unsetenv("CARLA_SHM_IDS");
  132. // kill ourselves if main carla dies
  133. carla_terminateProcessOnParentExit(true);
  134. for (int i=4; --i >= 0;) {
  135. CARLA_SAFE_ASSERT_RETURN(libjackSetup[i] >= '0' && libjackSetup[i] <= '0'+64,);
  136. }
  137. CARLA_SAFE_ASSERT_RETURN(libjackSetup[4] >= '0' && libjackSetup[4] <= '0' + LIBJACK_SESSION_MANAGER_NSM,);
  138. CARLA_SAFE_ASSERT_RETURN(libjackSetup[5] >= '0' && libjackSetup[5] < '0'+0x4f,);
  139. std::memcpy(fBaseNameAudioPool, shmIds+6*0, 6);
  140. std::memcpy(fBaseNameRtClientControl, shmIds+6*1, 6);
  141. std::memcpy(fBaseNameNonRtClientControl, shmIds+6*2, 6);
  142. std::memcpy(fBaseNameNonRtServerControl, shmIds+6*3, 6);
  143. fBaseNameAudioPool[6] = '\0';
  144. fBaseNameRtClientControl[6] = '\0';
  145. fBaseNameNonRtClientControl[6] = '\0';
  146. fBaseNameNonRtServerControl[6] = '\0';
  147. fServer.numAudioIns = static_cast<uint8_t>(libjackSetup[0] - '0');
  148. fServer.numAudioOuts = static_cast<uint8_t>(libjackSetup[1] - '0');
  149. fServer.numMidiIns = static_cast<uint8_t>(libjackSetup[2] - '0');
  150. fServer.numMidiOuts = static_cast<uint8_t>(libjackSetup[3] - '0');
  151. fSessionManager = static_cast<uint>(libjackSetup[4] - '0');
  152. fSetupHints = static_cast<uint>(libjackSetup[5] - '0');
  153. jack_carla_interposed_action(LIBJACK_INTERPOSER_ACTION_SET_HINTS_AND_CALLBACK,
  154. fSetupHints,
  155. (void*)carla_interposed_callback);
  156. jack_carla_interposed_action(LIBJACK_INTERPOSER_ACTION_SET_SESSION_MANAGER,
  157. fSessionManager,
  158. nullptr);
  159. fNonRealtimeThread.startThread(false);
  160. }
  161. ~CarlaJackAppClient() noexcept override
  162. {
  163. carla_debug("CarlaJackAppClient::~CarlaJackAppClient()");
  164. fLastPingTime = -1;
  165. fNonRealtimeThread.stopThread(5000);
  166. const CarlaMutexLocker cms(fRealtimeThreadMutex);
  167. for (LinkedList<JackClientState*>::Itenerator it = fClients.begin2(); it.valid(); it.next())
  168. {
  169. JackClientState* const jclient(it.getValue(nullptr));
  170. CARLA_SAFE_ASSERT_CONTINUE(jclient != nullptr);
  171. delete jclient;
  172. }
  173. fClients.clear();
  174. }
  175. JackClientState* createClient(const char* const name)
  176. {
  177. while (fNonRealtimeThread.isThreadRunning() && ! fIsReady)
  178. carla_sleep(1);
  179. return new JackClientState(fServer, name);
  180. }
  181. void destroyClient(JackClientState* const jclient)
  182. {
  183. {
  184. const CarlaMutexLocker cms(fRealtimeThreadMutex);
  185. fClients.removeOne(jclient);
  186. }
  187. delete jclient;
  188. }
  189. bool activateClient(JackClientState* const jclient)
  190. {
  191. const CarlaMutexLocker cms(fRealtimeThreadMutex);
  192. if (! fClients.append(jclient))
  193. return false;
  194. if (! fNewClients.append(jclient))
  195. {
  196. fClients.removeOne(jclient);
  197. return false;
  198. }
  199. jclient->activated = true;
  200. jclient->deactivated = false;
  201. return true;
  202. }
  203. bool deactivateClient(JackClientState* const jclient)
  204. {
  205. const CarlaMutexLocker cms(fRealtimeThreadMutex);
  206. if (! fClients.removeOne(jclient))
  207. return false;
  208. jclient->activated = false;
  209. jclient->deactivated = true;
  210. return true;
  211. }
  212. pthread_t getRealtimeThreadId() const noexcept
  213. {
  214. return (pthread_t)fRealtimeThread.getThreadId();
  215. }
  216. int handleInterposerCallback(const int cb_action, void* const ptr)
  217. {
  218. carla_debug("handleInterposerCallback(%o, %p)", cb_action, ptr);
  219. switch (cb_action)
  220. {
  221. case LIBJACK_INTERPOSER_CALLBACK_UI_HIDE: {
  222. const CarlaMutexLocker cml(fShmNonRtServerControl.mutex);
  223. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerUiClosed);
  224. fShmNonRtServerControl.commitWrite();
  225. break;
  226. }
  227. }
  228. return 0;
  229. // maybe unused
  230. (void)ptr;
  231. }
  232. // -------------------------------------------------------------------
  233. protected:
  234. void runRealtimeThread() override;
  235. void runNonRealtimeThread() override;
  236. private:
  237. bool initSharedMemmory();
  238. void clearSharedMemory() noexcept;
  239. bool handleRtData();
  240. bool handleNonRtData();
  241. BridgeAudioPool fShmAudioPool;
  242. BridgeRtClientControl fShmRtClientControl;
  243. BridgeNonRtClientControl fShmNonRtClientControl;
  244. BridgeNonRtServerControl fShmNonRtServerControl;
  245. float* fAudioPoolCopy;
  246. float* fAudioTmpBuf;
  247. JackMidiPortBufferDummy fDummyMidiInBuffer;
  248. JackMidiPortBufferDummy fDummyMidiOutBuffer;
  249. JackMidiPortBufferOnStack* fMidiInBuffers;
  250. JackMidiPortBufferOnStack* fMidiOutBuffers;
  251. char fBaseNameAudioPool[6+1];
  252. char fBaseNameRtClientControl[6+1];
  253. char fBaseNameNonRtClientControl[6+1];
  254. char fBaseNameNonRtServerControl[6+1];
  255. bool fIsOffline;
  256. volatile bool fIsReady;
  257. int64_t fLastPingTime;
  258. uint fSessionManager;
  259. uint fSetupHints;
  260. CarlaJackRealtimeThread fRealtimeThread;
  261. CarlaJackNonRealtimeThread fNonRealtimeThread;
  262. CarlaMutex fRealtimeThreadMutex;
  263. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaJackAppClient)
  264. };
  265. // ---------------------------------------------------------------------------------------------------------------------
  266. bool CarlaJackAppClient::initSharedMemmory()
  267. {
  268. if (! fShmAudioPool.attachClient(fBaseNameAudioPool))
  269. {
  270. carla_stderr("Failed to attach to audio pool shared memory");
  271. return false;
  272. }
  273. if (! fShmRtClientControl.attachClient(fBaseNameRtClientControl))
  274. {
  275. clearSharedMemory();
  276. carla_stderr("Failed to attach to rt client control shared memory");
  277. return false;
  278. }
  279. if (! fShmRtClientControl.mapData())
  280. {
  281. clearSharedMemory();
  282. carla_stderr("Failed to map rt client control shared memory");
  283. return false;
  284. }
  285. if (! fShmNonRtClientControl.attachClient(fBaseNameNonRtClientControl))
  286. {
  287. clearSharedMemory();
  288. carla_stderr("Failed to attach to non-rt client control shared memory");
  289. return false;
  290. }
  291. if (! fShmNonRtClientControl.mapData())
  292. {
  293. clearSharedMemory();
  294. carla_stderr("Failed to map non-rt control client shared memory");
  295. return false;
  296. }
  297. if (! fShmNonRtServerControl.attachClient(fBaseNameNonRtServerControl))
  298. {
  299. clearSharedMemory();
  300. carla_stderr("Failed to attach to non-rt server control shared memory");
  301. return false;
  302. }
  303. if (! fShmNonRtServerControl.mapData())
  304. {
  305. clearSharedMemory();
  306. carla_stderr("Failed to map non-rt control server shared memory");
  307. return false;
  308. }
  309. PluginBridgeNonRtClientOpcode opcode;
  310. opcode = fShmNonRtClientControl.readOpcode();
  311. CARLA_SAFE_ASSERT_RETURN(opcode == kPluginBridgeNonRtClientVersion, false);
  312. const uint32_t apiVersion = fShmNonRtClientControl.readUInt();
  313. CARLA_SAFE_ASSERT_RETURN(apiVersion == CARLA_PLUGIN_BRIDGE_API_VERSION, false);
  314. const uint32_t shmRtClientDataSize = fShmNonRtClientControl.readUInt();
  315. CARLA_SAFE_ASSERT_INT2(shmRtClientDataSize == sizeof(BridgeRtClientData), shmRtClientDataSize, sizeof(BridgeRtClientData));
  316. const uint32_t shmNonRtClientDataSize = fShmNonRtClientControl.readUInt();
  317. CARLA_SAFE_ASSERT_INT2(shmNonRtClientDataSize == sizeof(BridgeNonRtClientData), shmNonRtClientDataSize, sizeof(BridgeNonRtClientData));
  318. const uint32_t shmNonRtServerDataSize = fShmNonRtClientControl.readUInt();
  319. CARLA_SAFE_ASSERT_INT2(shmNonRtServerDataSize == sizeof(BridgeNonRtServerData), shmNonRtServerDataSize, sizeof(BridgeNonRtServerData));
  320. if (shmRtClientDataSize != sizeof(BridgeRtClientData) ||
  321. shmNonRtClientDataSize != sizeof(BridgeNonRtClientData) ||
  322. shmNonRtServerDataSize != sizeof(BridgeNonRtServerData))
  323. {
  324. carla_stderr2("CarlaJackAppClient: data size mismatch");
  325. return false;
  326. }
  327. opcode = fShmNonRtClientControl.readOpcode();
  328. CARLA_SAFE_ASSERT_RETURN(opcode == kPluginBridgeNonRtClientInitialSetup, false);
  329. fServer.bufferSize = fShmNonRtClientControl.readUInt();
  330. fServer.sampleRate = fShmNonRtClientControl.readDouble();
  331. if (fServer.bufferSize == 0 || carla_isZero(fServer.sampleRate))
  332. {
  333. carla_stderr2("CarlaJackAppClient: invalid empty state");
  334. return false;
  335. }
  336. fAudioTmpBuf = new float[fServer.bufferSize];
  337. carla_zeroFloats(fAudioTmpBuf, fServer.bufferSize);
  338. fLastPingTime = getCurrentTimeMilliseconds();
  339. CARLA_SAFE_ASSERT(fLastPingTime > 0);
  340. {
  341. // tell backend we're live
  342. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  343. // ready!
  344. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerReady);
  345. fShmNonRtServerControl.commitWrite();
  346. fShmNonRtServerControl.waitIfDataIsReachingLimit();
  347. }
  348. fIsReady = true;
  349. return true;
  350. }
  351. void CarlaJackAppClient::clearSharedMemory() noexcept
  352. {
  353. const CarlaMutexLocker cml(fRealtimeThreadMutex);
  354. if (fAudioPoolCopy != nullptr)
  355. {
  356. delete[] fAudioPoolCopy;
  357. fAudioPoolCopy = nullptr;
  358. }
  359. if (fAudioTmpBuf != nullptr)
  360. {
  361. delete[] fAudioTmpBuf;
  362. fAudioTmpBuf = nullptr;
  363. }
  364. if (fMidiInBuffers != nullptr)
  365. {
  366. delete[] fMidiInBuffers;
  367. fMidiInBuffers = nullptr;
  368. }
  369. if (fMidiOutBuffers != nullptr)
  370. {
  371. delete[] fMidiOutBuffers;
  372. fMidiOutBuffers = nullptr;
  373. }
  374. fShmAudioPool.clear();
  375. fShmRtClientControl.clear();
  376. fShmNonRtClientControl.clear();
  377. fShmNonRtServerControl.clear();
  378. }
  379. bool CarlaJackAppClient::handleRtData()
  380. {
  381. if (fNewClients.count() != 0)
  382. {
  383. for (LinkedList<JackClientState*>::Itenerator it = fNewClients.begin2(); it.valid(); it.next())
  384. {
  385. JackClientState* const jclient(it.getValue(nullptr));
  386. CARLA_SAFE_ASSERT_CONTINUE(jclient != nullptr);
  387. if (jclient->threadInitCb != nullptr)
  388. jclient->threadInitCb(jclient->threadInitCbPtr);
  389. }
  390. fNewClients.clear();
  391. }
  392. const BridgeRtClientControl::WaitHelper helper(fShmRtClientControl);
  393. if (! helper.ok)
  394. return false;
  395. bool ret = false;
  396. for (; fShmRtClientControl.isDataAvailableForReading();)
  397. {
  398. const PluginBridgeRtClientOpcode opcode(fShmRtClientControl.readOpcode());
  399. #ifdef DEBUG
  400. if (opcode != kPluginBridgeRtClientProcess && opcode != kPluginBridgeRtClientMidiEvent) {
  401. carla_debug("CarlaJackAppClientRtThread::run() - got opcode: %s", PluginBridgeRtClientOpcode2str(opcode));
  402. }
  403. #endif
  404. switch (opcode)
  405. {
  406. case kPluginBridgeRtClientNull:
  407. break;
  408. case kPluginBridgeRtClientSetAudioPool: {
  409. const CarlaMutexLocker cml(fRealtimeThreadMutex);
  410. if (fShmAudioPool.data != nullptr)
  411. {
  412. jackbridge_shm_unmap(fShmAudioPool.shm, fShmAudioPool.data);
  413. fShmAudioPool.data = nullptr;
  414. }
  415. if (fAudioPoolCopy != nullptr)
  416. {
  417. delete[] fAudioPoolCopy;
  418. fAudioPoolCopy = nullptr;
  419. }
  420. const uint64_t poolSize(fShmRtClientControl.readULong());
  421. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  422. fShmAudioPool.data = (float*)jackbridge_shm_map(fShmAudioPool.shm, static_cast<size_t>(poolSize));
  423. fAudioPoolCopy = new float[poolSize];
  424. break;
  425. }
  426. case kPluginBridgeRtClientSetBufferSize:
  427. if (const uint32_t newBufferSize = fShmRtClientControl.readUInt())
  428. {
  429. if (fServer.bufferSize != newBufferSize)
  430. {
  431. const CarlaMutexLocker cml(fRealtimeThreadMutex);
  432. fServer.bufferSize = newBufferSize;
  433. for (LinkedList<JackClientState*>::Itenerator it = fClients.begin2(); it.valid(); it.next())
  434. {
  435. JackClientState* const jclient(it.getValue(nullptr));
  436. CARLA_SAFE_ASSERT_CONTINUE(jclient != nullptr);
  437. if (jclient->bufferSizeCb != nullptr)
  438. jclient->bufferSizeCb(fServer.bufferSize, jclient->bufferSizeCbPtr);
  439. }
  440. delete[] fAudioTmpBuf;
  441. fAudioTmpBuf = new float[fServer.bufferSize];
  442. carla_zeroFloats(fAudioTmpBuf, fServer.bufferSize);
  443. }
  444. }
  445. break;
  446. case kPluginBridgeRtClientSetSampleRate: {
  447. const double newSampleRate = fShmRtClientControl.readDouble();
  448. if (carla_isNotZero(newSampleRate) && carla_isNotEqual(fServer.sampleRate, newSampleRate))
  449. {
  450. const CarlaMutexLocker cml(fRealtimeThreadMutex);
  451. fServer.sampleRate = newSampleRate;
  452. for (LinkedList<JackClientState*>::Itenerator it = fClients.begin2(); it.valid(); it.next())
  453. {
  454. JackClientState* const jclient(it.getValue(nullptr));
  455. CARLA_SAFE_ASSERT_CONTINUE(jclient != nullptr);
  456. if (jclient->sampleRateCb != nullptr)
  457. jclient->sampleRateCb(static_cast<uint32_t>(fServer.sampleRate), jclient->sampleRateCbPtr);
  458. }
  459. }
  460. } break;
  461. case kPluginBridgeRtClientSetOnline: {
  462. const bool offline = fShmRtClientControl.readBool();
  463. if (fIsOffline != offline)
  464. {
  465. const CarlaMutexLocker cml(fRealtimeThreadMutex);
  466. fIsOffline = offline;
  467. for (LinkedList<JackClientState*>::Itenerator it = fClients.begin2(); it.valid(); it.next())
  468. {
  469. JackClientState* const jclient(it.getValue(nullptr));
  470. CARLA_SAFE_ASSERT_CONTINUE(jclient != nullptr);
  471. if (jclient->freewheelCb != nullptr)
  472. jclient->freewheelCb(offline ? 1 : 0, jclient->freewheelCbPtr);
  473. }
  474. }
  475. } break;
  476. case kPluginBridgeRtClientControlEventParameter:
  477. case kPluginBridgeRtClientControlEventMidiBank:
  478. case kPluginBridgeRtClientControlEventMidiProgram:
  479. case kPluginBridgeRtClientControlEventAllSoundOff:
  480. case kPluginBridgeRtClientControlEventAllNotesOff:
  481. break;
  482. case kPluginBridgeRtClientMidiEvent: {
  483. const uint32_t time(fShmRtClientControl.readUInt());
  484. const uint8_t port(fShmRtClientControl.readByte());
  485. const uint8_t size(fShmRtClientControl.readByte());
  486. CARLA_SAFE_ASSERT_BREAK(size > 0);
  487. if (port >= fServer.numMidiIns || size > JackMidiPortBufferBase::kMaxEventSize || ! fRealtimeThreadMutex.tryLock())
  488. {
  489. for (uint8_t i=0; i<size; ++i)
  490. fShmRtClientControl.readByte();
  491. break;
  492. }
  493. JackMidiPortBufferOnStack& midiPortBuf(fMidiInBuffers[port]);
  494. if (midiPortBuf.count < JackMidiPortBufferBase::kMaxEventCount &&
  495. midiPortBuf.bufferPoolPos + size < JackMidiPortBufferBase::kBufferPoolSize)
  496. {
  497. jack_midi_event_t& ev(midiPortBuf.events[midiPortBuf.count++]);
  498. ev.time = time;
  499. ev.size = size;
  500. ev.buffer = midiPortBuf.bufferPool + midiPortBuf.bufferPoolPos;
  501. midiPortBuf.bufferPoolPos += size;
  502. for (uint8_t i=0; i<size; ++i)
  503. ev.buffer[i] = fShmRtClientControl.readByte();
  504. }
  505. fRealtimeThreadMutex.unlock(true);
  506. break;
  507. }
  508. case kPluginBridgeRtClientProcess: {
  509. const uint32_t frames(fShmRtClientControl.readUInt());
  510. CARLA_SAFE_ASSERT_UINT2_BREAK(frames == fServer.bufferSize, frames, fServer.bufferSize);
  511. // TODO tell client of xrun in case buffersize does not match
  512. const CarlaMutexTryLocker cmtl(fRealtimeThreadMutex, fIsOffline);
  513. if (cmtl.wasLocked())
  514. {
  515. CARLA_SAFE_ASSERT_BREAK(fShmAudioPool.data != nullptr);
  516. // mixdown is default, do buffer addition (for multiple clients) if requested
  517. const bool doBufferAddition = fSetupHints & 0x10;
  518. // location to start of audio outputs (shm buffer)
  519. float* const fdataRealOuts = fShmAudioPool.data+(fServer.bufferSize*fServer.numAudioIns);
  520. if (doBufferAddition && fServer.numAudioOuts > 0)
  521. carla_zeroFloats(fdataRealOuts, fServer.bufferSize*fServer.numAudioOuts);
  522. if (! fClients.isEmpty())
  523. {
  524. // save transport for all clients
  525. const BridgeTimeInfo& bridgeTimeInfo(fShmRtClientControl.data->timeInfo);
  526. fServer.playing = bridgeTimeInfo.playing;
  527. fServer.position.frame = static_cast<jack_nframes_t>(bridgeTimeInfo.frame);
  528. fServer.position.usecs = bridgeTimeInfo.usecs;
  529. if (bridgeTimeInfo.validFlags & kPluginBridgeTimeInfoValidBBT)
  530. {
  531. fServer.position.valid = JackPositionBBT;
  532. fServer.position.bar = bridgeTimeInfo.bar;
  533. fServer.position.beat = bridgeTimeInfo.beat;
  534. fServer.position.tick = static_cast<int32_t>(bridgeTimeInfo.tick + 0.5);
  535. fServer.position.beats_per_bar = bridgeTimeInfo.beatsPerBar;
  536. fServer.position.beat_type = bridgeTimeInfo.beatType;
  537. fServer.position.ticks_per_beat = bridgeTimeInfo.ticksPerBeat;
  538. fServer.position.beats_per_minute = bridgeTimeInfo.beatsPerMinute;
  539. fServer.position.bar_start_tick = bridgeTimeInfo.barStartTick;
  540. }
  541. else
  542. {
  543. fServer.position.valid = static_cast<jack_position_bits_t>(0x0);
  544. }
  545. int numClientOutputsProcessed = 0;
  546. // now go through each client
  547. for (LinkedList<JackClientState*>::Itenerator it = fClients.begin2(); it.valid(); it.next())
  548. {
  549. JackClientState* const jclient(it.getValue(nullptr));
  550. CARLA_SAFE_ASSERT_CONTINUE(jclient != nullptr);
  551. // FIXME - lock if offline
  552. const CarlaMutexTryLocker cmtl2(jclient->mutex);
  553. // check if we can process
  554. if (cmtl2.wasNotLocked() || jclient->processCb == nullptr || ! jclient->activated)
  555. {
  556. if (fServer.numAudioOuts > 0)
  557. carla_zeroFloats(fdataRealOuts, fServer.bufferSize*fServer.numAudioOuts);
  558. if (jclient->deactivated)
  559. fShmRtClientControl.data->procFlags = 1;
  560. }
  561. else
  562. {
  563. uint8_t i;
  564. // direct access to shm buffer, used only for inputs
  565. float* fdataReal = fShmAudioPool.data;
  566. // safe temp location for output, mixed down to shm buffer later on
  567. float* fdataCopy = fAudioPoolCopy;
  568. // wherever we're using fAudioTmpBuf
  569. bool needsTmpBufClear = false;
  570. // set audio inputs
  571. i = 0;
  572. for (LinkedList<JackPortState*>::Itenerator it2 = jclient->audioIns.begin2(); it2.valid(); it2.next())
  573. {
  574. JackPortState* const jport = it2.getValue(nullptr);
  575. CARLA_SAFE_ASSERT_CONTINUE(jport != nullptr);
  576. if (i++ < fServer.numAudioIns)
  577. {
  578. if (numClientOutputsProcessed == 0 || ! doBufferAddition)
  579. jport->buffer = fdataReal;
  580. else
  581. jport->buffer = fdataRealOuts + (i*fServer.bufferSize);
  582. fdataReal += fServer.bufferSize;
  583. fdataCopy += fServer.bufferSize;
  584. }
  585. else
  586. {
  587. jport->buffer = fAudioTmpBuf;
  588. needsTmpBufClear = true;
  589. }
  590. }
  591. if (i < fServer.numAudioIns)
  592. {
  593. const std::size_t remainingBufferSize = fServer.bufferSize * static_cast<uint8_t>(fServer.numAudioIns - i);
  594. //fdataReal += remainingBufferSize;
  595. fdataCopy += remainingBufferSize;
  596. }
  597. // location to start of audio outputs
  598. float* const fdataCopyOuts = fdataCopy;
  599. // set audio outputs
  600. i = 0;
  601. for (LinkedList<JackPortState*>::Itenerator it2 = jclient->audioOuts.begin2(); it2.valid(); it2.next())
  602. {
  603. JackPortState* const jport = it2.getValue(nullptr);
  604. CARLA_SAFE_ASSERT_CONTINUE(jport != nullptr);
  605. if (i++ < fServer.numAudioOuts)
  606. {
  607. jport->buffer = fdataCopy;
  608. fdataCopy += fServer.bufferSize;
  609. }
  610. else
  611. {
  612. jport->buffer = fAudioTmpBuf;
  613. needsTmpBufClear = true;
  614. }
  615. }
  616. if (i < fServer.numAudioOuts)
  617. {
  618. const std::size_t remainingBufferSize = fServer.bufferSize * static_cast<uint8_t>(fServer.numAudioOuts - i);
  619. carla_zeroFloats(fdataCopy, remainingBufferSize);
  620. //fdataCopy += remainingBufferSize;
  621. }
  622. // set midi inputs
  623. i = 0;
  624. for (LinkedList<JackPortState*>::Itenerator it2 = jclient->midiIns.begin2(); it2.valid(); it2.next())
  625. {
  626. JackPortState* const jport = it2.getValue(nullptr);
  627. CARLA_SAFE_ASSERT_CONTINUE(jport != nullptr);
  628. if (i++ < fServer.numMidiIns)
  629. jport->buffer = &fMidiInBuffers[i-1];
  630. else
  631. jport->buffer = &fDummyMidiInBuffer;
  632. }
  633. // set midi outputs
  634. i = 0;
  635. for (LinkedList<JackPortState*>::Itenerator it2 = jclient->midiOuts.begin2(); it2.valid(); it2.next())
  636. {
  637. JackPortState* const jport = it2.getValue(nullptr);
  638. CARLA_SAFE_ASSERT_CONTINUE(jport != nullptr);
  639. if (i++ < fServer.numMidiOuts)
  640. jport->buffer = &fMidiOutBuffers[i-1];
  641. else
  642. jport->buffer = &fDummyMidiOutBuffer;
  643. }
  644. if (needsTmpBufClear)
  645. carla_zeroFloats(fAudioTmpBuf, fServer.bufferSize);
  646. jclient->processCb(fServer.bufferSize, jclient->processCbPtr);
  647. if (fServer.numAudioOuts > 0)
  648. {
  649. if (++numClientOutputsProcessed == 1)
  650. {
  651. // first client, we can copy stuff over
  652. carla_copyFloats(fdataRealOuts, fdataCopyOuts,
  653. fServer.bufferSize*fServer.numAudioOuts);
  654. }
  655. else
  656. {
  657. // subsequent clients, add data (then divide by number of clients later on)
  658. carla_add(fdataRealOuts, fdataCopyOuts,
  659. fServer.bufferSize*fServer.numAudioOuts);
  660. if (doBufferAddition)
  661. {
  662. // for more than 1 client addition, we need to divide buffers now
  663. carla_multiply(fdataRealOuts,
  664. 1.0f/static_cast<float>(numClientOutputsProcessed),
  665. fServer.bufferSize*fServer.numAudioOuts);
  666. }
  667. }
  668. if (jclient->audioOuts.count() == 1 && fServer.numAudioOuts > 1)
  669. {
  670. for (uint8_t j=1; j<fServer.numAudioOuts; ++j)
  671. {
  672. carla_copyFloats(fdataRealOuts+(fServer.bufferSize*j),
  673. fdataCopyOuts,
  674. fServer.bufferSize);
  675. }
  676. }
  677. }
  678. }
  679. }
  680. if (numClientOutputsProcessed > 1 && ! doBufferAddition)
  681. {
  682. // more than 1 client active, need to divide buffers
  683. carla_multiply(fdataRealOuts,
  684. 1.0f/static_cast<float>(numClientOutputsProcessed),
  685. fServer.bufferSize*fServer.numAudioOuts);
  686. }
  687. }
  688. // fClients.isEmpty()
  689. else if (fServer.numAudioOuts > 0)
  690. {
  691. carla_zeroFloats(fdataRealOuts, fServer.bufferSize*fServer.numAudioOuts);
  692. }
  693. for (uint8_t i=0; i<fServer.numMidiIns; ++i)
  694. {
  695. fMidiInBuffers[i].count = 0;
  696. fMidiInBuffers[i].bufferPoolPos = 0;
  697. }
  698. if (fServer.numMidiOuts > 0)
  699. {
  700. uint8_t* midiData(fShmRtClientControl.data->midiOut);
  701. carla_zeroBytes(midiData, kBridgeBaseMidiOutHeaderSize);
  702. std::size_t curMidiDataPos = 0;
  703. for (uint8_t i=0; i<fServer.numMidiOuts; ++i)
  704. {
  705. JackMidiPortBufferOnStack& midiPortBuf(fMidiOutBuffers[i]);
  706. for (uint16_t j=0; j<midiPortBuf.count; ++j)
  707. {
  708. jack_midi_event_t& jmevent(midiPortBuf.events[j]);
  709. if (curMidiDataPos + kBridgeBaseMidiOutHeaderSize + jmevent.size >= kBridgeRtClientDataMidiOutSize)
  710. break;
  711. // set time
  712. *(uint32_t*)midiData = jmevent.time;
  713. midiData += 4;
  714. // set port
  715. *midiData++ = i;
  716. // set size
  717. *midiData++ = static_cast<uint8_t>(jmevent.size);
  718. // set data
  719. std::memcpy(midiData, jmevent.buffer, jmevent.size);
  720. midiData += jmevent.size;
  721. curMidiDataPos += kBridgeBaseMidiOutHeaderSize + jmevent.size;
  722. }
  723. }
  724. if (curMidiDataPos != 0 &&
  725. curMidiDataPos + kBridgeBaseMidiOutHeaderSize < kBridgeRtClientDataMidiOutSize)
  726. carla_zeroBytes(midiData, kBridgeBaseMidiOutHeaderSize);
  727. }
  728. }
  729. else
  730. {
  731. carla_stderr2("CarlaJackAppClient: fRealtimeThreadMutex tryLock failed");
  732. }
  733. break;
  734. }
  735. case kPluginBridgeRtClientQuit:
  736. ret = true;
  737. break;
  738. }
  739. #ifdef DEBUG
  740. if (opcode != kPluginBridgeRtClientProcess && opcode != kPluginBridgeRtClientMidiEvent) {
  741. carla_debug("CarlaJackAppClientRtThread::run() - opcode %s done", PluginBridgeRtClientOpcode2str(opcode));
  742. }
  743. #endif
  744. }
  745. return ret;
  746. }
  747. bool CarlaJackAppClient::handleNonRtData()
  748. {
  749. bool ret = false;
  750. for (; fShmNonRtClientControl.isDataAvailableForReading();)
  751. {
  752. const PluginBridgeNonRtClientOpcode opcode(fShmNonRtClientControl.readOpcode());
  753. #ifdef DEBUG
  754. if (opcode != kPluginBridgeNonRtClientPing) {
  755. carla_debug("CarlaJackAppClient::handleNonRtData() - got opcode: %s", PluginBridgeNonRtClientOpcode2str(opcode));
  756. }
  757. #endif
  758. if (opcode != kPluginBridgeNonRtClientNull && opcode != kPluginBridgeNonRtClientPingOnOff && fLastPingTime > 0)
  759. fLastPingTime = getCurrentTimeMilliseconds();
  760. switch (opcode)
  761. {
  762. case kPluginBridgeNonRtClientNull:
  763. break;
  764. case kPluginBridgeNonRtClientVersion: {
  765. const uint apiVersion = fShmNonRtServerControl.readUInt();
  766. CARLA_SAFE_ASSERT_UINT2(apiVersion == CARLA_PLUGIN_BRIDGE_API_VERSION, apiVersion, CARLA_PLUGIN_BRIDGE_API_VERSION);
  767. } break;
  768. case kPluginBridgeNonRtClientPing: {
  769. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  770. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerPong);
  771. fShmNonRtServerControl.commitWrite();
  772. } break;
  773. case kPluginBridgeNonRtClientPingOnOff: {
  774. const uint32_t onOff(fShmNonRtClientControl.readBool());
  775. fLastPingTime = onOff ? getCurrentTimeMilliseconds() : -1;
  776. } break;
  777. case kPluginBridgeNonRtClientActivate:
  778. case kPluginBridgeNonRtClientDeactivate:
  779. break;
  780. case kPluginBridgeNonRtClientInitialSetup:
  781. // should never happen!!
  782. fShmNonRtServerControl.readUInt();
  783. fShmNonRtServerControl.readDouble();
  784. break;
  785. case kPluginBridgeNonRtClientSetParameterValue:
  786. case kPluginBridgeNonRtClientSetParameterMidiChannel:
  787. case kPluginBridgeNonRtClientSetParameterMidiCC:
  788. case kPluginBridgeNonRtClientSetProgram:
  789. case kPluginBridgeNonRtClientSetMidiProgram:
  790. case kPluginBridgeNonRtClientSetCustomData:
  791. case kPluginBridgeNonRtClientSetChunkDataFile:
  792. break;
  793. case kPluginBridgeNonRtClientSetOption:
  794. fShmNonRtClientControl.readUInt();
  795. fShmNonRtClientControl.readBool();
  796. break;
  797. case kPluginBridgeNonRtClientSetCtrlChannel:
  798. fShmNonRtClientControl.readShort();
  799. break;
  800. case kPluginBridgeNonRtClientGetParameterText:
  801. fShmNonRtClientControl.readUInt();
  802. break;
  803. case kPluginBridgeNonRtClientPrepareForSave:
  804. {
  805. if (fSessionManager == LIBJACK_SESSION_MANAGER_AUTO && std::getenv("NSM_URL") == nullptr)
  806. {
  807. struct sigaction sig;
  808. carla_zeroStruct(sig);
  809. sigaction(SIGUSR1, nullptr, &sig);
  810. if (sig.sa_handler != nullptr)
  811. fSessionManager = LIBJACK_SESSION_MANAGER_LADISH;
  812. }
  813. if (fSessionManager == LIBJACK_SESSION_MANAGER_LADISH)
  814. ::kill(::getpid(), SIGUSR1);
  815. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  816. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerSaved);
  817. fShmNonRtServerControl.commitWrite();
  818. }
  819. break;
  820. case kPluginBridgeNonRtClientRestoreLV2State:
  821. break;
  822. case kPluginBridgeNonRtClientShowUI:
  823. if (jack_carla_interposed_action(LIBJACK_INTERPOSER_ACTION_SHOW_HIDE_GUI, 1, nullptr) == 1337)
  824. {
  825. // failed, LD_PRELOAD did not work?
  826. const char* const message("Cannot show UI, LD_PRELOAD not working?");
  827. const std::size_t messageSize(std::strlen(message));
  828. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  829. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerUiClosed);
  830. fShmNonRtServerControl.commitWrite();
  831. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerError);
  832. fShmNonRtServerControl.writeUInt(messageSize);
  833. fShmNonRtServerControl.writeCustomData(message, messageSize);
  834. fShmNonRtServerControl.commitWrite();
  835. }
  836. break;
  837. case kPluginBridgeNonRtClientHideUI:
  838. jack_carla_interposed_action(LIBJACK_INTERPOSER_ACTION_SHOW_HIDE_GUI, 0, nullptr);
  839. break;
  840. case kPluginBridgeNonRtClientUiParameterChange:
  841. case kPluginBridgeNonRtClientUiProgramChange:
  842. case kPluginBridgeNonRtClientUiMidiProgramChange:
  843. case kPluginBridgeNonRtClientUiNoteOn:
  844. case kPluginBridgeNonRtClientUiNoteOff:
  845. break;
  846. case kPluginBridgeNonRtClientQuit:
  847. ret = true;
  848. break;
  849. }
  850. #ifdef DEBUG
  851. if (opcode != kPluginBridgeNonRtClientPing) {
  852. carla_debug("CarlaJackAppClient::handleNonRtData() - opcode %s handled", PluginBridgeNonRtClientOpcode2str(opcode));
  853. }
  854. #endif
  855. }
  856. return ret;
  857. }
  858. void CarlaJackAppClient::runRealtimeThread()
  859. {
  860. carla_debug("CarlaJackAppClient runRealtimeThread START");
  861. #ifdef __SSE2_MATH__
  862. // Set FTZ and DAZ flags
  863. _mm_setcsr(_mm_getcsr() | 0x8040);
  864. #endif
  865. bool quitReceived = false;
  866. for (; ! fRealtimeThread.shouldThreadExit();)
  867. {
  868. if (handleRtData())
  869. {
  870. quitReceived = true;
  871. break;
  872. }
  873. }
  874. fNonRealtimeThread.signalThreadShouldExit();
  875. carla_debug("CarlaJackAppClient runRealtimeThread FINISHED");
  876. // TODO
  877. return; (void)quitReceived;
  878. }
  879. void CarlaJackAppClient::runNonRealtimeThread()
  880. {
  881. carla_debug("CarlaJackAppClient runNonRealtimeThread START");
  882. CARLA_SAFE_ASSERT_RETURN(initSharedMemmory(),);
  883. if (fServer.numMidiIns > 0)
  884. {
  885. fMidiInBuffers = new JackMidiPortBufferOnStack[fServer.numMidiIns];
  886. for (uint8_t i=0; i<fServer.numMidiIns; ++i)
  887. fMidiInBuffers[i].isInput = true;
  888. }
  889. if (fServer.numMidiOuts > 0)
  890. {
  891. fMidiOutBuffers = new JackMidiPortBufferOnStack[fServer.numMidiOuts];
  892. for (uint8_t i=0; i<fServer.numMidiOuts; ++i)
  893. fMidiOutBuffers[i].isInput = false;
  894. }
  895. fRealtimeThread.startThread(true);
  896. fLastPingTime = getCurrentTimeMilliseconds();
  897. carla_stdout("Carla Jack Client Ready!");
  898. bool quitReceived = false,
  899. timedOut = false;
  900. for (; ! fNonRealtimeThread.shouldThreadExit();)
  901. {
  902. carla_msleep(50);
  903. try {
  904. quitReceived = handleNonRtData();
  905. } CARLA_SAFE_EXCEPTION("handleNonRtData");
  906. if (quitReceived)
  907. break;
  908. /*
  909. if (fLastPingTime > 0 && getCurrentTimeMilliseconds() > fLastPingTime + 30000)
  910. {
  911. carla_stderr("Did not receive ping message from server for 30 secs, closing...");
  912. timedOut = true;
  913. fRealtimeThread.signalThreadShouldExit();
  914. break;
  915. }
  916. */
  917. }
  918. //callback(true, true, ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0.0f, nullptr);
  919. if (quitReceived)
  920. {
  921. ::kill(::getpid(), SIGTERM);
  922. }
  923. else if (timedOut)
  924. {
  925. // TODO send shutdown?
  926. carla_stderr("CarlaJackAppClient error: runNonRealtimeThread ended with time out");
  927. ::kill(::getpid(), SIGTERM);
  928. }
  929. else
  930. {
  931. bool activated;
  932. {
  933. const CarlaMutexLocker cms(fRealtimeThreadMutex);
  934. if (fClients.isEmpty())
  935. {
  936. activated = false;
  937. }
  938. else if (JackClientState* const jclient = fClients.getLast(nullptr))
  939. {
  940. const CarlaMutexLocker cms2(jclient->mutex);
  941. activated = jclient->activated;
  942. }
  943. else
  944. {
  945. activated = true;
  946. }
  947. }
  948. if (activated)
  949. {
  950. carla_stderr("CarlaJackAppClient error: runNonRealtimeThread ended while client is activated");
  951. const char* const message("Plugin bridge error, process thread has stopped");
  952. const std::size_t messageSize(std::strlen(message));
  953. const CarlaMutexLocker _cml(fShmNonRtServerControl.mutex);
  954. fShmNonRtServerControl.writeOpcode(kPluginBridgeNonRtServerError);
  955. fShmNonRtServerControl.writeUInt(messageSize);
  956. fShmNonRtServerControl.writeCustomData(message, messageSize);
  957. fShmNonRtServerControl.commitWrite();
  958. }
  959. }
  960. if (fRealtimeThread.isThreadRunning())
  961. {
  962. fRealtimeThread.signalThreadShouldExit();
  963. const CarlaMutexLocker cml(fRealtimeThreadMutex);
  964. if (fShmRtClientControl.data != nullptr)
  965. fShmRtClientControl.data->procFlags = 1;
  966. }
  967. clearSharedMemory();
  968. fRealtimeThread.stopThread(5000);
  969. carla_debug("CarlaJackAppClient runNonRealtimeThread FINISHED");
  970. }
  971. CARLA_BACKEND_END_NAMESPACE
  972. // ---------------------------------------------------------------------------------------------------------------------
  973. using CarlaBackend::CarlaJackAppClient;
  974. using CarlaBackend::JackClientState;
  975. static CarlaJackAppClient gClient;
  976. CARLA_BACKEND_START_NAMESPACE
  977. static int carla_interposed_callback(int cb_action, void* ptr)
  978. {
  979. return gClient.handleInterposerCallback(cb_action, ptr);
  980. }
  981. CARLA_BACKEND_END_NAMESPACE
  982. // ---------------------------------------------------------------------------------------------------------------------
  983. CARLA_EXPORT
  984. jack_client_t* jack_client_open(const char* client_name, jack_options_t options, jack_status_t* status, ...)
  985. {
  986. carla_debug("%s(%s, 0x%x, %p)", __FUNCTION__, client_name, options, status);
  987. if (JackClientState* const client = gClient.createClient(client_name))
  988. {
  989. if (status != nullptr)
  990. *status = static_cast<JackStatus>(0x0);
  991. return (jack_client_t*)client;
  992. }
  993. if (status != nullptr)
  994. *status = JackServerError;
  995. return nullptr;
  996. // unused
  997. (void)options;
  998. }
  999. CARLA_EXPORT
  1000. jack_client_t* jack_client_new(const char* client_name)
  1001. {
  1002. return jack_client_open(client_name, JackNullOption, nullptr);
  1003. }
  1004. CARLA_EXPORT
  1005. int jack_client_close(jack_client_t* client)
  1006. {
  1007. carla_debug("%s(%p)", __FUNCTION__, client);
  1008. JackClientState* const jclient = (JackClientState*)client;
  1009. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 1);
  1010. gClient.destroyClient(jclient);
  1011. return 0;
  1012. }
  1013. CARLA_EXPORT
  1014. int jack_activate(jack_client_t* client)
  1015. {
  1016. carla_debug("%s(%p)", __FUNCTION__, client);
  1017. JackClientState* const jclient = (JackClientState*)client;
  1018. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 1);
  1019. return gClient.activateClient(jclient) ? 0 : 1;
  1020. }
  1021. CARLA_EXPORT
  1022. int jack_deactivate(jack_client_t* client)
  1023. {
  1024. carla_debug("%s(%p)", __FUNCTION__, client);
  1025. JackClientState* const jclient = (JackClientState*)client;
  1026. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 1);
  1027. return gClient.deactivateClient(jclient) ? 0 : 1;
  1028. }
  1029. // ---------------------------------------------------------------------------------------------------------------------
  1030. CARLA_EXPORT
  1031. pthread_t jack_client_thread_id(jack_client_t* client)
  1032. {
  1033. carla_debug("%s(%p)", __FUNCTION__, client);
  1034. JackClientState* const jclient = (JackClientState*)client;
  1035. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
  1036. CarlaJackAppClient* const jackAppPtr = jclient->server.jackAppPtr;
  1037. CARLA_SAFE_ASSERT_RETURN(jackAppPtr != nullptr && jackAppPtr == &gClient, 0);
  1038. return jackAppPtr->getRealtimeThreadId();
  1039. }
  1040. // ---------------------------------------------------------------------------------------------------------------------
  1041. #include "jackbridge/JackBridge2.cpp"
  1042. #include "CarlaBridgeUtils.cpp"
  1043. // ---------------------------------------------------------------------------------------------------------------------
  1044. // TODO
  1045. CARLA_BACKEND_USE_NAMESPACE
  1046. CARLA_EXPORT
  1047. int jack_client_real_time_priority(jack_client_t* client)
  1048. {
  1049. carla_debug("%s(%p)", __FUNCTION__, client);
  1050. // code as used by water
  1051. const int minPriority = sched_get_priority_min(SCHED_RR);
  1052. const int maxPriority = sched_get_priority_max(SCHED_RR);
  1053. return ((maxPriority - minPriority) * 9) / 10 + minPriority;
  1054. // unused
  1055. (void)client;
  1056. }
  1057. int jack_client_create_thread(jack_client_t* client, pthread_t* thread, int priority,
  1058. int realtime, void *(*start_routine)(void*), void* arg)
  1059. {
  1060. carla_stderr2("%s(%p, %p, %i, %i, %p, %p)", __FUNCTION__, client, thread, priority, realtime, start_routine, arg);
  1061. return ENOSYS;
  1062. }
  1063. typedef void (*JackSessionCallback)(jack_session_event_t*, void*);
  1064. CARLA_EXPORT
  1065. int jack_set_session_callback(jack_client_t* client, JackSessionCallback callback, void* arg)
  1066. {
  1067. carla_stderr2("%s(%p, %p, %p)", __FUNCTION__, client, callback, arg);
  1068. return 0;
  1069. }
  1070. // ---------------------------------------------------------------------------------------------------------------------