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.

829 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. if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
  68. jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
  69. loadable_client->Close();
  70. delete loadable_client;
  71. } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
  72. jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
  73. external_client->Close();
  74. delete external_client;
  75. }
  76. }
  77. fSignal.Destroy();
  78. return 0;
  79. }
  80. //-----------------------------
  81. // Client ressource management
  82. //-----------------------------
  83. int JackEngine::AllocateRefnum()
  84. {
  85. for (int i = 0; i < CLIENT_NUM; i++) {
  86. if (!fClientTable[i]) {
  87. jack_log("JackEngine::AllocateRefNum ref = %ld", i);
  88. return i;
  89. }
  90. }
  91. return -1;
  92. }
  93. void JackEngine::ReleaseRefnum(int ref)
  94. {
  95. fClientTable[ref] = NULL;
  96. if (fEngineControl->fTemporary) {
  97. int i;
  98. for (i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  99. if (fClientTable[i])
  100. break;
  101. }
  102. if (i == CLIENT_NUM) {
  103. // last client and temporay case: quit the server
  104. jack_log("JackEngine::ReleaseRefnum server quit");
  105. fEngineControl->fTemporary = false;
  106. #ifndef WIN32
  107. exit(0);
  108. #endif
  109. }
  110. }
  111. }
  112. //------------------
  113. // Graph management
  114. //------------------
  115. void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
  116. {
  117. fLastSwitchUsecs = cur_cycle_begin;
  118. if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
  119. fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
  120. fSignal.SignalAll(); // Signal for threads waiting for next cycle
  121. }
  122. void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
  123. {
  124. if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
  125. CheckXRun(cur_cycle_begin);
  126. fGraphManager->RunCurrentGraph();
  127. }
  128. bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  129. {
  130. bool res = true;
  131. // Cycle begin
  132. fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
  133. // Graph
  134. if (fGraphManager->IsFinishedGraph()) {
  135. ProcessNext(cur_cycle_begin);
  136. res = true;
  137. } else {
  138. jack_log("Process: graph not finished!");
  139. if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  140. jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  141. ProcessNext(cur_cycle_begin);
  142. res = true;
  143. } else {
  144. jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  145. ProcessCurrent(cur_cycle_begin);
  146. res = false;
  147. }
  148. }
  149. // Cycle end
  150. fEngineControl->CycleEnd(fClientTable);
  151. return res;
  152. }
  153. /*
  154. Client that finish *after* the callback date are considered late even if their output buffers may have been
  155. correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
  156. */
  157. void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
  158. {
  159. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  160. JackClientInterface* client = fClientTable[i];
  161. if (client && client->GetClientControl()->fActive) {
  162. JackClientTiming* timing = fGraphManager->GetClientTiming(i);
  163. jack_client_state_t status = timing->fStatus;
  164. jack_time_t finished_date = timing->fFinishedAt;
  165. if (status != NotTriggered && status != Finished) {
  166. jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
  167. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  168. }
  169. if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
  170. jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
  171. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  172. }
  173. }
  174. }
  175. }
  176. //---------------
  177. // Notifications
  178. //---------------
  179. void JackEngine::NotifyClient(int refnum, int event, int sync, int value1, int value2)
  180. {
  181. JackClientInterface* client = fClientTable[refnum];
  182. // The client may be notified by the RT thread while closing
  183. if (!client) {
  184. jack_log("JackEngine::NotifyClient: client not available anymore");
  185. } else if (client->GetClientControl()->fCallback[event]) {
  186. if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
  187. jack_error("NotifyClient fails name = %s event = %ld = val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  188. } else {
  189. jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
  190. }
  191. }
  192. void JackEngine::NotifyClients(int event, int sync, int value1, int value2)
  193. {
  194. for (int i = 0; i < CLIENT_NUM; i++) {
  195. JackClientInterface* client = fClientTable[i];
  196. if (client) {
  197. if (client->GetClientControl()->fCallback[event]) {
  198. if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
  199. jack_error("NotifyClient fails name = %s event = %ld = val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  200. } else {
  201. jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
  202. }
  203. }
  204. }
  205. }
  206. int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
  207. {
  208. // Notify existing clients of the new client and new client of existing clients.
  209. for (int i = 0; i < CLIENT_NUM; i++) {
  210. JackClientInterface* old_client = fClientTable[i];
  211. if (old_client) {
  212. if (old_client->ClientNotify(refnum, name, kAddClient, true, 0, 0) < 0) {
  213. jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
  214. return -1;
  215. }
  216. if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, 0, 0) < 0) {
  217. jack_error("NotifyAddClient new_client fails name = %s", name);
  218. return -1;
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224. void JackEngine::NotifyRemoveClient(const char* name, int refnum)
  225. {
  226. // Notify existing clients (including the one beeing suppressed) of the removed client
  227. for (int i = 0; i < CLIENT_NUM; i++) {
  228. JackClientInterface* client = fClientTable[i];
  229. if (client) {
  230. client->ClientNotify(refnum, name, kRemoveClient, true, 0, 0);
  231. }
  232. }
  233. }
  234. // Coming from the driver
  235. void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
  236. {
  237. // Use the audio thread => request thread communication channel
  238. fEngineControl->ResetFrameTime(callback_usecs);
  239. fEngineControl->NotifyXRun(delayed_usecs);
  240. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
  241. }
  242. void JackEngine::NotifyXRun(int refnum)
  243. {
  244. if (refnum == ALL_CLIENTS) {
  245. NotifyClients(kXRunCallback, false, 0, 0);
  246. } else {
  247. NotifyClient(refnum, kXRunCallback, false, 0, 0);
  248. }
  249. }
  250. void JackEngine::NotifyGraphReorder()
  251. {
  252. NotifyClients(kGraphOrderCallback, false, 0, 0);
  253. }
  254. void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size)
  255. {
  256. NotifyClients(kBufferSizeCallback, true, buffer_size, 0);
  257. }
  258. void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
  259. {
  260. NotifyClients(kSampleRateCallback, true, sample_rate, 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. AssertRefnum(refnum);
  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. AssertRefnum(refnum);
  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. AssertRefnum(refnum);
  478. JackClientInterface* client = fClientTable[refnum];
  479. if (client) {
  480. fEngineControl->fTransport.ResetTimebase(refnum);
  481. int res = ClientCloseAux(refnum, client, true);
  482. client->Close();
  483. delete client;
  484. return res;
  485. } else {
  486. return -1;
  487. }
  488. }
  489. // Used for server internal clients or drivers when the RT thread is stopped
  490. int JackEngine::ClientInternalClose(int refnum, bool wait)
  491. {
  492. AssertRefnum(refnum);
  493. JackClientInterface* client = fClientTable[refnum];
  494. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  495. }
  496. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  497. {
  498. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  499. // Unregister all ports ==> notifications are sent
  500. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  501. int i;
  502. fGraphManager->GetInputPorts(refnum, ports);
  503. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  504. PortUnRegister(refnum, ports[i]);
  505. }
  506. fGraphManager->GetOutputPorts(refnum, ports);
  507. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  508. PortUnRegister(refnum, ports[i]);
  509. }
  510. // Remove the client from the table
  511. ReleaseRefnum(refnum);
  512. // Remove all ports
  513. fGraphManager->RemoveAllPorts(refnum);
  514. // Wait until next cycle to be sure client is not used anymore
  515. if (wait) {
  516. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  517. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  518. }
  519. }
  520. // Notify running clients
  521. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  522. // Cleanup...
  523. fSynchroTable[refnum].Destroy();
  524. fEngineControl->ResetRollingUsecs();
  525. return 0;
  526. }
  527. int JackEngine::ClientActivate(int refnum, bool state)
  528. {
  529. AssertRefnum(refnum);
  530. JackClientInterface* client = fClientTable[refnum];
  531. assert(fClientTable[refnum]);
  532. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  533. if (state)
  534. fGraphManager->Activate(refnum);
  535. // Wait for graph state change to be effective
  536. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  537. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  538. return -1;
  539. } else {
  540. NotifyActivate(refnum);
  541. return 0;
  542. }
  543. }
  544. // May be called without client
  545. int JackEngine::ClientDeactivate(int refnum)
  546. {
  547. AssertRefnum(refnum);
  548. JackClientInterface* client = fClientTable[refnum];
  549. if (client == NULL)
  550. return -1;
  551. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  552. // Disconnect all ports ==> notifications are sent
  553. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  554. int i;
  555. fGraphManager->GetInputPorts(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->GetOutputPorts(refnum, ports);
  560. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  561. PortDisconnect(refnum, ports[i], ALL_PORTS);
  562. }
  563. fGraphManager->Deactivate(refnum);
  564. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  565. // Wait for graph state change to be effective
  566. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  567. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  568. return -1;
  569. } else {
  570. return 0;
  571. }
  572. }
  573. //-----------------
  574. // Port management
  575. //-----------------
  576. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, unsigned int* port_index)
  577. {
  578. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  579. AssertRefnum(refnum);
  580. assert(fClientTable[refnum]);
  581. // Check if port name already exists
  582. if (fGraphManager->GetPort(name) != NO_PORT) {
  583. jack_error("port_name \"%s\" already exists", name);
  584. return -1;
  585. }
  586. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  587. if (*port_index != NO_PORT) {
  588. NotifyPortRegistation(*port_index, true);
  589. return 0;
  590. } else {
  591. return -1;
  592. }
  593. }
  594. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  595. {
  596. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  597. AssertRefnum(refnum);
  598. assert(fClientTable[refnum]);
  599. // Disconnect port ==> notification is sent
  600. PortDisconnect(refnum, port_index, ALL_PORTS);
  601. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  602. NotifyPortRegistation(port_index, false);
  603. return 0;
  604. } else {
  605. return -1;
  606. }
  607. }
  608. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  609. {
  610. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  611. jack_port_id_t port_src, port_dst;
  612. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  613. ? -1
  614. : PortConnect(refnum, port_src, port_dst);
  615. }
  616. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  617. {
  618. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  619. AssertRefnum(refnum);
  620. JackClientInterface* client;
  621. int ref;
  622. if (fGraphManager->CheckPorts(src, dst) < 0)
  623. return -1;
  624. ref = fGraphManager->GetOutputRefNum(src);
  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. ref = fGraphManager->GetInputRefNum(dst);
  634. assert(ref >= 0);
  635. client = fClientTable[ref];
  636. assert(client);
  637. if (!client->GetClientControl()->fActive) {
  638. jack_error("Cannot connect ports owned by inactive clients:"
  639. " \"%s\" is not active", client->GetClientControl()->fName);
  640. return -1;
  641. }
  642. int res = fGraphManager->Connect(src, dst);
  643. if (res == 0)
  644. NotifyPortConnect(src, dst, true);
  645. return res;
  646. }
  647. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  648. {
  649. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  650. AssertRefnum(refnum);
  651. jack_port_id_t port_src, port_dst;
  652. if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
  653. return -1;
  654. } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
  655. NotifyPortConnect(port_src, port_dst, false);
  656. return 0;
  657. } else {
  658. return -1;
  659. }
  660. }
  661. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  662. {
  663. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  664. AssertRefnum(refnum);
  665. if (dst == ALL_PORTS) {
  666. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  667. fGraphManager->GetConnections(src, connections);
  668. // Notifications
  669. JackPort* port = fGraphManager->GetPort(src);
  670. if (port->GetFlags() & JackPortIsOutput) {
  671. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  672. jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
  673. NotifyPortConnect(src, connections[i], false);
  674. }
  675. } else {
  676. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  677. jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
  678. NotifyPortConnect(connections[i], src, false);
  679. }
  680. }
  681. return fGraphManager->DisconnectAll(src);
  682. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  683. return -1;
  684. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  685. // Notifications
  686. NotifyPortConnect(src, dst, false);
  687. return 0;
  688. } else {
  689. return -1;
  690. }
  691. }
  692. } // end of namespace