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
46 #include <netinet/in.h>
48 #include <sys/socket.h>
51 /* These two required by FreeBSD. */
52 #include <sys/types.h>
55 #include <jack/jack.h>
57 //#include <net_driver.h>
58 #include <netjack_packet.h>
60 #include <samplerate.h>
64 #include <celt/celt.h>
69 JSList
*capture_ports
= NULL
;
70 JSList
*capture_srcs
= NULL
;
71 int capture_channels
= 0;
72 int capture_channels_audio
= 2;
73 int capture_channels_midi
= 1;
74 JSList
*playback_ports
= NULL
;
75 JSList
*playback_srcs
= NULL
;
76 int playback_channels
= 0;
77 int playback_channels_audio
= 2;
78 int playback_channels_midi
= 1;
79 int dont_htonl_floats
= 0;
82 jack_nframes_t factor
= 1;
88 jack_client_t
*client
;
90 int state_connected
= 0;
91 int state_latency
= 0;
92 int state_netxruns
= 0;
93 int state_currentframe
= 0;
94 int state_recv_packet_queue_time
= 0;
102 struct sockaddr_in destaddr
;
103 struct sockaddr_in bindaddr
;
105 struct sockaddr destaddr
;
106 struct sockaddr bindaddr
;
110 jack_transport_state_t last_transport_state
;
116 int freewheeling
= 0;
119 * This Function allocates all the I/O Ports which are added the lists.
122 alloc_ports (int n_capture_audio
, int n_playback_audio
, int n_capture_midi
, int n_playback_midi
)
125 int port_flags
= JackPortIsOutput
;
130 capture_ports
= NULL
;
131 /* Allocate audio capture channels */
132 for (chn
= 0; chn
< n_capture_audio
; chn
++)
134 snprintf (buf
, sizeof (buf
) - 1, "capture_%u", chn
+ 1);
135 port
= jack_port_register (client
, buf
, JACK_DEFAULT_AUDIO_TYPE
, port_flags
, 0);
138 printf( "jack_netsource: cannot register %s port\n", buf
);
141 if( bitdepth
== 1000 ) {
143 #if HAVE_CELT_API_0_7
144 CELTMode
*celt_mode
= celt_mode_create( jack_get_sample_rate( client
), jack_get_buffer_size(client
), NULL
);
145 capture_srcs
= jack_slist_append(capture_srcs
, celt_decoder_create( celt_mode
, 1, NULL
) );
147 CELTMode
*celt_mode
= celt_mode_create( jack_get_sample_rate( client
), 1, jack_get_buffer_size(client
), NULL
);
148 capture_srcs
= jack_slist_append(capture_srcs
, celt_decoder_create( celt_mode
) );
153 capture_srcs
= jack_slist_append (capture_srcs
, src_new (SRC_LINEAR
, 1, NULL
));
156 capture_ports
= jack_slist_append (capture_ports
, port
);
159 /* Allocate midi capture channels */
160 for (chn
= n_capture_audio
; chn
< n_capture_midi
+ n_capture_audio
; chn
++)
162 snprintf (buf
, sizeof (buf
) - 1, "capture_%u", chn
+ 1);
163 port
= jack_port_register (client
, buf
, JACK_DEFAULT_MIDI_TYPE
, port_flags
, 0);
166 printf ("jack_netsource: cannot register %s port\n", buf
);
169 capture_ports
= jack_slist_append(capture_ports
, port
);
172 /* Allocate audio playback channels */
173 port_flags
= JackPortIsInput
;
174 playback_ports
= NULL
;
175 for (chn
= 0; chn
< n_playback_audio
; chn
++)
177 snprintf (buf
, sizeof (buf
) - 1, "playback_%u", chn
+ 1);
178 port
= jack_port_register (client
, buf
, JACK_DEFAULT_AUDIO_TYPE
, port_flags
, 0);
181 printf ("jack_netsource: cannot register %s port\n", buf
);
184 if( bitdepth
== 1000 ) {
186 #if HAVE_CELT_API_0_7
187 CELTMode
*celt_mode
= celt_mode_create( jack_get_sample_rate (client
), jack_get_buffer_size(client
), NULL
);
188 playback_srcs
= jack_slist_append(playback_srcs
, celt_encoder_create( celt_mode
, 1, NULL
) );
190 CELTMode
*celt_mode
= celt_mode_create( jack_get_sample_rate (client
), 1, jack_get_buffer_size(client
), NULL
);
191 playback_srcs
= jack_slist_append(playback_srcs
, celt_encoder_create( celt_mode
) );
196 playback_srcs
= jack_slist_append (playback_srcs
, src_new (SRC_LINEAR
, 1, NULL
));
199 playback_ports
= jack_slist_append (playback_ports
, port
);
202 /* Allocate midi playback channels */
203 for (chn
= n_playback_audio
; chn
< n_playback_midi
+ n_playback_audio
; chn
++)
205 snprintf (buf
, sizeof (buf
) - 1, "playback_%u", chn
+ 1);
206 port
= jack_port_register (client
, buf
, JACK_DEFAULT_MIDI_TYPE
, port_flags
, 0);
209 printf ("jack_netsource: cannot register %s port\n", buf
);
212 playback_ports
= jack_slist_append (playback_ports
, port
);
217 * The Sync callback... sync state is set elsewhere...
218 * we will see if this is working correctly.
219 * i dont really believe in it yet.
222 sync_cb (jack_transport_state_t state
, jack_position_t
*pos
, void *arg
)
224 static int latency_count
= 0;
225 int retval
= sync_state
;
232 else if (state
== JackTransportStarting
&& last_transport_state
!= JackTransportStarting
)
235 latency_count
= latency
- 1;
238 last_transport_state
= state
;
243 freewheel_cb (int starting
, void *arg
)
245 freewheeling
= starting
;
248 int deadline_goodness
=0;
250 * The process callback for this JACK application.
251 * It is called by JACK at the appropriate times.
254 process (jack_nframes_t nframes
, void *arg
)
256 jack_nframes_t net_period
;
257 int rx_bufsize
, tx_bufsize
;
259 jack_default_audio_sample_t
*buf
;
264 const char *porttype
;
267 jack_position_t local_trans_pos
;
269 uint32_t *packet_buf
, *packet_bufX
;
270 uint32_t *rx_packet_ptr
;
271 jack_time_t packet_recv_timestamp
;
273 if( bitdepth
== 1000 )
274 net_period
= (factor
* jack_get_buffer_size(client
) * 1024 / jack_get_sample_rate(client
) / 8)&(~1) ;
276 net_period
= (float) nframes
/ (float) factor
;
278 rx_bufsize
= get_sample_size (bitdepth
) * capture_channels
* net_period
+ sizeof (jacknet_packet_header
);
279 tx_bufsize
= get_sample_size (bitdepth
) * playback_channels
* net_period
+ sizeof (jacknet_packet_header
);
282 /* Allocate a buffer where both In and Out Buffer will fit */
283 packet_buf
= alloca ((rx_bufsize
> tx_bufsize
) ? rx_bufsize
: tx_bufsize
);
285 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) packet_buf
;
288 * for latency==0 we need to send out the packet before we wait on the reply.
289 * but this introduces a cycle of latency, when netsource is connected to itself.
290 * so we send out before read only in zero latency mode.
295 /* reset packet_bufX... */
296 packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
) / sizeof (jack_default_audio_sample_t
);
298 /* ---------- Send ---------- */
299 render_jack_ports_to_payload (bitdepth
, playback_ports
, playback_srcs
, nframes
,
300 packet_bufX
, net_period
, dont_htonl_floats
);
302 /* fill in packet hdr */
303 pkthdr
->transport_state
= jack_transport_query (client
, &local_trans_pos
);
304 pkthdr
->transport_frame
= local_trans_pos
.frame
;
305 pkthdr
->framecnt
= framecnt
;
306 pkthdr
->latency
= latency
;
307 pkthdr
->reply_port
= reply_port
;
308 pkthdr
->sample_rate
= jack_get_sample_rate (client
);
309 pkthdr
->period_size
= nframes
;
311 /* playback for us is capture on the other side */
312 pkthdr
->capture_channels_audio
= playback_channels_audio
;
313 pkthdr
->playback_channels_audio
= capture_channels_audio
;
314 pkthdr
->capture_channels_midi
= playback_channels_midi
;
315 pkthdr
->playback_channels_midi
= capture_channels_midi
;
317 if( freewheeling
!= 0 )
318 pkthdr
->sync_state
= (jack_nframes_t
)MASTER_FREEWHEELS
;
320 pkthdr
->sync_state
= (jack_nframes_t
)deadline_goodness
;
321 //printf("goodness=%d\n", deadline_goodness );
323 packet_header_hton (pkthdr
);
324 if (cont_miss
< 3*latency
+5) {
326 for( r
=0; r
<redundancy
; r
++ )
327 netjack_sendto (outsockfd
, (char *) packet_buf
, tx_bufsize
, 0, &destaddr
, sizeof (destaddr
), mtu
);
329 else if (cont_miss
> 50+5*latency
)
332 packet_cache_reset_master_address( global_packcache
);
333 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
339 * ok... now the RECEIVE code.
343 /* reset packet_bufX... */
344 packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
) / sizeof (jack_default_audio_sample_t
);
349 input_fd
= outsockfd
;
351 // for latency == 0 we can poll.
352 if( (latency
== 0) || (freewheeling
!=0) ) {
353 jack_time_t deadline
= jack_get_time() + 1000000 * jack_get_buffer_size(client
)/jack_get_sample_rate(client
);
354 // Now loop until we get the right packet.
356 jack_nframes_t got_frame
;
357 if ( ! netjack_poll_deadline( input_fd
, deadline
) )
360 packet_cache_drain_socket(global_packcache
, input_fd
);
362 if (packet_cache_get_next_available_framecnt( global_packcache
, framecnt
- latency
, &got_frame
))
363 if( got_frame
== (framecnt
- latency
) )
368 // only drain socket.
369 packet_cache_drain_socket(global_packcache
, input_fd
);
372 size
= packet_cache_retreive_packet_pointer( global_packcache
, framecnt
- latency
, (char**)&rx_packet_ptr
, rx_bufsize
, &packet_recv_timestamp
);
373 /* First alternative : we received what we expected. Render the data
374 * to the JACK ports so it can be played. */
375 if (size
== rx_bufsize
)
377 packet_buf
= rx_packet_ptr
;
378 pkthdr
= (jacknet_packet_header
*) packet_buf
;
379 packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
) / sizeof (jack_default_audio_sample_t
);
380 // calculate how much time there would have been, if this packet was sent at the deadline.
382 int recv_time_offset
= (int) (jack_get_time() - packet_recv_timestamp
);
383 packet_header_ntoh (pkthdr
);
384 deadline_goodness
= recv_time_offset
- (int)pkthdr
->latency
;
385 //printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
389 //printf("Frame %d \tRecovered from dropouts\n", framecnt);
392 render_payload_to_jack_ports (bitdepth
, packet_bufX
, net_period
,
393 capture_ports
, capture_srcs
, nframes
, dont_htonl_floats
);
395 state_currentframe
= framecnt
;
396 state_recv_packet_queue_time
= recv_time_offset
;
398 sync_state
= pkthdr
->sync_state
;
399 packet_cache_release_packet( global_packcache
, framecnt
- latency
);
401 /* Second alternative : we've received something that's not
402 * as big as expected or we missed a packet. We render silence
403 * to the ouput ports */
406 jack_nframes_t latency_estimate
;
407 if( packet_cache_find_latency( global_packcache
, framecnt
, &latency_estimate
) )
408 //if( (state_latency == 0) || (latency_estimate < state_latency) )
409 state_latency
= latency_estimate
;
411 // Set the counters up.
412 state_currentframe
= framecnt
;
413 //state_latency = framecnt - pkthdr->framecnt;
416 //printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
417 //printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
420 node
= capture_ports
;
423 port
= (jack_port_t
*) node
->data
;
424 buf
= jack_port_get_buffer (port
, nframes
);
425 porttype
= jack_port_type (port
);
426 if (strncmp (porttype
, JACK_DEFAULT_AUDIO_TYPE
, jack_port_type_size ()) == 0)
427 for (i
= 0; i
< nframes
; i
++)
429 else if (strncmp (porttype
, JACK_DEFAULT_MIDI_TYPE
, jack_port_type_size ()) == 0)
430 jack_midi_clear_buffer (buf
);
431 node
= jack_slist_next (node
);
436 /* reset packet_bufX... */
437 packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
) / sizeof (jack_default_audio_sample_t
);
439 /* ---------- Send ---------- */
440 render_jack_ports_to_payload (bitdepth
, playback_ports
, playback_srcs
, nframes
,
441 packet_bufX
, net_period
, dont_htonl_floats
);
443 /* fill in packet hdr */
444 pkthdr
->transport_state
= jack_transport_query (client
, &local_trans_pos
);
445 pkthdr
->transport_frame
= local_trans_pos
.frame
;
446 pkthdr
->framecnt
= framecnt
;
447 pkthdr
->latency
= latency
;
448 pkthdr
->reply_port
= reply_port
;
449 pkthdr
->sample_rate
= jack_get_sample_rate (client
);
450 pkthdr
->period_size
= nframes
;
452 /* playback for us is capture on the other side */
453 pkthdr
->capture_channels_audio
= playback_channels_audio
;
454 pkthdr
->playback_channels_audio
= capture_channels_audio
;
455 pkthdr
->capture_channels_midi
= playback_channels_midi
;
456 pkthdr
->playback_channels_midi
= capture_channels_midi
;
458 if( freewheeling
!= 0 )
459 pkthdr
->sync_state
= (jack_nframes_t
)MASTER_FREEWHEELS
;
461 pkthdr
->sync_state
= (jack_nframes_t
)deadline_goodness
;
462 //printf("goodness=%d\n", deadline_goodness );
464 packet_header_hton (pkthdr
);
465 if (cont_miss
< 3*latency
+5) {
467 for( r
=0; r
<redundancy
; r
++ )
468 netjack_sendto (outsockfd
, (char *) packet_buf
, tx_bufsize
, 0, &destaddr
, sizeof (destaddr
), mtu
);
470 else if (cont_miss
> 50+5*latency
)
473 packet_cache_reset_master_address( global_packcache
);
474 //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
484 * This is the shutdown callback for this JACK application.
485 * It is called by JACK if the server ever shuts down or
486 * decides to disconnect the client.
490 jack_shutdown (void *arg
)
496 init_sockaddr_in (struct sockaddr_in
*name
, const char *hostname
, uint16_t port
)
498 name
->sin_family
= AF_INET
;
499 name
->sin_port
= htons (port
);
502 struct hostent
*hostinfo
= gethostbyname (hostname
);
503 if (hostinfo
== NULL
) {
504 fprintf (stderr
, "init_sockaddr_in: unknown host: %s.\n", hostname
);
508 name
->sin_addr
.s_addr
= inet_addr( hostname
);
510 name
->sin_addr
= *(struct in_addr
*) hostinfo
->h_addr
;
514 name
->sin_addr
.s_addr
= htonl (INADDR_ANY
) ;
521 fprintf (stderr
, "usage: jack_netsource [options]\n"
523 " -h this help text\n"
524 " -H <slave host> - Host name of the slave JACK\n"
525 " -o <num channels> - Number of audio playback channels\n"
526 " -i <num channels> - Number of audio capture channels\n"
527 " -O <num channels> - Number of midi playback channels\n"
528 " -I <num channels> - Number of midi capture channels\n"
529 " -n <periods> - Network latency in JACK periods\n"
530 " -p <port> - UDP port that the slave is listening on\n"
531 " -r <reply port> - UDP port that we are listening on\n"
532 " -B <bind port> - reply port, for use in NAT environments\n"
533 " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
534 " -c <kbits> - Use CELT encoding with <kbits> kbits per channel\n"
535 " -m <mtu> - Assume this mtu for the link\n"
536 " -R <N> - Redundancy: send out packets N times.\n"
537 " -e - skip host-to-network endianness conversion\n"
538 " -N <jack name> - Reports a different name to jack\n"
539 " -s <server name> - The name of the local jack server\n"
544 sigterm_handler( int signal
)
550 main (int argc
, char *argv
[])
552 /* Some startup related basics */
553 char *client_name
, *server_name
= NULL
, *peer_ip
;
554 int peer_port
= 3000;
555 jack_options_t options
= JackNullOption
;
556 jack_status_t status
;
559 int rc
= WSAStartup(MAKEWORD(2,0),&wsa
);
561 /* Torben's famous state variables, aka "the reporting API" ! */
562 /* heh ? these are only the copies of them ;) */
563 int statecopy_connected
, statecopy_latency
, statecopy_netxruns
;
564 jack_nframes_t net_period
;
565 /* Argument parsing stuff */
567 extern int optind
, optopt
;
576 client_name
= (char *) malloc (sizeof (char) * 10);
577 peer_ip
= (char *) malloc (sizeof (char) * 10);
578 sprintf(client_name
, "netjack");
579 sprintf(peer_ip
, "localhost");
581 while ((c
= getopt (argc
, argv
, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:")) != -1)
591 peer_ip
= (char *) malloc (sizeof (char) * strlen (optarg
)+1);
592 strcpy (peer_ip
, optarg
);
595 playback_channels_audio
= atoi (optarg
);
598 capture_channels_audio
= atoi (optarg
);
601 playback_channels_midi
= atoi (optarg
);
604 capture_channels_midi
= atoi (optarg
);
607 latency
= atoi (optarg
);
610 peer_port
= atoi (optarg
);
613 reply_port
= atoi (optarg
);
616 bind_port
= atoi (optarg
);
619 factor
= atoi (optarg
);
620 printf("This feature is deprecated and will be removed in future netjack versions. CELT offers a superiour way to conserve bandwidth");
623 bitdepth
= atoi (optarg
);
628 factor
= atoi (optarg
);
630 printf( "not built with celt supprt\n" );
638 redundancy
= atoi (optarg
);
641 dont_htonl_floats
= 1;
645 client_name
= (char *) malloc (sizeof (char) * strlen (optarg
)+1);
646 strcpy (client_name
, optarg
);
649 server_name
= (char *) malloc (sizeof (char) * strlen (optarg
)+1);
650 strcpy (server_name
, optarg
);
651 options
|= JackServerName
;
654 fprintf (stderr
, "Option -%c requires an operand\n", optopt
);
658 fprintf (stderr
, "Unrecognized option: -%c\n", optopt
);
668 capture_channels
= capture_channels_audio
+ capture_channels_midi
;
669 playback_channels
= playback_channels_audio
+ playback_channels_midi
;
671 outsockfd
= socket (AF_INET
, SOCK_DGRAM
, 0);
672 insockfd
= socket (AF_INET
, SOCK_DGRAM
, 0);
674 if( (outsockfd
== -1) || (insockfd
== -1) ) {
675 fprintf (stderr
, "cant open sockets\n" );
679 init_sockaddr_in ((struct sockaddr_in
*) &destaddr
, peer_ip
, peer_port
);
681 init_sockaddr_in ((struct sockaddr_in
*) &bindaddr
, NULL
, bind_port
);
682 if( bind (outsockfd
, &bindaddr
, sizeof (bindaddr
)) ) {
683 fprintf (stderr
, "bind failure\n" );
688 init_sockaddr_in ((struct sockaddr_in
*) &bindaddr
, NULL
, reply_port
);
689 if( bind (insockfd
, &bindaddr
, sizeof (bindaddr
)) ) {
690 fprintf (stderr
, "bind failure\n" );
694 /* try to become a client of the JACK server */
695 client
= jack_client_open (client_name
, options
, &status
, server_name
);
698 fprintf (stderr
, "jack_client_open() failed, status = 0x%2.0x\n"
699 "Is the JACK server running ?\n", status
);
703 /* Set up jack callbacks */
704 jack_set_process_callback (client
, process
, 0);
705 jack_set_sync_callback (client
, sync_cb
, 0);
706 jack_set_freewheel_callback (client
, freewheel_cb
, 0);
707 jack_on_shutdown (client
, jack_shutdown
, 0);
709 alloc_ports (capture_channels_audio
, playback_channels_audio
, capture_channels_midi
, playback_channels_midi
);
711 if( bitdepth
== 1000 )
712 net_period
= (factor
* jack_get_buffer_size(client
) * 1024 / jack_get_sample_rate(client
) / 8)&(~1) ;
714 net_period
= ceilf((float) jack_get_buffer_size (client
) / (float) factor
);
716 int rx_bufsize
= get_sample_size (bitdepth
) * capture_channels
* net_period
+ sizeof (jacknet_packet_header
);
717 global_packcache
= packet_cache_new (latency
+ 50, rx_bufsize
, mtu
);
719 /* tell the JACK server that we are ready to roll */
720 if (jack_activate (client
))
722 fprintf (stderr
, "Cannot activate client");
726 /* Now sleep forever... and evaluate the state_ vars */
728 signal( SIGTERM
, sigterm_handler
);
729 signal( SIGINT
, sigterm_handler
);
731 statecopy_connected
= 2; // make it report unconnected on start.
732 statecopy_latency
= state_latency
;
733 statecopy_netxruns
= state_netxruns
;
742 if (statecopy_connected
!= state_connected
)
744 statecopy_connected
= state_connected
;
745 if (statecopy_connected
)
747 state_netxruns
= 1; // We want to reset the netxrun count on each new connection
748 printf ("Connected :-)\n");
751 printf ("Not Connected\n");
756 if (statecopy_connected
)
758 if (statecopy_netxruns
!= state_netxruns
) {
759 statecopy_netxruns
= state_netxruns
;
760 printf ("%s: at frame %06d -> total netxruns %d (%d%%) queue time= %d\n",
764 100*statecopy_netxruns
/state_currentframe
,
765 state_recv_packet_queue_time
);
772 if (statecopy_latency
!= state_latency
)
774 statecopy_latency
= state_latency
;
775 if (statecopy_latency
> 1)
776 printf ("current latency %d\n", statecopy_latency
);
782 jack_client_close (client
);
783 packet_cache_free (global_packcache
);