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.

216 lines
6.4KB

  1. /*
  2. Copyright (C) 2004-2008 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 "JackLibClient.h"
  16. #include "JackTime.h"
  17. #include "JackLibGlobals.h"
  18. #include "JackGlobals.h"
  19. #include "JackPlatformPlug.h"
  20. #include "JackTools.h"
  21. namespace Jack
  22. {
  23. // Used for external C API (JackAPI.cpp)
  24. JackGraphManager* GetGraphManager()
  25. {
  26. if (JackLibGlobals::fGlobals) {
  27. return JackLibGlobals::fGlobals->fGraphManager;
  28. } else {
  29. return NULL;
  30. }
  31. }
  32. JackEngineControl* GetEngineControl()
  33. {
  34. if (JackLibGlobals::fGlobals) {
  35. return JackLibGlobals::fGlobals->fEngineControl;
  36. } else {
  37. return NULL;
  38. }
  39. }
  40. JackSynchro* GetSynchroTable()
  41. {
  42. return (JackLibGlobals::fGlobals ? JackLibGlobals::fGlobals->fSynchroTable : 0);
  43. }
  44. // Used for client-side Metadata API (JackLibAPI.cpp)
  45. JackMetadata* GetMetadata()
  46. {
  47. if (JackLibGlobals::fGlobals) {
  48. return JackLibGlobals::fGlobals->fMetadata;
  49. } else {
  50. return NULL;
  51. }
  52. }
  53. //-------------------
  54. // Client management
  55. //-------------------
  56. /*
  57. ShutDown is called:
  58. - from the RT thread when Execute method fails
  59. - possibly from a "closed" notification channel
  60. (Not needed since the synch object used (Sema of Fifo will fails when server quits... see ShutDown))
  61. */
  62. void JackLibClient::ShutDown(jack_status_t code, const char* message)
  63. {
  64. jack_log("JackLibClient::ShutDown");
  65. JackGlobals::fServerRunning = false;
  66. JackClient::ShutDown(code, message);
  67. }
  68. JackLibClient::JackLibClient(JackSynchro* table): JackClient(table)
  69. {
  70. jack_log("JackLibClient::JackLibClient table = %x", table);
  71. fChannel = new JackClientChannel();
  72. }
  73. JackLibClient::~JackLibClient()
  74. {
  75. jack_log("JackLibClient::~JackLibClient");
  76. delete fChannel;
  77. }
  78. int JackLibClient::Open(const char* server_name, const char* name, jack_uuid_t uuid, jack_options_t options, jack_status_t* status)
  79. {
  80. int shared_engine, shared_client, shared_graph, result;
  81. bool res;
  82. jack_log("JackLibClient::Open name = %s", name);
  83. if (strlen(name) >= JACK_CLIENT_NAME_SIZE) {
  84. jack_error("\"%s\" is too long to be used as a JACK client name.\n"
  85. "Please use %lu characters or less",
  86. name,
  87. JACK_CLIENT_NAME_SIZE - 1);
  88. return -1;
  89. }
  90. strncpy(fServerName, server_name, sizeof(fServerName));
  91. fServerName[sizeof(fServerName) - 1] = 0;
  92. // Open server/client channel
  93. char name_res[JACK_CLIENT_NAME_SIZE+1];
  94. if (fChannel->Open(server_name, name, uuid, name_res, this, options, status) < 0) {
  95. jack_error("Cannot connect to the server");
  96. goto error;
  97. }
  98. // Start receiving notifications
  99. if (fChannel->Start() < 0) {
  100. jack_error("Cannot start channel");
  101. goto error;
  102. }
  103. // Require new client
  104. fChannel->ClientOpen(name_res, JackTools::GetPID(), uuid, &shared_engine, &shared_client, &shared_graph, &result);
  105. if (result < 0) {
  106. jack_error("Cannot open %s client", name_res);
  107. goto error;
  108. }
  109. try {
  110. // Map shared memory segments
  111. JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
  112. JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
  113. fClientControl.SetShmIndex(shared_client, fServerName);
  114. JackGlobals::fVerbose = GetEngineControl()->fVerbose;
  115. } catch (...) {
  116. jack_error("Map shared memory segments exception");
  117. goto error;
  118. }
  119. SetupDriverSync(false);
  120. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  121. assert(JackGlobals::fSynchroMutex);
  122. JackGlobals::fSynchroMutex->Lock();
  123. res = fSynchroTable[GetClientControl()->fRefNum].Connect(name_res, fServerName);
  124. JackGlobals::fSynchroMutex->Unlock();
  125. if (!res) {
  126. jack_error("Cannot ConnectSemaphore %s client", name_res);
  127. goto error;
  128. }
  129. JackGlobals::fClientTable[GetClientControl()->fRefNum] = this;
  130. SetClockSource(GetEngineControl()->fClockSource);
  131. jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
  132. return 0;
  133. error:
  134. fChannel->Stop();
  135. fChannel->Close();
  136. return -1;
  137. }
  138. // Notifications received from the server
  139. // TODO this should be done once for all clients in the process, when a shared notification channel
  140. // will be shared by all clients...
  141. int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
  142. {
  143. int res = 0;
  144. assert(JackGlobals::fSynchroMutex);
  145. JackGlobals::fSynchroMutex->Lock();
  146. // Done all time
  147. switch (notify) {
  148. case kAddClient:
  149. jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
  150. // the synchro must be usable in I/O mode when several clients live in the same process
  151. res = fSynchroTable[refnum].Connect(name, fServerName) ? 0 : -1;
  152. break;
  153. case kRemoveClient:
  154. jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
  155. if (GetClientControl() && strcmp(GetClientControl()->fName, name) != 0) {
  156. res = fSynchroTable[refnum].Disconnect() ? 0 : -1;
  157. }
  158. break;
  159. }
  160. JackGlobals::fSynchroMutex->Unlock();
  161. return res;
  162. }
  163. JackGraphManager* JackLibClient::GetGraphManager() const
  164. {
  165. assert(JackLibGlobals::fGlobals->fGraphManager);
  166. return JackLibGlobals::fGlobals->fGraphManager;
  167. }
  168. JackEngineControl* JackLibClient::GetEngineControl() const
  169. {
  170. assert(JackLibGlobals::fGlobals->fEngineControl);
  171. return JackLibGlobals::fGlobals->fEngineControl;
  172. }
  173. JackClientControl* JackLibClient::GetClientControl() const
  174. {
  175. return fClientControl;
  176. }
  177. } // end of namespace