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.

812 lines
25KB

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