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.

420 lines
12KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <iostream>
  17. #include <assert.h>
  18. #include <signal.h>
  19. #include <pwd.h>
  20. #include <sys/types.h>
  21. #include <dirent.h>
  22. #include <getopt.h>
  23. #include "JackServer.h"
  24. #include "JackConstants.h"
  25. #include "driver_interface.h"
  26. #include "JackDriverLoader.h"
  27. #include "jslist.h"
  28. #include "JackError.h"
  29. #include "JackTools.h"
  30. #include "shm.h"
  31. #include "jack.h"
  32. #ifdef __APPLE_
  33. #include <CoreFoundation/CFNotificationCenter.h>
  34. #endif
  35. using namespace Jack;
  36. static JackServer* fServer;
  37. static char* server_name = NULL;
  38. static int realtime_priority = 10;
  39. static int do_mlock = 1;
  40. static int realtime = 0;
  41. static int loopback = 0;
  42. static int temporary = 0;
  43. static int client_timeout = 0; /* msecs; if zero, use period size. */
  44. static int do_unlock = 0;
  45. static JSList* drivers = NULL;
  46. static sigset_t signals;
  47. static void silent_jack_error_callback(const char *desc)
  48. {}
  49. static void copyright(FILE* file)
  50. {
  51. fprintf(file, "jackdmp " VERSION "\n"
  52. "Copyright 2001-2005 Paul Davis and others.\n"
  53. "Copyright 2004-2008 Grame.\n"
  54. "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
  55. "This is free software, and you are welcome to redistribute it\n"
  56. "under certain conditions; see the file COPYING for details\n");
  57. }
  58. static void usage(FILE* file)
  59. {
  60. copyright(file);
  61. fprintf(file, "\n"
  62. "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  63. " [ --name OR -n server-name ]\n"
  64. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  65. " [ --loopback OR -L loopback-port-number ]\n"
  66. " [ --verbose OR -v ]\n"
  67. " [ --replace-registry OR -r ]\n"
  68. " [ --silent OR -s ]\n"
  69. " [ --sync OR -S ]\n"
  70. " [ --temporary OR -T ]\n"
  71. " [ --version OR -V ]\n"
  72. " -d driver [ ... driver args ... ]\n"
  73. " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
  74. " jackdmp -d driver --help\n"
  75. " to display options for each driver\n\n");
  76. }
  77. static void DoNothingHandler(int sig)
  78. {
  79. /* this is used by the child (active) process, but it never
  80. gets called unless we are already shutting down after
  81. another signal.
  82. */
  83. char buf[64];
  84. snprintf(buf, sizeof(buf), "received signal %d during shutdown(ignored)\n", sig);
  85. write(1, buf, strlen(buf));
  86. }
  87. static int JackStart(const char* server_name, jack_driver_desc_t* driver_desc, JSList* driver_params, int sync, int temporary, int time_out_ms, int rt, int priority, int loopback, int verbose)
  88. {
  89. jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync, time_out_ms, rt, priority, verbose);
  90. fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose, server_name);
  91. int res = fServer->Open(driver_desc, driver_params);
  92. return (res < 0) ? res : fServer->Start();
  93. }
  94. static int JackStop()
  95. {
  96. fServer->Stop();
  97. fServer->Close();
  98. jack_log("Jackdmp: server close");
  99. delete fServer;
  100. jack_log("Jackdmp: delete server");
  101. return 0;
  102. }
  103. static int JackDelete()
  104. {
  105. delete fServer;
  106. jack_log("Jackdmp: delete server");
  107. return 0;
  108. }
  109. static void FilterSIGPIPE()
  110. {
  111. sigset_t set;
  112. sigemptyset(&set);
  113. sigaddset(&set, SIGPIPE);
  114. //sigprocmask(SIG_BLOCK, &set, 0);
  115. pthread_sigmask(SIG_BLOCK, &set, 0);
  116. }
  117. int main(int argc, char* argv[])
  118. {
  119. int sig;
  120. sigset_t allsignals;
  121. struct sigaction action;
  122. int waiting;
  123. jack_driver_desc_t* driver_desc;
  124. const char *options = "-ad:P:uvrshVRL:STFl:t:mn:";
  125. struct option long_options[] = {
  126. { "driver", 1, 0, 'd'},
  127. { "verbose", 0, 0, 'v' },
  128. { "help", 0, 0, 'h' },
  129. { "port-max", 1, 0, 'p' },
  130. { "no-mlock", 0, 0, 'm' },
  131. { "name", 0, 0, 'n' },
  132. { "unlock", 0, 0, 'u' },
  133. { "realtime", 0, 0, 'R' },
  134. { "replace-registry", 0, 0, 'r' },
  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. int opt = 0;
  145. int option_index = 0;
  146. int seen_driver = 0;
  147. char *driver_name = NULL;
  148. char **driver_args = NULL;
  149. JSList* driver_params;
  150. int driver_nargs = 1;
  151. int show_version = 0;
  152. int replace_registry = 0;
  153. int sync = 0;
  154. int rc, i;
  155. opterr = 0;
  156. while (!seen_driver &&
  157. (opt = getopt_long(argc, argv, options,
  158. long_options, &option_index)) != EOF) {
  159. switch (opt) {
  160. case 'd':
  161. seen_driver = 1;
  162. driver_name = optarg;
  163. break;
  164. case 'v':
  165. jack_verbose = 1;
  166. break;
  167. case 's':
  168. jack_set_error_function(silent_jack_error_callback);
  169. break;
  170. case 'S':
  171. sync = 1;
  172. break;
  173. case 'n':
  174. server_name = optarg;
  175. break;
  176. case 'm':
  177. do_mlock = 0;
  178. break;
  179. case 'P':
  180. realtime_priority = atoi(optarg);
  181. break;
  182. case 'r':
  183. replace_registry = 1;
  184. break;
  185. case 'R':
  186. realtime = 1;
  187. break;
  188. case 'L':
  189. loopback = atoi(optarg);
  190. break;
  191. case 'T':
  192. temporary = 1;
  193. break;
  194. case 't':
  195. client_timeout = atoi(optarg);
  196. break;
  197. case 'u':
  198. do_unlock = 1;
  199. break;
  200. case 'V':
  201. show_version = 1;
  202. break;
  203. default:
  204. fprintf(stderr, "unknown option character %c\n",
  205. optopt);
  206. /*fallthru*/
  207. case 'h':
  208. usage(stdout);
  209. return -1;
  210. }
  211. }
  212. if (show_version) {
  213. printf("jackdmp version " VERSION
  214. "\n");
  215. return -1;
  216. }
  217. if (!seen_driver) {
  218. usage(stderr);
  219. exit(1);
  220. }
  221. drivers = jack_drivers_load(drivers);
  222. if (!drivers) {
  223. fprintf(stderr, "jackdmp: no drivers found; exiting\n");
  224. exit(1);
  225. }
  226. driver_desc = jack_find_driver_descriptor(drivers, driver_name);
  227. if (!driver_desc) {
  228. fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  229. exit(1);
  230. }
  231. if (optind < argc) {
  232. driver_nargs = 1 + argc - optind;
  233. } else {
  234. driver_nargs = 1;
  235. }
  236. if (driver_nargs == 0) {
  237. fprintf(stderr, "No driver specified ... hmm. JACK won't do"
  238. " anything when run like this.\n");
  239. return -1;
  240. }
  241. driver_args = (char **) malloc(sizeof(char *) * driver_nargs);
  242. driver_args[0] = driver_name;
  243. for (i = 1; i < driver_nargs; i++) {
  244. driver_args[i] = argv[optind++];
  245. }
  246. if (jack_parse_driver_params(driver_desc, driver_nargs,
  247. driver_args, &driver_params)) {
  248. exit(0);
  249. }
  250. if (server_name == NULL)
  251. server_name = (char*)JackTools::DefaultServerName();
  252. copyright(stdout);
  253. rc = jack_register_server(server_name, replace_registry);
  254. switch (rc) {
  255. case EEXIST:
  256. fprintf(stderr, "`%s' server already active\n", server_name);
  257. exit(1);
  258. case ENOSPC:
  259. fprintf(stderr, "too many servers already active\n");
  260. exit(2);
  261. case ENOMEM:
  262. fprintf(stderr, "no access to shm registry\n");
  263. exit(3);
  264. default:
  265. if (jack_verbose)
  266. fprintf(stderr, "server `%s' registered\n", server_name);
  267. }
  268. /* clean up shared memory and files from any previous
  269. * instance of this server name */
  270. jack_cleanup_shm();
  271. JackTools::CleanupFiles(server_name);
  272. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  273. sigemptyset(&signals);
  274. sigaddset(&signals, SIGHUP);
  275. sigaddset(&signals, SIGINT);
  276. sigaddset(&signals, SIGQUIT);
  277. sigaddset(&signals, SIGPIPE);
  278. sigaddset(&signals, SIGTERM);
  279. sigaddset(&signals, SIGUSR1);
  280. sigaddset(&signals, SIGUSR2);
  281. // all child threads will inherit this mask unless they
  282. // explicitly reset it
  283. FilterSIGPIPE();
  284. pthread_sigmask(SIG_BLOCK, &signals, 0);
  285. if (!realtime && client_timeout == 0)
  286. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  287. int res = JackStart(server_name, driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, jack_verbose);
  288. if (res < 0) {
  289. jack_error("Cannot start server... exit");
  290. JackDelete();
  291. return 0;
  292. }
  293. #ifdef __APPLE__
  294. CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
  295. // Send notification to be used in the JackRouter plugin
  296. CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
  297. CFSTR("com.grame.jackserver.start"),
  298. ref,
  299. NULL,
  300. kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
  301. CFRelease(ref);
  302. #endif
  303. // install a do-nothing handler because otherwise pthreads
  304. // behaviour is undefined when we enter sigwait.
  305. sigfillset(&allsignals);
  306. action.sa_handler = DoNothingHandler;
  307. action.sa_mask = allsignals;
  308. action.sa_flags = SA_RESTART | SA_RESETHAND;
  309. for (i = 1; i < NSIG; i++) {
  310. if (sigismember(&signals, i)) {
  311. sigaction(i, &action, 0);
  312. }
  313. }
  314. waiting = TRUE;
  315. while (waiting) {
  316. sigwait(&signals, &sig);
  317. fprintf(stderr, "jack main caught signal %d\n", sig);
  318. switch (sig) {
  319. case SIGUSR1:
  320. //jack_dump_configuration(engine, 1);
  321. break;
  322. case SIGUSR2:
  323. // driver exit
  324. waiting = FALSE;
  325. break;
  326. default:
  327. waiting = FALSE;
  328. break;
  329. }
  330. }
  331. if (sig != SIGSEGV) {
  332. // unblock signals so we can see them during shutdown.
  333. // this will help prod developers not to lose sight of
  334. // bugs that cause segfaults etc. during shutdown.
  335. sigprocmask(SIG_UNBLOCK, &signals, 0);
  336. }
  337. JackStop();
  338. jack_cleanup_shm();
  339. JackTools::CleanupFiles(server_name);
  340. jack_unregister_server(server_name);
  341. #ifdef __APPLE__
  342. CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
  343. // Send notification to be used in the JackRouter plugin
  344. CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
  345. CFSTR("com.grame.jackserver.stop"),
  346. ref1,
  347. NULL,
  348. kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
  349. CFRelease(ref1);
  350. #endif
  351. return 1;
  352. }