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.

827 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. #define AssertRefnum(ref) assert(ref >= 0 && ref < CLIENT_NUM);
  36. JackEngine::JackEngine(JackGraphManager* manager,
  37. JackSynchro* table,
  38. JackEngineControl* control)
  39. {
  40. fGraphManager = manager;
  41. fSynchroTable = table;
  42. fEngineControl = control;
  43. for (int i = 0; i < CLIENT_NUM; i++)
  44. fClientTable[i] = NULL;
  45. }
  46. JackEngine::~JackEngine()
  47. {
  48. jack_log("JackEngine::~JackEngine");
  49. }
  50. int JackEngine::Open()
  51. {
  52. jack_log("JackEngine::Open");
  53. // Open audio thread => request thread communication channel
  54. if (fChannel.Open(fEngineControl->fServerName) < 0) {
  55. jack_error("Cannot connect to server");
  56. return -1;
  57. } else {
  58. return 0;
  59. }
  60. }
  61. int JackEngine::Close()
  62. {
  63. jack_log("JackEngine::Close");
  64. fChannel.Close();
  65. // Close remaining clients (RT is stopped)
  66. for (int i = 0; i < CLIENT_NUM; i++) {
  67. JackClientInterface* client = fClientTable[i];
  68. if (client) {
  69. jack_log("JackEngine::Close client = %s", client->GetClientControl()->fName);
  70. client->Close();
  71. delete 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 cur_cycle_begin)
  114. {
  115. fLastSwitchUsecs = cur_cycle_begin;
  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 cur_cycle_begin)
  121. {
  122. if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
  123. CheckXRun(cur_cycle_begin);
  124. fGraphManager->RunCurrentGraph();
  125. }
  126. bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  127. {
  128. bool res = true;
  129. // Cycle begin
  130. fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
  131. // Graph
  132. if (fGraphManager->IsFinishedGraph()) {
  133. ProcessNext(cur_cycle_begin);
  134. res = true;
  135. } else {
  136. jack_log("Process: graph not finished!");
  137. if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  138. jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  139. ProcessNext(cur_cycle_begin);
  140. res = true;
  141. } else {
  142. jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  143. ProcessCurrent(cur_cycle_begin);
  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 buffer_size)
  253. {
  254. NotifyClients(kBufferSizeCallback, true, buffer_size, 0);
  255. }
  256. void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
  257. {
  258. NotifyClients(kSampleRateCallback, true, sample_rate, 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. AssertRefnum(refnum);
  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. AssertRefnum(refnum);
  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. AssertRefnum(refnum);
  476. JackClientInterface* client = fClientTable[refnum];
  477. if (client) {
  478. fEngineControl->fTransport.ResetTimebase(refnum);
  479. int res = ClientCloseAux(refnum, client, true);
  480. client->Close();
  481. delete client;
  482. return res;
  483. } else {
  484. return -1;
  485. }
  486. }
  487. // Used for server internal clients or drivers when the RT thread is stopped
  488. int JackEngine::ClientInternalClose(int refnum, bool wait)
  489. {
  490. AssertRefnum(refnum);
  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. AssertRefnum(refnum);
  528. JackClientInterface* client = fClientTable[refnum];
  529. assert(fClientTable[refnum]);
  530. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  531. if (state)
  532. fGraphManager->Activate(refnum);
  533. // Wait for graph state change to be effective
  534. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  535. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  536. return -1;
  537. } else {
  538. NotifyActivate(refnum);
  539. return 0;
  540. }
  541. }
  542. // May be called without client
  543. int JackEngine::ClientDeactivate(int refnum)
  544. {
  545. AssertRefnum(refnum);
  546. JackClientInterface* client = fClientTable[refnum];
  547. if (client == NULL)
  548. return -1;
  549. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  550. // Disconnect all ports ==> notifications are sent
  551. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  552. int i;
  553. fGraphManager->GetInputPorts(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->GetOutputPorts(refnum, ports);
  558. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  559. PortDisconnect(refnum, ports[i], ALL_PORTS);
  560. }
  561. fGraphManager->Deactivate(refnum);
  562. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  563. // Wait for graph state change to be effective
  564. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  565. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  566. return -1;
  567. } else {
  568. return 0;
  569. }
  570. }
  571. //-----------------
  572. // Port management
  573. //-----------------
  574. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, unsigned int* port_index)
  575. {
  576. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  577. AssertRefnum(refnum);
  578. assert(fClientTable[refnum]);
  579. // Check if port name already exists
  580. if (fGraphManager->GetPort(name) != NO_PORT) {
  581. jack_error("port_name \"%s\" already exists", name);
  582. return -1;
  583. }
  584. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  585. if (*port_index != NO_PORT) {
  586. NotifyPortRegistation(*port_index, true);
  587. return 0;
  588. } else {
  589. return -1;
  590. }
  591. }
  592. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  593. {
  594. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  595. AssertRefnum(refnum);
  596. assert(fClientTable[refnum]);
  597. // Disconnect port ==> notification is sent
  598. PortDisconnect(refnum, port_index, ALL_PORTS);
  599. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  600. NotifyPortRegistation(port_index, false);
  601. return 0;
  602. } else {
  603. return -1;
  604. }
  605. }
  606. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  607. {
  608. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  609. jack_port_id_t port_src, port_dst;
  610. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  611. ? -1
  612. : PortConnect(refnum, port_src, port_dst);
  613. }
  614. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  615. {
  616. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  617. AssertRefnum(refnum);
  618. JackClientInterface* client;
  619. int ref;
  620. if (fGraphManager->CheckPorts(src, dst) < 0)
  621. return -1;
  622. ref = fGraphManager->GetOutputRefNum(src);
  623. assert(ref >= 0);
  624. client = fClientTable[ref];
  625. assert(client);
  626. if (!client->GetClientControl()->fActive) {
  627. jack_error("Cannot connect ports owned by inactive clients:"
  628. " \"%s\" is not active", client->GetClientControl()->fName);
  629. return -1;
  630. }
  631. ref = fGraphManager->GetInputRefNum(dst);
  632. assert(ref >= 0);
  633. client = fClientTable[ref];
  634. assert(client);
  635. if (!client->GetClientControl()->fActive) {
  636. jack_error("Cannot connect ports owned by inactive clients:"
  637. " \"%s\" is not active", client->GetClientControl()->fName);
  638. return -1;
  639. }
  640. int res = fGraphManager->Connect(src, dst);
  641. if (res == 0)
  642. NotifyPortConnect(src, dst, true);
  643. return res;
  644. }
  645. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  646. {
  647. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  648. AssertRefnum(refnum);
  649. jack_port_id_t port_src, port_dst;
  650. if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
  651. return -1;
  652. } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
  653. NotifyPortConnect(port_src, port_dst, false);
  654. return 0;
  655. } else {
  656. return -1;
  657. }
  658. }
  659. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  660. {
  661. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  662. AssertRefnum(refnum);
  663. if (dst == ALL_PORTS) {
  664. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  665. fGraphManager->GetConnections(src, connections);
  666. // Notifications
  667. JackPort* port = fGraphManager->GetPort(src);
  668. if (port->GetFlags() & JackPortIsOutput) {
  669. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  670. jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
  671. NotifyPortConnect(src, connections[i], false);
  672. }
  673. } else {
  674. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  675. jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
  676. NotifyPortConnect(connections[i], src, false);
  677. }
  678. }
  679. return fGraphManager->DisconnectAll(src);
  680. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  681. return -1;
  682. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  683. // Notifications
  684. NotifyPortConnect(src, dst, false);
  685. return 0;
  686. } else {
  687. return -1;
  688. }
  689. }
  690. } // end of namespace