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.

848 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. JackServerGlobals::fKilled = true;
  104. }
  105. }
  106. }
  107. //------------------
  108. // Graph management
  109. //------------------
  110. void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
  111. {
  112. fLastSwitchUsecs = cur_cycle_begin;
  113. if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
  114. fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
  115. fSignal.Signal(); // Signal for threads waiting for next cycle
  116. }
  117. void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
  118. {
  119. if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
  120. CheckXRun(cur_cycle_begin);
  121. fGraphManager->RunCurrentGraph();
  122. }
  123. bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  124. {
  125. bool res = true;
  126. // Cycle begin
  127. fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
  128. // Graph
  129. if (fGraphManager->IsFinishedGraph()) {
  130. ProcessNext(cur_cycle_begin);
  131. res = true;
  132. } else {
  133. jack_log("Process: graph not finished!");
  134. if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  135. jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  136. ProcessNext(cur_cycle_begin);
  137. res = true;
  138. } else {
  139. jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  140. ProcessCurrent(cur_cycle_begin);
  141. res = false;
  142. }
  143. }
  144. // Cycle end
  145. fEngineControl->CycleEnd(fClientTable);
  146. return res;
  147. }
  148. /*
  149. Client that finish *after* the callback date are considered late even if their output buffers may have been
  150. correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
  151. */
  152. void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
  153. {
  154. for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
  155. JackClientInterface* client = fClientTable[i];
  156. if (client && client->GetClientControl()->fActive) {
  157. JackClientTiming* timing = fGraphManager->GetClientTiming(i);
  158. jack_client_state_t status = timing->fStatus;
  159. jack_time_t finished_date = timing->fFinishedAt;
  160. if (status != NotTriggered && status != Finished) {
  161. jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
  162. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  163. }
  164. if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
  165. jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
  166. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  167. }
  168. }
  169. }
  170. }
  171. //---------------
  172. // Notifications
  173. //---------------
  174. void JackEngine::NotifyClient(int refnum, int event, int sync, const char* message, int value1, int value2)
  175. {
  176. JackClientInterface* client = fClientTable[refnum];
  177. // The client may be notified by the RT thread while closing
  178. if (!client) {
  179. jack_log("JackEngine::NotifyClient: client not available anymore");
  180. } else if (client->GetClientControl()->fCallback[event]) {
  181. if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0)
  182. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  183. } else {
  184. jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
  185. }
  186. }
  187. void JackEngine::NotifyClients(int event, int sync, const char* message, int value1, int value2)
  188. {
  189. for (int i = 0; i < CLIENT_NUM; i++) {
  190. JackClientInterface* client = fClientTable[i];
  191. if (client) {
  192. if (client->GetClientControl()->fCallback[event]) {
  193. if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0)
  194. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  195. } else {
  196. jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
  197. }
  198. }
  199. }
  200. }
  201. int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
  202. {
  203. jack_log("JackEngine::NotifyAddClient: name = %s", name);
  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::NotifyFailure(int code, const char* reason)
  259. {
  260. NotifyClients(kShutDownCallback, false, reason, code, 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::ClientExternalOpen: 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::ClientInternalOpen: 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 is_real_time)
  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 (is_real_time)
  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. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  622. {
  623. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  624. AssertRefnum(refnum);
  625. jack_port_id_t port_src, port_dst;
  626. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  627. ? -1
  628. : PortConnect(refnum, port_src, port_dst);
  629. }
  630. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  631. {
  632. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  633. AssertRefnum(refnum);
  634. JackClientInterface* client;
  635. int ref;
  636. if (fGraphManager->CheckPorts(src, dst) < 0)
  637. return -1;
  638. ref = fGraphManager->GetOutputRefNum(src);
  639. assert(ref >= 0);
  640. client = fClientTable[ref];
  641. assert(client);
  642. if (!client->GetClientControl()->fActive) {
  643. jack_error("Cannot connect ports owned by inactive clients:"
  644. " \"%s\" is not active", client->GetClientControl()->fName);
  645. return -1;
  646. }
  647. ref = fGraphManager->GetInputRefNum(dst);
  648. assert(ref >= 0);
  649. client = fClientTable[ref];
  650. assert(client);
  651. if (!client->GetClientControl()->fActive) {
  652. jack_error("Cannot connect ports owned by inactive clients:"
  653. " \"%s\" is not active", client->GetClientControl()->fName);
  654. return -1;
  655. }
  656. int res = fGraphManager->Connect(src, dst);
  657. if (res == 0)
  658. NotifyPortConnect(src, dst, true);
  659. return res;
  660. }
  661. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  662. {
  663. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  664. AssertRefnum(refnum);
  665. jack_port_id_t port_src, port_dst;
  666. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  667. ? -1
  668. : PortDisconnect(refnum, port_src, port_dst);
  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. JackPort* port = fGraphManager->GetPort(src);
  678. int ret = 0;
  679. if (port->GetFlags() & JackPortIsOutput) {
  680. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  681. if (PortDisconnect(refnum, src, connections[i]) != 0) {
  682. ret = -1;
  683. }
  684. }
  685. } else {
  686. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  687. if (PortDisconnect(refnum, connections[i], src) != 0) {
  688. ret = -1;
  689. }
  690. }
  691. }
  692. return ret;
  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. AssertRefnum(refnum);
  706. fGraphManager->GetPort(port)->SetName(name);
  707. NotifyPortRename(port);
  708. return 0;
  709. }
  710. } // end of namespace