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.

441 lines
14KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 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 <cassert>
  19. #include <csignal>
  20. #include <sys/types.h>
  21. #include <getopt.h>
  22. #include <cstring>
  23. #include <cstdio>
  24. #include "types.h"
  25. #include "jack.h"
  26. #include "JackConstants.h"
  27. #include "JackDriverLoader.h"
  28. #if defined(JACK_DBUS) && defined(__linux__)
  29. #include <dbus/dbus.h>
  30. #include "audio_reserve.h"
  31. #endif
  32. /*
  33. This is a simple port of the old jackdmp.cpp file to use the new Jack 2.0 control API. Available options for the server
  34. are "hard-coded" in the source. A much better approach would be to use the control API to:
  35. - dynamically retrieve available server parameters and then prepare to parse them
  36. - get available drivers and their possible parameters, then prepare to parse them.
  37. */
  38. #ifdef __APPLE__
  39. #include <CoreFoundation/CFNotificationCenter.h>
  40. #include <CoreFoundation/CoreFoundation.h>
  41. static void notify_server_start(const char* server_name)
  42. {
  43. // Send notification to be used in the JackRouter plugin
  44. CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
  45. CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
  46. CFSTR("com.grame.jackserver.start"),
  47. ref,
  48. NULL,
  49. kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
  50. CFRelease(ref);
  51. }
  52. static void notify_server_stop(const char* server_name)
  53. {
  54. // Send notification to be used in the JackRouter plugin
  55. CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
  56. CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
  57. CFSTR("com.grame.jackserver.stop"),
  58. ref1,
  59. NULL,
  60. kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
  61. CFRelease(ref1);
  62. }
  63. #else
  64. static void notify_server_start(const char* server_name)
  65. {}
  66. static void notify_server_stop(const char* server_name)
  67. {}
  68. #endif
  69. static void copyright(FILE* file)
  70. {
  71. fprintf(file, "jackdmp " VERSION "\n"
  72. "Copyright 2001-2005 Paul Davis and others.\n"
  73. "Copyright 2004-2009 Grame.\n"
  74. "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
  75. "This is free software, and you are welcome to redistribute it\n"
  76. "under certain conditions; see the file COPYING for details\n");
  77. }
  78. static void usage(FILE* file)
  79. {
  80. fprintf(file, "\n"
  81. "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  82. " [ --name OR -n server-name ]\n"
  83. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  84. " [ --midi OR -X midi-driver ]\n"
  85. " [ --verbose OR -v ]\n"
  86. #ifdef __linux__
  87. " [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
  88. #endif
  89. " [ --replace-registry OR -r ]\n"
  90. " [ --silent OR -s ]\n"
  91. " [ --sync OR -S ]\n"
  92. " [ --temporary OR -T ]\n"
  93. " [ --version OR -V ]\n"
  94. " -d audio-driver [ ... driver args ... ]\n"
  95. " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
  96. " jackdmp -d driver --help\n"
  97. " to display options for each driver\n\n");
  98. }
  99. // To put in the control.h interface??
  100. static jackctl_driver_t *
  101. jackctl_server_get_driver(
  102. jackctl_server_t *server,
  103. const char *driver_name)
  104. {
  105. const JSList * node_ptr;
  106. node_ptr = jackctl_server_get_drivers_list(server);
  107. while (node_ptr)
  108. {
  109. if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0)
  110. {
  111. return (jackctl_driver_t *)node_ptr->data;
  112. }
  113. node_ptr = jack_slist_next(node_ptr);
  114. }
  115. return NULL;
  116. }
  117. static jackctl_parameter_t *
  118. jackctl_get_parameter(
  119. const JSList * parameters_list,
  120. const char * parameter_name)
  121. {
  122. while (parameters_list)
  123. {
  124. if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0)
  125. {
  126. return (jackctl_parameter_t *)parameters_list->data;
  127. }
  128. parameters_list = jack_slist_next(parameters_list);
  129. }
  130. return NULL;
  131. }
  132. int main(int argc, char* argv[])
  133. {
  134. jackctl_server_t * server_ctl;
  135. const JSList * server_parameters;
  136. const char* server_name = "default";
  137. jackctl_driver_t * audio_driver_ctl;
  138. jackctl_driver_t * midi_driver_ctl;
  139. #ifdef __linux__
  140. const char *options = "-ad:X:P:uvrshVRL:STFl:t:mn:p:c:";
  141. #else
  142. const char *options = "-ad:X:P:uvrshVRL:STFl:t:mn:p:";
  143. #endif
  144. struct option long_options[] = {
  145. #ifdef __linux__
  146. { "clock-source", 1, 0, 'c' },
  147. #endif
  148. { "audio-driver", 1, 0, 'd' },
  149. { "midi-driver", 1, 0, 'X' },
  150. { "verbose", 0, 0, 'v' },
  151. { "help", 0, 0, 'h' },
  152. { "port-max", 1, 0, 'p' },
  153. { "no-mlock", 0, 0, 'm' },
  154. { "name", 0, 0, 'n' },
  155. { "unlock", 0, 0, 'u' },
  156. { "realtime", 0, 0, 'R' },
  157. { "replace-registry", 0, 0, 'r' },
  158. { "loopback", 0, 0, 'L' },
  159. { "realtime-priority", 1, 0, 'P' },
  160. { "timeout", 1, 0, 't' },
  161. { "temporary", 0, 0, 'T' },
  162. { "version", 0, 0, 'V' },
  163. { "silent", 0, 0, 's' },
  164. { "sync", 0, 0, 'S' },
  165. { 0, 0, 0, 0 }
  166. };
  167. int i,opt = 0;
  168. int option_index = 0;
  169. bool seen_audio_driver = false;
  170. bool seen_midi_driver = false;
  171. char *audio_driver_name = NULL;
  172. char **audio_driver_args = NULL;
  173. int audio_driver_nargs = 1;
  174. char *midi_driver_name = NULL;
  175. char **midi_driver_args = NULL;
  176. int midi_driver_nargs = 1;
  177. int port_max = 512;
  178. int do_mlock = 1;
  179. int do_unlock = 0;
  180. bool show_version = false;
  181. sigset_t signals;
  182. jackctl_parameter_t* param;
  183. union jackctl_parameter_value value;
  184. copyright(stdout);
  185. #if defined(JACK_DBUS) && defined(__linux__)
  186. server_ctl = jackctl_server_create(audio_acquire, audio_release);
  187. #else
  188. server_ctl = jackctl_server_create(NULL, NULL);
  189. #endif
  190. if (server_ctl == NULL) {
  191. fprintf(stderr, "Failed to create server object\n");
  192. return -1;
  193. }
  194. server_parameters = jackctl_server_get_parameters(server_ctl);
  195. opterr = 0;
  196. while (!seen_audio_driver &&
  197. (opt = getopt_long(argc, argv, options,
  198. long_options, &option_index)) != EOF) {
  199. switch (opt) {
  200. #ifdef __linux__
  201. case 'c':
  202. param = jackctl_get_parameter(server_parameters, "clock-source");
  203. if (param != NULL) {
  204. if (tolower (optarg[0]) == 'h') {
  205. value.ui = JACK_TIMER_HPET;
  206. jackctl_parameter_set_value(param, &value);
  207. } else if (tolower (optarg[0]) == 'c') {
  208. value.ui = JACK_TIMER_CYCLE_COUNTER;
  209. jackctl_parameter_set_value(param, &value);
  210. } else if (tolower (optarg[0]) == 's') {
  211. value.ui = JACK_TIMER_SYSTEM_CLOCK;
  212. jackctl_parameter_set_value(param, &value);
  213. } else {
  214. usage(stdout);
  215. goto fail_free;
  216. }
  217. }
  218. break;
  219. #endif
  220. case 'd':
  221. seen_audio_driver = true;
  222. audio_driver_name = optarg;
  223. break;
  224. case 'X':
  225. seen_midi_driver = true;
  226. midi_driver_name = optarg;
  227. break;
  228. case 'p':
  229. port_max = (unsigned int)atol(optarg);
  230. break;
  231. case 'm':
  232. do_mlock = 0;
  233. break;
  234. case 'u':
  235. do_unlock = 1;
  236. break;
  237. case 'v':
  238. param = jackctl_get_parameter(server_parameters, "verbose");
  239. if (param != NULL) {
  240. value.b = true;
  241. jackctl_parameter_set_value(param, &value);
  242. }
  243. break;
  244. case 's':
  245. jack_set_error_function(silent_jack_error_callback);
  246. break;
  247. case 'S':
  248. param = jackctl_get_parameter(server_parameters, "sync");
  249. if (param != NULL) {
  250. value.b = true;
  251. jackctl_parameter_set_value(param, &value);
  252. }
  253. break;
  254. case 'n':
  255. server_name = optarg;
  256. param = jackctl_get_parameter(server_parameters, "name");
  257. if (param != NULL) {
  258. strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
  259. jackctl_parameter_set_value(param, &value);
  260. }
  261. break;
  262. case 'P':
  263. param = jackctl_get_parameter(server_parameters, "realtime-priority");
  264. if (param != NULL) {
  265. value.i = atoi(optarg);
  266. jackctl_parameter_set_value(param, &value);
  267. }
  268. break;
  269. case 'r':
  270. param = jackctl_get_parameter(server_parameters, "replace-registry");
  271. if (param != NULL) {
  272. value.b = true;
  273. jackctl_parameter_set_value(param, &value);
  274. }
  275. break;
  276. case 'R':
  277. param = jackctl_get_parameter(server_parameters, "realtime");
  278. if (param != NULL) {
  279. value.b = true;
  280. jackctl_parameter_set_value(param, &value);
  281. }
  282. break;
  283. case 'L':
  284. param = jackctl_get_parameter(server_parameters, "loopback-ports");
  285. if (param != NULL) {
  286. value.ui = atoi(optarg);
  287. jackctl_parameter_set_value(param, &value);
  288. }
  289. break;
  290. case 'T':
  291. param = jackctl_get_parameter(server_parameters, "temporary");
  292. if (param != NULL) {
  293. value.b = true;
  294. jackctl_parameter_set_value(param, &value);
  295. }
  296. break;
  297. case 't':
  298. param = jackctl_get_parameter(server_parameters, "client-timeout");
  299. if (param != NULL) {
  300. value.i = atoi(optarg);
  301. jackctl_parameter_set_value(param, &value);
  302. }
  303. break;
  304. case 'V':
  305. show_version = true;
  306. break;
  307. default:
  308. fprintf(stderr, "unknown option character %c\n", optopt);
  309. /*fallthru*/
  310. case 'h':
  311. usage(stdout);
  312. goto fail_free;
  313. }
  314. }
  315. if (show_version) {
  316. printf( "jackdmp version " VERSION
  317. " tmpdir " jack_server_dir
  318. " protocol %d"
  319. "\n", JACK_PROTOCOL_VERSION);
  320. return -1;
  321. }
  322. if (!seen_audio_driver) {
  323. usage(stderr);
  324. goto fail_free;
  325. }
  326. // Audio driver
  327. audio_driver_ctl = jackctl_server_get_driver(server_ctl, audio_driver_name);
  328. if (audio_driver_ctl == NULL) {
  329. fprintf(stderr, "Unkown driver \"%s\"\n", audio_driver_name);
  330. goto fail_free;
  331. }
  332. if (optind < argc) {
  333. audio_driver_nargs = 1 + argc - optind;
  334. } else {
  335. audio_driver_nargs = 1;
  336. }
  337. if (audio_driver_nargs == 0) {
  338. fprintf(stderr, "No driver specified ... hmm. JACK won't do"
  339. " anything when run like this.\n");
  340. goto fail_free;
  341. }
  342. audio_driver_args = (char **) malloc(sizeof(char *) * audio_driver_nargs);
  343. audio_driver_args[0] = audio_driver_name;
  344. for (i = 1; i < audio_driver_nargs; i++) {
  345. audio_driver_args[i] = argv[optind++];
  346. }
  347. if (jackctl_parse_driver_params(audio_driver_ctl, audio_driver_nargs, audio_driver_args)) {
  348. goto fail_free;
  349. }
  350. // Start server
  351. if (!jackctl_server_start(server_ctl, audio_driver_ctl)) {
  352. fprintf(stderr, "Failed to start server\n");
  353. goto fail_free;
  354. }
  355. // MIDI driver
  356. if (seen_midi_driver) {
  357. midi_driver_ctl = jackctl_server_get_driver(server_ctl, midi_driver_name);
  358. if (midi_driver_ctl == NULL) {
  359. fprintf(stderr, "Unkown driver \"%s\"\n", midi_driver_name);
  360. goto fail_free;
  361. }
  362. jackctl_server_add_slave(server_ctl, midi_driver_ctl);
  363. }
  364. notify_server_start(server_name);
  365. // Waits for signal
  366. signals = jackctl_setup_signals(0);
  367. jackctl_wait_signals(signals);
  368. if (!jackctl_server_stop(server_ctl))
  369. fprintf(stderr, "Cannot stop server...\n");
  370. fail_free:
  371. jackctl_server_destroy(server_ctl);
  372. notify_server_stop(server_name);
  373. return 1;
  374. }