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.

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