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.

835 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. #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. // Used for external clients
  399. int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  400. {
  401. jack_log("JackEngine::ClientOpen: name = %s ", name);
  402. int refnum = AllocateRefnum();
  403. if (refnum < 0) {
  404. jack_error("No more refnum available");
  405. return -1;
  406. }
  407. JackExternalClient* client = new JackExternalClient();
  408. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  409. jack_error("Cannot allocate synchro");
  410. goto error;
  411. }
  412. if (client->Open(name, pid, refnum, shared_client) < 0) {
  413. jack_error("Cannot open client");
  414. goto error;
  415. }
  416. if (!fSignal.TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  417. // Failure if RT thread is not running (problem with the driver...)
  418. jack_error("Driver is not running");
  419. goto error;
  420. }
  421. fClientTable[refnum] = client;
  422. if (NotifyAddClient(client, name, refnum) < 0) {
  423. jack_error("Cannot notify add client");
  424. goto error;
  425. }
  426. fGraphManager->InitRefNum(refnum);
  427. fEngineControl->ResetRollingUsecs();
  428. *shared_engine = fEngineControl->GetShmIndex();
  429. *shared_graph_manager = fGraphManager->GetShmIndex();
  430. *ref = refnum;
  431. return 0;
  432. error:
  433. // Cleanup...
  434. fSynchroTable[refnum].Destroy();
  435. fClientTable[refnum] = 0;
  436. client->Close();
  437. delete client;
  438. return -1;
  439. }
  440. // Used for server driver clients
  441. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  442. {
  443. jack_log("JackEngine::ClientInternalNew: name = %s", name);
  444. int refnum = AllocateRefnum();
  445. if (refnum < 0) {
  446. jack_error("No more refnum available");
  447. goto error;
  448. }
  449. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  450. jack_error("Cannot allocate synchro");
  451. goto error;
  452. }
  453. if (wait && !fSignal.TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  454. // Failure if RT thread is not running (problem with the driver...)
  455. jack_error("Driver is not running");
  456. goto error;
  457. }
  458. fClientTable[refnum] = client;
  459. if (NotifyAddClient(client, name, refnum) < 0) {
  460. jack_error("Cannot notify add client");
  461. goto error;
  462. }
  463. fGraphManager->InitRefNum(refnum);
  464. fEngineControl->ResetRollingUsecs();
  465. *shared_engine = fEngineControl;
  466. *shared_manager = fGraphManager;
  467. *ref = refnum;
  468. return 0;
  469. error:
  470. // Cleanup...
  471. fSynchroTable[refnum].Destroy();
  472. fClientTable[refnum] = 0;
  473. return -1;
  474. }
  475. // Used for external clients
  476. int JackEngine::ClientExternalClose(int refnum)
  477. {
  478. AssertRefnum(refnum);
  479. JackClientInterface* client = fClientTable[refnum];
  480. if (client) {
  481. fEngineControl->fTransport.ResetTimebase(refnum);
  482. int res = ClientCloseAux(refnum, client, true);
  483. client->Close();
  484. delete client;
  485. return res;
  486. } else {
  487. return -1;
  488. }
  489. }
  490. // Used for server internal clients or drivers when the RT thread is stopped
  491. int JackEngine::ClientInternalClose(int refnum, bool wait)
  492. {
  493. AssertRefnum(refnum);
  494. JackClientInterface* client = fClientTable[refnum];
  495. return (client) ? ClientCloseAux(refnum, client, wait) : -1;
  496. }
  497. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  498. {
  499. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  500. // Unregister all ports ==> notifications are sent
  501. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  502. int i;
  503. fGraphManager->GetInputPorts(refnum, ports);
  504. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  505. PortUnRegister(refnum, ports[i]);
  506. }
  507. fGraphManager->GetOutputPorts(refnum, ports);
  508. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  509. PortUnRegister(refnum, ports[i]);
  510. }
  511. // Remove the client from the table
  512. ReleaseRefnum(refnum);
  513. // Remove all ports
  514. fGraphManager->RemoveAllPorts(refnum);
  515. // Wait until next cycle to be sure client is not used anymore
  516. if (wait) {
  517. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  518. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  519. }
  520. }
  521. // Notify running clients
  522. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  523. // Cleanup...
  524. fSynchroTable[refnum].Destroy();
  525. fEngineControl->ResetRollingUsecs();
  526. return 0;
  527. }
  528. int JackEngine::ClientActivate(int refnum, bool state)
  529. {
  530. AssertRefnum(refnum);
  531. JackClientInterface* client = fClientTable[refnum];
  532. assert(fClientTable[refnum]);
  533. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  534. if (state)
  535. fGraphManager->Activate(refnum);
  536. // Wait for graph state change to be effective
  537. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  538. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  539. return -1;
  540. } else {
  541. NotifyActivate(refnum);
  542. return 0;
  543. }
  544. }
  545. // May be called without client
  546. int JackEngine::ClientDeactivate(int refnum)
  547. {
  548. AssertRefnum(refnum);
  549. JackClientInterface* client = fClientTable[refnum];
  550. if (client == NULL)
  551. return -1;
  552. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  553. // Disconnect all ports ==> notifications are sent
  554. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  555. int i;
  556. fGraphManager->GetInputPorts(refnum, ports);
  557. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  558. PortDisconnect(refnum, ports[i], ALL_PORTS);
  559. }
  560. fGraphManager->GetOutputPorts(refnum, ports);
  561. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  562. PortDisconnect(refnum, ports[i], ALL_PORTS);
  563. }
  564. fGraphManager->Deactivate(refnum);
  565. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  566. // Wait for graph state change to be effective
  567. if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  568. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  569. return -1;
  570. } else {
  571. return 0;
  572. }
  573. }
  574. //-----------------
  575. // Port management
  576. //-----------------
  577. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  578. {
  579. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  580. AssertRefnum(refnum);
  581. assert(fClientTable[refnum]);
  582. // Check if port name already exists
  583. if (fGraphManager->GetPort(name) != NO_PORT) {
  584. jack_error("port_name \"%s\" already exists", name);
  585. return -1;
  586. }
  587. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  588. if (*port_index != NO_PORT) {
  589. NotifyPortRegistation(*port_index, true);
  590. return 0;
  591. } else {
  592. return -1;
  593. }
  594. }
  595. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  596. {
  597. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  598. AssertRefnum(refnum);
  599. assert(fClientTable[refnum]);
  600. // Disconnect port ==> notification is sent
  601. PortDisconnect(refnum, port_index, ALL_PORTS);
  602. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  603. NotifyPortRegistation(port_index, false);
  604. return 0;
  605. } else {
  606. return -1;
  607. }
  608. }
  609. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  610. {
  611. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  612. jack_port_id_t port_src, port_dst;
  613. return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
  614. ? -1
  615. : PortConnect(refnum, port_src, port_dst);
  616. }
  617. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  618. {
  619. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  620. AssertRefnum(refnum);
  621. JackClientInterface* client;
  622. int ref;
  623. if (fGraphManager->CheckPorts(src, dst) < 0)
  624. return -1;
  625. ref = fGraphManager->GetOutputRefNum(src);
  626. assert(ref >= 0);
  627. client = fClientTable[ref];
  628. assert(client);
  629. if (!client->GetClientControl()->fActive) {
  630. jack_error("Cannot connect ports owned by inactive clients:"
  631. " \"%s\" is not active", client->GetClientControl()->fName);
  632. return -1;
  633. }
  634. ref = fGraphManager->GetInputRefNum(dst);
  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. int res = fGraphManager->Connect(src, dst);
  644. if (res == 0)
  645. NotifyPortConnect(src, dst, true);
  646. return res;
  647. }
  648. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  649. {
  650. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  651. AssertRefnum(refnum);
  652. jack_port_id_t port_src, port_dst;
  653. if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
  654. return -1;
  655. } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
  656. NotifyPortConnect(port_src, port_dst, false);
  657. return 0;
  658. } else {
  659. return -1;
  660. }
  661. }
  662. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  663. {
  664. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  665. AssertRefnum(refnum);
  666. if (dst == ALL_PORTS) {
  667. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  668. fGraphManager->GetConnections(src, connections);
  669. // Notifications
  670. JackPort* port = fGraphManager->GetPort(src);
  671. if (port->GetFlags() & JackPortIsOutput) {
  672. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  673. jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
  674. NotifyPortConnect(src, connections[i], false);
  675. }
  676. } else {
  677. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  678. jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
  679. NotifyPortConnect(connections[i], src, false);
  680. }
  681. }
  682. return fGraphManager->DisconnectAll(src);
  683. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  684. return -1;
  685. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  686. // Notifications
  687. NotifyPortConnect(src, dst, false);
  688. return 0;
  689. } else {
  690. return -1;
  691. }
  692. }
  693. int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
  694. {
  695. fGraphManager->GetPort(port)->SetName(name);
  696. NotifyPortRename(port);
  697. return 0;
  698. }
  699. } // end of namespace