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.

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