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.

488 lines
13KB

  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 <process.h>
  19. #include <getopt.h>
  20. #include <signal.h>
  21. #include "JackServer.h"
  22. #include "JackConstants.h"
  23. #include "driver_interface.h"
  24. #include "JackDriverLoader.h"
  25. #include "shm.h"
  26. using namespace Jack;
  27. static JackServer* fServer;
  28. static char *server_name = "default";
  29. static int realtime_priority = 10;
  30. static int do_mlock = 1;
  31. static unsigned int port_max = 128;
  32. static int realtime = 0;
  33. static int loopback = 0;
  34. static int temporary = 0;
  35. static int client_timeout = 0; /* msecs; if zero, use period size. */
  36. static int do_unlock = 0;
  37. static JSList* drivers = NULL;
  38. static int sync = 0;
  39. static int xverbose = 0;
  40. #define DEFAULT_TMP_DIR "/tmp"
  41. char *jack_tmpdir = DEFAULT_TMP_DIR;
  42. HANDLE waitEvent;
  43. void jack_print_driver_options (jack_driver_desc_t * desc, FILE *file);
  44. void jack_print_driver_param_usage (jack_driver_desc_t * desc, unsigned long param, FILE *file);
  45. int jack_parse_driver_params (jack_driver_desc_t * desc, int argc, char* argv[], JSList ** param_ptr);
  46. static void silent_jack_error_callback (const char *desc)
  47. {}
  48. static void copyright(FILE* file)
  49. {
  50. fprintf (file, "jackdmp " VERSION "\n"
  51. "Copyright 2001-2005 Paul Davis and others.\n"
  52. "Copyright 2004-2006 Grame.\n"
  53. "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
  54. "This is free software, and you are welcome to redistribute it\n"
  55. "under certain conditions; see the file COPYING for details\n");
  56. }
  57. static void usage (FILE *file)
  58. {
  59. copyright (file);
  60. fprintf (file, "\n"
  61. "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  62. " [ --name OR -n server-name ]\n"
  63. // " [ --no-mlock OR -m ]\n"
  64. // " [ --unlock OR -u ]\n"
  65. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  66. " [ --loopback OR -L loopback-port-number ]\n"
  67. // " [ --port-max OR -p maximum-number-of-ports]\n"
  68. " [ --verbose OR -v ]\n"
  69. " [ --silent OR -s ]\n"
  70. " [ --sync OR -S ]\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 int JackStart(jack_driver_desc_t* driver_desc, JSList* driver_params, int sync, int time_out_ms, int rt, int priority, int loopback, int verbose)
  78. {
  79. printf("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  80. fServer = new JackServer(sync, time_out_ms, rt, priority, loopback, verbose);
  81. int res = fServer->Open(driver_desc, driver_params);
  82. return (res < 0) ? res : fServer->Start();
  83. }
  84. static int JackStop()
  85. {
  86. fServer->Stop();
  87. fServer->Close();
  88. printf("Jackdmp: server close\n");
  89. delete fServer;
  90. printf("Jackdmp: delete server\n");
  91. return 0;
  92. }
  93. static int JackDelete()
  94. {
  95. delete fServer;
  96. printf("Jackdmp: delete server\n");
  97. return 0;
  98. }
  99. static void intrpt(int signum)
  100. {
  101. printf("jack main caught signal %d\n", signum);
  102. (void) signal(SIGINT, SIG_DFL);
  103. SetEvent(waitEvent);
  104. }
  105. /*
  106. static char* jack_default_server_name(void)
  107. {
  108. char *server_name;
  109. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  110. server_name = "default";
  111. return server_name;
  112. }
  113. // returns the name of the per-user subdirectory of jack_tmpdir
  114. static char* jack_user_dir(void)
  115. {
  116. static char user_dir[PATH_MAX] = "";
  117. // format the path name on the first call
  118. if (user_dir[0] == '\0') {
  119. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  120. jack_tmpdir, _getuid ());
  121. }
  122. return user_dir;
  123. }
  124. // returns the name of the per-server subdirectory of jack_user_dir()
  125. static char* get_jack_server_dir(const char * toto)
  126. {
  127. static char server_dir[PATH_MAX] = "";
  128. // format the path name on the first call
  129. if (server_dir[0] == '\0') {
  130. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  131. jack_user_dir (), server_name);
  132. }
  133. return server_dir;
  134. }
  135. static void jack_cleanup_files (const char *server_name)
  136. {
  137. DIR *dir;
  138. struct dirent *dirent;
  139. char *dir_name = get_jack_server_dir (server_name);
  140. // nothing to do if the server directory does not exist
  141. if ((dir = opendir (dir_name)) == NULL) {
  142. return;
  143. }
  144. // unlink all the files in this directory, they are mine
  145. while ((dirent = readdir (dir)) != NULL) {
  146. char fullpath[PATH_MAX];
  147. if ((strcmp (dirent->d_name, ".") == 0)
  148. || (strcmp (dirent->d_name, "..") == 0)) {
  149. continue;
  150. }
  151. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  152. dir_name, dirent->d_name);
  153. if (unlink (fullpath)) {
  154. jack_error ("cannot unlink `%s' (%s)", fullpath,
  155. strerror (errno));
  156. }
  157. }
  158. closedir (dir);
  159. // now, delete the per-server subdirectory, itself
  160. if (rmdir (dir_name)) {
  161. jack_error ("cannot remove `%s' (%s)", dir_name,
  162. strerror (errno));
  163. }
  164. // finally, delete the per-user subdirectory, if empty
  165. if (rmdir (jack_user_dir ())) {
  166. if (errno != ENOTEMPTY) {
  167. jack_error ("cannot remove `%s' (%s)",
  168. jack_user_dir (), strerror (errno));
  169. }
  170. }
  171. }
  172. */
  173. /*
  174. BOOL CtrlHandler( DWORD fdwCtrlType )
  175. {
  176. switch( fdwCtrlType )
  177. {
  178. // Handle the CTRL-C signal.
  179. case CTRL_C_EVENT:
  180. printf( "Ctrl-C event\n\n" );
  181. Beep( 750, 300 );
  182. SetEvent(waitEvent);
  183. return( TRUE );
  184. // CTRL-CLOSE: confirm that the user wants to exit.
  185. case CTRL_CLOSE_EVENT:
  186. Beep( 600, 200 );
  187. printf( "Ctrl-Close event\n\n" );
  188. SetEvent(waitEvent);
  189. return( TRUE );
  190. // Pass other signals to the next handler.
  191. case CTRL_BREAK_EVENT:
  192. Beep( 900, 200 );
  193. printf( "Ctrl-Break event\n\n" );
  194. return FALSE;
  195. case CTRL_LOGOFF_EVENT:
  196. Beep( 1000, 200 );
  197. printf( "Ctrl-Logoff event\n\n" );
  198. return FALSE;
  199. case CTRL_SHUTDOWN_EVENT:
  200. Beep( 750, 500 );
  201. printf( "Ctrl-Shutdown event\n\n" );
  202. return FALSE;
  203. default:
  204. return FALSE;
  205. }
  206. }
  207. */
  208. int main(int argc, char* argv[])
  209. {
  210. jack_driver_desc_t * driver_desc;
  211. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
  212. struct option long_options[] = {
  213. { "driver", 1, 0, 'd'
  214. },
  215. { "verbose", 0, 0, 'v' },
  216. { "help", 0, 0, 'h' },
  217. { "port-max", 1, 0, 'p' },
  218. { "no-mlock", 0, 0, 'm' },
  219. { "name", 0, 0, 'n' },
  220. { "unlock", 0, 0, 'u' },
  221. { "realtime", 0, 0, 'R' },
  222. { "loopback", 0, 0, 'L' },
  223. { "realtime-priority", 1, 0, 'P' },
  224. { "timeout", 1, 0, 't' },
  225. { "temporary", 0, 0, 'T' },
  226. { "version", 0, 0, 'V' },
  227. { "silent", 0, 0, 's' },
  228. { "sync", 0, 0, 'S' },
  229. { 0, 0, 0, 0 }
  230. };
  231. int opt = 0;
  232. int option_index = 0;
  233. int seen_driver = 0;
  234. char *driver_name = NULL;
  235. char **driver_args = NULL;
  236. JSList * driver_params;
  237. int driver_nargs = 1;
  238. int show_version = 0;
  239. int sync = 0;
  240. int i;
  241. int rc;
  242. char c;
  243. // Creates wait event
  244. if ((waitEvent = CreateEvent(NULL, FALSE, FALSE, NULL)) == NULL) {
  245. printf("CreateEvent fails err = %ld\n", GetLastError());
  246. return 0;
  247. }
  248. opterr = 0;
  249. while (!seen_driver &&
  250. (opt = getopt_long(argc, argv, options,
  251. long_options, &option_index)) != EOF) {
  252. switch (opt) {
  253. case 'd':
  254. seen_driver = 1;
  255. driver_name = optarg;
  256. break;
  257. case 'v':
  258. xverbose = 1;
  259. break;
  260. case 's':
  261. // steph
  262. //jack_set_error_function(silent_jack_error_callback);
  263. break;
  264. case 'S':
  265. sync = 1;
  266. break;
  267. case 'n':
  268. server_name = optarg;
  269. break;
  270. case 'm':
  271. do_mlock = 0;
  272. break;
  273. case 'p':
  274. port_max = (unsigned int)atol(optarg);
  275. break;
  276. case 'P':
  277. realtime_priority = atoi(optarg);
  278. break;
  279. case 'R':
  280. realtime = 1;
  281. break;
  282. case 'L':
  283. loopback = atoi(optarg);
  284. break;
  285. case 'T':
  286. temporary = 1;
  287. break;
  288. case 't':
  289. client_timeout = atoi(optarg);
  290. break;
  291. case 'u':
  292. do_unlock = 1;
  293. break;
  294. case 'V':
  295. show_version = 1;
  296. break;
  297. default:
  298. fprintf(stderr, "unknown option character %c\n",
  299. optopt);
  300. /*fallthru*/
  301. case 'h':
  302. usage(stdout);
  303. return -1;
  304. }
  305. }
  306. if (!seen_driver) {
  307. usage (stderr);
  308. //exit (1);
  309. return 0;
  310. }
  311. drivers = jack_drivers_load (drivers);
  312. if (!drivers) {
  313. fprintf (stderr, "jackdmp: no drivers found; exiting\n");
  314. //exit (1);
  315. return 0;
  316. }
  317. driver_desc = jack_find_driver_descriptor (drivers, driver_name);
  318. if (!driver_desc) {
  319. fprintf (stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  320. //exit (1);
  321. return 0;
  322. }
  323. if (optind < argc) {
  324. driver_nargs = 1 + argc - optind;
  325. } else {
  326. driver_nargs = 1;
  327. }
  328. if (driver_nargs == 0) {
  329. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  330. " anything when run like this.\n");
  331. return -1;
  332. }
  333. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  334. driver_args[0] = driver_name;
  335. for (i = 1; i < driver_nargs; i++) {
  336. driver_args[i] = argv[optind++];
  337. }
  338. if (jack_parse_driver_params (driver_desc, driver_nargs,
  339. driver_args, &driver_params)) {
  340. // exit (0);
  341. return 0;
  342. }
  343. //if (server_name == NULL)
  344. // server_name = jack_default_server_name ();
  345. copyright (stdout);
  346. rc = jack_register_server (server_name);
  347. switch (rc) {
  348. case EEXIST:
  349. fprintf (stderr, "`%s' server already active\n", server_name);
  350. //exit (1);
  351. return 0;
  352. case ENOSPC:
  353. fprintf (stderr, "too many servers already active\n");
  354. //exit (2);
  355. return 0;
  356. case ENOMEM:
  357. fprintf (stderr, "no access to shm registry\n");
  358. //exit (3);
  359. return 0;
  360. default:
  361. if (xverbose)
  362. fprintf (stderr, "server `%s' registered\n",
  363. server_name);
  364. }
  365. /* clean up shared memory and files from any previous
  366. * instance of this server name */
  367. jack_cleanup_shm();
  368. // jack_cleanup_files(server_name);
  369. if (!realtime && client_timeout == 0)
  370. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  371. int res = JackStart(driver_desc, driver_params, sync, client_timeout, realtime, realtime_priority, loopback, xverbose);
  372. if (res < 0) {
  373. printf("Cannot start server... exit\n");
  374. JackDelete();
  375. return 0;
  376. }
  377. /*
  378. if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) )
  379. {
  380. printf( "\nThe Control Handler is installed.\n" );
  381. } else {
  382. printf( "\nERROR: Could not set control handler");
  383. }
  384. */
  385. (void) signal(SIGINT, intrpt);
  386. (void) signal(SIGABRT, intrpt);
  387. (void) signal(SIGTERM, intrpt);
  388. if ((res = WaitForSingleObject(waitEvent, INFINITE)) != WAIT_OBJECT_0) {
  389. printf("WaitForSingleObject fails err = %ld\n", GetLastError());
  390. }
  391. /*
  392. printf("Type 'q' to quit\n");
  393. while ((c = getchar()) != 'q') {}
  394. */
  395. JackStop();
  396. jack_cleanup_shm();
  397. // jack_cleanup_files(server_name);
  398. jack_unregister_server(server_name);
  399. CloseHandle(waitEvent);
  400. return 1;
  401. }