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.

402 lines
11KB

  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 "JackServer.h"
  21. #include "JackConstants.h"
  22. #include "driver_interface.h"
  23. #include "JackDriverLoader.h"
  24. #include "shm.h"
  25. using namespace Jack;
  26. static JackServer* fServer;
  27. static char *server_name = "default";
  28. static int realtime_priority = 10;
  29. static int do_mlock = 1;
  30. static unsigned int port_max = 128;
  31. static int realtime = 0;
  32. static int loopback = 0;
  33. static int temporary = 0;
  34. static int client_timeout = 0; /* msecs; if zero, use period size. */
  35. static int do_unlock = 0;
  36. static JSList* drivers = NULL;
  37. static int sync = 0;
  38. static int xverbose = 0;
  39. #define DEFAULT_TMP_DIR "/tmp"
  40. char *jack_tmpdir = DEFAULT_TMP_DIR;
  41. void jack_print_driver_options (jack_driver_desc_t * desc, FILE *file);
  42. void jack_print_driver_param_usage (jack_driver_desc_t * desc, unsigned long param, FILE *file);
  43. int jack_parse_driver_params (jack_driver_desc_t * desc, int argc, char* argv[], JSList ** param_ptr);
  44. static void silent_jack_error_callback (const char *desc)
  45. {}
  46. static void copyright(FILE* file)
  47. {
  48. fprintf (file, "jackdmp " VERSION "\n"
  49. "Copyright 2001-2005 Paul Davis and others.\n"
  50. "Copyright 2004-2006 Grame.\n"
  51. "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
  52. "This is free software, and you are welcome to redistribute it\n"
  53. "under certain conditions; see the file COPYING for details\n");
  54. }
  55. static void usage (FILE *file)
  56. {
  57. copyright (file);
  58. fprintf (file, "\n"
  59. "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  60. " [ --name OR -n server-name ]\n"
  61. // " [ --no-mlock OR -m ]\n"
  62. // " [ --unlock OR -u ]\n"
  63. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  64. " [ --loopback OR -L loopback-port-number ]\n"
  65. // " [ --port-max OR -p maximum-number-of-ports]\n"
  66. " [ --verbose OR -v ]\n"
  67. " [ --silent OR -s ]\n"
  68. " [ --sync OR -S ]\n"
  69. " [ --version OR -V ]\n"
  70. " -d driver [ ... driver args ... ]\n"
  71. " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
  72. " jackdmp -d driver --help\n"
  73. " to display options for each driver\n\n");
  74. }
  75. 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)
  76. {
  77. printf("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  78. fServer = new JackServer(sync, time_out_ms, rt, priority, loopback, verbose);
  79. int res = fServer->Open(driver_desc, driver_params);
  80. return (res < 0) ? res : fServer->Start();
  81. }
  82. static int JackStop()
  83. {
  84. fServer->Stop();
  85. fServer->Close();
  86. printf("Jackdmp: server close\n");
  87. delete fServer;
  88. printf("Jackdmp: delete server\n");
  89. return 0;
  90. }
  91. static int JackDelete()
  92. {
  93. delete fServer;
  94. printf("Jackdmp: delete server\n");
  95. return 0;
  96. }
  97. /*
  98. static char* jack_default_server_name(void)
  99. {
  100. char *server_name;
  101. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  102. server_name = "default";
  103. return server_name;
  104. }
  105. // returns the name of the per-user subdirectory of jack_tmpdir
  106. static char* jack_user_dir(void)
  107. {
  108. static char user_dir[PATH_MAX] = "";
  109. // format the path name on the first call
  110. if (user_dir[0] == '\0') {
  111. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  112. jack_tmpdir, _getuid ());
  113. }
  114. return user_dir;
  115. }
  116. // returns the name of the per-server subdirectory of jack_user_dir()
  117. static char* get_jack_server_dir(const char * toto)
  118. {
  119. static char server_dir[PATH_MAX] = "";
  120. // format the path name on the first call
  121. if (server_dir[0] == '\0') {
  122. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  123. jack_user_dir (), server_name);
  124. }
  125. return server_dir;
  126. }
  127. static void jack_cleanup_files (const char *server_name)
  128. {
  129. DIR *dir;
  130. struct dirent *dirent;
  131. char *dir_name = get_jack_server_dir (server_name);
  132. // nothing to do if the server directory does not exist
  133. if ((dir = opendir (dir_name)) == NULL) {
  134. return;
  135. }
  136. // unlink all the files in this directory, they are mine
  137. while ((dirent = readdir (dir)) != NULL) {
  138. char fullpath[PATH_MAX];
  139. if ((strcmp (dirent->d_name, ".") == 0)
  140. || (strcmp (dirent->d_name, "..") == 0)) {
  141. continue;
  142. }
  143. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  144. dir_name, dirent->d_name);
  145. if (unlink (fullpath)) {
  146. jack_error ("cannot unlink `%s' (%s)", fullpath,
  147. strerror (errno));
  148. }
  149. }
  150. closedir (dir);
  151. // now, delete the per-server subdirectory, itself
  152. if (rmdir (dir_name)) {
  153. jack_error ("cannot remove `%s' (%s)", dir_name,
  154. strerror (errno));
  155. }
  156. // finally, delete the per-user subdirectory, if empty
  157. if (rmdir (jack_user_dir ())) {
  158. if (errno != ENOTEMPTY) {
  159. jack_error ("cannot remove `%s' (%s)",
  160. jack_user_dir (), strerror (errno));
  161. }
  162. }
  163. }
  164. */
  165. int main(int argc, char* argv[])
  166. {
  167. jack_driver_desc_t * driver_desc;
  168. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
  169. struct option long_options[] = {
  170. { "driver", 1, 0, 'd'
  171. },
  172. { "verbose", 0, 0, 'v' },
  173. { "help", 0, 0, 'h' },
  174. { "port-max", 1, 0, 'p' },
  175. { "no-mlock", 0, 0, 'm' },
  176. { "name", 0, 0, 'n' },
  177. { "unlock", 0, 0, 'u' },
  178. { "realtime", 0, 0, 'R' },
  179. { "loopback", 0, 0, 'L' },
  180. { "realtime-priority", 1, 0, 'P' },
  181. { "timeout", 1, 0, 't' },
  182. { "temporary", 0, 0, 'T' },
  183. { "version", 0, 0, 'V' },
  184. { "silent", 0, 0, 's' },
  185. { "sync", 0, 0, 'S' },
  186. { 0, 0, 0, 0 }
  187. };
  188. int opt = 0;
  189. int option_index = 0;
  190. int seen_driver = 0;
  191. char *driver_name = NULL;
  192. char **driver_args = NULL;
  193. JSList * driver_params;
  194. int driver_nargs = 1;
  195. int show_version = 0;
  196. int sync = 0;
  197. int i;
  198. int rc;
  199. char c;
  200. opterr = 0;
  201. while (!seen_driver &&
  202. (opt = getopt_long(argc, argv, options,
  203. long_options, &option_index)) != EOF) {
  204. switch (opt) {
  205. case 'd':
  206. seen_driver = 1;
  207. driver_name = optarg;
  208. break;
  209. case 'v':
  210. xverbose = 1;
  211. break;
  212. case 's':
  213. // steph
  214. //jack_set_error_function(silent_jack_error_callback);
  215. break;
  216. case 'S':
  217. sync = 1;
  218. break;
  219. case 'n':
  220. server_name = optarg;
  221. break;
  222. case 'm':
  223. do_mlock = 0;
  224. break;
  225. case 'p':
  226. port_max = (unsigned int)atol(optarg);
  227. break;
  228. case 'P':
  229. realtime_priority = atoi(optarg);
  230. break;
  231. case 'R':
  232. realtime = 1;
  233. break;
  234. case 'L':
  235. loopback = atoi(optarg);
  236. break;
  237. case 'T':
  238. temporary = 1;
  239. break;
  240. case 't':
  241. client_timeout = atoi(optarg);
  242. break;
  243. case 'u':
  244. do_unlock = 1;
  245. break;
  246. case 'V':
  247. show_version = 1;
  248. break;
  249. default:
  250. fprintf(stderr, "unknown option character %c\n",
  251. optopt);
  252. /*fallthru*/
  253. case 'h':
  254. usage(stdout);
  255. return -1;
  256. }
  257. }
  258. if (!seen_driver) {
  259. usage (stderr);
  260. exit (1);
  261. }
  262. drivers = jack_drivers_load (drivers);
  263. if (!drivers) {
  264. fprintf (stderr, "jackdmp: no drivers found; exiting\n");
  265. exit (1);
  266. }
  267. driver_desc = jack_find_driver_descriptor (drivers, driver_name);
  268. if (!driver_desc) {
  269. fprintf (stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  270. exit (1);
  271. }
  272. if (optind < argc) {
  273. driver_nargs = 1 + argc - optind;
  274. } else {
  275. driver_nargs = 1;
  276. }
  277. if (driver_nargs == 0) {
  278. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  279. " anything when run like this.\n");
  280. return -1;
  281. }
  282. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  283. driver_args[0] = driver_name;
  284. for (i = 1; i < driver_nargs; i++) {
  285. driver_args[i] = argv[optind++];
  286. }
  287. if (jack_parse_driver_params (driver_desc, driver_nargs,
  288. driver_args, &driver_params)) {
  289. exit (0);
  290. }
  291. //if (server_name == NULL)
  292. // server_name = jack_default_server_name ();
  293. copyright (stdout);
  294. rc = jack_register_server (server_name);
  295. switch (rc) {
  296. case EEXIST:
  297. fprintf (stderr, "`%s' server already active\n", server_name);
  298. exit (1);
  299. case ENOSPC:
  300. fprintf (stderr, "too many servers already active\n");
  301. exit (2);
  302. case ENOMEM:
  303. fprintf (stderr, "no access to shm registry\n");
  304. exit (3);
  305. default:
  306. if (xverbose)
  307. fprintf (stderr, "server `%s' registered\n",
  308. server_name);
  309. }
  310. /* clean up shared memory and files from any previous
  311. * instance of this server name */
  312. jack_cleanup_shm();
  313. // jack_cleanup_files(server_name);
  314. if (!realtime && client_timeout == 0)
  315. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  316. int res = JackStart(driver_desc, driver_params, sync, client_timeout, realtime, realtime_priority, loopback, xverbose);
  317. if (res < 0) {
  318. printf("Cannot start server... exit\n");
  319. JackDelete();
  320. return 0;
  321. }
  322. printf("Type 'q' to quit\n");
  323. while ((c = getchar()) != 'q') {}
  324. JackStop();
  325. jack_cleanup_shm();
  326. // jack_cleanup_files(server_name);
  327. jack_unregister_server(server_name);
  328. return 1;
  329. }