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.

792 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() < 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. client->Close();
  75. delete client;
  76. }
  77. }
  78. fSignal->Destroy();
  79. return 0;
  80. }
  81. //-----------------------------
  82. // Client ressource management
  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. // Loadable client management
  326. //----------------------------
  327. int JackEngine::GetInternalClientName(int refnum, char* name_res)
  328. {
  329. assert(refnum >= 0 && refnum < CLIENT_NUM);
  330. JackClientInterface* client = fClientTable[refnum];
  331. if (client) {
  332. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  333. return 0;
  334. } else {
  335. return -1;
  336. }
  337. }
  338. int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
  339. {
  340. // Clear status
  341. *status = 0;
  342. for (int i = 0; i < CLIENT_NUM; i++) {
  343. JackClientInterface* client = fClientTable[i];
  344. if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
  345. JackLog("InternalClientHandle found client name = %s ref = %ld\n", client_name, i);
  346. *int_ref = i;
  347. return 0;
  348. }
  349. }
  350. *status |= (JackNoSuchClient | JackFailure);
  351. return -1;
  352. }
  353. int JackEngine::InternalClientUnload(int refnum, int* status)
  354. {
  355. assert(refnum >= 0 && refnum < CLIENT_NUM);
  356. JackClientInterface* client = fClientTable[refnum];
  357. if (client) {
  358. int res = client->Close();
  359. delete client;
  360. *status = 0;
  361. return res;
  362. } else {
  363. *status = (JackNoSuchClient | JackFailure);
  364. return -1;
  365. }
  366. }
  367. //-------------------
  368. // Client management
  369. //-------------------
  370. int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
  371. {
  372. // Clear status
  373. *status = 0;
  374. strcpy(name_res, name);
  375. if (protocol != JACK_PROTOCOL_VERSION) {
  376. *status |= (JackFailure | JackVersionError);
  377. jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
  378. return -1;
  379. }
  380. if (ClientCheckName(name)) {
  381. *status |= JackNameNotUnique;
  382. if (options & JackUseExactName) {
  383. jack_error("cannot create new client; %s already exists", name);
  384. *status |= JackFailure;
  385. return -1;
  386. }
  387. if (GenerateUniqueName(name_res)) {
  388. *status |= JackFailure;
  389. return -1;
  390. }
  391. }
  392. return 0;
  393. }
  394. bool JackEngine::GenerateUniqueName(char* name)
  395. {
  396. int tens, ones;
  397. int length = strlen(name);
  398. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  399. jack_error("%s exists and is too long to make unique", name);
  400. return true; /* failure */
  401. }
  402. /* generate a unique name by appending "-01".."-99" */
  403. name[length++] = '-';
  404. tens = length++;
  405. ones = length++;
  406. name[tens] = '0';
  407. name[ones] = '1';
  408. name[length] = '\0';
  409. while (ClientCheckName(name)) {
  410. if (name[ones] == '9') {
  411. if (name[tens] == '9') {
  412. jack_error("client %s has 99 extra instances already", name);
  413. return true; /* give up */
  414. }
  415. name[tens]++;
  416. name[ones] = '0';
  417. } else {
  418. name[ones]++;
  419. }
  420. }
  421. return false;
  422. }
  423. bool JackEngine::ClientCheckName(const char* name)
  424. {
  425. for (int i = 0; i < CLIENT_NUM; i++) {
  426. JackClientInterface* client = fClientTable[i];
  427. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  428. return true;
  429. }
  430. return false;
  431. }
  432. // Used for external clients
  433. int JackEngine::ClientExternalOpen(const char* name, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  434. {
  435. JackLog("JackEngine::ClientOpen: name = %s \n", name);
  436. int refnum = AllocateRefnum();
  437. if (refnum < 0) {
  438. jack_error("No more refnum available");
  439. return -1;
  440. }
  441. JackExternalClient* client = new JackExternalClient();
  442. if (!fSynchroTable[refnum]->Allocate(name, 0)) {
  443. jack_error("Cannot allocate synchro");
  444. goto error;
  445. }
  446. if (client->Open(name, refnum, shared_client) < 0) {
  447. jack_error("Cannot open client");
  448. goto error;
  449. }
  450. if (!fSignal->TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  451. // Failure if RT thread is not running (problem with the driver...)
  452. jack_error("Driver is not running");
  453. goto error;
  454. }
  455. if (NotifyAddClient(client, name, refnum) < 0) {
  456. jack_error("Cannot notify add client");
  457. goto error;
  458. }
  459. fClientTable[refnum] = client;
  460. fGraphManager->InitRefNum(refnum);
  461. fEngineTiming->ResetRollingUsecs();
  462. *shared_engine = fEngineControl->GetShmIndex();
  463. *shared_graph_manager = fGraphManager->GetShmIndex();
  464. *ref = refnum;
  465. return 0;
  466. error:
  467. ClientCloseAux(refnum, client, false);
  468. client->Close();
  469. delete client;
  470. return -1;
  471. }
  472. // Used for server driver clients
  473. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  474. {
  475. JackLog("JackEngine::ClientInternalNew: name = %s\n", name);
  476. int refnum = AllocateRefnum();
  477. if (refnum < 0) {
  478. jack_error("No more refnum available");
  479. return -1;
  480. }
  481. if (!fSynchroTable[refnum]->Allocate(name, 0)) {
  482. jack_error("Cannot allocate synchro");
  483. return -1;
  484. }
  485. if (wait && !fSignal->TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  486. // Failure if RT thread is not running (problem with the driver...)
  487. jack_error("Driver is not running");
  488. return -1;
  489. }
  490. if (NotifyAddClient(client, name, refnum) < 0) {
  491. jack_error("Cannot notify add client");
  492. return -1;
  493. }
  494. fClientTable[refnum] = client;
  495. fGraphManager->InitRefNum(refnum);
  496. fEngineTiming->ResetRollingUsecs();
  497. *shared_engine = fEngineControl;
  498. *shared_manager = fGraphManager;
  499. *ref = refnum;
  500. return 0;
  501. }
  502. // Used for externall clients
  503. int JackEngine::ClientExternalClose(int refnum)
  504. {
  505. JackClientInterface* client = fClientTable[refnum];
  506. if (client) {
  507. fEngineControl->fTransport.ResetTimebase(refnum);
  508. int res = ClientCloseAux(refnum, client, true);
  509. client->Close();
  510. delete client;
  511. return res;
  512. } else {
  513. return -1;
  514. }
  515. }
  516. // Used for server internal clients or drivers when the RT thread is stopped
  517. int JackEngine::ClientInternalClose(int refnum, bool wait)
  518. {
  519. JackClientInterface* client = fClientTable[refnum];
  520. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  521. }
  522. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  523. {
  524. JackLog("JackEngine::ClientCloseAux ref = %ld name = %s\n",
  525. refnum,
  526. (client->GetClientControl()) ? client->GetClientControl()->fName : "No name");
  527. // Remove the client from the table
  528. ReleaseRefnum(refnum);
  529. // Remove ports
  530. fGraphManager->RemoveAllPorts(refnum);
  531. // Wait until next cycle to be sure client is not used anymore
  532. if (wait) {
  533. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  534. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  535. }
  536. }
  537. // Notify running clients
  538. if (client->GetClientControl()) // When called in error cases, client may not be completely allocated
  539. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  540. // Cleanup...
  541. fSynchroTable[refnum]->Destroy();
  542. fEngineTiming->ResetRollingUsecs();
  543. return 0;
  544. }
  545. int JackEngine::ClientActivate(int refnum)
  546. {
  547. JackClientInterface* client = fClientTable[refnum];
  548. assert(fClientTable[refnum]);
  549. JackLog("JackEngine::ClientActivate ref = %ld name = %s\n", refnum, client->GetClientControl()->fName);
  550. fGraphManager->Activate(refnum);
  551. // Wait for graph state change to be effective
  552. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  553. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  554. return -1;
  555. } else {
  556. NotifyActivate(refnum);
  557. return 0;
  558. }
  559. }
  560. // May be called without client
  561. int JackEngine::ClientDeactivate(int refnum)
  562. {
  563. JackClientInterface* client = fClientTable[refnum];
  564. if (client == NULL)
  565. return -1;
  566. JackLog("JackEngine::ClientDeactivate ref = %ld name = %s\n", refnum, client->GetClientControl()->fName);
  567. fGraphManager->Deactivate(refnum);
  568. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  569. // Wait for graph state change to be effective
  570. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  571. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  572. return -1;
  573. } else {
  574. return 0;
  575. }
  576. }
  577. //-----------------
  578. // Port management
  579. //-----------------
  580. int JackEngine::PortRegister(int refnum, const char* name, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  581. {
  582. JackLog("JackEngine::PortRegister ref = %ld name = %s flags = %d buffer_size = %d\n", refnum, name, flags, buffer_size);
  583. assert(fClientTable[refnum]);
  584. *port_index = fGraphManager->AllocatePort(refnum, name, (JackPortFlags)flags);
  585. if (*port_index != NO_PORT) {
  586. NotifyPortRegistation(*port_index, true);
  587. return 0;
  588. } else {
  589. return -1;
  590. }
  591. }
  592. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  593. {
  594. JackLog("JackEngine::PortUnRegister ref = %ld port_index = %ld\n", refnum, port_index);
  595. assert(fClientTable[refnum]);
  596. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  597. NotifyPortRegistation(port_index, false);
  598. return 0;
  599. } else {
  600. return -1;
  601. }
  602. }
  603. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  604. {
  605. JackLog("JackEngine::PortConnect src = %s dst = %s\n", src, dst);
  606. jack_port_id_t port_src, port_dst;
  607. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  608. ? -1
  609. : PortConnect(refnum, port_src, port_dst);
  610. }
  611. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  612. {
  613. JackLog("JackEngine::PortDisconnect src = %s dst = %s\n", src, dst);
  614. jack_port_id_t port_src, port_dst;
  615. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  616. ? -1
  617. : fGraphManager->Disconnect(port_src, port_dst);
  618. }
  619. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  620. {
  621. JackLog("JackEngine::PortConnect src = %d dst = %d\n", src, dst);
  622. JackClientInterface* client;
  623. int ref;
  624. if (fGraphManager->CheckPorts(src, dst) < 0)
  625. return -1;
  626. ref = fGraphManager->GetOutputRefNum(src);
  627. assert(ref >= 0);
  628. client = fClientTable[ref];
  629. assert(client);
  630. if (!client->GetClientControl()->fActive) {
  631. jack_error("Cannot connect ports owned by inactive clients:"
  632. " \"%s\" is not active", client->GetClientControl()->fName);
  633. return -1;
  634. }
  635. ref = fGraphManager->GetInputRefNum(dst);
  636. assert(ref >= 0);
  637. client = fClientTable[ref];
  638. assert(client);
  639. if (!client->GetClientControl()->fActive) {
  640. jack_error("Cannot connect ports owned by inactive clients:"
  641. " \"%s\" is not active", client->GetClientControl()->fName);
  642. return -1;
  643. }
  644. return fGraphManager->Connect(src, dst);
  645. }
  646. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  647. {
  648. JackLog("JackEngine::PortDisconnect src = %d dst = %d\n", src, dst);
  649. if (dst == ALL_PORTS) {
  650. return (fGraphManager->CheckPort(src) < 0)
  651. ? -1
  652. : fGraphManager->DisconnectAll(src);
  653. } else {
  654. return (fGraphManager->CheckPorts(src, dst) < 0)
  655. ? -1
  656. : fGraphManager->Disconnect(src, dst);
  657. }
  658. }
  659. //----------------------
  660. // Transport management
  661. //----------------------
  662. int JackEngine::ReleaseTimebase(int refnum)
  663. {
  664. return fEngineControl->fTransport.ResetTimebase(refnum);
  665. }
  666. int JackEngine::SetTimebaseCallback(int refnum, int conditional)
  667. {
  668. return fEngineControl->fTransport.SetTimebase(refnum, conditional);
  669. }
  670. } // end of namespace