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.

771 lines
23KB

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