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.

390 lines
11KB

  1. /*
  2. Copyright (C) 2005 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackServerGlobals.h"
  16. #include "JackError.h"
  17. #include "shm.h"
  18. #include <getopt.h>
  19. #ifndef WIN32
  20. #include <dirent.h>
  21. #endif
  22. #define DEFAULT_TMP_DIR "/tmp"
  23. char* jack_tmpdir = DEFAULT_TMP_DIR;
  24. static char* server_name = "default";
  25. static int realtime = 0;
  26. static int client_timeout = 0; /* msecs; if zero, use period size. */
  27. static int realtime_priority = 10;
  28. static int verbose_aux = 0;
  29. static int do_mlock = 1;
  30. static unsigned int port_max = 128;
  31. static int loopback = 0;
  32. static int do_unlock = 0;
  33. static int temporary = 0;
  34. namespace Jack
  35. {
  36. JackServerGlobals* JackServerGlobals::fGlobals = NULL;
  37. long JackServerGlobals::fClientCount = 0;
  38. JackServer* JackServerGlobals::fServer = NULL;
  39. #ifndef WIN32
  40. static char* jack_default_server_name(void)
  41. {
  42. char *server_name;
  43. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  44. server_name = "default";
  45. return server_name;
  46. }
  47. /* returns the name of the per-user subdirectory of jack_tmpdir */
  48. static char* jack_user_dir(void)
  49. {
  50. static char user_dir[PATH_MAX] = "";
  51. /* format the path name on the first call */
  52. if (user_dir[0] == '\0') {
  53. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  54. jack_tmpdir, getuid ());
  55. }
  56. return user_dir;
  57. }
  58. /* returns the name of the per-server subdirectory of jack_user_dir() */
  59. static char* get_jack_server_dir(const char* toto)
  60. {
  61. static char server_dir[PATH_MAX] = "";
  62. // format the path name on the first call
  63. if (server_dir[0] == '\0') {
  64. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  65. jack_user_dir (), server_name);
  66. }
  67. return server_dir;
  68. }
  69. static void
  70. jack_cleanup_files (const char *server_name)
  71. {
  72. DIR *dir;
  73. struct dirent *dirent;
  74. char *dir_name = get_jack_server_dir (server_name);
  75. /* On termination, we remove all files that jackd creates so
  76. * subsequent attempts to start jackd will not believe that an
  77. * instance is already running. If the server crashes or is
  78. * terminated with SIGKILL, this is not possible. So, cleanup
  79. * is also attempted when jackd starts.
  80. *
  81. * There are several tricky issues. First, the previous JACK
  82. * server may have run for a different user ID, so its files
  83. * may be inaccessible. This is handled by using a separate
  84. * JACK_TMP_DIR subdirectory for each user. Second, there may
  85. * be other servers running with different names. Each gets
  86. * its own subdirectory within the per-user directory. The
  87. * current process has already registered as `server_name', so
  88. * we know there is no other server actively using that name.
  89. */
  90. /* nothing to do if the server directory does not exist */
  91. if ((dir = opendir (dir_name)) == NULL) {
  92. return ;
  93. }
  94. /* unlink all the files in this directory, they are mine */
  95. while ((dirent = readdir (dir)) != NULL) {
  96. char fullpath[PATH_MAX];
  97. if ((strcmp (dirent->d_name, ".") == 0)
  98. || (strcmp (dirent->d_name, "..") == 0)) {
  99. continue;
  100. }
  101. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  102. dir_name, dirent->d_name);
  103. if (unlink (fullpath)) {
  104. jack_error ("cannot unlink `%s' (%s)", fullpath,
  105. strerror (errno));
  106. }
  107. }
  108. closedir (dir);
  109. /* now, delete the per-server subdirectory, itself */
  110. if (rmdir (dir_name)) {
  111. jack_error ("cannot remove `%s' (%s)", dir_name,
  112. strerror (errno));
  113. }
  114. /* finally, delete the per-user subdirectory, if empty */
  115. if (rmdir (jack_user_dir ())) {
  116. if (errno != ENOTEMPTY) {
  117. jack_error ("cannot remove `%s' (%s)",
  118. jack_user_dir (), strerror (errno));
  119. }
  120. }
  121. }
  122. #endif
  123. int JackServerGlobals::JackStart(jack_driver_desc_t* driver_desc, JSList* driver_params, int sync, int time_out_ms, int rt, int priority, int loopback, int verbose)
  124. {
  125. JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  126. fServer = new JackServer(sync, time_out_ms, rt, priority, loopback, verbose);
  127. int res = fServer->Open(driver_desc, driver_params);
  128. return (res < 0) ? res : fServer->Start();
  129. }
  130. int JackServerGlobals::JackStop()
  131. {
  132. fServer->Stop();
  133. fServer->Close();
  134. JackLog("Jackdmp: server close\n");
  135. delete fServer;
  136. JackLog("Jackdmp: delete server\n");
  137. return 0;
  138. }
  139. int JackServerGlobals::JackDelete()
  140. {
  141. delete fServer;
  142. JackLog("Jackdmp: delete server\n");
  143. return 0;
  144. }
  145. // Temporary : to test
  146. JackServerGlobals::JackServerGlobals()
  147. {
  148. jack_driver_desc_t* driver_desc;
  149. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
  150. struct option long_options[] = {
  151. { "driver", 1, 0, 'd'
  152. },
  153. { "verbose", 0, 0, 'v' },
  154. { "help", 0, 0, 'h' },
  155. { "port-max", 1, 0, 'p' },
  156. { "no-mlock", 0, 0, 'm' },
  157. { "name", 0, 0, 'n' },
  158. { "unlock", 0, 0, 'u' },
  159. { "realtime", 0, 0, 'R' },
  160. { "loopback", 0, 0, 'L' },
  161. { "realtime-priority", 1, 0, 'P' },
  162. { "timeout", 1, 0, 't' },
  163. { "temporary", 0, 0, 'T' },
  164. { "version", 0, 0, 'V' },
  165. { "silent", 0, 0, 's' },
  166. { "sync", 0, 0, 'S' },
  167. { 0, 0, 0, 0 }
  168. };
  169. int opt = 0;
  170. int option_index = 0;
  171. int seen_driver = 0;
  172. char *driver_name = NULL;
  173. char **driver_args = NULL;
  174. JSList* driver_params;
  175. int driver_nargs = 1;
  176. JSList* drivers = NULL;
  177. char* server_name = NULL;
  178. int show_version = 0;
  179. int sync = 0;
  180. int rc, i;
  181. int argc = 8;
  182. //char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512"};
  183. char* argv[] = {"jackdmp", "-R", "-S", "-v", "-d", "portaudio", "-p", "512"};
  184. for (i = 0; i < argc; i++) {
  185. printf("arg %i %s\n", i, argv[i]);
  186. }
  187. opterr = 0;
  188. while (!seen_driver &&
  189. (opt = getopt_long(argc, argv, options,
  190. long_options, &option_index)) != EOF) {
  191. switch (opt) {
  192. case 'd':
  193. seen_driver = 1;
  194. driver_name = optarg;
  195. break;
  196. case 'v':
  197. verbose_aux = 1;
  198. break;
  199. case 's':
  200. // jack_set_error_function(silent_jack_error_callback);
  201. break;
  202. case 'S':
  203. sync = 1;
  204. break;
  205. case 'n':
  206. server_name = optarg;
  207. break;
  208. case 'm':
  209. do_mlock = 0;
  210. break;
  211. case 'p':
  212. port_max = (unsigned int)atol(optarg);
  213. break;
  214. case 'P':
  215. realtime_priority = atoi(optarg);
  216. break;
  217. case 'R':
  218. realtime = 1;
  219. break;
  220. case 'L':
  221. loopback = atoi(optarg);
  222. break;
  223. case 'T':
  224. temporary = 1;
  225. break;
  226. case 't':
  227. client_timeout = atoi(optarg);
  228. break;
  229. case 'u':
  230. do_unlock = 1;
  231. break;
  232. case 'V':
  233. show_version = 1;
  234. break;
  235. default:
  236. fprintf(stderr, "unknown option character %c\n",
  237. optopt);
  238. /*fallthru*/
  239. case 'h':
  240. //usage(stdout);
  241. return ;
  242. }
  243. }
  244. drivers = jack_drivers_load (drivers);
  245. if (!drivers) {
  246. fprintf (stderr, "jackdmp: no drivers found; exiting\n");
  247. exit (1);
  248. }
  249. driver_desc = jack_find_driver_descriptor (drivers, driver_name);
  250. if (!driver_desc) {
  251. fprintf (stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  252. exit (1);
  253. }
  254. if (optind < argc) {
  255. driver_nargs = 1 + argc - optind;
  256. } else {
  257. driver_nargs = 1;
  258. }
  259. if (driver_nargs == 0) {
  260. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  261. " anything when run like this.\n");
  262. return ;
  263. }
  264. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  265. driver_args[0] = driver_name;
  266. for (i = 1; i < driver_nargs; i++) {
  267. driver_args[i] = argv[optind++];
  268. }
  269. if (jack_parse_driver_params (driver_desc, driver_nargs,
  270. driver_args, &driver_params)) {
  271. return ;
  272. }
  273. #ifndef WIN32
  274. if (server_name == NULL)
  275. server_name = jack_default_server_name ();
  276. #endif
  277. rc = jack_register_server (server_name);
  278. /* clean up shared memory and files from any previous
  279. * instance of this server name */
  280. jack_cleanup_shm();
  281. #ifndef WIN32
  282. jack_cleanup_files(server_name);
  283. #endif
  284. if (!realtime && client_timeout == 0)
  285. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  286. int res = JackStart(driver_desc, driver_params, sync, client_timeout, realtime, realtime_priority, loopback, verbose_aux);
  287. if (res < 0) {
  288. jack_error("Cannot start server... exit");
  289. JackDelete();
  290. return ;
  291. }
  292. }
  293. JackServerGlobals::~JackServerGlobals()
  294. {
  295. JackLog("~JackServerGlobals\n");
  296. JackStop();
  297. jack_cleanup_shm();
  298. #ifndef WIN32
  299. jack_cleanup_files(server_name);
  300. #endif
  301. jack_unregister_server(server_name);
  302. }
  303. void JackServerGlobals::Init()
  304. {
  305. if (fClientCount++ == 0 && !fGlobals) {
  306. JackLog("JackServerGlobals Init %x\n", fGlobals);
  307. fGlobals = new JackServerGlobals();
  308. }
  309. }
  310. void JackServerGlobals::Destroy()
  311. {
  312. if (--fClientCount == 0 && fGlobals) {
  313. JackLog("JackServerGlobals Destroy %x\n", fGlobals);
  314. delete fGlobals;
  315. fGlobals = NULL;
  316. }
  317. }
  318. } // end of namespace