Correct netjack2 components help.
[jack2.git] / common / Jackdmp.cpp
blobbefff521ba23290a12d31e2c214a3dd3c22bb94f
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <iostream>
22 #include <assert.h>
23 #include <cassert>
24 #include <csignal>
25 #include <sys/types.h>
26 #include <getopt.h>
27 #include <cstring>
28 #include <cstdio>
29 #include <list>
31 #include "types.h"
32 #include "jack.h"
33 #include "control.h"
35 #include "JackConstants.h"
36 #include "JackPlatformPlug.h"
38 #if defined(JACK_DBUS) && defined(__linux__)
39 #include <dbus/dbus.h>
40 #include "audio_reserve.h"
41 #endif
44 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
45 are "hard-coded" in the source. A much better approach would be to use the control API to:
46 - dynamically retrieve available server parameters and then prepare to parse them
47 - get available drivers and their possible parameters, then prepare to parse them.
50 #ifdef __APPLE__
51 #include <CoreFoundation/CFNotificationCenter.h>
52 #include <CoreFoundation/CoreFoundation.h>
54 static void notify_server_start(const char* server_name)
56 // Send notification to be used in the JackRouter plugin
57 CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
58 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
59 CFSTR("com.grame.jackserver.start"),
60 ref,
61 NULL,
62 kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
63 CFRelease(ref);
66 static void notify_server_stop(const char* server_name)
68 // Send notification to be used in the JackRouter plugin
69 CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
70 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
71 CFSTR("com.grame.jackserver.stop"),
72 ref1,
73 NULL,
74 kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
75 CFRelease(ref1);
78 #else
80 static void notify_server_start(const char* server_name)
82 static void notify_server_stop(const char* server_name)
85 #endif
87 static void copyright(FILE* file)
89 fprintf(file, "jackdmp " VERSION "\n"
90 "Copyright 2001-2005 Paul Davis and others.\n"
91 "Copyright 2004-2012 Grame.\n"
92 "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
93 "This is free software, and you are welcome to redistribute it\n"
94 "under certain conditions; see the file COPYING for details\n");
97 static void usage(FILE* file)
99 fprintf(file, "\n"
100 "usage: jackdmp [ --no-realtime OR -r ]\n"
101 " [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
102 " (the two previous arguments are mutually exclusive. The default is --realtime)\n"
103 " [ --name OR -n server-name ]\n"
104 " [ --timeout OR -t client-timeout-in-msecs ]\n"
105 " [ --loopback OR -L loopback-port-number ]\n"
106 " [ --port-max OR -p maximum-number-of-ports]\n"
107 " [ --slave-backend OR -X slave-backend-name ]\n"
108 " [ --internal-client OR -I internal-client-name ]\n"
109 " [ --verbose OR -v ]\n"
110 #ifdef __linux__
111 " [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
112 #endif
113 " [ --replace-registry ]\n"
114 " [ --silent OR -s ]\n"
115 " [ --sync OR -S ]\n"
116 " [ --temporary OR -T ]\n"
117 " [ --version OR -V ]\n"
118 " -d master-backend-name [ ... master-backend args ... ]\n"
119 #ifdef __APPLE__
120 " Available master backends may include: coreaudio, dummy, net or netone.\n\n"
121 #endif
122 #ifdef WIN32
123 " Available master backends may include: portaudio, dummy, net or netone.\n\n"
124 #endif
125 #ifdef __linux__
126 " Available master backends may include: alsa, dummy, freebob, firewire, net or netone.\n\n"
127 #endif
128 #if defined(__sun__) || defined(sun)
129 " Available master backends may include: boomer, oss, dummy or net.\n\n"
130 #endif
131 " jackdmp -d master-backend-name --help\n"
132 " to display options for each master backend\n\n");
135 // To put in the control.h interface ??
136 static jackctl_driver_t * jackctl_server_get_driver(jackctl_server_t *server, const char *driver_name)
138 const JSList * node_ptr = jackctl_server_get_drivers_list(server);
140 while (node_ptr) {
141 if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0) {
142 return (jackctl_driver_t *)node_ptr->data;
144 node_ptr = jack_slist_next(node_ptr);
147 return NULL;
150 static jackctl_internal_t * jackctl_server_get_internal(jackctl_server_t *server, const char *internal_name)
152 const JSList * node_ptr = jackctl_server_get_internals_list(server);
154 while (node_ptr) {
155 if (strcmp(jackctl_internal_get_name((jackctl_internal_t *)node_ptr->data), internal_name) == 0) {
156 return (jackctl_internal_t *)node_ptr->data;
158 node_ptr = jack_slist_next(node_ptr);
161 return NULL;
164 static jackctl_parameter_t * jackctl_get_parameter(const JSList * parameters_list, const char * parameter_name)
166 while (parameters_list) {
167 if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0) {
168 return (jackctl_parameter_t *)parameters_list->data;
170 parameters_list = jack_slist_next(parameters_list);
173 return NULL;
176 // Prototype to be found in libjackserver
177 extern "C" void silent_jack_error_callback(const char *desc);
179 int main(int argc, char** argv)
181 jackctl_server_t * server_ctl;
182 const JSList * server_parameters;
183 const char* server_name = "default";
184 jackctl_driver_t * master_driver_ctl;
185 jackctl_driver_t * loopback_driver_ctl = NULL;
186 int replace_registry = 0;
188 const char *options = "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
189 #ifdef __linux__
190 "c:"
191 #endif
194 struct option long_options[] = {
195 #ifdef __linux__
196 { "clock-source", 1, 0, 'c' },
197 #endif
198 { "loopback-driver", 1, 0, 'L' },
199 { "audio-driver", 1, 0, 'd' },
200 { "midi-driver", 1, 0, 'X' },
201 { "internal-client", 1, 0, 'I' },
202 { "verbose", 0, 0, 'v' },
203 { "help", 0, 0, 'h' },
204 { "port-max", 1, 0, 'p' },
205 { "no-mlock", 0, 0, 'm' },
206 { "name", 1, 0, 'n' },
207 { "unlock", 0, 0, 'u' },
208 { "realtime", 0, 0, 'R' },
209 { "no-realtime", 0, 0, 'r' },
210 { "replace-registry", 0, &replace_registry, 0 },
211 { "loopback", 0, 0, 'L' },
212 { "realtime-priority", 1, 0, 'P' },
213 { "timeout", 1, 0, 't' },
214 { "temporary", 0, 0, 'T' },
215 { "version", 0, 0, 'V' },
216 { "silent", 0, 0, 's' },
217 { "sync", 0, 0, 'S' },
218 { 0, 0, 0, 0 }
221 int i,opt = 0;
222 int option_index = 0;
223 char* master_driver_name = NULL;
224 char** master_driver_args = NULL;
225 int master_driver_nargs = 1;
226 int do_mlock = 1;
227 int do_unlock = 0;
228 int loopback = 0;
229 bool show_version = false;
230 jackctl_sigmask_t * sigmask;
231 jackctl_parameter_t* param;
232 union jackctl_parameter_value value;
234 std::list<char*> internals_list;
235 std::list<char*> slaves_list;
236 std::list<char*>::iterator it;
238 // Assume that we fail.
239 int return_value = -1;
240 bool notify_sent = false;
242 copyright(stdout);
243 #if defined(JACK_DBUS) && defined(__linux__)
244 server_ctl = jackctl_server_create(audio_acquire, audio_release);
245 #else
246 server_ctl = jackctl_server_create(NULL, NULL);
247 #endif
248 if (server_ctl == NULL) {
249 fprintf(stderr, "Failed to create server object\n");
250 return -1;
253 server_parameters = jackctl_server_get_parameters(server_ctl);
255 opterr = 0;
256 while (!master_driver_name &&
257 (opt = getopt_long(argc, argv, options,
258 long_options, &option_index)) != EOF) {
259 switch (opt) {
261 #ifdef __linux__
262 case 'c':
263 param = jackctl_get_parameter(server_parameters, "clock-source");
264 if (param != NULL) {
265 if (tolower (optarg[0]) == 'h') {
266 value.ui = JACK_TIMER_HPET;
267 jackctl_parameter_set_value(param, &value);
268 } else if (tolower (optarg[0]) == 'c') {
269 value.ui = JACK_TIMER_CYCLE_COUNTER;
270 jackctl_parameter_set_value(param, &value);
271 } else if (tolower (optarg[0]) == 's') {
272 value.ui = JACK_TIMER_SYSTEM_CLOCK;
273 jackctl_parameter_set_value(param, &value);
274 } else {
275 usage(stdout);
276 goto destroy_server;
279 break;
280 #endif
282 case 'd':
283 master_driver_name = optarg;
284 break;
286 case 'L':
287 loopback = atoi(optarg);
288 break;
290 case 'X':
291 slaves_list.push_back(optarg);
292 break;
294 case 'I':
295 internals_list.push_back(optarg);
296 break;
298 case 'p':
299 param = jackctl_get_parameter(server_parameters, "port-max");
300 if (param != NULL) {
301 value.ui = atoi(optarg);
302 jackctl_parameter_set_value(param, &value);
304 break;
306 case 'm':
307 do_mlock = 0;
308 break;
310 case 'u':
311 do_unlock = 1;
312 break;
314 case 'v':
315 param = jackctl_get_parameter(server_parameters, "verbose");
316 if (param != NULL) {
317 value.b = true;
318 jackctl_parameter_set_value(param, &value);
320 break;
322 case 's':
323 jack_set_error_function(silent_jack_error_callback);
324 break;
326 case 'S':
327 param = jackctl_get_parameter(server_parameters, "sync");
328 if (param != NULL) {
329 value.b = true;
330 jackctl_parameter_set_value(param, &value);
332 break;
334 case 'n':
335 server_name = optarg;
336 param = jackctl_get_parameter(server_parameters, "name");
337 if (param != NULL) {
338 strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
339 jackctl_parameter_set_value(param, &value);
341 break;
343 case 'P':
344 param = jackctl_get_parameter(server_parameters, "realtime-priority");
345 if (param != NULL) {
346 value.i = atoi(optarg);
347 jackctl_parameter_set_value(param, &value);
349 break;
351 case 'r':
352 param = jackctl_get_parameter(server_parameters, "realtime");
353 if (param != NULL) {
354 value.b = false;
355 jackctl_parameter_set_value(param, &value);
357 break;
359 case 'R':
360 param = jackctl_get_parameter(server_parameters, "realtime");
361 if (param != NULL) {
362 value.b = true;
363 jackctl_parameter_set_value(param, &value);
365 break;
367 case 'T':
368 param = jackctl_get_parameter(server_parameters, "temporary");
369 if (param != NULL) {
370 value.b = true;
371 jackctl_parameter_set_value(param, &value);
373 break;
375 case 't':
376 param = jackctl_get_parameter(server_parameters, "client-timeout");
377 if (param != NULL) {
378 value.i = atoi(optarg);
379 jackctl_parameter_set_value(param, &value);
381 break;
383 case 'V':
384 show_version = true;
385 break;
387 default:
388 fprintf(stderr, "unknown option character %c\n", optopt);
389 /*fallthru*/
391 case 'h':
392 usage(stdout);
393 goto destroy_server;
397 // Long option with no letter so treated separately
398 param = jackctl_get_parameter(server_parameters, "replace-registry");
399 if (param != NULL) {
400 value.b = replace_registry;
401 jackctl_parameter_set_value(param, &value);
404 if (show_version) {
405 printf( "jackdmp version " VERSION
406 " tmpdir " jack_server_dir
407 " protocol %d"
408 "\n", JACK_PROTOCOL_VERSION);
409 return -1;
412 if (!master_driver_name) {
413 usage(stderr);
414 goto destroy_server;
417 // Master driver
418 master_driver_ctl = jackctl_server_get_driver(server_ctl, master_driver_name);
419 if (master_driver_ctl == NULL) {
420 fprintf(stderr, "Unknown driver \"%s\"\n", master_driver_name);
421 goto destroy_server;
424 if (jackctl_driver_get_type(master_driver_ctl) != JackMaster) {
425 fprintf(stderr, "Driver \"%s\" is not a master \n", master_driver_name);
426 goto destroy_server;
429 if (optind < argc) {
430 master_driver_nargs = 1 + argc - optind;
431 } else {
432 master_driver_nargs = 1;
435 if (master_driver_nargs == 0) {
436 fprintf(stderr, "No driver specified ... hmm. JACK won't do"
437 " anything when run like this.\n");
438 goto destroy_server;
441 master_driver_args = (char **) malloc(sizeof(char *) * master_driver_nargs);
442 master_driver_args[0] = master_driver_name;
444 for (i = 1; i < master_driver_nargs; i++) {
445 master_driver_args[i] = argv[optind++];
448 if (jackctl_driver_params_parse(master_driver_ctl, master_driver_nargs, master_driver_args)) {
449 goto destroy_server;
452 // Setup signals
453 sigmask = jackctl_setup_signals(0);
455 // Open server
456 if (! jackctl_server_open(server_ctl, master_driver_ctl)) {
457 fprintf(stderr, "Failed to open server\n");
458 goto destroy_server;
461 // Slave drivers
462 for (it = slaves_list.begin(); it != slaves_list.end(); it++) {
463 jackctl_driver_t * slave_driver_ctl = jackctl_server_get_driver(server_ctl, *it);
464 if (slave_driver_ctl == NULL) {
465 fprintf(stderr, "Unknown driver \"%s\"\n", *it);
466 goto close_server;
468 if (jackctl_driver_get_type(slave_driver_ctl) != JackSlave) {
469 fprintf(stderr, "Driver \"%s\" is not a slave \n", *it);
470 goto close_server;
472 if (!jackctl_server_add_slave(server_ctl, slave_driver_ctl)) {
473 fprintf(stderr, "Driver \"%s\" cannot be loaded\n", *it);
474 goto close_server;
478 // Loopback driver
479 if (loopback > 0) {
480 loopback_driver_ctl = jackctl_server_get_driver(server_ctl, "loopback");
482 if (loopback_driver_ctl != NULL) {
483 const JSList * loopback_parameters = jackctl_driver_get_parameters(loopback_driver_ctl);
484 param = jackctl_get_parameter(loopback_parameters, "channels");
485 if (param != NULL) {
486 value.ui = loopback;
487 jackctl_parameter_set_value(param, &value);
489 if (!jackctl_server_add_slave(server_ctl, loopback_driver_ctl)) {
490 fprintf(stderr, "Driver \"loopback\" cannot be loaded\n");
491 goto close_server;
493 } else {
494 fprintf(stderr, "Driver \"loopback\" not found\n");
495 goto close_server;
499 // Start the server
500 if (!jackctl_server_start(server_ctl)) {
501 fprintf(stderr, "Failed to start server\n");
502 goto close_server;
505 // Internal clients
506 for (it = internals_list.begin(); it != internals_list.end(); it++) {
507 jackctl_internal_t * internal_driver_ctl = jackctl_server_get_internal(server_ctl, *it);
508 if (internal_driver_ctl == NULL) {
509 fprintf(stderr, "Unknown internal \"%s\"\n", *it);
510 goto stop_server;
512 if (!jackctl_server_load_internal(server_ctl, internal_driver_ctl)) {
513 fprintf(stderr, "Internal client \"%s\" cannot be loaded\n", *it);
514 goto stop_server;
518 notify_server_start(server_name);
519 notify_sent = true;
520 return_value = 0;
522 // Waits for signal
523 jackctl_wait_signals(sigmask);
525 stop_server:
526 if (!jackctl_server_stop(server_ctl)) {
527 fprintf(stderr, "Cannot stop server...\n");
530 close_server:
531 if (loopback > 0 && loopback_driver_ctl) {
532 jackctl_server_remove_slave(server_ctl, loopback_driver_ctl);
534 // Slave drivers
535 for (it = slaves_list.begin(); it != slaves_list.end(); it++) {
536 jackctl_driver_t * slave_driver_ctl = jackctl_server_get_driver(server_ctl, *it);
537 if (slave_driver_ctl)
538 jackctl_server_remove_slave(server_ctl, slave_driver_ctl);
541 // Internal clients
542 for (it = internals_list.begin(); it != internals_list.end(); it++) {
543 jackctl_internal_t * internal_driver_ctl = jackctl_server_get_internal(server_ctl, *it);
544 if (internal_driver_ctl)
545 jackctl_server_unload_internal(server_ctl, internal_driver_ctl);
547 jackctl_server_close(server_ctl);
549 destroy_server:
550 jackctl_server_destroy(server_ctl);
551 if (notify_sent) {
552 notify_server_stop(server_name);
554 return return_value;