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.

949 lines
28KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackClient.h"
  17. #include "JackGraphManager.h"
  18. #include "JackClientControl.h"
  19. #include "JackEngineControl.h"
  20. #include "JackGlobals.h"
  21. #include "JackChannel.h"
  22. #include "JackTransportEngine.h"
  23. #include "driver_interface.h"
  24. #include <math.h>
  25. #include <string>
  26. #include <algorithm>
  27. using namespace std;
  28. namespace Jack
  29. {
  30. #define IsRealTime() ((fProcess != NULL) | (fThreadFun != NULL) | (fSync != NULL) | (fTimebase != NULL))
  31. JackClient::JackClient()
  32. {}
  33. JackClient::JackClient(JackSynchro** table)
  34. {
  35. fThread = JackGlobals::MakeThread(this);
  36. fSynchroTable = table;
  37. fProcess = NULL;
  38. fGraphOrder = NULL;
  39. fXrun = NULL;
  40. fShutdown = NULL;
  41. fInit = NULL;
  42. fBufferSize = NULL;
  43. fClientRegistration = NULL;
  44. fFreewheel = NULL;
  45. fPortRegistration = NULL;
  46. fPortConnect = NULL;
  47. fTimebase = NULL;
  48. fSync = NULL;
  49. fThreadFun = NULL;
  50. fProcessArg = NULL;
  51. fGraphOrderArg = NULL;
  52. fXrunArg = NULL;
  53. fShutdownArg = NULL;
  54. fInitArg = NULL;
  55. fBufferSizeArg = NULL;
  56. fFreewheelArg = NULL;
  57. fClientRegistrationArg = NULL;
  58. fPortRegistrationArg = NULL;
  59. fPortConnectArg = NULL;
  60. fSyncArg = NULL;
  61. fTimebaseArg = NULL;
  62. fThreadFunArg = NULL;
  63. }
  64. JackClient::~JackClient()
  65. {
  66. delete fThread;
  67. }
  68. int JackClient::Close()
  69. {
  70. jack_log("JackClient::Close ref = %ld", GetClientControl()->fRefNum);
  71. Deactivate();
  72. int result = -1;
  73. fChannel->Stop(); // Channels is stopped first to avoid receiving notifications while closing
  74. fChannel->ClientClose(GetClientControl()->fRefNum, &result);
  75. fChannel->Close();
  76. fSynchroTable[GetClientControl()->fRefNum]->Disconnect();
  77. return result;
  78. }
  79. bool JackClient::IsActive()
  80. {
  81. return (GetClientControl()) ? GetClientControl()->fActive : false;
  82. }
  83. pthread_t JackClient::GetThreadID()
  84. {
  85. return fThread->GetThreadID();
  86. }
  87. /*!
  88. In "async" mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
  89. The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
  90. Drivers synchro are setup in "flush" mode if server is "async" and NOT freewheel.
  91. */
  92. void JackClient::SetupDriverSync(bool freewheel)
  93. {
  94. if (!freewheel && !GetEngineControl()->fSyncMode) {
  95. jack_log("JackClient::SetupDriverSync driver sem in flush mode");
  96. fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
  97. fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
  98. fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
  99. } else {
  100. jack_log("JackClient::SetupDriverSync driver sem in normal mode");
  101. fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(false);
  102. fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(false);
  103. fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(false);
  104. }
  105. }
  106. /*!
  107. \brief Notification received from the server.
  108. */
  109. int JackClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value1, int value2)
  110. {
  111. return 0;
  112. }
  113. int JackClient::ClientNotify(int refnum, const char* name, int notify, int sync, int value1, int value2)
  114. {
  115. int res = 0;
  116. // Done all time: redirected on subclass implementation JackLibClient and JackInternalClient
  117. switch (notify) {
  118. case kAddClient:
  119. res = ClientNotifyImp(refnum, name, notify, sync, value1, value2);
  120. break;
  121. case kRemoveClient:
  122. res = ClientNotifyImp(refnum, name, notify, sync, value1, value2);
  123. break;
  124. case kActivateClient:
  125. jack_log("JackClient::kActivateClient name = %s ref = %ld ", name, refnum);
  126. Init();
  127. break;
  128. }
  129. /*
  130. The current semantic is that notifications can only be received when the client has been activated,
  131. although is this implementation, one could imagine calling notifications as soon as the client has be opened.
  132. */
  133. if (IsActive()) {
  134. switch (notify) {
  135. case kAddClient:
  136. jack_log("JackClient::kAddClient fName = %s name = %s", GetClientControl()->fName, name);
  137. if (fClientRegistration && strcmp(GetClientControl()->fName, name) != 0) // Don't call the callback for the registering client itself
  138. fClientRegistration(name, 1, fClientRegistrationArg);
  139. break;
  140. case kRemoveClient:
  141. jack_log("JackClient::kRemoveClient fName = %s name = %s", GetClientControl()->fName, name);
  142. if (fClientRegistration && strcmp(GetClientControl()->fName, name) != 0) // Don't call the callback for the registering client itself
  143. fClientRegistration(name, 0, fClientRegistrationArg);
  144. break;
  145. case kBufferSizeCallback:
  146. jack_log("JackClient::kBufferSizeCallback buffer_size = %ld", value1);
  147. if (fBufferSize)
  148. res = fBufferSize(value1, fBufferSizeArg);
  149. break;
  150. case kGraphOrderCallback:
  151. jack_log("JackClient::kGraphOrderCallback");
  152. if (fGraphOrder)
  153. res = fGraphOrder(fGraphOrderArg);
  154. break;
  155. case kStartFreewheelCallback:
  156. jack_log("JackClient::kStartFreewheel");
  157. SetupDriverSync(true);
  158. fThread->DropRealTime();
  159. if (fFreewheel)
  160. fFreewheel(1, fFreewheelArg);
  161. break;
  162. case kStopFreewheelCallback:
  163. jack_log("JackClient::kStopFreewheel");
  164. SetupDriverSync(false);
  165. if (fFreewheel)
  166. fFreewheel(0, fFreewheelArg);
  167. fThread->AcquireRealTime();
  168. break;
  169. case kPortRegistrationOnCallback:
  170. jack_log("JackClient::kPortRegistrationOn port_index = %ld", value1);
  171. if (fPortRegistration)
  172. fPortRegistration(value1, 1, fPortRegistrationArg);
  173. break;
  174. case kPortRegistrationOffCallback:
  175. jack_log("JackClient::kPortRegistrationOff port_index = %ld ", value1);
  176. if (fPortRegistration)
  177. fPortRegistration(value1, 0, fPortRegistrationArg);
  178. break;
  179. case kPortConnectCallback:
  180. jack_log("JackClient::kPortConnectCallback src = %ld dst = %ld", value1, value2);
  181. if (fPortConnect)
  182. fPortConnect(value1, value2, 1, fPortConnectArg);
  183. break;
  184. case kPortDisconnectCallback:
  185. jack_log("JackClient::kPortDisconnectCallback src = %ld dst = %ld", value1, value2);
  186. if (fPortConnect)
  187. fPortConnect(value1, value2, 0, fPortConnectArg);
  188. break;
  189. case kXRunCallback:
  190. jack_log("JackClient::kXRunCallback");
  191. if (fXrun)
  192. res = fXrun(fXrunArg);
  193. break;
  194. }
  195. }
  196. return res;
  197. }
  198. /*!
  199. \brief We need to start thread before activating in the server, otherwise the FW driver
  200. connected to the client may not be activated.
  201. */
  202. int JackClient::Activate()
  203. {
  204. jack_log("JackClient::Activate");
  205. if (IsActive())
  206. return 0;
  207. // RT thread is started only when needed...
  208. if (IsRealTime()) {
  209. if (StartThread() < 0)
  210. return -1;
  211. }
  212. /*
  213. Insertion of client in the graph will cause a kGraphOrderCallback notification
  214. to be delivered by the server, the client wants to receive it.
  215. */
  216. GetClientControl()->fActive = true;
  217. // Transport related callback become "active"
  218. GetClientControl()->fTransportSync = true;
  219. GetClientControl()->fTransportTimebase = true;
  220. int result = -1;
  221. GetClientControl()->fCallback[kRealTimeCallback] = IsRealTime();
  222. fChannel->ClientActivate(GetClientControl()->fRefNum, IsRealTime(), &result);
  223. return result;
  224. }
  225. /*!
  226. \brief Need to stop thread after deactivating in the server.
  227. */
  228. int JackClient::Deactivate()
  229. {
  230. jack_log("JackClient::Deactivate");
  231. if (!IsActive())
  232. return 0;
  233. GetClientControl()->fActive = false;
  234. // Transport related callback become "unactive"
  235. GetClientControl()->fTransportSync = false;
  236. GetClientControl()->fTransportTimebase = false;
  237. // We need to wait for the new engine cycle before stopping the RT thread, but this is done by ClientDeactivate
  238. int result = -1;
  239. fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
  240. jack_log("JackClient::Deactivate res = %ld", result);
  241. // RT thread is stopped only when needed...
  242. if (IsRealTime())
  243. fThread->Kill();
  244. return result;
  245. }
  246. //----------------------
  247. // RT thread management
  248. //----------------------
  249. /*!
  250. \brief Called once when the thread starts.
  251. */
  252. bool JackClient::Init()
  253. {
  254. if (fInit) {
  255. jack_log("JackClient::Init calling client thread init callback");
  256. fInit(fInitArg);
  257. }
  258. return true;
  259. }
  260. int JackClient::StartThread()
  261. {
  262. jack_log("JackClient::StartThread : period = %ld computation = %ld constraint = %ld",
  263. long(int64_t(GetEngineControl()->fPeriod) / 1000.0f),
  264. long(int64_t(GetEngineControl()->fComputation) / 1000.0f),
  265. long(int64_t(GetEngineControl()->fConstraint) / 1000.0f));
  266. // Will do "something" on OSX only...
  267. fThread->SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
  268. if (fThread->Start() < 0) {
  269. jack_error("Start thread error");
  270. return -1;
  271. }
  272. if (GetEngineControl()->fRealTime) {
  273. if (fThread->AcquireRealTime(GetEngineControl()->fPriority - 1) < 0) {
  274. jack_error("AcquireRealTime error");
  275. }
  276. }
  277. return 0;
  278. }
  279. /*!
  280. \brief RT thread.
  281. */
  282. bool JackClient::Execute()
  283. {
  284. if (!jack_tls_set(gRealTime, this))
  285. jack_error("failed to set thread realtime key");
  286. if (fThreadFun) {
  287. // Execute a dummy cycle to be sure thread has the correct properties (ensure thread creation is finished)
  288. WaitSync();
  289. SignalSync();
  290. fThreadFun(fThreadFunArg);
  291. } else {
  292. if (WaitFirstSync())
  293. ExecuteThread();
  294. }
  295. return false;
  296. }
  297. inline bool JackClient::WaitFirstSync()
  298. {
  299. while (true) {
  300. // Start first cycle
  301. WaitSync();
  302. if (IsActive()) {
  303. CallSyncCallback();
  304. // Finish first cycle
  305. if (Wait(CallProcessCallback()) != GetEngineControl()->fBufferSize)
  306. return false;
  307. return true;
  308. } else {
  309. jack_log("Process called for an inactive client");
  310. }
  311. SignalSync();
  312. }
  313. return false;
  314. }
  315. inline void JackClient::ExecuteThread()
  316. {
  317. while (Wait(CallProcessCallback()) == GetEngineControl()->fBufferSize);
  318. }
  319. jack_nframes_t JackClient::Wait(int status)
  320. {
  321. if (status == 0)
  322. CallTimebaseCallback();
  323. SignalSync();
  324. if (status != 0)
  325. return End();
  326. if (!WaitSync())
  327. return Error();
  328. CallSyncCallback();
  329. return GetEngineControl()->fBufferSize;
  330. }
  331. /*
  332. jack_nframes_t JackClient::Wait(int status)
  333. {
  334. CycleSignal(status);
  335. return CycleWait();
  336. }
  337. */
  338. jack_nframes_t JackClient::CycleWait()
  339. {
  340. if (!WaitSync())
  341. return Error();
  342. CallSyncCallback();
  343. return GetEngineControl()->fBufferSize;
  344. }
  345. void JackClient::CycleSignal(int status)
  346. {
  347. if (status == 0)
  348. CallTimebaseCallback();
  349. SignalSync();
  350. if (status != 0)
  351. End();
  352. }
  353. inline int JackClient::CallProcessCallback()
  354. {
  355. return (fProcess != NULL) ? fProcess(GetEngineControl()->fBufferSize, fProcessArg) : 0;
  356. }
  357. inline bool JackClient::WaitSync()
  358. {
  359. // Suspend itself: wait on the input synchro
  360. if (GetGraphManager()->SuspendRefNum(GetClientControl(), fSynchroTable, 0x7FFFFFFF) < 0) {
  361. jack_error("SuspendRefNum error");
  362. return false;
  363. } else {
  364. return true;
  365. }
  366. }
  367. inline void JackClient::SignalSync()
  368. {
  369. // Resume: signal output clients connected to the running client
  370. if (GetGraphManager()->ResumeRefNum(GetClientControl(), fSynchroTable) < 0) {
  371. jack_error("ResumeRefNum error");
  372. }
  373. }
  374. inline int JackClient::End()
  375. {
  376. jack_log("JackClient::Execute end name = %s", GetClientControl()->fName);
  377. // Hum... not sure about this, the following "close" code is called in the RT thread...
  378. int result;
  379. fThread->DropRealTime();
  380. GetClientControl()->fActive = false;
  381. fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
  382. return 0;
  383. }
  384. inline int JackClient::Error()
  385. {
  386. jack_error("JackClient::Execute error name = %s", GetClientControl()->fName);
  387. // Hum... not sure about this, the following "close" code is called in the RT thread...
  388. int result;
  389. fThread->DropRealTime();
  390. GetClientControl()->fActive = false;
  391. fChannel->ClientDeactivate(GetClientControl()->fRefNum, &result);
  392. ShutDown();
  393. return 0;
  394. }
  395. //-----------------
  396. // Port management
  397. //-----------------
  398. int JackClient::PortRegister(const char* port_name, const char* port_type, unsigned long flags, unsigned long buffer_size)
  399. {
  400. // Check if port name is empty
  401. string port_name_str = string(port_name);
  402. if (port_name_str.size() == 0) {
  403. jack_error("port_name is empty");
  404. return 0; // Means failure here...
  405. }
  406. // Check port name length
  407. string name = string(GetClientControl()->fName) + string(":") + port_name_str;
  408. if (name.size() >= JACK_PORT_NAME_SIZE) {
  409. jack_error("\"%s:%s\" is too long to be used as a JACK port name.\n"
  410. "Please use %lu characters or less",
  411. GetClientControl()->fName,
  412. port_name,
  413. JACK_PORT_NAME_SIZE - 1);
  414. return 0; // Means failure here...
  415. }
  416. int result = -1;
  417. unsigned int port_index = NO_PORT;
  418. fChannel->PortRegister(GetClientControl()->fRefNum, name.c_str(), port_type, flags, buffer_size, &port_index, &result);
  419. if (result == 0) {
  420. jack_log("JackClient::PortRegister ref = %ld name = %s type = %s port_index = %ld", GetClientControl()->fRefNum, name.c_str(), port_type, port_index);
  421. fPortList.push_back(port_index);
  422. return port_index;
  423. } else {
  424. return 0;
  425. }
  426. }
  427. int JackClient::PortUnRegister(jack_port_id_t port_index)
  428. {
  429. jack_log("JackClient::PortUnRegister port_index = %ld", port_index);
  430. list<jack_port_id_t>::iterator it = find(fPortList.begin(), fPortList.end(), port_index);
  431. if (it != fPortList.end()) {
  432. fPortList.erase(it);
  433. int result = -1;
  434. fChannel->PortUnRegister(GetClientControl()->fRefNum, port_index, &result);
  435. return result;
  436. } else {
  437. jack_error("unregistering a port %ld that is not own by the client", port_index);
  438. return -1;
  439. }
  440. }
  441. int JackClient::PortConnect(const char* src, const char* dst)
  442. {
  443. jack_log("JackClient::Connect src = %s dst = %s", src, dst);
  444. int result = -1;
  445. fChannel->PortConnect(GetClientControl()->fRefNum, src, dst, &result);
  446. return result;
  447. }
  448. int JackClient::PortDisconnect(const char* src, const char* dst)
  449. {
  450. jack_log("JackClient::Disconnect src = %s dst = %s", src, dst);
  451. int result = -1;
  452. fChannel->PortDisconnect(GetClientControl()->fRefNum, src, dst, &result);
  453. return result;
  454. }
  455. int JackClient::PortDisconnect(jack_port_id_t src)
  456. {
  457. jack_log("JackClient::PortDisconnect src = %ld", src);
  458. int result = -1;
  459. fChannel->PortDisconnect(GetClientControl()->fRefNum, src, ALL_PORTS, &result);
  460. return result;
  461. }
  462. int JackClient::PortIsMine(jack_port_id_t port_index)
  463. {
  464. JackPort* port = GetGraphManager()->GetPort(port_index);
  465. return GetClientControl()->fRefNum == port->GetRefNum();
  466. }
  467. //--------------------
  468. // Context management
  469. //--------------------
  470. int JackClient::SetBufferSize(jack_nframes_t buffer_size)
  471. {
  472. int result = -1;
  473. fChannel->SetBufferSize(buffer_size, &result);
  474. return result;
  475. }
  476. int JackClient::SetFreeWheel(int onoff)
  477. {
  478. int result = -1;
  479. fChannel->SetFreewheel(onoff, &result);
  480. return result;
  481. }
  482. /*
  483. ShutDown is called:
  484. - from the RT thread when Execute method fails
  485. - possibly from a "closed" notification channel
  486. (Not needed since the synch object used (Sema of Fifo will fails when server quits... see ShutDown))
  487. */
  488. void JackClient::ShutDown()
  489. {
  490. jack_log("ShutDown");
  491. if (fShutdown) {
  492. GetClientControl()->fActive = false;
  493. fShutdown(fShutdownArg);
  494. fShutdown = NULL;
  495. }
  496. }
  497. //----------------------
  498. // Transport management
  499. //----------------------
  500. int JackClient::ReleaseTimebase()
  501. {
  502. int result = -1;
  503. fChannel->ReleaseTimebase(GetClientControl()->fRefNum, &result);
  504. if (result == 0) {
  505. GetClientControl()->fTransportTimebase = false;
  506. fTimebase = NULL;
  507. fTimebaseArg = NULL;
  508. }
  509. return result;
  510. }
  511. /* Call the server if the client is active, otherwise keeps the arguments */
  512. int JackClient::SetSyncCallback(JackSyncCallback sync_callback, void* arg)
  513. {
  514. GetClientControl()->fTransportSync = (fSync != NULL);
  515. fSyncArg = arg;
  516. fSync = sync_callback;
  517. return 0;
  518. }
  519. int JackClient::SetSyncTimeout(jack_time_t timeout)
  520. {
  521. GetEngineControl()->fTransport.SetSyncTimeout(timeout);
  522. return 0;
  523. }
  524. int JackClient::SetTimebaseCallback(int conditional, JackTimebaseCallback timebase_callback, void* arg)
  525. {
  526. int result = -1;
  527. fChannel->SetTimebaseCallback(GetClientControl()->fRefNum, conditional, &result);
  528. jack_log("SetTimebaseCallback result = %ld", result);
  529. if (result == 0) {
  530. GetClientControl()->fTransportTimebase = true;
  531. fTimebase = timebase_callback;
  532. fTimebaseArg = arg;
  533. } else {
  534. fTimebase = NULL;
  535. fTimebaseArg = NULL;
  536. }
  537. return result;
  538. }
  539. // Must be RT safe
  540. int JackClient::RequestNewPos(jack_position_t* pos)
  541. {
  542. JackTransportEngine& transport = GetEngineControl()->fTransport;
  543. jack_position_t* request = transport.WriteNextStateStart(2);
  544. pos->unique_1 = pos->unique_2 = transport.GenerateUniqueID();
  545. JackTransportEngine::TransportCopyPosition(pos, request);
  546. jack_log("RequestNewPos pos = %ld", pos->frame);
  547. transport.WriteNextStateStop(2);
  548. return 0;
  549. }
  550. int JackClient::TransportLocate(jack_nframes_t frame)
  551. {
  552. jack_position_t pos;
  553. pos.frame = frame;
  554. pos.valid = (jack_position_bits_t)0;
  555. jack_log("TransportLocate pos = %ld", pos.frame);
  556. return RequestNewPos(&pos);
  557. }
  558. int JackClient::TransportReposition(jack_position_t* pos)
  559. {
  560. jack_position_t tmp = *pos;
  561. jack_log("TransportReposition pos = %ld", pos->frame);
  562. return (tmp.valid & ~JACK_POSITION_MASK) ? EINVAL : RequestNewPos(&tmp);
  563. }
  564. jack_transport_state_t JackClient::TransportQuery(jack_position_t* pos)
  565. {
  566. if (pos)
  567. GetEngineControl()->fTransport.ReadCurrentPos(pos);
  568. return GetEngineControl()->fTransport.GetState();
  569. }
  570. jack_nframes_t JackClient::GetCurrentTransportFrame()
  571. {
  572. jack_position_t pos;
  573. jack_transport_state_t state = TransportQuery(&pos);
  574. if (state == JackTransportRolling) {
  575. float usecs = GetMicroSeconds() - pos.usecs;
  576. jack_nframes_t elapsed = (jack_nframes_t)floor((((float) pos.frame_rate) / 1000000.0f) * usecs);
  577. return pos.frame + elapsed;
  578. } else {
  579. return pos.frame;
  580. }
  581. }
  582. // Must be RT safe: directly write in the transport shared mem
  583. void JackClient::TransportStart()
  584. {
  585. GetEngineControl()->fTransport.SetCommand(TransportCommandStart);
  586. }
  587. // Must be RT safe: directly write in the transport shared mem
  588. void JackClient::TransportStop()
  589. {
  590. GetEngineControl()->fTransport.SetCommand(TransportCommandStop);
  591. }
  592. // Never called concurently with the server
  593. // TODO check concurrency with SetSyncCallback
  594. void JackClient::CallSyncCallback()
  595. {
  596. if (GetClientControl()->fTransportSync) {
  597. JackTransportEngine& transport = GetEngineControl()->fTransport;
  598. jack_position_t* cur_pos = transport.ReadCurrentState();
  599. jack_transport_state_t transport_state = transport.GetState();
  600. if (fSync != NULL) {
  601. if (fSync(transport_state, cur_pos, fSyncArg)) {
  602. GetClientControl()->fTransportState = JackTransportRolling;
  603. GetClientControl()->fTransportSync = false;
  604. }
  605. } else {
  606. GetClientControl()->fTransportState = JackTransportRolling;
  607. GetClientControl()->fTransportSync = false;
  608. }
  609. }
  610. }
  611. void JackClient::CallTimebaseCallback()
  612. {
  613. JackTransportEngine& transport = GetEngineControl()->fTransport;
  614. if (GetClientControl()->fRefNum == transport.GetTimebaseMaster() && fTimebase) { // Client *is* timebase...
  615. jack_transport_state_t transport_state = transport.GetState();
  616. jack_position_t* cur_pos = transport.WriteNextStateStart(1);
  617. if (GetClientControl()->fTransportTimebase) {
  618. fTimebase(transport_state, GetEngineControl()->fBufferSize, cur_pos, true, fTimebaseArg);
  619. GetClientControl()->fTransportTimebase = false; // Callback is called only once with "new_pos" = true
  620. } else if (transport_state == JackTransportRolling) {
  621. fTimebase(transport_state, GetEngineControl()->fBufferSize, cur_pos, false, fTimebaseArg);
  622. }
  623. transport.WriteNextStateStop(1);
  624. }
  625. }
  626. //---------------------
  627. // Callback management
  628. //---------------------
  629. void JackClient::OnShutdown(JackShutdownCallback callback, void *arg)
  630. {
  631. if (IsActive()) {
  632. jack_error("You cannot set callbacks on an active client");
  633. } else {
  634. fShutdownArg = arg;
  635. fShutdown = callback;
  636. }
  637. }
  638. int JackClient::SetProcessCallback(JackProcessCallback callback, void *arg)
  639. {
  640. if (IsActive()) {
  641. jack_error("You cannot set callbacks on an active client");
  642. return -1;
  643. } else if (fThreadFun) {
  644. jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
  645. return -1;
  646. } else {
  647. fProcessArg = arg;
  648. fProcess = callback;
  649. return 0;
  650. }
  651. }
  652. int JackClient::SetXRunCallback(JackXRunCallback callback, void *arg)
  653. {
  654. if (IsActive()) {
  655. jack_error("You cannot set callbacks on an active client");
  656. return -1;
  657. } else {
  658. GetClientControl()->fCallback[kXRunCallback] = (callback != NULL);
  659. fXrunArg = arg;
  660. fXrun = callback;
  661. return 0;
  662. }
  663. }
  664. int JackClient::SetInitCallback(JackThreadInitCallback callback, void *arg)
  665. {
  666. if (IsActive()) {
  667. jack_error("You cannot set callbacks on an active client");
  668. return -1;
  669. } else {
  670. fInitArg = arg;
  671. fInit = callback;
  672. return 0;
  673. }
  674. }
  675. int JackClient::SetGraphOrderCallback(JackGraphOrderCallback callback, void *arg)
  676. {
  677. jack_log("SetGraphOrderCallback ");
  678. if (IsActive()) {
  679. jack_error("You cannot set callbacks on an active client");
  680. return -1;
  681. } else {
  682. GetClientControl()->fCallback[kGraphOrderCallback] = (callback != NULL);
  683. fGraphOrder = callback;
  684. fGraphOrderArg = arg;
  685. return 0;
  686. }
  687. }
  688. int JackClient::SetBufferSizeCallback(JackBufferSizeCallback callback, void *arg)
  689. {
  690. if (IsActive()) {
  691. jack_error("You cannot set callbacks on an active client");
  692. return -1;
  693. } else {
  694. GetClientControl()->fCallback[kBufferSizeCallback] = (callback != NULL);
  695. fBufferSizeArg = arg;
  696. fBufferSize = callback;
  697. return 0;
  698. }
  699. }
  700. int JackClient::SetClientRegistrationCallback(JackClientRegistrationCallback callback, void* arg)
  701. {
  702. if (IsActive()) {
  703. jack_error("You cannot set callbacks on an active client");
  704. return -1;
  705. } else {
  706. // kAddClient and kRemoveClient notifications must be delivered by the server in any case
  707. fClientRegistrationArg = arg;
  708. fClientRegistration = callback;
  709. return 0;
  710. }
  711. }
  712. int JackClient::SetFreewheelCallback(JackFreewheelCallback callback, void *arg)
  713. {
  714. if (IsActive()) {
  715. jack_error("You cannot set callbacks on an active client");
  716. return -1;
  717. } else {
  718. GetClientControl()->fCallback[kStartFreewheelCallback] = (callback != NULL);
  719. GetClientControl()->fCallback[kStopFreewheelCallback] = (callback != NULL);
  720. fFreewheelArg = arg;
  721. fFreewheel = callback;
  722. return 0;
  723. }
  724. }
  725. int JackClient::SetPortRegistrationCallback(JackPortRegistrationCallback callback, void *arg)
  726. {
  727. if (IsActive()) {
  728. jack_error("You cannot set callbacks on an active client");
  729. return -1;
  730. } else {
  731. GetClientControl()->fCallback[kPortRegistrationOnCallback] = (callback != NULL);
  732. GetClientControl()->fCallback[kPortRegistrationOffCallback] = (callback != NULL);
  733. fPortRegistrationArg = arg;
  734. fPortRegistration = callback;
  735. return 0;
  736. }
  737. }
  738. int JackClient::SetPortConnectCallback(JackPortConnectCallback callback, void *arg)
  739. {
  740. if (IsActive()) {
  741. jack_error("You cannot set callbacks on an active client");
  742. return -1;
  743. } else {
  744. GetClientControl()->fCallback[kPortConnectCallback] = (callback != NULL);
  745. GetClientControl()->fCallback[kPortDisconnectCallback] = (callback != NULL);
  746. fPortConnectArg = arg;
  747. fPortConnect = callback;
  748. return 0;
  749. }
  750. }
  751. int JackClient::SetProcessThread(JackThreadCallback fun, void *arg)
  752. {
  753. if (IsActive()) {
  754. jack_error("You cannot set callbacks on an active client");
  755. return -1;
  756. } else if (fProcess) {
  757. jack_error ("A process callback has already been setup, both models cannot be used at the same time!");
  758. return -1;
  759. } else {
  760. fThreadFun = fun;
  761. fThreadFunArg = arg;
  762. return 0;
  763. }
  764. }
  765. //------------------
  766. // Internal clients
  767. //------------------
  768. char* JackClient::GetInternalClientName(int ref)
  769. {
  770. char name_res[JACK_CLIENT_NAME_SIZE];
  771. int result = -1;
  772. fChannel->GetInternalClientName(GetClientControl()->fRefNum, ref, name_res, &result);
  773. if (result < 0) {
  774. return NULL;
  775. } else {
  776. char* name = (char*)malloc(strlen(name_res));
  777. strcpy(name, name_res);
  778. return name;
  779. }
  780. }
  781. int JackClient::InternalClientHandle(const char* client_name, jack_status_t* status)
  782. {
  783. int int_ref, result = -1;
  784. fChannel->InternalClientHandle(GetClientControl()->fRefNum, client_name, (int*)status, &int_ref, &result);
  785. return int_ref;
  786. }
  787. int JackClient::InternalClientLoad(const char* client_name, jack_options_t options, jack_status_t* status, jack_varargs_t* va)
  788. {
  789. if (strlen(client_name) >= JACK_CLIENT_NAME_SIZE) {
  790. jack_error ("\"%s\" is too long for a JACK client name.\n"
  791. "Please use %lu characters or less.",
  792. client_name, JACK_CLIENT_NAME_SIZE);
  793. return 0;
  794. }
  795. if (va->load_name && (strlen(va->load_name) >= PATH_MAX)) {
  796. jack_error("\"%s\" is too long for a shared object name.\n"
  797. "Please use %lu characters or less.",
  798. va->load_name, PATH_MAX);
  799. int my_status1 = *status | (JackFailure | JackInvalidOption);
  800. *status = (jack_status_t)my_status1;
  801. return 0;
  802. }
  803. if (va->load_init && (strlen(va->load_init) >= JACK_LOAD_INIT_LIMIT)) {
  804. jack_error ("\"%s\" is too long for internal client init "
  805. "string.\nPlease use %lu characters or less.",
  806. va->load_init, JACK_LOAD_INIT_LIMIT);
  807. int my_status1 = *status | (JackFailure | JackInvalidOption);
  808. *status = (jack_status_t)my_status1;
  809. return 0;
  810. }
  811. int int_ref, result = -1;
  812. fChannel->InternalClientLoad(GetClientControl()->fRefNum, client_name, va->load_name, va->load_init, options, (int*)status, &int_ref, &result);
  813. return int_ref;
  814. }
  815. void JackClient::InternalClientUnload(int ref, jack_status_t* status)
  816. {
  817. int result = -1;
  818. fChannel->InternalClientUnload(GetClientControl()->fRefNum, ref, (int*)status, &result);
  819. }
  820. } // end of namespace