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.

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