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.

875 lines
27KB

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