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.

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