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.

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