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.

1108 lines
35KB

  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 <set>
  18. #include <assert.h>
  19. #include "JackSystemDeps.h"
  20. #include "JackLockedEngine.h"
  21. #include "JackExternalClient.h"
  22. #include "JackInternalClient.h"
  23. #include "JackEngineControl.h"
  24. #include "JackClientControl.h"
  25. #include "JackServerGlobals.h"
  26. #include "JackGlobals.h"
  27. #include "JackChannel.h"
  28. #include "JackError.h"
  29. namespace Jack
  30. {
  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. fLastSwitchUsecs = 0;
  41. fMaxUUID = 0;
  42. fSessionPendingReplies = 0;
  43. fSessionTransaction = NULL;
  44. fSessionResult = NULL;
  45. }
  46. JackEngine::~JackEngine()
  47. {}
  48. int JackEngine::Open()
  49. {
  50. jack_log("JackEngine::Open");
  51. // Open audio thread => request thread communication channel
  52. if (fChannel.Open(fEngineControl->fServerName) < 0) {
  53. jack_error("Cannot connect to server");
  54. return -1;
  55. } else {
  56. return 0;
  57. }
  58. }
  59. int JackEngine::Close()
  60. {
  61. jack_log("JackEngine::Close");
  62. fChannel.Close();
  63. // Close remaining clients (RT is stopped)
  64. for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
  65. if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
  66. jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
  67. loadable_client->Close();
  68. // Close does not delete the pointer for internal clients
  69. fClientTable[i] = NULL;
  70. delete loadable_client;
  71. } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
  72. jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
  73. external_client->Close();
  74. // Close deletes the pointer for external clients
  75. fClientTable[i] = NULL;
  76. }
  77. }
  78. return 0;
  79. }
  80. void JackEngine::ShutDown()
  81. {
  82. jack_log("JackEngine::ShutDown");
  83. // Shutdown remaining clients (RT is stopped)
  84. for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
  85. if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
  86. jack_log("JackEngine::ShutDown loadable client = %s", loadable_client->GetClientControl()->fName);
  87. loadable_client->ShutDown();
  88. }
  89. }
  90. }
  91. void JackEngine::NotifyQuit()
  92. {
  93. fChannel.NotifyQuit();
  94. }
  95. //-----------------------------
  96. // Client ressource management
  97. //-----------------------------
  98. int JackEngine::AllocateRefnum()
  99. {
  100. for (int i = 0; i < CLIENT_NUM; i++) {
  101. if (!fClientTable[i]) {
  102. jack_log("JackEngine::AllocateRefNum ref = %ld", i);
  103. return i;
  104. }
  105. }
  106. return -1;
  107. }
  108. void JackEngine::ReleaseRefnum(int ref)
  109. {
  110. fClientTable[ref] = NULL;
  111. if (fEngineControl->fTemporary) {
  112. int i;
  113. for (i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
  114. if (fClientTable[i])
  115. break;
  116. }
  117. if (i == CLIENT_NUM) {
  118. // last client and temporay case: quit the server
  119. jack_log("JackEngine::ReleaseRefnum server quit");
  120. fEngineControl->fTemporary = false;
  121. throw JackTemporaryException();
  122. }
  123. }
  124. }
  125. //------------------
  126. // Graph management
  127. //------------------
  128. void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
  129. {
  130. fLastSwitchUsecs = cur_cycle_begin;
  131. if (fGraphManager->RunNextGraph()) { // True if the graph actually switched to a new state
  132. fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
  133. }
  134. fSignal.Signal(); // Signal for threads waiting for next cycle
  135. }
  136. void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
  137. {
  138. if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
  139. CheckXRun(cur_cycle_begin);
  140. fGraphManager->RunCurrentGraph();
  141. }
  142. bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  143. {
  144. bool res = true;
  145. // Cycle begin
  146. fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
  147. // Graph
  148. if (fGraphManager->IsFinishedGraph()) {
  149. ProcessNext(cur_cycle_begin);
  150. res = true;
  151. } else {
  152. jack_log("Process: graph not finished!");
  153. if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
  154. jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  155. ProcessNext(cur_cycle_begin);
  156. res = true;
  157. } else {
  158. jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
  159. ProcessCurrent(cur_cycle_begin);
  160. res = false;
  161. }
  162. }
  163. // Cycle end
  164. fEngineControl->CycleEnd(fClientTable);
  165. return res;
  166. }
  167. /*
  168. Client that finish *after* the callback date are considered late even if their output buffers may have been
  169. correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
  170. */
  171. void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
  172. {
  173. for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
  174. JackClientInterface* client = fClientTable[i];
  175. if (client && client->GetClientControl()->fActive) {
  176. JackClientTiming* timing = fGraphManager->GetClientTiming(i);
  177. jack_client_state_t status = timing->fStatus;
  178. jack_time_t finished_date = timing->fFinishedAt;
  179. if (status != NotTriggered && status != Finished) {
  180. jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
  181. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  182. }
  183. if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
  184. jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
  185. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
  186. }
  187. }
  188. }
  189. }
  190. int JackEngine::ComputeTotalLatencies()
  191. {
  192. std::vector<jack_int_t> sorted;
  193. std::vector<jack_int_t>::iterator it;
  194. std::vector<jack_int_t>::reverse_iterator rit;
  195. fGraphManager->TopologicalSort(sorted);
  196. /* iterate over all clients in graph order, and emit
  197. * capture latency callback.
  198. */
  199. for (it = sorted.begin(); it != sorted.end(); it++) {
  200. NotifyClient(*it, kLatencyCallback, true, "", 0, 0);
  201. }
  202. /* now issue playback latency callbacks in reverse graph order.
  203. */
  204. for (rit = sorted.rbegin(); rit != sorted.rend(); rit++) {
  205. NotifyClient(*rit, kLatencyCallback, true, "", 1, 0);
  206. }
  207. return 0;
  208. }
  209. //---------------
  210. // Notifications
  211. //---------------
  212. void JackEngine::NotifyClient(int refnum, int event, int sync, const char* message, int value1, int value2)
  213. {
  214. JackClientInterface* client = fClientTable[refnum];
  215. // The client may be notified by the RT thread while closing
  216. if (client) {
  217. if (client->GetClientControl()->fCallback[event]) {
  218. /*
  219. Important for internal clients : unlock before calling the notification callbacks.
  220. */
  221. bool res = Unlock();
  222. if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0)
  223. jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
  224. if (res)
  225. Lock();
  226. } else {
  227. jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
  228. }
  229. }
  230. }
  231. void JackEngine::NotifyClients(int event, int sync, const char* message, int value1, int value2)
  232. {
  233. for (int i = 0; i < CLIENT_NUM; i++) {
  234. NotifyClient(i, event, sync, message, value1, value2);
  235. }
  236. }
  237. int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
  238. {
  239. jack_log("JackEngine::NotifyAddClient: name = %s", name);
  240. // Notify existing clients of the new client and new client of existing clients.
  241. for (int i = 0; i < CLIENT_NUM; i++) {
  242. JackClientInterface* old_client = fClientTable[i];
  243. if (old_client && old_client != new_client) {
  244. if (old_client->ClientNotify(refnum, name, kAddClient, false, "", 0, 0) < 0) {
  245. jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
  246. // Not considered as a failure...
  247. }
  248. if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, "", 0, 0) < 0) {
  249. jack_error("NotifyAddClient new_client fails name = %s", name);
  250. return -1;
  251. }
  252. }
  253. }
  254. return 0;
  255. }
  256. void JackEngine::NotifyRemoveClient(const char* name, int refnum)
  257. {
  258. // Notify existing clients (including the one beeing suppressed) of the removed client
  259. for (int i = 0; i < CLIENT_NUM; i++) {
  260. JackClientInterface* client = fClientTable[i];
  261. if (client) {
  262. client->ClientNotify(refnum, name, kRemoveClient, false, "", 0, 0);
  263. }
  264. }
  265. }
  266. // Coming from the driver
  267. void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
  268. {
  269. // Use the audio thread => request thread communication channel
  270. fEngineControl->NotifyXRun(callback_usecs, delayed_usecs);
  271. fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
  272. }
  273. void JackEngine::NotifyXRun(int refnum)
  274. {
  275. if (refnum == ALL_CLIENTS) {
  276. NotifyClients(kXRunCallback, false, "", 0, 0);
  277. } else {
  278. NotifyClient(refnum, kXRunCallback, false, "", 0, 0);
  279. }
  280. }
  281. void JackEngine::NotifyGraphReorder()
  282. {
  283. NotifyClients(kGraphOrderCallback, false, "", 0, 0);
  284. ComputeTotalLatencies();
  285. }
  286. void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size)
  287. {
  288. NotifyClients(kBufferSizeCallback, true, "", buffer_size, 0);
  289. }
  290. void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
  291. {
  292. NotifyClients(kSampleRateCallback, true, "", sample_rate, 0);
  293. }
  294. void JackEngine::NotifyFailure(int code, const char* reason)
  295. {
  296. NotifyClients(kShutDownCallback, false, reason, code, 0);
  297. }
  298. void JackEngine::NotifyFreewheel(bool onoff)
  299. {
  300. if (onoff) {
  301. // Save RT state
  302. fEngineControl->fSavedRealTime = fEngineControl->fRealTime;
  303. fEngineControl->fRealTime = false;
  304. } else {
  305. // Restore RT state
  306. fEngineControl->fRealTime = fEngineControl->fSavedRealTime;
  307. fEngineControl->fSavedRealTime = false;
  308. }
  309. NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, "", 0, 0);
  310. }
  311. void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
  312. {
  313. NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, "", port_index, 0);
  314. }
  315. void JackEngine::NotifyPortRename(jack_port_id_t port, const char* old_name)
  316. {
  317. NotifyClients(kPortRenameCallback, false, old_name, port, 0);
  318. }
  319. void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
  320. {
  321. NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, "", src, dst);
  322. }
  323. void JackEngine::NotifyActivate(int refnum)
  324. {
  325. NotifyClient(refnum, kActivateClient, true, "", 0, 0);
  326. }
  327. //----------------------------
  328. // Loadable client management
  329. //----------------------------
  330. int JackEngine::GetInternalClientName(int refnum, char* name_res)
  331. {
  332. JackClientInterface* client = fClientTable[refnum];
  333. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  334. return 0;
  335. }
  336. int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
  337. {
  338. // Clear status
  339. *status = 0;
  340. for (int i = 0; i < CLIENT_NUM; i++) {
  341. JackClientInterface* client = fClientTable[i];
  342. if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
  343. jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
  344. *int_ref = i;
  345. return 0;
  346. }
  347. }
  348. *status |= (JackNoSuchClient | JackFailure);
  349. return -1;
  350. }
  351. int JackEngine::InternalClientUnload(int refnum, int* status)
  352. {
  353. JackClientInterface* client = fClientTable[refnum];
  354. if (client) {
  355. int res = client->Close();
  356. delete client;
  357. *status = 0;
  358. return res;
  359. } else {
  360. *status = (JackNoSuchClient | JackFailure);
  361. return -1;
  362. }
  363. }
  364. //-------------------
  365. // Client management
  366. //-------------------
  367. int JackEngine::ClientCheck(const char* name, int uuid, char* name_res, int protocol, int options, int* status)
  368. {
  369. // Clear status
  370. *status = 0;
  371. strcpy(name_res, name);
  372. jack_log("Check protocol client = %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
  373. if (protocol != JACK_PROTOCOL_VERSION) {
  374. *status |= (JackFailure | JackVersionError);
  375. jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
  376. return -1;
  377. }
  378. std::map<int,std::string>::iterator res = fReservationMap.find(uuid);
  379. if (res != fReservationMap.end()) {
  380. strncpy(name_res, res->second.c_str(), JACK_CLIENT_NAME_SIZE);
  381. } else if (ClientCheckName(name)) {
  382. *status |= JackNameNotUnique;
  383. if (options & JackUseExactName) {
  384. jack_error("cannot create new client; %s already exists", name);
  385. *status |= JackFailure;
  386. return -1;
  387. }
  388. if (GenerateUniqueName(name_res)) {
  389. *status |= JackFailure;
  390. return -1;
  391. }
  392. }
  393. return 0;
  394. }
  395. bool JackEngine::GenerateUniqueName(char* name)
  396. {
  397. int tens, ones;
  398. int length = strlen(name);
  399. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  400. jack_error("%s exists and is too long to make unique", name);
  401. return true; /* failure */
  402. }
  403. /* generate a unique name by appending "-01".."-99" */
  404. name[length++] = '-';
  405. tens = length++;
  406. ones = length++;
  407. name[tens] = '0';
  408. name[ones] = '1';
  409. name[length] = '\0';
  410. while (ClientCheckName(name)) {
  411. if (name[ones] == '9') {
  412. if (name[tens] == '9') {
  413. jack_error("client %s has 99 extra instances already", name);
  414. return true; /* give up */
  415. }
  416. name[tens]++;
  417. name[ones] = '0';
  418. } else {
  419. name[ones]++;
  420. }
  421. }
  422. return false;
  423. }
  424. bool JackEngine::ClientCheckName(const char* name)
  425. {
  426. for (int i = 0; i < CLIENT_NUM; i++) {
  427. JackClientInterface* client = fClientTable[i];
  428. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  429. return true;
  430. }
  431. for (std::map<int,std::string>::iterator i = fReservationMap.begin(); i != fReservationMap.end(); i++) {
  432. if (i->second == name)
  433. return true;
  434. }
  435. return false;
  436. }
  437. int JackEngine::GetNewUUID()
  438. {
  439. return fMaxUUID++;
  440. }
  441. void JackEngine::EnsureUUID(int uuid)
  442. {
  443. if (uuid > fMaxUUID)
  444. fMaxUUID = uuid+1;
  445. for (int i = 0; i < CLIENT_NUM; i++) {
  446. JackClientInterface* client = fClientTable[i];
  447. if (client && (client->GetClientControl()->fSessionID == uuid)) {
  448. client->GetClientControl()->fSessionID = GetNewUUID();
  449. }
  450. }
  451. }
  452. int JackEngine::GetClientPID(const char* name)
  453. {
  454. for (int i = 0; i < CLIENT_NUM; i++) {
  455. JackClientInterface* client = fClientTable[i];
  456. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  457. return client->GetClientControl()->fPID;
  458. }
  459. return 0;
  460. }
  461. int JackEngine::GetClientRefNum(const char* name)
  462. {
  463. for (int i = 0; i < CLIENT_NUM; i++) {
  464. JackClientInterface* client = fClientTable[i];
  465. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  466. return client->GetClientControl()->fRefNum;
  467. }
  468. return -1;
  469. }
  470. // Used for external clients
  471. int JackEngine::ClientExternalOpen(const char* name, int pid, int uuid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  472. {
  473. char real_name[JACK_CLIENT_NAME_SIZE + 1];
  474. if (uuid < 0) {
  475. uuid = GetNewUUID();
  476. strncpy(real_name, name, JACK_CLIENT_NAME_SIZE);
  477. } else {
  478. std::map<int, std::string>::iterator res = fReservationMap.find(uuid);
  479. if (res != fReservationMap.end()) {
  480. strncpy(real_name, res->second.c_str(), JACK_CLIENT_NAME_SIZE);
  481. fReservationMap.erase(uuid);
  482. } else {
  483. strncpy(real_name, name, JACK_CLIENT_NAME_SIZE);
  484. }
  485. EnsureUUID(uuid);
  486. }
  487. jack_log("JackEngine::ClientExternalOpen: uuid = %d, name = %s ", uuid, real_name);
  488. int refnum = AllocateRefnum();
  489. if (refnum < 0) {
  490. jack_error("No more refnum available");
  491. return -1;
  492. }
  493. JackExternalClient* client = new JackExternalClient();
  494. if (!fSynchroTable[refnum].Allocate(real_name, fEngineControl->fServerName, 0)) {
  495. jack_error("Cannot allocate synchro");
  496. goto error;
  497. }
  498. if (client->Open(real_name, pid, refnum, uuid, shared_client) < 0) {
  499. jack_error("Cannot open client");
  500. goto error;
  501. }
  502. if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  503. // Failure if RT thread is not running (problem with the driver...)
  504. jack_error("Driver is not running");
  505. goto error;
  506. }
  507. fClientTable[refnum] = client;
  508. if (NotifyAddClient(client, real_name, refnum) < 0) {
  509. jack_error("Cannot notify add client");
  510. goto error;
  511. }
  512. fGraphManager->InitRefNum(refnum);
  513. fEngineControl->ResetRollingUsecs();
  514. *shared_engine = fEngineControl->GetShmIndex();
  515. *shared_graph_manager = fGraphManager->GetShmIndex();
  516. *ref = refnum;
  517. return 0;
  518. error:
  519. // Cleanup...
  520. fSynchroTable[refnum].Destroy();
  521. fClientTable[refnum] = 0;
  522. client->Close();
  523. delete client;
  524. return -1;
  525. }
  526. // Used for server driver clients
  527. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  528. {
  529. jack_log("JackEngine::ClientInternalOpen: name = %s", name);
  530. int refnum = AllocateRefnum();
  531. if (refnum < 0) {
  532. jack_error("No more refnum available");
  533. goto error;
  534. }
  535. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  536. jack_error("Cannot allocate synchro");
  537. goto error;
  538. }
  539. if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  540. // Failure if RT thread is not running (problem with the driver...)
  541. jack_error("Driver is not running");
  542. goto error;
  543. }
  544. fClientTable[refnum] = client;
  545. if (NotifyAddClient(client, name, refnum) < 0) {
  546. jack_error("Cannot notify add client");
  547. goto error;
  548. }
  549. fGraphManager->InitRefNum(refnum);
  550. fEngineControl->ResetRollingUsecs();
  551. *shared_engine = fEngineControl;
  552. *shared_manager = fGraphManager;
  553. *ref = refnum;
  554. return 0;
  555. error:
  556. // Cleanup...
  557. fSynchroTable[refnum].Destroy();
  558. fClientTable[refnum] = 0;
  559. return -1;
  560. }
  561. // Used for external clients
  562. int JackEngine::ClientExternalClose(int refnum)
  563. {
  564. JackClientInterface* client = fClientTable[refnum];
  565. fEngineControl->fTransport.ResetTimebase(refnum);
  566. int res = ClientCloseAux(refnum, client, true);
  567. client->Close();
  568. delete client;
  569. return res;
  570. }
  571. // Used for server internal clients or drivers when the RT thread is stopped
  572. int JackEngine::ClientInternalClose(int refnum, bool wait)
  573. {
  574. JackClientInterface* client = fClientTable[refnum];
  575. return ClientCloseAux(refnum, client, wait);
  576. }
  577. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  578. {
  579. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  580. // Unregister all ports ==> notifications are sent
  581. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  582. int i;
  583. fGraphManager->GetInputPorts(refnum, ports);
  584. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY); i++) {
  585. PortUnRegister(refnum, ports[i]);
  586. }
  587. fGraphManager->GetOutputPorts(refnum, ports);
  588. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY); i++) {
  589. PortUnRegister(refnum, ports[i]);
  590. }
  591. // Remove the client from the table
  592. ReleaseRefnum(refnum);
  593. // Remove all ports
  594. fGraphManager->RemoveAllPorts(refnum);
  595. // Wait until next cycle to be sure client is not used anymore
  596. if (wait) {
  597. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  598. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  599. }
  600. }
  601. // Notify running clients
  602. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  603. // Cleanup...
  604. fSynchroTable[refnum].Destroy();
  605. fEngineControl->ResetRollingUsecs();
  606. return 0;
  607. }
  608. int JackEngine::ClientActivate(int refnum, bool is_real_time)
  609. {
  610. JackClientInterface* client = fClientTable[refnum];
  611. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  612. if (is_real_time)
  613. fGraphManager->Activate(refnum);
  614. // Wait for graph state change to be effective
  615. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  616. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  617. return -1;
  618. } else {
  619. jack_int_t input_ports[PORT_NUM_FOR_CLIENT];
  620. jack_int_t output_ports[PORT_NUM_FOR_CLIENT];
  621. fGraphManager->GetInputPorts(refnum, input_ports);
  622. fGraphManager->GetOutputPorts(refnum, output_ports);
  623. // Notify client
  624. NotifyActivate(refnum);
  625. // Then issue port registration notification
  626. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  627. NotifyPortRegistation(input_ports[i], true);
  628. }
  629. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  630. NotifyPortRegistation(output_ports[i], true);
  631. }
  632. return 0;
  633. }
  634. }
  635. // May be called without client
  636. int JackEngine::ClientDeactivate(int refnum)
  637. {
  638. JackClientInterface* client = fClientTable[refnum];
  639. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  640. jack_int_t input_ports[PORT_NUM_FOR_CLIENT];
  641. jack_int_t output_ports[PORT_NUM_FOR_CLIENT];
  642. fGraphManager->GetInputPorts(refnum, input_ports);
  643. fGraphManager->GetOutputPorts(refnum, output_ports);
  644. // First disconnect all ports
  645. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  646. PortDisconnect(refnum, input_ports[i], ALL_PORTS);
  647. }
  648. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  649. PortDisconnect(refnum, output_ports[i], ALL_PORTS);
  650. }
  651. // Then issue port registration notification
  652. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  653. NotifyPortRegistation(input_ports[i], false);
  654. }
  655. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  656. NotifyPortRegistation(output_ports[i], false);
  657. }
  658. fGraphManager->Deactivate(refnum);
  659. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  660. // Wait for graph state change to be effective
  661. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  662. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  663. return -1;
  664. } else {
  665. return 0;
  666. }
  667. }
  668. //-----------------
  669. // Port management
  670. //-----------------
  671. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  672. {
  673. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  674. JackClientInterface* client = fClientTable[refnum];
  675. // Check if port name already exists
  676. if (fGraphManager->GetPort(name) != NO_PORT) {
  677. jack_error("port_name \"%s\" already exists", name);
  678. return -1;
  679. }
  680. // buffer_size is actually ignored...
  681. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  682. if (*port_index != NO_PORT) {
  683. if (client->GetClientControl()->fActive)
  684. NotifyPortRegistation(*port_index, true);
  685. return 0;
  686. } else {
  687. return -1;
  688. }
  689. }
  690. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  691. {
  692. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  693. JackClientInterface* client = fClientTable[refnum];
  694. // Disconnect port ==> notification is sent
  695. PortDisconnect(refnum, port_index, ALL_PORTS);
  696. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  697. if (client->GetClientControl()->fActive)
  698. NotifyPortRegistation(port_index, false);
  699. return 0;
  700. } else {
  701. return -1;
  702. }
  703. }
  704. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  705. {
  706. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  707. jack_port_id_t port_src, port_dst;
  708. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  709. ? -1
  710. : PortConnect(refnum, port_src, port_dst);
  711. }
  712. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  713. {
  714. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  715. JackClientInterface* client;
  716. int ref;
  717. if (fGraphManager->CheckPorts(src, dst) < 0)
  718. return -1;
  719. ref = fGraphManager->GetOutputRefNum(src);
  720. assert(ref >= 0);
  721. client = fClientTable[ref];
  722. assert(client);
  723. if (!client->GetClientControl()->fActive) {
  724. jack_error("Cannot connect ports owned by inactive clients:"
  725. " \"%s\" is not active", client->GetClientControl()->fName);
  726. return -1;
  727. }
  728. ref = fGraphManager->GetInputRefNum(dst);
  729. assert(ref >= 0);
  730. client = fClientTable[ref];
  731. assert(client);
  732. if (!client->GetClientControl()->fActive) {
  733. jack_error("Cannot connect ports owned by inactive clients:"
  734. " \"%s\" is not active", client->GetClientControl()->fName);
  735. return -1;
  736. }
  737. int res = fGraphManager->Connect(src, dst);
  738. if (res == 0)
  739. NotifyPortConnect(src, dst, true);
  740. return res;
  741. }
  742. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  743. {
  744. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  745. jack_port_id_t port_src, port_dst;
  746. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  747. ? -1
  748. : PortDisconnect(refnum, port_src, port_dst);
  749. }
  750. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  751. {
  752. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  753. if (dst == ALL_PORTS) {
  754. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  755. fGraphManager->GetConnections(src, connections);
  756. JackPort* port = fGraphManager->GetPort(src);
  757. int ret = 0;
  758. if (port->GetFlags() & JackPortIsOutput) {
  759. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  760. if (PortDisconnect(refnum, src, connections[i]) != 0) {
  761. ret = -1;
  762. }
  763. }
  764. } else {
  765. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  766. if (PortDisconnect(refnum, connections[i], src) != 0) {
  767. ret = -1;
  768. }
  769. }
  770. }
  771. return ret;
  772. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  773. return -1;
  774. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  775. // Notifications
  776. NotifyPortConnect(src, dst, false);
  777. return 0;
  778. } else {
  779. return -1;
  780. }
  781. }
  782. int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
  783. {
  784. char old_name[REAL_JACK_PORT_NAME_SIZE];
  785. strcpy(old_name, fGraphManager->GetPort(port)->GetName());
  786. fGraphManager->GetPort(port)->SetName(name);
  787. NotifyPortRename(port, old_name);
  788. return 0;
  789. }
  790. //--------------------
  791. // Session management
  792. //--------------------
  793. void JackEngine::SessionNotify(int refnum, const char *target, jack_session_event_type_t type, const char *path, detail::JackChannelTransactionInterface *socket, JackSessionNotifyResult** result)
  794. {
  795. if (fSessionPendingReplies != 0) {
  796. JackSessionNotifyResult res(-1);
  797. res.Write(socket);
  798. jack_log("JackEngine::SessionNotify ... busy");
  799. if (result != NULL) {
  800. *result = NULL;
  801. }
  802. return;
  803. }
  804. for (int i = 0; i < CLIENT_NUM; i++) {
  805. JackClientInterface* client = fClientTable[i];
  806. if (client && (client->GetClientControl()->fSessionID < 0)) {
  807. client->GetClientControl()->fSessionID = GetNewUUID();
  808. }
  809. }
  810. fSessionResult = new JackSessionNotifyResult();
  811. for (int i = 0; i < CLIENT_NUM; i++) {
  812. JackClientInterface* client = fClientTable[i];
  813. if (client && client->GetClientControl()->fCallback[kSessionCallback]) {
  814. // check if this is a notification to a specific client.
  815. if (target != NULL && strlen(target) != 0) {
  816. if (strcmp(target, client->GetClientControl()->fName)) {
  817. continue;
  818. }
  819. }
  820. char path_buf[JACK_PORT_NAME_SIZE];
  821. snprintf(path_buf, sizeof(path_buf), "%s%s%c", path, client->GetClientControl()->fName, DIR_SEPARATOR);
  822. int res = JackTools::MkDir(path_buf);
  823. if (res)
  824. jack_error("JackEngine::SessionNotify: can not create session directory '%s'", path_buf);
  825. int result = client->ClientNotify(i, client->GetClientControl()->fName, kSessionCallback, true, path_buf, (int)type, 0);
  826. if (result == kPendingSessionReply) {
  827. fSessionPendingReplies += 1;
  828. } else if (result == kImmediateSessionReply) {
  829. char uuid_buf[JACK_UUID_SIZE];
  830. snprintf(uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID);
  831. fSessionResult->fCommandList.push_back(JackSessionCommand(uuid_buf,
  832. client->GetClientControl()->fName,
  833. client->GetClientControl()->fSessionCommand,
  834. client->GetClientControl()->fSessionFlags));
  835. }
  836. }
  837. }
  838. if (result != NULL) {
  839. *result = fSessionResult;
  840. }
  841. if (fSessionPendingReplies == 0) {
  842. fSessionResult->Write(socket);
  843. if (result == NULL) {
  844. delete fSessionResult;
  845. }
  846. fSessionResult = NULL;
  847. } else {
  848. fSessionTransaction = socket;
  849. }
  850. }
  851. int JackEngine::SessionReply(int refnum)
  852. {
  853. JackClientInterface* client = fClientTable[refnum];
  854. char uuid_buf[JACK_UUID_SIZE];
  855. snprintf(uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID);
  856. fSessionResult->fCommandList.push_back(JackSessionCommand(uuid_buf,
  857. client->GetClientControl()->fName,
  858. client->GetClientControl()->fSessionCommand,
  859. client->GetClientControl()->fSessionFlags));
  860. fSessionPendingReplies -= 1;
  861. if (fSessionPendingReplies == 0) {
  862. fSessionResult->Write(fSessionTransaction);
  863. if (fSessionTransaction != NULL)
  864. {
  865. delete fSessionResult;
  866. }
  867. fSessionResult = NULL;
  868. }
  869. return 0;
  870. }
  871. int JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res)
  872. {
  873. for (int i = 0; i < CLIENT_NUM; i++) {
  874. JackClientInterface* client = fClientTable[i];
  875. if (client && (strcmp(client_name, client->GetClientControl()->fName) == 0)) {
  876. snprintf(uuid_res, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
  877. return 0;
  878. }
  879. }
  880. // Did not find name.
  881. return -1;
  882. }
  883. int JackEngine::GetClientNameForUUID(const char *uuid, char *name_res)
  884. {
  885. for (int i = 0; i < CLIENT_NUM; i++) {
  886. JackClientInterface* client = fClientTable[i];
  887. if (!client) {
  888. continue;
  889. }
  890. char uuid_buf[JACK_UUID_SIZE];
  891. snprintf(uuid_buf, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
  892. if (strcmp(uuid,uuid_buf) == 0) {
  893. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  894. return 0;
  895. }
  896. }
  897. // Did not find uuid.
  898. return -1;
  899. }
  900. int JackEngine::ReserveClientName(const char *name, const char *uuid)
  901. {
  902. jack_log("JackEngine::ReserveClientName ( name = %s, uuid = %s )", name, uuid);
  903. if (ClientCheckName(name)) {
  904. jack_log("name already taken");
  905. return -1;
  906. }
  907. EnsureUUID(atoi(uuid));
  908. fReservationMap[atoi(uuid)] = name;
  909. return 0;
  910. }
  911. int JackEngine::ClientHasSessionCallback(const char *name)
  912. {
  913. JackClientInterface* client = NULL;
  914. for (int i = 0; i < CLIENT_NUM; i++) {
  915. client = fClientTable[i];
  916. if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) {
  917. break;
  918. }
  919. }
  920. if (client) {
  921. return client->GetClientControl()->fCallback[kSessionCallback];
  922. } else {
  923. return -1;
  924. }
  925. }
  926. } // end of namespace