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 #define CUSTOM_MODES // for opus_custom_decoder_init
70 #include <opus/opus.h>
71 #include <opus/opus_custom.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;
89 jack_nframes_t factor
= 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;
110 struct sockaddr_in destaddr
;
111 struct sockaddr_in bindaddr
;
113 struct sockaddr destaddr
;
114 struct sockaddr bindaddr
;
118 jack_transport_state_t last_transport_state
;
124 int freewheeling
= 0;
127 * This Function allocates all the I/O Ports which are added the lists.
130 alloc_ports (int n_capture_audio
, int n_playback_audio
, int n_capture_midi
, int n_playback_midi
)
133 int port_flags
= JackPortIsOutput
;
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);
144 printf( "jack_netsource: cannot register %s port\n", buf
);
147 if (bitdepth
== 1000) {
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
) );
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
) );
160 } else if (bitdepth
== 999) {
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
);
172 capture_srcs
= jack_slist_append (capture_srcs
, src_new (SRC_LINEAR
, 1, NULL
));
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);
183 printf ("jack_netsource: cannot register %s port\n", buf
);
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);
196 printf ("jack_netsource: cannot register %s port\n", buf
);
199 if( bitdepth
== 1000 ) {
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
) );
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
) );
212 } else if( bitdepth
== 999 ) {
214 const int kbps
= factor
;
215 printf("new opus encoder %d kbps\n", kbps
);
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
);
230 playback_srcs
= jack_slist_append (playback_srcs
, src_new (SRC_LINEAR
, 1, NULL
));
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);
241 printf ("jack_netsource: cannot register %s port\n", buf
);
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
) {
267 else if (state
== JackTransportStarting
&& last_transport_state
!= JackTransportStarting
) {
269 latency_count
= latency
- 1;
272 last_transport_state
= state
;
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
;
298 const char *porttype
;
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) ;
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.
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
;
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) {
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
) {
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);
370 * ok... now the RECEIVE code.
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.
385 jack_nframes_t got_frame
;
386 if ( ! netjack_poll_deadline( input_fd
, deadline
) )
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
) )
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 );
416 //printf("Frame %d \tRecovered from dropouts\n", framecnt);
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
;
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 */
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;
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);
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
++)
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
);
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
;
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) {
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
) {
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);
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.
513 jack_shutdown (void *arg
)
515 fprintf(stderr
, "JACK shut down, exiting ...\n");
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
);
525 struct hostent
*hostinfo
= gethostbyname (hostname
);
526 if (hostinfo
== NULL
) {
527 fprintf (stderr
, "init_sockaddr_in: unknown host: %s.\n", hostname
);
531 name
->sin_addr
.s_addr
= inet_addr( hostname
);
533 name
->sin_addr
= *(struct in_addr
*) hostinfo
->h_addr
;
536 name
->sin_addr
.s_addr
= htonl (INADDR_ANY
) ;
543 fprintf (stderr
, "usage: jack_netsource [options]\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"
567 sigterm_handler( int signal
)
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
;
582 int rc
= WSAStartup(MAKEWORD(2, 0), &wsa
);
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 */
590 extern int optind
, optopt
;
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) {
611 peer_ip
= (char *) malloc (sizeof (char) * strlen (optarg
) + 1);
612 strcpy (peer_ip
, optarg
);
615 playback_channels_audio
= atoi (optarg
);
618 capture_channels_audio
= atoi (optarg
);
621 playback_channels_midi
= atoi (optarg
);
624 capture_channels_midi
= atoi (optarg
);
627 latency
= atoi (optarg
);
630 peer_port
= atoi (optarg
);
633 reply_port
= atoi (optarg
);
636 bind_port
= atoi (optarg
);
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");
643 bitdepth
= atoi (optarg
);
648 factor
= atoi (optarg
);
650 printf( "not built with celt support\n" );
657 factor
= atoi (optarg
);
659 printf( "not built with opus support\n" );
667 redundancy
= atoi (optarg
);
670 dont_htonl_floats
= 1;
674 client_name
= (char *) malloc (sizeof (char) * strlen (optarg
) + 1);
675 strcpy (client_name
, optarg
);
678 server_name
= (char *) malloc (sizeof (char) * strlen (optarg
) + 1);
679 strcpy (server_name
, optarg
);
680 options
|= JackServerName
;
683 fprintf (stderr
, "Option -%c requires an operand\n", optopt
);
687 fprintf (stderr
, "Unrecognized option: -%c\n", optopt
);
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" );
707 init_sockaddr_in ((struct sockaddr_in
*) &destaddr
, peer_ip
, peer_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" );
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
);
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) ;
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");
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
;
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");
772 printf ("Not Connected\n");
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",
784 100 * statecopy_netxruns
/ state_currentframe
,
785 state_recv_packet_queue_time
);
790 if (statecopy_latency
!= state_latency
) {
791 statecopy_latency
= state_latency
;
792 if (statecopy_latency
> 1)
793 printf ("current latency %d\n", statecopy_latency
);
799 jack_client_close (client
);
800 packet_cache_free (packcache
);