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.

570 lines
17KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 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 "shm.h"
  31. #include "jack.h"
  32. using namespace Jack;
  33. static JackServer* fServer;
  34. static char* server_name = NULL;
  35. static int realtime_priority = 10;
  36. static int do_mlock = 1;
  37. static unsigned int port_max = 128;
  38. static int realtime = 0;
  39. static int loopback = 0;
  40. static int temporary = 0;
  41. static int client_timeout = 0; /* msecs; if zero, use period size. */
  42. static int do_unlock = 0;
  43. static JSList* drivers = NULL;
  44. static sigset_t signals;
  45. #define DEFAULT_TMP_DIR "/tmp"
  46. char* jack_tmpdir = DEFAULT_TMP_DIR;
  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-2007 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. // " [ --no-mlock OR -m ]\n"
  65. // " [ --unlock OR -u ]\n"
  66. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  67. " [ --loopback OR -L loopback-port-number ]\n"
  68. // " [ --port-max OR -p maximum-number-of-ports]\n"
  69. " [ --verbose OR -v ]\n"
  70. " [ --silent OR -s ]\n"
  71. " [ --sync OR -S ]\n"
  72. " [ --temporary OR -T ]\n"
  73. " [ --version OR -V ]\n"
  74. " -d driver [ ... driver args ... ]\n"
  75. " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
  76. " jackdmp -d driver --help\n"
  77. " to display options for each driver\n\n");
  78. }
  79. static void DoNothingHandler(int sig)
  80. {
  81. /* this is used by the child (active) process, but it never
  82. gets called unless we are already shutting down after
  83. another signal.
  84. */
  85. char buf[64];
  86. snprintf(buf, sizeof(buf), "received signal %d during shutdown(ignored)\n", sig);
  87. write(1, buf, strlen(buf));
  88. }
  89. static int JackStart(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)
  90. {
  91. JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  92. fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose);
  93. int res = fServer->Open(driver_desc, driver_params);
  94. return (res < 0) ? res : fServer->Start();
  95. }
  96. static int JackStop()
  97. {
  98. fServer->Stop();
  99. fServer->Close();
  100. JackLog("Jackdmp: server close\n");
  101. delete fServer;
  102. JackLog("Jackdmp: delete server\n");
  103. return 0;
  104. }
  105. static int JackDelete()
  106. {
  107. delete fServer;
  108. JackLog("Jackdmp: delete server\n");
  109. return 0;
  110. }
  111. static void FilterSIGPIPE()
  112. {
  113. sigset_t set;
  114. sigemptyset(&set);
  115. sigaddset(&set, SIGPIPE);
  116. //sigprocmask(SIG_BLOCK, &set, 0);
  117. pthread_sigmask(SIG_BLOCK, &set, 0);
  118. }
  119. static char* jack_default_server_name(void)
  120. {
  121. char *server_name;
  122. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  123. server_name = "jackdmp_default";
  124. return server_name;
  125. }
  126. /* returns the name of the per-user subdirectory of jack_tmpdir */
  127. static char* jack_user_dir(void)
  128. {
  129. static char user_dir[PATH_MAX] = "";
  130. /* format the path name on the first call */
  131. if (user_dir[0] == '\0') {
  132. snprintf(user_dir, sizeof(user_dir), "%s/jack-%d",
  133. jack_tmpdir, getuid());
  134. }
  135. return user_dir;
  136. }
  137. /* returns the name of the per-server subdirectory of jack_user_dir() */
  138. static char* get_jack_server_dir(const char* toto)
  139. {
  140. static char server_dir[PATH_MAX] = "";
  141. // format the path name on the first call
  142. if (server_dir[0] == '\0') {
  143. snprintf(server_dir, sizeof(server_dir), "%s/%s",
  144. jack_user_dir(), server_name);
  145. }
  146. return server_dir;
  147. }
  148. static void
  149. jack_cleanup_files(const char *server_name)
  150. {
  151. DIR *dir;
  152. struct dirent *dirent;
  153. char *dir_name = get_jack_server_dir(server_name);
  154. /* On termination, we remove all files that jackd creates so
  155. * subsequent attempts to start jackd will not believe that an
  156. * instance is already running. If the server crashes or is
  157. * terminated with SIGKILL, this is not possible. So, cleanup
  158. * is also attempted when jackd starts.
  159. *
  160. * There are several tricky issues. First, the previous JACK
  161. * server may have run for a different user ID, so its files
  162. * may be inaccessible. This is handled by using a separate
  163. * JACK_TMP_DIR subdirectory for each user. Second, there may
  164. * be other servers running with different names. Each gets
  165. * its own subdirectory within the per-user directory. The
  166. * current process has already registered as `server_name', so
  167. * we know there is no other server actively using that name.
  168. */
  169. /* nothing to do if the server directory does not exist */
  170. if ((dir = opendir(dir_name)) == NULL) {
  171. return ;
  172. }
  173. /* unlink all the files in this directory, they are mine */
  174. while ((dirent = readdir(dir)) != NULL) {
  175. char fullpath[PATH_MAX];
  176. if ((strcmp(dirent->d_name, ".") == 0)
  177. || (strcmp(dirent->d_name, "..") == 0)) {
  178. continue;
  179. }
  180. snprintf(fullpath, sizeof(fullpath), "%s/%s",
  181. dir_name, dirent->d_name);
  182. if (unlink(fullpath)) {
  183. jack_error("cannot unlink `%s' (%s)", fullpath,
  184. strerror(errno));
  185. }
  186. }
  187. closedir(dir);
  188. /* now, delete the per-server subdirectory, itself */
  189. if (rmdir(dir_name)) {
  190. jack_error("cannot remove `%s' (%s)", dir_name,
  191. strerror(errno));
  192. }
  193. /* finally, delete the per-user subdirectory, if empty */
  194. if (rmdir(jack_user_dir())) {
  195. if (errno != ENOTEMPTY) {
  196. jack_error("cannot remove `%s' (%s)",
  197. jack_user_dir(), strerror(errno));
  198. }
  199. }
  200. }
  201. #ifdef FORK_SERVER
  202. int main(int argc, char* argv[])
  203. {
  204. int sig;
  205. sigset_t allsignals;
  206. struct sigaction action;
  207. int waiting;
  208. jack_driver_desc_t* driver_desc;
  209. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
  210. struct option long_options[] = {
  211. { "driver", 1, 0, 'd' },
  212. { "verbose", 0, 0, 'v' },
  213. { "help", 0, 0, 'h' },
  214. { "port-max", 1, 0, 'p' },
  215. { "no-mlock", 0, 0, 'm' },
  216. { "name", 0, 0, 'n' },
  217. { "unlock", 0, 0, 'u' },
  218. { "realtime", 0, 0, 'R' },
  219. { "loopback", 0, 0, 'L' },
  220. { "realtime-priority", 1, 0, 'P' },
  221. { "timeout", 1, 0, 't' },
  222. { "temporary", 0, 0, 'T' },
  223. { "version", 0, 0, 'V' },
  224. { "silent", 0, 0, 's' },
  225. { "sync", 0, 0, 'S' },
  226. { 0, 0, 0, 0 }
  227. };
  228. int opt = 0;
  229. int option_index = 0;
  230. int seen_driver = 0;
  231. char *driver_name = NULL;
  232. char **driver_args = NULL;
  233. JSList* driver_params;
  234. int driver_nargs = 1;
  235. int show_version = 0;
  236. int sync = 0;
  237. int rc, i;
  238. opterr = 0;
  239. while (!seen_driver &&
  240. (opt = getopt_long(argc, argv, options,
  241. long_options, &option_index)) != EOF) {
  242. switch (opt) {
  243. case 'd':
  244. seen_driver = 1;
  245. driver_name = optarg;
  246. break;
  247. case 'v':
  248. jack_verbose = 1;
  249. break;
  250. case 's':
  251. jack_set_error_function(silent_jack_error_callback);
  252. break;
  253. case 'S':
  254. sync = 1;
  255. break;
  256. case 'n':
  257. server_name = optarg;
  258. break;
  259. case 'm':
  260. do_mlock = 0;
  261. break;
  262. case 'p':
  263. port_max = (unsigned int)atol(optarg);
  264. break;
  265. case 'P':
  266. realtime_priority = atoi(optarg);
  267. break;
  268. case 'R':
  269. realtime = 1;
  270. break;
  271. case 'L':
  272. loopback = atoi(optarg);
  273. break;
  274. case 'T':
  275. temporary = 1;
  276. break;
  277. case 't':
  278. client_timeout = atoi(optarg);
  279. break;
  280. case 'u':
  281. do_unlock = 1;
  282. break;
  283. case 'V':
  284. show_version = 1;
  285. break;
  286. default:
  287. fprintf(stderr, "unknown option character %c\n",
  288. optopt);
  289. /*fallthru*/
  290. case 'h':
  291. usage(stdout);
  292. return -1;
  293. }
  294. }
  295. /*
  296. if (show_version) {
  297. printf ( "jackd version " VERSION
  298. " tmpdir " DEFAULT_TMP_DIR
  299. " protocol " PROTOCOL_VERSION
  300. "\n");
  301. return -1;
  302. }
  303. */
  304. if (!seen_driver) {
  305. usage(stderr);
  306. exit(1);
  307. }
  308. drivers = jack_drivers_load(drivers);
  309. if (!drivers) {
  310. fprintf(stderr, "jackdmp: no drivers found; exiting\n");
  311. exit(1);
  312. }
  313. driver_desc = jack_find_driver_descriptor(drivers, driver_name);
  314. if (!driver_desc) {
  315. fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  316. exit(1);
  317. }
  318. if (optind < argc) {
  319. driver_nargs = 1 + argc - optind;
  320. } else {
  321. driver_nargs = 1;
  322. }
  323. if (driver_nargs == 0) {
  324. fprintf(stderr, "No driver specified ... hmm. JACK won't do"
  325. " anything when run like this.\n");
  326. return -1;
  327. }
  328. driver_args = (char **) malloc(sizeof(char *) * driver_nargs);
  329. driver_args[0] = driver_name;
  330. for (i = 1; i < driver_nargs; i++) {
  331. driver_args[i] = argv[optind++];
  332. }
  333. if (jack_parse_driver_params(driver_desc, driver_nargs,
  334. driver_args, &driver_params)) {
  335. exit(0);
  336. }
  337. if (server_name == NULL)
  338. server_name = jack_default_server_name();
  339. copyright(stdout);
  340. rc = jack_register_server(server_name);
  341. switch (rc) {
  342. case EEXIST:
  343. fprintf(stderr, "`%s' server already active\n", server_name);
  344. exit(1);
  345. case ENOSPC:
  346. fprintf(stderr, "too many servers already active\n");
  347. exit(2);
  348. case ENOMEM:
  349. fprintf(stderr, "no access to shm registry\n");
  350. exit(3);
  351. default:
  352. if (jack_verbose)
  353. fprintf(stderr, "server `%s' registered\n",
  354. server_name);
  355. }
  356. /* clean up shared memory and files from any previous
  357. * instance of this server name */
  358. jack_cleanup_shm();
  359. jack_cleanup_files(server_name);
  360. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  361. sigemptyset(&signals);
  362. sigaddset(&signals, SIGHUP);
  363. sigaddset(&signals, SIGINT);
  364. sigaddset(&signals, SIGQUIT);
  365. sigaddset(&signals, SIGPIPE);
  366. sigaddset(&signals, SIGTERM);
  367. sigaddset(&signals, SIGUSR1);
  368. sigaddset(&signals, SIGUSR2);
  369. // all child threads will inherit this mask unless they
  370. // explicitly reset it
  371. FilterSIGPIPE();
  372. pthread_sigmask(SIG_BLOCK, &signals, 0);
  373. if (!realtime && client_timeout == 0)
  374. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  375. int res = JackStart(driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, jack_verbose);
  376. if (res < 0) {
  377. jack_error("Cannot start server... exit");
  378. JackDelete();
  379. return 0;
  380. }
  381. /*
  382. For testing purpose...
  383. InternalMetro* client1 = new InternalMetro(1200, 0.4, 20, 80, "metro1");
  384. InternalMetro* client2 = new InternalMetro(600, 0.4, 20, 150, "metro2");
  385. InternalMetro* client3 = new InternalMetro(1000, 0.4, 20, 110, "metro3");
  386. InternalMetro* client4 = new InternalMetro(1200, 0.4, 20, 80, "metro4");
  387. InternalMetro* client5 = new InternalMetro(1500, 0.4, 20, 60, "metro5");
  388. InternalMetro* client6 = new InternalMetro(1200, 0.4, 20, 84, "metro6");
  389. InternalMetro* client7 = new InternalMetro(600, 0.4, 20, 160, "metro7");
  390. InternalMetro* client8 = new InternalMetro(1000, 0.4, 20, 113, "metro8");
  391. InternalMetro* client9 = new InternalMetro(1200, 0.4, 20, 84, "metro9");
  392. InternalMetro* client10 = new InternalMetro(1500, 0.4, 20, 70, "metro10");
  393. */
  394. // install a do-nothing handler because otherwise pthreads
  395. // behaviour is undefined when we enter sigwait.
  396. sigfillset(&allsignals);
  397. action.sa_handler = DoNothingHandler;
  398. action.sa_mask = allsignals;
  399. action.sa_flags = SA_RESTART | SA_RESETHAND;
  400. for (i = 1; i < NSIG; i++) {
  401. if (sigismember(&signals, i)) {
  402. sigaction(i, &action, 0);
  403. }
  404. }
  405. waiting = TRUE;
  406. while (waiting) {
  407. sigwait(&signals, &sig);
  408. fprintf(stderr, "jack main caught signal %d\n", sig);
  409. switch (sig) {
  410. case SIGUSR1:
  411. //jack_dump_configuration(engine, 1);
  412. break;
  413. case SIGUSR2:
  414. // driver exit
  415. waiting = FALSE;
  416. break;
  417. default:
  418. waiting = FALSE;
  419. break;
  420. }
  421. }
  422. if (sig != SIGSEGV) {
  423. // unblock signals so we can see them during shutdown.
  424. // this will help prod developers not to lose sight of
  425. // bugs that cause segfaults etc. during shutdown.
  426. sigprocmask(SIG_UNBLOCK, &signals, 0);
  427. }
  428. JackStop();
  429. jack_cleanup_shm();
  430. jack_cleanup_files(server_name);
  431. jack_unregister_server(server_name);
  432. return 1;
  433. }
  434. #else
  435. int main(int argc, char* argv[])
  436. {
  437. char c;
  438. long sample_sate = lopt(argv, "-r", 44100);
  439. long buffer_size = lopt(argv, "-p", 512);
  440. long chan_in = lopt(argv, "-i", 2);
  441. long chan_out = lopt(argv, "-o", 2);
  442. long audiodevice = lopt(argv, "-I", -1);
  443. long sync = lopt(argv, "-s", 0);
  444. long timeout = lopt(argv, "-t", 100 * 1000);
  445. const char* name = flag(argv, "-n", "Built-in Audio");
  446. long rt = lopt(argv, "-R", 0);
  447. verbose = lopt(argv, "-v", 0);
  448. copyright(stdout);
  449. usage(stdout);
  450. FilterSIGPIPE();
  451. printf("jackdmp: sample_sate = %ld buffer_size = %ld chan_in = %ld chan_out = %ld name = %s sync-mode = %ld\n",
  452. sample_sate, buffer_size, chan_in, chan_out, name, sync);
  453. assert(buffer_size <= BUFFER_SIZE_MAX);
  454. int res = JackStart(sample_sate, buffer_size, chan_in, chan_out, name, audiodevice, sync, timeout, rt);
  455. if (res < 0) {
  456. jack_error("Cannot start server... exit");
  457. JackDelete();
  458. return 0;
  459. }
  460. while (((c = getchar()) != 'q')) {
  461. switch (c) {
  462. case 's':
  463. fServer->PrintState();
  464. break;
  465. }
  466. }
  467. JackStop();
  468. return 0;
  469. }
  470. #endif