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.

793 lines
23KB

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