jack2 codebase
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.

1353 lines
41KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. Copyright (C) 2016-2023 Filipe Coelho <falktx@falktx.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include "JackSystemDeps.h"
  18. #include "JackGraphManager.h"
  19. #include "JackClientControl.h"
  20. #include "JackEngineControl.h"
  21. #include "JackGlobals.h"
  22. #include "JackChannel.h"
  23. #include "JackTransportEngine.h"
  24. #include "driver_interface.h"
  25. #include "JackLibGlobals.h"
  26. #include <math.h>
  27. #include <string>
  28. #include <algorithm>
  29. #include <climits>
  30. using namespace std;
  31. namespace Jack
  32. {
  33. #define IsRealTime() ((fProcess != NULL) | (fThreadFun != NULL) | (fSync != NULL) | (fTimebase != NULL))
  34. JackClient::JackClient(JackSynchro* table):fThread(this)
  35. {
  36. fSynchroTable = table;
  37. fProcess = NULL;
  38. fGraphOrder = NULL;
  39. fXrun = NULL;
  40. fShutdown = NULL;
  41. fInfoShutdown = NULL;
  42. fInit = NULL;
  43. fBufferSize = NULL;
  44. fClientRegistration = NULL;
  45. fFreewheel = NULL;
  46. fPortRegistration = NULL;
  47. fPortConnect = NULL;
  48. fPortRename = NULL;
  49. fTimebase = NULL;
  50. fSync = NULL;
  51. fThreadFun = NULL;
  52. fSession = NULL;
  53. fLatency = NULL;
  54. fPropertyChange = NULL;
  55. fProcessArg = NULL;
  56. fGraphOrderArg = NULL;
  57. fXrunArg = NULL;
  58. fShutdownArg = NULL;
  59. fInfoShutdownArg = NULL;
  60. fInitArg = NULL;
  61. fBufferSizeArg = NULL;
  62. fFreewheelArg = NULL;
  63. fClientRegistrationArg = NULL;
  64. fPortRegistrationArg = NULL;
  65. fPortConnectArg = NULL;
  66. fPortRenameArg = NULL;
  67. fSyncArg = NULL;
  68. fTimebaseArg = NULL;
  69. fThreadFunArg = NULL;
  70. fSessionArg = NULL;
  71. fLatencyArg = NULL;
  72. fPropertyChangeArg = NULL;
  73. fSessionReply = kPendingSessionReply;
  74. }
  75. JackClient::~JackClient()
  76. {}
  77. void JackClient::ShutDown(jack_status_t code, const char* message)
  78. {
  79. jack_log("JackClient::ShutDown");
  80. // If "fInfoShutdown" callback, then call it
  81. if (fInfoShutdown) {
  82. fInfoShutdown(code, message, fInfoShutdownArg);
  83. fInfoShutdown = NULL;
  84. // Otherwise possibly call the normal "fShutdown"
  85. } else if (fShutdown) {
  86. fShutdown(fShutdownArg);
  87. fShutdown = NULL;
  88. }
  89. }
  90. int JackClient::Close()
  91. {
  92. jack_log("JackClient::Close ref = %ld", GetClientControl()->fRefNum);
  93. int result = 0;
  94. Deactivate();
  95. // Channels is stopped first to avoid receiving notifications while closing
  96. fChannel->Stop();
  97. // Then close client
  98. fChannel->ClientClose(GetClientControl()->fRefNum, &result);
  99. fChannel->Close();
  100. assert(JackGlobals::fSynchroMutex);
  101. JackGlobals::fSynchroMutex->Lock();
  102. fSynchroTable[GetClientControl()->fRefNum].Disconnect();
  103. JackGlobals::fSynchroMutex->Unlock();
  104. JackGlobals::fClientTable[GetClientControl()->fRefNum] = NULL;
  105. return result;
  106. }
  107. bool JackClient::IsActive()
  108. {
  109. return (GetClientControl()) ? GetClientControl()->fActive : false;
  110. }
  111. jack_native_thread_t JackClient::GetThreadID()
  112. {
  113. return fThread.GetThreadID();
  114. }
  115. /*!
  116. In "async" mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
  117. The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
  118. Drivers synchro are setup in "flush" mode if server is "async" and NOT freewheel.
  119. */
  120. void JackClient::SetupDriverSync(bool freewheel)
  121. {
  122. if (!freewheel && !GetEngineControl()->fSyncMode) {
  123. jack_log("JackClient::SetupDriverSync driver sem in flush mode");
  124. for (int i = 0; i < GetEngineControl()->fDriverNum; i++) {
  125. fSynchroTable[i].SetFlush(true);
  126. }
  127. } else {
  128. jack_log("JackClient::SetupDriverSync driver sem in normal mode");
  129. for (int i = 0; i < GetEngineControl()->fDriverNum; i++) {
  130. fSynchroTable[i].SetFlush(false);
  131. }
  132. }
  133. }
  134. /*!
  135. \brief Notification received from the server.
  136. */
  137. int JackClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  138. {
  139. return 0;
  140. }
  141. int JackClient::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  142. {
  143. int res = 0;
  144. jack_log("JackClient::ClientNotify ref = %ld name = %s notify = %ld", refnum, name, notify);
  145. // Done all time: redirected on subclass implementation JackLibClient and JackInternalClient
  146. switch (notify) {
  147. case kAddClient:
  148. res = ClientNotifyImp(refnum, name, notify, sync, message, value1, value2);
  149. break;
  150. case kRemoveClient:
  151. res = ClientNotifyImp(refnum, name, notify, sync, message, value1, value2);
  152. break;
  153. case kActivateClient:
  154. jack_log("JackClient::kActivateClient name = %s ref = %ld ", name, refnum);
  155. InitAux();
  156. break;
  157. }
  158. /*
  159. The current semantic is that notifications can only be received when the client has been activated,
  160. although is this implementation, one could imagine calling notifications as soon as the client has be opened.
  161. */
  162. if (IsActive()) {
  163. switch (notify) {
  164. case kAddClient:
  165. jack_log("JackClient::kAddClient fName = %s name = %s", GetClientControl()->fName, name);
  166. if (fClientRegistration && strcmp(GetClientControl()->fName, name) != 0) { // Don't call the callback for the registering client itself
  167. fClientRegistration(name, 1, fClientRegistrationArg);
  168. }
  169. break;
  170. case kRemoveClient:
  171. jack_log("JackClient::kRemoveClient fName = %s name = %s", GetClientControl()->fName, name);
  172. if (fClientRegistration && strcmp(GetClientControl()->fName, name) != 0) { // Don't call the callback for the registering client itself
  173. fClientRegistration(name, 0, fClientRegistrationArg);
  174. }
  175. break;
  176. case kBufferSizeCallback:
  177. jack_log("JackClient::kBufferSizeCallback buffer_size = %ld", value1);
  178. if (fBufferSize) {
  179. res = fBufferSize(value1, fBufferSizeArg);
  180. }
  181. break;
  182. case kSampleRateCallback:
  183. jack_log("JackClient::kSampleRateCallback sample_rate = %ld", value1);
  184. if (fSampleRate) {
  185. res = fSampleRate(value1, fSampleRateArg);
  186. }
  187. break;
  188. case kGraphOrderCallback:
  189. jack_log("JackClient::kGraphOrderCallback");
  190. if (fGraphOrder) {
  191. res = fGraphOrder(fGraphOrderArg);
  192. }
  193. break;
  194. case kStartFreewheelCallback:
  195. jack_log("JackClient::kStartFreewheel");
  196. SetupDriverSync(true);
  197. // Drop RT only when the RT thread is actually running
  198. if (fThread.GetStatus() == JackThread::kRunning) {
  199. fThread.DropRealTime();
  200. }
  201. if (fFreewheel) {
  202. fFreewheel(1, fFreewheelArg);
  203. }
  204. break;
  205. case kStopFreewheelCallback:
  206. jack_log("JackClient::kStopFreewheel");
  207. SetupDriverSync(false);
  208. if (fFreewheel) {
  209. fFreewheel(0, fFreewheelArg);
  210. }
  211. // Acquire RT only when the RT thread is actually running
  212. if (GetEngineControl()->fRealTime && fThread.GetStatus() == JackThread::kRunning) {
  213. if (fThread.AcquireRealTime(GetEngineControl()->fClientPriority) < 0) {
  214. jack_error("JackClient::AcquireRealTime error");
  215. }
  216. }
  217. break;
  218. case kPortRegistrationOnCallback:
  219. jack_log("JackClient::kPortRegistrationOn port_index = %ld", value1);
  220. if (fPortRegistration) {
  221. fPortRegistration(value1, 1, fPortRegistrationArg);
  222. }
  223. break;
  224. case kPortRegistrationOffCallback:
  225. jack_log("JackClient::kPortRegistrationOff port_index = %ld ", value1);
  226. if (fPortRegistration) {
  227. fPortRegistration(value1, 0, fPortRegistrationArg);
  228. }
  229. break;
  230. case kPortConnectCallback:
  231. jack_log("JackClient::kPortConnectCallback src = %ld dst = %ld", value1, value2);
  232. if (fPortConnect) {
  233. fPortConnect(value1, value2, 1, fPortConnectArg);
  234. }
  235. break;
  236. case kPortDisconnectCallback:
  237. jack_log("JackClient::kPortDisconnectCallback src = %ld dst = %ld", value1, value2);
  238. if (fPortConnect) {
  239. fPortConnect(value1, value2, 0, fPortConnectArg);
  240. }
  241. break;
  242. case kPortRenameCallback:
  243. jack_log("JackClient::kPortRenameCallback port = %ld", value1);
  244. if (fPortRename) {
  245. fPortRename(value1, message, GetGraphManager()->GetPort(value1)->GetName(), fPortRenameArg);
  246. }
  247. break;
  248. case kXRunCallback:
  249. jack_log("JackClient::kXRunCallback");
  250. if (fXrun) {
  251. res = fXrun(fXrunArg);
  252. }
  253. break;
  254. case kShutDownCallback:
  255. jack_log("JackClient::kShutDownCallback");
  256. ShutDown(jack_status_t(value1), message);
  257. break;
  258. case kSessionCallback:
  259. jack_log("JackClient::kSessionCallback");
  260. if (fSession) {
  261. jack_session_event_t* event = (jack_session_event_t*)malloc( sizeof(jack_session_event_t));
  262. char uuid_buf[JACK_UUID_STRING_SIZE];
  263. event->type = (jack_session_event_type_t)value1;
  264. event->session_dir = strdup(message);
  265. event->command_line = NULL;
  266. event->flags = (jack_session_flags_t)0;
  267. jack_uuid_unparse(GetClientControl()->fSessionID, uuid_buf);
  268. event->client_uuid = strdup(uuid_buf);
  269. fSessionReply = kPendingSessionReply;
  270. // Session callback may change fSessionReply by directly using jack_session_reply
  271. fSession(event, fSessionArg);
  272. res = fSessionReply;
  273. }
  274. break;
  275. case kLatencyCallback:
  276. res = HandleLatencyCallback(value1);
  277. break;
  278. case kPropertyChangeCallback: {
  279. jack_uuid_t subject;
  280. jack_uuid_parse(name, &subject);
  281. const char* key = message;
  282. jack_property_change_t change = (jack_property_change_t)value1;
  283. jack_log("JackClient::kPropertyChangeCallback subject = %x key = %s change = %x", subject, key, change);
  284. if (fPropertyChange)
  285. fPropertyChange(subject, key, change, fPropertyChangeArg);
  286. break;
  287. }
  288. }
  289. }
  290. return res;
  291. }
  292. int JackClient::HandleLatencyCallback(int status)
  293. {
  294. jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
  295. jack_latency_range_t latency = { UINT32_MAX, 0 };
  296. /* first setup all latency values of the ports.
  297. * this is based on the connections of the ports.
  298. */
  299. list<jack_port_id_t>::iterator it;
  300. for (it = fPortList.begin(); it != fPortList.end(); it++) {
  301. JackPort* port = GetGraphManager()->GetPort(*it);
  302. if ((port->GetFlags() & JackPortIsOutput) && (mode == JackPlaybackLatency)) {
  303. GetGraphManager()->RecalculateLatency(*it, mode);
  304. }
  305. if ((port->GetFlags() & JackPortIsInput) && (mode == JackCaptureLatency)) {
  306. GetGraphManager()->RecalculateLatency(*it, mode);
  307. }
  308. }
  309. if (!fLatency) {
  310. /*
  311. * default action is to assume all ports depend on each other.
  312. * then always take the maximum latency.
  313. */
  314. if (mode == JackPlaybackLatency) {
  315. /* iterate over all OutputPorts, to find maximum playback latency
  316. */
  317. for (it = fPortList.begin(); it != fPortList.end(); it++) {
  318. JackPort* port = GetGraphManager()->GetPort(*it);
  319. if (port->GetFlags() & JackPortIsOutput) {
  320. jack_latency_range_t other_latency;
  321. port->GetLatencyRange(mode, &other_latency);
  322. if (other_latency.max > latency.max) {
  323. latency.max = other_latency.max;
  324. }
  325. if (other_latency.min < latency.min) {
  326. latency.min = other_latency.min;
  327. }
  328. }
  329. }
  330. if (latency.min == UINT32_MAX) {
  331. latency.min = 0;
  332. }
  333. /* now set the found latency on all input ports
  334. */
  335. for (it = fPortList.begin(); it != fPortList.end(); it++) {
  336. JackPort* port = GetGraphManager()->GetPort(*it);
  337. if (port->GetFlags() & JackPortIsInput) {
  338. port->SetLatencyRange(mode, &latency);
  339. }
  340. }
  341. }
  342. if (mode == JackCaptureLatency) {
  343. /* iterate over all InputPorts, to find maximum playback latency
  344. */
  345. for (it = fPortList.begin(); it != fPortList.end(); it++) {
  346. JackPort* port = GetGraphManager()->GetPort(*it);
  347. if (port->GetFlags() & JackPortIsInput) {
  348. jack_latency_range_t other_latency;
  349. port->GetLatencyRange(mode, &other_latency);
  350. if (other_latency.max > latency.max) {
  351. latency.max = other_latency.max;
  352. }
  353. if (other_latency.min < latency.min) {
  354. latency.min = other_latency.min;
  355. }
  356. }
  357. }
  358. if (latency.min == UINT32_MAX) {
  359. latency.min = 0;
  360. }
  361. /* now set the found latency on all output ports
  362. */
  363. for (it = fPortList.begin(); it != fPortList.end(); it++) {
  364. JackPort* port = GetGraphManager()->GetPort(*it);
  365. if (port->GetFlags() & JackPortIsOutput) {
  366. port->SetLatencyRange(mode, &latency);
  367. }
  368. }
  369. }
  370. return 0;
  371. }
  372. /* we have a latency callback setup by the client,
  373. * lets use it...
  374. */
  375. fLatency(mode, fLatencyArg);
  376. return 0;
  377. }
  378. /*!
  379. \brief We need to start thread before activating in the server, otherwise the FW driver
  380. connected to the client may not be activated.
  381. */
  382. int JackClient::Activate()
  383. {
  384. jack_log("JackClient::Activate");
  385. if (IsActive()) {
  386. return 0;
  387. }
  388. // RT thread is started only when needed...
  389. if (IsRealTime()) {
  390. if (StartThread() < 0) {
  391. return -1;
  392. }
  393. }
  394. /*
  395. Insertion of client in the graph will cause a kGraphOrderCallback notification
  396. to be delivered by the server, the client wants to receive it.
  397. */
  398. GetClientControl()->fActive = true;
  399. // Transport related callback become "active"
  400. GetClientControl()->fTransportSync = true;
  401. GetClientControl()->fTransportTimebase = true;
  402. int result = -1;
  403. GetClientControl()->fCallback[kRealTimeCallback] = IsRealTime();
  404. fChannel->ClientActivate(GetClientControl()->fRefNum, IsRealTime(), &result);
  405. return result;
  406. }
  407. /*!
  408. \brief Need to stop thread after deactivating in the server.
  409. */
  410. int JackClient::Deactivate()
  411. {
  412. jack_log("JackClient::Deactivate");
  413. if (!IsActive()) {
  414. return 0;
  415. }
  416. GetClientControl()->fActive = false;
  417. // Transport related callback become "unactive"
  418. GetClientControl()->fTransportSync = false;
  419. GetClientControl()->fTransportTimebase = false;
  420. // We need to wait for the new engine cycle before stopping the RT thread, but this is done by ClientDeactivate
  421. int result = -1;
  422. fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
  423. jack_log("JackClient::Deactivate res = %ld", result);
  424. // RT thread is stopped only when needed...
  425. if (IsRealTime()) {
  426. fThread.Kill();
  427. }
  428. return result;
  429. }
  430. //----------------------
  431. // RT thread management
  432. //----------------------
  433. void JackClient::InitAux()
  434. {
  435. if (fInit) {
  436. jack_log("JackClient::Init calling client thread init callback");
  437. fInit(fInitArg);
  438. }
  439. }
  440. /*!
  441. \brief Called once when the thread starts.
  442. */
  443. bool JackClient::Init()
  444. {
  445. /*
  446. Execute buffer_size callback.
  447. Since StartThread uses fThread.StartSync, we are sure that buffer_size callback
  448. is executed before StartThread returns (and then IsActive will be true).
  449. So no RT callback can be called at the same time.
  450. */
  451. jack_log("JackClient::kBufferSizeCallback buffer_size = %ld", GetEngineControl()->fBufferSize);
  452. if (fBufferSize) {
  453. fBufferSize(GetEngineControl()->fBufferSize, fBufferSizeArg);
  454. }
  455. // Init callback
  456. InitAux();
  457. // Setup context
  458. if (!jack_tls_set(JackGlobals::fRealTimeThread, this)) {
  459. jack_error("Failed to set thread realtime key");
  460. }
  461. // Setup RT
  462. if (GetEngineControl()->fRealTime) {
  463. set_threaded_log_function();
  464. SetupRealTime();
  465. }
  466. return true;
  467. }
  468. void JackClient::SetupRealTime()
  469. {
  470. jack_log("JackClient::Init : period = %ld computation = %ld constraint = %ld",
  471. long(int64_t(GetEngineControl()->fPeriod) / 1000.0f),
  472. long(int64_t(GetEngineControl()->fComputation) / 1000.0f),
  473. long(int64_t(GetEngineControl()->fConstraint) / 1000.0f));
  474. // Will do "something" on OSX only...
  475. fThread.SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
  476. if (fThread.AcquireSelfRealTime(GetEngineControl()->fClientPriority) < 0) {
  477. jack_error("JackClient::AcquireSelfRealTime error");
  478. }
  479. }
  480. int JackClient::StartThread()
  481. {
  482. if (fThread.StartSync() < 0) {
  483. jack_error("Start thread error");
  484. return -1;
  485. }
  486. return 0;
  487. }
  488. /*!
  489. \brief RT thread.
  490. */
  491. bool JackClient::Execute()
  492. {
  493. // Execute a dummy cycle to be sure thread has the correct properties
  494. DummyCycle();
  495. if (fThreadFun) {
  496. fThreadFun(fThreadFunArg);
  497. } else {
  498. ExecuteThread();
  499. }
  500. return false;
  501. }
  502. void JackClient::DummyCycle()
  503. {
  504. WaitSync();
  505. SignalSync();
  506. }
  507. inline void JackClient::ExecuteThread()
  508. {
  509. while (true) {
  510. CycleWaitAux();
  511. CycleSignalAux(CallProcessCallback());
  512. }
  513. }
  514. inline jack_nframes_t JackClient::CycleWaitAux()
  515. {
  516. if (!WaitSync()) {
  517. Error(); // Terminates the thread
  518. }
  519. CallSyncCallbackAux();
  520. return GetEngineControl()->fBufferSize;
  521. }
  522. inline void JackClient::CycleSignalAux(int status)
  523. {
  524. if (status == 0) {
  525. CallTimebaseCallbackAux();
  526. }
  527. SignalSync();
  528. if (status != 0) {
  529. End(); // Terminates the thread
  530. }
  531. }
  532. jack_nframes_t JackClient::CycleWait()
  533. {
  534. return CycleWaitAux();
  535. }
  536. void JackClient::CycleSignal(int status)
  537. {
  538. CycleSignalAux(status);
  539. }
  540. inline int JackClient::CallProcessCallback()
  541. {
  542. return (fProcess != NULL) ? fProcess(GetEngineControl()->fBufferSize, fProcessArg) : 0;
  543. }
  544. inline bool JackClient::WaitSync()
  545. {
  546. // Suspend itself: wait on the input synchro
  547. if (GetGraphManager()->SuspendRefNum(GetClientControl(), fSynchroTable, LONG_MAX) < 0) {
  548. jack_error("SuspendRefNum error");
  549. return false;
  550. } else {
  551. return true;
  552. }
  553. }
  554. inline void JackClient::SignalSync()
  555. {
  556. // Resume: signal output clients connected to the running client
  557. if (GetGraphManager()->ResumeRefNum(GetClientControl(), fSynchroTable) < 0) {
  558. jack_error("ResumeRefNum error");
  559. }
  560. }
  561. inline void JackClient::End()
  562. {
  563. jack_log("JackClient::Execute end name = %s", GetClientControl()->fName);
  564. // Hum... not sure about this, the following "close" code is called in the RT thread...
  565. int result;
  566. fThread.DropSelfRealTime();
  567. GetClientControl()->fActive = false;
  568. fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
  569. fThread.Terminate();
  570. }
  571. inline void JackClient::Error()
  572. {
  573. jack_error("JackClient::Execute error name = %s", GetClientControl()->fName);
  574. // Hum... not sure about this, the following "close" code is called in the RT thread...
  575. int result;
  576. fThread.DropSelfRealTime();
  577. GetClientControl()->fActive = false;
  578. fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
  579. ShutDown(jack_status_t(JackFailure | JackServerError), JACK_SERVER_FAILURE);
  580. fThread.Terminate();
  581. }
  582. //-----------------
  583. // Port management
  584. //-----------------
  585. int JackClient::PortRegister(const char* port_name, const char* port_type, unsigned long flags, unsigned long buffer_size)
  586. {
  587. // Check if port name is empty
  588. string port_short_name_str = string(port_name);
  589. if (port_short_name_str.size() == 0) {
  590. jack_error("port_name is empty");
  591. return 0; // Means failure here...
  592. }
  593. // Check port name length
  594. string port_full_name_str = string(GetClientControl()->fName) + string(":") + port_short_name_str;
  595. if (port_full_name_str.size() >= REAL_JACK_PORT_NAME_SIZE) {
  596. jack_error("\"%s:%s\" is too long to be used as a JACK port name.\n"
  597. "Please use %lu characters or less",
  598. GetClientControl()->fName,
  599. port_name,
  600. JACK_PORT_NAME_SIZE - 1);
  601. return 0; // Means failure here...
  602. }
  603. int result = -1;
  604. jack_port_id_t port_index = NO_PORT;
  605. fChannel->PortRegister(GetClientControl()->fRefNum, port_full_name_str.c_str(), port_type, flags, buffer_size, &port_index, &result);
  606. if (result == 0) {
  607. jack_log("JackClient::PortRegister ref = %ld name = %s type = %s port_index = %ld", GetClientControl()->fRefNum, port_full_name_str.c_str(), port_type, port_index);
  608. fPortList.push_back(port_index);
  609. return port_index;
  610. } else {
  611. return 0;
  612. }
  613. }
  614. int JackClient::PortUnRegister(jack_port_id_t port_index)
  615. {
  616. jack_log("JackClient::PortUnRegister port_index = %ld", port_index);
  617. list<jack_port_id_t>::iterator it = find(fPortList.begin(), fPortList.end(), port_index);
  618. if (it != fPortList.end()) {
  619. fPortList.erase(it);
  620. int result = -1;
  621. fChannel->PortUnRegister(GetClientControl()->fRefNum, port_index, &result);
  622. return result;
  623. } else {
  624. jack_error("unregistering a port %ld that is not own by the client", port_index);
  625. return -1;
  626. }
  627. }
  628. int JackClient::PortConnect(const char* src, const char* dst)
  629. {
  630. jack_log("JackClient::Connect src = %s dst = %s", src, dst);
  631. if (strlen(src) >= REAL_JACK_PORT_NAME_SIZE) {
  632. jack_error("\"%s\" is too long to be used as a JACK port name.\n", src);
  633. return -1;
  634. }
  635. if (strlen(dst) >= REAL_JACK_PORT_NAME_SIZE) {
  636. jack_error("\"%s\" is too long to be used as a JACK port name.\n", dst);
  637. return -1;
  638. }
  639. int result = -1;
  640. fChannel->PortConnect(GetClientControl()->fRefNum, src, dst, &result);
  641. return result;
  642. }
  643. int JackClient::PortDisconnect(const char* src, const char* dst)
  644. {
  645. jack_log("JackClient::Disconnect src = %s dst = %s", src, dst);
  646. if (strlen(src) >= REAL_JACK_PORT_NAME_SIZE) {
  647. jack_error("\"%s\" is too long to be used as a JACK port name.\n", src);
  648. return -1;
  649. }
  650. if (strlen(dst) >= REAL_JACK_PORT_NAME_SIZE) {
  651. jack_error("\"%s\" is too long to be used as a JACK port name.\n", dst);
  652. return -1;
  653. }
  654. int result = -1;
  655. fChannel->PortDisconnect(GetClientControl()->fRefNum, src, dst, &result);
  656. return result;
  657. }
  658. int JackClient::PortDisconnect(jack_port_id_t src)
  659. {
  660. jack_log("JackClient::PortDisconnect src = %ld", src);
  661. int result = -1;
  662. fChannel->PortDisconnect(GetClientControl()->fRefNum, src, ALL_PORTS, &result);
  663. return result;
  664. }
  665. int JackClient::PortIsMine(jack_port_id_t port_index)
  666. {
  667. JackPort* port = GetGraphManager()->GetPort(port_index);
  668. return GetClientControl()->fRefNum == port->GetRefNum();
  669. }
  670. int JackClient::PortRename(jack_port_id_t port_index, const char* name)
  671. {
  672. int result = -1;
  673. fChannel->PortRename(GetClientControl()->fRefNum, port_index, name, &result);
  674. return result;
  675. }
  676. //--------------------
  677. // Context management
  678. //--------------------
  679. int JackClient::SetBufferSize(jack_nframes_t buffer_size)
  680. {
  681. int result = -1;
  682. fChannel->SetBufferSize(buffer_size, &result);
  683. return result;
  684. }
  685. int JackClient::SetFreeWheel(int onoff)
  686. {
  687. int result = -1;
  688. fChannel->SetFreewheel(onoff, &result);
  689. return result;
  690. }
  691. int JackClient::ComputeTotalLatencies()
  692. {
  693. int result = -1;
  694. fChannel->ComputeTotalLatencies(&result);
  695. return result;
  696. }
  697. //----------------------
  698. // Transport management
  699. //----------------------
  700. inline int JackClient::ActivateAux()
  701. {
  702. // If activated without RT thread...
  703. if (IsActive() && fThread.GetStatus() != JackThread::kRunning) {
  704. jack_log("JackClient::ActivateAux");
  705. // RT thread is started
  706. if (StartThread() < 0) {
  707. return -1;
  708. }
  709. int result = -1;
  710. GetClientControl()->fCallback[kRealTimeCallback] = IsRealTime();
  711. fChannel->ClientActivate(GetClientControl()->fRefNum, IsRealTime(), &result);
  712. return result;
  713. } else {
  714. return 0;
  715. }
  716. }
  717. int JackClient::ReleaseTimebase()
  718. {
  719. int result = -1;
  720. fChannel->ReleaseTimebase(GetClientControl()->fRefNum, &result);
  721. if (result == 0) {
  722. GetClientControl()->fTransportTimebase = false;
  723. fTimebase = NULL;
  724. fTimebaseArg = NULL;
  725. }
  726. return result;
  727. }
  728. /* Call the server if the client is active, otherwise keeps the arguments */
  729. int JackClient::SetSyncCallback(JackSyncCallback sync_callback, void* arg)
  730. {
  731. GetClientControl()->fTransportSync = (fSync != NULL);
  732. fSyncArg = arg;
  733. fSync = sync_callback;
  734. return ActivateAux();
  735. }
  736. int JackClient::SetTimebaseCallback(int conditional, JackTimebaseCallback timebase_callback, void* arg)
  737. {
  738. int result = -1;
  739. fChannel->SetTimebaseCallback(GetClientControl()->fRefNum, conditional, &result);
  740. if (result == 0) {
  741. GetClientControl()->fTransportTimebase = true;
  742. fTimebase = timebase_callback;
  743. fTimebaseArg = arg;
  744. return ActivateAux();
  745. } else {
  746. fTimebase = NULL;
  747. fTimebaseArg = NULL;
  748. return result;
  749. }
  750. }
  751. int JackClient::SetSyncTimeout(jack_time_t timeout)
  752. {
  753. GetEngineControl()->fTransport.SetSyncTimeout(timeout);
  754. return 0;
  755. }
  756. // Must be RT safe
  757. void JackClient::TransportLocate(jack_nframes_t frame)
  758. {
  759. jack_position_t pos;
  760. pos.frame = frame;
  761. pos.valid = (jack_position_bits_t)0;
  762. jack_log("JackClient::TransportLocate pos = %ld", pos.frame);
  763. GetEngineControl()->fTransport.RequestNewPos(&pos);
  764. }
  765. int JackClient::TransportReposition(const jack_position_t* pos)
  766. {
  767. jack_position_t tmp = *pos;
  768. jack_log("JackClient::TransportReposition pos = %ld", pos->frame);
  769. if (tmp.valid & ~JACK_POSITION_MASK) {
  770. return EINVAL;
  771. } else {
  772. GetEngineControl()->fTransport.RequestNewPos(&tmp);
  773. return 0;
  774. }
  775. }
  776. jack_transport_state_t JackClient::TransportQuery(jack_position_t* pos)
  777. {
  778. return GetEngineControl()->fTransport.Query(pos);
  779. }
  780. jack_nframes_t JackClient::GetCurrentTransportFrame()
  781. {
  782. return GetEngineControl()->fTransport.GetCurrentFrame();
  783. }
  784. // Must be RT safe: directly write in the transport shared mem
  785. void JackClient::TransportStart()
  786. {
  787. GetEngineControl()->fTransport.SetCommand(TransportCommandStart);
  788. }
  789. // Must be RT safe: directly write in the transport shared mem
  790. void JackClient::TransportStop()
  791. {
  792. GetEngineControl()->fTransport.SetCommand(TransportCommandStop);
  793. }
  794. // Never called concurrently with the server
  795. // TODO check concurrency with SetSyncCallback
  796. void JackClient::CallSyncCallback()
  797. {
  798. CallSyncCallbackAux();
  799. }
  800. inline void JackClient::CallSyncCallbackAux()
  801. {
  802. if (GetClientControl()->fTransportSync) {
  803. JackTransportEngine& transport = GetEngineControl()->fTransport;
  804. jack_position_t* cur_pos = transport.ReadCurrentState();
  805. jack_transport_state_t transport_state = transport.GetState();
  806. if (fSync != NULL) {
  807. if (fSync(transport_state, cur_pos, fSyncArg)) {
  808. GetClientControl()->fTransportState = JackTransportRolling;
  809. GetClientControl()->fTransportSync = false;
  810. }
  811. } else {
  812. GetClientControl()->fTransportState = JackTransportRolling;
  813. GetClientControl()->fTransportSync = false;
  814. }
  815. }
  816. }
  817. void JackClient::CallTimebaseCallback()
  818. {
  819. CallTimebaseCallbackAux();
  820. }
  821. inline void JackClient::CallTimebaseCallbackAux()
  822. {
  823. JackTransportEngine& transport = GetEngineControl()->fTransport;
  824. int master;
  825. bool unused;
  826. transport.GetTimebaseMaster(master, unused);
  827. if (GetClientControl()->fRefNum == master && fTimebase) { // Client *is* timebase...
  828. jack_transport_state_t transport_state = transport.GetState();
  829. jack_position_t* cur_pos = transport.WriteNextStateStart(1);
  830. if (GetClientControl()->fTransportTimebase) {
  831. fTimebase(transport_state, GetEngineControl()->fBufferSize, cur_pos, true, fTimebaseArg);
  832. GetClientControl()->fTransportTimebase = false; // Callback is called only once with "new_pos" = true
  833. } else if (transport_state == JackTransportRolling) {
  834. fTimebase(transport_state, GetEngineControl()->fBufferSize, cur_pos, false, fTimebaseArg);
  835. }
  836. transport.WriteNextStateStop(1);
  837. }
  838. }
  839. //---------------------
  840. // Callback management
  841. //---------------------
  842. void JackClient::OnShutdown(JackShutdownCallback callback, void *arg)
  843. {
  844. if (IsActive()) {
  845. jack_error("You cannot set callbacks on an active client");
  846. } else {
  847. // Shutdown callback will either be an old API version or the new version (with info)
  848. GetClientControl()->fCallback[kShutDownCallback] = (callback != NULL);
  849. fShutdownArg = arg;
  850. fShutdown = callback;
  851. }
  852. }
  853. void JackClient::OnInfoShutdown(JackInfoShutdownCallback callback, void *arg)
  854. {
  855. if (IsActive()) {
  856. jack_error("You cannot set callbacks on an active client");
  857. } else {
  858. // Shutdown callback will either be an old API version or the new version (with info)
  859. GetClientControl()->fCallback[kShutDownCallback] = (callback != NULL);
  860. fInfoShutdownArg = arg;
  861. fInfoShutdown = callback;
  862. }
  863. }
  864. int JackClient::SetProcessCallback(JackProcessCallback callback, void *arg)
  865. {
  866. if (IsActive()) {
  867. jack_error("You cannot set callbacks on an active client");
  868. return -1;
  869. } else if (fThreadFun) {
  870. jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
  871. return -1;
  872. } else {
  873. fProcessArg = arg;
  874. fProcess = callback;
  875. return 0;
  876. }
  877. }
  878. int JackClient::SetXRunCallback(JackXRunCallback callback, void *arg)
  879. {
  880. if (IsActive()) {
  881. jack_error("You cannot set callbacks on an active client");
  882. return -1;
  883. } else {
  884. GetClientControl()->fCallback[kXRunCallback] = (callback != NULL);
  885. fXrunArg = arg;
  886. fXrun = callback;
  887. return 0;
  888. }
  889. }
  890. int JackClient::SetInitCallback(JackThreadInitCallback callback, void *arg)
  891. {
  892. if (IsActive()) {
  893. jack_error("You cannot set callbacks on an active client");
  894. return -1;
  895. } else {
  896. fInitArg = arg;
  897. fInit = callback;
  898. /* make sure that the message buffer thread is initialized too */
  899. return JackMessageBuffer::fInstance->SetInitCallback(callback, arg);
  900. }
  901. }
  902. int JackClient::SetGraphOrderCallback(JackGraphOrderCallback callback, void *arg)
  903. {
  904. if (IsActive()) {
  905. jack_error("You cannot set callbacks on an active client");
  906. return -1;
  907. } else {
  908. GetClientControl()->fCallback[kGraphOrderCallback] = (callback != NULL);
  909. fGraphOrder = callback;
  910. fGraphOrderArg = arg;
  911. return 0;
  912. }
  913. }
  914. int JackClient::SetBufferSizeCallback(JackBufferSizeCallback callback, void *arg)
  915. {
  916. if (IsActive()) {
  917. jack_error("You cannot set callbacks on an active client");
  918. return -1;
  919. } else {
  920. GetClientControl()->fCallback[kBufferSizeCallback] = (callback != NULL);
  921. fBufferSizeArg = arg;
  922. fBufferSize = callback;
  923. return 0;
  924. }
  925. }
  926. int JackClient::SetSampleRateCallback(JackSampleRateCallback callback, void *arg)
  927. {
  928. if (IsActive()) {
  929. jack_error("You cannot set callbacks on an active client");
  930. return -1;
  931. } else {
  932. GetClientControl()->fCallback[kSampleRateCallback] = (callback != NULL);
  933. fSampleRateArg = arg;
  934. fSampleRate = callback;
  935. // Now invoke it
  936. if (callback) {
  937. callback(GetEngineControl()->fSampleRate, arg);
  938. }
  939. return 0;
  940. }
  941. }
  942. int JackClient::SetClientRegistrationCallback(JackClientRegistrationCallback callback, void* arg)
  943. {
  944. if (IsActive()) {
  945. jack_error("You cannot set callbacks on an active client");
  946. return -1;
  947. } else {
  948. // kAddClient and kRemoveClient notifications must be delivered by the server in any case
  949. fClientRegistrationArg = arg;
  950. fClientRegistration = callback;
  951. return 0;
  952. }
  953. }
  954. int JackClient::SetFreewheelCallback(JackFreewheelCallback callback, void *arg)
  955. {
  956. if (IsActive()) {
  957. jack_error("You cannot set callbacks on an active client");
  958. return -1;
  959. } else {
  960. GetClientControl()->fCallback[kStartFreewheelCallback] = (callback != NULL);
  961. GetClientControl()->fCallback[kStopFreewheelCallback] = (callback != NULL);
  962. fFreewheelArg = arg;
  963. fFreewheel = callback;
  964. return 0;
  965. }
  966. }
  967. int JackClient::SetPortRegistrationCallback(JackPortRegistrationCallback callback, void *arg)
  968. {
  969. if (IsActive()) {
  970. jack_error("You cannot set callbacks on an active client");
  971. return -1;
  972. } else {
  973. GetClientControl()->fCallback[kPortRegistrationOnCallback] = (callback != NULL);
  974. GetClientControl()->fCallback[kPortRegistrationOffCallback] = (callback != NULL);
  975. fPortRegistrationArg = arg;
  976. fPortRegistration = callback;
  977. return 0;
  978. }
  979. }
  980. int JackClient::SetPortConnectCallback(JackPortConnectCallback callback, void *arg)
  981. {
  982. if (IsActive()) {
  983. jack_error("You cannot set callbacks on an active client");
  984. return -1;
  985. } else {
  986. GetClientControl()->fCallback[kPortConnectCallback] = (callback != NULL);
  987. GetClientControl()->fCallback[kPortDisconnectCallback] = (callback != NULL);
  988. fPortConnectArg = arg;
  989. fPortConnect = callback;
  990. return 0;
  991. }
  992. }
  993. int JackClient::SetPortRenameCallback(JackPortRenameCallback callback, void *arg)
  994. {
  995. if (IsActive()) {
  996. jack_error("You cannot set callbacks on an active client");
  997. return -1;
  998. } else {
  999. GetClientControl()->fCallback[kPortRenameCallback] = (callback != NULL);
  1000. fPortRenameArg = arg;
  1001. fPortRename = callback;
  1002. return 0;
  1003. }
  1004. }
  1005. int JackClient::SetProcessThread(JackThreadCallback fun, void *arg)
  1006. {
  1007. if (IsActive()) {
  1008. jack_error("You cannot set callbacks on an active client");
  1009. return -1;
  1010. } else if (fProcess) {
  1011. jack_error("A process callback has already been setup, both models cannot be used at the same time!");
  1012. return -1;
  1013. } else {
  1014. fThreadFun = fun;
  1015. fThreadFunArg = arg;
  1016. return 0;
  1017. }
  1018. }
  1019. int JackClient::SetSessionCallback(JackSessionCallback callback, void *arg)
  1020. {
  1021. if (IsActive()) {
  1022. jack_error("You cannot set callbacks on an active client");
  1023. return -1;
  1024. } else {
  1025. GetClientControl()->fCallback[kSessionCallback] = (callback != NULL);
  1026. fSessionArg = arg;
  1027. fSession = callback;
  1028. return 0;
  1029. }
  1030. }
  1031. int JackClient::SetLatencyCallback(JackLatencyCallback callback, void *arg)
  1032. {
  1033. if (IsActive()) {
  1034. jack_error("You cannot set callbacks on an active client");
  1035. return -1;
  1036. } else {
  1037. // fCallback[kLatencyCallback] must always be 'true'
  1038. fLatencyArg = arg;
  1039. fLatency = callback;
  1040. return 0;
  1041. }
  1042. }
  1043. int JackClient::SetPropertyChangeCallback(JackPropertyChangeCallback callback, void *arg)
  1044. {
  1045. if (IsActive()) {
  1046. jack_error("You cannot set callbacks on an active client");
  1047. return -1;
  1048. } else {
  1049. fPropertyChangeArg = arg;
  1050. fPropertyChange = callback;
  1051. return 0;
  1052. }
  1053. }
  1054. //------------------
  1055. // Internal clients
  1056. //------------------
  1057. char* JackClient::GetInternalClientName(int ref)
  1058. {
  1059. char name_res[JACK_CLIENT_NAME_SIZE+1];
  1060. int result = -1;
  1061. fChannel->GetInternalClientName(GetClientControl()->fRefNum, ref, name_res, &result);
  1062. return (result < 0) ? NULL : strdup(name_res);
  1063. }
  1064. int JackClient::InternalClientHandle(const char* client_name, jack_status_t* status)
  1065. {
  1066. int int_ref, result = -1;
  1067. fChannel->InternalClientHandle(GetClientControl()->fRefNum, client_name, (int*)status, &int_ref, &result);
  1068. return int_ref;
  1069. }
  1070. int JackClient::InternalClientLoad(const char* client_name, jack_options_t options, jack_status_t* status, jack_varargs_t* va)
  1071. {
  1072. if (strlen(client_name) >= JACK_CLIENT_NAME_SIZE) {
  1073. jack_error ("\"%s\" is too long for a JACK client name.\n"
  1074. "Please use %lu characters or less.",
  1075. client_name, JACK_CLIENT_NAME_SIZE);
  1076. return 0;
  1077. }
  1078. if (va->load_name && (strlen(va->load_name) >= JACK_PATH_MAX)) {
  1079. jack_error("\"%s\" is too long for a shared object name.\n"
  1080. "Please use %lu characters or less.",
  1081. va->load_name, JACK_PATH_MAX);
  1082. int my_status1 = *status | (JackFailure | JackInvalidOption);
  1083. *status = (jack_status_t)my_status1;
  1084. return 0;
  1085. }
  1086. if (va->load_init && (strlen(va->load_init) >= JACK_LOAD_INIT_LIMIT)) {
  1087. jack_error ("\"%s\" is too long for internal client init "
  1088. "string.\nPlease use %lu characters or less.",
  1089. va->load_init, JACK_LOAD_INIT_LIMIT);
  1090. int my_status1 = *status | (JackFailure | JackInvalidOption);
  1091. *status = (jack_status_t)my_status1;
  1092. return 0;
  1093. }
  1094. int int_ref, result = -1;
  1095. fChannel->InternalClientLoad(GetClientControl()->fRefNum, client_name, va->load_name, va->load_init, options, (int*)status, &int_ref, -1, &result);
  1096. return int_ref;
  1097. }
  1098. void JackClient::InternalClientUnload(int ref, jack_status_t* status)
  1099. {
  1100. int result = -1;
  1101. fChannel->InternalClientUnload(GetClientControl()->fRefNum, ref, (int*)status, &result);
  1102. }
  1103. //------------------
  1104. // Session API
  1105. //------------------
  1106. jack_session_command_t* JackClient::SessionNotify(const char* target, jack_session_event_type_t type, const char* path)
  1107. {
  1108. jack_session_command_t* res;
  1109. fChannel->SessionNotify(GetClientControl()->fRefNum, target, type, path, &res);
  1110. return res;
  1111. }
  1112. int JackClient::SessionReply(jack_session_event_t* ev)
  1113. {
  1114. if (ev->command_line) {
  1115. strncpy(GetClientControl()->fSessionCommand, ev->command_line, sizeof(GetClientControl()->fSessionCommand));
  1116. } else {
  1117. GetClientControl()->fSessionCommand[0] = '\0';
  1118. }
  1119. GetClientControl()->fSessionFlags = ev->flags;
  1120. jack_log("JackClient::SessionReply... we are here");
  1121. if (fChannel->IsChannelThread()) {
  1122. jack_log("JackClient::SessionReply... in callback reply");
  1123. // OK, immediate reply...
  1124. fSessionReply = kImmediateSessionReply;
  1125. return 0;
  1126. }
  1127. jack_log("JackClient::SessionReply... out of cb");
  1128. int result = -1;
  1129. fChannel->SessionReply(GetClientControl()->fRefNum, &result);
  1130. return result;
  1131. }
  1132. char* JackClient::GetUUIDForClientName(const char* client_name)
  1133. {
  1134. char uuid_res[JACK_UUID_STRING_SIZE];
  1135. int result = -1;
  1136. fChannel->GetUUIDForClientName(GetClientControl()->fRefNum, client_name, uuid_res, &result);
  1137. return (result) ? NULL : strdup(uuid_res);
  1138. }
  1139. char* JackClient::GetClientNameByUUID(const char* uuid)
  1140. {
  1141. char name_res[JACK_CLIENT_NAME_SIZE + 1];
  1142. int result = -1;
  1143. fChannel->GetClientNameForUUID(GetClientControl()->fRefNum, uuid, name_res, &result);
  1144. return (result) ? NULL : strdup(name_res);
  1145. }
  1146. int JackClient::ReserveClientName(const char* client_name, const char* uuid)
  1147. {
  1148. int result = -1;
  1149. fChannel->ReserveClientName( GetClientControl()->fRefNum, client_name, uuid, &result);
  1150. return result;
  1151. }
  1152. int JackClient::ClientHasSessionCallback(const char* client_name)
  1153. {
  1154. int result = -1;
  1155. fChannel->ClientHasSessionCallback(client_name, &result);
  1156. return result;
  1157. }
  1158. //------------------
  1159. // Metadata API
  1160. //------------------
  1161. int JackClient::PropertyChangeNotify(jack_uuid_t subject, const char* key, jack_property_change_t change)
  1162. {
  1163. int result = -1;
  1164. fChannel->PropertyChangeNotify(subject, key, change, &result);
  1165. return result;
  1166. }
  1167. } // end of namespace