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.

434 lines
13KB

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