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.

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