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.

844 lines
26KB

  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 "JackGlobals.h"
  25. #include "JackChannel.h"
  26. #include "JackError.h"
  27. namespace Jack
  28. {
  29. #define AssertRefnum(ref) assert(ref >= 0 && ref < CLIENT_NUM);
  30. JackEngine::JackEngine(JackGraphManager* manager,
  31. JackSynchro* table,
  32. JackEngineControl* control)
  33. {
  34. fGraphManager = manager;
  35. fSynchroTable = table;
  36. fEngineControl = control;
  37. for (int i = 0; i < CLIENT_NUM; i++)
  38. fClientTable[i] = NULL;
  39. }
  40. JackEngine::~JackEngine()
  41. {
  42. jack_log("JackEngine::~JackEngine");
  43. }
  44. int JackEngine::Open()
  45. {
  46. jack_log("JackEngine::Open");
  47. // Open audio thread => request thread communication channel
  48. if (fChannel.Open(fEngineControl->fServerName) < 0) {
  49. jack_error("Cannot connect to server");
  50. return -1;
  51. } else {
  52. return 0;
  53. }
  54. }
  55. int JackEngine::Close()
  56. {
  57. jack_log("JackEngine::Close");
  58. fChannel.Close();
  59. // Close remaining clients (RT is stopped)
  60. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  61. if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
  62. jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
  63. loadable_client->Close();
  64. // Close does not delete the pointer for internal clients
  65. fClientTable[i] = NULL;
  66. delete loadable_client;
  67. } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
  68. jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
  69. external_client->Close();
  70. // Close deletes the pointer for external clients
  71. fClientTable[i] = NULL;
  72. }
  73. }
  74. return 0;
  75. }
  76. //-----------------------------
  77. // Client ressource management
  78. //-----------------------------
  79. int JackEngine::AllocateRefnum()
  80. {
  81. for (int i = 0; i < CLIENT_NUM; i++) {
  82. if (!fClientTable[i]) {
  83. jack_log("JackEngine::AllocateRefNum ref = %ld", i);
  84. return i;
  85. }
  86. }
  87. return -1;
  88. }
  89. void JackEngine::ReleaseRefnum(int ref)
  90. {
  91. fClientTable[ref] = NULL;
  92. if (fEngineControl->fTemporary) {
  93. int i;
  94. for (i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  95. if (fClientTable[i])
  96. break;
  97. }
  98. if (i == CLIENT_NUM) {
  99. // last client and temporay case: quit the server
  100. jack_log("JackEngine::ReleaseRefnum server quit");
  101. fEngineControl->fTemporary = false;
  102. #ifndef WIN32
  103. exit(0);
  104. #endif
  105. }
  106. }
  107. }
  108. //------------------
  109. // Graph management
  110. //------------------
  111. void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
  112. {
  113. fLastSwitchUsecs = cur_cycle_begin;
  114. if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
  115. fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
  116. fSignal.Signal(); // Signal for threads waiting for next cycle
  117. }
  118. void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
  119. {
  120. if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
  121. CheckXRun(cur_cycle_begin);
  122. fGraphManager->RunCurrentGraph();
  123. }
  124. bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  125. {
  126. bool res = true;
  127. // Cycle begin
  128. fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
  129. // Graph
  130. if (fGraphManager->IsFinishedGraph()) {
  131. ProcessNext(cur_cycle_begin);
  132. res = true;
  133. } else {
  134. jack_log("Process: graph not finished!");
  135. if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  136. jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  137. ProcessNext(cur_cycle_begin);
  138. res = true;
  139. } else {
  140. jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  141. ProcessCurrent(cur_cycle_begin);
  142. res = false;
  143. }
  144. }
  145. // Cycle end
  146. fEngineControl->CycleEnd(fClientTable);
  147. return res;
  148. }
  149. /*
  150. Client that finish *after* the callback date are considered late even if their output buffers may have been
  151. correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
  152. */
  153. void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
  154. {
  155. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  156. JackClientInterface* client = fClientTable[i];
  157. if (client && client->GetClientControl()->fActive) {
  158. JackClientTiming* timing = fGraphManager->GetClientTiming(i);
  159. jack_client_state_t status = timing->fStatus;
  160. jack_time_t finished_date = timing->fFinishedAt;
  161. if (status != NotTriggered && status != Finished) {
  162. jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
  163. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  164. }
  165. if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
  166. jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
  167. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  168. }
  169. }
  170. }
  171. }
  172. //---------------
  173. // Notifications
  174. //---------------
  175. void JackEngine::NotifyClient(int refnum, int event, int sync, int value1, int value2)
  176. {
  177. JackClientInterface* client = fClientTable[refnum];
  178. // The client may be notified by the RT thread while closing
  179. if (!client) {
  180. jack_log("JackEngine::NotifyClient: client not available anymore");
  181. } else if (client->GetClientControl()->fCallback[event]) {
  182. if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
  183. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  184. } else {
  185. jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
  186. }
  187. }
  188. void JackEngine::NotifyClients(int event, int sync, int value1, int value2)
  189. {
  190. for (int i = 0; i < CLIENT_NUM; i++) {
  191. JackClientInterface* client = fClientTable[i];
  192. if (client) {
  193. if (client->GetClientControl()->fCallback[event]) {
  194. if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
  195. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  196. } else {
  197. jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
  198. }
  199. }
  200. }
  201. }
  202. int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
  203. {
  204. // Notify existing clients of the new client and new client of existing clients.
  205. for (int i = 0; i < CLIENT_NUM; i++) {
  206. JackClientInterface* old_client = fClientTable[i];
  207. if (old_client) {
  208. if (old_client->ClientNotify(refnum, name, kAddClient, true, 0, 0) < 0) {
  209. jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
  210. return -1;
  211. }
  212. if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, 0, 0) < 0) {
  213. jack_error("NotifyAddClient new_client fails name = %s", name);
  214. return -1;
  215. }
  216. }
  217. }
  218. return 0;
  219. }
  220. void JackEngine::NotifyRemoveClient(const char* name, int refnum)
  221. {
  222. // Notify existing clients (including the one beeing suppressed) of the removed client
  223. for (int i = 0; i < CLIENT_NUM; i++) {
  224. JackClientInterface* client = fClientTable[i];
  225. if (client) {
  226. client->ClientNotify(refnum, name, kRemoveClient, true, 0, 0);
  227. }
  228. }
  229. }
  230. // Coming from the driver
  231. void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
  232. {
  233. // Use the audio thread => request thread communication channel
  234. fEngineControl->ResetFrameTime(callback_usecs);
  235. fEngineControl->NotifyXRun(delayed_usecs);
  236. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
  237. }
  238. void JackEngine::NotifyXRun(int refnum)
  239. {
  240. if (refnum == ALL_CLIENTS) {
  241. NotifyClients(kXRunCallback, false, 0, 0);
  242. } else {
  243. NotifyClient(refnum, kXRunCallback, false, 0, 0);
  244. }
  245. }
  246. void JackEngine::NotifyGraphReorder()
  247. {
  248. NotifyClients(kGraphOrderCallback, false, 0, 0);
  249. }
  250. void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size)
  251. {
  252. NotifyClients(kBufferSizeCallback, true, buffer_size, 0);
  253. }
  254. void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
  255. {
  256. NotifyClients(kSampleRateCallback, true, sample_rate, 0);
  257. }
  258. void JackEngine::NotifyFreewheel(bool onoff)
  259. {
  260. fEngineControl->fRealTime = !onoff;
  261. NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, 0, 0);
  262. }
  263. void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
  264. {
  265. NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, port_index, 0);
  266. }
  267. void JackEngine::NotifyPortRename(jack_port_id_t port)
  268. {
  269. NotifyClients(kPortRenameCallback, false, port, 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. int JackEngine::GetClientRefNum(const char* name)
  398. {
  399. for (int i = 0; i < CLIENT_NUM; i++) {
  400. JackClientInterface* client = fClientTable[i];
  401. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  402. return client->GetClientControl()->fRefNum;
  403. }
  404. return -1;
  405. }
  406. // Used for external clients
  407. int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  408. {
  409. jack_log("JackEngine::ClientOpen: name = %s ", name);
  410. int refnum = AllocateRefnum();
  411. if (refnum < 0) {
  412. jack_error("No more refnum available");
  413. return -1;
  414. }
  415. JackExternalClient* client = new JackExternalClient();
  416. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  417. jack_error("Cannot allocate synchro");
  418. goto error;
  419. }
  420. if (client->Open(name, pid, refnum, shared_client) < 0) {
  421. jack_error("Cannot open client");
  422. goto error;
  423. }
  424. if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  425. // Failure if RT thread is not running (problem with the driver...)
  426. jack_error("Driver is not running");
  427. goto error;
  428. }
  429. fClientTable[refnum] = client;
  430. if (NotifyAddClient(client, name, refnum) < 0) {
  431. jack_error("Cannot notify add client");
  432. goto error;
  433. }
  434. fGraphManager->InitRefNum(refnum);
  435. fEngineControl->ResetRollingUsecs();
  436. *shared_engine = fEngineControl->GetShmIndex();
  437. *shared_graph_manager = fGraphManager->GetShmIndex();
  438. *ref = refnum;
  439. return 0;
  440. error:
  441. // Cleanup...
  442. fSynchroTable[refnum].Destroy();
  443. fClientTable[refnum] = 0;
  444. client->Close();
  445. delete client;
  446. return -1;
  447. }
  448. // Used for server driver clients
  449. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  450. {
  451. jack_log("JackEngine::ClientInternalNew: name = %s", name);
  452. int refnum = AllocateRefnum();
  453. if (refnum < 0) {
  454. jack_error("No more refnum available");
  455. goto error;
  456. }
  457. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  458. jack_error("Cannot allocate synchro");
  459. goto error;
  460. }
  461. if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  462. // Failure if RT thread is not running (problem with the driver...)
  463. jack_error("Driver is not running");
  464. goto error;
  465. }
  466. fClientTable[refnum] = client;
  467. if (NotifyAddClient(client, name, refnum) < 0) {
  468. jack_error("Cannot notify add client");
  469. goto error;
  470. }
  471. fGraphManager->InitRefNum(refnum);
  472. fEngineControl->ResetRollingUsecs();
  473. *shared_engine = fEngineControl;
  474. *shared_manager = fGraphManager;
  475. *ref = refnum;
  476. return 0;
  477. error:
  478. // Cleanup...
  479. fSynchroTable[refnum].Destroy();
  480. fClientTable[refnum] = 0;
  481. return -1;
  482. }
  483. // Used for external clients
  484. int JackEngine::ClientExternalClose(int refnum)
  485. {
  486. AssertRefnum(refnum);
  487. JackClientInterface* client = fClientTable[refnum];
  488. if (client) {
  489. fEngineControl->fTransport.ResetTimebase(refnum);
  490. int res = ClientCloseAux(refnum, client, true);
  491. client->Close();
  492. delete client;
  493. return res;
  494. } else {
  495. return -1;
  496. }
  497. }
  498. // Used for server internal clients or drivers when the RT thread is stopped
  499. int JackEngine::ClientInternalClose(int refnum, bool wait)
  500. {
  501. AssertRefnum(refnum);
  502. JackClientInterface* client = fClientTable[refnum];
  503. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  504. }
  505. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  506. {
  507. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  508. // Unregister all ports ==> notifications are sent
  509. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  510. int i;
  511. fGraphManager->GetInputPorts(refnum, ports);
  512. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  513. PortUnRegister(refnum, ports[i]);
  514. }
  515. fGraphManager->GetOutputPorts(refnum, ports);
  516. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  517. PortUnRegister(refnum, ports[i]);
  518. }
  519. // Remove the client from the table
  520. ReleaseRefnum(refnum);
  521. // Remove all ports
  522. fGraphManager->RemoveAllPorts(refnum);
  523. // Wait until next cycle to be sure client is not used anymore
  524. if (wait) {
  525. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  526. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  527. }
  528. }
  529. // Notify running clients
  530. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  531. // Cleanup...
  532. fSynchroTable[refnum].Destroy();
  533. fEngineControl->ResetRollingUsecs();
  534. return 0;
  535. }
  536. int JackEngine::ClientActivate(int refnum, bool state)
  537. {
  538. AssertRefnum(refnum);
  539. JackClientInterface* client = fClientTable[refnum];
  540. assert(fClientTable[refnum]);
  541. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  542. if (state)
  543. fGraphManager->Activate(refnum);
  544. // Wait for graph state change to be effective
  545. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  546. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  547. return -1;
  548. } else {
  549. NotifyActivate(refnum);
  550. return 0;
  551. }
  552. }
  553. // May be called without client
  554. int JackEngine::ClientDeactivate(int refnum)
  555. {
  556. AssertRefnum(refnum);
  557. JackClientInterface* client = fClientTable[refnum];
  558. if (client == NULL)
  559. return -1;
  560. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  561. // Disconnect all ports ==> notifications are sent
  562. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  563. int i;
  564. fGraphManager->GetInputPorts(refnum, ports);
  565. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  566. PortDisconnect(refnum, ports[i], ALL_PORTS);
  567. }
  568. fGraphManager->GetOutputPorts(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->Deactivate(refnum);
  573. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  574. // Wait for graph state change to be effective
  575. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  576. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  577. return -1;
  578. } else {
  579. return 0;
  580. }
  581. }
  582. //-----------------
  583. // Port management
  584. //-----------------
  585. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  586. {
  587. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  588. AssertRefnum(refnum);
  589. assert(fClientTable[refnum]);
  590. // Check if port name already exists
  591. if (fGraphManager->GetPort(name) != NO_PORT) {
  592. jack_error("port_name \"%s\" already exists", name);
  593. return -1;
  594. }
  595. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  596. if (*port_index != NO_PORT) {
  597. NotifyPortRegistation(*port_index, true);
  598. return 0;
  599. } else {
  600. return -1;
  601. }
  602. }
  603. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  604. {
  605. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  606. AssertRefnum(refnum);
  607. assert(fClientTable[refnum]);
  608. // Disconnect port ==> notification is sent
  609. PortDisconnect(refnum, port_index, ALL_PORTS);
  610. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  611. NotifyPortRegistation(port_index, false);
  612. return 0;
  613. } else {
  614. return -1;
  615. }
  616. }
  617. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  618. {
  619. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  620. jack_port_id_t port_src, port_dst;
  621. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  622. ? -1
  623. : PortConnect(refnum, port_src, port_dst);
  624. }
  625. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  626. {
  627. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  628. AssertRefnum(refnum);
  629. JackClientInterface* client;
  630. int ref;
  631. if (fGraphManager->CheckPorts(src, dst) < 0)
  632. return -1;
  633. ref = fGraphManager->GetOutputRefNum(src);
  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. ref = fGraphManager->GetInputRefNum(dst);
  643. assert(ref >= 0);
  644. client = fClientTable[ref];
  645. assert(client);
  646. if (!client->GetClientControl()->fActive) {
  647. jack_error("Cannot connect ports owned by inactive clients:"
  648. " \"%s\" is not active", client->GetClientControl()->fName);
  649. return -1;
  650. }
  651. int res = fGraphManager->Connect(src, dst);
  652. if (res == 0)
  653. NotifyPortConnect(src, dst, true);
  654. return res;
  655. }
  656. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  657. {
  658. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  659. AssertRefnum(refnum);
  660. jack_port_id_t port_src, port_dst;
  661. if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
  662. return -1;
  663. } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
  664. NotifyPortConnect(port_src, port_dst, false);
  665. return 0;
  666. } else {
  667. return -1;
  668. }
  669. }
  670. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  671. {
  672. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  673. AssertRefnum(refnum);
  674. if (dst == ALL_PORTS) {
  675. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  676. fGraphManager->GetConnections(src, connections);
  677. // Notifications
  678. JackPort* port = fGraphManager->GetPort(src);
  679. if (port->GetFlags() & JackPortIsOutput) {
  680. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  681. jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
  682. NotifyPortConnect(src, connections[i], false);
  683. }
  684. } else {
  685. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  686. jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
  687. NotifyPortConnect(connections[i], src, false);
  688. }
  689. }
  690. return fGraphManager->DisconnectAll(src);
  691. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  692. return -1;
  693. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  694. // Notifications
  695. NotifyPortConnect(src, dst, false);
  696. return 0;
  697. } else {
  698. return -1;
  699. }
  700. }
  701. int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
  702. {
  703. fGraphManager->GetPort(port)->SetName(name);
  704. NotifyPortRename(port);
  705. return 0;
  706. }
  707. } // end of namespace