jack_test.cpp 74.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) 2005 Samuel TRACOL for 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.

*/

/** @file jack_test.c
 *
 * @brief This client test the jack API.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <string.h>
#include <getopt.h>
#include <math.h>
#include <assert.h>
36
#include <stdarg.h>
sletz's avatar
sletz committed
37

sletz's avatar
sletz committed
38
39
#include "jack.h"
#include "transport.h"
sletz's avatar
sletz committed
40

sletz's avatar
sletz committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

#ifdef WIN32
#define M_PI 3.151592653
#endif

#ifdef WIN32
#define jack_sleep(val) Sleep((val))
#else
#define jack_sleep(val) usleep((val) * 1000)
#endif

typedef struct
{
    jack_nframes_t ft;		// running counter frame time
    jack_nframes_t fcs;		// from sycle start...
    jack_nframes_t lft;		// last frame time...
}
FrameTimeCollector;

FILE *file;
FrameTimeCollector* framecollect;
FrameTimeCollector perpetualcollect;
FrameTimeCollector lastperpetualcollect;
int frames_collected = 0;

// ports
jack_port_t *output_port1;
jack_port_t *output_port1b;
jack_port_t *input_port2;
jack_port_t *output_port2;
jack_port_t *input_port1;

// clients
jack_client_t *client1;
jack_client_t *client2;
const char *client_name1;
const char *client_name2;

unsigned long sr;	// sample rate
// for time -t option
int time_to_run = 0;
int time_before_exit = 1;
// standard error count
int t_error = 0;
int reorder = 0;	// graph reorder callback
int RT = 0;			// is real time or not...
int FW = 0;			// freewheel mode
int init_clbk = 0;	// init callback
int i, j, k = 0;
int port_callback_reg = 0;
jack_nframes_t cur_buffer_size, old_buffer_size, cur_pos;
int activated = 0;
int count1, count2 = 0;			// for freewheel
int xrun = 0;
int have_xrun = 0;				// msg to tell the process1 function to write a special thing in the frametime file.
int process1_activated = -1;	// to control processing...
int process2_activated = -1;	// to control processing...
unsigned long int index1 = 0;
unsigned long int index2 = 0;
jack_default_audio_sample_t *signal1;	// signal source d'emission
jack_default_audio_sample_t *signal2;	// tableau de reception
jack_transport_state_t ts;
jack_position_t pos;
jack_position_t request_pos;
int silent_error = 0;	// jack silent mode
int verbose_mode = 0;
int transport_mode = 1;
int ext_latency = 0;	// test latency for PHY devices

int sync_called = 0;
int starting_state = 1;

int linecount = 0;		// line counter for log file of sampleframe counter --> for graph function.
int linebuf = 0;		// reminders for graph analysis
int linetransport = 0;
int linefw = 0;
int lineports = 0;
int linecl2 = 0;

/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 
							Callbacks & basics functions
 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*/

void usage()
{
    fprintf (stderr, "\n\n"
             "usage: jack_test \n"
             "              [ --time OR -t time_to_run (in seconds) ]\n"
             "              [ --quiet OR -q (quiet mode : without jack server errors) ]\n"
             "              [ --verbose OR -v (verbose mode : no details on tests done. Only main results & errors) ]\n"
             "              [ --transport OR -k (Do not test transport functions.) ]\n"
             "                --realtime OR -R (jack is in rt mode)\n\n\n"
            );
    exit(1);
}

void Log(char *fmt, ...)
{
    if (verbose_mode) {
        va_list ap;
        va_start(ap, fmt);
        vfprintf(stderr, fmt, ap);
        va_end(ap);
    }
}

void Collect(FrameTimeCollector* TheFrame)
{
    TheFrame->lft = jack_last_frame_time(client1);
    TheFrame->ft = jack_frame_time(client1);
    TheFrame->fcs = jack_frames_since_cycle_start(client1);
}

void Jack_Thread_Init_Callback(void *arg)
{
    Log("Init callback has been successfully called. (msg from callback)\n");
    init_clbk = 1;
}

void Jack_Freewheel_Callback(int starting, void *arg)
{
sletz's avatar
sletz committed
168
    Log("Freewhell callback has been successfully called with value %i. (msg from callback)\n", starting);
sletz's avatar
sletz committed
169
170
171
    FW = starting;
}

172
173
void Jack_Client_Registration_Callback(const char* name, int val, void *arg)
{
sletz's avatar
sletz committed
174
    Log("Client registration callback name = %s has been successfully called with value %i. (msg from callback)\n", name, val);
175
176
}

sletz's avatar
sletz committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
int Jack_Update_Buffer_Size(jack_nframes_t nframes, void *arg)
{
    cur_buffer_size = jack_get_buffer_size(client1);
    Log("Buffer size = %d (msg from callback)\n", cur_buffer_size);
    return 0;
}

int Jack_XRun_Callback(void *arg)
{
    xrun++;
    have_xrun = 1;
    printf("Xrun has been detected ! (msg from callback)\n");
    return 0;
}

int Jack_Graph_Order_Callback(void *arg)
{
    reorder++;
    return 0;
}

int Jack_Sample_Rate_Callback(jack_nframes_t nframes, void *arg)
{
    Log("Sample rate : %i.\n", nframes);
    return 0;
}

void Jack_Error_Callback(const char *msg)
{
    if (silent_error == 0) {
        fprintf(stderr, "error : %s (msg from callback)\n", msg);
    }
}

211
void jack_shutdown(void *arg)
sletz's avatar
sletz committed
212
213
{
    printf("Jack_test has been kicked out by jackd !\n");
214
    exit(1);
sletz's avatar
sletz committed
215
216
217
218
219
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
}

void Jack_Port_Register(jack_port_id_t port, int mode, void *arg)
{
    port_callback_reg++;
}

int Jack_Sync_Callback(jack_transport_state_t state, jack_position_t *pos, void *arg)
{
    int res = 0;

    switch (state) {

        case JackTransportStarting:
            sync_called++;
            if (starting_state == 0) {
                Log("sync callback : Releasing status : now ready...\n");
                res = 1;
            } else {
                if (sync_called == 1) {
                    Log("sync callback : Holding status...\n");
                }
                res = 0;
            }
            break;

        case JackTransportStopped:
            Log("sync callback : JackTransportStopped...\n");
            res = 0;
            break;

        default:
            res = 0;
            break;
    }

    return res;
}

/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 
							processing functions
 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 * Proccess1 is for client1
 * 4 modes, activated with process1_activated
 *
 * -1 : idle mode
 *  0 : write zeros to output 1
 *  1 : write continuously signal1 (sinusodal test signal) to output1
 *  3 : mode for summation test. While record (done by process2) is not running, write signal1 to both out1 & out1b.
 *      when record begin (index2 > 0), write signal1 in phase opposition to out1 & out2
 *  5 : Frames Time checking mode : write the array containing the three values of frame_time, frames cycles start and
 *      last frame time during 150 cycles.
 */

int process1(jack_nframes_t nframes, void *arg)
{
    if (FW == 0) {
        Collect(&perpetualcollect);
        if (have_xrun) {
            fprintf(file, "%i	%i\n", (perpetualcollect.ft - lastperpetualcollect.ft), (2*cur_buffer_size));
            have_xrun = 0;
        } else {
            fprintf(file, "%i	0\n", (perpetualcollect.ft - lastperpetualcollect.ft));
        }
        linecount++;
        lastperpetualcollect.ft = perpetualcollect.ft;
    }

    jack_default_audio_sample_t *out1;
    jack_default_audio_sample_t *out1b;
    activated++; // counter of callback activation
    if (process1_activated == 1) {
        out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
        for (int p = 0; p < nframes;p++) {
            out1[p] = signal1[index1];
            index1++;
            if (index1 == 48000)
                index1 = 0;
        }
    }
    if (process1_activated == 3) {
        out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
        out1b = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1b, nframes);
        for (int p = 0; p < nframes;p++) {
            out1[p] = signal1[index1];
            if (index2 != 0) {
                out1b[p] = ( -1 * signal1[index1]);
            } else {
                out1b[p] = signal1[index1];
            }
            index1++;
            if (index1 == 48000)
                index1 = 0;
        }
    }
    if (process1_activated == 0) {
        out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
        memset (out1, 0, sizeof (jack_default_audio_sample_t) * nframes); //crit des zros en sortie...
    }
    if (process1_activated == 5) {
        Collect(&framecollect[frames_collected]);
        frames_collected++;
        if (frames_collected > 798) {
            process1_activated = -1;
        }
    }
    return 0;
}

/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 * Proccess2 is for client2
 * 3 modes, activated with process1_activated
 *
 * -1 : idle mode
 *  0 : idle mode
 *  1 : record in2 into signal2.(for first transmit test)
 *  2 : record in2 into signal2 while send signal1 in out2. used dor Tie data test.
 *  3 : record in2 into sigal2 for summation data test.
 * In records modes, at the end of the record (signal2 is full), it stop the test, setting both activation states to -1.
 */

int process2(jack_nframes_t nframes, void *arg)
{
    jack_default_audio_sample_t *out2;
    jack_default_audio_sample_t *in2;

sletz's avatar
sletz committed
348
    if (process2_activated == 1) { // Reception du process1 pour comparer les donnees
sletz's avatar
sletz committed
349
350
351
352
353
354
355
356
357
358
359
360
        in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);
        for (int p = 0; p < nframes;p++) {
            signal2[index2] = in2[p];
            if (index2 == 95999) {
                process2_activated = 0;
                process1_activated = 0;
                //index2 = 0;
            } else {
                index2++;
            }
        }
    }
sletz's avatar
sletz committed
361
	
sletz's avatar
sletz committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
    if (process2_activated == 2) { // envoie de signal1 pour test tie mode et le rcupre direct + latence de la boucle jack...
        out2 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port2, nframes);
        in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);

        for (int p = 0; p < nframes;p++) {
            out2[p] = signal1[index1];
            index1++;
            if (index1 == 48000)
                index1 = 0;
            signal2[index2] = in2[p];
            if (index2 == 95999) {
                process2_activated = -1;
                //index2 = 0;
            } else {
                index2++;
            }
        }
    }
sletz's avatar
sletz committed
380
	
sletz's avatar
sletz committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    if (process2_activated == 3) { // envoie de -signal1 pour sommation en oppo de phase par jack
        in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);

        for (int p = 0; p < nframes;p++) {
            signal2[index2] = in2[p];
            if (index2 == 95999) {
                process2_activated = 0;
                process1_activated = 0;
                //index2 = 0;
            } else {
                index2++;
            }
        }
    }

    return 0;
}

sletz's avatar
sletz committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// To test callback exiting
int process3(jack_nframes_t nframes, void *arg)
{
	static int process3_call = 0;
	
	if (process3_call++ > 10) {
		Log("process3 callback : exiting...\n");
		return -1;
	} else {
		Log("calling process3 callback : process3_call = %ld\n", process3_call);
		return 0;
	}
}

413
414
415
int process4(jack_nframes_t nframes, void *arg)
{
	static jack_nframes_t last_time = jack_frame_time((jack_client_t*)arg);
sletz's avatar
sletz committed
416
	static jack_nframes_t tolerance = (jack_nframes_t)(cur_buffer_size * 0.1f);
417
418
419
420
421
422
423
424
425
426
427
428
429
	
	jack_nframes_t cur_time = jack_frame_time((jack_client_t*)arg);
	jack_nframes_t delta_time = cur_time - last_time;
	
	Log("calling process4 callback : jack_frame_time = %ld delta_time = %ld\n", cur_time, delta_time);
	if (delta_time > 0  && abs(delta_time - cur_buffer_size) > tolerance) {
		printf("!!! ERROR !!! jack_frame_time seems to return incorrect values cur_buffer_size = %ld, delta_time = %ld\n", cur_buffer_size, delta_time);
	}
	
	last_time = cur_time;
	return 0;
}

sletz's avatar
sletz committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
static void display_transport_state()
{
    jack_transport_state_t ts;
    jack_position_t pos;

    ts = jack_transport_query(client2, &pos);
    switch (ts) {
        case JackTransportStopped:
            Log("Transport is stopped...\n");
            break;
        case JackTransportRolling:
            Log("Transport is rolling...\n");
            break;
        case JackTransportLooping:
            Log("Transport is looping...\n");
            break;
        case JackTransportStarting:
            Log("Transport is starting...\n");
            break;
    }
}

/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
							MAIN FUNCTION
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*/
int main (int argc, char *argv[])
{
    const char **inports; // array of PHY input/output
    const char **outports; // array of PHY input/outputs
    const char *server_name = NULL;
    const char **connexions1;
    const char **connexions2;
    jack_status_t status;
    char portname[128] = "port";
    char filename[128] = "framefile.ext";
    char *nullportname = "";
    int option_index;
    int opt;
    int a = 0;			// working number for in/out port (PHY)...
    int test_link = 0;	// for testing the "overconnect" function
    int flag;			// flag for ports...
    int is_mine = 0;	// to test jack_port_is_mine function...
    int connected = 0;	// check connect functions of ports
    const char *options = "kRnqvt:";
    float ratio;		// for speed calculation in freewheel mode
    jack_options_t jack_options = JackNullOption;
    struct option long_options[] = {
                                       {"realtime", 0, 0, 'R'
                                       },
                                       {"non-realtime", 0, 0, 'n'},
                                       {"time", 0, 0, 't'},
                                       {"quiet", 0, 0, 'q'},
                                       {"verbose", 0, 0, 'v'},
                                       {"transport", 0, 0, 'k'},
                                       {0, 0, 0, 0}
                                   };

    client_name1 = "jack_test";
    time_to_run = 1;
    while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
        switch (opt) {
            case 'k':
                transport_mode = 0;
                break;
            case 'q':
                silent_error = 1;
                break;
            case 'v':
                verbose_mode = 1;
                printf("Verbose mode is activated...\n");
                break;
            case 't':
                time_to_run = atoi(optarg);
                break;
            case 'R':
                RT = 1;
                break;
            default:
                fprintf (stderr, "unknown option %c\n", opt);
                usage ();
        }
    }

    if (RT) {
        printf("Jack server is said being in realtime mode...\n");
    } else {
        printf("Jack server is said being in non-realtime mode...\n");
    }
    /**
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    						init signal data for test
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    */
    framecollect = (FrameTimeCollector *) malloc(800 * sizeof(FrameTimeCollector));

    signal1 = (jack_default_audio_sample_t *) malloc(48000 * sizeof(jack_default_audio_sample_t));
    signal2 = (jack_default_audio_sample_t *) malloc(96000 * sizeof(jack_default_audio_sample_t));
    signal1[0] = 0;
    int p;
    for (p = 1; p < 48000;p++) {
        signal1[p] = (sin((p * 2 * M_PI * 1000 ) / 48000));
    }
    for (p = 0; p < 95999;p++) {
        signal2[p] = 0.0 ;
    }
    index1 = 0;
    index2 = 0;
    /**
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    						begin test
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    */
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-* Start jack server stress test  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");

    /**
     * Register a client... 
     * 
     */
sletz's avatar
sletz committed
564
	Log("Register a client using jack_client_open()...\n");
sletz's avatar
sletz committed
565
566
567
568
569
570
571
572
573
574
575
576
    client1 = jack_client_open(client_name1, jack_options, &status, server_name);
    if (client1 == NULL) {
        fprintf (stderr, "jack_client_open() failed, "
                 "status = 0x%2.0x\n", status);
        if (status & JackServerFailed) {
            fprintf(stderr, "Unable to connect to JACK server\n");
        }
        exit (1);
    }
    if (status & JackServerStarted) {
        fprintf(stderr, "JACK server started\n");
    }
577
	
sletz's avatar
sletz committed
578
579
580
581
582
583
584
585
586
587
588
589
    /**
     * try to register another one with the same name...
     *
     */
    Log("trying to register a new jackd client with name %s using jack_client_new()...\n", client_name1);
    client2 = jack_client_new(client_name1);
    if (client2 == NULL) {
        Log ("valid : a second client with the same name cannot be registered\n");
    } else {
        printf("!!! ERROR !!! Jackd server has accepted multiples client with the same name !");
        jack_client_close(client2);
    }
590
591
592
593
594
595
596
597
598
	
	/**
     * try to register another one with the same name using jack_client_open ==> since JackUseExactName is not used, an new client should be opened...
     *
     */
    Log("trying to register a new jackd client with name %s using jack_client_open()...\n", client_name1);
    client2 = jack_client_open(client_name1, jack_options, &status, server_name);
    if (client2 != NULL) {
        Log ("valid : a second client with the same name can be registered (client automatic renaming)\n");
599
		jack_client_close(client2);
600
601
602
    } else {
        printf("!!! ERROR !!! Jackd server automatic renaming feature does not work!");
    }
sletz's avatar
sletz committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618

    /**
     * testing client name...
     * Verify that the name sended at registration and the one returned by jack server is the same...
     *
     */
    Log("Testing name...");
    client_name2 = jack_get_client_name(client1);
    if (strcmp(client_name1, client_name2) == 0)
        Log(" ok\n");
    else
        printf("\n!!! ERROR !!! name returned different from the one given : %s\n", client_name2);

    /**
     * Test RT mode...
     * verify if the real time mode returned by jack match the optional argument defined when launching jack_test*/
sletz's avatar
sletz committed
619
    if (jack_is_realtime(client1) == RT)
sletz's avatar
sletz committed
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
        Log("Jackd is in realtime mode (RT = %i).\n", RT);
    else
        printf("!!! ERROR !!! Jackd is in a non-expected realtime mode (RT = %i).\n", RT);

    /**
     * Register all callbacks...
     *
     */
    if (jack_set_thread_init_callback(client1, Jack_Thread_Init_Callback, 0) != 0)
        printf("!!! ERROR !!! while calling jack_set_thread_init_callback()...\n");
    if (jack_set_freewheel_callback(client1, Jack_Freewheel_Callback, 0) != 0 )
        printf("\n!!! ERROR !!! while calling jack_set_freewheel_callback()...\n");

    if (jack_set_process_callback(client1, process1, 0) != 0) {
        printf("Error when calling jack_set_process_callback() !\n");
    }

    jack_on_shutdown(client1, jack_shutdown, 0);

    if (jack_set_buffer_size_callback(client1, Jack_Update_Buffer_Size, 0) != 0) {
        printf("Error when calling buffer_size_callback !\n");
    }
642
	
sletz's avatar
sletz committed
643
644
645
    if (jack_set_graph_order_callback(client1, Jack_Graph_Order_Callback, 0) != 0) {
        printf("Error when calling Jack_Graph_Order_Callback() !\n");
    }
646
	
sletz's avatar
sletz committed
647
648
649
    if (jack_set_xrun_callback(client1, Jack_XRun_Callback, 0 ) != 0) {
        printf("Error when calling jack_set_xrun_callback() !\n");
    }
650
	
sletz's avatar
sletz committed
651
652
653
    if (jack_set_sample_rate_callback(client1, Jack_Sample_Rate_Callback, 0 ) != 0) {
        printf("Error when calling Jack_Sample_Rate_Callback() !\n");
    }
654
	
sletz's avatar
sletz committed
655
656
657
    if (jack_set_port_registration_callback(client1, Jack_Port_Register, 0) != 0) {
        printf("Error when calling jack_set_port_registration_callback() !\n");
    }
658
	
659
660
661
	if (jack_set_client_registration_callback(client1, Jack_Client_Registration_Callback, 0) != 0) {
		printf("Error when calling jack_set_client_registration_callback() !\n");
	}
662
	
sletz's avatar
sletz committed
663
    jack_set_error_function(Jack_Error_Callback);
sletz's avatar
sletz committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701

    /**
     * Create file for clock "frame time" analysis
     *
     */
    cur_buffer_size = jack_get_buffer_size(client1);
    sprintf (filename, "framefile-%i.dat", cur_buffer_size);
    file = fopen(filename, "w");
    if (file == NULL) {
        fprintf(stderr, "Erreur dans l'ouverture du fichier log framefile.dat");
        exit( -1);
    }

    /**
     * Try to register a client with a NULL name/zero length name...
     *
     */
    output_port1 = jack_port_register(client1, nullportname,
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    if (output_port1 == NULL) {
        Log("Can't register a port with a NULL portname... ok.\n");
    } else {
        printf("!!! ERROR !!! Can register a port with a NULL portname !\n");
        jack_port_unregister(client1, output_port1);
    }

    /**
     * Register 1 port in order to stress other functions.
     *
     */
    output_port1 = jack_port_register(client1, portname,
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    if (output_port1 == NULL) {
        printf("!!! ERROR !!! Can't register any port for the client !\n");
        exit(1);
    }
702
703
704
705
706
707
708
709
710
711
	
   /**
     * Test port type of the just registered port.
     *
     */
    if (strcmp(jack_port_type(output_port1), JACK_DEFAULT_AUDIO_TYPE) != 0) {
        printf("!!! ERROR !!! jack_port_type returns an incorrect value!\n");
	} else {
		Log("Checking jack_port_type()... ok.\n");
	}
sletz's avatar
sletz committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750

    /**
     * Try to register another port with the same name...
     *
     */
    output_port2 = jack_port_register(client1, portname,
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    if (output_port2 == NULL) {
        Log("Can't register two ports with the same name... ok\n");
    } else {
        if (strcmp (jack_port_name(output_port1), jack_port_name(output_port2)) == 0) {
            printf("!!! ERROR !!! Can register two ports with the same name ! (%i : %s & %i : %s).\n", output_port1, jack_port_name(output_port1), output_port2, jack_port_name(output_port2));
            jack_port_unregister(client1, output_port2);
        } else {
            Log("Can't register two ports with the same name... ok (auto-rename %s into %s).\n", jack_port_name(output_port1), jack_port_name(output_port2));
            jack_port_unregister(client1, output_port2);
        }
    }

    /**
     * Verify that both port_name and port_short_name return correct results...
     *
     */
    sprintf (portname, "%s:%s", jack_get_client_name(client1), jack_port_short_name(output_port1));
    if (strcmp(jack_port_name(output_port1), portname) != 0) {
        printf("!!! ERROR !!! functions jack_port_name and/or jack_short_port_name seems to be invalid !\n");
        printf("client_name = %s\n short_port_name = %s\n port_name = %s\n", jack_get_client_name(client1), jack_port_short_name(output_port1), jack_port_name(output_port1));
    }

    /**
     * Verify the function port_set_name
     *
     */
    if (jack_port_set_name (output_port1, "renamed-port#") == 0 ) {
        if (strcmp(jack_port_name(output_port1), "renamed-port#") == 0) {
            printf("!!! ERROR !!! functions jack_port_set_name seems to be invalid !\n");
            printf("Jack_port_name return '%s' whereas 'renamed-port#' was expected...\n");
        } else {
751
            Log("Checking jack_port_set_name()... ok\n");
sletz's avatar
sletz committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
            jack_port_set_name (output_port1, "port");
        }
    } else {
        printf("error : port_set_name function can't be tested...\n");
    }

    /**
     * Activate the client
     *
     */
    if (jack_activate(client1) < 0) {
        printf ("Fatal error : cannot activate client1\n");
        exit (1);
    }

    /**
     * Test if init callback initThread have been called.
     *
     */
    if (init_clbk == 0)
        printf("!!! ERROR !!! JackThreadInitCallback was not called !!.\n");

    jack_sleep(10 * 1000); // test see the clock in the graph at the begining...

    /**
     * Stress Freewheel mode...
     * Try to enter freewheel mode. Check the realtime mode de-activation.
     * Check that the number of call of the process callback is greater than in non-freewheel mode.
     * Give an approximated speed ratio (number of process call) between the two modes.
     * Then return in normal mode.
     */
    t_error = 0;
    activated = 0;
    jack_sleep(1 * 1000);
    count1 = activated;
    Log("Testing activation freewheel mode...\n");
    linefw = linecount; // count for better graph reading with gnuplot
    jack_set_freewheel(client1, 1);
    activated = 0;
    jack_sleep(1 * 1000);
    count2 = activated;
    if (jack_is_realtime(client1) == 0) {
        t_error = 0;
    } else {
        printf("\n!!! ERROR !!! RT mode is always activated while freewheel mode is applied !\n");
        t_error = 1;
    }
    if (activated == 0)
        printf("!!! ERROR !!! Freewheel mode doesn't activate audio callback !!\n");

    jack_set_freewheel(client1, 0);
    jack_sleep(7 * 1000);

sletz's avatar
Cleanup    
sletz committed
805
	if (jack_is_realtime(client1) == 1) {}
sletz's avatar
sletz committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
    else {
        printf("\n!!! ERROR !!! freewheel mode fail to reactivate RT mode when exiting !\n");
        t_error = 1;
    }
    if (t_error == 0) {
        Log("Freewheel mode appears to work well...\n");
    }
    if (count1 == 0) {
        Log("Audio Callback in 'standard' (non-freewheel) mode seems not to be called...\n");
        Log("Ratio speed would be unavailable...\n");
    } else {
        ratio = (float) ((count2 - count1) / count1);
        Log("Approximative speed ratio of freewheel mode = %f : 1.00\n", ratio);
    }

    /**
     * Stress buffer function...
     * get current buffer size.
     * Try to apply a new buffer size value ( 2 x the precedent buffer size value)
     * Then return in previous buffer size mode.
     *
     */

    float factor = 0.5f;
    old_buffer_size = jack_get_buffer_size(client1);
    Log("Testing BufferSize change & Callback...\n--> Current buffer size : %i.\n", old_buffer_size);
    linebuf = linecount;
sletz's avatar
sletz committed
833
    if (jack_set_buffer_size(client1, (jack_nframes_t)(old_buffer_size * factor)) < 0) {
sletz's avatar
sletz committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        printf("!!! ERROR !!! jack_set_buffer_size fails !\n");
    }
    jack_sleep(1 * 1000);
    cur_buffer_size = jack_get_buffer_size(client1);
    if ((old_buffer_size * factor) != cur_buffer_size) {
        printf("!!! ERROR !!! Buffer size has not been changed !\n");
        printf("!!! Maybe jack was compiled without the '--enable-resize' flag...\n");
    } else {
        Log("jack_set_buffer_size() command successfully applied...\n");
    }
    jack_sleep(3 * 1000);
    jack_set_buffer_size(client1, old_buffer_size);
    cur_buffer_size = jack_get_buffer_size(client1);

    /**
     * Test the last regestered port to see if port_is_mine function the right value.
     * A second test will be performed later.
     * The result will be printed at the end.
     *
     */
    if (jack_port_is_mine(client1, output_port1)) {
        is_mine = 1;
    } else {
        is_mine = 0;
    }

    /**
     * Check that the ID returned by the port_by_name is right.
     * (it seems there is a problem here in some jack versions).
     *
    */
    if (output_port1 != jack_port_by_name(client1, jack_port_name(output_port1))) {
        printf("!!! ERROR !!! function jack_port_by_name() return bad value !\n");
        printf("!!! jack_port_by_name(jack_port_name(_ID_) ) != _ID_returned_at_port_registering ! (%i != %i)\n", jack_port_by_name(client1, jack_port_name(output_port1)), output_port1);
    } else {
        Log("Checking jack_port_by_name() return value... ok\n");
    }
    if (NULL != jack_port_by_name(client1, jack_port_short_name(output_port1))) {
        printf("!!! ERROR !!! function jack_port_by_name() return a value (%i) while name is incomplete !\n", jack_port_by_name(client1, jack_port_short_name(output_port1)));
    } else {
        Log("Checking jack_port_by_name() with bad argument... ok (returned id 0)\n");
    }

    /**
    * remove the output port previously created
    * no more ports should subsist here for our client.
    * 
    */
    if (jack_port_unregister(client1, output_port1) != 0) {
        printf("!!! ERROR !!! while unregistering port %s.\n", jack_port_name(output_port1));
    }

    /**
     * list all in ports
     * 
     */
    inports = jack_get_ports(client1, NULL, NULL, 0);

    /**
     * Test the first PHY (physical) connection to see if it's "mine".
     * and report the result in the test that began before.
     * The result is printed later.
     *
     */
    if (jack_port_is_mine(client1, jack_port_by_name(client1, inports[0]))) {
        is_mine = 0;
    }

    /**
     * List all devices' flags and print them...
     *
     */
    Log("\nTry functions jack_get_ports, jack_port_flag & jack_port_by_name to list PHY devices...\n");
    Log("-----------------------------------------------------------\n");
    Log("---------------------------DEVICES-------------------------\n");
    Log("-----------------------------------------------------------\n");
    a = 0;
    while (inports[a] != NULL) {
        flag = jack_port_flags(jack_port_by_name(client1, inports[a]) );
        Log("   * %s (id : %i)\n", inports[a], jack_port_by_name(client1, inports[a]));
        Log("    (");
        if (flag & JackPortIsInput)
            Log("JackPortIsInput ");
        if (flag & JackPortIsOutput)
            Log("JackPortIsOutput ");
        if (flag & JackPortIsPhysical)
            Log("JackPortIsPhysical ");
        if (flag & JackPortCanMonitor)
            Log("JackPortCanMonitor ");
        if (flag & JackPortIsTerminal)
            Log("JackPortIsTerminal ");
        Log(")\n\n");
        a++;
    }
    Log("-----------------------------------------------------------\n\n");

    /**
     * list all PHY in/out ports...
     * This list will be used later many times.
     *
     */
    outports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
    inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);

    if (outports == NULL) {
        printf("!!! WARNING !!! no physical capture ports founded !\n");
    }
    if (inports == NULL) {
        printf("!!! WARNING !!! no physical output ports founded !\n");
    }

    /**
     * Brute test : try to create as many ports as possible.
     * It stops when jack returns an error.
     * Then try to connect each port to physical entry...
     * Check also that graph reorder callback is called.
     *
     */
    Log("Registering as many ports as possible and connect them to physical entries...\n");
    lineports = linecount;
    t_error = 0;

    i = 0;	// number of couple 'input-ouput'
    j = 0;	// number of ports created
    port_callback_reg = 0;	// number of port registration received by the callback
    reorder = 0;			// number of graph reorder callback activation
    test_link = 0 ;			// Test the "overconnect" function only one time
    while (t_error == 0) {
        sprintf (portname, "input_%d", i);
        input_port1 = jack_port_register(client1, portname,
                                         JACK_DEFAULT_AUDIO_TYPE,
                                         JackPortIsInput, 0);
        j++;
        if (input_port1 == NULL) {
            j--;
            t_error = 1;
        } else {
            // Connect created input to PHY output
            a = 0;
            while (outports[a] != NULL) {
                if (jack_connect(client1, outports[a], jack_port_name(input_port1))) {
                    printf ("error : cannot connect input PHY port to client port %s\n", jack_port_name(input_port1));
                } else {
                    // printf ("input PHY port %s connected to client port %s\n", outports[a], jack_port_name(input_port1));
                }
                a++;
            }
            // Try one time to "overconnect" 2 ports (the latest created)...
            if (test_link == 0) {
                if (jack_connect(client1, outports[a - 1], jack_port_name(input_port1)) == EEXIST) {
                    // cannot over-connect input PHY port to client port. ok.
                    test_link = 1;
                }
            }
        }

        sprintf(portname, "output_%d", i);
        output_port1 = jack_port_register(client1, portname,
                                          JACK_DEFAULT_AUDIO_TYPE,
                                          JackPortIsOutput, 0);
        j++;
        if (output_port1 == NULL) {
            t_error = 1;
            j--;
        } else {
            // Connect created input to PHY output
            a = 0;
For faster browsing, not all history is shown. View entire blame