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.

457 lines
15KB

  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 = "-d:X:P:uvrshVRL:STFl:t:mn:p:c:a:";
  139. #else
  140. const char *options = "-d:X:P:uvrshVRL:STFl:t:mn:p:a:";
  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. { "autoconnect", 1, 0, 'a' },
  164. { 0, 0, 0, 0 }
  165. };
  166. int i,opt = 0;
  167. int option_index = 0;
  168. bool seen_audio_driver = false;
  169. bool seen_midi_driver = false;
  170. char *audio_driver_name = NULL;
  171. char **audio_driver_args = NULL;
  172. int audio_driver_nargs = 1;
  173. char *midi_driver_name = NULL;
  174. char **midi_driver_args = NULL;
  175. int midi_driver_nargs = 1;
  176. int port_max = 512;
  177. int do_mlock = 1;
  178. int do_unlock = 0;
  179. bool show_version = false;
  180. sigset_t signals;
  181. jackctl_parameter_t* param;
  182. union jackctl_parameter_value value;
  183. copyright(stdout);
  184. server_ctl = jackctl_server_create();
  185. if (server_ctl == NULL) {
  186. fprintf(stderr, "Failed to create server object\n");
  187. return -1;
  188. }
  189. server_parameters = jackctl_server_get_parameters(server_ctl);
  190. opterr = 0;
  191. while (!seen_audio_driver &&
  192. (opt = getopt_long(argc, argv, options,
  193. long_options, &option_index)) != EOF) {
  194. switch (opt) {
  195. #ifdef __linux__
  196. case 'c':
  197. param = jackctl_get_parameter(server_parameters, "clock-source");
  198. if (param != NULL) {
  199. if (tolower (optarg[0]) == 'h') {
  200. value.ui = JACK_TIMER_HPET;
  201. jackctl_parameter_set_value(param, &value);
  202. } else if (tolower (optarg[0]) == 'c') {
  203. value.ui = JACK_TIMER_CYCLE_COUNTER;
  204. jackctl_parameter_set_value(param, &value);
  205. } else if (tolower (optarg[0]) == 's') {
  206. value.ui = JACK_TIMER_SYSTEM_CLOCK;
  207. jackctl_parameter_set_value(param, &value);
  208. } else {
  209. usage(stdout);
  210. goto fail_free;
  211. }
  212. }
  213. break;
  214. #endif
  215. case 'a':
  216. param = jackctl_get_parameter(server_parameters, "self-connect-mode");
  217. if (param != NULL) {
  218. bool value_valid = false;
  219. for (int k=0; k<jackctl_parameter_get_enum_constraints_count( param ); k++ ) {
  220. value = jackctl_parameter_get_enum_constraint_value( param, k );
  221. if( value.c == optarg[0] )
  222. value_valid = true;
  223. }
  224. if( value_valid ) {
  225. value.c = optarg[0];
  226. jackctl_parameter_set_value(param, &value);
  227. } else {
  228. usage(stdout);
  229. goto fail_free;
  230. }
  231. }
  232. break;
  233. case 'd':
  234. seen_audio_driver = true;
  235. audio_driver_name = optarg;
  236. break;
  237. case 'X':
  238. seen_midi_driver = true;
  239. midi_driver_name = optarg;
  240. break;
  241. case 'p':
  242. port_max = (unsigned int)atol(optarg);
  243. break;
  244. case 'm':
  245. do_mlock = 0;
  246. break;
  247. case 'u':
  248. do_unlock = 1;
  249. break;
  250. case 'v':
  251. param = jackctl_get_parameter(server_parameters, "verbose");
  252. if (param != NULL) {
  253. value.b = true;
  254. jackctl_parameter_set_value(param, &value);
  255. }
  256. break;
  257. case 's':
  258. jack_set_error_function(silent_jack_error_callback);
  259. break;
  260. case 'S':
  261. param = jackctl_get_parameter(server_parameters, "sync");
  262. if (param != NULL) {
  263. value.b = true;
  264. jackctl_parameter_set_value(param, &value);
  265. }
  266. break;
  267. case 'n':
  268. server_name = optarg;
  269. param = jackctl_get_parameter(server_parameters, "name");
  270. if (param != NULL) {
  271. strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
  272. jackctl_parameter_set_value(param, &value);
  273. }
  274. break;
  275. case 'P':
  276. param = jackctl_get_parameter(server_parameters, "realtime-priority");
  277. if (param != NULL) {
  278. value.i = atoi(optarg);
  279. jackctl_parameter_set_value(param, &value);
  280. }
  281. break;
  282. case 'r':
  283. param = jackctl_get_parameter(server_parameters, "replace-registry");
  284. if (param != NULL) {
  285. value.b = true;
  286. jackctl_parameter_set_value(param, &value);
  287. }
  288. break;
  289. case 'R':
  290. param = jackctl_get_parameter(server_parameters, "realtime");
  291. if (param != NULL) {
  292. value.b = true;
  293. jackctl_parameter_set_value(param, &value);
  294. }
  295. break;
  296. case 'L':
  297. param = jackctl_get_parameter(server_parameters, "loopback-ports");
  298. if (param != NULL) {
  299. value.ui = atoi(optarg);
  300. jackctl_parameter_set_value(param, &value);
  301. }
  302. break;
  303. case 'T':
  304. param = jackctl_get_parameter(server_parameters, "temporary");
  305. if (param != NULL) {
  306. value.b = true;
  307. jackctl_parameter_set_value(param, &value);
  308. }
  309. break;
  310. case 't':
  311. param = jackctl_get_parameter(server_parameters, "client-timeout");
  312. if (param != NULL) {
  313. value.i = atoi(optarg);
  314. jackctl_parameter_set_value(param, &value);
  315. }
  316. break;
  317. case 'V':
  318. show_version = true;
  319. break;
  320. default:
  321. fprintf(stderr, "unknown option character %c\n", optopt);
  322. /*fallthru*/
  323. case 'h':
  324. usage(stdout);
  325. goto fail_free;
  326. }
  327. }
  328. if (show_version) {
  329. printf( "jackdmp version " VERSION
  330. " tmpdir " jack_server_dir
  331. " protocol %d"
  332. "\n", JACK_PROTOCOL_VERSION);
  333. return -1;
  334. }
  335. if (!seen_audio_driver) {
  336. usage(stderr);
  337. goto fail_free;
  338. }
  339. // Audio driver
  340. audio_driver_ctl = jackctl_server_get_driver(server_ctl, audio_driver_name);
  341. if (audio_driver_ctl == NULL) {
  342. fprintf(stderr, "Unkown driver \"%s\"\n", audio_driver_name);
  343. goto fail_free;
  344. }
  345. if (optind < argc) {
  346. audio_driver_nargs = 1 + argc - optind;
  347. } else {
  348. audio_driver_nargs = 1;
  349. }
  350. if (audio_driver_nargs == 0) {
  351. fprintf(stderr, "No driver specified ... hmm. JACK won't do"
  352. " anything when run like this.\n");
  353. goto fail_free;
  354. }
  355. audio_driver_args = (char **) malloc(sizeof(char *) * audio_driver_nargs);
  356. audio_driver_args[0] = audio_driver_name;
  357. for (i = 1; i < audio_driver_nargs; i++) {
  358. audio_driver_args[i] = argv[optind++];
  359. }
  360. if (jackctl_parse_driver_params(audio_driver_ctl, audio_driver_nargs, audio_driver_args)) {
  361. goto fail_free;
  362. }
  363. // Start server
  364. if (!jackctl_server_start(server_ctl, audio_driver_ctl)) {
  365. fprintf(stderr, "Failed to start server\n");
  366. goto fail_free;
  367. }
  368. // MIDI driver
  369. if (seen_midi_driver) {
  370. midi_driver_ctl = jackctl_server_get_driver(server_ctl, midi_driver_name);
  371. if (midi_driver_ctl == NULL) {
  372. fprintf(stderr, "Unkown driver \"%s\"\n", midi_driver_name);
  373. goto fail_free;
  374. }
  375. jackctl_server_add_slave(server_ctl, midi_driver_ctl);
  376. }
  377. notify_server_start(server_name);
  378. // Waits for signal
  379. signals = jackctl_setup_signals(0);
  380. jackctl_wait_signals(signals);
  381. if (!jackctl_server_stop(server_ctl))
  382. fprintf(stderr, "Cannot stop server...\n");
  383. fail_free:
  384. jackctl_server_destroy(server_ctl);
  385. notify_server_stop(server_name);
  386. return 1;
  387. }