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.

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