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.

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