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.

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