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.

238 lines
7.6KB

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