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.

332 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 port_max,
  36. int verbose,
  37. jack_timer_type_t clock)
  38. {
  39. jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync, time_out_ms, rt, priority, verbose);
  40. new JackServer(sync, temporary, time_out_ms, rt, priority, port_max, verbose, clock, server_name); // Will setup fInstance and fUserCount globals
  41. int res = fInstance->Open(driver_desc, driver_params);
  42. return (res < 0) ? res : fInstance->Start();
  43. }
  44. void JackServerGlobals::Stop()
  45. {
  46. jack_log("Jackdmp: server close");
  47. fInstance->Stop();
  48. fInstance->Close();
  49. }
  50. void JackServerGlobals::Delete()
  51. {
  52. jack_log("Jackdmp: delete server");
  53. delete fInstance;
  54. fInstance = NULL;
  55. }
  56. bool JackServerGlobals::Init()
  57. {
  58. int realtime = 0;
  59. int client_timeout = 0; /* msecs; if zero, use period size. */
  60. int realtime_priority = 10;
  61. int verbose_aux = 0;
  62. int do_mlock = 1;
  63. unsigned int port_max = 128;
  64. int do_unlock = 0;
  65. int temporary = 0;
  66. int opt = 0;
  67. int option_index = 0;
  68. int seen_driver = 0;
  69. char *driver_name = NULL;
  70. char **driver_args = NULL;
  71. JSList* driver_params = NULL;
  72. int driver_nargs = 1;
  73. JSList* drivers = NULL;
  74. int show_version = 0;
  75. int sync = 0;
  76. int rc, i;
  77. int ret;
  78. FILE* fp = 0;
  79. char filename[255];
  80. char buffer[255];
  81. int argc = 0;
  82. char* argv[32];
  83. jack_timer_type_t clock_source = JACK_TIMER_SYSTEM_CLOCK;
  84. // First user starts the server
  85. if (fUserCount++ == 0) {
  86. jack_log("JackServerGlobals Init");
  87. jack_driver_desc_t* driver_desc;
  88. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:c:";
  89. static struct option long_options[] = {
  90. { "clock-source", 1, 0, 'c' },
  91. { "driver", 1, 0, 'd' },
  92. { "verbose", 0, 0, 'v' },
  93. { "help", 0, 0, 'h' },
  94. { "port-max", 1, 0, 'p' },
  95. { "no-mlock", 0, 0, 'm' },
  96. { "name", 0, 0, 'n' },
  97. { "unlock", 0, 0, 'u' },
  98. { "realtime", 0, 0, 'R' },
  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 'T':
  174. temporary = 1;
  175. break;
  176. case 't':
  177. client_timeout = atoi(optarg);
  178. break;
  179. case 'u':
  180. do_unlock = 1;
  181. break;
  182. case 'V':
  183. show_version = 1;
  184. break;
  185. default:
  186. jack_error("unknown option character %c", optopt);
  187. break;
  188. }
  189. }
  190. drivers = jack_drivers_load(drivers);
  191. if (!drivers) {
  192. jack_error("jackdmp: no drivers found; exiting");
  193. goto error;
  194. }
  195. driver_desc = jack_find_driver_descriptor(drivers, driver_name);
  196. if (!driver_desc) {
  197. jack_error("jackdmp: unknown driver '%s'", driver_name);
  198. goto error;
  199. }
  200. if (optind < argc) {
  201. driver_nargs = 1 + argc - optind;
  202. } else {
  203. driver_nargs = 1;
  204. }
  205. if (driver_nargs == 0) {
  206. jack_error("No driver specified ... hmm. JACK won't do"
  207. " anything when run like this.");
  208. goto error;
  209. }
  210. driver_args = (char**)malloc(sizeof(char*) * driver_nargs);
  211. driver_args[0] = driver_name;
  212. for (i = 1; i < driver_nargs; i++) {
  213. driver_args[i] = argv[optind++];
  214. }
  215. if (jack_parse_driver_params(driver_desc, driver_nargs, driver_args, &driver_params)) {
  216. goto error;
  217. }
  218. #ifndef WIN32
  219. if (server_name == NULL)
  220. server_name = (char*)JackTools::DefaultServerName();
  221. #endif
  222. rc = jack_register_server(server_name, false);
  223. switch (rc) {
  224. case EEXIST:
  225. jack_error("`%s' server already active", server_name);
  226. goto error;
  227. case ENOSPC:
  228. jack_error("too many servers already active");
  229. goto error;
  230. case ENOMEM:
  231. jack_error("no access to shm registry");
  232. goto error;
  233. default:
  234. jack_info("server `%s' registered", server_name);
  235. }
  236. /* clean up shared memory and files from any previous instance of this server name */
  237. jack_cleanup_shm();
  238. JackTools::CleanupFiles(server_name);
  239. if (!realtime && client_timeout == 0)
  240. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  241. for (i = 0; i < argc; i++) {
  242. free(argv[i]);
  243. }
  244. int res = Start(server_name, driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, port_max, verbose_aux, clock_source);
  245. if (res < 0) {
  246. jack_error("Cannot start server... exit");
  247. Delete();
  248. jack_cleanup_shm();
  249. JackTools::CleanupFiles(server_name);
  250. jack_unregister_server(server_name);
  251. goto error;
  252. }
  253. }
  254. if (driver_params)
  255. jack_free_driver_params(driver_params);
  256. return true;
  257. error:
  258. if (driver_params)
  259. jack_free_driver_params(driver_params);
  260. fUserCount--;
  261. return false;
  262. }
  263. void JackServerGlobals::Destroy()
  264. {
  265. if (--fUserCount == 0) {
  266. jack_log("JackServerGlobals Destroy");
  267. Stop();
  268. Delete();
  269. jack_cleanup_shm();
  270. JackTools::CleanupFiles(server_name);
  271. jack_unregister_server(server_name);
  272. }
  273. }
  274. } // end of namespace