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.

1306 lines
45KB

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