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.

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