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.
26 * @brief This client connects a remote slave JACK to a local JACK server assumed to be the master
37 #if defined(HAVE_CONFIG_H)
46 #include <netinet/in.h>
48 #include <sys/socket.h>
51 /* These two required by FreeBSD. */
52 #include <sys/types.h>
54 #include <jack/jack.h>
56 #include <netjack_packet.h>
58 #include <samplerate.h>
62 #include <celt/celt.h>
66 #include <opus/opus.h>
67 #include <opus/opus_custom.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;
85 jack_nframes_t factor
= 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;
106 struct sockaddr_in destaddr
;
107 struct sockaddr_in bindaddr
;
109 struct sockaddr destaddr
;
110 struct sockaddr bindaddr
;
114 jack_transport_state_t last_transport_state
;
120 int freewheeling
= 0;
123 * This Function allocates all the I/O Ports which are added the lists.
126 alloc_ports (int n_capture_audio
, int n_playback_audio
, int n_capture_midi
, int n_playback_midi
)
129 int port_flags
= JackPortIsOutput
;
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);
140 printf( "jack_netsource: cannot register %s port\n", buf
);
143 if (bitdepth
== 1000) {
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
) );
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
) );
156 } else if (bitdepth
== 999) {
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
);
168 capture_srcs
= jack_slist_append (capture_srcs
, src_new (SRC_LINEAR
, 1, NULL
));
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);
179 printf ("jack_netsource: cannot register %s port\n", buf
);
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);
192 printf ("jack_netsource: cannot register %s port\n", buf
);
195 if( bitdepth
== 1000 ) {
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
) );
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
) );
208 } else if( bitdepth
== 999 ) {
210 const int kbps
= factor
;
211 printf("new opus encoder %d kbps\n", kbps
);
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
);
226 playback_srcs
= jack_slist_append (playback_srcs
, src_new (SRC_LINEAR
, 1, NULL
));
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);
237 printf ("jack_netsource: cannot register %s port\n", buf
);
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
) {
263 else if (state
== JackTransportStarting
&& last_transport_state
!= JackTransportStarting
) {
265 latency_count
= latency
- 1;
268 last_transport_state
= state
;
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
;
294 const char *porttype
;
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) ;
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.
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
;
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) {
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
) {
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);
366 * ok... now the RECEIVE code.
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.
381 jack_nframes_t got_frame
;
382 if ( ! netjack_poll_deadline( input_fd
, deadline
) )
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
) )
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 );
412 //printf("Frame %d \tRecovered from dropouts\n", framecnt);
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
;
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 */
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;
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);
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
++)
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
);
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
;
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) {
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
) {
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);
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.
509 jack_shutdown (void *arg
)
511 fprintf(stderr
, "JACK shut down, exiting ...\n");
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
);
521 struct hostent
*hostinfo
= gethostbyname (hostname
);
522 if (hostinfo
== NULL
) {
523 fprintf (stderr
, "init_sockaddr_in: unknown host: %s.\n", hostname
);
527 name
->sin_addr
.s_addr
= inet_addr( hostname
);
529 name
->sin_addr
= *(struct in_addr
*) hostinfo
->h_addr
;
532 name
->sin_addr
.s_addr
= htonl (INADDR_ANY
) ;
539 fprintf (stderr
, "usage: jack_netsource [options]\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"
563 sigterm_handler( int signal
)
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
;
578 int rc
= WSAStartup(MAKEWORD(2, 0), &wsa
);
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 */
586 extern int optind
, optopt
;
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) {
607 peer_ip
= (char *) malloc (sizeof (char) * strlen (optarg
) + 1);
608 strcpy (peer_ip
, optarg
);
611 playback_channels_audio
= atoi (optarg
);
614 capture_channels_audio
= atoi (optarg
);
617 playback_channels_midi
= atoi (optarg
);
620 capture_channels_midi
= atoi (optarg
);
623 latency
= atoi (optarg
);
626 peer_port
= atoi (optarg
);
629 reply_port
= atoi (optarg
);
632 bind_port
= atoi (optarg
);
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");
639 bitdepth
= atoi (optarg
);
644 factor
= atoi (optarg
);
646 printf( "not built with celt support\n" );
653 factor
= atoi (optarg
);
655 printf( "not built with opus support\n" );
663 redundancy
= atoi (optarg
);
666 dont_htonl_floats
= 1;
670 client_name
= (char *) malloc (sizeof (char) * strlen (optarg
) + 1);
671 strcpy (client_name
, optarg
);
674 server_name
= (char *) malloc (sizeof (char) * strlen (optarg
) + 1);
675 strcpy (server_name
, optarg
);
676 options
|= JackServerName
;
679 fprintf (stderr
, "Option -%c requires an operand\n", optopt
);
683 fprintf (stderr
, "Unrecognized option: -%c\n", optopt
);
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" );
703 init_sockaddr_in ((struct sockaddr_in
*) &destaddr
, peer_ip
, peer_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" );
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
);
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) ;
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");
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
;
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");
768 printf ("Not Connected\n");
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",
780 100 * statecopy_netxruns
/ state_currentframe
,
781 state_recv_packet_queue_time
);
786 if (statecopy_latency
!= state_latency
) {
787 statecopy_latency
= state_latency
;
788 if (statecopy_latency
> 1)
789 printf ("current latency %d\n", statecopy_latency
);
795 jack_client_close (client
);
796 packet_cache_free (packcache
);