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.

422 lines
14KB

  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. std::map<std::string, JackDriverInfo*> JackServerGlobals::fSlavesList;
  27. std::map<std::string, int> JackServerGlobals::fInternalsList;
  28. bool (* JackServerGlobals::on_device_acquire)(const char * device_name) = NULL;
  29. void (* JackServerGlobals::on_device_release)(const char * device_name) = NULL;
  30. void (* JackServerGlobals::on_device_reservation_loop)(void) = 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. char 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. unsigned int port_max = 128;
  89. int temporary = 0;
  90. int opt = 0;
  91. int option_index = 0;
  92. char *master_driver_name = NULL;
  93. char **master_driver_args = NULL;
  94. JSList* master_driver_params = NULL;
  95. jack_driver_desc_t* driver_desc;
  96. jack_timer_type_t clock_source = JACK_TIMER_SYSTEM_CLOCK;
  97. int driver_nargs = 1;
  98. JSList* drivers = NULL;
  99. int loopback = 0;
  100. int sync = 0;
  101. int rc, i;
  102. int res;
  103. int replace_registry = 0;
  104. FILE* fp = 0;
  105. char filename[255];
  106. char buffer[255];
  107. int argc = 0;
  108. char* argv[32];
  109. // First user starts the server
  110. if (fUserCount++ == 0) {
  111. jack_log("JackServerGlobals Init");
  112. const char *options = "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
  113. #ifdef __linux__
  114. "c:"
  115. #endif
  116. ;
  117. struct option long_options[] = {
  118. #ifdef __linux__
  119. { "clock-source", 1, 0, 'c' },
  120. #endif
  121. { "loopback-driver", 1, 0, 'L' },
  122. { "audio-driver", 1, 0, 'd' },
  123. { "midi-driver", 1, 0, 'X' },
  124. { "internal-client", 1, 0, 'I' },
  125. { "verbose", 0, 0, 'v' },
  126. { "help", 0, 0, 'h' },
  127. { "port-max", 1, 0, 'p' },
  128. { "no-mlock", 0, 0, 'm' },
  129. { "name", 1, 0, 'n' },
  130. { "unlock", 0, 0, 'u' },
  131. { "realtime", 0, 0, 'R' },
  132. { "no-realtime", 0, 0, 'r' },
  133. { "replace-registry", 0, &replace_registry, 0 },
  134. { "loopback", 0, 0, 'L' },
  135. { "realtime-priority", 1, 0, 'P' },
  136. { "timeout", 1, 0, 't' },
  137. { "temporary", 0, 0, 'T' },
  138. { "version", 0, 0, 'V' },
  139. { "silent", 0, 0, 's' },
  140. { "sync", 0, 0, 'S' },
  141. { 0, 0, 0, 0 }
  142. };
  143. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  144. fp = fopen(filename, "r");
  145. if (!fp) {
  146. fp = fopen("/etc/jackdrc", "r");
  147. }
  148. // if still not found, check old config name for backwards compatibility
  149. if (!fp) {
  150. fp = fopen("/etc/jackd.conf", "r");
  151. }
  152. argc = 0;
  153. if (fp) {
  154. res = fscanf(fp, "%s", buffer);
  155. while (res != 0 && res != EOF) {
  156. argv[argc] = (char*)malloc(64);
  157. strcpy(argv[argc], buffer);
  158. res = fscanf(fp, "%s", buffer);
  159. argc++;
  160. }
  161. fclose(fp);
  162. }
  163. /*
  164. For testing
  165. int argc = 15;
  166. char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
  167. */
  168. opterr = 0;
  169. optind = 1; // Important : to reset argv parsing
  170. while (!master_driver_name &&
  171. (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
  172. switch (opt) {
  173. case 'c':
  174. if (tolower (optarg[0]) == 'h') {
  175. clock_source = JACK_TIMER_HPET;
  176. } else if (tolower (optarg[0]) == 'c') {
  177. /* For backwards compatibility with scripts, allow
  178. * the user to request the cycle clock on the
  179. * command line, but use the system clock instead
  180. */
  181. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  182. } else if (tolower (optarg[0]) == 's') {
  183. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  184. } else {
  185. jack_error("unknown option character %c", optopt);
  186. }
  187. break;
  188. case 'd':
  189. master_driver_name = optarg;
  190. break;
  191. case 'L':
  192. loopback = atoi(optarg);
  193. break;
  194. case 'X':
  195. fSlavesList[optarg] = NULL;
  196. break;
  197. case 'I':
  198. fInternalsList[optarg] = -1;
  199. break;
  200. case 'p':
  201. port_max = (unsigned int)atol(optarg);
  202. break;
  203. case 'm':
  204. break;
  205. case 'u':
  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. }
  268. #endif
  269. rc = jack_register_server(server_name, false);
  270. switch (rc) {
  271. case EEXIST:
  272. jack_error("`%s' server already active", server_name);
  273. goto error;
  274. case ENOSPC:
  275. jack_error("too many servers already active");
  276. goto error;
  277. case ENOMEM:
  278. jack_error("no access to shm registry");
  279. goto error;
  280. default:
  281. jack_info("server `%s' registered", server_name);
  282. }
  283. /* clean up shared memory and files from any previous instance of this server name */
  284. jack_cleanup_shm();
  285. JackTools::CleanupFiles(server_name);
  286. if (!realtime && client_timeout == 0) {
  287. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  288. }
  289. for (i = 0; i < argc; i++) {
  290. free(argv[i]);
  291. }
  292. 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);
  293. if (res < 0) {
  294. jack_error("Cannot start server... exit");
  295. Delete();
  296. jack_cleanup_shm();
  297. JackTools::CleanupFiles(server_name);
  298. jack_unregister_server(server_name);
  299. goto error;
  300. }
  301. // Slave drivers
  302. std::map<std::string, JackDriverInfo*>::iterator it1;
  303. for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
  304. const char* name = ((*it1).first).c_str();
  305. driver_desc = jack_find_driver_descriptor(drivers, name);
  306. if (!driver_desc) {
  307. jack_error("jackdmp: unknown slave driver '%s'", name);
  308. } else {
  309. (*it1).second = fInstance->AddSlave(driver_desc, NULL);
  310. }
  311. }
  312. // Loopback driver
  313. if (loopback > 0) {
  314. driver_desc = jack_find_driver_descriptor(drivers, "loopback");
  315. if (!driver_desc) {
  316. jack_error("jackdmp: unknown driver '%s'", "loopback");
  317. } else {
  318. fSlavesList["loopback"] = fInstance->AddSlave(driver_desc, NULL);
  319. }
  320. }
  321. // Internal clients
  322. std::map<std::string, int>::iterator it2;
  323. for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
  324. int status, refnum;
  325. const char* name = ((*it2).first).c_str();
  326. fInstance->InternalClientLoad2(name, name, NULL, JackNullOption, &refnum, -1, &status);
  327. (*it2).second = refnum;
  328. }
  329. }
  330. if (master_driver_params) {
  331. jack_free_driver_params(master_driver_params);
  332. }
  333. return true;
  334. error:
  335. jack_log("JackServerGlobals Init error");
  336. if (master_driver_params) {
  337. jack_free_driver_params(master_driver_params);
  338. }
  339. Destroy();
  340. return false;
  341. }
  342. void JackServerGlobals::Destroy()
  343. {
  344. if (--fUserCount == 0) {
  345. jack_log("JackServerGlobals Destroy");
  346. Stop();
  347. Delete();
  348. jack_cleanup_shm();
  349. JackTools::CleanupFiles(server_name);
  350. jack_unregister_server(server_name);
  351. }
  352. }
  353. } // end of namespace