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.

256 lines
8.3KB

  1. /*
  2. Copyright (C) 2004-2006 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 "JackMachClientChannel.h"
  16. #include "JackRPCEngine.h"
  17. #include "JackRPCClientServer.c"
  18. #include "JackError.h"
  19. #include "JackLibClient.h"
  20. #include "JackLibGlobals.h"
  21. #include "JackMachThread.h"
  22. #include "JackConstants.h"
  23. namespace Jack
  24. {
  25. JackMachClientChannel::JackMachClientChannel()
  26. {
  27. fThread = new JackMachThread(this);
  28. }
  29. JackMachClientChannel::~JackMachClientChannel()
  30. {
  31. delete fThread;
  32. }
  33. // Server <===> client
  34. int JackMachClientChannel::Open(const char* name, JackClient* client, jack_options_t options, jack_status_t* status)
  35. {
  36. JackLog("JackMachClientChannel::Open name = %s\n", name);
  37. // Connect to server
  38. if (!fServerPort.ConnectPort(jack_server_entry)) {
  39. jack_error("Cannot connect to server Mach port");
  40. return -1;
  41. }
  42. // Check name in server
  43. int result = 0;
  44. char name_res[JACK_CLIENT_NAME_SIZE];
  45. ClientCheck(name, name_res, (int)options, (int*)status, &result);
  46. if (result < 0) {
  47. jack_error("Cannot check clientname");
  48. return -1;
  49. }
  50. // Prepare local port using client name
  51. char buf[JACK_CLIENT_NAME_SIZE];
  52. snprintf(buf, sizeof(buf) - 1, "%s:%s", jack_client_entry, name);
  53. if (!fClientPort.AllocatePort(buf, 16)) {
  54. jack_error("Cannot allocate client Mach port");
  55. return -1;
  56. }
  57. JackLibGlobals::fGlobals->fClientTable[fClientPort.GetPort()] = client;
  58. return 0;
  59. }
  60. void JackMachClientChannel::Close()
  61. {
  62. JackLog("JackMachClientChannel::Close\n");
  63. JackLibGlobals::fGlobals->fClientTable.erase(fClientPort.GetPort());
  64. fServerPort.DisconnectPort();
  65. fClientPort.DestroyPort();
  66. // TO CHECK
  67. kern_return_t res;
  68. if ((res = mach_port_destroy(mach_task_self(), fPrivatePort)) != KERN_SUCCESS) {
  69. jack_error("JackMachClientChannel::Close err = %s", mach_error_string(res));
  70. }
  71. }
  72. int JackMachClientChannel::Start()
  73. {
  74. JackLog("JackMachClientChannel::Start\n");
  75. if (fThread->Start() != 0) {
  76. jack_error("Cannot start Jack client listener");
  77. return -1;
  78. } else {
  79. return 0;
  80. }
  81. }
  82. void JackMachClientChannel::Stop()
  83. {
  84. JackLog("JackMachClientChannel::Stop\n");
  85. fThread->Kill();
  86. }
  87. void JackMachClientChannel::ClientCheck(const char* name, char* name_res, int options, int* status, int* result)
  88. {
  89. kern_return_t res = rpc_jack_client_check(fPrivatePort, (char*)name, name_res, options, status, result);
  90. if (res != KERN_SUCCESS) {
  91. *result = -1;
  92. jack_error("JackMachClientChannel::ClientCheck err = %s", mach_error_string(res));
  93. }
  94. }
  95. void JackMachClientChannel::ClientOpen(const char* name, int* shared_engine, int* shared_client, int* shared_graph, int* result)
  96. {
  97. kern_return_t res = rpc_jack_client_open(fServerPort.GetPort(), (char*)name, &fPrivatePort, shared_engine, shared_client, shared_graph, result);
  98. if (res != KERN_SUCCESS) {
  99. *result = -1;
  100. jack_error("JackMachClientChannel::ClientOpen err = %s", mach_error_string(res));
  101. }
  102. }
  103. void JackMachClientChannel::ClientClose(int refnum, int* result)
  104. {
  105. kern_return_t res = rpc_jack_client_close(fPrivatePort, refnum, result);
  106. if (res != KERN_SUCCESS) {
  107. *result = -1;
  108. jack_error("JackMachClientChannel::ClientClose err = %s", mach_error_string(res));
  109. }
  110. }
  111. void JackMachClientChannel::ClientActivate(int refnum, int* result)
  112. {
  113. kern_return_t res = rpc_jack_client_activate(fPrivatePort, refnum, result);
  114. if (res != KERN_SUCCESS) {
  115. *result = -1;
  116. jack_error("JackMachClientChannel::ClientActivate err = %s", mach_error_string(res));
  117. }
  118. }
  119. void JackMachClientChannel::ClientDeactivate(int refnum, int* result)
  120. {
  121. kern_return_t res = rpc_jack_client_deactivate(fPrivatePort, refnum, result);
  122. if (res != KERN_SUCCESS) {
  123. *result = -1;
  124. jack_error("JackMachClientChannel::ClientDeactivate err = %s", mach_error_string(res));
  125. }
  126. }
  127. void JackMachClientChannel::PortRegister(int refnum, const char* name, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index, int* result)
  128. {
  129. kern_return_t res = rpc_jack_port_register(fPrivatePort, refnum, (char*)name, flags, buffer_size, port_index, result);
  130. if (res != KERN_SUCCESS) {
  131. *result = -1;
  132. jack_error("JackMachClientChannel::PortRegister err = %s", mach_error_string(res));
  133. }
  134. }
  135. void JackMachClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
  136. {
  137. kern_return_t res = rpc_jack_port_unregister(fPrivatePort, refnum, port_index, result);
  138. if (res != KERN_SUCCESS) {
  139. *result = -1;
  140. jack_error("JackMachClientChannel::PortUnRegister err = %s", mach_error_string(res));
  141. }
  142. }
  143. void JackMachClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
  144. {
  145. kern_return_t res = rpc_jack_port_connect_name(fPrivatePort, refnum, (char*)src, (char*)dst, result);
  146. if (res != KERN_SUCCESS) {
  147. jack_error("JackMachClientChannel::PortConnect err = %s", mach_error_string(res));
  148. }
  149. }
  150. void JackMachClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
  151. {
  152. kern_return_t res = rpc_jack_port_disconnect_name(fPrivatePort, refnum, (char*)src, (char*)dst, result);
  153. if (res != KERN_SUCCESS) {
  154. *result = -1;
  155. jack_error("JackMachClientChannel::PortDisconnect err = %s", mach_error_string(res));
  156. }
  157. }
  158. void JackMachClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  159. {
  160. kern_return_t res = rpc_jack_port_connect(fPrivatePort, refnum, src, dst, result);
  161. if (res != KERN_SUCCESS) {
  162. *result = -1;
  163. jack_error("JackMachClientChannel::PortConnect err = %s", mach_error_string(res));
  164. }
  165. }
  166. void JackMachClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  167. {
  168. kern_return_t res = rpc_jack_port_disconnect(fPrivatePort, refnum, src, dst, result);
  169. if (res != KERN_SUCCESS) {
  170. *result = -1;
  171. jack_error("JackMachClientChannel::PortDisconnect err = %s", mach_error_string(res));
  172. }
  173. }
  174. void JackMachClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
  175. {
  176. kern_return_t res = rpc_jack_set_buffer_size(fPrivatePort, buffer_size, result);
  177. if (res != KERN_SUCCESS) {
  178. *result = -1;
  179. jack_error("JackMachClientChannel::SetBufferSize err = %s", mach_error_string(res));
  180. }
  181. }
  182. void JackMachClientChannel::SetFreewheel(int onoff, int* result)
  183. {
  184. kern_return_t res = rpc_jack_set_freewheel(fPrivatePort, onoff, result);
  185. if (res != KERN_SUCCESS) {
  186. *result = -1;
  187. jack_error("JackMachClientChannel::SetFreewheel err = %s", mach_error_string(res));
  188. }
  189. }
  190. void JackMachClientChannel::ReleaseTimebase(int refnum, int* result)
  191. {
  192. kern_return_t res = rpc_jack_release_timebase(fPrivatePort, refnum, result);
  193. if (res != KERN_SUCCESS) {
  194. *result = -1;
  195. jack_error("JackMachClientChannel::ReleaseTimebase err = %s", mach_error_string(res));
  196. }
  197. }
  198. void JackMachClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
  199. {
  200. kern_return_t res = rpc_jack_set_timebase_callback(fPrivatePort, refnum, conditional, result);
  201. if (res != KERN_SUCCESS) {
  202. *result = -1;
  203. jack_error("JackMachClientChannel::SetTimebaseCallback err = %s", mach_error_string(res));
  204. }
  205. }
  206. bool JackMachClientChannel::Execute()
  207. {
  208. kern_return_t res;
  209. if ((res = mach_msg_server(JackRPCClient_server, 1024, fClientPort.GetPort(), 0)) != KERN_SUCCESS) {
  210. jack_error("JackMachClientChannel::Execute err = %s", mach_error_string(res));
  211. //fClient->ShutDown();
  212. return false;
  213. } else {
  214. return true;
  215. }
  216. }
  217. } // end of namespace