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.

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