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.

496 lines
13KB

  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 Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackConnectionManager__
  16. #define __JackConnectionManager__
  17. #include "JackConstants.h"
  18. #include "JackActivationCount.h"
  19. #include "JackError.h"
  20. #include "JackCompilerDeps.h"
  21. #include <vector>
  22. #include <assert.h>
  23. namespace Jack
  24. {
  25. struct JackClientControl;
  26. /*!
  27. \brief Utility class.
  28. */
  29. PRE_PACKED_STRUCTURE
  30. template <int SIZE>
  31. class JackFixedArray
  32. {
  33. private:
  34. jack_int_t fTable[SIZE];
  35. uint32_t fCounter;
  36. public:
  37. JackFixedArray()
  38. {
  39. Init();
  40. }
  41. void Init()
  42. {
  43. for (int i = 0; i < SIZE; i++)
  44. fTable[i] = EMPTY;
  45. fCounter = 0;
  46. }
  47. bool AddItem(jack_int_t index)
  48. {
  49. for (int i = 0; i < SIZE; i++) {
  50. if (fTable[i] == EMPTY) {
  51. fTable[i] = index;
  52. fCounter++;
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. bool RemoveItem(jack_int_t index)
  59. {
  60. for (int i = 0; i < SIZE; i++) {
  61. if (fTable[i] == index) {
  62. fCounter--;
  63. // Shift all indexes
  64. if (i == SIZE - 1) {
  65. fTable[i] = EMPTY;
  66. } else {
  67. int j;
  68. for (j = i; j <= SIZE - 2 && fTable[j] != EMPTY; j++) {
  69. fTable[j] = fTable[j + 1];
  70. }
  71. fTable[j] = EMPTY;
  72. }
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. jack_int_t GetItem(jack_int_t index) const
  79. {
  80. return (index < SIZE) ? fTable[index] : EMPTY;
  81. }
  82. const jack_int_t* GetItems() const
  83. {
  84. return fTable;
  85. }
  86. bool CheckItem(jack_int_t index) const
  87. {
  88. for (int i = 0; i < SIZE && fTable[i] != EMPTY; i++) {
  89. if (fTable[i] == index)
  90. return true;
  91. }
  92. return false;
  93. }
  94. uint32_t GetItemCount() const
  95. {
  96. return fCounter;
  97. }
  98. } POST_PACKED_STRUCTURE;
  99. /*!
  100. \brief Utility class.
  101. */
  102. PRE_PACKED_STRUCTURE
  103. template <int SIZE>
  104. class JackFixedArray1 : public JackFixedArray<SIZE>
  105. {
  106. private:
  107. bool fUsed;
  108. public:
  109. JackFixedArray1()
  110. {
  111. Init();
  112. }
  113. void Init()
  114. {
  115. JackFixedArray<SIZE>::Init();
  116. fUsed = false;
  117. }
  118. bool IsAvailable()
  119. {
  120. if (fUsed) {
  121. return false;
  122. } else {
  123. fUsed = true;
  124. return true;
  125. }
  126. }
  127. } POST_PACKED_STRUCTURE;
  128. /*!
  129. \brief Utility class.
  130. */
  131. PRE_PACKED_STRUCTURE
  132. template <int SIZE>
  133. class JackFixedMatrix
  134. {
  135. private:
  136. jack_int_t fTable[SIZE][SIZE];
  137. public:
  138. JackFixedMatrix()
  139. {}
  140. void Init(jack_int_t index)
  141. {
  142. for (int i = 0; i < SIZE; i++) {
  143. fTable[index][i] = 0;
  144. fTable[i][index] = 0;
  145. }
  146. }
  147. const jack_int_t* GetItems(jack_int_t index) const
  148. {
  149. return fTable[index];
  150. }
  151. jack_int_t IncItem(jack_int_t index1, jack_int_t index2)
  152. {
  153. fTable[index1][index2]++;
  154. return fTable[index1][index2];
  155. }
  156. jack_int_t DecItem(jack_int_t index1, jack_int_t index2)
  157. {
  158. fTable[index1][index2]--;
  159. return fTable[index1][index2];
  160. }
  161. jack_int_t GetItemCount(jack_int_t index1, jack_int_t index2) const
  162. {
  163. return fTable[index1][index2];
  164. }
  165. void ClearItem(jack_int_t index1, jack_int_t index2)
  166. {
  167. fTable[index1][index2] = 0;
  168. }
  169. /*!
  170. \brief Get the output indexes of a given index.
  171. */
  172. void GetOutputTable(jack_int_t index, jack_int_t* output) const
  173. {
  174. int i, j;
  175. for (i = 0; i < SIZE; i++)
  176. output[i] = EMPTY;
  177. for (i = 0, j = 0; i < SIZE; i++) {
  178. if (fTable[index][i] > 0) {
  179. output[j] = i;
  180. j++;
  181. }
  182. }
  183. }
  184. void GetOutputTable1(jack_int_t index, jack_int_t* output) const
  185. {
  186. for (int i = 0; i < SIZE; i++) {
  187. output[i] = fTable[i][index];
  188. }
  189. }
  190. bool IsInsideTable(jack_int_t index, jack_int_t* output) const
  191. {
  192. for (int i = 0; i < SIZE && output[i] != EMPTY; i++) {
  193. if (output[i] == index)
  194. return true;
  195. }
  196. return false;
  197. }
  198. void Copy(JackFixedMatrix& copy)
  199. {
  200. for (int i = 0; i < SIZE; i++) {
  201. memcpy(copy.fTable[i], fTable[i], sizeof(jack_int_t) * SIZE);
  202. }
  203. }
  204. } POST_PACKED_STRUCTURE;
  205. /*!
  206. \brief Utility class.
  207. */
  208. PRE_PACKED_STRUCTURE
  209. template <int SIZE>
  210. class JackLoopFeedback
  211. {
  212. private:
  213. int fTable[SIZE][3];
  214. /*!
  215. \brief Add a feedback connection between 2 refnum.
  216. */
  217. bool AddConnectionAux(int ref1, int ref2)
  218. {
  219. for (int i = 0; i < SIZE; i++) {
  220. if (fTable[i][0] == EMPTY) {
  221. fTable[i][0] = ref1;
  222. fTable[i][1] = ref2;
  223. fTable[i][2] = 1;
  224. jack_log("JackLoopFeedback::AddConnectionAux ref1 = %ld ref2 = %ld", ref1, ref2);
  225. return true;
  226. }
  227. }
  228. jack_error("Feedback table is full !!\n");
  229. return false;
  230. }
  231. /*!
  232. \brief Remove a feedback connection between 2 refnum.
  233. */
  234. bool RemoveConnectionAux(int ref1, int ref2)
  235. {
  236. for (int i = 0; i < SIZE; i++) {
  237. if (fTable[i][0] == ref1 && fTable[i][1] == ref2) {
  238. fTable[i][0] = EMPTY;
  239. fTable[i][1] = EMPTY;
  240. fTable[i][2] = 0;
  241. jack_log("JackLoopFeedback::RemoveConnectionAux ref1 = %ld ref2 = %ld", ref1, ref2);
  242. return true;
  243. }
  244. }
  245. jack_error("Feedback connection not found\n");
  246. return false;
  247. }
  248. int IncConnection(int index)
  249. {
  250. fTable[index][2]++;
  251. return fTable[index][2];
  252. }
  253. int DecConnection(int index)
  254. {
  255. fTable[index][2]--;
  256. return fTable[index][2];
  257. }
  258. public:
  259. JackLoopFeedback()
  260. {
  261. Init();
  262. }
  263. void Init()
  264. {
  265. for (int i = 0; i < SIZE; i++) {
  266. fTable[i][0] = EMPTY;
  267. fTable[i][1] = EMPTY;
  268. fTable[i][2] = 0;
  269. }
  270. }
  271. bool IncConnection(int ref1, int ref2)
  272. {
  273. int index = GetConnectionIndex(ref1, ref2);
  274. if (index >= 0) { // Feedback connection is already added, increment counter
  275. IncConnection(index);
  276. return true;
  277. } else {
  278. return AddConnectionAux(ref1, ref2); // Add the feedback connection
  279. }
  280. }
  281. bool DecConnection(int ref1, int ref2)
  282. {
  283. int index = GetConnectionIndex(ref1, ref2);
  284. if (index >= 0) {
  285. jack_log("JackLoopFeedback::DecConnection ref1 = %ld ref2 = %ld index = %ld", ref1, ref2, index);
  286. return (DecConnection(index) == 0) ? RemoveConnectionAux(ref1, ref2) : true;
  287. } else {
  288. return false;
  289. }
  290. }
  291. /*!
  292. \brief Test if a connection between 2 refnum is a feedback connection.
  293. */
  294. int GetConnectionIndex(int ref1, int ref2) const
  295. {
  296. for (int i = 0; i < SIZE; i++) {
  297. if (fTable[i][0] == ref1 && fTable[i][1] == ref2)
  298. return i;
  299. }
  300. return -1;
  301. }
  302. } POST_PACKED_STRUCTURE;
  303. /*!
  304. \brief For client timing measurements.
  305. */
  306. PRE_PACKED_STRUCTURE
  307. struct JackClientTiming
  308. {
  309. jack_time_t fSignaledAt;
  310. jack_time_t fAwakeAt;
  311. jack_time_t fFinishedAt;
  312. jack_client_state_t fStatus;
  313. JackClientTiming()
  314. {
  315. Init();
  316. }
  317. ~JackClientTiming()
  318. {}
  319. void Init()
  320. {
  321. fSignaledAt = 0;
  322. fAwakeAt = 0;
  323. fFinishedAt = 0;
  324. fStatus = NotTriggered;
  325. }
  326. } POST_PACKED_STRUCTURE;
  327. /*!
  328. \brief Connection manager.
  329. <UL>
  330. <LI>The <B>fConnection</B> array contains the list (array line) of connected ports for a given port.
  331. <LI>The <B>fInputPort</B> array contains the list (array line) of input connected ports for a given client.
  332. <LI>The <B>fOutputPort</B> array contains the list (array line) of output connected ports for a given client.
  333. <LI>The <B>fConnectionRef</B> array contains the number of ports connected between two clients.
  334. <LI>The <B>fInputCounter</B> array contains the number of input clients connected to a given for activation purpose.
  335. </UL>
  336. */
  337. PRE_PACKED_STRUCTURE
  338. class SERVER_EXPORT JackConnectionManager
  339. {
  340. private:
  341. JackFixedArray<CONNECTION_NUM_FOR_PORT> fConnection[PORT_NUM_MAX]; /*! Connection matrix: list of connected ports for a given port: needed to compute Mix buffer */
  342. JackFixedArray1<PORT_NUM_FOR_CLIENT> fInputPort[CLIENT_NUM]; /*! Table of input port per refnum : to find a refnum for a given port */
  343. JackFixedArray<PORT_NUM_FOR_CLIENT> fOutputPort[CLIENT_NUM]; /*! Table of output port per refnum : to find a refnum for a given port */
  344. JackFixedMatrix<CLIENT_NUM> fConnectionRef; /*! Table of port connections by (refnum , refnum) */
  345. alignas(UInt32) alignas(JackActivationCount) JackActivationCount fInputCounter[CLIENT_NUM]; /*! Activation counter per refnum */
  346. JackLoopFeedback<CONNECTION_NUM_FOR_PORT> fLoopFeedback; /*! Loop feedback connections */
  347. bool IsLoopPathAux(int ref1, int ref2) const;
  348. public:
  349. JackConnectionManager();
  350. ~JackConnectionManager();
  351. // Connections management
  352. int Connect(jack_port_id_t port_src, jack_port_id_t port_dst);
  353. int Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst);
  354. bool IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst) const;
  355. /*!
  356. \brief Get the connection number of a given port.
  357. */
  358. jack_int_t Connections(jack_port_id_t port_index) const
  359. {
  360. return fConnection[port_index].GetItemCount();
  361. }
  362. jack_port_id_t GetPort(jack_port_id_t port_index, int connection) const
  363. {
  364. assert(connection < CONNECTION_NUM_FOR_PORT);
  365. return (jack_port_id_t)fConnection[port_index].GetItem(connection);
  366. }
  367. const jack_int_t* GetConnections(jack_port_id_t port_index) const;
  368. bool IncFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst);
  369. bool DecFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst);
  370. bool IsFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst) const;
  371. bool IsLoopPath(jack_port_id_t port_src, jack_port_id_t port_dst) const;
  372. void IncDirectConnection(jack_port_id_t port_src, jack_port_id_t port_dst);
  373. void DecDirectConnection(jack_port_id_t port_src, jack_port_id_t port_dst);
  374. // Ports management
  375. int AddInputPort(int refnum, jack_port_id_t port_index);
  376. int AddOutputPort(int refnum, jack_port_id_t port_index);
  377. int RemoveInputPort(int refnum, jack_port_id_t port_index);
  378. int RemoveOutputPort(int refnum, jack_port_id_t port_index);
  379. const jack_int_t* GetInputPorts(int refnum);
  380. const jack_int_t* GetOutputPorts(int refnum);
  381. // Client management
  382. void InitRefNum(int refnum);
  383. int GetInputRefNum(jack_port_id_t port_index) const;
  384. int GetOutputRefNum(jack_port_id_t port_index) const;
  385. // Connect/Disconnect 2 refnum "directly"
  386. bool IsDirectConnection(int ref1, int ref2) const;
  387. void DirectConnect(int ref1, int ref2);
  388. void DirectDisconnect(int ref1, int ref2);
  389. int GetActivation(int refnum) const
  390. {
  391. return fInputCounter[refnum].GetValue();
  392. }
  393. // Graph
  394. void ResetGraph(JackClientTiming* timing);
  395. int ResumeRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing);
  396. int SuspendRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing, long time_out_usec);
  397. void TopologicalSort(std::vector<jack_int_t>& sorted);
  398. } POST_PACKED_STRUCTURE;
  399. } // end of namespace
  400. #endif