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.

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