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.

899 lines
28KB

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