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.

1042 lines
32KB

  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 (uuid < 0)
  460. uuid = GetNewUUID();
  461. else
  462. EnsureUUID(uuid);
  463. if (client->Open(real_name, pid, refnum, uuid, shared_client) < 0) {
  464. jack_error("Cannot open client");
  465. goto error;
  466. }
  467. if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  468. // Failure if RT thread is not running (problem with the driver...)
  469. jack_error("Driver is not running");
  470. goto error;
  471. }
  472. fClientTable[refnum] = client;
  473. if (NotifyAddClient(client, real_name, refnum) < 0) {
  474. jack_error("Cannot notify add client");
  475. goto error;
  476. }
  477. fGraphManager->InitRefNum(refnum);
  478. fEngineControl->ResetRollingUsecs();
  479. *shared_engine = fEngineControl->GetShmIndex();
  480. *shared_graph_manager = fGraphManager->GetShmIndex();
  481. *ref = refnum;
  482. return 0;
  483. error:
  484. // Cleanup...
  485. fSynchroTable[refnum].Destroy();
  486. fClientTable[refnum] = 0;
  487. client->Close();
  488. delete client;
  489. return -1;
  490. }
  491. // Used for server driver clients
  492. int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  493. {
  494. jack_log("JackEngine::ClientInternalOpen: name = %s", name);
  495. int refnum = AllocateRefnum();
  496. if (refnum < 0) {
  497. jack_error("No more refnum available");
  498. goto error;
  499. }
  500. if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
  501. jack_error("Cannot allocate synchro");
  502. goto error;
  503. }
  504. if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
  505. // Failure if RT thread is not running (problem with the driver...)
  506. jack_error("Driver is not running");
  507. goto error;
  508. }
  509. fClientTable[refnum] = client;
  510. if (NotifyAddClient(client, name, refnum) < 0) {
  511. jack_error("Cannot notify add client");
  512. goto error;
  513. }
  514. fGraphManager->InitRefNum(refnum);
  515. fEngineControl->ResetRollingUsecs();
  516. *shared_engine = fEngineControl;
  517. *shared_manager = fGraphManager;
  518. *ref = refnum;
  519. return 0;
  520. error:
  521. // Cleanup...
  522. fSynchroTable[refnum].Destroy();
  523. fClientTable[refnum] = 0;
  524. return -1;
  525. }
  526. // Used for external clients
  527. int JackEngine::ClientExternalClose(int refnum)
  528. {
  529. JackClientInterface* client = fClientTable[refnum];
  530. fEngineControl->fTransport.ResetTimebase(refnum);
  531. int res = ClientCloseAux(refnum, client, true);
  532. client->Close();
  533. delete client;
  534. return res;
  535. }
  536. // Used for server internal clients or drivers when the RT thread is stopped
  537. int JackEngine::ClientInternalClose(int refnum, bool wait)
  538. {
  539. JackClientInterface* client = fClientTable[refnum];
  540. return ClientCloseAux(refnum, client, wait);
  541. }
  542. int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
  543. {
  544. jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
  545. // Unregister all ports ==> notifications are sent
  546. jack_int_t ports[PORT_NUM_FOR_CLIENT];
  547. int i;
  548. fGraphManager->GetInputPorts(refnum, ports);
  549. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  550. PortUnRegister(refnum, ports[i]);
  551. }
  552. fGraphManager->GetOutputPorts(refnum, ports);
  553. for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
  554. PortUnRegister(refnum, ports[i]);
  555. }
  556. // Remove the client from the table
  557. ReleaseRefnum(refnum);
  558. // Remove all ports
  559. fGraphManager->RemoveAllPorts(refnum);
  560. // Wait until next cycle to be sure client is not used anymore
  561. if (wait) {
  562. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
  563. jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
  564. }
  565. }
  566. // Notify running clients
  567. NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
  568. // Cleanup...
  569. fSynchroTable[refnum].Destroy();
  570. fEngineControl->ResetRollingUsecs();
  571. return 0;
  572. }
  573. int JackEngine::ClientActivate(int refnum, bool is_real_time)
  574. {
  575. JackClientInterface* client = fClientTable[refnum];
  576. jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  577. if (is_real_time)
  578. fGraphManager->Activate(refnum);
  579. // Wait for graph state change to be effective
  580. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  581. jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  582. return -1;
  583. } else {
  584. jack_int_t input_ports[PORT_NUM_FOR_CLIENT];
  585. jack_int_t output_ports[PORT_NUM_FOR_CLIENT];
  586. fGraphManager->GetInputPorts(refnum, input_ports);
  587. fGraphManager->GetOutputPorts(refnum, output_ports);
  588. // First add port state to JackPortIsActive
  589. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  590. fGraphManager->ActivatePort(input_ports[i]);
  591. }
  592. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  593. fGraphManager->ActivatePort(output_ports[i]);
  594. }
  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. fGraphManager->DeactivatePort(input_ports[i]);
  620. }
  621. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  622. PortDisconnect(refnum, output_ports[i], ALL_PORTS);
  623. fGraphManager->DeactivatePort(output_ports[i]);
  624. }
  625. // Then issue port registration notification
  626. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
  627. NotifyPortRegistation(input_ports[i], false);
  628. }
  629. for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
  630. NotifyPortRegistation(output_ports[i], false);
  631. }
  632. fGraphManager->Deactivate(refnum);
  633. fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
  634. // Wait for graph state change to be effective
  635. if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
  636. jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
  637. return -1;
  638. } else {
  639. return 0;
  640. }
  641. }
  642. //-----------------
  643. // Port management
  644. //-----------------
  645. int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
  646. {
  647. jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
  648. JackClientInterface* client = fClientTable[refnum];
  649. // Check if port name already exists
  650. if (fGraphManager->GetPort(name) != NO_PORT) {
  651. jack_error("port_name \"%s\" already exists", name);
  652. return -1;
  653. }
  654. *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
  655. if (*port_index != NO_PORT) {
  656. if (client->GetClientControl()->fActive)
  657. NotifyPortRegistation(*port_index, true);
  658. return 0;
  659. } else {
  660. return -1;
  661. }
  662. }
  663. int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
  664. {
  665. jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
  666. JackClientInterface* client = fClientTable[refnum];
  667. // Disconnect port ==> notification is sent
  668. PortDisconnect(refnum, port_index, ALL_PORTS);
  669. if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
  670. if (client->GetClientControl()->fActive)
  671. NotifyPortRegistation(port_index, false);
  672. return 0;
  673. } else {
  674. return -1;
  675. }
  676. }
  677. int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
  678. {
  679. jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
  680. jack_port_id_t port_src, port_dst;
  681. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  682. ? -1
  683. : PortConnect(refnum, port_src, port_dst);
  684. }
  685. int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  686. {
  687. jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
  688. JackClientInterface* client;
  689. int ref;
  690. if (fGraphManager->CheckPorts(src, dst) < 0)
  691. return -1;
  692. ref = fGraphManager->GetOutputRefNum(src);
  693. assert(ref >= 0);
  694. client = fClientTable[ref];
  695. assert(client);
  696. if (!client->GetClientControl()->fActive) {
  697. jack_error("Cannot connect ports owned by inactive clients:"
  698. " \"%s\" is not active", client->GetClientControl()->fName);
  699. return -1;
  700. }
  701. ref = fGraphManager->GetInputRefNum(dst);
  702. assert(ref >= 0);
  703. client = fClientTable[ref];
  704. assert(client);
  705. if (!client->GetClientControl()->fActive) {
  706. jack_error("Cannot connect ports owned by inactive clients:"
  707. " \"%s\" is not active", client->GetClientControl()->fName);
  708. return -1;
  709. }
  710. int res = fGraphManager->Connect(src, dst);
  711. if (res == 0)
  712. NotifyPortConnect(src, dst, true);
  713. return res;
  714. }
  715. int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
  716. {
  717. jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
  718. jack_port_id_t port_src, port_dst;
  719. return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
  720. ? -1
  721. : PortDisconnect(refnum, port_src, port_dst);
  722. }
  723. int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  724. {
  725. jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
  726. if (dst == ALL_PORTS) {
  727. jack_int_t connections[CONNECTION_NUM_FOR_PORT];
  728. fGraphManager->GetConnections(src, connections);
  729. JackPort* port = fGraphManager->GetPort(src);
  730. int ret = 0;
  731. if (port->GetFlags() & JackPortIsOutput) {
  732. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  733. if (PortDisconnect(refnum, src, connections[i]) != 0) {
  734. ret = -1;
  735. }
  736. }
  737. } else {
  738. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
  739. if (PortDisconnect(refnum, connections[i], src) != 0) {
  740. ret = -1;
  741. }
  742. }
  743. }
  744. return ret;
  745. } else if (fGraphManager->CheckPorts(src, dst) < 0) {
  746. return -1;
  747. } else if (fGraphManager->Disconnect(src, dst) == 0) {
  748. // Notifications
  749. NotifyPortConnect(src, dst, false);
  750. return 0;
  751. } else {
  752. return -1;
  753. }
  754. }
  755. int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
  756. {
  757. char old_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  758. strcpy(old_name, fGraphManager->GetPort(port)->GetName());
  759. fGraphManager->GetPort(port)->SetName(name);
  760. NotifyPortRename(port, old_name);
  761. return 0;
  762. }
  763. void JackEngine::SessionNotify(int refnum, const char *target, jack_session_event_type_t type, const char *path, JackChannelTransaction *socket )
  764. {
  765. if (fSessionPendingReplies != 0) {
  766. JackSessionNotifyResult res(-1);
  767. res.Write(socket);
  768. jack_log("JackEngine::SessionNotify ... busy");
  769. return;
  770. }
  771. for (int i = 0; i < CLIENT_NUM; i++) {
  772. JackClientInterface* client = fClientTable[i];
  773. if (client && (client->GetClientControl()->fSessionID < 0)) {
  774. client->GetClientControl()->fSessionID = GetNewUUID();
  775. }
  776. }
  777. fSessionResult = new JackSessionNotifyResult();
  778. for (int i = 0; i < CLIENT_NUM; i++) {
  779. jack_log("JackEngine::SessionNotify ... checking client %d", i);
  780. JackClientInterface* client = fClientTable[i];
  781. if (client && client->GetClientControl()->fCallback[kSessionCallback]) {
  782. jack_log("JackEngine::SessionNotify ... got target: %s", client->GetClientControl()->fName);
  783. // check if this is a notification to a specific client.
  784. if (target!=NULL && strlen(target)!=0) {
  785. if (strcmp(target, client->GetClientControl()->fName)) {
  786. jack_log("JackEngine::SessionNotify ... not this one");
  787. continue;
  788. }
  789. }
  790. jack_log("JackEngine::SessionNotify ... sending");
  791. int result = client->ClientNotify(i, client->GetClientControl()->fName, kSessionCallback, true, path, (int) type, 0);
  792. jack_log("JackEngine::SessionNotify ... got reply: %d", result);
  793. if (result == 2) {
  794. fSessionPendingReplies += 1;
  795. } else if (result == 1) {
  796. char uuid_buf[32];
  797. snprintf( uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID );
  798. fSessionResult->fCommandList.push_back( JackSessionCommand( uuid_buf,
  799. client->GetClientControl()->fName,
  800. client->GetClientControl()->fSessionCommand,
  801. client->GetClientControl()->fSessionFlags ));
  802. }
  803. }
  804. }
  805. if (fSessionPendingReplies == 0) {
  806. fSessionResult->Write(socket);
  807. delete fSessionResult;
  808. fSessionResult = NULL;
  809. } else {
  810. fSessionTransaction = socket;
  811. }
  812. }
  813. void JackEngine::SessionReply(int refnum)
  814. {
  815. JackClientInterface* client = fClientTable[refnum];
  816. char uuid_buf[32];
  817. snprintf( uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID );
  818. fSessionResult->fCommandList.push_back( JackSessionCommand( uuid_buf,
  819. client->GetClientControl()->fName,
  820. client->GetClientControl()->fSessionCommand,
  821. client->GetClientControl()->fSessionFlags ));
  822. fSessionPendingReplies -= 1;
  823. if (fSessionPendingReplies == 0) {
  824. fSessionResult->Write(fSessionTransaction);
  825. delete fSessionResult;
  826. fSessionResult = NULL;
  827. }
  828. }
  829. void JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res, int *result)
  830. {
  831. for (int i = 0; i < CLIENT_NUM; i++) {
  832. JackClientInterface* client = fClientTable[i];
  833. if (client && (strcmp(client_name, client->GetClientControl()->fName)==0)) {
  834. snprintf(uuid_res, 32, "%d", client->GetClientControl()->fSessionID);
  835. *result = 0;
  836. return;
  837. }
  838. }
  839. // did not find name.
  840. *result = -1;
  841. return;
  842. }
  843. void JackEngine::GetClientNameForUUID(const char *uuid, char *name_res, int *result)
  844. {
  845. jack_log( "want uuid %s", uuid );
  846. for (int i = 0; i < CLIENT_NUM; i++) {
  847. JackClientInterface* client = fClientTable[i];
  848. if (!client)
  849. continue;
  850. char uuid_buf[33];
  851. snprintf(uuid_buf, 32, "%d", client->GetClientControl()->fSessionID);
  852. jack_log( "check uuid %s", uuid_buf );
  853. if (strcmp(uuid,uuid_buf) == 0) {
  854. strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
  855. *result = 0;
  856. jack_log( "found name\n" );
  857. return;
  858. }
  859. }
  860. // did not find uuid.
  861. *result = -1;
  862. return;
  863. }
  864. void JackEngine::ReserveClientName(const char *name, const char *uuid, int *result)
  865. {
  866. if (ClientCheckName(name))
  867. {
  868. *result = -1;
  869. return;
  870. }
  871. EnsureUUID(atoi(uuid));
  872. fReservationMap[atoi(uuid)] = name;
  873. }
  874. } // end of namespace