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.

1047 lines
33KB

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