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.

935 lines
29KB

  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. throw JackTemporaryException();
  106. }
  107. }
  108. }
  109. //------------------
  110. // Graph management
  111. //------------------
  112. void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
  113. {
  114. fLastSwitchUsecs = cur_cycle_begin;
  115. if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
  116. fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
  117. fSignal.Signal(); // Signal for threads waiting for next cycle
  118. }
  119. void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
  120. {
  121. if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
  122. CheckXRun(cur_cycle_begin);
  123. fGraphManager->RunCurrentGraph();
  124. }
  125. bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  126. {
  127. bool res = true;
  128. // Cycle begin
  129. fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
  130. // Graph
  131. if (fGraphManager->IsFinishedGraph()) {
  132. ProcessNext(cur_cycle_begin);
  133. res = true;
  134. } else {
  135. jack_log("Process: graph not finished!");
  136. if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  137. jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  138. ProcessNext(cur_cycle_begin);
  139. res = true;
  140. } else {
  141. jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  142. ProcessCurrent(cur_cycle_begin);
  143. res = false;
  144. }
  145. }
  146. // Cycle end
  147. fEngineControl->CycleEnd(fClientTable);
  148. return res;
  149. }
  150. /*
  151. Client that finish *after* the callback date are considered late even if their output buffers may have been
  152. correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
  153. */
  154. void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
  155. {
  156. for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
  157. JackClientInterface* client = fClientTable[i];
  158. if (client && client->GetClientControl()->fActive) {
  159. JackClientTiming* timing = fGraphManager->GetClientTiming(i);
  160. jack_client_state_t status = timing->fStatus;
  161. jack_time_t finished_date = timing->fFinishedAt;
  162. if (status != NotTriggered && status != Finished) {
  163. jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
  164. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  165. }
  166. if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
  167. jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
  168. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  169. }
  170. }
  171. }
  172. }
  173. //---------------
  174. // Notifications
  175. //---------------
  176. void JackEngine::NotifyClient(int refnum, int event, int sync, const char* message, int value1, int value2)
  177. {
  178. JackClientInterface* client = fClientTable[refnum];
  179. // The client may be notified by the RT thread while closing
  180. if (!client) {
  181. jack_log("JackEngine::NotifyClient: client not available anymore");
  182. } else if (client->GetClientControl()->fCallback[event]) {
  183. if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0)
  184. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  185. } else {
  186. jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
  187. }
  188. }
  189. void JackEngine::NotifyClients(int event, int sync, const char* message, int value1, int value2)
  190. {
  191. for (int i = 0; i < CLIENT_NUM; i++) {
  192. JackClientInterface* client = fClientTable[i];
  193. if (client) {
  194. if (client->GetClientControl()->fCallback[event]) {
  195. if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0)
  196. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  197. } else {
  198. jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
  199. }
  200. }
  201. }
  202. }
  203. int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
  204. {
  205. jack_log("JackEngine::NotifyAddClient: name = %s", name);
  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::NotifyFailure(int code, const char* reason)
  261. {
  262. NotifyClients(kShutDownCallback, false, reason, code, 0);
  263. }
  264. void JackEngine::NotifyFreewheel(bool onoff)
  265. {
  266. if (onoff) {
  267. // Save RT state
  268. fEngineControl->fSavedRealTime = fEngineControl->fRealTime;
  269. fEngineControl->fRealTime = false;
  270. } else {
  271. // Restore RT state
  272. fEngineControl->fRealTime = fEngineControl->fSavedRealTime;
  273. fEngineControl->fSavedRealTime = false;
  274. }
  275. NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, "", 0, 0);
  276. }
  277. void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
  278. {
  279. NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, "", port_index, 0);
  280. }
  281. void JackEngine::NotifyPortRename(jack_port_id_t port)
  282. {
  283. NotifyClients(kPortRenameCallback, false, "", port, 0);
  284. }
  285. void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
  286. {
  287. NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, "", src, dst);
  288. }
  289. void JackEngine::NotifyActivate(int refnum)
  290. {
  291. NotifyClient(refnum, kActivateClient, true, "", 0, 0);
  292. }
  293. //----------------------------
  294. // Loadable client management
  295. //----------------------------
  296. int JackEngine::GetInternalClientName(int refnum, char* name_res)
  297. {
  298. AssertRefnum(refnum);
  299. JackClientInterface* client = fClientTable[refnum];
  300. if (client) {
  301. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  302. return 0;
  303. } else {
  304. return -1;
  305. }
  306. }
  307. int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
  308. {
  309. // Clear status
  310. *status = 0;
  311. for (int i = 0; i < CLIENT_NUM; i++) {
  312. JackClientInterface* client = fClientTable[i];
  313. if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
  314. jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
  315. *int_ref = i;
  316. return 0;
  317. }
  318. }
  319. *status |= (JackNoSuchClient | JackFailure);
  320. return -1;
  321. }
  322. int JackEngine::InternalClientUnload(int refnum, int* status)
  323. {
  324. AssertRefnum(refnum);
  325. JackClientInterface* client = fClientTable[refnum];
  326. if (client) {
  327. int res = client->Close();
  328. delete client;
  329. *status = 0;
  330. return res;
  331. } else {
  332. *status = (JackNoSuchClient | JackFailure);
  333. return -1;
  334. }
  335. }
  336. //-------------------
  337. // Client management
  338. //-------------------
  339. int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
  340. {
  341. // Clear status
  342. *status = 0;
  343. strcpy(name_res, name);
  344. jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
  345. if (protocol != JACK_PROTOCOL_VERSION) {
  346. *status |= (JackFailure | JackVersionError);
  347. jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
  348. return -1;
  349. }
  350. if (ClientCheckName(name)) {
  351. *status |= JackNameNotUnique;
  352. if (options & JackUseExactName) {
  353. jack_error("cannot create new client; %s already exists", name);
  354. *status |= JackFailure;
  355. return -1;
  356. }
  357. if (GenerateUniqueName(name_res)) {
  358. *status |= JackFailure;
  359. return -1;
  360. }
  361. }
  362. return 0;
  363. }
  364. bool JackEngine::GenerateUniqueName(char* name)
  365. {
  366. int tens, ones;
  367. int length = strlen(name);
  368. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  369. jack_error("%s exists and is too long to make unique", name);
  370. return true; /* failure */
  371. }
  372. /* generate a unique name by appending "-01".."-99" */
  373. name[length++] = '-';
  374. tens = length++;
  375. ones = length++;
  376. name[tens] = '0';
  377. name[ones] = '1';
  378. name[length] = '\0';
  379. while (ClientCheckName(name)) {
  380. if (name[ones] == '9') {
  381. if (name[tens] == '9') {
  382. jack_error("client %s has 99 extra instances already", name);
  383. return true; /* give up */
  384. }
  385. name[tens]++;
  386. name[ones] = '0';
  387. } else {
  388. name[ones]++;
  389. }
  390. }
  391. return false;
  392. }
  393. bool JackEngine::ClientCheckName(const char* name)
  394. {
  395. for (int i = 0; i < CLIENT_NUM; i++) {
  396. JackClientInterface* client = fClientTable[i];
  397. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  398. return true;
  399. }
  400. return false;
  401. }
  402. int JackEngine::GetClientPID(const char* name)
  403. {
  404. for (int i = 0; i < CLIENT_NUM; i++) {
  405. JackClientInterface* client = fClientTable[i];
  406. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  407. return client->GetClientControl()->fPID;
  408. }
  409. return 0;
  410. }
  411. int JackEngine::GetClientRefNum(const char* name)
  412. {
  413. for (int i = 0; i < CLIENT_NUM; i++) {
  414. JackClientInterface* client = fClientTable[i];
  415. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  416. return client->GetClientControl()->fRefNum;
  417. }
  418. return -1;
  419. }
  420. // Used for external clients
  421. int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  422. {
  423. jack_log("JackEngine::ClientExternalOpen: name = %s ", name);
  424. int refnum = AllocateRefnum();
  425. if (refnum < 0) {
  426. jack_error("No more refnum available");
  427. return -1;
  428. }
  429. JackExternalClient* client = new JackExternalClient();
  430. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  431. jack_error("Cannot allocate synchro");
  432. goto error;
  433. }
  434. if (client->Open(name, pid, refnum, shared_client) < 0) {
  435. jack_error("Cannot open client");
  436. goto error;
  437. }
  438. if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  439. // Failure if RT thread is not running (problem with the driver...)
  440. jack_error("Driver is not running");
  441. goto error;
  442. }
  443. fClientTable[refnum] = client;
  444. if (NotifyAddClient(client, name, refnum) < 0) {
  445. jack_error("Cannot notify add client");
  446. goto error;
  447. }
  448. fGraphManager->InitRefNum(refnum);
  449. fEngineControl->ResetRollingUsecs();
  450. *shared_engine = fEngineControl->GetShmIndex();
  451. *shared_graph_manager = fGraphManager->GetShmIndex();
  452. *ref = refnum;
  453. return 0;
  454. error:
  455. // Cleanup...
  456. fSynchroTable[refnum].Destroy();
  457. fClientTable[refnum] = 0;
  458. client->Close();
  459. delete client;
  460. return -1;
  461. }
  462. // Used for server driver clients
  463. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  464. {
  465. jack_log("JackEngine::ClientInternalOpen: name = %s", name);
  466. int refnum = AllocateRefnum();
  467. if (refnum < 0) {
  468. jack_error("No more refnum available");
  469. goto error;
  470. }
  471. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  472. jack_error("Cannot allocate synchro");
  473. goto error;
  474. }
  475. if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  476. // Failure if RT thread is not running (problem with the driver...)
  477. jack_error("Driver is not running");
  478. goto error;
  479. }
  480. fClientTable[refnum] = client;
  481. if (NotifyAddClient(client, name, refnum) < 0) {
  482. jack_error("Cannot notify add client");
  483. goto error;
  484. }
  485. fGraphManager->InitRefNum(refnum);
  486. fEngineControl->ResetRollingUsecs();
  487. *shared_engine = fEngineControl;
  488. *shared_manager = fGraphManager;
  489. *ref = refnum;
  490. return 0;
  491. error:
  492. // Cleanup...
  493. fSynchroTable[refnum].Destroy();
  494. fClientTable[refnum] = 0;
  495. return -1;
  496. }
  497. // Used for external clients
  498. int JackEngine::ClientExternalClose(int refnum)
  499. {
  500. AssertRefnum(refnum);
  501. JackClientInterface* client = fClientTable[refnum];
  502. if (client) {
  503. fEngineControl->fTransport.ResetTimebase(refnum);
  504. int res = ClientCloseAux(refnum, client, true);
  505. client->Close();
  506. delete client;
  507. return res;
  508. } else {
  509. return -1;
  510. }
  511. }
  512. // Used for server internal clients or drivers when the RT thread is stopped
  513. int JackEngine::ClientInternalClose(int refnum, bool wait)
  514. {
  515. AssertRefnum(refnum);
  516. JackClientInterface* client = fClientTable[refnum];
  517. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  518. }
  519. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  520. {
  521. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  522. // Unregister all ports ==> notifications are sent
  523. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  524. int i;
  525. fGraphManager->GetInputPorts(refnum, ports);
  526. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  527. PortUnRegister(refnum, ports[i]);
  528. }
  529. fGraphManager->GetOutputPorts(refnum, ports);
  530. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  531. PortUnRegister(refnum, ports[i]);
  532. }
  533. // Remove the client from the table
  534. ReleaseRefnum(refnum);
  535. // Remove all ports
  536. fGraphManager->RemoveAllPorts(refnum);
  537. // Wait until next cycle to be sure client is not used anymore
  538. if (wait) {
  539. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  540. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  541. }
  542. }
  543. // Notify running clients
  544. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  545. // Cleanup...
  546. fSynchroTable[refnum].Destroy();
  547. fEngineControl->ResetRollingUsecs();
  548. return 0;
  549. }
  550. int JackEngine::ClientActivate(int refnum, bool is_real_time)
  551. {
  552. AssertRefnum(refnum);
  553. JackClientInterface* client = fClientTable[refnum];
  554. assert(fClientTable[refnum]);
  555. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  556. if (is_real_time)
  557. fGraphManager->Activate(refnum);
  558. // Wait for graph state change to be effective
  559. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  560. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  561. return -1;
  562. } else {
  563. NotifyActivate(refnum);
  564. return 0;
  565. }
  566. }
  567. // May be called without client
  568. int JackEngine::ClientDeactivate(int refnum)
  569. {
  570. AssertRefnum(refnum);
  571. JackClientInterface* client = fClientTable[refnum];
  572. if (client == NULL)
  573. return -1;
  574. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  575. // Disconnect all ports ==> notifications are sent
  576. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  577. int i;
  578. fGraphManager->GetInputPorts(refnum, ports);
  579. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  580. PortDisconnect(refnum, ports[i], ALL_PORTS);
  581. }
  582. fGraphManager->GetOutputPorts(refnum, ports);
  583. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  584. PortDisconnect(refnum, ports[i], ALL_PORTS);
  585. }
  586. fGraphManager->Deactivate(refnum);
  587. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  588. // Wait for graph state change to be effective
  589. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  590. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  591. return -1;
  592. } else {
  593. return 0;
  594. }
  595. }
  596. //-----------------
  597. // Port management
  598. //-----------------
  599. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  600. {
  601. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  602. AssertRefnum(refnum);
  603. assert(fClientTable[refnum]);
  604. // Check if port name already exists
  605. if (fGraphManager->GetPort(name) != NO_PORT) {
  606. jack_error("port_name \"%s\" already exists", name);
  607. return -1;
  608. }
  609. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  610. if (*port_index != NO_PORT) {
  611. NotifyPortRegistation(*port_index, true);
  612. return 0;
  613. } else {
  614. return -1;
  615. }
  616. }
  617. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  618. {
  619. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  620. AssertRefnum(refnum);
  621. assert(fClientTable[refnum]);
  622. // Disconnect port ==> notification is sent
  623. PortDisconnect(refnum, port_index, ALL_PORTS);
  624. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  625. NotifyPortRegistation(port_index, false);
  626. return 0;
  627. } else {
  628. return -1;
  629. }
  630. }
  631. // this check is to prevent apps to self connect to other apps
  632. // TODO: make this work with multiple clients per app
  633. int JackEngine::CheckPortsConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  634. {
  635. JackPort* src_port = fGraphManager->GetPort(src);
  636. JackPort* dst_port = fGraphManager->GetPort(dst);
  637. jack_log("CheckPortsConnect(caller = %d, src = %d, dst = %d)", refnum, src_port->GetRefNum(), dst_port->GetRefNum());
  638. int src_self = src_port->GetRefNum() == refnum ? 1 : 0;
  639. int dst_self = dst_port->GetRefNum() == refnum ? 1 : 0;
  640. jack_log("src_self is %s", src_self ? "true" : "false");
  641. jack_log("dst_self is %s", dst_self ? "true" : "false");
  642. // 0 means client is connecting other client ports (i.e. control app patchbay functionality)
  643. // 1 means client is connecting its own port to port of other client (i.e. self hooking into system app)
  644. // 2 means client is connecting its own ports (i.e. for app internal functionality)
  645. // TODO: Make this check an engine option and more tweakable (return error or success)
  646. // MAYBE: make the engine option changable on the fly and expose it through client or control API
  647. switch (fSelfConnectMode)
  648. {
  649. case JackSelfConnectFailExternalOnly:
  650. if (src_self + dst_self == 1)
  651. {
  652. jack_info("rejecting port self connect request to external port (%s -> %s)", src_port->GetName(), dst_port->GetName());
  653. return -1;
  654. }
  655. return 1;
  656. case JackSelfConnectIgnoreExternalOnly:
  657. if (src_self + dst_self == 1)
  658. {
  659. jack_info("ignoring port self connect request to external port (%s -> %s)", src_port->GetName(), dst_port->GetName());
  660. return 0;
  661. }
  662. return 1;
  663. case JackSelfConnectFailAll:
  664. if (src_self + dst_self != 0)
  665. {
  666. jack_info("rejecting port self connect request (%s -> %s)", src_port->GetName(), dst_port->GetName());
  667. return -1;
  668. }
  669. return 1;
  670. case JackSelfConnectIgnoreAll:
  671. if (src_self + dst_self != 0)
  672. {
  673. jack_info("ignoring port self connect request (%s -> %s)", src_port->GetName(), dst_port->GetName());
  674. return 0;
  675. }
  676. return 1;
  677. case JackSelfConnectAllow: // fix warning
  678. return 1;
  679. }
  680. return 1;
  681. }
  682. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  683. {
  684. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  685. AssertRefnum(refnum);
  686. jack_port_id_t port_src, port_dst;
  687. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  688. ? -1
  689. : PortConnect(refnum, port_src, port_dst);
  690. }
  691. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  692. {
  693. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  694. AssertRefnum(refnum);
  695. JackClientInterface* client;
  696. int ref;
  697. if (fGraphManager->CheckPorts(src, dst) < 0)
  698. return -1;
  699. ref = fGraphManager->GetOutputRefNum(src);
  700. assert(ref >= 0);
  701. client = fClientTable[ref];
  702. assert(client);
  703. if (!client->GetClientControl()->fActive) {
  704. jack_error("Cannot connect ports owned by inactive clients:"
  705. " \"%s\" is not active", client->GetClientControl()->fName);
  706. return -1;
  707. }
  708. ref = fGraphManager->GetInputRefNum(dst);
  709. assert(ref >= 0);
  710. client = fClientTable[ref];
  711. assert(client);
  712. if (!client->GetClientControl()->fActive) {
  713. jack_error("Cannot connect ports owned by inactive clients:"
  714. " \"%s\" is not active", client->GetClientControl()->fName);
  715. return -1;
  716. }
  717. int res = CheckPortsConnect(refnum, src, dst);
  718. if (res != 1) {
  719. return res;
  720. }
  721. res = fGraphManager->Connect(src, dst);
  722. if (res == 0)
  723. NotifyPortConnect(src, dst, true);
  724. return res;
  725. }
  726. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  727. {
  728. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  729. AssertRefnum(refnum);
  730. jack_port_id_t port_src, port_dst;
  731. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  732. ? -1
  733. : PortDisconnect(refnum, port_src, port_dst);
  734. }
  735. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  736. {
  737. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  738. AssertRefnum(refnum);
  739. if (dst == ALL_PORTS) {
  740. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  741. fGraphManager->GetConnections(src, connections);
  742. JackPort* port = fGraphManager->GetPort(src);
  743. int ret = 0;
  744. if (port->GetFlags() & JackPortIsOutput) {
  745. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  746. if (PortDisconnect(refnum, src, connections[i]) != 0) {
  747. ret = -1;
  748. }
  749. }
  750. } else {
  751. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  752. if (PortDisconnect(refnum, connections[i], src) != 0) {
  753. ret = -1;
  754. }
  755. }
  756. }
  757. return ret;
  758. }
  759. if (fGraphManager->CheckPorts(src, dst) < 0) {
  760. return -1;
  761. }
  762. int res = CheckPortsConnect(refnum, src, dst);
  763. if (res != 1) {
  764. return res;
  765. }
  766. res = fGraphManager->Disconnect(src, dst);
  767. if (res == 0)
  768. NotifyPortConnect(src, dst, false);
  769. return res;
  770. }
  771. int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
  772. {
  773. AssertRefnum(refnum);
  774. fGraphManager->GetPort(port)->SetName(name);
  775. NotifyPortRename(port);
  776. return 0;
  777. }
  778. } // end of namespace