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.

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