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.

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