Merge branch 'master' into develop
[jack2.git] / common / Jackdmp.cpp
blobe0b70c94e92974389b667b1c6826e657634763c2
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2013 Grame
4 Copyright (C) 2016-2019 Grame
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <iostream>
23 #include <assert.h>
24 #include <cassert>
25 #include <csignal>
26 #include <sys/types.h>
27 #include <getopt.h>
28 #include <cstring>
29 #include <cstdio>
30 #include <list>
32 #include "types.h"
33 #include "jack.h"
34 #include "control.h"
35 #include "JackConstants.h"
36 #include "JackPlatformPlug.h"
37 #ifdef __ANDROID__
38 #include "JackControlAPIAndroid.h"
39 #endif
41 #if defined(JACK_DBUS) && defined(__linux__)
42 #include <cstdlib>
43 #include <dbus/dbus.h>
44 #include "audio_reserve.h"
45 #endif
47 #if HAVE_SYSTEMD
48 #include <systemd/sd-daemon.h>
49 #endif
52 This is a simple port of the old jackdmp.cpp file to use the new jack2 control API. Available options for the server
53 are "hard-coded" in the source. A much better approach would be to use the control API to:
54 - dynamically retrieve available server parameters and then prepare to parse them
55 - get available drivers and their possible parameters, then prepare to parse them.
58 #ifdef __APPLE__
59 #include <CoreFoundation/CFNotificationCenter.h>
60 #include <CoreFoundation/CoreFoundation.h>
62 static void notify_server_start(const char* server_name)
64 // Send notification to be used in the JackRouter plugin
65 CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
66 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
67 CFSTR("com.grame.jackserver.start"),
68 ref,
69 NULL,
70 kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
71 CFRelease(ref);
74 static void notify_server_stop(const char* server_name)
76 // Send notification to be used in the JackRouter plugin
77 CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
78 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
79 CFSTR("com.grame.jackserver.stop"),
80 ref1,
81 NULL,
82 kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
83 CFRelease(ref1);
86 #else
88 static void notify_server_start(const char* server_name)
90 static void notify_server_stop(const char* server_name)
93 #endif
95 static void copyright(FILE* file)
97 fprintf(file, "jackdmp " VERSION "\n"
98 "Copyright 2001-2005 Paul Davis and others.\n"
99 "Copyright 2004-2016 Grame.\n"
100 "Copyright 2016-2019 Filipe Coelho.\n"
101 "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
102 "This is free software, and you are welcome to redistribute it\n"
103 "under certain conditions; see the file COPYING for details\n");
106 static jackctl_driver_t * jackctl_server_get_driver(jackctl_server_t *server, const char *driver_name)
108 const JSList * node_ptr = jackctl_server_get_drivers_list(server);
110 while (node_ptr) {
111 if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0) {
112 return (jackctl_driver_t *)node_ptr->data;
114 node_ptr = jack_slist_next(node_ptr);
117 return NULL;
120 static jackctl_internal_t * jackctl_server_get_internal(jackctl_server_t *server, const char *internal_name)
122 const JSList * node_ptr = jackctl_server_get_internals_list(server);
124 while (node_ptr) {
125 if (strcmp(jackctl_internal_get_name((jackctl_internal_t *)node_ptr->data), internal_name) == 0) {
126 return (jackctl_internal_t *)node_ptr->data;
128 node_ptr = jack_slist_next(node_ptr);
131 return NULL;
134 static jackctl_parameter_t * jackctl_get_parameter(const JSList * parameters_list, const char * parameter_name)
136 while (parameters_list) {
137 if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0) {
138 return (jackctl_parameter_t *)parameters_list->data;
140 parameters_list = jack_slist_next(parameters_list);
143 return NULL;
146 #ifdef __ANDROID__
147 static void jackctl_server_switch_master_dummy(jackctl_server_t * server_ctl, char * master_driver_name)
149 static bool is_dummy_driver = false;
150 if(!strcmp(master_driver_name, "dummy")) {
151 return;
153 jackctl_driver_t * driver_ctr;
154 if(is_dummy_driver) {
155 is_dummy_driver = false;
156 driver_ctr = jackctl_server_get_driver(server_ctl, master_driver_name);
157 } else {
158 is_dummy_driver = true;
159 driver_ctr = jackctl_server_get_driver(server_ctl, "dummy");
161 jackctl_server_switch_master(server_ctl, driver_ctr);
163 #endif
165 static void print_server_drivers(jackctl_server_t *server, FILE* file)
167 const JSList * node_ptr = jackctl_server_get_drivers_list(server);
169 fprintf(file, "Available backends:\n");
171 while (node_ptr) {
172 jackctl_driver_t* driver = (jackctl_driver_t *)node_ptr->data;
173 fprintf(file, " %s (%s)\n", jackctl_driver_get_name(driver), (jackctl_driver_get_type(driver) == JackMaster) ? "master" : "slave");
174 node_ptr = jack_slist_next(node_ptr);
176 fprintf(file, "\n");
179 static void print_server_internals(jackctl_server_t *server, FILE* file)
181 const JSList * node_ptr = jackctl_server_get_internals_list(server);
183 fprintf(file, "Available internals:\n");
185 while (node_ptr) {
186 jackctl_internal_t* internal = (jackctl_internal_t *)node_ptr->data;
187 fprintf(file, " %s\n", jackctl_internal_get_name(internal));
188 node_ptr = jack_slist_next(node_ptr);
190 fprintf(file, "\n");
193 static void usage(FILE* file, jackctl_server_t *server, bool full = true)
195 jackctl_parameter_t * param;
196 const JSList * server_parameters;
197 uint32_t i;
198 union jackctl_parameter_value value;
200 fprintf(file, "\n"
201 "Usage: jackdmp [ --no-realtime OR -r ]\n"
202 " [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
203 " (the two previous arguments are mutually exclusive. The default is --realtime)\n"
204 " [ --name OR -n server-name ]\n"
205 " [ --timeout OR -t client-timeout-in-msecs ]\n"
206 " [ --loopback OR -L loopback-port-number ]\n"
207 " [ --port-max OR -p maximum-number-of-ports]\n"
208 " [ --slave-backend OR -X slave-backend-name ]\n"
209 " [ --internal-client OR -I internal-client-name ]\n"
210 " [ --internal-session-file OR -C internal-session-file ]\n"
211 " [ --verbose OR -v ]\n"
212 #ifdef __linux__
213 " [ --clocksource OR -c [ h(pet) | s(ystem) ]\n"
214 #endif
215 " [ --autoconnect OR -a <modechar>]\n");
217 server_parameters = jackctl_server_get_parameters(server);
218 param = jackctl_get_parameter(server_parameters, "self-connect-mode");
219 fprintf(file,
220 " where <modechar> is one of:\n");
221 for (i = 0; i < jackctl_parameter_get_enum_constraints_count(param); i++)
223 value = jackctl_parameter_get_enum_constraint_value(param, i);
224 fprintf(file, " '%c' - %s", value.c, jackctl_parameter_get_enum_constraint_description(param, i));
225 if (value.c == JACK_DEFAULT_SELF_CONNECT_MODE)
227 fprintf(file, " (default)");
229 fprintf(file, "\n");
232 fprintf(file,
233 " [ --replace-registry ]\n"
234 " [ --silent OR -s ]\n"
235 " [ --sync OR -S ]\n"
236 " [ --temporary OR -T ]\n"
237 " [ --version OR -V ]\n"
238 " -d master-backend-name [ ... master-backend args ... ]\n"
239 " jackdmp -d master-backend-name --help\n"
240 " to display options for each master backend\n\n");
242 if (full) {
243 print_server_drivers(server, file);
244 print_server_internals(server, file);
248 // Prototype to be found in libjackserver
249 extern "C" void silent_jack_error_callback(const char *desc);
251 void print_version()
253 printf( "jackdmp version " VERSION " tmpdir "
254 jack_server_dir " protocol %d" "\n",
255 JACK_PROTOCOL_VERSION);
256 exit(-1);
260 int main(int argc, char** argv)
262 jackctl_server_t * server_ctl;
263 const JSList * server_parameters;
264 const char* server_name = JACK_DEFAULT_SERVER_NAME;
265 jackctl_driver_t * master_driver_ctl;
266 jackctl_driver_t * loopback_driver_ctl = NULL;
267 int replace_registry = 0;
269 for(int a = 1; a < argc; ++a) {
270 if( !strcmp(argv[a], "--version") || !strcmp(argv[a], "-V") ) {
271 print_version();
274 const char *options = "-d:X:I:P:uvshrRL:STFl:t:mn:p:C:"
275 "a:"
276 #ifdef __linux__
277 "c:"
278 #endif
281 struct option long_options[] = {
282 #ifdef __linux__
283 { "clock-source", 1, 0, 'c' },
284 #endif
285 { "internal-session-file", 1, 0, 'C' },
286 { "loopback-driver", 1, 0, 'L' },
287 { "audio-driver", 1, 0, 'd' },
288 { "midi-driver", 1, 0, 'X' },
289 { "internal-client", 1, 0, 'I' },
290 { "verbose", 0, 0, 'v' },
291 { "help", 0, 0, 'h' },
292 { "port-max", 1, 0, 'p' },
293 { "no-mlock", 0, 0, 'm' },
294 { "name", 1, 0, 'n' },
295 { "unlock", 0, 0, 'u' },
296 { "realtime", 0, 0, 'R' },
297 { "no-realtime", 0, 0, 'r' },
298 { "replace-registry", 0, &replace_registry, 0 },
299 { "loopback", 0, 0, 'L' },
300 { "realtime-priority", 1, 0, 'P' },
301 { "timeout", 1, 0, 't' },
302 { "temporary", 0, 0, 'T' },
303 { "silent", 0, 0, 's' },
304 { "sync", 0, 0, 'S' },
305 { "autoconnect", 1, 0, 'a' },
306 { 0, 0, 0, 0 }
309 int i,opt = 0;
310 int option_index = 0;
311 char* internal_session_file = NULL;
312 char* master_driver_name = NULL;
313 char** master_driver_args = NULL;
314 int master_driver_nargs = 1;
315 int loopback = 0;
316 jackctl_sigmask_t * sigmask;
317 jackctl_parameter_t* param;
318 union jackctl_parameter_value value;
320 std::list<char*> internals_list;
321 std::list<char*> slaves_list;
322 std::list<char*>::iterator it;
324 // Assume that we fail.
325 int return_value = -1;
326 bool notify_sent = false;
328 copyright(stdout);
329 #if defined(JACK_DBUS) && defined(__linux__)
330 if (getenv("JACK_NO_AUDIO_RESERVATION"))
331 server_ctl = jackctl_server_create(NULL, NULL);
332 else
333 server_ctl = jackctl_server_create(audio_acquire, audio_release);
334 #else
335 server_ctl = jackctl_server_create(NULL, NULL);
336 #endif
337 if (server_ctl == NULL) {
338 fprintf(stderr, "Failed to create server object\n");
339 return -1;
342 server_parameters = jackctl_server_get_parameters(server_ctl);
344 opterr = 0;
345 while (!master_driver_name &&
346 (opt = getopt_long(argc, argv, options,
347 long_options, &option_index)) != EOF) {
348 switch (opt) {
350 #ifdef __linux__
351 case 'c':
352 param = jackctl_get_parameter(server_parameters, "clock-source");
353 if (param != NULL) {
354 if (tolower (optarg[0]) == 'h') {
355 value.ui = JACK_TIMER_HPET;
356 jackctl_parameter_set_value(param, &value);
357 } else if (tolower (optarg[0]) == 'c') {
358 /* For backwards compatibility with scripts, allow
359 * the user to request the cycle clock on the
360 * command line, but use the system clock instead
362 value.ui = JACK_TIMER_SYSTEM_CLOCK;
363 jackctl_parameter_set_value(param, &value);
364 } else if (tolower (optarg[0]) == 's') {
365 value.ui = JACK_TIMER_SYSTEM_CLOCK;
366 jackctl_parameter_set_value(param, &value);
367 } else {
368 usage(stdout, server_ctl);
369 goto destroy_server;
372 break;
373 #endif
375 case 'a':
376 param = jackctl_get_parameter(server_parameters, "self-connect-mode");
377 if (param != NULL) {
378 bool value_valid = false;
379 for (uint32_t k=0; k<jackctl_parameter_get_enum_constraints_count( param ); k++ ) {
380 value = jackctl_parameter_get_enum_constraint_value( param, k );
381 if( value.c == optarg[0] )
382 value_valid = true;
385 if( value_valid ) {
386 value.c = optarg[0];
387 jackctl_parameter_set_value(param, &value);
388 } else {
389 usage(stdout, server_ctl);
390 goto destroy_server;
393 break;
395 case 'd':
396 master_driver_name = optarg;
397 break;
399 case 'L':
400 loopback = atoi(optarg);
401 break;
403 case 'X':
404 slaves_list.push_back(optarg);
405 break;
407 case 'I':
408 internals_list.push_back(optarg);
409 break;
411 case 'p':
412 param = jackctl_get_parameter(server_parameters, "port-max");
413 if (param != NULL) {
414 value.ui = atoi(optarg);
415 jackctl_parameter_set_value(param, &value);
417 break;
419 case 'm':
420 break;
422 case 'u':
423 break;
425 case 'v':
426 param = jackctl_get_parameter(server_parameters, "verbose");
427 if (param != NULL) {
428 value.b = true;
429 jackctl_parameter_set_value(param, &value);
431 break;
433 case 's':
434 jack_set_error_function(silent_jack_error_callback);
435 break;
437 case 'S':
438 param = jackctl_get_parameter(server_parameters, "sync");
439 if (param != NULL) {
440 value.b = true;
441 jackctl_parameter_set_value(param, &value);
443 break;
445 case 'n':
446 server_name = optarg;
447 param = jackctl_get_parameter(server_parameters, "name");
448 if (param != NULL) {
449 strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
450 jackctl_parameter_set_value(param, &value);
452 break;
454 case 'C':
455 internal_session_file = optarg;
456 break;
458 case 'P':
459 param = jackctl_get_parameter(server_parameters, "realtime-priority");
460 if (param != NULL) {
461 value.i = atoi(optarg);
462 jackctl_parameter_set_value(param, &value);
464 break;
466 case 'r':
467 param = jackctl_get_parameter(server_parameters, "realtime");
468 if (param != NULL) {
469 value.b = false;
470 jackctl_parameter_set_value(param, &value);
472 break;
474 case 'R':
475 param = jackctl_get_parameter(server_parameters, "realtime");
476 if (param != NULL) {
477 value.b = true;
478 jackctl_parameter_set_value(param, &value);
480 break;
482 case 'T':
483 param = jackctl_get_parameter(server_parameters, "temporary");
484 if (param != NULL) {
485 value.b = true;
486 jackctl_parameter_set_value(param, &value);
488 break;
490 case 't':
491 param = jackctl_get_parameter(server_parameters, "client-timeout");
492 if (param != NULL) {
493 value.i = atoi(optarg);
494 jackctl_parameter_set_value(param, &value);
496 break;
498 default:
499 fprintf(stderr, "unknown option character %c\n", optopt);
500 /*fallthru*/
502 case 'h':
503 usage(stdout, server_ctl);
504 goto destroy_server;
508 // Long option with no letter so treated separately
509 param = jackctl_get_parameter(server_parameters, "replace-registry");
510 if (param != NULL) {
511 value.b = replace_registry;
512 jackctl_parameter_set_value(param, &value);
515 if (!master_driver_name) {
516 usage(stderr, server_ctl, false);
517 goto destroy_server;
520 // Master driver
521 master_driver_ctl = jackctl_server_get_driver(server_ctl, master_driver_name);
522 if (master_driver_ctl == NULL) {
523 fprintf(stderr, "Unknown driver \"%s\"\n", master_driver_name);
524 goto destroy_server;
527 if (jackctl_driver_get_type(master_driver_ctl) != JackMaster) {
528 fprintf(stderr, "Driver \"%s\" is not a master \n", master_driver_name);
529 goto destroy_server;
532 if (optind < argc) {
533 master_driver_nargs = 1 + argc - optind;
534 } else {
535 master_driver_nargs = 1;
538 if (master_driver_nargs == 0) {
539 fprintf(stderr, "No driver specified ... hmm. JACK won't do"
540 " anything when run like this.\n");
541 goto destroy_server;
544 master_driver_args = (char **) malloc(sizeof(char *) * master_driver_nargs);
545 master_driver_args[0] = master_driver_name;
547 for (i = 1; i < master_driver_nargs; i++) {
548 master_driver_args[i] = argv[optind++];
551 if (jackctl_driver_params_parse(master_driver_ctl, master_driver_nargs, master_driver_args)) {
552 goto destroy_server;
555 // Setup signals
556 sigmask = jackctl_setup_signals(0);
558 // Open server
559 if (! jackctl_server_open(server_ctl, master_driver_ctl)) {
560 fprintf(stderr, "Failed to open server\n");
561 goto destroy_server;
564 // Slave drivers
565 for (it = slaves_list.begin(); it != slaves_list.end(); it++) {
566 jackctl_driver_t * slave_driver_ctl = jackctl_server_get_driver(server_ctl, *it);
567 if (slave_driver_ctl == NULL) {
568 fprintf(stderr, "Unknown driver \"%s\"\n", *it);
569 goto close_server;
571 if (jackctl_driver_get_type(slave_driver_ctl) != JackSlave) {
572 fprintf(stderr, "Driver \"%s\" is not a slave \n", *it);
573 goto close_server;
575 if (!jackctl_server_add_slave(server_ctl, slave_driver_ctl)) {
576 fprintf(stderr, "Driver \"%s\" cannot be loaded\n", *it);
577 goto close_server;
581 // Loopback driver
582 if (loopback > 0) {
583 loopback_driver_ctl = jackctl_server_get_driver(server_ctl, "loopback");
585 if (loopback_driver_ctl != NULL) {
586 const JSList * loopback_parameters = jackctl_driver_get_parameters(loopback_driver_ctl);
587 param = jackctl_get_parameter(loopback_parameters, "channels");
588 if (param != NULL) {
589 value.ui = loopback;
590 jackctl_parameter_set_value(param, &value);
592 if (!jackctl_server_add_slave(server_ctl, loopback_driver_ctl)) {
593 fprintf(stderr, "Driver \"loopback\" cannot be loaded\n");
594 goto close_server;
596 } else {
597 fprintf(stderr, "Driver \"loopback\" not found\n");
598 goto close_server;
602 // Start the server
603 if (!jackctl_server_start(server_ctl)) {
604 fprintf(stderr, "Failed to start server\n");
605 goto close_server;
608 // Internal clients
609 for (it = internals_list.begin(); it != internals_list.end(); it++) {
610 jackctl_internal_t * internal_driver_ctl = jackctl_server_get_internal(server_ctl, *it);
611 if (internal_driver_ctl == NULL) {
612 fprintf(stderr, "Unknown internal \"%s\"\n", *it);
613 goto stop_server;
615 if (!jackctl_server_load_internal(server_ctl, internal_driver_ctl)) {
616 fprintf(stderr, "Internal client \"%s\" cannot be loaded\n", *it);
617 goto stop_server;
621 if (internal_session_file != NULL) {
622 if (!jackctl_server_load_session_file(server_ctl, internal_session_file)) {
623 fprintf(stderr, "Internal session file %s cannot be loaded!\n", internal_session_file);
624 goto stop_server;
628 notify_server_start(server_name);
629 notify_sent = true;
630 return_value = 0;
632 #if HAVE_SYSTEMD
633 sd_notify(0, "READY=1");
634 #endif
636 // Waits for signal
637 #ifdef __ANDROID__
638 //reserve SIGUSR2 signal for switching master driver
639 while(1) {
640 int signal = jackctl_wait_signals_and_return(sigmask);
641 if (signal == SIGUSR2) {
642 jackctl_server_switch_master_dummy(server_ctl, master_driver_name);
643 } else {
644 break;
647 #else
648 jackctl_wait_signals(sigmask);
649 #endif
651 #if HAVE_SYSTEMD
652 sd_notify(0, "STOPPING=1");
653 #endif
655 stop_server:
656 if (!jackctl_server_stop(server_ctl)) {
657 fprintf(stderr, "Cannot stop server...\n");
660 close_server:
661 if (loopback > 0 && loopback_driver_ctl) {
662 jackctl_server_remove_slave(server_ctl, loopback_driver_ctl);
664 // Slave drivers
665 for (it = slaves_list.begin(); it != slaves_list.end(); it++) {
666 jackctl_driver_t * slave_driver_ctl = jackctl_server_get_driver(server_ctl, *it);
667 if (slave_driver_ctl) {
668 jackctl_server_remove_slave(server_ctl, slave_driver_ctl);
672 // Internal clients
673 for (it = internals_list.begin(); it != internals_list.end(); it++) {
674 jackctl_internal_t * internal_driver_ctl = jackctl_server_get_internal(server_ctl, *it);
675 if (internal_driver_ctl) {
676 jackctl_server_unload_internal(server_ctl, internal_driver_ctl);
679 jackctl_server_close(server_ctl);
681 destroy_server:
682 jackctl_server_destroy(server_ctl);
683 if (notify_sent) {
684 notify_server_stop(server_name);
686 return return_value;