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