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.

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