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.

410 lines
13KB

  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 "JackLockedEngine.h"
  17. #include "JackTools.h"
  18. #include "shm.h"
  19. #include <getopt.h>
  20. #include <errno.h>
  21. static char* server_name = NULL;
  22. namespace Jack
  23. {
  24. JackServer* JackServerGlobals::fInstance;
  25. unsigned int JackServerGlobals::fUserCount;
  26. std::map<std::string, JackDriverInfo*> JackServerGlobals::fSlavesList;
  27. std::map<std::string, int> JackServerGlobals::fInternalsList;
  28. bool (* JackServerGlobals::on_device_acquire)(const char * device_name) = NULL;
  29. void (* JackServerGlobals::on_device_release)(const char * device_name) = NULL;
  30. int JackServerGlobals::Start(const char* server_name,
  31. jack_driver_desc_t* driver_desc,
  32. JSList* driver_params,
  33. int sync,
  34. int temporary,
  35. int time_out_ms,
  36. int rt,
  37. int priority,
  38. int port_max,
  39. int verbose,
  40. jack_timer_type_t clock)
  41. {
  42. jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync, time_out_ms, rt, priority, verbose);
  43. new JackServer(sync, temporary, time_out_ms, rt, priority, port_max, verbose, clock, server_name); // Will setup fInstance and fUserCount globals
  44. int res = fInstance->Open(driver_desc, driver_params);
  45. return (res < 0) ? res : fInstance->Start();
  46. }
  47. void JackServerGlobals::Stop()
  48. {
  49. jack_log("Jackdmp: server close");
  50. fInstance->Stop();
  51. fInstance->Close();
  52. }
  53. void JackServerGlobals::Delete()
  54. {
  55. jack_log("Jackdmp: delete server");
  56. // Slave drivers
  57. std::map<std::string, JackDriverInfo*>::iterator it1;
  58. for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
  59. JackDriverInfo* info = (*it1).second;
  60. if (info) {
  61. fInstance->RemoveSlave((info));
  62. delete (info);
  63. }
  64. }
  65. fSlavesList.clear();
  66. // Internal clients
  67. std::map<std::string, int> ::iterator it2;
  68. for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
  69. int status;
  70. int refnum = (*it2).second;
  71. if (refnum > 0) {
  72. // Client object is internally kept in JackEngine, and will be deallocated in InternalClientUnload
  73. fInstance->GetEngine()->InternalClientUnload(refnum, &status);
  74. }
  75. }
  76. fInternalsList.clear();
  77. delete fInstance;
  78. fInstance = NULL;
  79. }
  80. bool JackServerGlobals::Init()
  81. {
  82. int realtime = 0;
  83. int client_timeout = 0; /* msecs; if zero, use period size. */
  84. int realtime_priority = 10;
  85. int verbose_aux = 0;
  86. unsigned int port_max = 128;
  87. int temporary = 0;
  88. int opt = 0;
  89. int option_index = 0;
  90. char *master_driver_name = NULL;
  91. char **master_driver_args = NULL;
  92. JSList* master_driver_params = NULL;
  93. jack_driver_desc_t* driver_desc;
  94. jack_timer_type_t clock_source = JACK_TIMER_SYSTEM_CLOCK;
  95. int driver_nargs = 1;
  96. JSList* drivers = NULL;
  97. int loopback = 0;
  98. int sync = 0;
  99. int rc, i;
  100. int res;
  101. int replace_registry = 0;
  102. FILE* fp = 0;
  103. char filename[255];
  104. char buffer[255];
  105. int argc = 0;
  106. char* argv[32];
  107. // First user starts the server
  108. if (fUserCount++ == 0) {
  109. jack_log("JackServerGlobals Init");
  110. const char *options = "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
  111. #ifdef __linux__
  112. "c:"
  113. #endif
  114. ;
  115. struct option long_options[] = {
  116. #ifdef __linux__
  117. { "clock-source", 1, 0, 'c' },
  118. #endif
  119. { "loopback-driver", 1, 0, 'L' },
  120. { "audio-driver", 1, 0, 'd' },
  121. { "midi-driver", 1, 0, 'X' },
  122. { "internal-client", 1, 0, 'I' },
  123. { "verbose", 0, 0, 'v' },
  124. { "help", 0, 0, 'h' },
  125. { "port-max", 1, 0, 'p' },
  126. { "no-mlock", 0, 0, 'm' },
  127. { "name", 1, 0, 'n' },
  128. { "unlock", 0, 0, 'u' },
  129. { "realtime", 0, 0, 'R' },
  130. { "no-realtime", 0, 0, 'r' },
  131. { "replace-registry", 0, &replace_registry, 0 },
  132. { "loopback", 0, 0, 'L' },
  133. { "realtime-priority", 1, 0, 'P' },
  134. { "timeout", 1, 0, 't' },
  135. { "temporary", 0, 0, 'T' },
  136. { "version", 0, 0, 'V' },
  137. { "silent", 0, 0, 's' },
  138. { "sync", 0, 0, 'S' },
  139. { 0, 0, 0, 0 }
  140. };
  141. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  142. fp = fopen(filename, "r");
  143. if (!fp) {
  144. fp = fopen("/etc/jackdrc", "r");
  145. }
  146. // if still not found, check old config name for backwards compatability
  147. if (!fp) {
  148. fp = fopen("/etc/jackd.conf", "r");
  149. }
  150. argc = 0;
  151. if (fp) {
  152. res = fscanf(fp, "%s", buffer);
  153. while (res != 0 && res != EOF) {
  154. argv[argc] = (char*)malloc(64);
  155. strcpy(argv[argc], buffer);
  156. res = fscanf(fp, "%s", buffer);
  157. argc++;
  158. }
  159. fclose(fp);
  160. }
  161. /*
  162. For testing
  163. int argc = 15;
  164. char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
  165. */
  166. opterr = 0;
  167. optind = 1; // Important : to reset argv parsing
  168. while (!master_driver_name &&
  169. (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
  170. switch (opt) {
  171. case 'c':
  172. if (tolower (optarg[0]) == 'h') {
  173. clock_source = JACK_TIMER_HPET;
  174. } else if (tolower (optarg[0]) == 'c') {
  175. clock_source = JACK_TIMER_CYCLE_COUNTER;
  176. } else if (tolower (optarg[0]) == 's') {
  177. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  178. } else {
  179. jack_error("unknown option character %c", optopt);
  180. }
  181. break;
  182. case 'd':
  183. master_driver_name = optarg;
  184. break;
  185. case 'L':
  186. loopback = atoi(optarg);
  187. break;
  188. case 'X':
  189. fSlavesList[optarg] = NULL;
  190. break;
  191. case 'I':
  192. fInternalsList[optarg] = -1;
  193. break;
  194. case 'p':
  195. port_max = (unsigned int)atol(optarg);
  196. break;
  197. case 'v':
  198. verbose_aux = 1;
  199. break;
  200. case 'S':
  201. sync = 1;
  202. break;
  203. case 'n':
  204. server_name = optarg;
  205. break;
  206. case 'P':
  207. realtime_priority = atoi(optarg);
  208. break;
  209. case 'r':
  210. realtime = 0;
  211. break;
  212. case 'R':
  213. realtime = 1;
  214. break;
  215. case 'T':
  216. temporary = 1;
  217. break;
  218. case 't':
  219. client_timeout = atoi(optarg);
  220. break;
  221. default:
  222. jack_error("unknown option character %c", optopt);
  223. break;
  224. }
  225. }
  226. drivers = jack_drivers_load(drivers);
  227. if (!drivers) {
  228. jack_error("jackdmp: no drivers found; exiting");
  229. goto error;
  230. }
  231. driver_desc = jack_find_driver_descriptor(drivers, master_driver_name);
  232. if (!driver_desc) {
  233. jack_error("jackdmp: unknown master driver '%s'", master_driver_name);
  234. goto error;
  235. }
  236. if (optind < argc) {
  237. driver_nargs = 1 + argc - optind;
  238. } else {
  239. driver_nargs = 1;
  240. }
  241. if (driver_nargs == 0) {
  242. jack_error("No driver specified ... hmm. JACK won't do"
  243. " anything when run like this.");
  244. goto error;
  245. }
  246. master_driver_args = (char**)malloc(sizeof(char*) * driver_nargs);
  247. master_driver_args[0] = master_driver_name;
  248. for (i = 1; i < driver_nargs; i++) {
  249. master_driver_args[i] = argv[optind++];
  250. }
  251. if (jack_parse_driver_params(driver_desc, driver_nargs, master_driver_args, &master_driver_params)) {
  252. goto error;
  253. }
  254. #ifndef WIN32
  255. if (server_name == NULL) {
  256. server_name = (char*)JackTools::DefaultServerName();
  257. }
  258. #endif
  259. rc = jack_register_server(server_name, false);
  260. switch (rc) {
  261. case EEXIST:
  262. jack_error("`%s' server already active", server_name);
  263. goto error;
  264. case ENOSPC:
  265. jack_error("too many servers already active");
  266. goto error;
  267. case ENOMEM:
  268. jack_error("no access to shm registry");
  269. goto error;
  270. default:
  271. jack_info("server `%s' registered", server_name);
  272. }
  273. /* clean up shared memory and files from any previous instance of this server name */
  274. jack_cleanup_shm();
  275. JackTools::CleanupFiles(server_name);
  276. if (!realtime && client_timeout == 0) {
  277. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  278. }
  279. for (i = 0; i < argc; i++) {
  280. free(argv[i]);
  281. }
  282. int res = Start(server_name, driver_desc, master_driver_params, sync, temporary, client_timeout, realtime, realtime_priority, port_max, verbose_aux, clock_source);
  283. if (res < 0) {
  284. jack_error("Cannot start server... exit");
  285. Delete();
  286. jack_cleanup_shm();
  287. JackTools::CleanupFiles(server_name);
  288. jack_unregister_server(server_name);
  289. goto error;
  290. }
  291. // Slave drivers
  292. std::map<std::string, JackDriverInfo*>::iterator it1;
  293. for (it1 = fSlavesList.begin(); it1 != fSlavesList.end(); it1++) {
  294. const char* name = ((*it1).first).c_str();
  295. driver_desc = jack_find_driver_descriptor(drivers, name);
  296. if (!driver_desc) {
  297. jack_error("jackdmp: unknown slave driver '%s'", name);
  298. } else {
  299. (*it1).second = fInstance->AddSlave(driver_desc, NULL);
  300. }
  301. }
  302. // Loopback driver
  303. if (loopback > 0) {
  304. driver_desc = jack_find_driver_descriptor(drivers, "loopback");
  305. if (!driver_desc) {
  306. jack_error("jackdmp: unknown driver '%s'", "loopback");
  307. } else {
  308. fSlavesList["loopback"] = fInstance->AddSlave(driver_desc, NULL);
  309. }
  310. }
  311. // Internal clients
  312. std::map<std::string, int>::iterator it2;
  313. for (it2 = fInternalsList.begin(); it2 != fInternalsList.end(); it2++) {
  314. int status, refnum;
  315. const char* name = ((*it2).first).c_str();
  316. fInstance->InternalClientLoad2(name, name, NULL, JackNullOption, &refnum, -1, &status);
  317. (*it2).second = refnum;
  318. }
  319. }
  320. if (master_driver_params) {
  321. jack_free_driver_params(master_driver_params);
  322. }
  323. return true;
  324. error:
  325. jack_log("JackServerGlobals Init error");
  326. if (master_driver_params) {
  327. jack_free_driver_params(master_driver_params);
  328. }
  329. Destroy();
  330. return false;
  331. }
  332. void JackServerGlobals::Destroy()
  333. {
  334. if (--fUserCount == 0) {
  335. jack_log("JackServerGlobals Destroy");
  336. Stop();
  337. Delete();
  338. jack_cleanup_shm();
  339. JackTools::CleanupFiles(server_name);
  340. jack_unregister_server(server_name);
  341. }
  342. }
  343. } // end of namespace