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
24KB

  1. /*
  2. Copyright (C) 2004-2008 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. jack_log("JackEngine::Open");
  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. jack_log("JackEngine::Close");
  63. fChannel->Close();
  64. for (int i = 0; i < CLIENT_NUM; i++) {
  65. /*
  66. Can only delete clients that where loaded using "jack_internal_client_load" (and not properly unloaded using "jack_internal_client_unload"...)
  67. */
  68. JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i]);
  69. if (loadable_client) {
  70. jack_log("JackEngine::Close delete loadable client %ld", i);
  71. delete loadable_client;
  72. fClientTable[i] = NULL;
  73. }
  74. }
  75. fSignal->Destroy();
  76. return 0;
  77. }
  78. //-----------------------------
  79. // Client ressource management
  80. //-----------------------------
  81. int JackEngine::AllocateRefnum()
  82. {
  83. for (int i = 0; i < CLIENT_NUM; i++) {
  84. if (!fClientTable[i]) {
  85. jack_log("JackEngine::AllocateRefNum ref = %ld", i);
  86. return i;
  87. }
  88. }
  89. return -1;
  90. }
  91. void JackEngine::ReleaseRefnum(int ref)
  92. {
  93. fClientTable[ref] = NULL;
  94. if (fEngineControl->fTemporary) {
  95. int i;
  96. for (i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  97. if (fClientTable[i])
  98. break;
  99. }
  100. if (i == CLIENT_NUM) {
  101. // last client and temporay case: quit the server
  102. jack_log("JackEngine::ReleaseRefnum server quit");
  103. fEngineControl->fTemporary = false;
  104. #ifndef WIN32
  105. exit(0);
  106. #endif
  107. }
  108. }
  109. }
  110. //------------------
  111. // Graph management
  112. //------------------
  113. void JackEngine::ProcessNext(jack_time_t callback_usecs)
  114. {
  115. fLastSwitchUsecs = callback_usecs;
  116. if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
  117. fChannel->Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
  118. fSignal->SignalAll(); // Signal for threads waiting for next cycle
  119. }
  120. void JackEngine::ProcessCurrent(jack_time_t callback_usecs)
  121. {
  122. if (callback_usecs < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
  123. CheckXRun(callback_usecs);
  124. fGraphManager->RunCurrentGraph();
  125. }
  126. bool JackEngine::Process(jack_time_t callback_usecs)
  127. {
  128. bool res = true;
  129. // Cycle begin
  130. fEngineControl->CycleBegin(fClientTable, fGraphManager, callback_usecs);
  131. // Graph
  132. if (fGraphManager->IsFinishedGraph()) {
  133. ProcessNext(callback_usecs);
  134. res = true;
  135. } else {
  136. jack_log("Process: graph not finished!");
  137. if (callback_usecs > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  138. jack_log("Process: switch to next state delta = %ld", long(callback_usecs - fLastSwitchUsecs));
  139. ProcessNext(callback_usecs);
  140. res = true;
  141. } else {
  142. jack_log("Process: waiting to switch delta = %ld", 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->Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  166. }
  167. if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
  168. jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
  169. fChannel->Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  170. }
  171. }
  172. }
  173. }
  174. //---------------
  175. // Notifications
  176. //---------------
  177. void JackEngine::NotifyClient(int refnum, int event, int sync, int value1, int value2)
  178. {
  179. JackClientInterface* client = fClientTable[refnum];
  180. // The client may be notified by the RT thread while closing
  181. if (!client) {
  182. jack_log("JackEngine::NotifyClient: client not available anymore");
  183. } else if (client->GetClientControl()->fCallback[event]) {
  184. if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
  185. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  186. } else {
  187. jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
  188. }
  189. }
  190. void JackEngine::NotifyClients(int event, int sync, int value1, int value2)
  191. {
  192. for (int i = 0; i < CLIENT_NUM; i++) {
  193. JackClientInterface* client = fClientTable[i];
  194. if (client) {
  195. if (client->GetClientControl()->fCallback[event]) {
  196. if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
  197. jack_error("NotifyClient fails name = %s event = %ld = val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  198. } else {
  199. jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
  200. }
  201. }
  202. }
  203. }
  204. int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
  205. {
  206. // Notify existing clients of the new client and new client of existing clients.
  207. for (int i = 0; i < CLIENT_NUM; i++) {
  208. JackClientInterface* old_client = fClientTable[i];
  209. if (old_client) {
  210. if (old_client->ClientNotify(refnum, name, kAddClient, true, 0, 0) < 0) {
  211. jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
  212. return -1;
  213. }
  214. if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, 0, 0) < 0) {
  215. jack_error("NotifyAddClient new_client fails name = %s", name);
  216. return -1;
  217. }
  218. }
  219. }
  220. return 0;
  221. }
  222. void JackEngine::NotifyRemoveClient(const char* name, int refnum)
  223. {
  224. // Notify existing clients (including the one beeing suppressed) of the removed client
  225. for (int i = 0; i < CLIENT_NUM; i++) {
  226. JackClientInterface* client = fClientTable[i];
  227. if (client) {
  228. client->ClientNotify(refnum, name, kRemoveClient, true, 0, 0);
  229. }
  230. }
  231. }
  232. // Coming from the driver
  233. void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
  234. {
  235. // Use the audio thread => request thread communication channel
  236. fEngineControl->ResetFrameTime(callback_usecs);
  237. fEngineControl->NotifyXRun(delayed_usecs);
  238. fChannel->Notify(ALL_CLIENTS, kXRunCallback, 0);
  239. }
  240. void JackEngine::NotifyXRun(int refnum)
  241. {
  242. if (refnum == ALL_CLIENTS) {
  243. NotifyClients(kXRunCallback, false, 0, 0);
  244. } else {
  245. NotifyClient(refnum, kXRunCallback, false, 0, 0);
  246. }
  247. }
  248. void JackEngine::NotifyGraphReorder()
  249. {
  250. NotifyClients(kGraphOrderCallback, false, 0, 0);
  251. }
  252. void JackEngine::NotifyBufferSize(jack_nframes_t nframes)
  253. {
  254. NotifyClients(kBufferSizeCallback, true, nframes, 0);
  255. }
  256. void JackEngine::NotifyFreewheel(bool onoff)
  257. {
  258. fEngineControl->fRealTime = !onoff;
  259. NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, 0, 0);
  260. }
  261. void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
  262. {
  263. NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, port_index, 0);
  264. }
  265. void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
  266. {
  267. NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, src, dst);
  268. }
  269. void JackEngine::NotifyActivate(int refnum)
  270. {
  271. NotifyClient(refnum, kActivateClient, true, 0, 0);
  272. }
  273. //----------------------------
  274. // Loadable client management
  275. //----------------------------
  276. int JackEngine::GetInternalClientName(int refnum, char* name_res)
  277. {
  278. assert(refnum >= 0 && refnum < CLIENT_NUM);
  279. JackClientInterface* client = fClientTable[refnum];
  280. if (client) {
  281. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  282. return 0;
  283. } else {
  284. return -1;
  285. }
  286. }
  287. int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
  288. {
  289. // Clear status
  290. *status = 0;
  291. for (int i = 0; i < CLIENT_NUM; i++) {
  292. JackClientInterface* client = fClientTable[i];
  293. if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
  294. jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
  295. *int_ref = i;
  296. return 0;
  297. }
  298. }
  299. *status |= (JackNoSuchClient | JackFailure);
  300. return -1;
  301. }
  302. int JackEngine::InternalClientUnload(int refnum, int* status)
  303. {
  304. assert(refnum >= 0 && refnum < CLIENT_NUM);
  305. JackClientInterface* client = fClientTable[refnum];
  306. if (client) {
  307. int res = client->Close();
  308. delete client;
  309. *status = 0;
  310. return res;
  311. } else {
  312. *status = (JackNoSuchClient | JackFailure);
  313. return -1;
  314. }
  315. }
  316. //-------------------
  317. // Client management
  318. //-------------------
  319. int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
  320. {
  321. // Clear status
  322. *status = 0;
  323. strcpy(name_res, name);
  324. jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
  325. if (protocol != JACK_PROTOCOL_VERSION) {
  326. *status |= (JackFailure | JackVersionError);
  327. jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
  328. return -1;
  329. }
  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. jack_log("JackEngine::ClientOpen: name = %s ", 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, fEngineControl->fServerName, 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(DRIVER_OPEN_TIMEOUT * 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. fEngineControl->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, bool wait)
  424. {
  425. jack_log("JackEngine::ClientInternalNew: name = %s", 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, fEngineControl->fServerName, 0)) {
  432. jack_error("Cannot allocate synchro");
  433. return -1;
  434. }
  435. if (wait && !fSignal->TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  436. // Failure if RT thread is not running (problem with the driver...)
  437. jack_error("Driver is not running");
  438. return -1;
  439. }
  440. if (NotifyAddClient(client, name, refnum) < 0) {
  441. jack_error("Cannot notify add client");
  442. return -1;
  443. }
  444. fClientTable[refnum] = client;
  445. fGraphManager->InitRefNum(refnum);
  446. fEngineControl->ResetRollingUsecs();
  447. *shared_engine = fEngineControl;
  448. *shared_manager = fGraphManager;
  449. *ref = refnum;
  450. return 0;
  451. }
  452. // Used for external clients
  453. int JackEngine::ClientExternalClose(int refnum)
  454. {
  455. JackClientInterface* client = fClientTable[refnum];
  456. if (client) {
  457. fEngineControl->fTransport.ResetTimebase(refnum);
  458. int res = ClientCloseAux(refnum, client, true);
  459. client->Close();
  460. delete client;
  461. return res;
  462. } else {
  463. return -1;
  464. }
  465. }
  466. // Used for server internal clients or drivers when the RT thread is stopped
  467. int JackEngine::ClientInternalClose(int refnum, bool wait)
  468. {
  469. JackClientInterface* client = fClientTable[refnum];
  470. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  471. }
  472. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  473. {
  474. jack_log("JackEngine::ClientCloseAux ref = %ld name = %s",
  475. refnum,
  476. (client->GetClientControl()) ? client->GetClientControl()->fName : "No name");
  477. // Unregister all ports ==> notifications are sent
  478. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  479. int i;
  480. fGraphManager->GetInputPorts(refnum, ports);
  481. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  482. PortUnRegister(refnum, ports[i]);
  483. }
  484. fGraphManager->GetOutputPorts(refnum, ports);
  485. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  486. PortUnRegister(refnum, ports[i]);
  487. }
  488. // Remove the client from the table
  489. ReleaseRefnum(refnum);
  490. // Remove all ports
  491. fGraphManager->RemoveAllPorts(refnum);
  492. // Wait until next cycle to be sure client is not used anymore
  493. if (wait) {
  494. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  495. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  496. }
  497. }
  498. // Notify running clients
  499. if (client->GetClientControl()) // When called in error cases, client may not be completely allocated
  500. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  501. // Cleanup...
  502. fSynchroTable[refnum]->Destroy();
  503. fEngineControl->ResetRollingUsecs();
  504. return 0;
  505. }
  506. int JackEngine::ClientActivate(int refnum, bool state)
  507. {
  508. JackClientInterface* client = fClientTable[refnum];
  509. assert(fClientTable[refnum]);
  510. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  511. if (state)
  512. fGraphManager->Activate(refnum);
  513. // Wait for graph state change to be effective
  514. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  515. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  516. return -1;
  517. } else {
  518. NotifyActivate(refnum);
  519. return 0;
  520. }
  521. }
  522. // May be called without client
  523. int JackEngine::ClientDeactivate(int refnum)
  524. {
  525. JackClientInterface* client = fClientTable[refnum];
  526. if (client == NULL)
  527. return -1;
  528. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  529. // Disconnect all ports ==> notifications are sent
  530. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  531. int i;
  532. fGraphManager->GetInputPorts(refnum, ports);
  533. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  534. PortDisconnect(refnum, ports[i], ALL_PORTS);
  535. }
  536. fGraphManager->GetOutputPorts(refnum, ports);
  537. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  538. PortDisconnect(refnum, ports[i], ALL_PORTS);
  539. }
  540. fGraphManager->Deactivate(refnum);
  541. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  542. // Wait for graph state change to be effective
  543. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  544. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  545. return -1;
  546. } else {
  547. return 0;
  548. }
  549. }
  550. //-----------------
  551. // Port management
  552. //-----------------
  553. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, unsigned int* port_index)
  554. {
  555. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  556. assert(fClientTable[refnum]);
  557. // Check if port name already exists
  558. if (fGraphManager->GetPort(name) != NO_PORT) {
  559. jack_error("port_name \"%s\" already exists", name);
  560. return -1;
  561. }
  562. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  563. if (*port_index != NO_PORT) {
  564. NotifyPortRegistation(*port_index, true);
  565. return 0;
  566. } else {
  567. return -1;
  568. }
  569. }
  570. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  571. {
  572. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  573. assert(fClientTable[refnum]);
  574. // Disconnect port ==> notification is sent
  575. PortDisconnect(refnum, port_index, ALL_PORTS);
  576. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  577. NotifyPortRegistation(port_index, false);
  578. return 0;
  579. } else {
  580. return -1;
  581. }
  582. }
  583. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  584. {
  585. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  586. jack_port_id_t port_src, port_dst;
  587. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  588. ? -1
  589. : PortConnect(refnum, port_src, port_dst);
  590. }
  591. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  592. {
  593. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  594. JackClientInterface* client;
  595. int ref;
  596. if (fGraphManager->CheckPorts(src, dst) < 0)
  597. return -1;
  598. ref = fGraphManager->GetOutputRefNum(src);
  599. assert(ref >= 0);
  600. client = fClientTable[ref];
  601. assert(client);
  602. if (!client->GetClientControl()->fActive) {
  603. jack_error("Cannot connect ports owned by inactive clients:"
  604. " \"%s\" is not active", client->GetClientControl()->fName);
  605. return -1;
  606. }
  607. ref = fGraphManager->GetInputRefNum(dst);
  608. assert(ref >= 0);
  609. client = fClientTable[ref];
  610. assert(client);
  611. if (!client->GetClientControl()->fActive) {
  612. jack_error("Cannot connect ports owned by inactive clients:"
  613. " \"%s\" is not active", client->GetClientControl()->fName);
  614. return -1;
  615. }
  616. int res = fGraphManager->Connect(src, dst);
  617. if (res == 0)
  618. NotifyPortConnect(src, dst, true);
  619. return res;
  620. }
  621. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  622. {
  623. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  624. jack_port_id_t port_src, port_dst;
  625. if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
  626. return -1;
  627. } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
  628. NotifyPortConnect(port_src, port_dst, false);
  629. return 0;
  630. } else {
  631. return -1;
  632. }
  633. }
  634. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  635. {
  636. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  637. if (dst == ALL_PORTS) {
  638. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  639. fGraphManager->GetConnections(src, connections);
  640. // Notifications
  641. JackPort* port = fGraphManager->GetPort(src);
  642. if (port->GetFlags() & JackPortIsOutput) {
  643. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  644. jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
  645. NotifyPortConnect(src, connections[i], false);
  646. }
  647. } else {
  648. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  649. jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
  650. NotifyPortConnect(connections[i], src, false);
  651. }
  652. }
  653. return fGraphManager->DisconnectAll(src);
  654. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  655. return -1;
  656. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  657. // Notifications
  658. NotifyPortConnect(src, dst, false);
  659. return 0;
  660. } else {
  661. return -1;
  662. }
  663. }
  664. } // end of namespace