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.

1037 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, 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. if (ClientCheckName(name)) {
  346. *status |= JackNameNotUnique;
  347. if (options & JackUseExactName) {
  348. jack_error("cannot create new client; %s already exists", name);
  349. *status |= JackFailure;
  350. return -1;
  351. }
  352. if (GenerateUniqueName(name_res)) {
  353. *status |= JackFailure;
  354. return -1;
  355. }
  356. }
  357. return 0;
  358. }
  359. bool JackEngine::GenerateUniqueName(char* name)
  360. {
  361. int tens, ones;
  362. int length = strlen(name);
  363. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  364. jack_error("%s exists and is too long to make unique", name);
  365. return true; /* failure */
  366. }
  367. /* generate a unique name by appending "-01".."-99" */
  368. name[length++] = '-';
  369. tens = length++;
  370. ones = length++;
  371. name[tens] = '0';
  372. name[ones] = '1';
  373. name[length] = '\0';
  374. while (ClientCheckName(name)) {
  375. if (name[ones] == '9') {
  376. if (name[tens] == '9') {
  377. jack_error("client %s has 99 extra instances already", name);
  378. return true; /* give up */
  379. }
  380. name[tens]++;
  381. name[ones] = '0';
  382. } else {
  383. name[ones]++;
  384. }
  385. }
  386. return false;
  387. }
  388. bool JackEngine::ClientCheckName(const char* name)
  389. {
  390. for (int i = 0; i < CLIENT_NUM; i++) {
  391. JackClientInterface* client = fClientTable[i];
  392. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  393. return true;
  394. }
  395. for (std::map<int,std::string>::iterator i=fReservationMap.begin(); i!=fReservationMap.end(); i++) {
  396. if (i->second == name)
  397. return true;
  398. }
  399. return false;
  400. }
  401. int JackEngine::GetNewUUID()
  402. {
  403. return fMaxUUID++;
  404. }
  405. void JackEngine::EnsureUUID(int uuid)
  406. {
  407. if (uuid > fMaxUUID)
  408. fMaxUUID = uuid+1;
  409. for (int i = 0; i < CLIENT_NUM; i++) {
  410. JackClientInterface* client = fClientTable[i];
  411. if (client && (client->GetClientControl()->fSessionID==uuid)) {
  412. client->GetClientControl()->fSessionID = GetNewUUID();
  413. }
  414. }
  415. }
  416. int JackEngine::GetClientPID(const char* name)
  417. {
  418. for (int i = 0; i < CLIENT_NUM; i++) {
  419. JackClientInterface* client = fClientTable[i];
  420. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  421. return client->GetClientControl()->fPID;
  422. }
  423. return 0;
  424. }
  425. int JackEngine::GetClientRefNum(const char* name)
  426. {
  427. for (int i = 0; i < CLIENT_NUM; i++) {
  428. JackClientInterface* client = fClientTable[i];
  429. if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
  430. return client->GetClientControl()->fRefNum;
  431. }
  432. return -1;
  433. }
  434. // Used for external clients
  435. int JackEngine::ClientExternalOpen(const char* name, int pid, int uuid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  436. {
  437. char real_name[JACK_CLIENT_NAME_SIZE+1];
  438. if (uuid < 0) {
  439. uuid = GetNewUUID();
  440. strncpy( real_name, name, JACK_CLIENT_NAME_SIZE );
  441. } else {
  442. std::map<int,std::string>::iterator res = fReservationMap.find(uuid);
  443. if (res != fReservationMap.end()) {
  444. strncpy( real_name, res->second.c_str(), JACK_CLIENT_NAME_SIZE );
  445. fReservationMap.erase(uuid);
  446. } else {
  447. strncpy( real_name, name, JACK_CLIENT_NAME_SIZE );
  448. }
  449. EnsureUUID(uuid);
  450. }
  451. jack_log("JackEngine::ClientExternalOpen: name = %s ", real_name);
  452. int refnum = AllocateRefnum();
  453. if (refnum < 0) {
  454. jack_error("No more refnum available");
  455. return -1;
  456. }
  457. JackExternalClient* client = new JackExternalClient();
  458. if (!fSynchroTable[refnum].Allocate(real_name, fEngineControl->fServerName, 0)) {
  459. jack_error("Cannot allocate synchro");
  460. goto error;
  461. }
  462. if (client->Open(real_name, pid, refnum, uuid, shared_client) < 0) {
  463. jack_error("Cannot open client");
  464. goto error;
  465. }
  466. if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  467. // Failure if RT thread is not running (problem with the driver...)
  468. jack_error("Driver is not running");
  469. goto error;
  470. }
  471. fClientTable[refnum] = client;
  472. if (NotifyAddClient(client, real_name, refnum) < 0) {
  473. jack_error("Cannot notify add client");
  474. goto error;
  475. }
  476. fGraphManager->InitRefNum(refnum);
  477. fEngineControl->ResetRollingUsecs();
  478. *shared_engine = fEngineControl->GetShmIndex();
  479. *shared_graph_manager = fGraphManager->GetShmIndex();
  480. *ref = refnum;
  481. return 0;
  482. error:
  483. // Cleanup...
  484. fSynchroTable[refnum].Destroy();
  485. fClientTable[refnum] = 0;
  486. client->Close();
  487. delete client;
  488. return -1;
  489. }
  490. // Used for server driver clients
  491. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  492. {
  493. jack_log("JackEngine::ClientInternalOpen: name = %s", name);
  494. int refnum = AllocateRefnum();
  495. if (refnum < 0) {
  496. jack_error("No more refnum available");
  497. goto error;
  498. }
  499. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  500. jack_error("Cannot allocate synchro");
  501. goto error;
  502. }
  503. if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  504. // Failure if RT thread is not running (problem with the driver...)
  505. jack_error("Driver is not running");
  506. goto error;
  507. }
  508. fClientTable[refnum] = client;
  509. if (NotifyAddClient(client, name, refnum) < 0) {
  510. jack_error("Cannot notify add client");
  511. goto error;
  512. }
  513. fGraphManager->InitRefNum(refnum);
  514. fEngineControl->ResetRollingUsecs();
  515. *shared_engine = fEngineControl;
  516. *shared_manager = fGraphManager;
  517. *ref = refnum;
  518. return 0;
  519. error:
  520. // Cleanup...
  521. fSynchroTable[refnum].Destroy();
  522. fClientTable[refnum] = 0;
  523. return -1;
  524. }
  525. // Used for external clients
  526. int JackEngine::ClientExternalClose(int refnum)
  527. {
  528. JackClientInterface* client = fClientTable[refnum];
  529. fEngineControl->fTransport.ResetTimebase(refnum);
  530. int res = ClientCloseAux(refnum, client, true);
  531. client->Close();
  532. delete client;
  533. return res;
  534. }
  535. // Used for server internal clients or drivers when the RT thread is stopped
  536. int JackEngine::ClientInternalClose(int refnum, bool wait)
  537. {
  538. JackClientInterface* client = fClientTable[refnum];
  539. return ClientCloseAux(refnum, client, wait);
  540. }
  541. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  542. {
  543. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  544. // Unregister all ports ==> notifications are sent
  545. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  546. int i;
  547. fGraphManager->GetInputPorts(refnum, ports);
  548. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  549. PortUnRegister(refnum, ports[i]);
  550. }
  551. fGraphManager->GetOutputPorts(refnum, ports);
  552. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  553. PortUnRegister(refnum, ports[i]);
  554. }
  555. // Remove the client from the table
  556. ReleaseRefnum(refnum);
  557. // Remove all ports
  558. fGraphManager->RemoveAllPorts(refnum);
  559. // Wait until next cycle to be sure client is not used anymore
  560. if (wait) {
  561. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  562. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  563. }
  564. }
  565. // Notify running clients
  566. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  567. // Cleanup...
  568. fSynchroTable[refnum].Destroy();
  569. fEngineControl->ResetRollingUsecs();
  570. return 0;
  571. }
  572. int JackEngine::ClientActivate(int refnum, bool is_real_time)
  573. {
  574. JackClientInterface* client = fClientTable[refnum];
  575. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  576. if (is_real_time)
  577. fGraphManager->Activate(refnum);
  578. // Wait for graph state change to be effective
  579. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  580. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  581. return -1;
  582. } else {
  583. jack_int_t input_ports[PORT_NUM_FOR_CLIENT];
  584. jack_int_t output_ports[PORT_NUM_FOR_CLIENT];
  585. fGraphManager->GetInputPorts(refnum, input_ports);
  586. fGraphManager->GetOutputPorts(refnum, output_ports);
  587. // First add port state to JackPortIsActive
  588. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  589. fGraphManager->ActivatePort(input_ports[i]);
  590. }
  591. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  592. fGraphManager->ActivatePort(output_ports[i]);
  593. }
  594. // Notify client
  595. NotifyActivate(refnum);
  596. // Then issue port registration notification
  597. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  598. NotifyPortRegistation(input_ports[i], true);
  599. }
  600. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  601. NotifyPortRegistation(output_ports[i], true);
  602. }
  603. return 0;
  604. }
  605. }
  606. // May be called without client
  607. int JackEngine::ClientDeactivate(int refnum)
  608. {
  609. JackClientInterface* client = fClientTable[refnum];
  610. jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  611. jack_int_t input_ports[PORT_NUM_FOR_CLIENT];
  612. jack_int_t output_ports[PORT_NUM_FOR_CLIENT];
  613. fGraphManager->GetInputPorts(refnum, input_ports);
  614. fGraphManager->GetOutputPorts(refnum, output_ports);
  615. // First disconnect all ports and remove their JackPortIsActive state
  616. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  617. PortDisconnect(refnum, input_ports[i], ALL_PORTS);
  618. fGraphManager->DeactivatePort(input_ports[i]);
  619. }
  620. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  621. PortDisconnect(refnum, output_ports[i], ALL_PORTS);
  622. fGraphManager->DeactivatePort(output_ports[i]);
  623. }
  624. // Then issue port registration notification
  625. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  626. NotifyPortRegistation(input_ports[i], false);
  627. }
  628. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  629. NotifyPortRegistation(output_ports[i], false);
  630. }
  631. fGraphManager->Deactivate(refnum);
  632. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  633. // Wait for graph state change to be effective
  634. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  635. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  636. return -1;
  637. } else {
  638. return 0;
  639. }
  640. }
  641. //-----------------
  642. // Port management
  643. //-----------------
  644. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  645. {
  646. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  647. JackClientInterface* client = fClientTable[refnum];
  648. // Check if port name already exists
  649. if (fGraphManager->GetPort(name) != NO_PORT) {
  650. jack_error("port_name \"%s\" already exists", name);
  651. return -1;
  652. }
  653. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  654. if (*port_index != NO_PORT) {
  655. if (client->GetClientControl()->fActive)
  656. NotifyPortRegistation(*port_index, true);
  657. return 0;
  658. } else {
  659. return -1;
  660. }
  661. }
  662. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  663. {
  664. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  665. JackClientInterface* client = fClientTable[refnum];
  666. // Disconnect port ==> notification is sent
  667. PortDisconnect(refnum, port_index, ALL_PORTS);
  668. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  669. if (client->GetClientControl()->fActive)
  670. NotifyPortRegistation(port_index, false);
  671. return 0;
  672. } else {
  673. return -1;
  674. }
  675. }
  676. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  677. {
  678. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  679. jack_port_id_t port_src, port_dst;
  680. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  681. ? -1
  682. : PortConnect(refnum, port_src, port_dst);
  683. }
  684. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  685. {
  686. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  687. JackClientInterface* client;
  688. int ref;
  689. if (fGraphManager->CheckPorts(src, dst) < 0)
  690. return -1;
  691. ref = fGraphManager->GetOutputRefNum(src);
  692. assert(ref >= 0);
  693. client = fClientTable[ref];
  694. assert(client);
  695. if (!client->GetClientControl()->fActive) {
  696. jack_error("Cannot connect ports owned by inactive clients:"
  697. " \"%s\" is not active", client->GetClientControl()->fName);
  698. return -1;
  699. }
  700. ref = fGraphManager->GetInputRefNum(dst);
  701. assert(ref >= 0);
  702. client = fClientTable[ref];
  703. assert(client);
  704. if (!client->GetClientControl()->fActive) {
  705. jack_error("Cannot connect ports owned by inactive clients:"
  706. " \"%s\" is not active", client->GetClientControl()->fName);
  707. return -1;
  708. }
  709. int res = fGraphManager->Connect(src, dst);
  710. if (res == 0)
  711. NotifyPortConnect(src, dst, true);
  712. return res;
  713. }
  714. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  715. {
  716. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  717. jack_port_id_t port_src, port_dst;
  718. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  719. ? -1
  720. : PortDisconnect(refnum, port_src, port_dst);
  721. }
  722. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  723. {
  724. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  725. if (dst == ALL_PORTS) {
  726. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  727. fGraphManager->GetConnections(src, connections);
  728. JackPort* port = fGraphManager->GetPort(src);
  729. int ret = 0;
  730. if (port->GetFlags() & JackPortIsOutput) {
  731. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  732. if (PortDisconnect(refnum, src, connections[i]) != 0) {
  733. ret = -1;
  734. }
  735. }
  736. } else {
  737. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  738. if (PortDisconnect(refnum, connections[i], src) != 0) {
  739. ret = -1;
  740. }
  741. }
  742. }
  743. return ret;
  744. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  745. return -1;
  746. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  747. // Notifications
  748. NotifyPortConnect(src, dst, false);
  749. return 0;
  750. } else {
  751. return -1;
  752. }
  753. }
  754. int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
  755. {
  756. char old_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  757. strcpy(old_name, fGraphManager->GetPort(port)->GetName());
  758. fGraphManager->GetPort(port)->SetName(name);
  759. NotifyPortRename(port, old_name);
  760. return 0;
  761. }
  762. void JackEngine::SessionNotify(int refnum, const char *target, jack_session_event_type_t type, const char *path, JackChannelTransaction *socket)
  763. {
  764. if (fSessionPendingReplies != 0) {
  765. JackSessionNotifyResult res(-1);
  766. res.Write(socket);
  767. jack_log("JackEngine::SessionNotify ... busy");
  768. return;
  769. }
  770. for (int i = 0; i < CLIENT_NUM; i++) {
  771. JackClientInterface* client = fClientTable[i];
  772. if (client && (client->GetClientControl()->fSessionID < 0)) {
  773. client->GetClientControl()->fSessionID = GetNewUUID();
  774. }
  775. }
  776. fSessionResult = new JackSessionNotifyResult();
  777. for (int i = 0; i < CLIENT_NUM; i++) {
  778. JackClientInterface* client = fClientTable[i];
  779. if (client && client->GetClientControl()->fCallback[kSessionCallback]) {
  780. // check if this is a notification to a specific client.
  781. if (target!=NULL && strlen(target)!=0) {
  782. if (strcmp(target, client->GetClientControl()->fName)) {
  783. continue;
  784. }
  785. }
  786. char path_buf[JACK_PORT_NAME_SIZE];
  787. snprintf( path_buf, sizeof(path_buf), "%s%s%c", path, client->GetClientControl()->fName, DIR_SEPARATOR );
  788. int res = JackTools::MkDir(path_buf);
  789. if (res)
  790. jack_error( "JackEngine::SessionNotify: can not create session directory '%s'", path_buf );
  791. int result = client->ClientNotify(i, client->GetClientControl()->fName, kSessionCallback, true, path_buf, (int) type, 0);
  792. if (result == 2) {
  793. fSessionPendingReplies += 1;
  794. } else if (result == 1) {
  795. char uuid_buf[JACK_UUID_SIZE];
  796. snprintf( uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID );
  797. fSessionResult->fCommandList.push_back( JackSessionCommand( uuid_buf,
  798. client->GetClientControl()->fName,
  799. client->GetClientControl()->fSessionCommand,
  800. client->GetClientControl()->fSessionFlags ));
  801. }
  802. }
  803. }
  804. if (fSessionPendingReplies == 0) {
  805. fSessionResult->Write(socket);
  806. delete fSessionResult;
  807. fSessionResult = NULL;
  808. } else {
  809. fSessionTransaction = socket;
  810. }
  811. }
  812. void JackEngine::SessionReply(int refnum)
  813. {
  814. JackClientInterface* client = fClientTable[refnum];
  815. char uuid_buf[JACK_UUID_SIZE];
  816. snprintf( uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID );
  817. fSessionResult->fCommandList.push_back( JackSessionCommand( uuid_buf,
  818. client->GetClientControl()->fName,
  819. client->GetClientControl()->fSessionCommand,
  820. client->GetClientControl()->fSessionFlags ));
  821. fSessionPendingReplies -= 1;
  822. if (fSessionPendingReplies == 0) {
  823. fSessionResult->Write(fSessionTransaction);
  824. delete fSessionResult;
  825. fSessionResult = NULL;
  826. }
  827. }
  828. void JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res, int *result)
  829. {
  830. for (int i = 0; i < CLIENT_NUM; i++) {
  831. JackClientInterface* client = fClientTable[i];
  832. if (client && (strcmp(client_name, client->GetClientControl()->fName)==0)) {
  833. snprintf(uuid_res, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
  834. *result = 0;
  835. return;
  836. }
  837. }
  838. // did not find name.
  839. *result = -1;
  840. return;
  841. }
  842. void JackEngine::GetClientNameForUUID(const char *uuid, char *name_res, int *result)
  843. {
  844. for (int i = 0; i < CLIENT_NUM; i++) {
  845. JackClientInterface* client = fClientTable[i];
  846. if (!client)
  847. continue;
  848. char uuid_buf[JACK_UUID_SIZE];
  849. snprintf(uuid_buf, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
  850. if (strcmp(uuid,uuid_buf) == 0) {
  851. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  852. *result = 0;
  853. return;
  854. }
  855. }
  856. // did not find uuid.
  857. *result = -1;
  858. return;
  859. }
  860. void JackEngine::ReserveClientName(const char *name, const char *uuid, int *result)
  861. {
  862. if (ClientCheckName(name)) {
  863. *result = -1;
  864. return;
  865. }
  866. EnsureUUID(atoi(uuid));
  867. fReservationMap[atoi(uuid)] = name;
  868. }
  869. } // end of namespace