waf: configure option for enforcing autostart method
[jack2.git] / example-clients / netsource.c
blob9386aecc931d119122fa9b067bfe09ccfe98f6c3
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 #if HAVE_OPUS
66 #include <opus/opus.h>
67 #include <opus/opus_custom.h>
68 #endif
70 #include <math.h>
72 JSList *capture_ports = NULL;
73 JSList *capture_srcs = NULL;
74 int capture_channels = 0;
75 int capture_channels_audio = 2;
76 int capture_channels_midi = 1;
77 JSList *playback_ports = NULL;
78 JSList *playback_srcs = NULL;
79 int playback_channels = 0;
80 int playback_channels_audio = 2;
81 int playback_channels_midi = 1;
82 int dont_htonl_floats = 0;
84 int latency = 5;
85 jack_nframes_t factor = 1;
86 int bitdepth = 0;
87 int mtu = 1400;
88 int reply_port = 0;
89 int bind_port = 0;
90 int redundancy = 1;
91 jack_client_t *client;
92 packet_cache * packcache = 0;
94 int state_connected = 0;
95 int state_latency = 0;
96 int state_netxruns = 0;
97 int state_currentframe = 0;
98 int state_recv_packet_queue_time = 0;
100 int quit = 0;
103 int outsockfd;
104 int insockfd;
105 #ifdef WIN32
106 struct sockaddr_in destaddr;
107 struct sockaddr_in bindaddr;
108 #else
109 struct sockaddr destaddr;
110 struct sockaddr bindaddr;
111 #endif
113 int sync_state;
114 jack_transport_state_t last_transport_state;
116 int framecnt = 0;
118 int cont_miss = 0;
120 int freewheeling = 0;
123 * This Function allocates all the I/O Ports which are added the lists.
125 void
126 alloc_ports (int n_capture_audio, int n_playback_audio, int n_capture_midi, int n_playback_midi)
129 int port_flags = JackPortIsOutput;
130 int chn;
131 jack_port_t *port;
132 char buf[32];
134 capture_ports = NULL;
135 /* Allocate audio capture channels */
136 for (chn = 0; chn < n_capture_audio; chn++) {
137 snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
138 port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
139 if (!port) {
140 printf( "jack_netsource: cannot register %s port\n", buf);
141 break;
143 if (bitdepth == 1000) {
144 #if HAVE_CELT
145 #if HAVE_CELT_API_0_11
146 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
147 capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create_custom( celt_mode, 1, NULL ) );
148 #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
149 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
150 capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode, 1, NULL ) );
151 #else
152 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), 1, jack_get_buffer_size(client), NULL );
153 capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode ) );
154 #endif
155 #endif
156 } else if (bitdepth == 999) {
157 #if HAVE_OPUS
158 int err;
159 OpusCustomMode *opus_mode = opus_custom_mode_create(jack_get_sample_rate( client ), jack_get_buffer_size(client), &err);
160 if (err != OPUS_OK) { printf("OPUS MODE FAILED\n"); }
161 OpusCustomDecoder *decoder = opus_custom_decoder_create(opus_mode, 1, &err);
162 if (err != OPUS_OK) { printf("OPUS DECODER FAILED\n"); }
163 opus_custom_decoder_init(decoder, opus_mode, 1);
164 capture_srcs = jack_slist_append(capture_srcs, decoder);
165 #endif
166 } else {
167 #if HAVE_SAMPLERATE
168 capture_srcs = jack_slist_append (capture_srcs, src_new (SRC_LINEAR, 1, NULL));
169 #endif
171 capture_ports = jack_slist_append (capture_ports, port);
174 /* Allocate midi capture channels */
175 for (chn = n_capture_audio; chn < n_capture_midi + n_capture_audio; chn++) {
176 snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
177 port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
178 if (!port) {
179 printf ("jack_netsource: cannot register %s port\n", buf);
180 break;
182 capture_ports = jack_slist_append(capture_ports, port);
185 /* Allocate audio playback channels */
186 port_flags = JackPortIsInput;
187 playback_ports = NULL;
188 for (chn = 0; chn < n_playback_audio; chn++) {
189 snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
190 port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
191 if (!port) {
192 printf ("jack_netsource: cannot register %s port\n", buf);
193 break;
195 if( bitdepth == 1000 ) {
196 #if HAVE_CELT
197 #if HAVE_CELT_API_0_11
198 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
199 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create_custom( celt_mode, 1, NULL ) );
200 #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
201 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
202 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
203 #else
204 CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), 1, jack_get_buffer_size(client), NULL );
205 playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode ) );
206 #endif
207 #endif
208 } else if( bitdepth == 999 ) {
209 #if HAVE_OPUS
210 const int kbps = factor;
211 printf("new opus encoder %d kbps\n", kbps);
212 int err;
213 OpusCustomMode *opus_mode = opus_custom_mode_create(jack_get_sample_rate (client), jack_get_buffer_size(client), &err ); // XXX free me
214 if (err != OPUS_OK) { printf("OPUS MODE FAILED\n"); }
215 OpusCustomEncoder *oe = opus_custom_encoder_create( opus_mode, 1, &err );
216 if (err != OPUS_OK) { printf("OPUS ENCODER FAILED\n"); }
217 opus_custom_encoder_ctl(oe, OPUS_SET_BITRATE(kbps*1024)); // bits per second
218 opus_custom_encoder_ctl(oe, OPUS_SET_COMPLEXITY(10));
219 opus_custom_encoder_ctl(oe, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));
220 opus_custom_encoder_ctl(oe, OPUS_SET_SIGNAL(OPUS_APPLICATION_RESTRICTED_LOWDELAY));
221 opus_custom_encoder_init(oe, opus_mode, 1);
222 playback_srcs = jack_slist_append(playback_srcs, oe);
223 #endif
224 } else {
225 #if HAVE_SAMPLERATE
226 playback_srcs = jack_slist_append (playback_srcs, src_new (SRC_LINEAR, 1, NULL));
227 #endif
229 playback_ports = jack_slist_append (playback_ports, port);
232 /* Allocate midi playback channels */
233 for (chn = n_playback_audio; chn < n_playback_midi + n_playback_audio; chn++) {
234 snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
235 port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
236 if (!port) {
237 printf ("jack_netsource: cannot register %s port\n", buf);
238 break;
240 playback_ports = jack_slist_append (playback_ports, port);
245 * The Sync callback... sync state is set elsewhere...
246 * we will see if this is working correctly.
247 * i dont really believe in it yet.
250 sync_cb (jack_transport_state_t state, jack_position_t *pos, void *arg)
252 static int latency_count = 0;
253 int retval = sync_state;
255 if (! state_connected) {
256 return 1;
258 if (latency_count) {
259 latency_count--;
260 retval = 0;
263 else if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
264 retval = 0;
265 latency_count = latency - 1;
268 last_transport_state = state;
269 return retval;
272 void
273 freewheel_cb (int starting, void *arg)
275 freewheeling = starting;
278 int deadline_goodness = 0;
280 * The process callback for this JACK application.
281 * It is called by JACK at the appropriate times.
284 process (jack_nframes_t nframes, void *arg)
286 jack_nframes_t net_period;
287 int rx_bufsize, tx_bufsize;
289 jack_default_audio_sample_t *buf;
290 jack_port_t *port;
291 JSList *node;
292 int chn;
293 int size, i;
294 const char *porttype;
295 int input_fd;
297 jack_position_t local_trans_pos;
299 uint32_t *packet_buf_tx, *packet_bufX;
300 uint32_t *rx_packet_ptr;
301 jack_time_t packet_recv_timestamp;
303 if( bitdepth == 1000 || bitdepth == 999)
304 net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8) & (~1) ;
305 else
306 net_period = (float) nframes / (float) factor;
308 rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
309 tx_bufsize = get_sample_size (bitdepth) * playback_channels * net_period + sizeof (jacknet_packet_header);
311 /* Allocate a buffer where both In and Out Buffer will fit */
312 packet_buf_tx = alloca (tx_bufsize);
314 jacknet_packet_header *pkthdr_tx = (jacknet_packet_header *) packet_buf_tx;
317 * for latency==0 we need to send out the packet before we wait on the reply.
318 * but this introduces a cycle of latency, when netsource is connected to itself.
319 * so we send out before read only in zero latency mode.
323 if( latency == 0 ) {
324 /* reset packet_bufX... */
325 packet_bufX = packet_buf_tx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
327 /* ---------- Send ---------- */
328 render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
329 packet_bufX, net_period, dont_htonl_floats);
331 /* fill in packet hdr */
332 pkthdr_tx->transport_state = jack_transport_query (client, &local_trans_pos);
333 pkthdr_tx->transport_frame = local_trans_pos.frame;
334 pkthdr_tx->framecnt = framecnt;
335 pkthdr_tx->latency = latency;
336 pkthdr_tx->reply_port = reply_port;
337 pkthdr_tx->sample_rate = jack_get_sample_rate (client);
338 pkthdr_tx->period_size = nframes;
340 /* playback for us is capture on the other side */
341 pkthdr_tx->capture_channels_audio = playback_channels_audio;
342 pkthdr_tx->playback_channels_audio = capture_channels_audio;
343 pkthdr_tx->capture_channels_midi = playback_channels_midi;
344 pkthdr_tx->playback_channels_midi = capture_channels_midi;
345 pkthdr_tx->mtu = mtu;
346 if( freewheeling != 0 )
347 pkthdr_tx->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
348 else
349 pkthdr_tx->sync_state = (jack_nframes_t)deadline_goodness;
350 //printf("goodness=%d\n", deadline_goodness );
352 packet_header_hton (pkthdr_tx);
353 if (cont_miss < 3 * latency + 5) {
354 int r;
355 for( r = 0; r < redundancy; r++ )
356 netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
357 } else if (cont_miss > 50 + 5 * latency) {
358 state_connected = 0;
359 packet_cache_reset_master_address( packcache );
360 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
361 cont_miss = 0;
366 * ok... now the RECEIVE code.
371 if( reply_port )
372 input_fd = insockfd;
373 else
374 input_fd = outsockfd;
376 // for latency == 0 we can poll.
377 if( (latency == 0) || (freewheeling != 0) ) {
378 jack_time_t deadline = jack_get_time() + 1000000 * jack_get_buffer_size(client) / jack_get_sample_rate(client);
379 // Now loop until we get the right packet.
380 while(1) {
381 jack_nframes_t got_frame;
382 if ( ! netjack_poll_deadline( input_fd, deadline ) )
383 break;
385 packet_cache_drain_socket(packcache, input_fd);
387 if (packet_cache_get_next_available_framecnt( packcache, framecnt - latency, &got_frame ))
388 if( got_frame == (framecnt - latency) )
389 break;
391 } else {
392 // normally:
393 // only drain socket.
394 packet_cache_drain_socket(packcache, input_fd);
397 size = packet_cache_retreive_packet_pointer( packcache, framecnt - latency, (char**)&rx_packet_ptr, rx_bufsize, &packet_recv_timestamp );
398 /* First alternative : we received what we expected. Render the data
399 * to the JACK ports so it can be played. */
400 if (size == rx_bufsize) {
401 uint32_t *packet_buf_rx = rx_packet_ptr;
402 jacknet_packet_header *pkthdr_rx = (jacknet_packet_header *) packet_buf_rx;
403 packet_bufX = packet_buf_rx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
404 // calculate how much time there would have been, if this packet was sent at the deadline.
406 int recv_time_offset = (int) (jack_get_time() - packet_recv_timestamp);
407 packet_header_ntoh (pkthdr_rx);
408 deadline_goodness = recv_time_offset - (int)pkthdr_rx->latency;
409 //printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
411 if (cont_miss) {
412 //printf("Frame %d \tRecovered from dropouts\n", framecnt);
413 cont_miss = 0;
415 render_payload_to_jack_ports (bitdepth, packet_bufX, net_period,
416 capture_ports, capture_srcs, nframes, dont_htonl_floats);
418 state_currentframe = framecnt;
419 state_recv_packet_queue_time = recv_time_offset;
420 state_connected = 1;
421 sync_state = pkthdr_rx->sync_state;
422 packet_cache_release_packet( packcache, framecnt - latency );
424 /* Second alternative : we've received something that's not
425 * as big as expected or we missed a packet. We render silence
426 * to the ouput ports */
427 else {
428 jack_nframes_t latency_estimate;
429 if( packet_cache_find_latency( packcache, framecnt, &latency_estimate ) )
430 //if( (state_latency == 0) || (latency_estimate < state_latency) )
431 state_latency = latency_estimate;
433 // Set the counters up.
434 state_currentframe = framecnt;
435 //state_latency = framecnt - pkthdr->framecnt;
436 state_netxruns += 1;
438 //printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
439 //printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
440 cont_miss += 1;
441 chn = 0;
442 node = capture_ports;
443 while (node != NULL) {
444 port = (jack_port_t *) node->data;
445 buf = jack_port_get_buffer (port, nframes);
446 porttype = jack_port_type (port);
447 if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size ()) == 0)
448 for (i = 0; i < nframes; i++)
449 buf[i] = 0.0;
450 else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size ()) == 0)
451 jack_midi_clear_buffer (buf);
452 node = jack_slist_next (node);
453 chn++;
456 if (latency != 0) {
457 /* reset packet_bufX... */
458 packet_bufX = packet_buf_tx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
460 /* ---------- Send ---------- */
461 render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
462 packet_bufX, net_period, dont_htonl_floats);
464 /* fill in packet hdr */
465 pkthdr_tx->transport_state = jack_transport_query (client, &local_trans_pos);
466 pkthdr_tx->transport_frame = local_trans_pos.frame;
467 pkthdr_tx->framecnt = framecnt;
468 pkthdr_tx->latency = latency;
469 pkthdr_tx->reply_port = reply_port;
470 pkthdr_tx->sample_rate = jack_get_sample_rate (client);
471 pkthdr_tx->period_size = nframes;
473 /* playback for us is capture on the other side */
474 pkthdr_tx->capture_channels_audio = playback_channels_audio;
475 pkthdr_tx->playback_channels_audio = capture_channels_audio;
476 pkthdr_tx->capture_channels_midi = playback_channels_midi;
477 pkthdr_tx->playback_channels_midi = capture_channels_midi;
478 pkthdr_tx->mtu = mtu;
479 if( freewheeling != 0 )
480 pkthdr_tx->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
481 else
482 pkthdr_tx->sync_state = (jack_nframes_t)deadline_goodness;
483 //printf("goodness=%d\n", deadline_goodness );
485 packet_header_hton (pkthdr_tx);
486 if (cont_miss < 3 * latency + 5) {
487 int r;
488 for( r = 0; r < redundancy; r++ )
489 netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
490 } else if (cont_miss > 50 + 5 * latency) {
491 state_connected = 0;
492 packet_cache_reset_master_address( packcache );
493 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
494 cont_miss = 0;
498 framecnt++;
499 return 0;
503 * This is the shutdown callback for this JACK application.
504 * It is called by JACK if the server ever shuts down or
505 * decides to disconnect the client.
508 void
509 jack_shutdown (void *arg)
511 fprintf(stderr, "JACK shut down, exiting ...\n");
512 exit (1);
515 void
516 init_sockaddr_in (struct sockaddr_in *name , const char *hostname , uint16_t port)
518 name->sin_family = AF_INET ;
519 name->sin_port = htons (port);
520 if (hostname) {
521 struct hostent *hostinfo = gethostbyname (hostname);
522 if (hostinfo == NULL) {
523 fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
524 fflush( stderr );
526 #ifdef WIN32
527 name->sin_addr.s_addr = inet_addr( hostname );
528 #else
529 name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
530 #endif
531 } else
532 name->sin_addr.s_addr = htonl (INADDR_ANY) ;
536 void
537 printUsage ()
539 fprintf (stderr, "usage: jack_netsource [options]\n"
540 "\n"
541 " -h this help text\n"
542 " -H <slave host> - Host name of the slave JACK\n"
543 " -o <num channels> - Number of audio playback channels\n"
544 " -i <num channels> - Number of audio capture channels\n"
545 " -O <num channels> - Number of midi playback channels\n"
546 " -I <num channels> - Number of midi capture channels\n"
547 " -n <periods> - Network latency in JACK periods\n"
548 " -p <port> - UDP port that the slave is listening on\n"
549 " -r <reply port> - UDP port that we are listening on\n"
550 " -B <bind port> - reply port, for use in NAT environments\n"
551 " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
552 " -c <kbits> - Use CELT encoding with <kbits> kbits per channel\n"
553 " -P <kbits> - Use Opus encoding with <kbits> kbits per channel\n"
554 " -m <mtu> - Assume this mtu for the link\n"
555 " -R <N> - Redundancy: send out packets N times.\n"
556 " -e - skip host-to-network endianness conversion\n"
557 " -N <jack name> - Reports a different name to jack\n"
558 " -s <server name> - The name of the local jack server\n"
559 "\n");
562 void
563 sigterm_handler( int signal )
565 quit = 1;
569 main (int argc, char *argv[])
571 /* Some startup related basics */
572 char *client_name, *server_name = NULL, *peer_ip;
573 int peer_port = 3000;
574 jack_options_t options = JackNullOption;
575 jack_status_t status;
576 #ifdef WIN32
577 WSADATA wsa;
578 int rc = WSAStartup(MAKEWORD(2, 0), &wsa);
579 #endif
580 /* Torben's famous state variables, aka "the reporting API" ! */
581 /* heh ? these are only the copies of them ;) */
582 int statecopy_connected, statecopy_latency, statecopy_netxruns;
583 jack_nframes_t net_period;
584 /* Argument parsing stuff */
585 extern char *optarg;
586 extern int optind, optopt;
587 int errflg = 0, c;
589 if (argc < 3) {
590 printUsage ();
591 return 1;
594 client_name = (char *) malloc (sizeof (char) * 10);
595 peer_ip = (char *) malloc (sizeof (char) * 10);
596 sprintf(client_name, "netjack");
597 sprintf(peer_ip, "localhost");
599 while ((c = getopt (argc, argv, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:P:")) != -1) {
600 switch (c) {
601 case 'h':
602 printUsage();
603 exit (0);
604 break;
605 case 'H':
606 free(peer_ip);
607 peer_ip = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
608 strcpy (peer_ip, optarg);
609 break;
610 case 'o':
611 playback_channels_audio = atoi (optarg);
612 break;
613 case 'i':
614 capture_channels_audio = atoi (optarg);
615 break;
616 case 'O':
617 playback_channels_midi = atoi (optarg);
618 break;
619 case 'I':
620 capture_channels_midi = atoi (optarg);
621 break;
622 case 'n':
623 latency = atoi (optarg);
624 break;
625 case 'p':
626 peer_port = atoi (optarg);
627 break;
628 case 'r':
629 reply_port = atoi (optarg);
630 break;
631 case 'B':
632 bind_port = atoi (optarg);
633 break;
634 case 'f':
635 factor = atoi (optarg);
636 printf("This feature is deprecated and will be removed in future netjack versions. CELT offers a superiour way to conserve bandwidth");
637 break;
638 case 'b':
639 bitdepth = atoi (optarg);
640 break;
641 case 'c':
642 #if HAVE_CELT
643 bitdepth = 1000;
644 factor = atoi (optarg);
645 #else
646 printf( "not built with celt support\n" );
647 exit(10);
648 #endif
649 break;
650 case 'P':
651 #if HAVE_OPUS
652 bitdepth = 999;
653 factor = atoi (optarg);
654 #else
655 printf( "not built with opus support\n" );
656 exit(10);
657 #endif
658 break;
659 case 'm':
660 mtu = atoi (optarg);
661 break;
662 case 'R':
663 redundancy = atoi (optarg);
664 break;
665 case 'e':
666 dont_htonl_floats = 1;
667 break;
668 case 'N':
669 free(client_name);
670 client_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
671 strcpy (client_name, optarg);
672 break;
673 case 's':
674 server_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
675 strcpy (server_name, optarg);
676 options |= JackServerName;
677 break;
678 case ':':
679 fprintf (stderr, "Option -%c requires an operand\n", optopt);
680 errflg++;
681 break;
682 case '?':
683 fprintf (stderr, "Unrecognized option: -%c\n", optopt);
684 errflg++;
687 if (errflg) {
688 printUsage ();
689 exit (2);
692 capture_channels = capture_channels_audio + capture_channels_midi;
693 playback_channels = playback_channels_audio + playback_channels_midi;
695 outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
696 insockfd = socket (AF_INET, SOCK_DGRAM, 0);
698 if ((outsockfd == -1) || (insockfd == -1)) {
699 fprintf (stderr, "cant open sockets\n" );
700 return 1;
703 init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
704 if (bind_port) {
705 init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
706 if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
707 fprintf (stderr, "bind failure\n" );
710 if (reply_port) {
711 init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
712 if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
713 fprintf (stderr, "bind failure\n" );
717 /* try to become a client of the JACK server */
718 client = jack_client_open (client_name, options, &status, server_name);
719 if (client == NULL) {
720 fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
721 "Is the JACK server running ?\n", status);
722 return 1;
725 /* Set up jack callbacks */
726 jack_set_process_callback (client, process, 0);
727 jack_set_sync_callback (client, sync_cb, 0);
728 jack_set_freewheel_callback (client, freewheel_cb, 0);
729 jack_on_shutdown (client, jack_shutdown, 0);
731 alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);
733 if( bitdepth == 1000 || bitdepth == 999)
734 net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8) & (~1) ;
735 else
736 net_period = ceilf((float) jack_get_buffer_size (client) / (float) factor);
738 int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
739 packcache = packet_cache_new (latency + 50, rx_bufsize, mtu);
741 /* tell the JACK server that we are ready to roll */
742 if (jack_activate (client)) {
743 fprintf (stderr, "Cannot activate client");
744 return 1;
747 /* Now sleep forever... and evaluate the state_ vars */
749 signal( SIGTERM, sigterm_handler );
750 signal( SIGINT, sigterm_handler );
752 statecopy_connected = 2; // make it report unconnected on start.
753 statecopy_latency = state_latency;
754 statecopy_netxruns = state_netxruns;
756 while ( !quit ) {
757 #ifdef WIN32
758 Sleep (1000);
759 #else
760 sleep(1);
761 #endif
762 if (statecopy_connected != state_connected) {
763 statecopy_connected = state_connected;
764 if (statecopy_connected) {
765 state_netxruns = 1; // We want to reset the netxrun count on each new connection
766 printf ("Connected :-)\n");
767 } else
768 printf ("Not Connected\n");
770 fflush(stdout);
773 if (statecopy_connected) {
774 if (statecopy_netxruns != state_netxruns) {
775 statecopy_netxruns = state_netxruns;
776 printf ("%s: at frame %06d -> total netxruns %d (%d%%) queue time= %d\n",
777 client_name,
778 state_currentframe,
779 statecopy_netxruns,
780 100 * statecopy_netxruns / state_currentframe,
781 state_recv_packet_queue_time);
783 fflush(stdout);
785 } else {
786 if (statecopy_latency != state_latency) {
787 statecopy_latency = state_latency;
788 if (statecopy_latency > 1)
789 printf ("current latency %d\n", statecopy_latency);
790 fflush(stdout);
795 jack_client_close (client);
796 packet_cache_free (packcache);
797 exit (0);