Jackdmp.cpp 12.5 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Copyright (C) 2001 Paul Davis 
Copyright (C) 2004-2006 Grame

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>
#include <signal.h>
#include <pwd.h>
#include <sys/types.h>
#include <dirent.h>
#include <getopt.h>

#include "JackServer.h"
#include "JackConstants.h"
#include "driver_interface.h"
#include "driver_parse.h"
#include "JackDriverLoader.h"
#include "jslist.h"
#include "JackError.h"
36
#include "JackTools.h"
sletz's avatar
sletz committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "shm.h"
#include "jack.h"

using namespace Jack;

static JackServer* fServer;
static char* server_name = NULL;
static int realtime_priority = 10;
static int do_mlock = 1;
static unsigned int port_max = 128;
static int realtime = 0;
static int loopback = 0;
static int temporary = 0;
static int client_timeout = 0; /* msecs; if zero, use period size. */
static int do_unlock = 0;
static JSList* drivers = NULL;
static sigset_t signals;

sletz's avatar
sletz committed
55
static void silent_jack_error_callback(const char *desc)
sletz's avatar
sletz committed
56
57
58
59
{}

static void copyright(FILE* file)
{
sletz's avatar
sletz committed
60
    fprintf(file, "jackdmp " VERSION "\n"
sletz's avatar
sletz committed
61
             "Copyright 2001-2005 Paul Davis and others.\n"
sletz's avatar
sletz committed
62
             "Copyright 2004-2007 Grame.\n"
sletz's avatar
sletz committed
63
64
65
66
67
             "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
68
static void usage(FILE* file)
sletz's avatar
sletz committed
69
{
sletz's avatar
sletz committed
70
71
    copyright(file);
    fprintf(file, "\n"
sletz's avatar
sletz committed
72
             "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
73
			 "               [ --name OR -n server-name ]\n"
sletz's avatar
sletz committed
74
75
76
77
78
79
             // "               [ --no-mlock OR -m ]\n"
             // "               [ --unlock OR -u ]\n"
             "               [ --timeout OR -t client-timeout-in-msecs ]\n"
             "               [ --loopback OR -L loopback-port-number ]\n"
             // "               [ --port-max OR -p maximum-number-of-ports]\n"
             "               [ --verbose OR -v ]\n"
80
			 "               [ --replace-registry OR -r ]\n"
sletz's avatar
sletz committed
81
82
             "               [ --silent OR -s ]\n"
             "               [ --sync OR -S ]\n"
sletz's avatar
Typo    
sletz committed
83
			 "               [ --temporary OR -T ]\n"
sletz's avatar
sletz committed
84
85
             "               [ --version OR -V ]\n"
             "         -d driver [ ... driver args ... ]\n"
sletz's avatar
sletz committed
86
             "             where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
sletz's avatar
sletz committed
87
88
89
90
91
92
93
94
95
96
97
98
             "       jackdmp -d driver --help\n"
             "             to display options for each driver\n\n");
}


static void DoNothingHandler(int sig)
{
    /* this is used by the child (active) process, but it never
       gets called unless we are already shutting down after
       another signal.
    */
    char buf[64];
sletz's avatar
sletz committed
99
    snprintf(buf, sizeof(buf), "received signal %d during shutdown(ignored)\n", sig);
sletz's avatar
sletz committed
100
101
102
    write(1, buf, strlen(buf));
}

103
static int JackStart(const char* server_name, jack_driver_desc_t* driver_desc, JSList* driver_params, int sync, int temporary, int time_out_ms, int rt, int priority, int loopback, int verbose)
sletz's avatar
sletz committed
104
105
{
    JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
106
    fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose, server_name);
sletz's avatar
sletz committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    int res = fServer->Open(driver_desc, driver_params);
    return (res < 0) ? res : fServer->Start();
}

static int JackStop()
{
    fServer->Stop();
    fServer->Close();
    JackLog("Jackdmp: server close\n");
    delete fServer;
    JackLog("Jackdmp: delete server\n");
    return 0;
}

static int JackDelete()
{
    delete fServer;
    JackLog("Jackdmp: delete server\n");
    return 0;
}

static void FilterSIGPIPE()
{
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGPIPE);
    //sigprocmask(SIG_BLOCK, &set, 0);
    pthread_sigmask(SIG_BLOCK, &set, 0);
}

int main(int argc, char* argv[])
{
    int sig;
    sigset_t allsignals;
    struct sigaction action;
    int waiting;

    jack_driver_desc_t* driver_desc;
145
    const char *options = "-ad:P:uvrshVRL:STFl:t:mn:p:";
sletz's avatar
sletz committed
146
    struct option long_options[] = {
147
                                       { "driver", 1, 0, 'd' },
sletz's avatar
sletz committed
148
149
150
151
152
153
154
                                       { "verbose", 0, 0, 'v' },
                                       { "help", 0, 0, 'h' },
                                       { "port-max", 1, 0, 'p' },
                                       { "no-mlock", 0, 0, 'm' },
                                       { "name", 0, 0, 'n' },
                                       { "unlock", 0, 0, 'u' },
                                       { "realtime", 0, 0, 'R' },
155
									   { "replace-registry", 0, 0, 'r' },
sletz's avatar
sletz committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
                                       { "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 }
                                   };
    int opt = 0;
    int option_index = 0;
    int seen_driver = 0;
    char *driver_name = NULL;
    char **driver_args = NULL;
    JSList* driver_params;
    int driver_nargs = 1;
    int show_version = 0;
173
	int replace_registry = 0;
sletz's avatar
sletz committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    int sync = 0;
    int rc, i;

    opterr = 0;
    while (!seen_driver &&
            (opt = getopt_long(argc, argv, options,
                               long_options, &option_index)) != EOF) {
        switch (opt) {

            case 'd':
                seen_driver = 1;
                driver_name = optarg;
                break;

            case 'v':
sletz's avatar
sletz committed
189
                jack_verbose = 1;
sletz's avatar
sletz committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
                break;

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

            case 'S':
                sync = 1;
                break;

            case 'n':
                server_name = optarg;
                break;

            case 'm':
                do_mlock = 0;
                break;

            case 'p':
                port_max = (unsigned int)atol(optarg);
                break;

            case 'P':
                realtime_priority = atoi(optarg);
                break;
215
216
217
218
219
			
			case 'r':
				replace_registry = 1;
				break;
				
sletz's avatar
sletz committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
            case 'R':
                realtime = 1;
                break;

            case 'L':
                loopback = atoi(optarg);
                break;

            case 'T':
                temporary = 1;
                break;

            case 't':
                client_timeout = atoi(optarg);
                break;

            case 'u':
                do_unlock = 1;
                break;

            case 'V':
                show_version = 1;
                break;

            default:
                fprintf(stderr, "unknown option character %c\n",
                        optopt);
                /*fallthru*/
            case 'h':
                usage(stdout);
                return -1;
        }
    }

    /*
    if (show_version) {
    	printf ( "jackd version " VERSION 
    			" tmpdir " DEFAULT_TMP_DIR 
    			" protocol " PROTOCOL_VERSION
    			"\n");
    	return -1;
    }
    */

    if (!seen_driver) {
sletz's avatar
sletz committed
265
266
        usage(stderr);
        exit(1);
sletz's avatar
sletz committed
267
268
    }

sletz's avatar
sletz committed
269
    drivers = jack_drivers_load(drivers);
sletz's avatar
sletz committed
270
    if (!drivers) {
sletz's avatar
sletz committed
271
272
        fprintf(stderr, "jackdmp: no drivers found; exiting\n");
        exit(1);
sletz's avatar
sletz committed
273
274
    }

sletz's avatar
sletz committed
275
    driver_desc = jack_find_driver_descriptor(drivers, driver_name);
sletz's avatar
sletz committed
276
    if (!driver_desc) {
sletz's avatar
sletz committed
277
278
        fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
        exit(1);
sletz's avatar
sletz committed
279
280
281
282
283
284
285
286
287
    }

    if (optind < argc) {
        driver_nargs = 1 + argc - optind;
    } else {
        driver_nargs = 1;
    }

    if (driver_nargs == 0) {
sletz's avatar
sletz committed
288
        fprintf(stderr, "No driver specified ... hmm. JACK won't do"
sletz's avatar
sletz committed
289
290
291
292
                 " anything when run like this.\n");
        return -1;
    }

sletz's avatar
sletz committed
293
    driver_args = (char **) malloc(sizeof(char *) * driver_nargs);
sletz's avatar
sletz committed
294
295
296
297
298
299
    driver_args[0] = driver_name;

    for (i = 1; i < driver_nargs; i++) {
        driver_args[i] = argv[optind++];
    }

sletz's avatar
sletz committed
300
    if (jack_parse_driver_params(driver_desc, driver_nargs,
sletz's avatar
sletz committed
301
                                  driver_args, &driver_params)) {
sletz's avatar
sletz committed
302
        exit(0);
sletz's avatar
sletz committed
303
304
305
    }

    if (server_name == NULL)
306
        server_name = JackTools::DefaultServerName();
sletz's avatar
sletz committed
307

sletz's avatar
sletz committed
308
    copyright(stdout);
sletz's avatar
sletz committed
309

310
    rc = jack_register_server(server_name, replace_registry);
sletz's avatar
sletz committed
311
312
    switch (rc) {
        case EEXIST:
sletz's avatar
sletz committed
313
314
            fprintf(stderr, "`%s' server already active\n", server_name);
            exit(1);
sletz's avatar
sletz committed
315
        case ENOSPC:
sletz's avatar
sletz committed
316
317
            fprintf(stderr, "too many servers already active\n");
            exit(2);
sletz's avatar
sletz committed
318
        case ENOMEM:
sletz's avatar
sletz committed
319
320
            fprintf(stderr, "no access to shm registry\n");
            exit(3);
sletz's avatar
sletz committed
321
        default:
sletz's avatar
sletz committed
322
            if (jack_verbose)
sletz's avatar
Cleanup    
sletz committed
323
                fprintf(stderr, "server `%s' registered\n", server_name);
sletz's avatar
sletz committed
324
325
326
327
328
    }

    /* clean up shared memory and files from any previous
     * instance of this server name */
    jack_cleanup_shm();
329
    JackTools::CleanupFiles(server_name);
sletz's avatar
sletz committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    sigemptyset(&signals);
    sigaddset(&signals, SIGHUP);
    sigaddset(&signals, SIGINT);
    sigaddset(&signals, SIGQUIT);
    sigaddset(&signals, SIGPIPE);
    sigaddset(&signals, SIGTERM);
    sigaddset(&signals, SIGUSR1);
    sigaddset(&signals, SIGUSR2);

    // all child threads will inherit this mask unless they
    // explicitly reset it

    FilterSIGPIPE();
    pthread_sigmask(SIG_BLOCK, &signals, 0);

    if (!realtime && client_timeout == 0)
        client_timeout = 500; /* 0.5 sec; usable when non realtime. */

351
    int res = JackStart(server_name, driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, jack_verbose);
sletz's avatar
sletz committed
352
353
354
355
356
    if (res < 0) {
        jack_error("Cannot start server... exit");
        JackDelete();
        return 0;
    }
357
358
	
	/*
sletz's avatar
sletz committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
    For testing purpose...
    InternalMetro* client1  = new InternalMetro(1200, 0.4, 20, 80, "metro1");
    InternalMetro* client2  = new InternalMetro(600, 0.4, 20, 150, "metro2");
    InternalMetro* client3  = new InternalMetro(1000, 0.4, 20, 110, "metro3");
    InternalMetro* client4  = new InternalMetro(1200, 0.4, 20, 80, "metro4");
    InternalMetro* client5  = new InternalMetro(1500, 0.4, 20, 60, "metro5");
    InternalMetro* client6  = new InternalMetro(1200, 0.4, 20, 84, "metro6");
    InternalMetro* client7  = new InternalMetro(600, 0.4, 20, 160, "metro7");
    InternalMetro* client8  = new InternalMetro(1000, 0.4, 20, 113, "metro8");
    InternalMetro* client9  = new InternalMetro(1200, 0.4, 20, 84, "metro9");
    InternalMetro* client10  = new InternalMetro(1500, 0.4, 20, 70, "metro10");
    */

    // install a do-nothing handler because otherwise pthreads
    // behaviour is undefined when we enter sigwait.

    sigfillset(&allsignals);
    action.sa_handler = DoNothingHandler;
    action.sa_mask = allsignals;
    action.sa_flags = SA_RESTART | SA_RESETHAND;

    for (i = 1; i < NSIG; i++) {
        if (sigismember(&signals, i)) {
            sigaction(i, &action, 0);
        }
    }

    waiting = TRUE;

    while (waiting) {
        sigwait(&signals, &sig);

        fprintf(stderr, "jack main caught signal %d\n", sig);

        switch (sig) {
            case SIGUSR1:
                //jack_dump_configuration(engine, 1);
                break;
            case SIGUSR2:
                // driver exit
                waiting = FALSE;
                break;
            default:
                waiting = FALSE;
                break;
        }
    }

    if (sig != SIGSEGV) {
        // unblock signals so we can see them during shutdown.
        // this will help prod developers not to lose sight of
        // bugs that cause segfaults etc. during shutdown.
        sigprocmask(SIG_UNBLOCK, &signals, 0);
    }
413
	
414
	JackStop();
sletz's avatar
sletz committed
415
416

    jack_cleanup_shm();
417
    JackTools::CleanupFiles(server_name);
sletz's avatar
sletz committed
418
419
420
421
    jack_unregister_server(server_name);

    return 1;
}