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.

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