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.

336 lines
10KB

  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 "JackTools.h"
  17. #include "shm.h"
  18. #include <getopt.h>
  19. #include <errno.h>
  20. static char* server_name = NULL;
  21. namespace Jack
  22. {
  23. JackServer* JackServerGlobals::fInstance;
  24. unsigned int JackServerGlobals::fUserCount;
  25. int JackServerGlobals::Start(const char* server_name,
  26. jack_driver_desc_t* driver_desc,
  27. JSList* driver_params,
  28. int sync,
  29. int temporary,
  30. int time_out_ms,
  31. int rt,
  32. int priority,
  33. int loopback,
  34. int verbose,
  35. jack_timer_type_t clock)
  36. {
  37. jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync, time_out_ms, rt, priority, verbose);
  38. new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose, clock, server_name); // Will setup fInstance and fUserCount globals
  39. int res = fInstance->Open(driver_desc, driver_params);
  40. return (res < 0) ? res : fInstance->Start();
  41. }
  42. void JackServerGlobals::Stop()
  43. {
  44. jack_log("Jackdmp: server close");
  45. fInstance->Stop();
  46. fInstance->Close();
  47. }
  48. void JackServerGlobals::Delete()
  49. {
  50. jack_log("Jackdmp: delete server");
  51. delete fInstance;
  52. fInstance = NULL;
  53. }
  54. bool JackServerGlobals::Init()
  55. {
  56. int realtime = 0;
  57. int client_timeout = 0; /* msecs; if zero, use period size. */
  58. int realtime_priority = 10;
  59. int verbose_aux = 0;
  60. int do_mlock = 1;
  61. unsigned int port_max = 128;
  62. int loopback = 0;
  63. int do_unlock = 0;
  64. int temporary = 0;
  65. int opt = 0;
  66. int option_index = 0;
  67. int seen_driver = 0;
  68. char *driver_name = NULL;
  69. char **driver_args = NULL;
  70. JSList* driver_params = NULL;
  71. int driver_nargs = 1;
  72. JSList* drivers = NULL;
  73. int show_version = 0;
  74. int sync = 0;
  75. int rc, i;
  76. int ret;
  77. FILE* fp = 0;
  78. char filename[255];
  79. char buffer[255];
  80. int argc = 0;
  81. char* argv[32];
  82. jack_timer_type_t clock_source = JACK_TIMER_SYSTEM_CLOCK;
  83. // First user starts the server
  84. if (fUserCount++ == 0) {
  85. jack_log("JackServerGlobals Init");
  86. jack_driver_desc_t* driver_desc;
  87. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:c:";
  88. static struct option long_options[] = {
  89. { "clock-source", 1, 0, 'c' },
  90. { "driver", 1, 0, 'd' },
  91. { "verbose", 0, 0, 'v' },
  92. { "help", 0, 0, 'h' },
  93. { "port-max", 1, 0, 'p' },
  94. { "no-mlock", 0, 0, 'm' },
  95. { "name", 0, 0, 'n' },
  96. { "unlock", 0, 0, 'u' },
  97. { "realtime", 0, 0, 'R' },
  98. { "loopback", 0, 0, 'L' },
  99. { "realtime-priority", 1, 0, 'P' },
  100. { "timeout", 1, 0, 't' },
  101. { "temporary", 0, 0, 'T' },
  102. { "version", 0, 0, 'V' },
  103. { "silent", 0, 0, 's' },
  104. { "sync", 0, 0, 'S' },
  105. { 0, 0, 0, 0 }
  106. };
  107. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  108. fp = fopen(filename, "r");
  109. if (!fp) {
  110. fp = fopen("/etc/jackdrc", "r");
  111. }
  112. // if still not found, check old config name for backwards compatability
  113. if (!fp) {
  114. fp = fopen("/etc/jackd.conf", "r");
  115. }
  116. argc = 0;
  117. if (fp) {
  118. ret = fscanf(fp, "%s", buffer);
  119. while (ret != 0 && ret != EOF) {
  120. argv[argc] = (char*)malloc(64);
  121. strcpy(argv[argc], buffer);
  122. ret = fscanf(fp, "%s", buffer);
  123. argc++;
  124. }
  125. fclose(fp);
  126. }
  127. /*
  128. For testing
  129. int argc = 15;
  130. char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
  131. */
  132. opterr = 0;
  133. optind = 1; // Important : to reset argv parsing
  134. while (!seen_driver &&
  135. (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
  136. switch (opt) {
  137. case 'c':
  138. if (tolower (optarg[0]) == 'h') {
  139. clock_source = JACK_TIMER_HPET;
  140. } else if (tolower (optarg[0]) == 'c') {
  141. clock_source = JACK_TIMER_CYCLE_COUNTER;
  142. } else if (tolower (optarg[0]) == 's') {
  143. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  144. } else {
  145. jack_error("unknown option character %c", optopt);
  146. }
  147. break;
  148. case 'd':
  149. seen_driver = 1;
  150. driver_name = optarg;
  151. break;
  152. case 'v':
  153. verbose_aux = 1;
  154. break;
  155. case 'S':
  156. sync = 1;
  157. break;
  158. case 'n':
  159. server_name = optarg;
  160. break;
  161. case 'm':
  162. do_mlock = 0;
  163. break;
  164. case 'p':
  165. port_max = (unsigned int)atol(optarg);
  166. break;
  167. case 'P':
  168. realtime_priority = atoi(optarg);
  169. break;
  170. case 'R':
  171. realtime = 1;
  172. break;
  173. case 'L':
  174. loopback = atoi(optarg);
  175. break;
  176. case 'T':
  177. temporary = 1;
  178. break;
  179. case 't':
  180. client_timeout = atoi(optarg);
  181. break;
  182. case 'u':
  183. do_unlock = 1;
  184. break;
  185. case 'V':
  186. show_version = 1;
  187. break;
  188. default:
  189. jack_error("unknown option character %c", optopt);
  190. break;
  191. }
  192. }
  193. drivers = jack_drivers_load(drivers);
  194. if (!drivers) {
  195. jack_error("jackdmp: no drivers found; exiting");
  196. goto error;
  197. }
  198. driver_desc = jack_find_driver_descriptor(drivers, driver_name);
  199. if (!driver_desc) {
  200. jack_error("jackdmp: unknown driver '%s'", driver_name);
  201. goto error;
  202. }
  203. if (optind < argc) {
  204. driver_nargs = 1 + argc - optind;
  205. } else {
  206. driver_nargs = 1;
  207. }
  208. if (driver_nargs == 0) {
  209. jack_error("No driver specified ... hmm. JACK won't do"
  210. " anything when run like this.");
  211. goto error;
  212. }
  213. driver_args = (char**)malloc(sizeof(char*) * driver_nargs);
  214. driver_args[0] = driver_name;
  215. for (i = 1; i < driver_nargs; i++) {
  216. driver_args[i] = argv[optind++];
  217. }
  218. if (jack_parse_driver_params(driver_desc, driver_nargs, driver_args, &driver_params)) {
  219. goto error;
  220. }
  221. #ifndef WIN32
  222. if (server_name == NULL)
  223. server_name = (char*)JackTools::DefaultServerName();
  224. #endif
  225. rc = jack_register_server(server_name, false);
  226. switch (rc) {
  227. case EEXIST:
  228. jack_error("`%s' server already active", server_name);
  229. goto error;
  230. case ENOSPC:
  231. jack_error("too many servers already active");
  232. goto error;
  233. case ENOMEM:
  234. jack_error("no access to shm registry");
  235. goto error;
  236. default:
  237. jack_info("server `%s' registered", server_name);
  238. }
  239. /* clean up shared memory and files from any previous instance of this server name */
  240. jack_cleanup_shm();
  241. JackTools::CleanupFiles(server_name);
  242. if (!realtime && client_timeout == 0)
  243. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  244. for (i = 0; i < argc; i++) {
  245. free(argv[i]);
  246. }
  247. int res = Start(server_name, driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, verbose_aux, clock_source);
  248. if (res < 0) {
  249. jack_error("Cannot start server... exit");
  250. Delete();
  251. jack_cleanup_shm();
  252. JackTools::CleanupFiles(server_name);
  253. jack_unregister_server(server_name);
  254. goto error;
  255. }
  256. }
  257. if (driver_params)
  258. jack_free_driver_params(driver_params);
  259. return true;
  260. error:
  261. if (driver_params)
  262. jack_free_driver_params(driver_params);
  263. fUserCount--;
  264. return false;
  265. }
  266. void JackServerGlobals::Destroy()
  267. {
  268. if (--fUserCount == 0) {
  269. jack_log("JackServerGlobals Destroy");
  270. Stop();
  271. Delete();
  272. jack_cleanup_shm();
  273. JackTools::CleanupFiles(server_name);
  274. jack_unregister_server(server_name);
  275. }
  276. }
  277. } // end of namespace