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 "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 buffer_size)
  254. {
  255. NotifyClients(kBufferSizeCallback, true, buffer_size, 0);
  256. }
  257. void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
  258. {
  259. NotifyClients(kSampleRateCallback, true, sample_rate, 0);
  260. }
  261. void JackEngine::NotifyFreewheel(bool onoff)
  262. {
  263. fEngineControl->fRealTime = !onoff;
  264. NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, 0, 0);
  265. }
  266. void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
  267. {
  268. NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, port_index, 0);
  269. }
  270. void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
  271. {
  272. NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, src, dst);
  273. }
  274. void JackEngine::NotifyActivate(int refnum)
  275. {
  276. NotifyClient(refnum, kActivateClient, true, 0, 0);
  277. }
  278. //----------------------------
  279. // Loadable client management
  280. //----------------------------
  281. int JackEngine::GetInternalClientName(int refnum, char* name_res)
  282. {
  283. assert(refnum >= 0 && refnum < CLIENT_NUM);
  284. JackClientInterface* client = fClientTable[refnum];
  285. if (client) {
  286. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  287. return 0;
  288. } else {
  289. return -1;
  290. }
  291. }
  292. int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
  293. {
  294. // Clear status
  295. *status = 0;
  296. for (int i = 0; i < CLIENT_NUM; i++) {
  297. JackClientInterface* client = fClientTable[i];
  298. if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
  299. jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
  300. *int_ref = i;
  301. return 0;
  302. }
  303. }
  304. *status |= (JackNoSuchClient | JackFailure);
  305. return -1;
  306. }
  307. int JackEngine::InternalClientUnload(int refnum, int* status)
  308. {
  309. assert(refnum >= 0 && refnum < CLIENT_NUM);
  310. JackClientInterface* client = fClientTable[refnum];
  311. if (client) {
  312. int res = client->Close();
  313. delete client;
  314. *status = 0;
  315. return res;
  316. } else {
  317. *status = (JackNoSuchClient | JackFailure);
  318. return -1;
  319. }
  320. }
  321. //-------------------
  322. // Client management
  323. //-------------------
  324. int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
  325. {
  326. // Clear status
  327. *status = 0;
  328. strcpy(name_res, name);
  329. jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
  330. if (protocol != JACK_PROTOCOL_VERSION) {
  331. *status |= (JackFailure | JackVersionError);
  332. jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
  333. return -1;
  334. }
  335. if (ClientCheckName(name)) {
  336. *status |= JackNameNotUnique;
  337. if (options & JackUseExactName) {
  338. jack_error("cannot create new client; %s already exists", name);
  339. *status |= JackFailure;
  340. return -1;
  341. }
  342. if (GenerateUniqueName(name_res)) {
  343. *status |= JackFailure;
  344. return -1;
  345. }
  346. }
  347. return 0;
  348. }
  349. bool JackEngine::GenerateUniqueName(char* name)
  350. {
  351. int tens, ones;
  352. int length = strlen(name);
  353. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  354. jack_error("%s exists and is too long to make unique", name);
  355. return true; /* failure */
  356. }
  357. /* generate a unique name by appending "-01".."-99" */
  358. name[length++] = '-';
  359. tens = length++;
  360. ones = length++;
  361. name[tens] = '0';
  362. name[ones] = '1';
  363. name[length] = '\0';
  364. while (ClientCheckName(name)) {
  365. if (name[ones] == '9') {
  366. if (name[tens] == '9') {
  367. jack_error("client %s has 99 extra instances already", name);
  368. return true; /* give up */
  369. }
  370. name[tens]++;
  371. name[ones] = '0';
  372. } else {
  373. name[ones]++;
  374. }
  375. }
  376. return false;
  377. }
  378. bool JackEngine::ClientCheckName(const char* name)
  379. {
  380. for (int i = 0; i < CLIENT_NUM; i++) {
  381. JackClientInterface* client = fClientTable[i];
  382. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  383. return true;
  384. }
  385. return false;
  386. }
  387. int JackEngine::GetClientPID(const char* name)
  388. {
  389. for (int i = 0; i < CLIENT_NUM; i++) {
  390. JackClientInterface* client = fClientTable[i];
  391. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  392. return client->GetClientControl()->fPID;
  393. }
  394. return 0;
  395. }
  396. // Used for external clients
  397. int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  398. {
  399. jack_log("JackEngine::ClientOpen: name = %s ", name);
  400. int refnum = AllocateRefnum();
  401. if (refnum < 0) {
  402. jack_error("No more refnum available");
  403. return -1;
  404. }
  405. JackExternalClient* client = new JackExternalClient();
  406. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  407. jack_error("Cannot allocate synchro");
  408. goto error;
  409. }
  410. if (client->Open(name, pid, refnum, shared_client) < 0) {
  411. jack_error("Cannot open client");
  412. goto error;
  413. }
  414. if (!fSignal.TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  415. // Failure if RT thread is not running (problem with the driver...)
  416. jack_error("Driver is not running");
  417. goto error;
  418. }
  419. fClientTable[refnum] = client;
  420. if (NotifyAddClient(client, name, refnum) < 0) {
  421. jack_error("Cannot notify add client");
  422. goto error;
  423. }
  424. fGraphManager->InitRefNum(refnum);
  425. fEngineControl->ResetRollingUsecs();
  426. *shared_engine = fEngineControl->GetShmIndex();
  427. *shared_graph_manager = fGraphManager->GetShmIndex();
  428. *ref = refnum;
  429. return 0;
  430. error:
  431. // Cleanup...
  432. fSynchroTable[refnum].Destroy();
  433. fClientTable[refnum] = 0;
  434. client->Close();
  435. delete client;
  436. return -1;
  437. }
  438. // Used for server driver clients
  439. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  440. {
  441. jack_log("JackEngine::ClientInternalNew: name = %s", name);
  442. int refnum = AllocateRefnum();
  443. if (refnum < 0) {
  444. jack_error("No more refnum available");
  445. goto error;
  446. }
  447. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  448. jack_error("Cannot allocate synchro");
  449. goto error;
  450. }
  451. if (wait && !fSignal.TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  452. // Failure if RT thread is not running (problem with the driver...)
  453. jack_error("Driver is not running");
  454. goto error;
  455. }
  456. fClientTable[refnum] = client;
  457. if (NotifyAddClient(client, name, refnum) < 0) {
  458. jack_error("Cannot notify add client");
  459. goto error;
  460. }
  461. fGraphManager->InitRefNum(refnum);
  462. fEngineControl->ResetRollingUsecs();
  463. *shared_engine = fEngineControl;
  464. *shared_manager = fGraphManager;
  465. *ref = refnum;
  466. return 0;
  467. error:
  468. // Cleanup...
  469. fSynchroTable[refnum].Destroy();
  470. fClientTable[refnum] = 0;
  471. return -1;
  472. }
  473. // Used for external clients
  474. int JackEngine::ClientExternalClose(int refnum)
  475. {
  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. JackClientInterface* client = fClientTable[refnum];
  491. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  492. }
  493. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  494. {
  495. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  496. // Unregister all ports ==> notifications are sent
  497. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  498. int i;
  499. fGraphManager->GetInputPorts(refnum, ports);
  500. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  501. PortUnRegister(refnum, ports[i]);
  502. }
  503. fGraphManager->GetOutputPorts(refnum, ports);
  504. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  505. PortUnRegister(refnum, ports[i]);
  506. }
  507. // Remove the client from the table
  508. ReleaseRefnum(refnum);
  509. // Remove all ports
  510. fGraphManager->RemoveAllPorts(refnum);
  511. // Wait until next cycle to be sure client is not used anymore
  512. if (wait) {
  513. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  514. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  515. }
  516. }
  517. // Notify running clients
  518. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  519. // Cleanup...
  520. fSynchroTable[refnum].Destroy();
  521. fEngineControl->ResetRollingUsecs();
  522. return 0;
  523. }
  524. int JackEngine::ClientActivate(int refnum, bool state)
  525. {
  526. JackClientInterface* client = fClientTable[refnum];
  527. assert(fClientTable[refnum]);
  528. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  529. if (state)
  530. fGraphManager->Activate(refnum);
  531. // Wait for graph state change to be effective
  532. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  533. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  534. return -1;
  535. } else {
  536. NotifyActivate(refnum);
  537. return 0;
  538. }
  539. }
  540. // May be called without client
  541. int JackEngine::ClientDeactivate(int refnum)
  542. {
  543. JackClientInterface* client = fClientTable[refnum];
  544. if (client == NULL)
  545. return -1;
  546. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  547. // Disconnect all ports ==> notifications are sent
  548. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  549. int i;
  550. fGraphManager->GetInputPorts(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->GetOutputPorts(refnum, ports);
  555. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  556. PortDisconnect(refnum, ports[i], ALL_PORTS);
  557. }
  558. fGraphManager->Deactivate(refnum);
  559. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  560. // Wait for graph state change to be effective
  561. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  562. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  563. return -1;
  564. } else {
  565. return 0;
  566. }
  567. }
  568. //-----------------
  569. // Port management
  570. //-----------------
  571. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, unsigned int* port_index)
  572. {
  573. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  574. assert(fClientTable[refnum]);
  575. // Check if port name already exists
  576. if (fGraphManager->GetPort(name) != NO_PORT) {
  577. jack_error("port_name \"%s\" already exists", name);
  578. return -1;
  579. }
  580. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  581. if (*port_index != NO_PORT) {
  582. NotifyPortRegistation(*port_index, true);
  583. return 0;
  584. } else {
  585. return -1;
  586. }
  587. }
  588. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  589. {
  590. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  591. assert(fClientTable[refnum]);
  592. // Disconnect port ==> notification is sent
  593. PortDisconnect(refnum, port_index, ALL_PORTS);
  594. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  595. NotifyPortRegistation(port_index, false);
  596. return 0;
  597. } else {
  598. return -1;
  599. }
  600. }
  601. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  602. {
  603. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  604. jack_port_id_t port_src, port_dst;
  605. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  606. ? -1
  607. : PortConnect(refnum, port_src, port_dst);
  608. }
  609. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  610. {
  611. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  612. JackClientInterface* client;
  613. int ref;
  614. if (fGraphManager->CheckPorts(src, dst) < 0)
  615. return -1;
  616. ref = fGraphManager->GetOutputRefNum(src);
  617. assert(ref >= 0);
  618. client = fClientTable[ref];
  619. assert(client);
  620. if (!client->GetClientControl()->fActive) {
  621. jack_error("Cannot connect ports owned by inactive clients:"
  622. " \"%s\" is not active", client->GetClientControl()->fName);
  623. return -1;
  624. }
  625. ref = fGraphManager->GetInputRefNum(dst);
  626. assert(ref >= 0);
  627. client = fClientTable[ref];
  628. assert(client);
  629. if (!client->GetClientControl()->fActive) {
  630. jack_error("Cannot connect ports owned by inactive clients:"
  631. " \"%s\" is not active", client->GetClientControl()->fName);
  632. return -1;
  633. }
  634. int res = fGraphManager->Connect(src, dst);
  635. if (res == 0)
  636. NotifyPortConnect(src, dst, true);
  637. return res;
  638. }
  639. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  640. {
  641. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  642. jack_port_id_t port_src, port_dst;
  643. if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
  644. return -1;
  645. } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
  646. NotifyPortConnect(port_src, port_dst, false);
  647. return 0;
  648. } else {
  649. return -1;
  650. }
  651. }
  652. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  653. {
  654. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  655. if (dst == ALL_PORTS) {
  656. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  657. fGraphManager->GetConnections(src, connections);
  658. // Notifications
  659. JackPort* port = fGraphManager->GetPort(src);
  660. if (port->GetFlags() & JackPortIsOutput) {
  661. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  662. jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
  663. NotifyPortConnect(src, connections[i], false);
  664. }
  665. } else {
  666. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  667. jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
  668. NotifyPortConnect(connections[i], src, false);
  669. }
  670. }
  671. return fGraphManager->DisconnectAll(src);
  672. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  673. return -1;
  674. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  675. // Notifications
  676. NotifyPortConnect(src, dst, false);
  677. return 0;
  678. } else {
  679. return -1;
  680. }
  681. }
  682. } // end of namespace