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.

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