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