Jackdmp.cpp 16.2 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2001 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <iostream>
#include <assert.h>
23
24
#include <cassert>
#include <csignal>
sletz's avatar
sletz committed
25
26
#include <sys/types.h>
#include <getopt.h>
27
28
29
#include <cstring>
#include <cstdio>

30
#include "types.h"
sletz's avatar
sletz committed
31
#include "jack.h"
sletz's avatar
sletz committed
32
33
#include "JackConstants.h"
#include "JackDriverLoader.h"
sletz's avatar
sletz committed
34

35
#if defined(JACK_DBUS) && defined(__linux__)
36
#include <dbus/dbus.h>
37
38
39
#include "audio_reserve.h"
#endif

sletz's avatar
sletz committed
40
/*
sletz's avatar
sletz committed
41
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
sletz's avatar
sletz committed
42
43
44
45
are "hard-coded" in the source. A much better approach would be to use the control API to:
- dynamically retrieve available server parameters and then prepare to parse them
- get available drivers and their possible parameters, then prepare to parse them.
*/
sletz's avatar
sletz committed
46

47
#ifdef __APPLE__
48
#include <CoreFoundation/CFNotificationCenter.h>
49
#include <CoreFoundation/CoreFoundation.h>
50

sletz's avatar
sletz committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
static void notify_server_start(const char* server_name)
{
    // Send notification to be used in the JackRouter plugin
    CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
    CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
            CFSTR("com.grame.jackserver.start"),
            ref,
            NULL,
            kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
    CFRelease(ref);
}

static void notify_server_stop(const char* server_name)
{
    // Send notification to be used in the JackRouter plugin
sletz's avatar
sletz committed
66
    CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
sletz's avatar
sletz committed
67
68
69
70
71
72
73
    CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
            CFSTR("com.grame.jackserver.stop"),
            ref1,
            NULL,
            kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
    CFRelease(ref1);
}
sletz's avatar
sletz committed
74

sletz's avatar
sletz committed
75
76
77
78
79
80
81
82
#else

static void notify_server_start(const char* server_name)
{}
static void notify_server_stop(const char* server_name)
{}

#endif
sletz's avatar
sletz committed
83
84
85

static void copyright(FILE* file)
{
sletz's avatar
sletz committed
86
    fprintf(file, "jackdmp " VERSION "\n"
sletz's avatar
sletz committed
87
            "Copyright 2001-2005 Paul Davis and others.\n"
88
            "Copyright 2004-2011 Grame.\n"
sletz's avatar
sletz committed
89
90
91
            "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
            "This is free software, and you are welcome to redistribute it\n"
            "under certain conditions; see the file COPYING for details\n");
sletz's avatar
sletz committed
92
93
}

sletz's avatar
sletz committed
94
static void usage(FILE* file)
sletz's avatar
sletz committed
95
{
sletz's avatar
sletz committed
96
    fprintf(file, "\n"
97
98
99
            "usage: jackdmp [ --no-realtime OR -r ]\n"
            "               [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
            "      (the two previous arguments are mutually exclusive. The default is --realtime)\n"
sletz's avatar
sletz committed
100
101
            "               [ --name OR -n server-name ]\n"
            "               [ --timeout OR -t client-timeout-in-msecs ]\n"
102
            "               [ --loopback OR -L loopback-port-number ]\n"
sletz's avatar
sletz committed
103
            "               [ --port-max OR -p maximum-number-of-ports]\n"
sletz's avatar
sletz committed
104
            "               [ --midi OR -X midi-driver ]\n"
sletz's avatar
sletz committed
105
            "               [ --verbose OR -v ]\n"
106
107
108
#ifdef __linux__
            "               [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
#endif
109
            "               [ --replace-registry ]\n"
sletz's avatar
sletz committed
110
111
112
113
            "               [ --silent OR -s ]\n"
            "               [ --sync OR -S ]\n"
            "               [ --temporary OR -T ]\n"
            "               [ --version OR -V ]\n"
114
115
116
            "         -d backend [ ... backend args ... ]\n"
#ifdef __APPLE__
            "               Available backends may include: coreaudio, dummy or net.\n\n"
117
#endif
118
119
#ifdef WIN32
            "               Available backends may include: portaudio, dummy or net.\n\n"
120
#endif
121
#ifdef __linux__
122
            "               Available backends may include: alsa, dummy, freebob, firewire or net\n\n"
123
124
125
126
127
128
#endif
#if defined(__sun__) || defined(sun)
            "               Available backends may include: boomer, oss, dummy or net.\n\n"
#endif
            "       jackdmp -d backend --help\n"
            "             to display options for each backend\n\n");
sletz's avatar
sletz committed
129
130
}

sletz's avatar
sletz committed
131
132
133
134
135
// To put in the control.h interface??
static jackctl_driver_t *
jackctl_server_get_driver(
    jackctl_server_t *server,
    const char *driver_name)
sletz's avatar
sletz committed
136
{
sletz's avatar
sletz committed
137
    const JSList * node_ptr;
sletz's avatar
sletz committed
138

sletz's avatar
sletz committed
139
    node_ptr = jackctl_server_get_drivers_list(server);
sletz's avatar
sletz committed
140

sletz's avatar
sletz committed
141
142
143
144
145
146
    while (node_ptr)
    {
        if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0)
        {
            return (jackctl_driver_t *)node_ptr->data;
        }
sletz's avatar
sletz committed
147

sletz's avatar
sletz committed
148
149
150
151
        node_ptr = jack_slist_next(node_ptr);
    }

    return NULL;
sletz's avatar
sletz committed
152
153
}

sletz's avatar
sletz committed
154
155
156
157
static jackctl_parameter_t *
jackctl_get_parameter(
    const JSList * parameters_list,
    const char * parameter_name)
sletz's avatar
sletz committed
158
{
sletz's avatar
sletz committed
159
160
161
162
163
164
165
166
167
168
169
    while (parameters_list)
    {
        if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0)
        {
            return (jackctl_parameter_t *)parameters_list->data;
        }

        parameters_list = jack_slist_next(parameters_list);
    }

    return NULL;
sletz's avatar
sletz committed
170
171
172
173
}

int main(int argc, char* argv[])
{
sletz's avatar
sletz committed
174
175
176
    jackctl_server_t * server_ctl;
    const JSList * server_parameters;
    const char* server_name = "default";
177
178
    jackctl_driver_t * audio_driver_ctl;
    jackctl_driver_t * midi_driver_ctl;
179
    jackctl_driver_t * loopback_driver_ctl;
180
    int replace_registry = 0;
181

182
    const char *options = "-d:X:P:uvshVrRL:STFl:t:mn:p:"
183
#ifdef __linux__
184
        "c:"
185
#endif
186
        ;
187

sletz's avatar
sletz committed
188
    struct option long_options[] = {
189
190
191
#ifdef __linux__
                                       { "clock-source", 1, 0, 'c' },
#endif
192
                                       { "loopback-driver", 1, 0, 'L' },
193
194
                                       { "audio-driver", 1, 0, 'd' },
                                       { "midi-driver", 1, 0, 'X' },
sletz's avatar
sletz committed
195
196
197
198
                                       { "verbose", 0, 0, 'v' },
                                       { "help", 0, 0, 'h' },
                                       { "port-max", 1, 0, 'p' },
                                       { "no-mlock", 0, 0, 'm' },
sletz's avatar
sletz committed
199
                                       { "name", 1, 0, 'n' },
sletz's avatar
sletz committed
200
201
                                       { "unlock", 0, 0, 'u' },
                                       { "realtime", 0, 0, 'R' },
202
                                       { "no-realtime", 0, 0, 'r' },
203
                                       { "replace-registry", 0, &replace_registry, 0 },
sletz's avatar
sletz committed
204
205
206
207
208
209
210
211
212
                                       { "loopback", 0, 0, 'L' },
                                       { "realtime-priority", 1, 0, 'P' },
                                       { "timeout", 1, 0, 't' },
                                       { "temporary", 0, 0, 'T' },
                                       { "version", 0, 0, 'V' },
                                       { "silent", 0, 0, 's' },
                                       { "sync", 0, 0, 'S' },
                                       { 0, 0, 0, 0 }
                                   };
213

sletz's avatar
sletz committed
214
    int i,opt = 0;
sletz's avatar
sletz committed
215
    int option_index = 0;
216
217
218
219
220
221
222
223
    bool seen_audio_driver = false;
    bool seen_midi_driver = false;
    char *audio_driver_name = NULL;
    char **audio_driver_args = NULL;
    int audio_driver_nargs = 1;
    char *midi_driver_name = NULL;
    char **midi_driver_args = NULL;
    int midi_driver_nargs = 1;
224
    int do_mlock = 1;
225
    int do_unlock = 0;
226
    int loopback = 0;
sletz's avatar
sletz committed
227
228
229
230
    bool show_version = false;
    sigset_t signals;
    jackctl_parameter_t* param;
    union jackctl_parameter_value value;
sletz's avatar
sletz committed
231

nedko's avatar
nedko committed
232
    copyright(stdout);
233
234
235
#if defined(JACK_DBUS) && defined(__linux__)
    server_ctl = jackctl_server_create(audio_acquire, audio_release);
#else
236
    server_ctl = jackctl_server_create(NULL, NULL);
237
#endif
sletz's avatar
sletz committed
238
    if (server_ctl == NULL) {
sletz's avatar
Cleanup    
sletz committed
239
        fprintf(stderr, "Failed to create server object\n");
sletz's avatar
sletz committed
240
241
        return -1;
    }
242

sletz's avatar
sletz committed
243
    server_parameters = jackctl_server_get_parameters(server_ctl);
244

245
246
247
248
249
250
    // Default setting
    param = jackctl_get_parameter(server_parameters, "realtime");
    if (param != NULL) {
        value.b = true;
        jackctl_parameter_set_value(param, &value);
    }
251

sletz's avatar
sletz committed
252
    opterr = 0;
253
    while (!seen_audio_driver &&
sletz's avatar
sletz committed
254
255
256
257
            (opt = getopt_long(argc, argv, options,
                               long_options, &option_index)) != EOF) {
        switch (opt) {

258
        #ifdef __linux__
259
260
261
262
263
264
265
266
267
268
269
270
271
272
            case 'c':
                param = jackctl_get_parameter(server_parameters, "clock-source");
                if (param != NULL) {
                    if (tolower (optarg[0]) == 'h') {
                        value.ui = JACK_TIMER_HPET;
                        jackctl_parameter_set_value(param, &value);
                    } else if (tolower (optarg[0]) == 'c') {
                        value.ui = JACK_TIMER_CYCLE_COUNTER;
                        jackctl_parameter_set_value(param, &value);
                    } else if (tolower (optarg[0]) == 's') {
                        value.ui = JACK_TIMER_SYSTEM_CLOCK;
                        jackctl_parameter_set_value(param, &value);
                    } else {
                        usage(stdout);
273
                        goto fail_free1;
274
275
276
277
278
                    }
                }
                break;
        #endif

sletz's avatar
sletz committed
279
            case 'd':
280
281
282
                seen_audio_driver = true;
                audio_driver_name = optarg;
                break;
283

284
285
286
            case 'L':
                loopback = atoi(optarg);
                break;
sletz's avatar
sletz committed
287

288
289
290
            case 'X':
                seen_midi_driver = true;
                midi_driver_name = optarg;
sletz's avatar
sletz committed
291
                break;
sletz's avatar
sletz committed
292

293
            case 'p':
sletz's avatar
sletz committed
294
295
296
297
298
                param = jackctl_get_parameter(server_parameters, "port-max");
                if (param != NULL) {
                    value.ui = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
299
                break;
sletz's avatar
sletz committed
300

301
302
303
            case 'm':
                do_mlock = 0;
                break;
sletz's avatar
sletz committed
304

305
306
307
            case 'u':
                do_unlock = 1;
                break;
sletz's avatar
sletz committed
308
309

            case 'v':
sletz's avatar
sletz committed
310
311
312
313
314
                param = jackctl_get_parameter(server_parameters, "verbose");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
315
316
317
318
319
320
321
                break;

            case 's':
                jack_set_error_function(silent_jack_error_callback);
                break;

            case 'S':
sletz's avatar
sletz committed
322
323
324
325
326
                param = jackctl_get_parameter(server_parameters, "sync");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
327
328
329
330
                break;

            case 'n':
                server_name = optarg;
sletz's avatar
sletz committed
331
332
333
334
335
                param = jackctl_get_parameter(server_parameters, "name");
                if (param != NULL) {
                    strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
336
337
338
                break;

            case 'P':
sletz's avatar
sletz committed
339
340
341
342
343
                param = jackctl_get_parameter(server_parameters, "realtime-priority");
                if (param != NULL) {
                    value.i = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
344
                break;
345

sletz's avatar
sletz committed
346
            case 'r':
347
                param = jackctl_get_parameter(server_parameters, "realtime");
sletz's avatar
sletz committed
348
                if (param != NULL) {
349
                    value.b = false;
sletz's avatar
sletz committed
350
351
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
352
353
                break;

sletz's avatar
sletz committed
354
            case 'R':
sletz's avatar
sletz committed
355
356
357
358
359
                param = jackctl_get_parameter(server_parameters, "realtime");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
360
361
362
                break;

            case 'T':
sletz's avatar
sletz committed
363
364
365
366
367
                param = jackctl_get_parameter(server_parameters, "temporary");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
368
369
370
                break;

            case 't':
sletz's avatar
sletz committed
371
372
373
374
375
                param = jackctl_get_parameter(server_parameters, "client-timeout");
                if (param != NULL) {
                    value.i = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
376
377
378
                break;

            case 'V':
sletz's avatar
sletz committed
379
                show_version = true;
sletz's avatar
sletz committed
380
381
382
                break;

            default:
sletz's avatar
sletz committed
383
                fprintf(stderr, "unknown option character %c\n", optopt);
sletz's avatar
sletz committed
384
                /*fallthru*/
sletz's avatar
sletz committed
385

sletz's avatar
sletz committed
386
387
            case 'h':
                usage(stdout);
388
                goto fail_free1;
sletz's avatar
sletz committed
389
390
        }
    }
391

392
393
394
395
396
397
    // Long option with no letter so treated separately
    param = jackctl_get_parameter(server_parameters, "replace-registry");
    if (param != NULL) {
        value.b = replace_registry;
        jackctl_parameter_set_value(param, &value);
    }
398

sletz's avatar
sletz committed
399
    if (show_version) {
sletz's avatar
sletz committed
400
        printf( "jackdmp version " VERSION
nedko's avatar
nedko committed
401
402
403
404
                " tmpdir " jack_server_dir
                " protocol %d"
                "\n", JACK_PROTOCOL_VERSION);
        return -1;
sletz's avatar
sletz committed
405
    }
sletz's avatar
sletz committed
406

407
    if (!seen_audio_driver) {
sletz's avatar
sletz committed
408
        usage(stderr);
409
        goto fail_free1;
sletz's avatar
sletz committed
410
    }
sletz's avatar
sletz committed
411

412
413
414
    // Audio driver
    audio_driver_ctl = jackctl_server_get_driver(server_ctl, audio_driver_name);
    if (audio_driver_ctl == NULL) {
415
        fprintf(stderr, "Unknown driver \"%s\"\n", audio_driver_name);
416
        goto fail_free1;
sletz's avatar
sletz committed
417
418
419
    }

    if (optind < argc) {
420
        audio_driver_nargs = 1 + argc - optind;
sletz's avatar
sletz committed
421
    } else {
422
        audio_driver_nargs = 1;
sletz's avatar
sletz committed
423
424
    }

425
    if (audio_driver_nargs == 0) {
sletz's avatar
sletz committed
426
        fprintf(stderr, "No driver specified ... hmm. JACK won't do"
sletz's avatar
sletz committed
427
                " anything when run like this.\n");
428
        goto fail_free1;
sletz's avatar
sletz committed
429
430
    }

431
432
    audio_driver_args = (char **) malloc(sizeof(char *) * audio_driver_nargs);
    audio_driver_args[0] = audio_driver_name;
sletz's avatar
sletz committed
433

434
435
    for (i = 1; i < audio_driver_nargs; i++) {
        audio_driver_args[i] = argv[optind++];
sletz's avatar
sletz committed
436
437
    }

438
    if (jackctl_parse_driver_params(audio_driver_ctl, audio_driver_nargs, audio_driver_args)) {
439
        goto fail_free1;
sletz's avatar
sletz committed
440
    }
sletz's avatar
sletz committed
441

442
443
    // Setup signals then start server
    signals = jackctl_setup_signals(0);
444

445
    if (!jackctl_server_start(server_ctl, audio_driver_ctl)) {
sletz's avatar
Cleanup    
sletz committed
446
        fprintf(stderr, "Failed to start server\n");
447
        goto fail_free1;
sletz's avatar
sletz committed
448
    }
sletz's avatar
sletz committed
449

450
451
    // MIDI driver
    if (seen_midi_driver) {
sletz's avatar
sletz committed
452

453
454
        midi_driver_ctl = jackctl_server_get_driver(server_ctl, midi_driver_name);
        if (midi_driver_ctl == NULL) {
455
            fprintf(stderr, "Unknown driver \"%s\"\n", midi_driver_name);
456
            goto fail_free2;
457
        }
sletz's avatar
sletz committed
458

459
        jackctl_server_add_slave(server_ctl, midi_driver_ctl);
460
    }
461

462
463
464
465
466
467
468
469
470
471
472
473
474
    // Loopback driver
    if (loopback > 0) {
        loopback_driver_ctl = jackctl_server_get_driver(server_ctl, "loopback");
        if (loopback_driver_ctl != NULL) {
            const JSList * loopback_parameters = jackctl_driver_get_parameters(loopback_driver_ctl);
            param = jackctl_get_parameter(loopback_parameters, "channels");
            if (param != NULL) {
                value.ui = loopback;
                jackctl_parameter_set_value(param, &value);
            }
            jackctl_server_add_slave(server_ctl, loopback_driver_ctl);
        }
    }
475

sletz's avatar
sletz committed
476
477
478
479
    notify_server_start(server_name);

    // Waits for signal
    jackctl_wait_signals(signals);
sletz's avatar
sletz committed
480

sletz's avatar
sletz committed
481
    if (!jackctl_server_stop(server_ctl))
sletz's avatar
Cleanup    
sletz committed
482
        fprintf(stderr, "Cannot stop server...\n");
483

484
485
486
    jackctl_server_destroy(server_ctl);
    notify_server_stop(server_name);
    return 0;
sletz's avatar
sletz committed
487

sletz's avatar
sletz committed
488
fail_free1:
489
490
    jackctl_server_destroy(server_ctl);
    return -1;
491

492
493
fail_free2:
    jackctl_server_stop(server_ctl);
sletz's avatar
sletz committed
494
495
    jackctl_server_destroy(server_ctl);
    notify_server_stop(server_name);
496
    return -1;
sletz's avatar
sletz committed
497
}