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.

469 lines
13KB

  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. #include "JackSystemDeps.h"
  17. #include "JackServerGlobals.h"
  18. #include "JackTime.h"
  19. #include "JackFreewheelDriver.h"
  20. #include "JackThreadedDriver.h"
  21. #include "JackGlobals.h"
  22. #include "JackLockedEngine.h"
  23. #include "JackAudioDriver.h"
  24. #include "JackChannel.h"
  25. #include "JackClientControl.h"
  26. #include "JackEngineControl.h"
  27. #include "JackGraphManager.h"
  28. #include "JackInternalClient.h"
  29. #include "JackError.h"
  30. #include "JackMessageBuffer.h"
  31. #include "JackInternalSessionLoader.h"
  32. const char * jack_get_self_connect_mode_description(char mode);
  33. namespace Jack
  34. {
  35. //----------------
  36. // Server control
  37. //----------------
  38. JackServer::JackServer(bool sync, bool temporary, int timeout, bool rt, int priority, int port_max, bool verbose, jack_timer_type_t clock, char self_connect_mode, 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. jack_info("self-connect-mode is \"%s\"", jack_get_self_connect_mode_description(self_connect_mode));
  46. fGraphManager = JackGraphManager::Allocate(port_max);
  47. fEngineControl = new JackEngineControl(sync, temporary, timeout, rt, priority, verbose, clock, server_name);
  48. fEngine = new JackLockedEngine(fGraphManager, GetSynchroTable(), fEngineControl, self_connect_mode);
  49. // A distinction is made between the threaded freewheel driver and the
  50. // regular freewheel driver because the freewheel driver needs to run in
  51. // threaded mode when freewheel mode is active and needs to run as a slave
  52. // when freewheel mode isn't active.
  53. JackFreewheelDriver* freewheelDriver = new JackFreewheelDriver(fEngine, GetSynchroTable());
  54. fThreadedFreewheelDriver = new JackThreadedDriver(freewheelDriver);
  55. fFreewheelDriver = freewheelDriver;
  56. fDriverInfo = new JackDriverInfo();
  57. fAudioDriver = NULL;
  58. fFreewheel = false;
  59. JackServerGlobals::fInstance = this; // Unique instance
  60. JackServerGlobals::fUserCount = 1; // One user
  61. JackGlobals::fVerbose = verbose;
  62. }
  63. JackServer::~JackServer()
  64. {
  65. JackGraphManager::Destroy(fGraphManager);
  66. delete fDriverInfo;
  67. delete fThreadedFreewheelDriver;
  68. delete fEngine;
  69. delete fEngineControl;
  70. }
  71. int JackServer::Open(jack_driver_desc_t* driver_desc, JSList* driver_params)
  72. {
  73. // TODO: move that in reworked JackServerGlobals::Init()
  74. if (!JackMessageBuffer::Create()) {
  75. jack_error("Cannot create message buffer");
  76. }
  77. if ((fAudioDriver = fDriverInfo->Open(driver_desc, fEngine, GetSynchroTable(), driver_params)) == NULL) {
  78. jack_error("Cannot initialize driver");
  79. goto fail_close1;
  80. }
  81. if (fRequestChannel.Open(fEngineControl->fServerName, this) < 0) {
  82. jack_error("Server channel open error");
  83. goto fail_close2;
  84. }
  85. if (fEngine->Open() < 0) {
  86. jack_error("Cannot open engine");
  87. goto fail_close3;
  88. }
  89. if (fFreewheelDriver->Open() < 0) {
  90. jack_error("Cannot open freewheel driver");
  91. goto fail_close4;
  92. }
  93. if (fAudioDriver->Attach() < 0) {
  94. jack_error("Cannot attach audio driver");
  95. goto fail_close5;
  96. }
  97. fFreewheelDriver->SetMaster(false);
  98. fAudioDriver->SetMaster(true);
  99. fAudioDriver->AddSlave(fFreewheelDriver);
  100. InitTime();
  101. SetClockSource(fEngineControl->fClockSource);
  102. return 0;
  103. fail_close5:
  104. fFreewheelDriver->Close();
  105. fail_close4:
  106. fEngine->Close();
  107. fail_close3:
  108. fRequestChannel.Close();
  109. fail_close2:
  110. fAudioDriver->Close();
  111. fail_close1:
  112. JackMessageBuffer::Destroy();
  113. return -1;
  114. }
  115. int JackServer::Close()
  116. {
  117. jack_log("JackServer::Close");
  118. fRequestChannel.Close();
  119. fAudioDriver->Detach();
  120. fAudioDriver->Close();
  121. fFreewheelDriver->Close();
  122. fEngine->Close();
  123. // TODO: move that in reworked JackServerGlobals::Destroy()
  124. JackMessageBuffer::Destroy();
  125. EndTime();
  126. return 0;
  127. }
  128. int JackServer::Start()
  129. {
  130. jack_log("JackServer::Start");
  131. if (fAudioDriver->Start() < 0) {
  132. return -1;
  133. }
  134. return fRequestChannel.Start();
  135. }
  136. int JackServer::Stop()
  137. {
  138. jack_log("JackServer::Stop");
  139. int res = -1;
  140. if (fFreewheel) {
  141. if (fThreadedFreewheelDriver) {
  142. res = fThreadedFreewheelDriver->Stop();
  143. }
  144. } else {
  145. if (fAudioDriver) {
  146. res = fAudioDriver->Stop();
  147. }
  148. }
  149. fEngine->NotifyQuit();
  150. fRequestChannel.Stop();
  151. fEngine->NotifyFailure(JackFailure | JackServerError, JACK_SERVER_FAILURE);
  152. return res;
  153. }
  154. bool JackServer::IsRunning()
  155. {
  156. jack_log("JackServer::IsRunning");
  157. assert(fAudioDriver);
  158. return fAudioDriver->IsRunning();
  159. }
  160. //------------------
  161. // Internal clients
  162. //------------------
  163. int JackServer::InternalClientLoad1(const char* client_name, const char* so_name, const char* objet_data, int options, int* int_ref, jack_uuid_t uuid, int* status)
  164. {
  165. JackLoadableInternalClient* client = new JackLoadableInternalClient1(JackServerGlobals::fInstance, GetSynchroTable(), objet_data);
  166. assert(client);
  167. return InternalClientLoadAux(client, so_name, client_name, options, int_ref, uuid, status);
  168. }
  169. int JackServer::InternalClientLoad2(const char* client_name, const char* so_name, const JSList * parameters, int options, int* int_ref, jack_uuid_t uuid, int* status)
  170. {
  171. JackLoadableInternalClient* client = new JackLoadableInternalClient2(JackServerGlobals::fInstance, GetSynchroTable(), parameters);
  172. assert(client);
  173. return InternalClientLoadAux(client, so_name, client_name, options, int_ref, uuid, status);
  174. }
  175. int JackServer::InternalClientLoadAux(JackLoadableInternalClient* client, const char* so_name, const char* client_name, int options, int* int_ref, jack_uuid_t uuid, int* status)
  176. {
  177. // Clear status
  178. *status = 0;
  179. // Client object is internally kept in JackEngine
  180. if ((client->Init(so_name) < 0) || (client->Open(JackTools::DefaultServerName(), client_name, uuid, (jack_options_t)options, (jack_status_t*)status) < 0)) {
  181. delete client;
  182. int my_status1 = *status | JackFailure;
  183. *status = (jack_status_t)my_status1;
  184. *int_ref = 0;
  185. return -1;
  186. } else {
  187. *int_ref = client->GetClientControl()->fRefNum;
  188. return 0;
  189. }
  190. }
  191. //-----------------------
  192. // Internal session file
  193. //-----------------------
  194. int JackServer::LoadInternalSessionFile(const char* file)
  195. {
  196. JackInternalSessionLoader loader(this);
  197. return loader.Load(file);
  198. }
  199. //---------------------------
  200. // From request thread : API
  201. //---------------------------
  202. int JackServer::SetBufferSize(jack_nframes_t buffer_size)
  203. {
  204. jack_log("JackServer::SetBufferSize nframes = %ld", buffer_size);
  205. jack_nframes_t current_buffer_size = fEngineControl->fBufferSize;
  206. if (current_buffer_size == buffer_size) {
  207. jack_log("SetBufferSize: requirement for new buffer size equals current value");
  208. return 0;
  209. }
  210. if (fAudioDriver->IsFixedBufferSize()) {
  211. jack_log("SetBufferSize: driver only supports a fixed buffer size");
  212. return -1;
  213. }
  214. if (fAudioDriver->Stop() != 0) {
  215. jack_error("Cannot stop audio driver");
  216. return -1;
  217. }
  218. if (fAudioDriver->SetBufferSize(buffer_size) == 0) {
  219. fEngine->NotifyBufferSize(buffer_size);
  220. return fAudioDriver->Start();
  221. } else { // Failure: try to restore current value
  222. jack_error("Cannot SetBufferSize for audio driver, restore current value %ld", current_buffer_size);
  223. fAudioDriver->SetBufferSize(current_buffer_size);
  224. fAudioDriver->Start();
  225. // SetBufferSize actually failed, so return an error...
  226. return -1;
  227. }
  228. }
  229. /*
  230. Freewheel mode is implemented by switching from the (audio [slaves] + freewheel) driver to the freewheel driver only:
  231. - "global" connection state is saved
  232. - all audio driver and slaves ports are deconnected, thus there is no more dependencies with the audio driver and slaves
  233. - the freewheel driver will be synchronized with the end of graph execution : all clients are connected to the freewheel driver
  234. - the freewheel driver becomes the "master"
  235. Normal mode is restored with the connections state valid before freewheel mode was done. Thus one consider that
  236. no graph state change can be done during freewheel mode.
  237. */
  238. int JackServer::SetFreewheel(bool onoff)
  239. {
  240. jack_log("JackServer::SetFreewheel is = %ld want = %ld", fFreewheel, onoff);
  241. if (fFreewheel) {
  242. if (onoff) {
  243. return -1;
  244. } else {
  245. fFreewheel = false;
  246. fThreadedFreewheelDriver->Stop();
  247. fGraphManager->Restore(&fConnectionState); // Restore connection state
  248. fEngine->NotifyFreewheel(onoff);
  249. fFreewheelDriver->SetMaster(false);
  250. fAudioDriver->SetMaster(true);
  251. return fAudioDriver->Start();
  252. }
  253. } else {
  254. if (onoff) {
  255. fFreewheel = true;
  256. fAudioDriver->Stop();
  257. fGraphManager->Save(&fConnectionState); // Save connection state
  258. // Disconnect all slaves
  259. std::list<JackDriverInterface*> slave_list = fAudioDriver->GetSlaves();
  260. std::list<JackDriverInterface*>::const_iterator it;
  261. for (it = slave_list.begin(); it != slave_list.end(); it++) {
  262. JackDriver* slave = dynamic_cast<JackDriver*>(*it);
  263. assert(slave);
  264. fGraphManager->DisconnectAllPorts(slave->GetClientControl()->fRefNum);
  265. }
  266. // Disconnect master
  267. fGraphManager->DisconnectAllPorts(fAudioDriver->GetClientControl()->fRefNum);
  268. fEngine->NotifyFreewheel(onoff);
  269. fAudioDriver->SetMaster(false);
  270. fFreewheelDriver->SetMaster(true);
  271. return fThreadedFreewheelDriver->Start();
  272. } else {
  273. return -1;
  274. }
  275. }
  276. }
  277. //---------------------------
  278. // Coming from the RT thread
  279. //---------------------------
  280. void JackServer::Notify(int refnum, int notify, int value)
  281. {
  282. switch (notify) {
  283. case kGraphOrderCallback:
  284. fEngine->NotifyGraphReorder();
  285. break;
  286. case kXRunCallback:
  287. fEngine->NotifyClientXRun(refnum);
  288. break;
  289. }
  290. }
  291. //--------------------
  292. // Backend management
  293. //--------------------
  294. JackDriverInfo* JackServer::AddSlave(jack_driver_desc_t* driver_desc, JSList* driver_params)
  295. {
  296. JackDriverInfo* info = new JackDriverInfo();
  297. JackDriverClientInterface* slave = info->Open(driver_desc, fEngine, GetSynchroTable(), driver_params);
  298. if (!slave) {
  299. goto error1;
  300. }
  301. if (slave->Attach() < 0) {
  302. goto error2;
  303. }
  304. slave->SetMaster(false);
  305. fAudioDriver->AddSlave(slave);
  306. return info;
  307. error2:
  308. slave->Close();
  309. error1:
  310. delete info;
  311. return NULL;
  312. }
  313. void JackServer::RemoveSlave(JackDriverInfo* info)
  314. {
  315. JackDriverClientInterface* slave = info->GetBackend();
  316. fAudioDriver->RemoveSlave(slave);
  317. slave->Detach();
  318. slave->Close();
  319. }
  320. int JackServer::SwitchMaster(jack_driver_desc_t* driver_desc, JSList* driver_params)
  321. {
  322. std::list<JackDriverInterface*> slave_list;
  323. std::list<JackDriverInterface*>::const_iterator it;
  324. // Remove current master
  325. fAudioDriver->Stop();
  326. fAudioDriver->Detach();
  327. fAudioDriver->Close();
  328. // Open new master
  329. JackDriverInfo* info = new JackDriverInfo();
  330. JackDriverClientInterface* master = info->Open(driver_desc, fEngine, GetSynchroTable(), driver_params);
  331. if (!master) {
  332. goto error;
  333. }
  334. // Get slaves list
  335. slave_list = fAudioDriver->GetSlaves();
  336. // Move slaves in new master
  337. for (it = slave_list.begin(); it != slave_list.end(); it++) {
  338. JackDriverInterface* slave = *it;
  339. master->AddSlave(slave);
  340. }
  341. // Delete old master
  342. delete fDriverInfo;
  343. // Activate master
  344. fAudioDriver = master;
  345. fDriverInfo = info;
  346. if (fAudioDriver->Attach() < 0) {
  347. goto error;
  348. }
  349. // Notify clients of new values
  350. fEngine->NotifyBufferSize(fEngineControl->fBufferSize);
  351. fEngine->NotifySampleRate(fEngineControl->fSampleRate);
  352. // And finally start
  353. fAudioDriver->SetMaster(true);
  354. return fAudioDriver->Start();
  355. error:
  356. delete info;
  357. return -1;
  358. }
  359. //----------------------
  360. // Transport management
  361. //----------------------
  362. int JackServer::ReleaseTimebase(int refnum)
  363. {
  364. return fEngineControl->fTransport.ResetTimebase(refnum);
  365. }
  366. int JackServer::SetTimebaseCallback(int refnum, int conditional)
  367. {
  368. return fEngineControl->fTransport.SetTimebaseMaster(refnum, conditional);
  369. }
  370. JackLockedEngine* JackServer::GetEngine()
  371. {
  372. return fEngine;
  373. }
  374. JackSynchro* JackServer::GetSynchroTable()
  375. {
  376. return fSynchroTable;
  377. }
  378. JackEngineControl* JackServer::GetEngineControl()
  379. {
  380. return fEngineControl;
  381. }
  382. JackGraphManager* JackServer::GetGraphManager()
  383. {
  384. return fGraphManager;
  385. }
  386. } // end of namespace