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.

427 lines
12KB

  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. int main(int argc, char* argv[])
  174. {
  175. jack_driver_desc_t * driver_desc;
  176. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
  177. struct option long_options[] = {
  178. { "driver", 1, 0, 'd'
  179. },
  180. { "verbose", 0, 0, 'v' },
  181. { "help", 0, 0, 'h' },
  182. { "port-max", 1, 0, 'p' },
  183. { "no-mlock", 0, 0, 'm' },
  184. { "name", 0, 0, 'n' },
  185. { "unlock", 0, 0, 'u' },
  186. { "realtime", 0, 0, 'R' },
  187. { "loopback", 0, 0, 'L' },
  188. { "realtime-priority", 1, 0, 'P' },
  189. { "timeout", 1, 0, 't' },
  190. { "temporary", 0, 0, 'T' },
  191. { "version", 0, 0, 'V' },
  192. { "silent", 0, 0, 's' },
  193. { "sync", 0, 0, 'S' },
  194. { 0, 0, 0, 0 }
  195. };
  196. int opt = 0;
  197. int option_index = 0;
  198. int seen_driver = 0;
  199. char *driver_name = NULL;
  200. char **driver_args = NULL;
  201. JSList * driver_params;
  202. int driver_nargs = 1;
  203. int show_version = 0;
  204. int sync = 0;
  205. int i;
  206. int rc;
  207. char c;
  208. // Creates wait event
  209. if ((waitEvent = CreateEvent(NULL, FALSE, FALSE, NULL)) == NULL) {
  210. printf("CreateEvent fails err = %ld\n", GetLastError());
  211. return 0;
  212. }
  213. opterr = 0;
  214. while (!seen_driver &&
  215. (opt = getopt_long(argc, argv, options,
  216. long_options, &option_index)) != EOF) {
  217. switch (opt) {
  218. case 'd':
  219. seen_driver = 1;
  220. driver_name = optarg;
  221. break;
  222. case 'v':
  223. xverbose = 1;
  224. break;
  225. case 's':
  226. // steph
  227. //jack_set_error_function(silent_jack_error_callback);
  228. break;
  229. case 'S':
  230. sync = 1;
  231. break;
  232. case 'n':
  233. server_name = optarg;
  234. break;
  235. case 'm':
  236. do_mlock = 0;
  237. break;
  238. case 'p':
  239. port_max = (unsigned int)atol(optarg);
  240. break;
  241. case 'P':
  242. realtime_priority = atoi(optarg);
  243. break;
  244. case 'R':
  245. realtime = 1;
  246. break;
  247. case 'L':
  248. loopback = atoi(optarg);
  249. break;
  250. case 'T':
  251. temporary = 1;
  252. break;
  253. case 't':
  254. client_timeout = atoi(optarg);
  255. break;
  256. case 'u':
  257. do_unlock = 1;
  258. break;
  259. case 'V':
  260. show_version = 1;
  261. break;
  262. default:
  263. fprintf(stderr, "unknown option character %c\n",
  264. optopt);
  265. /*fallthru*/
  266. case 'h':
  267. usage(stdout);
  268. return -1;
  269. }
  270. }
  271. if (!seen_driver) {
  272. usage (stderr);
  273. exit (1);
  274. }
  275. drivers = jack_drivers_load (drivers);
  276. if (!drivers) {
  277. fprintf (stderr, "jackdmp: no drivers found; exiting\n");
  278. exit (1);
  279. }
  280. driver_desc = jack_find_driver_descriptor (drivers, driver_name);
  281. if (!driver_desc) {
  282. fprintf (stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  283. exit (1);
  284. }
  285. if (optind < argc) {
  286. driver_nargs = 1 + argc - optind;
  287. } else {
  288. driver_nargs = 1;
  289. }
  290. if (driver_nargs == 0) {
  291. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  292. " anything when run like this.\n");
  293. return -1;
  294. }
  295. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  296. driver_args[0] = driver_name;
  297. for (i = 1; i < driver_nargs; i++) {
  298. driver_args[i] = argv[optind++];
  299. }
  300. if (jack_parse_driver_params (driver_desc, driver_nargs,
  301. driver_args, &driver_params)) {
  302. exit (0);
  303. }
  304. //if (server_name == NULL)
  305. // server_name = jack_default_server_name ();
  306. copyright (stdout);
  307. rc = jack_register_server (server_name);
  308. switch (rc) {
  309. case EEXIST:
  310. fprintf (stderr, "`%s' server already active\n", server_name);
  311. exit (1);
  312. case ENOSPC:
  313. fprintf (stderr, "too many servers already active\n");
  314. exit (2);
  315. case ENOMEM:
  316. fprintf (stderr, "no access to shm registry\n");
  317. exit (3);
  318. default:
  319. if (xverbose)
  320. fprintf (stderr, "server `%s' registered\n",
  321. server_name);
  322. }
  323. /* clean up shared memory and files from any previous
  324. * instance of this server name */
  325. jack_cleanup_shm();
  326. // jack_cleanup_files(server_name);
  327. if (!realtime && client_timeout == 0)
  328. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  329. int res = JackStart(driver_desc, driver_params, sync, client_timeout, realtime, realtime_priority, loopback, xverbose);
  330. if (res < 0) {
  331. printf("Cannot start server... exit\n");
  332. JackDelete();
  333. return 0;
  334. }
  335. (void) signal(SIGINT, intrpt);
  336. if ((res = WaitForSingleObject(waitEvent, INFINITE)) != WAIT_OBJECT_0) {
  337. printf("WaitForSingleObject fails err = %ld\n", GetLastError());
  338. }
  339. /*
  340. printf("Type 'q' to quit\n");
  341. while ((c = getchar()) != 'q') {}
  342. */
  343. JackStop();
  344. jack_cleanup_shm();
  345. // jack_cleanup_files(server_name);
  346. jack_unregister_server(server_name);
  347. CloseHandle(waitEvent);
  348. return 1;
  349. }