PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jack-1.9.8/jack-1.9.8/common/JackGraphManager.cpp

#
C++ | 902 lines | 662 code | 140 blank | 100 comment | 145 complexity | 69c01e1cb029cf5df11045157abed61b MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackGraphManager.h"
  17. #include "JackConstants.h"
  18. #include "JackError.h"
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <algorithm>
  22. #include <regex.h>
  23. namespace Jack
  24. {
  25. static void AssertBufferSize(jack_nframes_t buffer_size)
  26. {
  27. if (buffer_size > BUFFER_SIZE_MAX) {
  28. jack_log("JackGraphManager::AssertBufferSize frames = %ld", buffer_size);
  29. assert(buffer_size <= BUFFER_SIZE_MAX);
  30. }
  31. }
  32. void JackGraphManager::AssertPort(jack_port_id_t port_index)
  33. {
  34. if (port_index >= fPortMax) {
  35. jack_log("JackGraphManager::AssertPort port_index = %ld", port_index);
  36. assert(port_index < fPortMax);
  37. }
  38. }
  39. JackGraphManager* JackGraphManager::Allocate(int port_max)
  40. {
  41. // Using "Placement" new
  42. void* shared_ptr = JackShmMem::operator new(sizeof(JackGraphManager) + port_max * sizeof(JackPort));
  43. return new(shared_ptr) JackGraphManager(port_max);
  44. }
  45. void JackGraphManager::Destroy(JackGraphManager* manager)
  46. {
  47. // "Placement" new was used
  48. manager->~JackGraphManager();
  49. JackShmMem::operator delete(manager);
  50. }
  51. JackGraphManager::JackGraphManager(int port_max)
  52. {
  53. assert(port_max <= PORT_NUM_MAX);
  54. for (int i = 0; i < port_max; i++) {
  55. fPortArray[i].Release();
  56. }
  57. fPortMax = port_max;
  58. }
  59. JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
  60. {
  61. AssertPort(port_index);
  62. return &fPortArray[port_index];
  63. }
  64. jack_default_audio_sample_t* JackGraphManager::GetBuffer(jack_port_id_t port_index)
  65. {
  66. return fPortArray[port_index].GetBuffer();
  67. }
  68. // Server
  69. void JackGraphManager::InitRefNum(int refnum)
  70. {
  71. JackConnectionManager* manager = WriteNextStateStart();
  72. manager->InitRefNum(refnum);
  73. WriteNextStateStop();
  74. }
  75. // RT
  76. void JackGraphManager::RunCurrentGraph()
  77. {
  78. JackConnectionManager* manager = ReadCurrentState();
  79. manager->ResetGraph(fClientTiming);
  80. }
  81. // RT
  82. bool JackGraphManager::RunNextGraph()
  83. {
  84. bool res;
  85. JackConnectionManager* manager = TrySwitchState(&res);
  86. manager->ResetGraph(fClientTiming);
  87. return res;
  88. }
  89. // RT
  90. bool JackGraphManager::IsFinishedGraph()
  91. {
  92. JackConnectionManager* manager = ReadCurrentState();
  93. return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
  94. }
  95. // RT
  96. int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro* table)
  97. {
  98. JackConnectionManager* manager = ReadCurrentState();
  99. return manager->ResumeRefNum(control, table, fClientTiming);
  100. }
  101. // RT
  102. int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro* table, long usec)
  103. {
  104. JackConnectionManager* manager = ReadCurrentState();
  105. return manager->SuspendRefNum(control, table, fClientTiming, usec);
  106. }
  107. void JackGraphManager::TopologicalSort(std::vector<jack_int_t>& sorted)
  108. {
  109. UInt16 cur_index;
  110. UInt16 next_index;
  111. do {
  112. cur_index = GetCurrentIndex();
  113. sorted.clear();
  114. ReadCurrentState()->TopologicalSort(sorted);
  115. next_index = GetCurrentIndex();
  116. } while (cur_index != next_index); // Until a coherent state has been read
  117. }
  118. // Server
  119. void JackGraphManager::DirectConnect(int ref1, int ref2)
  120. {
  121. JackConnectionManager* manager = WriteNextStateStart();
  122. manager->DirectConnect(ref1, ref2);
  123. jack_log("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  124. WriteNextStateStop();
  125. }
  126. // Server
  127. void JackGraphManager::DirectDisconnect(int ref1, int ref2)
  128. {
  129. JackConnectionManager* manager = WriteNextStateStart();
  130. manager->DirectDisconnect(ref1, ref2);
  131. jack_log("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  132. WriteNextStateStop();
  133. }
  134. // Server
  135. bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
  136. {
  137. JackConnectionManager* manager = ReadCurrentState();
  138. return manager->IsDirectConnection(ref1, ref2);
  139. }
  140. // RT
  141. void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size)
  142. {
  143. AssertPort(port_index);
  144. AssertBufferSize(buffer_size);
  145. JackConnectionManager* manager = ReadCurrentState();
  146. JackPort* port = GetPort(port_index);
  147. // This happens when a port has just been unregistered and is still used by the RT code
  148. if (!port->IsUsed()) {
  149. jack_log("JackGraphManager::GetBuffer : port = %ld is released state", port_index);
  150. return GetBuffer(0); // port_index 0 is not used
  151. }
  152. jack_int_t len = manager->Connections(port_index);
  153. // Output port
  154. if (port->fFlags & JackPortIsOutput) {
  155. return (port->fTied != NO_PORT) ? GetBuffer(port->fTied, buffer_size) : GetBuffer(port_index);
  156. }
  157. // No connections : return a zero-filled buffer
  158. if (len == 0) {
  159. port->ClearBuffer(buffer_size);
  160. return port->GetBuffer();
  161. // One connection
  162. } else if (len == 1) {
  163. jack_port_id_t src_index = manager->GetPort(port_index, 0);
  164. // Ports in same client : copy the buffer
  165. if (GetPort(src_index)->GetRefNum() == port->GetRefNum()) {
  166. void* buffers[1];
  167. buffers[0] = GetBuffer(src_index, buffer_size);
  168. port->MixBuffers(buffers, 1, buffer_size);
  169. return port->GetBuffer();
  170. // Otherwise, use zero-copy mode, just pass the buffer of the connected (output) port.
  171. } else {
  172. return GetBuffer(src_index, buffer_size);
  173. }
  174. // Multiple connections : mix all buffers
  175. } else {
  176. const jack_int_t* connections = manager->GetConnections(port_index);
  177. void* buffers[CONNECTION_NUM_FOR_PORT];
  178. jack_port_id_t src_index;
  179. int i;
  180. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  181. AssertPort(src_index);
  182. buffers[i] = GetBuffer(src_index, buffer_size);
  183. }
  184. port->MixBuffers(buffers, i, buffer_size);
  185. return port->GetBuffer();
  186. }
  187. }
  188. // Server
  189. int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
  190. {
  191. AssertPort(port_index);
  192. JackPort* port = GetPort(port_index);
  193. /**
  194. jackd.h
  195. * If @ref JackPortCanMonitor is set for this @a port, turn input
  196. * monitoring on or off. Otherwise, do nothing.
  197. if (!(fFlags & JackPortCanMonitor))
  198. return -1;
  199. */
  200. port->RequestMonitor(onoff);
  201. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  202. if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags & JackPortIsInput) ?
  203. jack_port_id_t src_index;
  204. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  205. // XXX much worse things will happen if there is a feedback loop !!!
  206. RequestMonitor(src_index, onoff);
  207. }
  208. }
  209. return 0;
  210. }
  211. // Client
  212. jack_nframes_t JackGraphManager::ComputeTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
  213. {
  214. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  215. jack_nframes_t max_latency = 0;
  216. jack_port_id_t dst_index;
  217. if (hop_count > 8)
  218. return GetPort(port_index)->GetLatency();
  219. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  220. if (src_port_index != dst_index) {
  221. AssertPort(dst_index);
  222. JackPort* dst_port = GetPort(dst_index);
  223. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  224. ? dst_port->GetLatency()
  225. : ComputeTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  226. max_latency = ((max_latency > this_latency) ? max_latency : this_latency);
  227. }
  228. }
  229. return max_latency + GetPort(port_index)->GetLatency();
  230. }
  231. // Client
  232. int JackGraphManager::ComputeTotalLatency(jack_port_id_t port_index)
  233. {
  234. UInt16 cur_index;
  235. UInt16 next_index;
  236. JackPort* port = GetPort(port_index);
  237. AssertPort(port_index);
  238. do {
  239. cur_index = GetCurrentIndex();
  240. port->fTotalLatency = ComputeTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  241. next_index = GetCurrentIndex();
  242. } while (cur_index != next_index); // Until a coherent state has been read
  243. jack_log("JackGraphManager::GetTotalLatency port_index = %ld total latency = %ld", port_index, port->fTotalLatency);
  244. return 0;
  245. }
  246. // Client
  247. int JackGraphManager::ComputeTotalLatencies()
  248. {
  249. jack_port_id_t port_index;
  250. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  251. JackPort* port = GetPort(port_index);
  252. if (port->IsUsed())
  253. ComputeTotalLatency(port_index);
  254. }
  255. return 0;
  256. }
  257. void JackGraphManager::RecalculateLatencyAux(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  258. {
  259. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  260. JackPort* port = GetPort(port_index);
  261. jack_latency_range_t latency = { UINT32_MAX, 0 };
  262. jack_port_id_t dst_index;
  263. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  264. AssertPort(dst_index);
  265. JackPort* dst_port = GetPort(dst_index);
  266. jack_latency_range_t other_latency;
  267. dst_port->GetLatencyRange(mode, &other_latency);
  268. if (other_latency.max > latency.max)
  269. latency.max = other_latency.max;
  270. if (other_latency.min < latency.min)
  271. latency.min = other_latency.min;
  272. }
  273. if (latency.min == UINT32_MAX)
  274. latency.min = 0;
  275. port->SetLatencyRange(mode, &latency);
  276. }
  277. void JackGraphManager::RecalculateLatency(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  278. {
  279. UInt16 cur_index;
  280. UInt16 next_index;
  281. do {
  282. cur_index = GetCurrentIndex();
  283. RecalculateLatencyAux(port_index, mode);
  284. next_index = GetCurrentIndex();
  285. } while (cur_index != next_index); // Until a coherent state has been read
  286. jack_log("JackGraphManager::RecalculateLatency port_index = %ld", port_index);
  287. }
  288. // Server
  289. void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
  290. {
  291. jack_log("JackGraphManager::SetBufferSize size = %ld", buffer_size);
  292. jack_port_id_t port_index;
  293. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  294. JackPort* port = GetPort(port_index);
  295. if (port->IsUsed())
  296. port->ClearBuffer(buffer_size);
  297. }
  298. }
  299. // Server
  300. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  301. {
  302. jack_port_id_t port_index;
  303. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  304. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  305. JackPort* port = GetPort(port_index);
  306. if (!port->IsUsed()) {
  307. jack_log("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s", port_index, port_name, port_type);
  308. if (!port->Allocate(refnum, port_name, port_type, flags))
  309. return NO_PORT;
  310. break;
  311. }
  312. }
  313. return (port_index < fPortMax) ? port_index : NO_PORT;
  314. }
  315. // Server
  316. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags, jack_nframes_t buffer_size)
  317. {
  318. JackConnectionManager* manager = WriteNextStateStart();
  319. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);
  320. if (port_index != NO_PORT) {
  321. JackPort* port = GetPort(port_index);
  322. assert(port);
  323. port->ClearBuffer(buffer_size);
  324. int res;
  325. if (flags & JackPortIsOutput) {
  326. res = manager->AddOutputPort(refnum, port_index);
  327. } else {
  328. res = manager->AddInputPort(refnum, port_index);
  329. }
  330. // Insertion failure
  331. if (res < 0) {
  332. port->Release();
  333. port_index = NO_PORT;
  334. }
  335. }
  336. WriteNextStateStop();
  337. return port_index;
  338. }
  339. // Server
  340. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  341. {
  342. JackConnectionManager* manager = WriteNextStateStart();
  343. JackPort* port = GetPort(port_index);
  344. int res;
  345. if (port->fFlags & JackPortIsOutput) {
  346. DisconnectAllOutput(port_index);
  347. res = manager->RemoveOutputPort(refnum, port_index);
  348. } else {
  349. DisconnectAllInput(port_index);
  350. res = manager->RemoveInputPort(refnum, port_index);
  351. }
  352. port->Release();
  353. WriteNextStateStop();
  354. return res;
  355. }
  356. void JackGraphManager::GetInputPorts(int refnum, jack_int_t* res)
  357. {
  358. JackConnectionManager* manager = WriteNextStateStart();
  359. const jack_int_t* input = manager->GetInputPorts(refnum);
  360. memcpy(res, input, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  361. WriteNextStateStop();
  362. }
  363. void JackGraphManager::GetOutputPorts(int refnum, jack_int_t* res)
  364. {
  365. JackConnectionManager* manager = WriteNextStateStart();
  366. const jack_int_t* output = manager->GetOutputPorts(refnum);
  367. memcpy(res, output, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  368. WriteNextStateStop();
  369. }
  370. // Server
  371. void JackGraphManager::RemoveAllPorts(int refnum)
  372. {
  373. jack_log("JackGraphManager::RemoveAllPorts ref = %ld", refnum);
  374. JackConnectionManager* manager = WriteNextStateStart();
  375. jack_port_id_t port_index;
  376. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "input" table is empty
  377. const jack_int_t* input = manager->GetInputPorts(refnum);
  378. while ((port_index = input[0]) != EMPTY) {
  379. int res = ReleasePort(refnum, port_index);
  380. if (res < 0) {
  381. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  382. assert(true);
  383. break;
  384. }
  385. }
  386. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "output" table is empty
  387. const jack_int_t* output = manager->GetOutputPorts(refnum);
  388. while ((port_index = output[0]) != EMPTY) {
  389. int res = ReleasePort(refnum, port_index);
  390. if (res < 0) {
  391. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  392. assert(true);
  393. break;
  394. }
  395. }
  396. WriteNextStateStop();
  397. }
  398. // Server
  399. void JackGraphManager::DisconnectAllPorts(int refnum)
  400. {
  401. int i;
  402. jack_log("JackGraphManager::DisconnectAllPorts ref = %ld", refnum);
  403. JackConnectionManager* manager = WriteNextStateStart();
  404. const jack_int_t* input = manager->GetInputPorts(refnum);
  405. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  406. DisconnectAllInput(input[i]);
  407. }
  408. const jack_int_t* output = manager->GetOutputPorts(refnum);
  409. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  410. DisconnectAllOutput(output[i]);
  411. }
  412. WriteNextStateStop();
  413. }
  414. // Server
  415. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  416. {
  417. jack_log("JackGraphManager::DisconnectAllInput port_index = %ld", port_index);
  418. JackConnectionManager* manager = WriteNextStateStart();
  419. for (unsigned int i = 0; i < fPortMax; i++) {
  420. if (manager->IsConnected(i, port_index)) {
  421. jack_log("JackGraphManager::Disconnect i = %ld port_index = %ld", i, port_index);
  422. Disconnect(i, port_index);
  423. }
  424. }
  425. WriteNextStateStop();
  426. }
  427. // Server
  428. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  429. {
  430. jack_log("JackGraphManager::DisconnectAllOutput port_index = %ld ", port_index);
  431. JackConnectionManager* manager = WriteNextStateStart();
  432. const jack_int_t* connections = manager->GetConnections(port_index);
  433. while (connections[0] != EMPTY) {
  434. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  435. }
  436. WriteNextStateStop();
  437. }
  438. // Server
  439. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  440. {
  441. AssertPort(port_index);
  442. JackPort* port = GetPort(port_index);
  443. if (port->fFlags & JackPortIsOutput) {
  444. DisconnectAllOutput(port_index);
  445. } else {
  446. DisconnectAllInput(port_index);
  447. }
  448. return 0;
  449. }
  450. // Server
  451. void JackGraphManager::GetConnections(jack_port_id_t port_index, jack_int_t* res)
  452. {
  453. JackConnectionManager* manager = WriteNextStateStart();
  454. const jack_int_t* connections = manager->GetConnections(port_index);
  455. memcpy(res, connections, sizeof(jack_int_t) * CONNECTION_NUM_FOR_PORT);
  456. WriteNextStateStop();
  457. }
  458. // Server
  459. void JackGraphManager::Activate(int refnum)
  460. {
  461. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  462. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  463. }
  464. /*
  465. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  466. (thus unactivated) state may happen where the client is still checked for its end.
  467. */
  468. // Server
  469. void JackGraphManager::Deactivate(int refnum)
  470. {
  471. // Disconnect only when needed
  472. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  473. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  474. } else {
  475. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  476. }
  477. // Disconnect only when needed
  478. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  479. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  480. } else {
  481. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  482. }
  483. }
  484. // Server
  485. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  486. {
  487. AssertPort(port_index);
  488. JackConnectionManager* manager = WriteNextStateStart();
  489. int res = manager->GetInputRefNum(port_index);
  490. WriteNextStateStop();
  491. return res;
  492. }
  493. // Server
  494. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  495. {
  496. AssertPort(port_index);
  497. JackConnectionManager* manager = WriteNextStateStart();
  498. int res = manager->GetOutputRefNum(port_index);
  499. WriteNextStateStop();
  500. return res;
  501. }
  502. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  503. {
  504. JackConnectionManager* manager = WriteNextStateStart();
  505. jack_log("JackGraphManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
  506. JackPort* src = GetPort(port_src);
  507. JackPort* dst = GetPort(port_dst);
  508. int res = 0;
  509. if (!src->fInUse || !dst->fInUse) {
  510. if (!src->fInUse)
  511. jack_error("JackGraphManager::Connect port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  512. if (!dst->fInUse)
  513. jack_error("JackGraphManager::Connect port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  514. res = -1;
  515. goto end;
  516. }
  517. if (src->fTypeId != dst->fTypeId) {
  518. jack_error("JackGraphManager::Connect different port types port_src = %ld port_dst = %ld", port_src, port_dst);
  519. res = -1;
  520. goto end;
  521. }
  522. if (manager->IsConnected(port_src, port_dst)) {
  523. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  524. res = EEXIST;
  525. goto end;
  526. }
  527. res = manager->Connect(port_src, port_dst);
  528. if (res < 0) {
  529. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  530. goto end;
  531. }
  532. res = manager->Connect(port_dst, port_src);
  533. if (res < 0) {
  534. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  535. goto end;
  536. }
  537. if (manager->IsLoopPath(port_src, port_dst)) {
  538. jack_log("JackGraphManager::Connect: LOOP detected");
  539. manager->IncFeedbackConnection(port_src, port_dst);
  540. } else {
  541. manager->IncDirectConnection(port_src, port_dst);
  542. }
  543. end:
  544. WriteNextStateStop();
  545. return res;
  546. }
  547. // Server
  548. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  549. {
  550. JackConnectionManager* manager = WriteNextStateStart();
  551. jack_log("JackGraphManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
  552. bool in_use_src = GetPort(port_src)->fInUse;
  553. bool in_use_dst = GetPort(port_dst)->fInUse;
  554. int res = 0;
  555. if (!in_use_src || !in_use_dst) {
  556. if (!in_use_src)
  557. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  558. if (!in_use_dst)
  559. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  560. res = -1;
  561. goto end;
  562. }
  563. if (!manager->IsConnected(port_src, port_dst)) {
  564. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  565. res = -1;
  566. goto end;
  567. }
  568. res = manager->Disconnect(port_src, port_dst);
  569. if (res < 0) {
  570. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  571. goto end;
  572. }
  573. res = manager->Disconnect(port_dst, port_src);
  574. if (res < 0) {
  575. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  576. goto end;
  577. }
  578. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  579. jack_log("JackGraphManager::Disconnect: FEEDBACK removed");
  580. manager->DecFeedbackConnection(port_src, port_dst);
  581. } else {
  582. manager->DecDirectConnection(port_src, port_dst);
  583. }
  584. end:
  585. WriteNextStateStop();
  586. return res;
  587. }
  588. // Client
  589. int JackGraphManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst)
  590. {
  591. JackConnectionManager* manager = ReadCurrentState();
  592. return manager->IsConnected(port_src, port_dst);
  593. }
  594. // Server
  595. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  596. {
  597. JackPort* src = GetPort(port_src);
  598. JackPort* dst = GetPort(port_dst);
  599. if ((dst->fFlags & JackPortIsInput) == 0) {
  600. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  601. return -1;
  602. }
  603. if ((src->fFlags & JackPortIsOutput) == 0) {
  604. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  605. return -1;
  606. }
  607. return 0;
  608. }
  609. int JackGraphManager::GetTwoPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  610. {
  611. jack_log("JackGraphManager::CheckConnect src_name = %s dst_name = %s", src_name, dst_name);
  612. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  613. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  614. return -1;
  615. }
  616. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  617. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  618. return -1;
  619. }
  620. return 0;
  621. }
  622. // Client : port array
  623. jack_port_id_t JackGraphManager::GetPort(const char* name)
  624. {
  625. for (unsigned int i = 0; i < fPortMax; i++) {
  626. JackPort* port = GetPort(i);
  627. if (port->IsUsed() && port->NameEquals(name)) {
  628. return i;
  629. }
  630. }
  631. return NO_PORT;
  632. }
  633. /*!
  634. \brief Get the connection port name array.
  635. */
  636. // Client
  637. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  638. {
  639. const jack_int_t* connections = manager->GetConnections(port_index);
  640. jack_int_t index;
  641. int i;
  642. // Cleanup connection array
  643. memset(res, 0, sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  644. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((index = connections[i]) != EMPTY); i++) {
  645. JackPort* port = GetPort(index);
  646. res[i] = port->fName;
  647. }
  648. res[i] = NULL;
  649. }
  650. /*
  651. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  652. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  653. read to loop forever.
  654. */
  655. // Client
  656. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  657. {
  658. const char** res = (const char**)malloc(sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  659. UInt16 cur_index, next_index;
  660. if (!res)
  661. return NULL;
  662. do {
  663. cur_index = GetCurrentIndex();
  664. GetConnectionsAux(ReadCurrentState(), res, port_index);
  665. next_index = GetCurrentIndex();
  666. } while (cur_index != next_index); // Until a coherent state has been read
  667. if (res[0]) { // At least one connection
  668. return res;
  669. } else { // Empty array, should return NULL
  670. free(res);
  671. return NULL;
  672. }
  673. }
  674. // Client
  675. void JackGraphManager::GetPortsAux(const char** matching_ports, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  676. {
  677. int match_cnt = 0;
  678. regex_t port_regex, type_regex;
  679. if (port_name_pattern && port_name_pattern[0]) {
  680. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  681. }
  682. if (type_name_pattern && type_name_pattern[0]) {
  683. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  684. }
  685. // Cleanup port array
  686. memset(matching_ports, 0, sizeof(char*) * fPortMax);
  687. for (unsigned int i = 0; i < fPortMax; i++) {
  688. bool matching = true;
  689. JackPort* port = GetPort(i);
  690. if (port->IsUsed()) {
  691. if (flags) {
  692. if ((port->fFlags & flags) != flags) {
  693. matching = false;
  694. }
  695. }
  696. if (matching && port_name_pattern && port_name_pattern[0]) {
  697. if (regexec(&port_regex, port->GetName(), 0, NULL, 0)) {
  698. matching = false;
  699. }
  700. }
  701. if (matching && type_name_pattern && type_name_pattern[0]) {
  702. if (regexec(&type_regex, port->GetType(), 0, NULL, 0)) {
  703. matching = false;
  704. }
  705. }
  706. if (matching) {
  707. matching_ports[match_cnt++] = port->fName;
  708. }
  709. }
  710. }
  711. matching_ports[match_cnt] = 0;
  712. if (port_name_pattern && port_name_pattern[0]) {
  713. regfree(&port_regex);
  714. }
  715. if (type_name_pattern && type_name_pattern[0]) {
  716. regfree(&type_regex);
  717. }
  718. }
  719. // Client
  720. /*
  721. Check that the state was not changed during the read operation.
  722. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  723. read to loop forever.
  724. */
  725. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  726. {
  727. const char** res = (const char**)malloc(sizeof(char*) * fPortMax);
  728. UInt16 cur_index, next_index;
  729. if (!res)
  730. return NULL;
  731. do {
  732. cur_index = GetCurrentIndex();
  733. GetPortsAux(res, port_name_pattern, type_name_pattern, flags);
  734. next_index = GetCurrentIndex();
  735. } while (cur_index != next_index); // Until a coherent state has been read
  736. if (res[0]) { // At least one port
  737. return res;
  738. } else {
  739. free(res); // Empty array, should return NULL
  740. return NULL;
  741. }
  742. }
  743. // Server
  744. void JackGraphManager::Save(JackConnectionManager* dst)
  745. {
  746. JackConnectionManager* manager = WriteNextStateStart();
  747. memcpy(dst, manager, sizeof(JackConnectionManager));
  748. WriteNextStateStop();
  749. }
  750. // Server
  751. void JackGraphManager::Restore(JackConnectionManager* src)
  752. {
  753. JackConnectionManager* manager = WriteNextStateStart();
  754. memcpy(manager, src, sizeof(JackConnectionManager));
  755. WriteNextStateStop();
  756. }
  757. } // end of namespace