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.

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