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.

361 lines
11KB

  1. /*
  2. Copyright (C) 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. #ifndef __JackLockedEngine__
  16. #define __JackLockedEngine__
  17. #include "JackEngine.h"
  18. #include "JackMutex.h"
  19. #include "JackTools.h"
  20. #include "JackException.h"
  21. namespace Jack
  22. {
  23. #define TRY_CALL \
  24. try { \
  25. /*
  26. See : http://groups.google.com/group/comp.programming.threads/browse_thread/thread/652bcf186fbbf697/f63757846514e5e5
  27. catch (...) {
  28. // Assuming thread cancellation, must rethrow
  29. throw;
  30. }
  31. */
  32. #define CATCH_EXCEPTION_RETURN \
  33. } catch(std::bad_alloc& e) { \
  34. jack_error("Memory allocation error..."); \
  35. return -1; \
  36. } catch(JackTemporaryException& e) { \
  37. jack_error("JackTemporaryException : now quits..."); \
  38. JackTools::KillServer(); \
  39. return -1; \
  40. } catch (...) { \
  41. jack_error("Unknown error..."); \
  42. throw; \
  43. } \
  44. #define CATCH_EXCEPTION \
  45. } catch(std::bad_alloc& e) { \
  46. jack_error("Memory allocation error..."); \
  47. } catch (...) { \
  48. jack_error("Unknown error..."); \
  49. throw; \
  50. } \
  51. /*!
  52. \brief Locked Engine, access to methods is serialized using a mutex.
  53. */
  54. class SERVER_EXPORT JackLockedEngine
  55. {
  56. private:
  57. JackEngine fEngine;
  58. public:
  59. JackLockedEngine(JackGraphManager* manager, JackSynchro* table, JackEngineControl* controler):
  60. fEngine(manager, table, controler)
  61. {}
  62. ~JackLockedEngine()
  63. {}
  64. int Open()
  65. {
  66. // No lock needed
  67. TRY_CALL
  68. return fEngine.Open();
  69. CATCH_EXCEPTION_RETURN
  70. }
  71. int Close()
  72. {
  73. // No lock needed
  74. TRY_CALL
  75. return fEngine.Close();
  76. CATCH_EXCEPTION_RETURN
  77. }
  78. // Client management
  79. int ClientCheck(const char* name, int uuid, char* name_res, int protocol, int options, int* status)
  80. {
  81. TRY_CALL
  82. JackLock lock(&fEngine);
  83. return fEngine.ClientCheck(name, uuid, name_res, protocol, options, status);
  84. CATCH_EXCEPTION_RETURN
  85. }
  86. int ClientExternalOpen(const char* name, int pid, int uuid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  87. {
  88. TRY_CALL
  89. JackLock lock(&fEngine);
  90. return fEngine.ClientExternalOpen(name, pid, uuid, ref, shared_engine, shared_client, shared_graph_manager);
  91. CATCH_EXCEPTION_RETURN
  92. }
  93. int ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  94. {
  95. TRY_CALL
  96. JackLock lock(&fEngine);
  97. return fEngine.ClientInternalOpen(name, ref, shared_engine, shared_manager, client, wait);
  98. CATCH_EXCEPTION_RETURN
  99. }
  100. int ClientExternalClose(int refnum)
  101. {
  102. TRY_CALL
  103. JackLock lock(&fEngine);
  104. return (fEngine.CheckClient(refnum)) ? fEngine.ClientExternalClose(refnum) : - 1;
  105. CATCH_EXCEPTION_RETURN
  106. }
  107. int ClientInternalClose(int refnum, bool wait)
  108. {
  109. TRY_CALL
  110. JackLock lock(&fEngine);
  111. return (fEngine.CheckClient(refnum)) ? fEngine.ClientInternalClose(refnum, wait) : -1;
  112. CATCH_EXCEPTION_RETURN
  113. }
  114. int ClientActivate(int refnum, bool is_real_time)
  115. {
  116. TRY_CALL
  117. JackLock lock(&fEngine);
  118. return (fEngine.CheckClient(refnum)) ? fEngine.ClientActivate(refnum, is_real_time) : -1;
  119. CATCH_EXCEPTION_RETURN
  120. }
  121. int ClientDeactivate(int refnum)
  122. {
  123. TRY_CALL
  124. JackLock lock(&fEngine);
  125. return (fEngine.CheckClient(refnum)) ? fEngine.ClientDeactivate(refnum) : -1;
  126. CATCH_EXCEPTION_RETURN
  127. }
  128. // Internal client management
  129. int GetInternalClientName(int int_ref, char* name_res)
  130. {
  131. TRY_CALL
  132. JackLock lock(&fEngine);
  133. return fEngine.GetInternalClientName(int_ref, name_res);
  134. CATCH_EXCEPTION_RETURN
  135. }
  136. int InternalClientHandle(const char* client_name, int* status, int* int_ref)
  137. {
  138. TRY_CALL
  139. JackLock lock(&fEngine);
  140. return fEngine.InternalClientHandle(client_name, status, int_ref);
  141. CATCH_EXCEPTION_RETURN
  142. }
  143. int InternalClientUnload(int refnum, int* status)
  144. {
  145. TRY_CALL
  146. JackLock lock(&fEngine);
  147. // Client is tested in fEngine.InternalClientUnload
  148. return fEngine.InternalClientUnload(refnum, status);
  149. CATCH_EXCEPTION_RETURN
  150. }
  151. // Port management
  152. int PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port)
  153. {
  154. TRY_CALL
  155. JackLock lock(&fEngine);
  156. return (fEngine.CheckClient(refnum)) ? fEngine.PortRegister(refnum, name, type, flags, buffer_size, port) : -1;
  157. CATCH_EXCEPTION_RETURN
  158. }
  159. int PortUnRegister(int refnum, jack_port_id_t port)
  160. {
  161. TRY_CALL
  162. JackLock lock(&fEngine);
  163. return (fEngine.CheckClient(refnum)) ? fEngine.PortUnRegister(refnum, port) : -1;
  164. CATCH_EXCEPTION_RETURN
  165. }
  166. int PortConnect(int refnum, const char* src, const char* dst)
  167. {
  168. TRY_CALL
  169. JackLock lock(&fEngine);
  170. return (fEngine.CheckClient(refnum)) ? fEngine.PortConnect(refnum, src, dst) : -1;
  171. CATCH_EXCEPTION_RETURN
  172. }
  173. int PortDisconnect(int refnum, const char* src, const char* dst)
  174. {
  175. TRY_CALL
  176. JackLock lock(&fEngine);
  177. return (fEngine.CheckClient(refnum)) ? fEngine.PortDisconnect(refnum, src, dst) : -1;
  178. CATCH_EXCEPTION_RETURN
  179. }
  180. int PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  181. {
  182. TRY_CALL
  183. JackLock lock(&fEngine);
  184. return (fEngine.CheckClient(refnum)) ? fEngine.PortConnect(refnum, src, dst) : -1;
  185. CATCH_EXCEPTION_RETURN
  186. }
  187. int PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  188. {
  189. TRY_CALL
  190. JackLock lock(&fEngine);
  191. return (fEngine.CheckClient(refnum)) ? fEngine.PortDisconnect(refnum, src, dst) : -1;
  192. CATCH_EXCEPTION_RETURN
  193. }
  194. int PortRename(int refnum, jack_port_id_t port, const char* name)
  195. {
  196. TRY_CALL
  197. JackLock lock(&fEngine);
  198. return (fEngine.CheckClient(refnum)) ? fEngine.PortRename(refnum, port, name) : -1;
  199. CATCH_EXCEPTION_RETURN
  200. }
  201. int ComputeTotalLatencies()
  202. {
  203. TRY_CALL
  204. JackLock lock(&fEngine);
  205. return fEngine.ComputeTotalLatencies();
  206. CATCH_EXCEPTION_RETURN
  207. }
  208. // Graph
  209. bool Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  210. {
  211. // RT : no lock
  212. return fEngine.Process(cur_cycle_begin, prev_cycle_end);
  213. }
  214. // Notifications
  215. void NotifyXRun(jack_time_t cur_cycle_begin, float delayed_usecs)
  216. {
  217. // RT : no lock
  218. fEngine.NotifyXRun(cur_cycle_begin, delayed_usecs);
  219. }
  220. void NotifyXRun(int refnum)
  221. {
  222. // RT : no lock
  223. fEngine.NotifyXRun(refnum);
  224. }
  225. void NotifyGraphReorder()
  226. {
  227. TRY_CALL
  228. JackLock lock(&fEngine);
  229. fEngine.NotifyGraphReorder();
  230. CATCH_EXCEPTION
  231. }
  232. void NotifyBufferSize(jack_nframes_t buffer_size)
  233. {
  234. TRY_CALL
  235. JackLock lock(&fEngine);
  236. fEngine.NotifyBufferSize(buffer_size);
  237. CATCH_EXCEPTION
  238. }
  239. void NotifySampleRate(jack_nframes_t sample_rate)
  240. {
  241. TRY_CALL
  242. JackLock lock(&fEngine);
  243. fEngine.NotifySampleRate(sample_rate);
  244. CATCH_EXCEPTION
  245. }
  246. void NotifyFreewheel(bool onoff)
  247. {
  248. TRY_CALL
  249. JackLock lock(&fEngine);
  250. fEngine.NotifyFreewheel(onoff);
  251. CATCH_EXCEPTION
  252. }
  253. void NotifyFailure(int code, const char* reason)
  254. {
  255. TRY_CALL
  256. JackLock lock(&fEngine);
  257. fEngine.NotifyFailure(code, reason);
  258. CATCH_EXCEPTION
  259. }
  260. int GetClientPID(const char* name)
  261. {
  262. TRY_CALL
  263. JackLock lock(&fEngine);
  264. return fEngine.GetClientPID(name);
  265. CATCH_EXCEPTION_RETURN
  266. }
  267. int GetClientRefNum(const char* name)
  268. {
  269. TRY_CALL
  270. JackLock lock(&fEngine);
  271. return fEngine.GetClientRefNum(name);
  272. CATCH_EXCEPTION_RETURN
  273. }
  274. void NotifyQuit()
  275. {
  276. TRY_CALL
  277. JackLock lock(&fEngine);
  278. return fEngine.NotifyQuit();
  279. CATCH_EXCEPTION
  280. }
  281. void SessionNotify(int refnum, const char* target, jack_session_event_type_t type, const char *path, JackChannelTransaction *socket)
  282. {
  283. TRY_CALL
  284. JackLock lock(&fEngine);
  285. fEngine.SessionNotify(refnum, target, type, path, socket);
  286. CATCH_EXCEPTION
  287. }
  288. void SessionReply(int refnum)
  289. {
  290. TRY_CALL
  291. JackLock lock(&fEngine);
  292. fEngine.SessionReply(refnum);
  293. CATCH_EXCEPTION
  294. }
  295. void GetUUIDForClientName(const char *client_name, char *uuid_res, int *result)
  296. {
  297. TRY_CALL
  298. JackLock lock(&fEngine);
  299. fEngine.GetUUIDForClientName(client_name, uuid_res, result);
  300. CATCH_EXCEPTION
  301. }
  302. void GetClientNameForUUID(const char *uuid, char *name_res, int *result)
  303. {
  304. TRY_CALL
  305. JackLock lock(&fEngine);
  306. fEngine.GetClientNameForUUID(uuid, name_res, result);
  307. CATCH_EXCEPTION
  308. }
  309. void ReserveClientName(const char *name, const char *uuid, int *result)
  310. {
  311. TRY_CALL
  312. JackLock lock(&fEngine);
  313. fEngine.ReserveClientName(name, uuid, result);
  314. CATCH_EXCEPTION
  315. }
  316. };
  317. } // end of namespace
  318. #endif