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.

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