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.

749 lines
22KB

  1. /*
  2. Copyright (C) 2004-2006 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include <iostream>
  16. #include <fstream>
  17. #include <assert.h>
  18. #include "JackEngine.h"
  19. #include "JackExternalClient.h"
  20. #include "JackEngineControl.h"
  21. #include "JackClientControl.h"
  22. #include "JackEngineTiming.h"
  23. #include "JackGlobals.h"
  24. #include "JackChannel.h"
  25. #include "JackSyncInterface.h"
  26. namespace Jack
  27. {
  28. JackEngine::JackEngine(JackGraphManager* manager,
  29. JackSynchro** table,
  30. JackEngineControl* control)
  31. {
  32. fGraphManager = manager;
  33. fSynchroTable = table;
  34. fEngineControl = control;
  35. fChannel = JackGlobals::MakeServerNotifyChannel();
  36. fSignal = JackGlobals::MakeInterProcessSync();
  37. fEngineTiming = new JackEngineTiming(fClientTable, fGraphManager, fEngineControl);
  38. for (int i = 0; i < CLIENT_NUM; i++)
  39. fClientTable[i] = NULL;
  40. fEngineTiming->ClearTimeMeasures();
  41. fEngineTiming->ResetRollingUsecs();
  42. }
  43. JackEngine::~JackEngine()
  44. {
  45. delete fChannel;
  46. delete fEngineTiming;
  47. delete fSignal;
  48. }
  49. //-------------------
  50. // Client management
  51. //-------------------
  52. int JackEngine::Open()
  53. {
  54. JackLog("JackEngine::Open\n");
  55. // Open audio thread => request thread communication channel
  56. if (fChannel->Open() < 0) {
  57. jack_error("Cannot connect to server");
  58. return -1;
  59. } else {
  60. return 0;
  61. }
  62. }
  63. int JackEngine::Close()
  64. {
  65. JackLog("JackEngine::Close\n");
  66. fChannel->Close();
  67. // Close (possibly) remaining clients (RT is stopped)
  68. for (int i = 0; i < CLIENT_NUM; i++) {
  69. JackClientInterface* client = fClientTable[i];
  70. if (client) {
  71. JackLog("JackEngine::Close remaining client %ld\n", i);
  72. ClientCloseAux(i, client, false);
  73. client->Close();
  74. delete client;
  75. }
  76. }
  77. fSignal->Destroy();
  78. return 0;
  79. }
  80. int JackEngine::AllocateRefnum()
  81. {
  82. for (int i = 0; i < CLIENT_NUM; i++) {
  83. if (!fClientTable[i]) {
  84. JackLog("JackEngine::AllocateRefNum ref = %ld\n", i);
  85. return i;
  86. }
  87. }
  88. return -1;
  89. }
  90. void JackEngine::ReleaseRefnum(int ref)
  91. {
  92. fClientTable[ref] = NULL;
  93. int i;
  94. if (fEngineControl->fTemporary) {
  95. for (i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  96. if (fClientTable[i])
  97. break;
  98. }
  99. if (i == CLIENT_NUM) {
  100. // last client and temporay case: quit the server
  101. JackLog("JackEngine::ReleaseRefnum server quit\n");
  102. fEngineControl->fTemporary = false;
  103. #ifndef WIN32
  104. kill(getpid(), SIGINT);
  105. #endif
  106. }
  107. }
  108. }
  109. //------------------
  110. // Graph management
  111. //------------------
  112. void JackEngine::ProcessNext(jack_time_t callback_usecs)
  113. {
  114. fLastSwitchUsecs = callback_usecs;
  115. if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
  116. fChannel->ClientNotify(ALL_CLIENTS, kGraphOrderCallback, 0);
  117. fSignal->SignalAll(); // Signal for threads waiting for next cycle
  118. }
  119. void JackEngine::ProcessCurrent(jack_time_t callback_usecs)
  120. {
  121. if (callback_usecs < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failling cycle
  122. CheckXRun(callback_usecs);
  123. fGraphManager->RunCurrentGraph();
  124. }
  125. bool JackEngine::Process(jack_time_t callback_usecs)
  126. {
  127. bool res = true;
  128. // Transport begin
  129. fEngineControl->CycleBegin(callback_usecs);
  130. // Timing
  131. fEngineControl->IncFrameTime(callback_usecs);
  132. fEngineTiming->UpdateTiming(callback_usecs);
  133. // Graph
  134. if (fGraphManager->IsFinishedGraph()) {
  135. ProcessNext(callback_usecs);
  136. res = true;
  137. } else {
  138. JackLog("Process: graph not finished!\n");
  139. if (callback_usecs > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  140. JackLog("Process: switch to next state delta = %ld\n", long(callback_usecs - fLastSwitchUsecs));
  141. //RemoveZombifiedClients(callback_usecs); TODO
  142. ProcessNext(callback_usecs);
  143. res = true;
  144. } else {
  145. JackLog("Process: waiting to switch delta = %ld\n", long(callback_usecs - fLastSwitchUsecs));
  146. ProcessCurrent(callback_usecs);
  147. res = false;
  148. }
  149. }
  150. // Transport end
  151. fEngineControl->CycleEnd(fClientTable);
  152. return res;
  153. }
  154. /*
  155. Client that finish *after* the callback date are considered late even if their output buffers may have been
  156. correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
  157. */
  158. void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
  159. {
  160. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  161. JackClientInterface* client = fClientTable[i];
  162. if (client && client->GetClientControl()->fActive) {
  163. JackClientTiming* timing = fGraphManager->GetClientTiming(i);
  164. jack_client_state_t status = timing->fStatus;
  165. jack_time_t finished_date = timing->fFinishedAt;
  166. if (status != NotTriggered && status != Finished) {
  167. jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
  168. //fChannel->ClientNotify(i, kXRunCallback, 0); // Notify the failing client
  169. fChannel->ClientNotify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  170. }
  171. if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
  172. jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
  173. //fChannel->ClientNotify(i, kXRunCallback, 0); // Notify the failing client
  174. fChannel->ClientNotify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  175. }
  176. }
  177. }
  178. }
  179. //---------------
  180. // Zombification
  181. //---------------
  182. bool JackEngine::IsZombie(JackClientInterface* client, jack_time_t current_time)
  183. {
  184. return ((current_time - fGraphManager->GetClientTiming(client->GetClientControl()->fRefNum)->fFinishedAt) > 2 * fEngineControl->fTimeOutUsecs); // A VERIFIER
  185. }
  186. // TODO : check what happens with looped sub-graph....
  187. void JackEngine::GetZombifiedClients(bool zombi_clients[CLIENT_NUM], jack_time_t current_time)
  188. {
  189. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  190. JackClientInterface* client1 = fClientTable[i];
  191. if (client1 && IsZombie(client1, current_time)) {
  192. JackLog("JackEngine::GetZombifiedClients: %s\n", client1->GetClientControl()->fName);
  193. zombi_clients[i] = true; // Assume client is dead
  194. // If another dead client is connected to the scanned one, then the scanned one is not the first of the dead subgraph
  195. for (int j = REAL_REFNUM; j < CLIENT_NUM; j++) {
  196. JackClientInterface* client2 = fClientTable[j];
  197. if (client2 && IsZombie(client2, current_time) && fGraphManager->IsDirectConnection(j, i)) {
  198. zombi_clients[i] = false;
  199. break;
  200. }
  201. }
  202. } else {
  203. zombi_clients[i] = false;
  204. }
  205. }
  206. }
  207. void JackEngine::RemoveZombifiedClients(jack_time_t current_time)
  208. {
  209. bool zombi_clients[CLIENT_NUM];
  210. GetZombifiedClients(zombi_clients, current_time);
  211. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  212. if (zombi_clients[i] && !fClientTable[i]->GetClientControl()->fZombie) {
  213. fClientTable[i]->GetClientControl()->fZombie = true;
  214. JackLog("RemoveZombifiedCients: name = %s\n", fClientTable[i]->GetClientControl()->fName);
  215. fGraphManager->DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, i);
  216. fGraphManager->DirectDisconnect(i, FREEWHEEL_DRIVER_REFNUM);
  217. fGraphManager->DisconnectAllPorts(i);
  218. fChannel->ClientNotify(i, kZombifyClient, 0); // Signal engine
  219. }
  220. }
  221. }
  222. void JackEngine::ZombifyClient(int refnum)
  223. {
  224. NotifyClient(refnum, kZombifyClient, false, 0);
  225. }
  226. //---------------
  227. // Notifications
  228. //---------------
  229. void JackEngine::NotifyClient(int refnum, int event, int sync, int value)
  230. {
  231. JackClientInterface* client = fClientTable[refnum];
  232. // The client may be notified by the RT thread while closing
  233. if (!client) {
  234. JackLog("JackEngine::NotifyClient: client not available anymore\n");
  235. } else if (client->GetClientControl()->fCallback[event]) {
  236. if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, value) < 0)
  237. jack_error("NotifyClient fails name = %s event = %ld = val = %ld", client->GetClientControl()->fName, event, value);
  238. } else {
  239. JackLog("JackEngine::NotifyClient: no callback for event = %ld\n", event);
  240. }
  241. }
  242. void JackEngine::NotifyClients(int event, int sync, int value)
  243. {
  244. for (int i = 0; i < CLIENT_NUM; i++) {
  245. JackClientInterface* client = fClientTable[i];
  246. if (client) {
  247. if (client->GetClientControl()->fCallback[event]) {
  248. if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, value) < 0)
  249. jack_error("NotifyClient fails name = %s event = %ld = val = %ld", client->GetClientControl()->fName, event, value);
  250. } else {
  251. JackLog("JackEngine::NotifyClients: no callback for event = %ld\n", event);
  252. }
  253. }
  254. }
  255. }
  256. int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
  257. {
  258. // Notify existing clients of the new client and new client of existing clients.
  259. for (int i = 0; i < CLIENT_NUM; i++) {
  260. JackClientInterface* old_client = fClientTable[i];
  261. if (old_client) {
  262. if (old_client->ClientNotify(refnum, name, kAddClient, true, 0) < 0) {
  263. jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
  264. return -1;
  265. }
  266. if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, 0) < 0) {
  267. jack_error("NotifyAddClient new_client fails name = %s", name);
  268. return -1;
  269. }
  270. }
  271. }
  272. return 0;
  273. }
  274. void JackEngine::NotifyRemoveClient(const char* name, int refnum)
  275. {
  276. // Notify existing clients (including the one beeing suppressed) of the removed client
  277. for (int i = 0; i < CLIENT_NUM; i++) {
  278. JackClientInterface* client = fClientTable[i];
  279. if (client) {
  280. client->ClientNotify(refnum, name, kRemoveClient, true, 0);
  281. }
  282. }
  283. }
  284. // Coming from the driver
  285. void JackEngine::NotifyXRun(jack_time_t callback_usecs)
  286. {
  287. // Use the audio thread => request thread communication channel
  288. fEngineControl->ResetFrameTime(callback_usecs);
  289. fChannel->ClientNotify(ALL_CLIENTS, kXRunCallback, 0);
  290. }
  291. void JackEngine::NotifyXRun(int refnum)
  292. {
  293. if (refnum == ALL_CLIENTS) {
  294. NotifyClients(kXRunCallback, false, 0);
  295. } else {
  296. NotifyClient(refnum, kXRunCallback, false, 0);
  297. }
  298. }
  299. void JackEngine::NotifyGraphReorder()
  300. {
  301. NotifyClients(kGraphOrderCallback, false, 0);
  302. }
  303. void JackEngine::NotifyBufferSize(jack_nframes_t nframes)
  304. {
  305. NotifyClients(kBufferSizeCallback, true, nframes);
  306. }
  307. void JackEngine::NotifyFreewheel(bool onoff)
  308. {
  309. fEngineControl->fRealTime = !onoff;
  310. NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, 0);
  311. }
  312. void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
  313. {
  314. NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, port_index);
  315. }
  316. void JackEngine::NotifyActivate(int refnum)
  317. {
  318. NotifyClient(refnum, kActivateClient, true, 0);
  319. }
  320. //-------------------
  321. // Client management
  322. //-------------------
  323. int JackEngine::ClientCheck(const char* name, char* name_res, int options, int* status)
  324. {
  325. strcpy(name_res, name);
  326. if (ClientCheckName(name)) {
  327. *status |= JackNameNotUnique;
  328. if (options & JackUseExactName) {
  329. jack_error("cannot create new client; %s already exists", name);
  330. *status |= JackFailure;
  331. return -1;
  332. }
  333. if (GenerateUniqueName(name_res)) {
  334. *status |= JackFailure;
  335. return -1;
  336. }
  337. }
  338. return 0;
  339. }
  340. bool JackEngine::GenerateUniqueName(char* name)
  341. {
  342. int tens, ones;
  343. int length = strlen(name);
  344. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  345. jack_error("%s exists and is too long to make unique", name);
  346. return true; /* failure */
  347. }
  348. /* generate a unique name by appending "-01".."-99" */
  349. name[length++] = '-';
  350. tens = length++;
  351. ones = length++;
  352. name[tens] = '0';
  353. name[ones] = '1';
  354. name[length] = '\0';
  355. while (ClientCheckName(name)) {
  356. if (name[ones] == '9') {
  357. if (name[tens] == '9') {
  358. jack_error("client %s has 99 extra instances already", name);
  359. return true; /* give up */
  360. }
  361. name[tens]++;
  362. name[ones] = '0';
  363. } else {
  364. name[ones]++;
  365. }
  366. }
  367. return false;
  368. }
  369. bool JackEngine::ClientCheckName(const char* name)
  370. {
  371. for (int i = 0; i < CLIENT_NUM; i++) {
  372. JackClientInterface* client = fClientTable[i];
  373. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  374. return true;
  375. }
  376. return false;
  377. }
  378. // Used for external clients
  379. int JackEngine::ClientExternalOpen(const char* name, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  380. {
  381. JackLog("JackEngine::ClientOpen: name = %s \n", name);
  382. int refnum = AllocateRefnum();
  383. if (refnum < 0) {
  384. jack_error("No more refnum available");
  385. return -1;
  386. }
  387. JackExternalClient* client = new JackExternalClient();
  388. if (!fSynchroTable[refnum]->Allocate(name, 0)) {
  389. jack_error("Cannot allocate synchro");
  390. goto error;
  391. }
  392. if (client->Open(name, refnum, shared_client) < 0) {
  393. jack_error("Cannot open client");
  394. goto error;
  395. }
  396. if (!fSignal->TimedWait(5 * 1000000)) {
  397. // Failure if RT thread is not running (problem with the driver...)
  398. jack_error("Driver is not running");
  399. goto error;
  400. }
  401. if (NotifyAddClient(client, name, refnum) < 0) {
  402. jack_error("Cannot notify add client");
  403. goto error;
  404. }
  405. fClientTable[refnum] = client;
  406. fGraphManager->InitRefNum(refnum);
  407. fEngineTiming->ResetRollingUsecs();
  408. *shared_engine = fEngineControl->GetShmIndex();
  409. *shared_graph_manager = fGraphManager->GetShmIndex();
  410. *ref = refnum;
  411. return 0;
  412. error:
  413. ClientCloseAux(refnum, client, false);
  414. client->Close();
  415. delete client;
  416. return -1;
  417. }
  418. // Used for server driver clients
  419. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client)
  420. {
  421. JackLog("JackEngine::ClientInternalNew: name = %s\n", name);
  422. int refnum = AllocateRefnum();
  423. if (refnum < 0) {
  424. jack_error("No more refnum available");
  425. return -1;
  426. }
  427. if (!fSynchroTable[refnum]->Allocate(name, 0)) {
  428. jack_error("Cannot allocate synchro");
  429. return -1;
  430. }
  431. if (NotifyAddClient(client, name, refnum) < 0) {
  432. jack_error("Cannot notify add client");
  433. return -1;
  434. }
  435. fClientTable[refnum] = client;
  436. fGraphManager->InitRefNum(refnum);
  437. fEngineTiming->ResetRollingUsecs();
  438. *shared_engine = fEngineControl;
  439. *shared_manager = fGraphManager;
  440. *ref = refnum;
  441. return 0;
  442. }
  443. // Used for externall clients
  444. int JackEngine::ClientExternalClose(int refnum)
  445. {
  446. JackClientInterface* client = fClientTable[refnum];
  447. if (client) {
  448. fEngineControl->fTransport.ResetTimebase(refnum);
  449. int res = ClientCloseAux(refnum, client, true);
  450. client->Close();
  451. delete client;
  452. return res;
  453. } else {
  454. return -1;
  455. }
  456. }
  457. // Used for server internal clients
  458. int JackEngine::ClientInternalClose(int refnum)
  459. {
  460. JackClientInterface* client = fClientTable[refnum];
  461. return (client) ? ClientCloseAux(refnum, client, true) : -1;
  462. }
  463. // Used for drivers that close when the RT thread is stopped
  464. int JackEngine::ClientInternalCloseIm(int refnum)
  465. {
  466. JackClientInterface* client = fClientTable[refnum];
  467. return (client) ? ClientCloseAux(refnum, client, false) : -1;
  468. }
  469. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  470. {
  471. JackLog("JackEngine::ClientCloseAux ref = %ld name = %s\n",
  472. refnum,
  473. (client->GetClientControl()) ? client->GetClientControl()->fName : "No name");
  474. // Remove the client from the table
  475. ReleaseRefnum(refnum);
  476. // Remove ports
  477. fGraphManager->RemoveAllPorts(refnum);
  478. // Wait until next cycle to be sure client is not used anymore
  479. if (wait) {
  480. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  481. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  482. }
  483. }
  484. // Notify running clients
  485. if (client->GetClientControl()) // When called in error cases, client may not be completely allocated
  486. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  487. // Cleanup...
  488. fSynchroTable[refnum]->Destroy();
  489. fEngineTiming->ResetRollingUsecs();
  490. return 0;
  491. }
  492. int JackEngine::ClientActivate(int refnum)
  493. {
  494. JackClientInterface* client = fClientTable[refnum];
  495. assert(fClientTable[refnum]);
  496. JackLog("JackEngine::ClientActivate ref = %ld name = %s\n", refnum, client->GetClientControl()->fName);
  497. fGraphManager->Activate(refnum);
  498. // Wait for graph state change to be effective
  499. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  500. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  501. return -1;
  502. } else {
  503. NotifyActivate(refnum);
  504. return 0;
  505. }
  506. }
  507. // May be called without client
  508. int JackEngine::ClientDeactivate(int refnum)
  509. {
  510. JackClientInterface* client = fClientTable[refnum];
  511. if (client == NULL)
  512. return -1;
  513. JackLog("JackEngine::ClientDeactivate ref = %ld name = %s\n", refnum, client->GetClientControl()->fName);
  514. fGraphManager->Deactivate(refnum);
  515. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  516. // Wait for graph state change to be effective
  517. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  518. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  519. return -1;
  520. } else {
  521. return 0;
  522. }
  523. }
  524. //-----------------
  525. // Port management
  526. //-----------------
  527. int JackEngine::PortRegister(int refnum, const char* name, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  528. {
  529. JackLog("JackEngine::PortRegister ref = %ld name = %s flags = %d buffer_size = %d\n", refnum, name, flags, buffer_size);
  530. assert(fClientTable[refnum]);
  531. *port_index = fGraphManager->AllocatePort(refnum, name, (JackPortFlags)flags);
  532. if (*port_index != NO_PORT) {
  533. NotifyPortRegistation(*port_index, true);
  534. return 0;
  535. } else {
  536. return -1;
  537. }
  538. }
  539. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  540. {
  541. JackLog("JackEngine::PortUnRegister ref = %ld port_index = %ld\n", refnum, port_index);
  542. assert(fClientTable[refnum]);
  543. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  544. NotifyPortRegistation(port_index, false);
  545. return 0;
  546. } else {
  547. return -1;
  548. }
  549. }
  550. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  551. {
  552. JackLog("JackEngine::PortConnect src = %s dst = %s\n", src, dst);
  553. jack_port_id_t port_src, port_dst;
  554. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  555. ? -1
  556. : PortConnect(refnum, port_src, port_dst);
  557. }
  558. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  559. {
  560. JackLog("JackEngine::PortDisconnect src = %s dst = %s\n", src, dst);
  561. jack_port_id_t port_src, port_dst;
  562. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  563. ? -1
  564. : fGraphManager->Disconnect(port_src, port_dst);
  565. }
  566. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  567. {
  568. JackLog("JackEngine::PortConnect src = %d dst = %d\n", src, dst);
  569. JackClientInterface* client;
  570. int ref;
  571. if (fGraphManager->CheckPorts(src, dst) < 0)
  572. return -1;
  573. ref = fGraphManager->GetOutputRefNum(src);
  574. assert(ref >= 0);
  575. client = fClientTable[ref];
  576. assert(client);
  577. if (!client->GetClientControl()->fActive) {
  578. jack_error("Cannot connect ports owned by inactive clients:"
  579. " \"%s\" is not active", client->GetClientControl()->fName);
  580. return -1;
  581. }
  582. ref = fGraphManager->GetInputRefNum(dst);
  583. assert(ref >= 0);
  584. client = fClientTable[ref];
  585. assert(client);
  586. if (!client->GetClientControl()->fActive) {
  587. jack_error("Cannot connect ports owned by inactive clients:"
  588. " \"%s\" is not active", client->GetClientControl()->fName);
  589. return -1;
  590. }
  591. return fGraphManager->Connect(src, dst);
  592. }
  593. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  594. {
  595. JackLog("JackEngine::PortDisconnect src = %d dst = %d\n", src, dst);
  596. if (dst == ALL_PORTS) {
  597. return (fGraphManager->CheckPort(src) < 0)
  598. ? -1
  599. : fGraphManager->DisconnectAll(src);
  600. } else {
  601. return (fGraphManager->CheckPorts(src, dst) < 0)
  602. ? -1
  603. : fGraphManager->Disconnect(src, dst);
  604. }
  605. }
  606. //----------------------
  607. // Transport management
  608. //----------------------
  609. int JackEngine::ReleaseTimebase(int refnum)
  610. {
  611. return fEngineControl->fTransport.ResetTimebase(refnum);
  612. }
  613. int JackEngine::SetTimebaseCallback(int refnum, int conditional)
  614. {
  615. return fEngineControl->fTransport.SetTimebase(refnum, conditional);
  616. }
  617. //-----------
  618. // Debugging
  619. //-----------
  620. void JackEngine::PrintState()
  621. {
  622. std::cout << "Engine State" << std::endl;
  623. for (int i = 0; i < CLIENT_NUM; i++) {
  624. JackClientInterface* client = fClientTable[i];
  625. if (client)
  626. std::cout << "Client : " << client->GetClientControl()->fName << " : " << i << std::endl;
  627. }
  628. //fGraphManager->PrintState();
  629. fEngineTiming->PrintState();
  630. }
  631. } // end of namespace