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.

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