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.

265 lines
7.0KB

  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 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. #include "JackSocketClientChannel.h"
  16. #include "JackRequest.h"
  17. #include "JackClient.h"
  18. #include "JackGlobals.h"
  19. namespace Jack
  20. {
  21. JackSocketClientChannel::JackSocketClientChannel()
  22. {
  23. fThread = JackGlobals::MakeThread(this);
  24. fNotificationSocket = NULL;
  25. fClient = NULL;
  26. }
  27. JackSocketClientChannel::~JackSocketClientChannel()
  28. {
  29. delete fThread;
  30. delete fNotificationSocket;
  31. }
  32. int JackSocketClientChannel::Open(const char* name, JackClient* obj)
  33. {
  34. JackLog("JackSocketClientChannel::Open name = %s\n", name);
  35. if (fRequestSocket.Connect(jack_server_dir, 0) < 0) {
  36. jack_error("Cannot connect to server socket");
  37. goto error;
  38. }
  39. if (fNotificationListenSocket.Bind(jack_client_dir, name, 0) < 0) {
  40. jack_error("Cannot bind socket");
  41. goto error;
  42. }
  43. fClient = obj;
  44. return 0;
  45. error:
  46. fRequestSocket.Close();
  47. fNotificationListenSocket.Close();
  48. return -1;
  49. }
  50. void JackSocketClientChannel::Close()
  51. {
  52. fRequestSocket.Close();
  53. fNotificationListenSocket.Close();
  54. if (fNotificationSocket)
  55. fNotificationSocket->Close();
  56. }
  57. int JackSocketClientChannel::Start()
  58. {
  59. JackLog("JackSocketClientChannel::Start\n");
  60. if (fThread->Start() != 0) {
  61. jack_error("Cannot start Jack client listener");
  62. return -1;
  63. } else {
  64. return 0;
  65. }
  66. }
  67. void JackSocketClientChannel::Stop()
  68. {
  69. JackLog("JackSocketClientChannel::Stop\n");
  70. fThread->Kill();
  71. }
  72. void JackSocketClientChannel::ServerSyncCall(JackRequest* req, JackResult* res, int* result)
  73. {
  74. if (req->Write(&fRequestSocket) < 0) {
  75. jack_error("Could not write request type = %ld", req->fType);
  76. *result = -1;
  77. return ;
  78. }
  79. if (res->Read(&fRequestSocket) < 0) {
  80. jack_error("Could not read result type = %ld", req->fType);
  81. *result = -1;
  82. return ;
  83. }
  84. *result = res->fResult;
  85. }
  86. void JackSocketClientChannel::ServerAsyncCall(JackRequest* req, JackResult* res, int* result)
  87. {
  88. if (req->Write(&fRequestSocket) < 0) {
  89. jack_error("Could not write request type = %ld", req->fType);
  90. *result = -1;
  91. } else {
  92. *result = 0;
  93. }
  94. }
  95. void JackSocketClientChannel::ClientNew(const char* name, int* shared_engine, int* shared_client, int* shared_ports, int* result)
  96. {
  97. JackClientNewRequest req(name);
  98. JackClientNewResult res;
  99. ServerSyncCall(&req, &res, result);
  100. *shared_engine = res.fSharedEngine;
  101. *shared_client = res.fSharedClient;
  102. *shared_ports = res.fSharedPorts;
  103. }
  104. void JackSocketClientChannel::ClientClose(int refnum, int* result)
  105. {
  106. JackClientCloseRequest req(refnum);
  107. JackResult res;
  108. ServerAsyncCall(&req, &res, result);
  109. }
  110. void JackSocketClientChannel::ClientActivate(int refnum, int* result)
  111. {
  112. JackActivateRequest req(refnum);
  113. JackResult res;
  114. ServerSyncCall(&req, &res, result);
  115. }
  116. void JackSocketClientChannel::ClientDeactivate(int refnum, int* result)
  117. {
  118. JackDeactivateRequest req(refnum);
  119. JackResult res;
  120. ServerSyncCall(&req, &res, result);
  121. }
  122. void JackSocketClientChannel::PortRegister(int refnum, const char* name, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index, int* result)
  123. {
  124. JackPortRegisterRequest req(refnum, name, "audio", flags, buffer_size);
  125. JackPortRegisterResult res;
  126. ServerSyncCall(&req, &res, result);
  127. *port_index = res.fPortIndex;
  128. }
  129. void JackSocketClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
  130. {
  131. JackPortUnRegisterRequest req(refnum, port_index);
  132. JackResult res;
  133. ServerSyncCall(&req, &res, result);
  134. }
  135. void JackSocketClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
  136. {
  137. JackPortConnectNameRequest req(refnum, src, dst);
  138. JackResult res;
  139. ServerSyncCall(&req, &res, result);
  140. }
  141. void JackSocketClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
  142. {
  143. JackPortDisconnectNameRequest req(refnum, src, dst);
  144. JackResult res;
  145. ServerSyncCall(&req, &res, result);
  146. }
  147. void JackSocketClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  148. {
  149. JackPortConnectRequest req(refnum, src, dst);
  150. JackResult res;
  151. ServerSyncCall(&req, &res, result);
  152. }
  153. void JackSocketClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  154. {
  155. JackPortDisconnectRequest req(refnum, src, dst);
  156. JackResult res;
  157. ServerSyncCall(&req, &res, result);
  158. }
  159. void JackSocketClientChannel::SetBufferSize(jack_nframes_t nframes, int* result)
  160. {
  161. JackSetBufferSizeRequest req(nframes);
  162. JackResult res;
  163. ServerSyncCall(&req, &res, result);
  164. }
  165. void JackSocketClientChannel::SetFreewheel(int onoff, int* result)
  166. {
  167. JackSetFreeWheelRequest req(onoff);
  168. JackResult res;
  169. ServerSyncCall(&req, &res, result);
  170. }
  171. void JackSocketClientChannel::ReleaseTimebase(int refnum, int* result)
  172. {
  173. JackReleaseTimebaseRequest req(refnum);
  174. JackResult res;
  175. ServerSyncCall(&req, &res, result);
  176. }
  177. void JackSocketClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
  178. {
  179. JackSetTimebaseCallbackRequest req(refnum, conditional);
  180. JackResult res;
  181. ServerSyncCall(&req, &res, result);
  182. }
  183. bool JackSocketClientChannel::Init()
  184. {
  185. JackLog("JackSocketClientChannel::Init \n");
  186. fNotificationSocket = fNotificationListenSocket.Accept();
  187. // No more needed
  188. fNotificationListenSocket.Close();
  189. if (!fNotificationSocket) {
  190. jack_error("JackSocketClientChannel: cannot establish notication socket");
  191. return false;
  192. } else {
  193. return true;
  194. }
  195. }
  196. bool JackSocketClientChannel::Execute()
  197. {
  198. JackClientNotification event;
  199. JackResult res;
  200. //fClient->Init(); // To be checked
  201. if (event.Read(fNotificationSocket) < 0) {
  202. fNotificationSocket->Close();
  203. jack_error("JackSocketClientChannel read fail");
  204. goto error;
  205. }
  206. res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fValue);
  207. if (event.fSync) {
  208. if (res.Write(fNotificationSocket) < 0) {
  209. fNotificationSocket->Close();
  210. jack_error("JackSocketClientChannel write fail");
  211. goto error;
  212. }
  213. }
  214. return true;
  215. error:
  216. fClient->ShutDown();
  217. return false;
  218. }
  219. } // end of namespace