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.

810 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. int JackEngine::GetClientPID(const char* name)
  383. {
  384. for (int i = 0; i < CLIENT_NUM; i++) {
  385. JackClientInterface* client = fClientTable[i];
  386. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  387. return client->GetClientControl()->fPID;
  388. }
  389. return 0;
  390. }
  391. // Used for external clients
  392. int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  393. {
  394. jack_log("JackEngine::ClientOpen: name = %s ", name);
  395. int refnum = AllocateRefnum();
  396. if (refnum < 0) {
  397. jack_error("No more refnum available");
  398. return -1;
  399. }
  400. JackExternalClient* client = new JackExternalClient();
  401. if (!fSynchroTable[refnum]->Allocate(name, fEngineControl->fServerName, 0)) {
  402. jack_error("Cannot allocate synchro");
  403. goto error;
  404. }
  405. if (client->Open(name, pid, refnum, shared_client) < 0) {
  406. jack_error("Cannot open client");
  407. goto error;
  408. }
  409. if (!fSignal->TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  410. // Failure if RT thread is not running (problem with the driver...)
  411. jack_error("Driver is not running");
  412. goto error;
  413. }
  414. fClientTable[refnum] = client;
  415. if (NotifyAddClient(client, name, refnum) < 0) {
  416. jack_error("Cannot notify add client");
  417. goto error;
  418. }
  419. fGraphManager->InitRefNum(refnum);
  420. fEngineControl->ResetRollingUsecs();
  421. *shared_engine = fEngineControl->GetShmIndex();
  422. *shared_graph_manager = fGraphManager->GetShmIndex();
  423. *ref = refnum;
  424. return 0;
  425. error:
  426. // Cleanup...
  427. fSynchroTable[refnum]->Destroy();
  428. fClientTable[refnum] = 0;
  429. client->Close();
  430. delete client;
  431. return -1;
  432. }
  433. // Used for server driver clients
  434. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  435. {
  436. jack_log("JackEngine::ClientInternalNew: name = %s", name);
  437. int refnum = AllocateRefnum();
  438. if (refnum < 0) {
  439. jack_error("No more refnum available");
  440. goto error;
  441. }
  442. if (!fSynchroTable[refnum]->Allocate(name, fEngineControl->fServerName, 0)) {
  443. jack_error("Cannot allocate synchro");
  444. goto error;
  445. }
  446. if (wait && !fSignal->TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  447. // Failure if RT thread is not running (problem with the driver...)
  448. jack_error("Driver is not running");
  449. goto error;
  450. }
  451. fClientTable[refnum] = client;
  452. if (NotifyAddClient(client, name, refnum) < 0) {
  453. jack_error("Cannot notify add client");
  454. goto error;
  455. }
  456. fGraphManager->InitRefNum(refnum);
  457. fEngineControl->ResetRollingUsecs();
  458. *shared_engine = fEngineControl;
  459. *shared_manager = fGraphManager;
  460. *ref = refnum;
  461. return 0;
  462. error:
  463. // Cleanup...
  464. fSynchroTable[refnum]->Destroy();
  465. fClientTable[refnum] = 0;
  466. return -1;
  467. }
  468. // Used for external clients
  469. int JackEngine::ClientExternalClose(int refnum)
  470. {
  471. JackClientInterface* client = fClientTable[refnum];
  472. if (client) {
  473. fEngineControl->fTransport.ResetTimebase(refnum);
  474. int res = ClientCloseAux(refnum, client, true);
  475. client->Close();
  476. delete client;
  477. return res;
  478. } else {
  479. return -1;
  480. }
  481. }
  482. // Used for server internal clients or drivers when the RT thread is stopped
  483. int JackEngine::ClientInternalClose(int refnum, bool wait)
  484. {
  485. JackClientInterface* client = fClientTable[refnum];
  486. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  487. }
  488. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  489. {
  490. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  491. // Unregister all ports ==> notifications are sent
  492. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  493. int i;
  494. fGraphManager->GetInputPorts(refnum, ports);
  495. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  496. PortUnRegister(refnum, ports[i]);
  497. }
  498. fGraphManager->GetOutputPorts(refnum, ports);
  499. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  500. PortUnRegister(refnum, ports[i]);
  501. }
  502. // Remove the client from the table
  503. ReleaseRefnum(refnum);
  504. // Remove all ports
  505. fGraphManager->RemoveAllPorts(refnum);
  506. // Wait until next cycle to be sure client is not used anymore
  507. if (wait) {
  508. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  509. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  510. }
  511. }
  512. // Notify running clients
  513. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  514. // Cleanup...
  515. fSynchroTable[refnum]->Destroy();
  516. fEngineControl->ResetRollingUsecs();
  517. return 0;
  518. }
  519. int JackEngine::ClientActivate(int refnum, bool state)
  520. {
  521. JackClientInterface* client = fClientTable[refnum];
  522. assert(fClientTable[refnum]);
  523. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  524. if (state)
  525. fGraphManager->Activate(refnum);
  526. // Wait for graph state change to be effective
  527. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  528. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  529. return -1;
  530. } else {
  531. NotifyActivate(refnum);
  532. return 0;
  533. }
  534. }
  535. // May be called without client
  536. int JackEngine::ClientDeactivate(int refnum)
  537. {
  538. JackClientInterface* client = fClientTable[refnum];
  539. if (client == NULL)
  540. return -1;
  541. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  542. // Disconnect all ports ==> notifications are sent
  543. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  544. int i;
  545. fGraphManager->GetInputPorts(refnum, ports);
  546. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  547. PortDisconnect(refnum, ports[i], ALL_PORTS);
  548. }
  549. fGraphManager->GetOutputPorts(refnum, ports);
  550. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  551. PortDisconnect(refnum, ports[i], ALL_PORTS);
  552. }
  553. fGraphManager->Deactivate(refnum);
  554. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  555. // Wait for graph state change to be effective
  556. if (!fSignal->TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  557. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  558. return -1;
  559. } else {
  560. return 0;
  561. }
  562. }
  563. //-----------------
  564. // Port management
  565. //-----------------
  566. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, unsigned int* port_index)
  567. {
  568. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  569. assert(fClientTable[refnum]);
  570. // Check if port name already exists
  571. if (fGraphManager->GetPort(name) != NO_PORT) {
  572. jack_error("port_name \"%s\" already exists", name);
  573. return -1;
  574. }
  575. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  576. if (*port_index != NO_PORT) {
  577. NotifyPortRegistation(*port_index, true);
  578. return 0;
  579. } else {
  580. return -1;
  581. }
  582. }
  583. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  584. {
  585. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  586. assert(fClientTable[refnum]);
  587. // Disconnect port ==> notification is sent
  588. PortDisconnect(refnum, port_index, ALL_PORTS);
  589. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  590. NotifyPortRegistation(port_index, false);
  591. return 0;
  592. } else {
  593. return -1;
  594. }
  595. }
  596. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  597. {
  598. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  599. jack_port_id_t port_src, port_dst;
  600. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  601. ? -1
  602. : PortConnect(refnum, port_src, port_dst);
  603. }
  604. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  605. {
  606. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  607. JackClientInterface* client;
  608. int ref;
  609. if (fGraphManager->CheckPorts(src, dst) < 0)
  610. return -1;
  611. ref = fGraphManager->GetOutputRefNum(src);
  612. assert(ref >= 0);
  613. client = fClientTable[ref];
  614. assert(client);
  615. if (!client->GetClientControl()->fActive) {
  616. jack_error("Cannot connect ports owned by inactive clients:"
  617. " \"%s\" is not active", client->GetClientControl()->fName);
  618. return -1;
  619. }
  620. ref = fGraphManager->GetInputRefNum(dst);
  621. assert(ref >= 0);
  622. client = fClientTable[ref];
  623. assert(client);
  624. if (!client->GetClientControl()->fActive) {
  625. jack_error("Cannot connect ports owned by inactive clients:"
  626. " \"%s\" is not active", client->GetClientControl()->fName);
  627. return -1;
  628. }
  629. int res = fGraphManager->Connect(src, dst);
  630. if (res == 0)
  631. NotifyPortConnect(src, dst, true);
  632. return res;
  633. }
  634. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  635. {
  636. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  637. jack_port_id_t port_src, port_dst;
  638. if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
  639. return -1;
  640. } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
  641. NotifyPortConnect(port_src, port_dst, false);
  642. return 0;
  643. } else {
  644. return -1;
  645. }
  646. }
  647. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  648. {
  649. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  650. if (dst == ALL_PORTS) {
  651. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  652. fGraphManager->GetConnections(src, connections);
  653. // Notifications
  654. JackPort* port = fGraphManager->GetPort(src);
  655. if (port->GetFlags() & JackPortIsOutput) {
  656. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  657. jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
  658. NotifyPortConnect(src, connections[i], false);
  659. }
  660. } else {
  661. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  662. jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
  663. NotifyPortConnect(connections[i], src, false);
  664. }
  665. }
  666. return fGraphManager->DisconnectAll(src);
  667. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  668. return -1;
  669. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  670. // Notifications
  671. NotifyPortConnect(src, dst, false);
  672. return 0;
  673. } else {
  674. return -1;
  675. }
  676. }
  677. } // end of namespace