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.

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