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