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.

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