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.

353 lines
10KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackSystemDeps.h"
  20. #include "JackServer.h"
  21. #include "JackTime.h"
  22. #include "JackFreewheelDriver.h"
  23. #include "JackLoopbackDriver.h"
  24. #include "JackThreadedDriver.h"
  25. #include "JackGlobals.h"
  26. #include "JackLockedEngine.h"
  27. #include "JackAudioDriver.h"
  28. #include "JackChannel.h"
  29. #include "JackClientControl.h"
  30. #include "JackEngineControl.h"
  31. #include "JackGraphManager.h"
  32. #include "JackInternalClient.h"
  33. #include "JackError.h"
  34. #include "JackMessageBuffer.h"
  35. namespace Jack
  36. {
  37. JackServer* JackServer::fInstance = NULL;
  38. JackServer::JackServer(bool sync, bool temporary, long timeout, bool rt, long priority, long loopback, bool verbose, const char* server_name)
  39. {
  40. if (rt) {
  41. jack_info("JACK server starting in realtime mode with priority %ld", priority);
  42. } else {
  43. jack_info("JACK server starting in non-realtime mode");
  44. }
  45. fGraphManager = new JackGraphManager();
  46. fEngineControl = new JackEngineControl(sync, temporary, timeout, rt, priority, verbose, server_name);
  47. fEngine = new JackLockedEngine(fGraphManager, GetSynchroTable(), fEngineControl);
  48. fFreewheelDriver = new JackThreadedDriver(new JackFreewheelDriver(fEngine, GetSynchroTable()));
  49. fLoopbackDriver = new JackLoopbackDriver(fEngine, GetSynchroTable());
  50. fFreewheel = false;
  51. fLoopback = loopback;
  52. fDriverInfo = NULL;
  53. fAudioDriver = NULL;
  54. fInstance = this; // Unique instance
  55. jack_verbose = verbose;
  56. }
  57. JackServer::~JackServer()
  58. {
  59. delete fGraphManager;
  60. delete fAudioDriver;
  61. delete fFreewheelDriver;
  62. delete fLoopbackDriver;
  63. delete fEngine;
  64. delete fEngineControl;
  65. if (fDriverInfo) {
  66. UnloadDriverModule(fDriverInfo->handle);
  67. free(fDriverInfo);
  68. }
  69. }
  70. // TODO : better handling of intermediate failing cases...
  71. int JackServer::Open(jack_driver_desc_t* driver_desc, JSList* driver_params)
  72. {
  73. // TODO: move that in reworked JackServerGlobals::Init()
  74. JackMessageBuffer::Create();
  75. if (fChannel.Open(fEngineControl->fServerName, this) < 0) {
  76. jack_error("Server channel open error");
  77. return -1;
  78. }
  79. if (fEngine->Open() != 0) {
  80. jack_error("Cannot open engine");
  81. return -1;
  82. }
  83. if ((fDriverInfo = jack_load_driver(driver_desc)) == NULL) {
  84. return -1;
  85. }
  86. if ((fAudioDriver = fDriverInfo->initialize(fEngine, GetSynchroTable(), driver_params)) == NULL) {
  87. jack_error("Cannot initialize driver");
  88. return -1;
  89. }
  90. if (fFreewheelDriver->Open() != 0) { // before engine open
  91. jack_error("Cannot open driver");
  92. return -1;
  93. }
  94. if (fLoopbackDriver->Open(fEngineControl->fBufferSize, fEngineControl->fSampleRate, 1, 1, fLoopback, fLoopback, false, "loopback", "loopback", 0, 0) != 0) {
  95. jack_error("Cannot open driver");
  96. return -1;
  97. }
  98. if (fAudioDriver->Attach() != 0) {
  99. jack_error("Cannot attach audio driver");
  100. return -1;
  101. }
  102. if (fLoopback > 0 && fLoopbackDriver->Attach() != 0) {
  103. jack_error("Cannot attach loopback driver");
  104. return -1;
  105. }
  106. fFreewheelDriver->SetMaster(false);
  107. fAudioDriver->SetMaster(true);
  108. if (fLoopback > 0)
  109. fAudioDriver->AddSlave(fLoopbackDriver);
  110. fAudioDriver->AddSlave(fFreewheelDriver); // After ???
  111. InitTime();
  112. return 0;
  113. }
  114. int JackServer::Close()
  115. {
  116. jack_log("JackServer::Close");
  117. fChannel.Close();
  118. fAudioDriver->Detach();
  119. if (fLoopback > 0)
  120. fLoopbackDriver->Detach();
  121. fAudioDriver->Close();
  122. fFreewheelDriver->Close();
  123. fLoopbackDriver->Close();
  124. fEngine->Close();
  125. // TODO: move that in reworked JackServerGlobals::Destroy()
  126. JackMessageBuffer::Destroy();
  127. return 0;
  128. }
  129. int JackServer::InternalClientLoad(const char* client_name, const char* so_name, const char* objet_data, int options, int* int_ref, int* status)
  130. {
  131. JackLoadableInternalClient* client = new JackLoadableInternalClient1(fInstance, GetSynchroTable(), so_name, objet_data);
  132. assert(client);
  133. if (client->Init(so_name) < 0) {
  134. int my_status1 = *status | JackFailure;
  135. *status = (jack_status_t)my_status1;
  136. *int_ref = 0;
  137. return -1;
  138. }
  139. return InternalClientLoadAux(client, client_name, options, int_ref, status);
  140. }
  141. int JackServer::InternalClientLoad(const char* client_name, const char* so_name, const JSList * parameters, int options, int* int_ref, int* status)
  142. {
  143. JackLoadableInternalClient* client = new JackLoadableInternalClient2(fInstance, GetSynchroTable(), so_name, parameters);
  144. assert(client);
  145. if (client->Init(so_name) < 0) {
  146. int my_status1 = *status | JackFailure;
  147. *status = (jack_status_t)my_status1;
  148. *int_ref = 0;
  149. return -1;
  150. }
  151. return InternalClientLoadAux(client, client_name, options, int_ref, status);
  152. }
  153. int JackServer::InternalClientLoadAux(JackLoadableInternalClient* client, const char* client_name, int options, int* int_ref, int* status)
  154. {
  155. // Clear status
  156. *status = 0;
  157. if (client->Open(JACK_DEFAULT_SERVER_NAME, client_name, (jack_options_t)options, (jack_status_t*)status) < 0) {
  158. delete client;
  159. int my_status1 = *status | JackFailure;
  160. *status = (jack_status_t)my_status1;
  161. *int_ref = 0;
  162. return -1;
  163. } else {
  164. *int_ref = client->GetClientControl()->fRefNum;
  165. return 0;
  166. }
  167. }
  168. int JackServer::Start()
  169. {
  170. jack_log("JackServer::Start");
  171. fEngineControl->InitFrameTime();
  172. return fAudioDriver->Start();
  173. }
  174. int JackServer::Stop()
  175. {
  176. jack_log("JackServer::Stop");
  177. return fAudioDriver->Stop();
  178. }
  179. int JackServer::SetBufferSize(jack_nframes_t buffer_size)
  180. {
  181. jack_log("JackServer::SetBufferSize nframes = %ld", buffer_size);
  182. jack_nframes_t current_buffer_size = fEngineControl->fBufferSize;
  183. if (current_buffer_size == buffer_size) {
  184. jack_log("SetBufferSize: requirement for new buffer size equals current value");
  185. return 0;
  186. }
  187. if (fAudioDriver->IsFixedBufferSize()) {
  188. jack_log("SetBufferSize: driver only supports a fixed buffer size");
  189. return -1;
  190. }
  191. if (fAudioDriver->Stop() != 0) {
  192. jack_error("Cannot stop audio driver");
  193. return -1;
  194. }
  195. if (fAudioDriver->SetBufferSize(buffer_size) == 0) {
  196. fFreewheelDriver->SetBufferSize(buffer_size);
  197. fEngine->NotifyBufferSize(buffer_size);
  198. fEngineControl->InitFrameTime();
  199. return fAudioDriver->Start();
  200. } else { // Failure: try to restore current value
  201. jack_error("Cannot SetBufferSize for audio driver, restore current value %ld", current_buffer_size);
  202. fFreewheelDriver->SetBufferSize(current_buffer_size);
  203. fEngineControl->InitFrameTime();
  204. return fAudioDriver->Start();
  205. }
  206. }
  207. /*
  208. Freewheel mode is implemented by switching from the (audio + freewheel) driver to the freewheel driver only:
  209. - "global" connection state is saved
  210. - all audio driver ports are deconnected, thus there is no more dependancies with the audio driver
  211. - the freewheel driver will be synchronized with the end of graph execution : all clients are connected to the freewheel driver
  212. - the freewheel driver becomes the "master"
  213. Normal mode is restored with the connections state valid before freewheel mode was done. Thus one consider that
  214. no graph state change can be done during freewheel mode.
  215. */
  216. int JackServer::SetFreewheel(bool onoff)
  217. {
  218. jack_log("JackServer::SetFreewheel is = %ld want = %ld", fFreewheel, onoff);
  219. if (fFreewheel) {
  220. if (onoff) {
  221. return -1;
  222. } else {
  223. fFreewheel = false;
  224. fFreewheelDriver->Stop();
  225. fGraphManager->Restore(&fConnectionState); // Restore previous connection state
  226. fEngine->NotifyFreewheel(onoff);
  227. fFreewheelDriver->SetMaster(false);
  228. fEngineControl->InitFrameTime();
  229. return fAudioDriver->Start();
  230. }
  231. } else {
  232. if (onoff) {
  233. fFreewheel = true;
  234. fAudioDriver->Stop();
  235. fGraphManager->Save(&fConnectionState); // Save connection state
  236. fGraphManager->DisconnectAllPorts(fAudioDriver->GetClientControl()->fRefNum);
  237. fEngine->NotifyFreewheel(onoff);
  238. fFreewheelDriver->SetMaster(true);
  239. return fFreewheelDriver->Start();
  240. } else {
  241. return -1;
  242. }
  243. }
  244. }
  245. // Coming from the RT thread
  246. void JackServer::Notify(int refnum, int notify, int value)
  247. {
  248. switch (notify) {
  249. case kGraphOrderCallback:
  250. fEngine->NotifyGraphReorder();
  251. break;
  252. case kXRunCallback:
  253. fEngine->NotifyXRun(refnum);
  254. break;
  255. }
  256. }
  257. void JackServer::ClientKill(int refnum)
  258. {
  259. jack_log("JackServer::ClientKill ref = %ld", refnum);
  260. if (fEngine->ClientDeactivate(refnum) < 0) {
  261. jack_error("JackServer::ClientKill ref = %ld cannot be removed from the graph !!", refnum);
  262. }
  263. if (fEngine->ClientExternalClose(refnum) < 0) {
  264. jack_error("JackServer::ClientKill ref = %ld cannot be closed", refnum);
  265. }
  266. }
  267. //----------------------
  268. // Transport management
  269. //----------------------
  270. int JackServer::ReleaseTimebase(int refnum)
  271. {
  272. return fEngineControl->fTransport.ResetTimebase(refnum);
  273. }
  274. int JackServer::SetTimebaseCallback(int refnum, int conditional)
  275. {
  276. return fEngineControl->fTransport.SetTimebaseMaster(refnum, conditional);
  277. }
  278. JackLockedEngine* JackServer::GetEngine()
  279. {
  280. return fEngine;
  281. }
  282. JackSynchro* JackServer::GetSynchroTable()
  283. {
  284. return fSynchroTable;
  285. }
  286. JackEngineControl* JackServer::GetEngineControl()
  287. {
  288. return fEngineControl;
  289. }
  290. JackGraphManager* JackServer::GetGraphManager()
  291. {
  292. return fGraphManager;
  293. }
  294. } // end of namespace