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.

922 lines
28KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include <iostream>
  16. #include <fstream>
  17. #include <assert.h>
  18. #include "JackSystemDeps.h"
  19. #include "JackLockedEngine.h"
  20. #include "JackExternalClient.h"
  21. #include "JackInternalClient.h"
  22. #include "JackEngineControl.h"
  23. #include "JackClientControl.h"
  24. #include "JackServerGlobals.h"
  25. #include "JackGlobals.h"
  26. #include "JackChannel.h"
  27. #include "JackError.h"
  28. namespace Jack
  29. {
  30. #define AssertRefnum(ref) assert(ref >= 0 && ref < CLIENT_NUM);
  31. JackEngine::JackEngine(JackGraphManager* manager,
  32. JackSynchro* table,
  33. JackEngineControl* control,
  34. JackSelfConnectMode self_connect_mode)
  35. {
  36. fGraphManager = manager;
  37. fSynchroTable = table;
  38. fEngineControl = control;
  39. fSelfConnectMode = self_connect_mode;
  40. for (int i = 0; i < CLIENT_NUM; i++)
  41. fClientTable[i] = NULL;
  42. }
  43. JackEngine::~JackEngine()
  44. {
  45. jack_log("JackEngine::~JackEngine");
  46. }
  47. int JackEngine::Open()
  48. {
  49. jack_log("JackEngine::Open");
  50. // Open audio thread => request thread communication channel
  51. if (fChannel.Open(fEngineControl->fServerName) < 0) {
  52. jack_error("Cannot connect to server");
  53. return -1;
  54. } else {
  55. return 0;
  56. }
  57. }
  58. int JackEngine::Close()
  59. {
  60. jack_log("JackEngine::Close");
  61. fChannel.Close();
  62. // Close remaining clients (RT is stopped)
  63. for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
  64. if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
  65. jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
  66. loadable_client->Close();
  67. // Close does not delete the pointer for internal clients
  68. fClientTable[i] = NULL;
  69. delete loadable_client;
  70. } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
  71. jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
  72. external_client->Close();
  73. // Close deletes the pointer for external clients
  74. fClientTable[i] = NULL;
  75. }
  76. }
  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 = fEngineControl->fDriverNum; 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.Signal(); // 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 = fEngineControl->fDriverNum; 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. jack_log("JackEngine::NotifyAddClient: name = %s", name);
  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::NotifyPortRename(jack_port_id_t port)
  272. {
  273. NotifyClients(kPortRenameCallback, false, port, 0);
  274. }
  275. void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
  276. {
  277. NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, src, dst);
  278. }
  279. void JackEngine::NotifyActivate(int refnum)
  280. {
  281. NotifyClient(refnum, kActivateClient, true, 0, 0);
  282. }
  283. //----------------------------
  284. // Loadable client management
  285. //----------------------------
  286. int JackEngine::GetInternalClientName(int refnum, char* name_res)
  287. {
  288. AssertRefnum(refnum);
  289. JackClientInterface* client = fClientTable[refnum];
  290. if (client) {
  291. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  292. return 0;
  293. } else {
  294. return -1;
  295. }
  296. }
  297. int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
  298. {
  299. // Clear status
  300. *status = 0;
  301. for (int i = 0; i < CLIENT_NUM; i++) {
  302. JackClientInterface* client = fClientTable[i];
  303. if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
  304. jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
  305. *int_ref = i;
  306. return 0;
  307. }
  308. }
  309. *status |= (JackNoSuchClient | JackFailure);
  310. return -1;
  311. }
  312. int JackEngine::InternalClientUnload(int refnum, int* status)
  313. {
  314. AssertRefnum(refnum);
  315. JackClientInterface* client = fClientTable[refnum];
  316. if (client) {
  317. int res = client->Close();
  318. delete client;
  319. *status = 0;
  320. return res;
  321. } else {
  322. *status = (JackNoSuchClient | JackFailure);
  323. return -1;
  324. }
  325. }
  326. //-------------------
  327. // Client management
  328. //-------------------
  329. int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
  330. {
  331. // Clear status
  332. *status = 0;
  333. strcpy(name_res, name);
  334. jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
  335. if (protocol != JACK_PROTOCOL_VERSION) {
  336. *status |= (JackFailure | JackVersionError);
  337. jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
  338. return -1;
  339. }
  340. if (ClientCheckName(name)) {
  341. *status |= JackNameNotUnique;
  342. if (options & JackUseExactName) {
  343. jack_error("cannot create new client; %s already exists", name);
  344. *status |= JackFailure;
  345. return -1;
  346. }
  347. if (GenerateUniqueName(name_res)) {
  348. *status |= JackFailure;
  349. return -1;
  350. }
  351. }
  352. return 0;
  353. }
  354. bool JackEngine::GenerateUniqueName(char* name)
  355. {
  356. int tens, ones;
  357. int length = strlen(name);
  358. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  359. jack_error("%s exists and is too long to make unique", name);
  360. return true; /* failure */
  361. }
  362. /* generate a unique name by appending "-01".."-99" */
  363. name[length++] = '-';
  364. tens = length++;
  365. ones = length++;
  366. name[tens] = '0';
  367. name[ones] = '1';
  368. name[length] = '\0';
  369. while (ClientCheckName(name)) {
  370. if (name[ones] == '9') {
  371. if (name[tens] == '9') {
  372. jack_error("client %s has 99 extra instances already", name);
  373. return true; /* give up */
  374. }
  375. name[tens]++;
  376. name[ones] = '0';
  377. } else {
  378. name[ones]++;
  379. }
  380. }
  381. return false;
  382. }
  383. bool JackEngine::ClientCheckName(const char* name)
  384. {
  385. for (int i = 0; i < CLIENT_NUM; i++) {
  386. JackClientInterface* client = fClientTable[i];
  387. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  388. return true;
  389. }
  390. return false;
  391. }
  392. int JackEngine::GetClientPID(const char* name)
  393. {
  394. for (int i = 0; i < CLIENT_NUM; i++) {
  395. JackClientInterface* client = fClientTable[i];
  396. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  397. return client->GetClientControl()->fPID;
  398. }
  399. return 0;
  400. }
  401. int JackEngine::GetClientRefNum(const char* name)
  402. {
  403. for (int i = 0; i < CLIENT_NUM; i++) {
  404. JackClientInterface* client = fClientTable[i];
  405. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  406. return client->GetClientControl()->fRefNum;
  407. }
  408. return -1;
  409. }
  410. // Used for external clients
  411. int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  412. {
  413. jack_log("JackEngine::ClientOpen: name = %s ", name);
  414. int refnum = AllocateRefnum();
  415. if (refnum < 0) {
  416. jack_error("No more refnum available");
  417. return -1;
  418. }
  419. JackExternalClient* client = new JackExternalClient();
  420. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  421. jack_error("Cannot allocate synchro");
  422. goto error;
  423. }
  424. if (client->Open(name, pid, refnum, shared_client) < 0) {
  425. jack_error("Cannot open client");
  426. goto error;
  427. }
  428. if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  429. // Failure if RT thread is not running (problem with the driver...)
  430. jack_error("Driver is not running");
  431. goto error;
  432. }
  433. fClientTable[refnum] = client;
  434. if (NotifyAddClient(client, name, refnum) < 0) {
  435. jack_error("Cannot notify add client");
  436. goto error;
  437. }
  438. fGraphManager->InitRefNum(refnum);
  439. fEngineControl->ResetRollingUsecs();
  440. *shared_engine = fEngineControl->GetShmIndex();
  441. *shared_graph_manager = fGraphManager->GetShmIndex();
  442. *ref = refnum;
  443. return 0;
  444. error:
  445. // Cleanup...
  446. fSynchroTable[refnum].Destroy();
  447. fClientTable[refnum] = 0;
  448. client->Close();
  449. delete client;
  450. return -1;
  451. }
  452. // Used for server driver clients
  453. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  454. {
  455. jack_log("JackEngine::ClientInternalNew: name = %s", name);
  456. int refnum = AllocateRefnum();
  457. if (refnum < 0) {
  458. jack_error("No more refnum available");
  459. goto error;
  460. }
  461. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  462. jack_error("Cannot allocate synchro");
  463. goto error;
  464. }
  465. if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  466. // Failure if RT thread is not running (problem with the driver...)
  467. jack_error("Driver is not running");
  468. goto error;
  469. }
  470. fClientTable[refnum] = client;
  471. if (NotifyAddClient(client, name, refnum) < 0) {
  472. jack_error("Cannot notify add client");
  473. goto error;
  474. }
  475. fGraphManager->InitRefNum(refnum);
  476. fEngineControl->ResetRollingUsecs();
  477. *shared_engine = fEngineControl;
  478. *shared_manager = fGraphManager;
  479. *ref = refnum;
  480. return 0;
  481. error:
  482. // Cleanup...
  483. fSynchroTable[refnum].Destroy();
  484. fClientTable[refnum] = 0;
  485. return -1;
  486. }
  487. // Used for external clients
  488. int JackEngine::ClientExternalClose(int refnum)
  489. {
  490. AssertRefnum(refnum);
  491. JackClientInterface* client = fClientTable[refnum];
  492. if (client) {
  493. fEngineControl->fTransport.ResetTimebase(refnum);
  494. int res = ClientCloseAux(refnum, client, true);
  495. client->Close();
  496. delete client;
  497. return res;
  498. } else {
  499. return -1;
  500. }
  501. }
  502. // Used for server internal clients or drivers when the RT thread is stopped
  503. int JackEngine::ClientInternalClose(int refnum, bool wait)
  504. {
  505. AssertRefnum(refnum);
  506. JackClientInterface* client = fClientTable[refnum];
  507. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  508. }
  509. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  510. {
  511. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  512. // Unregister all ports ==> notifications are sent
  513. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  514. int i;
  515. fGraphManager->GetInputPorts(refnum, ports);
  516. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  517. PortUnRegister(refnum, ports[i]);
  518. }
  519. fGraphManager->GetOutputPorts(refnum, ports);
  520. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  521. PortUnRegister(refnum, ports[i]);
  522. }
  523. // Remove the client from the table
  524. ReleaseRefnum(refnum);
  525. // Remove all ports
  526. fGraphManager->RemoveAllPorts(refnum);
  527. // Wait until next cycle to be sure client is not used anymore
  528. if (wait) {
  529. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  530. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  531. }
  532. }
  533. // Notify running clients
  534. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  535. // Cleanup...
  536. fSynchroTable[refnum].Destroy();
  537. fEngineControl->ResetRollingUsecs();
  538. return 0;
  539. }
  540. int JackEngine::ClientActivate(int refnum, bool state)
  541. {
  542. AssertRefnum(refnum);
  543. JackClientInterface* client = fClientTable[refnum];
  544. assert(fClientTable[refnum]);
  545. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  546. if (state)
  547. fGraphManager->Activate(refnum);
  548. // Wait for graph state change to be effective
  549. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  550. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  551. return -1;
  552. } else {
  553. NotifyActivate(refnum);
  554. return 0;
  555. }
  556. }
  557. // May be called without client
  558. int JackEngine::ClientDeactivate(int refnum)
  559. {
  560. AssertRefnum(refnum);
  561. JackClientInterface* client = fClientTable[refnum];
  562. if (client == NULL)
  563. return -1;
  564. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  565. // Disconnect all ports ==> notifications are sent
  566. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  567. int i;
  568. fGraphManager->GetInputPorts(refnum, ports);
  569. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  570. PortDisconnect(refnum, ports[i], ALL_PORTS);
  571. }
  572. fGraphManager->GetOutputPorts(refnum, ports);
  573. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  574. PortDisconnect(refnum, ports[i], ALL_PORTS);
  575. }
  576. fGraphManager->Deactivate(refnum);
  577. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  578. // Wait for graph state change to be effective
  579. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  580. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  581. return -1;
  582. } else {
  583. return 0;
  584. }
  585. }
  586. //-----------------
  587. // Port management
  588. //-----------------
  589. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  590. {
  591. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  592. AssertRefnum(refnum);
  593. assert(fClientTable[refnum]);
  594. // Check if port name already exists
  595. if (fGraphManager->GetPort(name) != NO_PORT) {
  596. jack_error("port_name \"%s\" already exists", name);
  597. return -1;
  598. }
  599. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  600. if (*port_index != NO_PORT) {
  601. NotifyPortRegistation(*port_index, true);
  602. return 0;
  603. } else {
  604. return -1;
  605. }
  606. }
  607. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  608. {
  609. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  610. AssertRefnum(refnum);
  611. assert(fClientTable[refnum]);
  612. // Disconnect port ==> notification is sent
  613. PortDisconnect(refnum, port_index, ALL_PORTS);
  614. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  615. NotifyPortRegistation(port_index, false);
  616. return 0;
  617. } else {
  618. return -1;
  619. }
  620. }
  621. // this check is to prevent apps to self connect to other apps
  622. // TODO: make this work with multiple clients per app
  623. int JackEngine::CheckPortsConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  624. {
  625. JackPort* src_port = fGraphManager->GetPort(src);
  626. JackPort* dst_port = fGraphManager->GetPort(dst);
  627. jack_log("CheckPortsConnect(caller = %d, src = %d, dst = %d)", refnum, src_port->GetRefNum(), dst_port->GetRefNum());
  628. int src_self = src_port->GetRefNum() == refnum ? 1 : 0;
  629. int dst_self = dst_port->GetRefNum() == refnum ? 1 : 0;
  630. jack_log("src_self is %s", src_self ? "true" : "false");
  631. jack_log("dst_self is %s", dst_self ? "true" : "false");
  632. // 0 means client is connecting other client ports (i.e. control app patchbay functionality)
  633. // 1 means client is connecting its own port to port of other client (i.e. self hooking into system app)
  634. // 2 means client is connecting its own ports (i.e. for app internal functionality)
  635. // TODO: Make this check an engine option and more tweakable (return error or success)
  636. // MAYBE: make the engine option changable on the fly and expose it through client or control API
  637. switch (fSelfConnectMode)
  638. {
  639. case JackSelfConnectFailExternalOnly:
  640. if (src_self + dst_self == 1)
  641. {
  642. jack_info("rejecting port self connect request to external port (%s -> %s)", src_port->GetName(), dst_port->GetName());
  643. return -1;
  644. }
  645. return 1;
  646. case JackSelfConnectIgnoreExternalOnly:
  647. if (src_self + dst_self == 1)
  648. {
  649. jack_info("ignoring port self connect request to external port (%s -> %s)", src_port->GetName(), dst_port->GetName());
  650. return 0;
  651. }
  652. return 1;
  653. case JackSelfConnectFailAll:
  654. if (src_self + dst_self != 0)
  655. {
  656. jack_info("rejecting port self connect request (%s -> %s)", src_port->GetName(), dst_port->GetName());
  657. return -1;
  658. }
  659. return 1;
  660. case JackSelfConnectIgnoreAll:
  661. if (src_self + dst_self != 0)
  662. {
  663. jack_info("ignoring port self connect request (%s -> %s)", src_port->GetName(), dst_port->GetName());
  664. return 0;
  665. }
  666. return 1;
  667. case JackSelfConnectAllow: // fix warning
  668. return 1;
  669. }
  670. return 1;
  671. }
  672. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  673. {
  674. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  675. jack_port_id_t port_src, port_dst;
  676. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  677. ? -1
  678. : PortConnect(refnum, port_src, port_dst);
  679. }
  680. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  681. {
  682. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  683. AssertRefnum(refnum);
  684. JackClientInterface* client;
  685. int ref;
  686. if (fGraphManager->CheckPorts(src, dst) < 0)
  687. return -1;
  688. ref = fGraphManager->GetOutputRefNum(src);
  689. assert(ref >= 0);
  690. client = fClientTable[ref];
  691. assert(client);
  692. if (!client->GetClientControl()->fActive) {
  693. jack_error("Cannot connect ports owned by inactive clients:"
  694. " \"%s\" is not active", client->GetClientControl()->fName);
  695. return -1;
  696. }
  697. ref = fGraphManager->GetInputRefNum(dst);
  698. assert(ref >= 0);
  699. client = fClientTable[ref];
  700. assert(client);
  701. if (!client->GetClientControl()->fActive) {
  702. jack_error("Cannot connect ports owned by inactive clients:"
  703. " \"%s\" is not active", client->GetClientControl()->fName);
  704. return -1;
  705. }
  706. int res = CheckPortsConnect(refnum, src, dst);
  707. if (res != 1) {
  708. return res;
  709. }
  710. res = fGraphManager->Connect(src, dst);
  711. if (res == 0)
  712. NotifyPortConnect(src, dst, true);
  713. return res;
  714. }
  715. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  716. {
  717. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  718. AssertRefnum(refnum);
  719. jack_port_id_t port_src, port_dst;
  720. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  721. ? -1
  722. : PortDisconnect(refnum, port_src, port_dst);
  723. }
  724. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  725. {
  726. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  727. AssertRefnum(refnum);
  728. if (dst == ALL_PORTS) {
  729. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  730. fGraphManager->GetConnections(src, connections);
  731. JackPort* port = fGraphManager->GetPort(src);
  732. int ret = 0;
  733. if (port->GetFlags() & JackPortIsOutput) {
  734. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  735. if (PortDisconnect(refnum, src, connections[i]) != 0) {
  736. ret = -1;
  737. }
  738. }
  739. } else {
  740. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  741. if (PortDisconnect(refnum, connections[i], src) != 0) {
  742. ret = -1;
  743. }
  744. }
  745. }
  746. return ret;
  747. }
  748. if (fGraphManager->CheckPorts(src, dst) < 0) {
  749. return -1;
  750. }
  751. int res = CheckPortsConnect(refnum, src, dst);
  752. if (res != 1) {
  753. return res;
  754. }
  755. res = fGraphManager->Disconnect(src, dst);
  756. if (res == 0)
  757. NotifyPortConnect(src, dst, false);
  758. return res;
  759. }
  760. int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
  761. {
  762. fGraphManager->GetPort(port)->SetName(name);
  763. NotifyPortRename(port);
  764. return 0;
  765. }
  766. } // end of namespace