Replace Util.pprint by Logs.pprint
[jack2.git] / example-clients / netsource.c
blobe2b916e00c9df7502bc4dba3f883f24ab03bd709
1 /*
2 NetJack Client
4 Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
5 Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
6 Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /** @file netsource.c
26 * @brief This client connects a remote slave JACK to a local JACK server assumed to be the master
30 #include <stdio.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <signal.h>
37 #if defined(HAVE_CONFIG_H)
38 #include "config.h"
39 #endif
41 #ifdef WIN32
42 #include <winsock2.h>
43 #define socklen_t int
44 #include <malloc.h>
45 #else
46 #include <netinet/in.h>
47 #include <netdb.h>
48 #include <sys/socket.h>
49 #endif
51 /* These two required by FreeBSD. */
52 #include <sys/types.h>
54 #include <jack/jack.h>
56 #include <netjack_packet.h>
57 #if HAVE_SAMPLERATE
58 #include <samplerate.h>
59 #endif
61 #if HAVE_CELT
62 #include <celt/celt.h>
63 #endif
65 #include <math.h>
67 JSList *capture_ports = NULL;
68 JSList *capture_srcs = NULL;
69 int capture_channels = 0;
70 int capture_channels_audio = 2;
71 int capture_channels_midi = 1;
72 JSList *playback_ports = NULL;
73 JSList *playback_srcs = NULL;
74 int playback_channels = 0;
75 int playback_channels_audio = 2;
76 int playback_channels_midi = 1;
77 int dont_htonl_floats = 0;
79 int latency = 5;
80 jack_nframes_t factor = 1;
81 int bitdepth = 0;
82 int mtu = 1400;
83 int reply_port = 0;
84 int bind_port = 0;
85 int redundancy = 1;
86 jack_client_t *client;
87 packet_cache * packcache = 0;
89 int state_connected = 0;
90 int state_latency = 0;
91 int state_netxruns = 0;
92 int state_currentframe = 0;
93 int state_recv_packet_queue_time = 0;
95 int quit = 0;
98 int outsockfd;
99 int insockfd;
100 #ifdef WIN32
101 struct sockaddr_in destaddr;
102 struct sockaddr_in bindaddr;
103 #else
104 struct sockaddr destaddr;
105 struct sockaddr bindaddr;
106 #endif
108 int sync_state;
109 jack_transport_state_t last_transport_state;
111 int framecnt = 0;
113 int cont_miss = 0;
115 int freewheeling = 0;
118 * This Function allocates all the I/O Ports which are added the lists.
120 void
121 alloc_ports (int n_capture_audio, int n_playback_audio, int n_capture_midi, int n_playback_midi)
124 int port_flags = JackPortIsOutput;
125 int chn;
126 jack_port_t *port;
127 char buf[32];
129 capture_ports = NULL;
130 /* Allocate audio capture channels */
131 for (chn = 0; chn < n_capture_audio; chn++) {
132 snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
133 port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
134 if (!port) {
135 printf( "jack_netsource: cannot register %s port\n", buf);
136 break;
138 if (bitdepth == 1000) {
139 #if HAVE_CELT
140 #if HAVE_CELT_API_0_11
141 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
142 capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create_custom( celt_mode, 1, NULL ) );
143 #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
144 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
145 capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode, 1, NULL ) );
146 #else
147 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), 1, jack_get_buffer_size(client), NULL );
148 capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode ) );
149 #endif
150 #endif
151 } else {
152 #if HAVE_SAMPLERATE
153 capture_srcs = jack_slist_append (capture_srcs, src_new (SRC_LINEAR, 1, NULL));
154 #endif
156 capture_ports = jack_slist_append (capture_ports, port);
159 /* Allocate midi capture channels */
160 for (chn = n_capture_audio; chn < n_capture_midi + n_capture_audio; chn++) {
161 snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
162 port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
163 if (!port) {
164 printf ("jack_netsource: cannot register %s port\n", buf);
165 break;
167 capture_ports = jack_slist_append(capture_ports, port);
170 /* Allocate audio playback channels */
171 port_flags = JackPortIsInput;
172 playback_ports = NULL;
173 for (chn = 0; chn < n_playback_audio; chn++) {
174 snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
175 port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
176 if (!port) {
177 printf ("jack_netsource: cannot register %s port\n", buf);
178 break;
180 if( bitdepth == 1000 ) {
181 #if HAVE_CELT
182 #if HAVE_CELT_API_0_11
183 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
184 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create_custom( celt_mode, 1, NULL ) );
185 #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
186 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
187 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
188 #else
189 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), 1, jack_get_buffer_size(client), NULL );
190 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode ) );
191 #endif
192 #endif
193 } else {
194 #if HAVE_SAMPLERATE
195 playback_srcs = jack_slist_append (playback_srcs, src_new (SRC_LINEAR, 1, NULL));
196 #endif
198 playback_ports = jack_slist_append (playback_ports, port);
201 /* Allocate midi playback channels */
202 for (chn = n_playback_audio; chn < n_playback_midi + n_playback_audio; chn++) {
203 snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
204 port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
205 if (!port) {
206 printf ("jack_netsource: cannot register %s port\n", buf);
207 break;
209 playback_ports = jack_slist_append (playback_ports, port);
214 * The Sync callback... sync state is set elsewhere...
215 * we will see if this is working correctly.
216 * i dont really believe in it yet.
219 sync_cb (jack_transport_state_t state, jack_position_t *pos, void *arg)
221 static int latency_count = 0;
222 int retval = sync_state;
224 if (! state_connected) {
225 return 1;
227 if (latency_count) {
228 latency_count--;
229 retval = 0;
232 else if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
233 retval = 0;
234 latency_count = latency - 1;
237 last_transport_state = state;
238 return retval;
241 void
242 freewheel_cb (int starting, void *arg)
244 freewheeling = starting;
247 int deadline_goodness = 0;
249 * The process callback for this JACK application.
250 * It is called by JACK at the appropriate times.
253 process (jack_nframes_t nframes, void *arg)
255 jack_nframes_t net_period;
256 int rx_bufsize, tx_bufsize;
258 jack_default_audio_sample_t *buf;
259 jack_port_t *port;
260 JSList *node;
261 int chn;
262 int size, i;
263 const char *porttype;
264 int input_fd;
266 jack_position_t local_trans_pos;
268 uint32_t *packet_buf_tx, *packet_bufX;
269 uint32_t *rx_packet_ptr;
270 jack_time_t packet_recv_timestamp;
272 if( bitdepth == 1000 )
273 net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8) & (~1) ;
274 else
275 net_period = (float) nframes / (float) factor;
277 rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
278 tx_bufsize = get_sample_size (bitdepth) * playback_channels * net_period + sizeof (jacknet_packet_header);
280 /* Allocate a buffer where both In and Out Buffer will fit */
281 packet_buf_tx = alloca (tx_bufsize);
283 jacknet_packet_header *pkthdr_tx = (jacknet_packet_header *) packet_buf_tx;
286 * for latency==0 we need to send out the packet before we wait on the reply.
287 * but this introduces a cycle of latency, when netsource is connected to itself.
288 * so we send out before read only in zero latency mode.
292 if( latency == 0 ) {
293 /* reset packet_bufX... */
294 packet_bufX = packet_buf_tx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
296 /* ---------- Send ---------- */
297 render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
298 packet_bufX, net_period, dont_htonl_floats);
300 /* fill in packet hdr */
301 pkthdr_tx->transport_state = jack_transport_query (client, &local_trans_pos);
302 pkthdr_tx->transport_frame = local_trans_pos.frame;
303 pkthdr_tx->framecnt = framecnt;
304 pkthdr_tx->latency = latency;
305 pkthdr_tx->reply_port = reply_port;
306 pkthdr_tx->sample_rate = jack_get_sample_rate (client);
307 pkthdr_tx->period_size = nframes;
309 /* playback for us is capture on the other side */
310 pkthdr_tx->capture_channels_audio = playback_channels_audio;
311 pkthdr_tx->playback_channels_audio = capture_channels_audio;
312 pkthdr_tx->capture_channels_midi = playback_channels_midi;
313 pkthdr_tx->playback_channels_midi = capture_channels_midi;
314 pkthdr_tx->mtu = mtu;
315 if( freewheeling != 0 )
316 pkthdr_tx->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
317 else
318 pkthdr_tx->sync_state = (jack_nframes_t)deadline_goodness;
319 //printf("goodness=%d\n", deadline_goodness );
321 packet_header_hton (pkthdr_tx);
322 if (cont_miss < 3 * latency + 5) {
323 int r;
324 for( r = 0; r < redundancy; r++ )
325 netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
326 } else if (cont_miss > 50 + 5 * latency) {
327 state_connected = 0;
328 packet_cache_reset_master_address( packcache );
329 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
330 cont_miss = 0;
335 * ok... now the RECEIVE code.
340 if( reply_port )
341 input_fd = insockfd;
342 else
343 input_fd = outsockfd;
345 // for latency == 0 we can poll.
346 if( (latency == 0) || (freewheeling != 0) ) {
347 jack_time_t deadline = jack_get_time() + 1000000 * jack_get_buffer_size(client) / jack_get_sample_rate(client);
348 // Now loop until we get the right packet.
349 while(1) {
350 jack_nframes_t got_frame;
351 if ( ! netjack_poll_deadline( input_fd, deadline ) )
352 break;
354 packet_cache_drain_socket(packcache, input_fd);
356 if (packet_cache_get_next_available_framecnt( packcache, framecnt - latency, &got_frame ))
357 if( got_frame == (framecnt - latency) )
358 break;
360 } else {
361 // normally:
362 // only drain socket.
363 packet_cache_drain_socket(packcache, input_fd);
366 size = packet_cache_retreive_packet_pointer( packcache, framecnt - latency, (char**)&rx_packet_ptr, rx_bufsize, &packet_recv_timestamp );
367 /* First alternative : we received what we expected. Render the data
368 * to the JACK ports so it can be played. */
369 if (size == rx_bufsize) {
370 uint32_t *packet_buf_rx = rx_packet_ptr;
371 jacknet_packet_header *pkthdr_rx = (jacknet_packet_header *) packet_buf_rx;
372 packet_bufX = packet_buf_rx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
373 // calculate how much time there would have been, if this packet was sent at the deadline.
375 int recv_time_offset = (int) (jack_get_time() - packet_recv_timestamp);
376 packet_header_ntoh (pkthdr_rx);
377 deadline_goodness = recv_time_offset - (int)pkthdr_rx->latency;
378 //printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
380 if (cont_miss) {
381 //printf("Frame %d \tRecovered from dropouts\n", framecnt);
382 cont_miss = 0;
384 render_payload_to_jack_ports (bitdepth, packet_bufX, net_period,
385 capture_ports, capture_srcs, nframes, dont_htonl_floats);
387 state_currentframe = framecnt;
388 state_recv_packet_queue_time = recv_time_offset;
389 state_connected = 1;
390 sync_state = pkthdr_rx->sync_state;
391 packet_cache_release_packet( packcache, framecnt - latency );
393 /* Second alternative : we've received something that's not
394 * as big as expected or we missed a packet. We render silence
395 * to the ouput ports */
396 else {
397 jack_nframes_t latency_estimate;
398 if( packet_cache_find_latency( packcache, framecnt, &latency_estimate ) )
399 //if( (state_latency == 0) || (latency_estimate < state_latency) )
400 state_latency = latency_estimate;
402 // Set the counters up.
403 state_currentframe = framecnt;
404 //state_latency = framecnt - pkthdr->framecnt;
405 state_netxruns += 1;
407 //printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
408 //printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
409 cont_miss += 1;
410 chn = 0;
411 node = capture_ports;
412 while (node != NULL) {
413 port = (jack_port_t *) node->data;
414 buf = jack_port_get_buffer (port, nframes);
415 porttype = jack_port_type (port);
416 if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size ()) == 0)
417 for (i = 0; i < nframes; i++)
418 buf[i] = 0.0;
419 else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size ()) == 0)
420 jack_midi_clear_buffer (buf);
421 node = jack_slist_next (node);
422 chn++;
425 if (latency != 0) {
426 /* reset packet_bufX... */
427 packet_bufX = packet_buf_tx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
429 /* ---------- Send ---------- */
430 render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
431 packet_bufX, net_period, dont_htonl_floats);
433 /* fill in packet hdr */
434 pkthdr_tx->transport_state = jack_transport_query (client, &local_trans_pos);
435 pkthdr_tx->transport_frame = local_trans_pos.frame;
436 pkthdr_tx->framecnt = framecnt;
437 pkthdr_tx->latency = latency;
438 pkthdr_tx->reply_port = reply_port;
439 pkthdr_tx->sample_rate = jack_get_sample_rate (client);
440 pkthdr_tx->period_size = nframes;
442 /* playback for us is capture on the other side */
443 pkthdr_tx->capture_channels_audio = playback_channels_audio;
444 pkthdr_tx->playback_channels_audio = capture_channels_audio;
445 pkthdr_tx->capture_channels_midi = playback_channels_midi;
446 pkthdr_tx->playback_channels_midi = capture_channels_midi;
447 pkthdr_tx->mtu = mtu;
448 if( freewheeling != 0 )
449 pkthdr_tx->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
450 else
451 pkthdr_tx->sync_state = (jack_nframes_t)deadline_goodness;
452 //printf("goodness=%d\n", deadline_goodness );
454 packet_header_hton (pkthdr_tx);
455 if (cont_miss < 3 * latency + 5) {
456 int r;
457 for( r = 0; r < redundancy; r++ )
458 netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
459 } else if (cont_miss > 50 + 5 * latency) {
460 state_connected = 0;
461 packet_cache_reset_master_address( packcache );
462 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
463 cont_miss = 0;
467 framecnt++;
468 return 0;
472 * This is the shutdown callback for this JACK application.
473 * It is called by JACK if the server ever shuts down or
474 * decides to disconnect the client.
477 void
478 jack_shutdown (void *arg)
480 fprintf(stderr, "JACK shut down, exiting ...\n");
481 exit (1);
484 void
485 init_sockaddr_in (struct sockaddr_in *name , const char *hostname , uint16_t port)
487 name->sin_family = AF_INET ;
488 name->sin_port = htons (port);
489 if (hostname) {
490 struct hostent *hostinfo = gethostbyname (hostname);
491 if (hostinfo == NULL) {
492 fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
493 fflush( stderr );
495 #ifdef WIN32
496 name->sin_addr.s_addr = inet_addr( hostname );
497 #else
498 name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
499 #endif
500 } else
501 name->sin_addr.s_addr = htonl (INADDR_ANY) ;
505 void
506 printUsage ()
508 fprintf (stderr, "usage: jack_netsource [options]\n"
509 "\n"
510 " -h this help text\n"
511 " -H <slave host> - Host name of the slave JACK\n"
512 " -o <num channels> - Number of audio playback channels\n"
513 " -i <num channels> - Number of audio capture channels\n"
514 " -O <num channels> - Number of midi playback channels\n"
515 " -I <num channels> - Number of midi capture channels\n"
516 " -n <periods> - Network latency in JACK periods\n"
517 " -p <port> - UDP port that the slave is listening on\n"
518 " -r <reply port> - UDP port that we are listening on\n"
519 " -B <bind port> - reply port, for use in NAT environments\n"
520 " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
521 " -c <kbits> - Use CELT encoding with <kbits> kbits per channel\n"
522 " -m <mtu> - Assume this mtu for the link\n"
523 " -R <N> - Redundancy: send out packets N times.\n"
524 " -e - skip host-to-network endianness conversion\n"
525 " -N <jack name> - Reports a different name to jack\n"
526 " -s <server name> - The name of the local jack server\n"
527 "\n");
530 void
531 sigterm_handler( int signal )
533 quit = 1;
537 main (int argc, char *argv[])
539 /* Some startup related basics */
540 char *client_name, *server_name = NULL, *peer_ip;
541 int peer_port = 3000;
542 jack_options_t options = JackNullOption;
543 jack_status_t status;
544 #ifdef WIN32
545 WSADATA wsa;
546 int rc = WSAStartup(MAKEWORD(2, 0), &wsa);
547 #endif
548 /* Torben's famous state variables, aka "the reporting API" ! */
549 /* heh ? these are only the copies of them ;) */
550 int statecopy_connected, statecopy_latency, statecopy_netxruns;
551 jack_nframes_t net_period;
552 /* Argument parsing stuff */
553 extern char *optarg;
554 extern int optind, optopt;
555 int errflg = 0, c;
557 if (argc < 3) {
558 printUsage ();
559 return 1;
562 client_name = (char *) malloc (sizeof (char) * 10);
563 peer_ip = (char *) malloc (sizeof (char) * 10);
564 sprintf(client_name, "netjack");
565 sprintf(peer_ip, "localhost");
567 while ((c = getopt (argc, argv, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:")) != -1) {
568 switch (c) {
569 case 'h':
570 printUsage();
571 exit (0);
572 break;
573 case 'H':
574 free(peer_ip);
575 peer_ip = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
576 strcpy (peer_ip, optarg);
577 break;
578 case 'o':
579 playback_channels_audio = atoi (optarg);
580 break;
581 case 'i':
582 capture_channels_audio = atoi (optarg);
583 break;
584 case 'O':
585 playback_channels_midi = atoi (optarg);
586 break;
587 case 'I':
588 capture_channels_midi = atoi (optarg);
589 break;
590 case 'n':
591 latency = atoi (optarg);
592 break;
593 case 'p':
594 peer_port = atoi (optarg);
595 break;
596 case 'r':
597 reply_port = atoi (optarg);
598 break;
599 case 'B':
600 bind_port = atoi (optarg);
601 break;
602 case 'f':
603 factor = atoi (optarg);
604 printf("This feature is deprecated and will be removed in future netjack versions. CELT offers a superiour way to conserve bandwidth");
605 break;
606 case 'b':
607 bitdepth = atoi (optarg);
608 break;
609 case 'c':
610 #if HAVE_CELT
611 bitdepth = 1000;
612 factor = atoi (optarg);
613 #else
614 printf( "not built with celt support\n" );
615 exit(10);
616 #endif
617 break;
618 case 'm':
619 mtu = atoi (optarg);
620 break;
621 case 'R':
622 redundancy = atoi (optarg);
623 break;
624 case 'e':
625 dont_htonl_floats = 1;
626 break;
627 case 'N':
628 free(client_name);
629 client_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
630 strcpy (client_name, optarg);
631 break;
632 case 's':
633 server_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
634 strcpy (server_name, optarg);
635 options |= JackServerName;
636 break;
637 case ':':
638 fprintf (stderr, "Option -%c requires an operand\n", optopt);
639 errflg++;
640 break;
641 case '?':
642 fprintf (stderr, "Unrecognized option: -%c\n", optopt);
643 errflg++;
646 if (errflg) {
647 printUsage ();
648 exit (2);
651 capture_channels = capture_channels_audio + capture_channels_midi;
652 playback_channels = playback_channels_audio + playback_channels_midi;
654 outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
655 insockfd = socket (AF_INET, SOCK_DGRAM, 0);
657 if ((outsockfd == -1) || (insockfd == -1)) {
658 fprintf (stderr, "cant open sockets\n" );
659 return 1;
662 init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
663 if (bind_port) {
664 init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
665 if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
666 fprintf (stderr, "bind failure\n" );
669 if (reply_port) {
670 init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
671 if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
672 fprintf (stderr, "bind failure\n" );
676 /* try to become a client of the JACK server */
677 client = jack_client_open (client_name, options, &status, server_name);
678 if (client == NULL) {
679 fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
680 "Is the JACK server running ?\n", status);
681 return 1;
684 /* Set up jack callbacks */
685 jack_set_process_callback (client, process, 0);
686 jack_set_sync_callback (client, sync_cb, 0);
687 jack_set_freewheel_callback (client, freewheel_cb, 0);
688 jack_on_shutdown (client, jack_shutdown, 0);
690 alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);
692 if( bitdepth == 1000 )
693 net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8) & (~1) ;
694 else
695 net_period = ceilf((float) jack_get_buffer_size (client) / (float) factor);
697 int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
698 packcache = packet_cache_new (latency + 50, rx_bufsize, mtu);
700 /* tell the JACK server that we are ready to roll */
701 if (jack_activate (client)) {
702 fprintf (stderr, "Cannot activate client");
703 return 1;
706 /* Now sleep forever... and evaluate the state_ vars */
708 signal( SIGTERM, sigterm_handler );
709 signal( SIGINT, sigterm_handler );
711 statecopy_connected = 2; // make it report unconnected on start.
712 statecopy_latency = state_latency;
713 statecopy_netxruns = state_netxruns;
715 while ( !quit ) {
716 #ifdef WIN32
717 Sleep (1000);
718 #else
719 sleep(1);
720 #endif
721 if (statecopy_connected != state_connected) {
722 statecopy_connected = state_connected;
723 if (statecopy_connected) {
724 state_netxruns = 1; // We want to reset the netxrun count on each new connection
725 printf ("Connected :-)\n");
726 } else
727 printf ("Not Connected\n");
729 fflush(stdout);
732 if (statecopy_connected) {
733 if (statecopy_netxruns != state_netxruns) {
734 statecopy_netxruns = state_netxruns;
735 printf ("%s: at frame %06d -> total netxruns %d (%d%%) queue time= %d\n",
736 client_name,
737 state_currentframe,
738 statecopy_netxruns,
739 100 * statecopy_netxruns / state_currentframe,
740 state_recv_packet_queue_time);
742 fflush(stdout);
744 } else {
745 if (statecopy_latency != state_latency) {
746 statecopy_latency = state_latency;
747 if (statecopy_latency > 1)
748 printf ("current latency %d\n", statecopy_latency);
749 fflush(stdout);
754 jack_client_close (client);
755 packet_cache_free (packcache);
756 exit (0);