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.

881 lines
26KB

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