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.

215 lines
6.3KB

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