jack_netsource : fix crash when hostname is unknown.
[jack2.git] / example-clients / netsource.c
bloba78dc8cbd71a68e0d456bef4410bd0b8b709c5ad
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 #ifdef __linux__
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>
55 #include <jack/jack.h>
57 //#include <net_driver.h>
58 #include <netjack_packet.h>
59 #if HAVE_SAMPLERATE
60 #include <samplerate.h>
61 #endif
63 #if HAVE_CELT
64 #include <celt/celt.h>
65 #endif
67 #include <math.h>
69 JSList *capture_ports = NULL;
70 JSList *capture_srcs = NULL;
71 int capture_channels = 0;
72 int capture_channels_audio = 2;
73 int capture_channels_midi = 1;
74 JSList *playback_ports = NULL;
75 JSList *playback_srcs = NULL;
76 int playback_channels = 0;
77 int playback_channels_audio = 2;
78 int playback_channels_midi = 1;
79 int dont_htonl_floats = 0;
81 int latency = 5;
82 jack_nframes_t factor = 1;
83 int bitdepth = 0;
84 int mtu = 1400;
85 int reply_port = 0;
86 int bind_port = 0;
87 int redundancy = 1;
88 jack_client_t *client;
90 int state_connected = 0;
91 int state_latency = 0;
92 int state_netxruns = 0;
93 int state_currentframe = 0;
94 int state_recv_packet_queue_time = 0;
96 int quit=0;
99 int outsockfd;
100 int insockfd;
101 #ifdef WIN32
102 struct sockaddr_in destaddr;
103 struct sockaddr_in bindaddr;
104 #else
105 struct sockaddr destaddr;
106 struct sockaddr bindaddr;
107 #endif
109 int sync_state;
110 jack_transport_state_t last_transport_state;
112 int framecnt = 0;
114 int cont_miss = 0;
116 int freewheeling = 0;
119 * This Function allocates all the I/O Ports which are added the lists.
121 void
122 alloc_ports (int n_capture_audio, int n_playback_audio, int n_capture_midi, int n_playback_midi)
125 int port_flags = JackPortIsOutput;
126 int chn;
127 jack_port_t *port;
128 char buf[32];
130 capture_ports = NULL;
131 /* Allocate audio capture channels */
132 for (chn = 0; chn < n_capture_audio; chn++)
134 snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
135 port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
136 if (!port)
138 printf( "jack_netsource: cannot register %s port\n", buf);
139 break;
141 if( bitdepth == 1000 ) {
142 #if HAVE_CELT
143 #if HAVE_CELT_API_0_7
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++)
162 snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
163 port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
164 if (!port)
166 printf ("jack_netsource: cannot register %s port\n", buf);
167 break;
169 capture_ports = jack_slist_append(capture_ports, port);
172 /* Allocate audio playback channels */
173 port_flags = JackPortIsInput;
174 playback_ports = NULL;
175 for (chn = 0; chn < n_playback_audio; chn++)
177 snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
178 port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
179 if (!port)
181 printf ("jack_netsource: cannot register %s port\n", buf);
182 break;
184 if( bitdepth == 1000 ) {
185 #if HAVE_CELT
186 #if HAVE_CELT_API_0_7
187 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
188 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
189 #else
190 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), 1, jack_get_buffer_size(client), NULL );
191 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode ) );
192 #endif
193 #endif
194 } else {
195 #if HAVE_SAMPLERATE
196 playback_srcs = jack_slist_append (playback_srcs, src_new (SRC_LINEAR, 1, NULL));
197 #endif
199 playback_ports = jack_slist_append (playback_ports, port);
202 /* Allocate midi playback channels */
203 for (chn = n_playback_audio; chn < n_playback_midi + n_playback_audio; chn++)
205 snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
206 port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
207 if (!port)
209 printf ("jack_netsource: cannot register %s port\n", buf);
210 break;
212 playback_ports = jack_slist_append (playback_ports, port);
217 * The Sync callback... sync state is set elsewhere...
218 * we will see if this is working correctly.
219 * i dont really believe in it yet.
222 sync_cb (jack_transport_state_t state, jack_position_t *pos, void *arg)
224 static int latency_count = 0;
225 int retval = sync_state;
227 if (latency_count) {
228 latency_count--;
229 retval = 0;
232 else if (state == JackTransportStarting && last_transport_state != JackTransportStarting)
234 retval = 0;
235 latency_count = latency - 1;
238 last_transport_state = state;
239 return retval;
242 void
243 freewheel_cb (int starting, void *arg)
245 freewheeling = starting;
248 int deadline_goodness=0;
250 * The process callback for this JACK application.
251 * It is called by JACK at the appropriate times.
254 process (jack_nframes_t nframes, void *arg)
256 jack_nframes_t net_period;
257 int rx_bufsize, tx_bufsize;
259 jack_default_audio_sample_t *buf;
260 jack_port_t *port;
261 JSList *node;
262 int chn;
263 int size, i;
264 const char *porttype;
265 int input_fd;
267 jack_position_t local_trans_pos;
269 uint32_t *packet_buf, *packet_bufX;
270 uint32_t *rx_packet_ptr;
271 jack_time_t packet_recv_timestamp;
273 if( bitdepth == 1000 )
274 net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8)&(~1) ;
275 else
276 net_period = (float) nframes / (float) factor;
278 rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
279 tx_bufsize = get_sample_size (bitdepth) * playback_channels * net_period + sizeof (jacknet_packet_header);
282 /* Allocate a buffer where both In and Out Buffer will fit */
283 packet_buf = alloca ((rx_bufsize > tx_bufsize) ? rx_bufsize : tx_bufsize);
285 jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
288 * for latency==0 we need to send out the packet before we wait on the reply.
289 * but this introduces a cycle of latency, when netsource is connected to itself.
290 * so we send out before read only in zero latency mode.
294 if( latency == 0 ) {
295 /* reset packet_bufX... */
296 packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
298 /* ---------- Send ---------- */
299 render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
300 packet_bufX, net_period, dont_htonl_floats);
302 /* fill in packet hdr */
303 pkthdr->transport_state = jack_transport_query (client, &local_trans_pos);
304 pkthdr->transport_frame = local_trans_pos.frame;
305 pkthdr->framecnt = framecnt;
306 pkthdr->latency = latency;
307 pkthdr->reply_port = reply_port;
308 pkthdr->sample_rate = jack_get_sample_rate (client);
309 pkthdr->period_size = nframes;
311 /* playback for us is capture on the other side */
312 pkthdr->capture_channels_audio = playback_channels_audio;
313 pkthdr->playback_channels_audio = capture_channels_audio;
314 pkthdr->capture_channels_midi = playback_channels_midi;
315 pkthdr->playback_channels_midi = capture_channels_midi;
316 pkthdr->mtu = mtu;
317 if( freewheeling!= 0 )
318 pkthdr->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
319 else
320 pkthdr->sync_state = (jack_nframes_t)deadline_goodness;
321 //printf("goodness=%d\n", deadline_goodness );
323 packet_header_hton (pkthdr);
324 if (cont_miss < 3*latency+5) {
325 int r;
326 for( r=0; r<redundancy; r++ )
327 netjack_sendto (outsockfd, (char *) packet_buf, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
329 else if (cont_miss > 50+5*latency)
331 state_connected = 0;
332 packet_cache_reset_master_address( global_packcache );
333 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
334 cont_miss = 0;
339 * ok... now the RECEIVE code.
343 /* reset packet_bufX... */
344 packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
346 if( reply_port )
347 input_fd = insockfd;
348 else
349 input_fd = outsockfd;
351 // for latency == 0 we can poll.
352 if( (latency == 0) || (freewheeling!=0) ) {
353 jack_time_t deadline = jack_get_time() + 1000000 * jack_get_buffer_size(client)/jack_get_sample_rate(client);
354 // Now loop until we get the right packet.
355 while(1) {
356 jack_nframes_t got_frame;
357 if ( ! netjack_poll_deadline( input_fd, deadline ) )
358 break;
360 packet_cache_drain_socket(global_packcache, input_fd);
362 if (packet_cache_get_next_available_framecnt( global_packcache, framecnt - latency, &got_frame ))
363 if( got_frame == (framecnt - latency) )
364 break;
366 } else {
367 // normally:
368 // only drain socket.
369 packet_cache_drain_socket(global_packcache, input_fd);
372 size = packet_cache_retreive_packet_pointer( global_packcache, framecnt - latency, (char**)&rx_packet_ptr, rx_bufsize, &packet_recv_timestamp );
373 /* First alternative : we received what we expected. Render the data
374 * to the JACK ports so it can be played. */
375 if (size == rx_bufsize)
377 packet_buf = rx_packet_ptr;
378 pkthdr = (jacknet_packet_header *) packet_buf;
379 packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
380 // calculate how much time there would have been, if this packet was sent at the deadline.
382 int recv_time_offset = (int) (jack_get_time() - packet_recv_timestamp);
383 packet_header_ntoh (pkthdr);
384 deadline_goodness = recv_time_offset - (int)pkthdr->latency;
385 //printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
387 if (cont_miss)
389 //printf("Frame %d \tRecovered from dropouts\n", framecnt);
390 cont_miss = 0;
392 render_payload_to_jack_ports (bitdepth, packet_bufX, net_period,
393 capture_ports, capture_srcs, nframes, dont_htonl_floats);
395 state_currentframe = framecnt;
396 state_recv_packet_queue_time = recv_time_offset;
397 state_connected = 1;
398 sync_state = pkthdr->sync_state;
399 packet_cache_release_packet( global_packcache, framecnt - latency );
401 /* Second alternative : we've received something that's not
402 * as big as expected or we missed a packet. We render silence
403 * to the ouput ports */
404 else
406 jack_nframes_t latency_estimate;
407 if( packet_cache_find_latency( global_packcache, framecnt, &latency_estimate ) )
408 //if( (state_latency == 0) || (latency_estimate < state_latency) )
409 state_latency = latency_estimate;
411 // Set the counters up.
412 state_currentframe = framecnt;
413 //state_latency = framecnt - pkthdr->framecnt;
414 state_netxruns += 1;
416 //printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
417 //printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
418 cont_miss += 1;
419 chn = 0;
420 node = capture_ports;
421 while (node != NULL)
423 port = (jack_port_t *) node->data;
424 buf = jack_port_get_buffer (port, nframes);
425 porttype = jack_port_type (port);
426 if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size ()) == 0)
427 for (i = 0; i < nframes; i++)
428 buf[i] = 0.0;
429 else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size ()) == 0)
430 jack_midi_clear_buffer (buf);
431 node = jack_slist_next (node);
432 chn++;
435 if( latency != 0 ) {
436 /* reset packet_bufX... */
437 packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
439 /* ---------- Send ---------- */
440 render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
441 packet_bufX, net_period, dont_htonl_floats);
443 /* fill in packet hdr */
444 pkthdr->transport_state = jack_transport_query (client, &local_trans_pos);
445 pkthdr->transport_frame = local_trans_pos.frame;
446 pkthdr->framecnt = framecnt;
447 pkthdr->latency = latency;
448 pkthdr->reply_port = reply_port;
449 pkthdr->sample_rate = jack_get_sample_rate (client);
450 pkthdr->period_size = nframes;
452 /* playback for us is capture on the other side */
453 pkthdr->capture_channels_audio = playback_channels_audio;
454 pkthdr->playback_channels_audio = capture_channels_audio;
455 pkthdr->capture_channels_midi = playback_channels_midi;
456 pkthdr->playback_channels_midi = capture_channels_midi;
457 pkthdr->mtu = mtu;
458 if( freewheeling!= 0 )
459 pkthdr->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
460 else
461 pkthdr->sync_state = (jack_nframes_t)deadline_goodness;
462 //printf("goodness=%d\n", deadline_goodness );
464 packet_header_hton (pkthdr);
465 if (cont_miss < 3*latency+5) {
466 int r;
467 for( r=0; r<redundancy; r++ )
468 netjack_sendto (outsockfd, (char *) packet_buf, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
470 else if (cont_miss > 50+5*latency)
472 state_connected = 0;
473 packet_cache_reset_master_address( global_packcache );
474 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
475 cont_miss = 0;
479 framecnt++;
480 return 0;
484 * This is the shutdown callback for this JACK application.
485 * It is called by JACK if the server ever shuts down or
486 * decides to disconnect the client.
489 void
490 jack_shutdown (void *arg)
492 exit (1);
495 void
496 init_sockaddr_in (struct sockaddr_in *name , const char *hostname , uint16_t port)
498 name->sin_family = AF_INET ;
499 name->sin_port = htons (port);
500 if (hostname)
502 struct hostent *hostinfo = gethostbyname (hostname);
503 if (hostinfo == NULL) {
504 fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
505 fflush( stderr );
506 return;
508 #ifdef WIN32
509 name->sin_addr.s_addr = inet_addr( hostname );
510 #else
511 name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
512 #endif
514 else
515 name->sin_addr.s_addr = htonl (INADDR_ANY) ;
519 void
520 printUsage ()
522 fprintf (stderr, "usage: jack_netsource [options]\n"
523 "\n"
524 " -h this help text\n"
525 " -H <slave host> - Host name of the slave JACK\n"
526 " -o <num channels> - Number of audio playback channels\n"
527 " -i <num channels> - Number of audio capture channels\n"
528 " -O <num channels> - Number of midi playback channels\n"
529 " -I <num channels> - Number of midi capture channels\n"
530 " -n <periods> - Network latency in JACK periods\n"
531 " -p <port> - UDP port that the slave is listening on\n"
532 " -r <reply port> - UDP port that we are listening on\n"
533 " -B <bind port> - reply port, for use in NAT environments\n"
534 " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
535 " -c <kbits> - Use CELT encoding with <kbits> kbits per channel\n"
536 " -m <mtu> - Assume this mtu for the link\n"
537 " -R <N> - Redundancy: send out packets N times.\n"
538 " -e - skip host-to-network endianness conversion\n"
539 " -N <jack name> - Reports a different name to jack\n"
540 " -s <server name> - The name of the local jack server\n"
541 "\n");
544 void
545 sigterm_handler( int signal )
547 quit = 1;
551 main (int argc, char *argv[])
553 /* Some startup related basics */
554 char *client_name, *server_name = NULL, *peer_ip;
555 int peer_port = 3000;
556 jack_options_t options = JackNullOption;
557 jack_status_t status;
558 #ifdef WIN32
559 WSADATA wsa;
560 int rc = WSAStartup(MAKEWORD(2,0),&wsa);
561 #endif
562 /* Torben's famous state variables, aka "the reporting API" ! */
563 /* heh ? these are only the copies of them ;) */
564 int statecopy_connected, statecopy_latency, statecopy_netxruns;
565 jack_nframes_t net_period;
566 /* Argument parsing stuff */
567 extern char *optarg;
568 extern int optind, optopt;
569 int errflg=0, c;
571 if (argc < 3)
573 printUsage ();
574 return 1;
577 client_name = (char *) malloc (sizeof (char) * 10);
578 peer_ip = (char *) malloc (sizeof (char) * 10);
579 sprintf(client_name, "netjack");
580 sprintf(peer_ip, "localhost");
582 while ((c = getopt (argc, argv, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:")) != -1)
584 switch (c)
586 case 'h':
587 printUsage();
588 exit (0);
589 break;
590 case 'H':
591 free(peer_ip);
592 peer_ip = (char *) malloc (sizeof (char) * strlen (optarg)+1);
593 strcpy (peer_ip, optarg);
594 break;
595 case 'o':
596 playback_channels_audio = atoi (optarg);
597 break;
598 case 'i':
599 capture_channels_audio = atoi (optarg);
600 break;
601 case 'O':
602 playback_channels_midi = atoi (optarg);
603 break;
604 case 'I':
605 capture_channels_midi = atoi (optarg);
606 break;
607 case 'n':
608 latency = atoi (optarg);
609 break;
610 case 'p':
611 peer_port = atoi (optarg);
612 break;
613 case 'r':
614 reply_port = atoi (optarg);
615 break;
616 case 'B':
617 bind_port = atoi (optarg);
618 break;
619 case 'f':
620 factor = atoi (optarg);
621 printf("This feature is deprecated and will be removed in future netjack versions. CELT offers a superiour way to conserve bandwidth");
622 break;
623 case 'b':
624 bitdepth = atoi (optarg);
625 break;
626 case 'c':
627 #if HAVE_CELT
628 bitdepth = 1000;
629 factor = atoi (optarg);
630 #else
631 printf( "not built with celt supprt\n" );
632 exit(10);
633 #endif
634 break;
635 case 'm':
636 mtu = atoi (optarg);
637 break;
638 case 'R':
639 redundancy = atoi (optarg);
640 break;
641 case 'e':
642 dont_htonl_floats = 1;
643 break;
644 case 'N':
645 free(client_name);
646 client_name = (char *) malloc (sizeof (char) * strlen (optarg)+1);
647 strcpy (client_name, optarg);
648 break;
649 case 's':
650 server_name = (char *) malloc (sizeof (char) * strlen (optarg)+1);
651 strcpy (server_name, optarg);
652 options |= JackServerName;
653 break;
654 case ':':
655 fprintf (stderr, "Option -%c requires an operand\n", optopt);
656 errflg++;
657 break;
658 case '?':
659 fprintf (stderr, "Unrecognized option: -%c\n", optopt);
660 errflg++;
663 if (errflg)
665 printUsage ();
666 exit (2);
669 capture_channels = capture_channels_audio + capture_channels_midi;
670 playback_channels = playback_channels_audio + playback_channels_midi;
672 outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
673 insockfd = socket (AF_INET, SOCK_DGRAM, 0);
675 if( (outsockfd == -1) || (insockfd == -1) ) {
676 fprintf (stderr, "cant open sockets\n" );
677 return 1;
680 init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
681 if (bind_port) {
682 init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
683 if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
684 fprintf (stderr, "bind failure\n" );
687 if (reply_port) {
688 init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
689 if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
690 fprintf (stderr, "bind failure\n" );
694 /* try to become a client of the JACK server */
695 client = jack_client_open (client_name, options, &status, server_name);
696 if (client == NULL)
698 fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
699 "Is the JACK server running ?\n", status);
700 return 1;
703 /* Set up jack callbacks */
704 jack_set_process_callback (client, process, 0);
705 jack_set_sync_callback (client, sync_cb, 0);
706 jack_set_freewheel_callback (client, freewheel_cb, 0);
707 jack_on_shutdown (client, jack_shutdown, 0);
709 alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);
711 if( bitdepth == 1000 )
712 net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8)&(~1) ;
713 else
714 net_period = ceilf((float) jack_get_buffer_size (client) / (float) factor);
716 int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
717 global_packcache = packet_cache_new (latency + 50, rx_bufsize, mtu);
719 /* tell the JACK server that we are ready to roll */
720 if (jack_activate (client))
722 fprintf (stderr, "Cannot activate client");
723 return 1;
726 /* Now sleep forever... and evaluate the state_ vars */
728 signal( SIGTERM, sigterm_handler );
729 signal( SIGINT, sigterm_handler );
731 statecopy_connected = 2; // make it report unconnected on start.
732 statecopy_latency = state_latency;
733 statecopy_netxruns = state_netxruns;
735 while ( !quit )
737 #ifdef WIN32
738 Sleep (1000);
739 #else
740 sleep(1);
741 #endif
742 if (statecopy_connected != state_connected)
744 statecopy_connected = state_connected;
745 if (statecopy_connected)
747 state_netxruns = 1; // We want to reset the netxrun count on each new connection
748 printf ("Connected :-)\n");
750 else
751 printf ("Not Connected\n");
753 fflush(stdout);
756 if (statecopy_connected)
758 if (statecopy_netxruns != state_netxruns) {
759 statecopy_netxruns = state_netxruns;
760 printf ("%s: at frame %06d -> total netxruns %d (%d%%) queue time= %d\n",
761 client_name,
762 state_currentframe,
763 statecopy_netxruns,
764 100*statecopy_netxruns/state_currentframe,
765 state_recv_packet_queue_time);
767 fflush(stdout);
770 else
772 if (statecopy_latency != state_latency)
774 statecopy_latency = state_latency;
775 if (statecopy_latency > 1)
776 printf ("current latency %d\n", statecopy_latency);
777 fflush(stdout);
782 jack_client_close (client);
783 packet_cache_free (global_packcache);
784 exit (0);