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.

418 lines
13KB

  1. /*
  2. Copyright (C) 2005 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackServerGlobals.h"
  16. #include "JackLockedEngine.h"
  17. #include "JackTools.h"
  18. #include "shm.h"
  19. #include <getopt.h>
  20. #include <errno.h>
  21. static char* server_name = NULL;
  22. namespace Jack
  23. {
  24. JackServer* JackServerGlobals::fInstance;
  25. unsigned int JackServerGlobals::fUserCount;
  26. int JackServerGlobals::fRTNotificationSocket;
  27. std::map<std::string, JackDriverInfo*> JackServerGlobals::fSlavesList;
  28. std::map<std::string, int> JackServerGlobals::fInternalsList;
  29. bool (* JackServerGlobals::on_device_acquire)(const char * device_name) = NULL;
  30. void (* JackServerGlobals::on_device_release)(const char * device_name) = NULL;
  31. int JackServerGlobals::Start(const char* server_name,
  32. jack_driver_desc_t* driver_desc,
  33. JSList* driver_params,
  34. int sync,
  35. int temporary,
  36. int time_out_ms,
  37. int rt,
  38. int priority,
  39. int port_max,
  40. int verbose,
  41. jack_timer_type_t clock,
  42. JackSelfConnectMode self_connect_mode)
  43. {
  44. jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync, time_out_ms, rt, priority, verbose);
  45. new JackServer(sync, temporary, time_out_ms, rt, priority, port_max, verbose, clock, self_connect_mode, server_name); // Will setup fInstance and fUserCount globals
  46. int res = fInstance->Open(driver_desc, driver_params);
  47. return (res < 0) ? res : fInstance->Start();
  48. }
  49. void JackServerGlobals::Stop()
  50. {
  51. jack_log("Jackdmp: server close");
  52. fInstance->Stop();
  53. fInstance->Close();
  54. }
  55. void JackServerGlobals::Delete()
  56. {
  57. jack_log("Jackdmp: delete server");
  58. // Slave drivers
  59. std::map<std::string, JackDriverInfo*>::iterator it1;
  60. for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
  61. JackDriverInfo* info = (*it1).second;
  62. if (info) {
  63. fInstance->RemoveSlave((info));
  64. delete (info);
  65. }
  66. }
  67. fSlavesList.clear();
  68. // Internal clients
  69. std::map<std::string, int> ::iterator it2;
  70. for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
  71. int status;
  72. int refnum = (*it2).second;
  73. if (refnum > 0) {
  74. // Client object is internally kept in JackEngine, and will be deallocated in InternalClientUnload
  75. fInstance->GetEngine()->InternalClientUnload(refnum, &status);
  76. }
  77. }
  78. fInternalsList.clear();
  79. delete fInstance;
  80. fInstance = NULL;
  81. }
  82. bool JackServerGlobals::Init()
  83. {
  84. int realtime = 0;
  85. int client_timeout = 0; /* msecs; if zero, use period size. */
  86. int realtime_priority = 10;
  87. int verbose_aux = 0;
  88. int do_mlock = 1;
  89. unsigned int port_max = 128;
  90. int do_unlock = 0;
  91. int temporary = 0;
  92. int opt = 0;
  93. int option_index = 0;
  94. char *master_driver_name = NULL;
  95. char **master_driver_args = NULL;
  96. JSList* master_driver_params = NULL;
  97. jack_driver_desc_t* driver_desc;
  98. jack_timer_type_t clock_source = JACK_TIMER_SYSTEM_CLOCK;
  99. int driver_nargs = 1;
  100. JSList* drivers = NULL;
  101. int loopback = 0;
  102. int sync = 0;
  103. int rc, i;
  104. int ret;
  105. int replace_registry = 0;
  106. FILE* fp = 0;
  107. char filename[255];
  108. char buffer[255];
  109. int argc = 0;
  110. char* argv[32];
  111. // First user starts the server
  112. if (fUserCount++ == 0) {
  113. jack_log("JackServerGlobals Init");
  114. const char *options = "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
  115. #ifdef __linux__
  116. "c:"
  117. #endif
  118. ;
  119. struct option long_options[] = {
  120. #ifdef __linux__
  121. { "clock-source", 1, 0, 'c' },
  122. #endif
  123. { "loopback-driver", 1, 0, 'L' },
  124. { "audio-driver", 1, 0, 'd' },
  125. { "midi-driver", 1, 0, 'X' },
  126. { "internal-client", 1, 0, 'I' },
  127. { "verbose", 0, 0, 'v' },
  128. { "help", 0, 0, 'h' },
  129. { "port-max", 1, 0, 'p' },
  130. { "no-mlock", 0, 0, 'm' },
  131. { "name", 1, 0, 'n' },
  132. { "unlock", 0, 0, 'u' },
  133. { "realtime", 0, 0, 'R' },
  134. { "no-realtime", 0, 0, 'r' },
  135. { "replace-registry", 0, &replace_registry, 0 },
  136. { "loopback", 0, 0, 'L' },
  137. { "realtime-priority", 1, 0, 'P' },
  138. { "timeout", 1, 0, 't' },
  139. { "temporary", 0, 0, 'T' },
  140. { "version", 0, 0, 'V' },
  141. { "silent", 0, 0, 's' },
  142. { "sync", 0, 0, 'S' },
  143. { 0, 0, 0, 0 }
  144. };
  145. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  146. fp = fopen(filename, "r");
  147. if (!fp) {
  148. fp = fopen("/etc/jackdrc", "r");
  149. }
  150. // if still not found, check old config name for backwards compatability
  151. if (!fp) {
  152. fp = fopen("/etc/jackd.conf", "r");
  153. }
  154. argc = 0;
  155. if (fp) {
  156. ret = fscanf(fp, "%s", buffer);
  157. while (ret != 0 && ret != EOF) {
  158. argv[argc] = (char*)malloc(64);
  159. strcpy(argv[argc], buffer);
  160. ret = fscanf(fp, "%s", buffer);
  161. argc++;
  162. }
  163. fclose(fp);
  164. }
  165. /*
  166. For testing
  167. int argc = 15;
  168. char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
  169. */
  170. opterr = 0;
  171. optind = 1; // Important : to reset argv parsing
  172. while (!master_driver_name &&
  173. (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
  174. switch (opt) {
  175. case 'c':
  176. if (tolower (optarg[0]) == 'h') {
  177. clock_source = JACK_TIMER_HPET;
  178. } else if (tolower (optarg[0]) == 'c') {
  179. clock_source = JACK_TIMER_CYCLE_COUNTER;
  180. } else if (tolower (optarg[0]) == 's') {
  181. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  182. } else {
  183. jack_error("unknown option character %c", optopt);
  184. }
  185. break;
  186. case 'd':
  187. master_driver_name = optarg;
  188. break;
  189. case 'L':
  190. loopback = atoi(optarg);
  191. break;
  192. case 'X':
  193. fSlavesList[optarg] = NULL;
  194. break;
  195. case 'I':
  196. fInternalsList[optarg] = -1;
  197. break;
  198. case 'p':
  199. port_max = (unsigned int)atol(optarg);
  200. break;
  201. case 'm':
  202. do_mlock = 0;
  203. break;
  204. case 'u':
  205. do_unlock = 1;
  206. break;
  207. case 'v':
  208. verbose_aux = 1;
  209. break;
  210. case 'S':
  211. sync = 1;
  212. break;
  213. case 'n':
  214. server_name = optarg;
  215. break;
  216. case 'P':
  217. realtime_priority = atoi(optarg);
  218. break;
  219. case 'r':
  220. realtime = 0;
  221. break;
  222. case 'R':
  223. realtime = 1;
  224. break;
  225. case 'T':
  226. temporary = 1;
  227. break;
  228. case 't':
  229. client_timeout = atoi(optarg);
  230. break;
  231. default:
  232. jack_error("unknown option character %c", optopt);
  233. break;
  234. }
  235. }
  236. drivers = jack_drivers_load(drivers);
  237. if (!drivers) {
  238. jack_error("jackdmp: no drivers found; exiting");
  239. goto error;
  240. }
  241. driver_desc = jack_find_driver_descriptor(drivers, master_driver_name);
  242. if (!driver_desc) {
  243. jack_error("jackdmp: unknown master driver '%s'", master_driver_name);
  244. goto error;
  245. }
  246. if (optind < argc) {
  247. driver_nargs = 1 + argc - optind;
  248. } else {
  249. driver_nargs = 1;
  250. }
  251. if (driver_nargs == 0) {
  252. jack_error("No driver specified ... hmm. JACK won't do"
  253. " anything when run like this.");
  254. goto error;
  255. }
  256. master_driver_args = (char**)malloc(sizeof(char*) * driver_nargs);
  257. master_driver_args[0] = master_driver_name;
  258. for (i = 1; i < driver_nargs; i++) {
  259. master_driver_args[i] = argv[optind++];
  260. }
  261. if (jack_parse_driver_params(driver_desc, driver_nargs, master_driver_args, &master_driver_params)) {
  262. goto error;
  263. }
  264. #ifndef WIN32
  265. if (server_name == NULL)
  266. server_name = (char*)JackTools::DefaultServerName();
  267. #endif
  268. rc = jack_register_server(server_name, false);
  269. switch (rc) {
  270. case EEXIST:
  271. jack_error("`%s' server already active", server_name);
  272. goto error;
  273. case ENOSPC:
  274. jack_error("too many servers already active");
  275. goto error;
  276. case ENOMEM:
  277. jack_error("no access to shm registry");
  278. goto error;
  279. default:
  280. jack_info("server `%s' registered", server_name);
  281. }
  282. /* clean up shared memory and files from any previous instance of this server name */
  283. jack_cleanup_shm();
  284. JackTools::CleanupFiles(server_name);
  285. if (!realtime && client_timeout == 0)
  286. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  287. for (i = 0; i < argc; i++) {
  288. free(argv[i]);
  289. }
  290. int res = Start(server_name, driver_desc, master_driver_params, sync, temporary, client_timeout, realtime, realtime_priority, port_max, verbose_aux, clock_source, JACK_DEFAULT_SELF_CONNECT_MODE);
  291. if (res < 0) {
  292. jack_error("Cannot start server... exit");
  293. Delete();
  294. jack_cleanup_shm();
  295. JackTools::CleanupFiles(server_name);
  296. jack_unregister_server(server_name);
  297. goto error;
  298. }
  299. // Slave drivers
  300. std::map<std::string, JackDriverInfo*>::iterator it1;
  301. for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
  302. const char* name = ((*it1).first).c_str();
  303. driver_desc = jack_find_driver_descriptor(drivers, name);
  304. if (!driver_desc) {
  305. jack_error("jackdmp: unknown slave driver '%s'", name);
  306. } else {
  307. (*it1).second = fInstance->AddSlave(driver_desc, NULL);
  308. }
  309. }
  310. // Loopback driver
  311. if (loopback > 0) {
  312. driver_desc = jack_find_driver_descriptor(drivers, "loopback");
  313. if (!driver_desc) {
  314. jack_error("jackdmp: unknown driver '%s'", "loopback");
  315. } else {
  316. fSlavesList["loopback"] = fInstance->AddSlave(driver_desc, NULL);
  317. }
  318. }
  319. // Internal clients
  320. std::map<std::string, int>::iterator it2;
  321. for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
  322. int status, refnum;
  323. const char* name = ((*it2).first).c_str();
  324. fInstance->InternalClientLoad2(name, name, NULL, JackNullOption, &refnum, -1, &status);
  325. (*it2).second = refnum;
  326. }
  327. }
  328. if (master_driver_params)
  329. jack_free_driver_params(master_driver_params);
  330. return true;
  331. error:
  332. jack_log("JackServerGlobals Init error");
  333. if (master_driver_params)
  334. jack_free_driver_params(master_driver_params);
  335. Destroy();
  336. return false;
  337. }
  338. void JackServerGlobals::Destroy()
  339. {
  340. if (--fUserCount == 0) {
  341. jack_log("JackServerGlobals Destroy");
  342. Stop();
  343. Delete();
  344. jack_cleanup_shm();
  345. JackTools::CleanupFiles(server_name);
  346. jack_unregister_server(server_name);
  347. }
  348. }
  349. } // end of namespace