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.

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