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.

839 lines
25KB

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