3 * NetJack - Packet Handling functions
5 * used by the driver and the jacknet_client
7 * Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
8 * Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
9 * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
31 #define _XOPEN_SOURCE 600
35 #define _DARWIN_C_SOURCE
51 #include <jack/types.h>
53 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
68 #include <samplerate.h>
72 #include <celt/celt.h>
75 #include "netjack_packet.h"
78 #include "jack/control.h"
81 #define jack_error printf
86 packet_cache
*global_packcache
= NULL
;
89 packet_header_hton (jacknet_packet_header
*pkthdr
)
91 pkthdr
->capture_channels_audio
= htonl(pkthdr
->capture_channels_audio
);
92 pkthdr
->playback_channels_audio
= htonl(pkthdr
->playback_channels_audio
);
93 pkthdr
->capture_channels_midi
= htonl(pkthdr
->capture_channels_midi
);
94 pkthdr
->playback_channels_midi
= htonl(pkthdr
->playback_channels_midi
);
95 pkthdr
->period_size
= htonl(pkthdr
->period_size
);
96 pkthdr
->sample_rate
= htonl(pkthdr
->sample_rate
);
97 pkthdr
->sync_state
= htonl(pkthdr
->sync_state
);
98 pkthdr
->transport_frame
= htonl(pkthdr
->transport_frame
);
99 pkthdr
->transport_state
= htonl(pkthdr
->transport_state
);
100 pkthdr
->framecnt
= htonl(pkthdr
->framecnt
);
101 pkthdr
->latency
= htonl(pkthdr
->latency
);
102 pkthdr
->reply_port
= htonl(pkthdr
->reply_port
);
103 pkthdr
->mtu
= htonl(pkthdr
->mtu
);
104 pkthdr
->fragment_nr
= htonl(pkthdr
->fragment_nr
);
108 packet_header_ntoh (jacknet_packet_header
*pkthdr
)
110 pkthdr
->capture_channels_audio
= ntohl(pkthdr
->capture_channels_audio
);
111 pkthdr
->playback_channels_audio
= ntohl(pkthdr
->playback_channels_audio
);
112 pkthdr
->capture_channels_midi
= ntohl(pkthdr
->capture_channels_midi
);
113 pkthdr
->playback_channels_midi
= ntohl(pkthdr
->playback_channels_midi
);
114 pkthdr
->period_size
= ntohl(pkthdr
->period_size
);
115 pkthdr
->sample_rate
= ntohl(pkthdr
->sample_rate
);
116 pkthdr
->sync_state
= ntohl(pkthdr
->sync_state
);
117 pkthdr
->transport_frame
= ntohl(pkthdr
->transport_frame
);
118 pkthdr
->transport_state
= ntohl(pkthdr
->transport_state
);
119 pkthdr
->framecnt
= ntohl(pkthdr
->framecnt
);
120 pkthdr
->latency
= ntohl(pkthdr
->latency
);
121 pkthdr
->reply_port
= ntohl(pkthdr
->reply_port
);
122 pkthdr
->mtu
= ntohl(pkthdr
->mtu
);
123 pkthdr
->fragment_nr
= ntohl(pkthdr
->fragment_nr
);
126 int get_sample_size (int bitdepth
)
129 return sizeof (int8_t);
131 return sizeof (int16_t);
132 //JN: why? is this for buffer sizes before or after encoding?
133 //JN: if the former, why not int16_t, if the latter, shouldn't it depend on -c N?
134 if( bitdepth
== CELT_MODE
)
135 return sizeof( unsigned char );
136 return sizeof (int32_t);
139 int jack_port_is_audio(const char *porttype
)
141 return (strncmp (porttype
, JACK_DEFAULT_AUDIO_TYPE
, jack_port_type_size()) == 0);
144 int jack_port_is_midi(const char *porttype
)
146 return (strncmp (porttype
, JACK_DEFAULT_MIDI_TYPE
, jack_port_type_size()) == 0);
150 // fragment management functions.
153 *packet_cache_new (int num_packets
, int pkt_size
, int mtu
)
155 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
156 int i
, fragment_number
;
158 if( pkt_size
== sizeof(jacknet_packet_header
) )
161 fragment_number
= (pkt_size
- sizeof (jacknet_packet_header
) - 1) / fragment_payload_size
+ 1;
163 packet_cache
*pcache
= malloc (sizeof (packet_cache
));
166 jack_error ("could not allocate packet cache (1)\n");
170 pcache
->size
= num_packets
;
171 pcache
->packets
= malloc (sizeof (cache_packet
) * num_packets
);
172 pcache
->master_address_valid
= 0;
173 pcache
->last_framecnt_retreived
= 0;
174 pcache
->last_framecnt_retreived_valid
= 0;
176 if (pcache
->packets
== NULL
)
178 jack_error ("could not allocate packet cache (2)\n");
182 for (i
= 0; i
< num_packets
; i
++)
184 pcache
->packets
[i
].valid
= 0;
185 pcache
->packets
[i
].num_fragments
= fragment_number
;
186 pcache
->packets
[i
].packet_size
= pkt_size
;
187 pcache
->packets
[i
].mtu
= mtu
;
188 pcache
->packets
[i
].framecnt
= 0;
189 pcache
->packets
[i
].fragment_array
= malloc (sizeof (char) * fragment_number
);
190 pcache
->packets
[i
].packet_buf
= malloc (pkt_size
);
191 if ((pcache
->packets
[i
].fragment_array
== NULL
) || (pcache
->packets
[i
].packet_buf
== NULL
))
193 jack_error ("could not allocate packet cache (3)\n");
203 packet_cache_free (packet_cache
*pcache
)
209 for (i
= 0; i
< pcache
->size
; i
++)
211 free (pcache
->packets
[i
].fragment_array
);
212 free (pcache
->packets
[i
].packet_buf
);
215 free (pcache
->packets
);
220 *packet_cache_get_packet (packet_cache
*pcache
, jack_nframes_t framecnt
)
223 cache_packet
*retval
;
225 for (i
= 0; i
< pcache
->size
; i
++)
227 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
))
228 return &(pcache
->packets
[i
]);
231 // The Packet is not in the packet cache.
232 // find a free packet.
234 retval
= packet_cache_get_free_packet (pcache
);
237 cache_packet_set_framecnt (retval
, framecnt
);
241 // No Free Packet available
242 // Get The Oldest packet and reset it.
244 retval
= packet_cache_get_oldest_packet (pcache
);
245 //printf( "Dropping %d from Cache :S\n", retval->framecnt );
246 cache_packet_reset (retval
);
247 cache_packet_set_framecnt (retval
, framecnt
);
252 // TODO: fix wrapping case... need to pass
253 // current expected frame here.
255 // or just save framecount into packet_cache.
258 *packet_cache_get_oldest_packet (packet_cache
*pcache
)
260 jack_nframes_t minimal_frame
= JACK_MAX_FRAMES
;
261 cache_packet
*retval
= &(pcache
->packets
[0]);
264 for (i
= 0; i
< pcache
->size
; i
++)
266 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< minimal_frame
))
268 minimal_frame
= pcache
->packets
[i
].framecnt
;
269 retval
= &(pcache
->packets
[i
]);
277 *packet_cache_get_free_packet (packet_cache
*pcache
)
281 for (i
= 0; i
< pcache
->size
; i
++)
283 if (pcache
->packets
[i
].valid
== 0)
284 return &(pcache
->packets
[i
]);
291 cache_packet_reset (cache_packet
*pack
)
296 // XXX: i dont think this is necessary here...
297 // fragement array is cleared in _set_framecnt()
299 for (i
= 0; i
< pack
->num_fragments
; i
++)
300 pack
->fragment_array
[i
] = 0;
304 cache_packet_set_framecnt (cache_packet
*pack
, jack_nframes_t framecnt
)
308 pack
->framecnt
= framecnt
;
310 for (i
= 0; i
< pack
->num_fragments
; i
++)
311 pack
->fragment_array
[i
] = 0;
317 cache_packet_add_fragment (cache_packet
*pack
, char *packet_buf
, int rcv_len
)
319 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) packet_buf
;
320 int fragment_payload_size
= pack
->mtu
- sizeof (jacknet_packet_header
);
321 char *packet_bufX
= pack
->packet_buf
+ sizeof (jacknet_packet_header
);
322 char *dataX
= packet_buf
+ sizeof (jacknet_packet_header
);
324 jack_nframes_t fragment_nr
= ntohl (pkthdr
->fragment_nr
);
325 jack_nframes_t framecnt
= ntohl (pkthdr
->framecnt
);
327 if (framecnt
!= pack
->framecnt
)
329 jack_error ("errror. framecnts dont match\n");
334 if (fragment_nr
== 0)
336 memcpy (pack
->packet_buf
, packet_buf
, rcv_len
);
337 pack
->fragment_array
[0] = 1;
342 if ((fragment_nr
< pack
->num_fragments
) && (fragment_nr
> 0))
344 if ((fragment_nr
* fragment_payload_size
+ rcv_len
- sizeof (jacknet_packet_header
)) <= (pack
->packet_size
- sizeof (jacknet_packet_header
)))
346 memcpy (packet_bufX
+ fragment_nr
* fragment_payload_size
, dataX
, rcv_len
- sizeof (jacknet_packet_header
));
347 pack
->fragment_array
[fragment_nr
] = 1;
350 jack_error ("too long packet received...");
355 cache_packet_is_complete (cache_packet
*pack
)
358 for (i
= 0; i
< pack
->num_fragments
; i
++)
359 if (pack
->fragment_array
[i
] == 0)
366 // new poll using nanoseconds resolution and
367 // not waiting forever.
369 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
374 struct sigaction action
;
376 struct timespec timeout_spec
= { 0, 0 };
383 jack_time_t now
= jack_get_time();
384 if( now
>= deadline
)
387 if( (deadline
-now
) >= 1000000 ) {
388 jack_error( "deadline more than 1 second in the future, trimming it." );
389 deadline
= now
+500000;
392 timeout_spec
.tv_nsec
= (deadline
- now
) * 1000;
394 timeout
= lrintf( (float)(deadline
- now
) / 1000.0 );
397 sigemptyset(&sigmask
);
398 sigaddset(&sigmask
, SIGHUP
);
399 sigaddset(&sigmask
, SIGINT
);
400 sigaddset(&sigmask
, SIGQUIT
);
401 sigaddset(&sigmask
, SIGPIPE
);
402 sigaddset(&sigmask
, SIGTERM
);
403 sigaddset(&sigmask
, SIGUSR1
);
404 sigaddset(&sigmask
, SIGUSR2
);
406 action
.sa_handler
= SIG_DFL
;
407 action
.sa_mask
= sigmask
;
408 action
.sa_flags
= SA_RESTART
;
410 for (i
= 1; i
< NSIG
; i
++)
411 if (sigismember (&sigmask
, i
))
412 sigaction (i
, &action
, 0);
418 poll_err
= ppoll (&fds
, 1, &timeout_spec
, &sigmask
);
420 sigprocmask (SIG_UNBLOCK
, &sigmask
, &rsigmask
);
421 poll_err
= poll (&fds
, 1, timeout
);
422 sigprocmask (SIG_SETMASK
, &rsigmask
, NULL
);
430 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
433 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
436 jack_error ("Error %d: A signal occurred before any requested event", errno
);
439 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
442 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
450 netjack_poll (int sockfd
, int timeout
)
454 sigset_t sigmask
, rsigmask
;
455 struct sigaction action
;
457 sigemptyset(&sigmask
);
458 sigaddset(&sigmask
, SIGHUP
);
459 sigaddset(&sigmask
, SIGINT
);
460 sigaddset(&sigmask
, SIGQUIT
);
461 sigaddset(&sigmask
, SIGPIPE
);
462 sigaddset(&sigmask
, SIGTERM
);
463 sigaddset(&sigmask
, SIGUSR1
);
464 sigaddset(&sigmask
, SIGUSR2
);
466 action
.sa_handler
= SIG_DFL
;
467 action
.sa_mask
= sigmask
;
468 action
.sa_flags
= SA_RESTART
;
470 for (i
= 1; i
< NSIG
; i
++)
471 if (sigismember (&sigmask
, i
))
472 sigaction (i
, &action
, 0);
477 sigprocmask(SIG_UNBLOCK
, &sigmask
, &rsigmask
);
478 while (poll_err
== 0)
480 poll_err
= poll (&fds
, 1, timeout
);
482 sigprocmask(SIG_SETMASK
, &rsigmask
, NULL
);
489 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
492 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
495 jack_error ("Error %d: A signal occurred before any requested event", errno
);
498 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
501 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
511 netjack_poll (int sockfd
, int timeout
)
513 jack_error( "netjack_poll not implemented\n" );
517 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
521 FD_SET( sockfd
, &fds
);
523 struct timeval timeout
;
525 jack_time_t now
= jack_get_time();
526 if( now
>= deadline
)
529 int timeout_usecs
= (deadline
- now
);
530 //jack_error( "timeout = %d", timeout_usecs );
532 timeout
.tv_usec
= (timeout_usecs
< 500) ? 500 : timeout_usecs
;
533 timeout
.tv_usec
= (timeout_usecs
> 1000000) ? 500000 : timeout_usecs
;
535 int poll_err
= select (0, &fds
, NULL
, NULL
, &timeout
);
543 // This now reads all a socket has into the cache.
544 // replacing netjack_recv functions.
547 packet_cache_drain_socket( packet_cache
*pcache
, int sockfd
)
549 char *rx_packet
= alloca (pcache
->mtu
);
550 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) rx_packet
;
552 jack_nframes_t framecnt
;
554 struct sockaddr_in sender_address
;
556 size_t senderlen
= sizeof( struct sockaddr_in
);
558 ioctlsocket( sockfd
, FIONBIO
, &parm
);
560 socklen_t senderlen
= sizeof( struct sockaddr_in
);
565 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, 0,
566 (struct sockaddr
*) &sender_address
, &senderlen
);
568 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, MSG_DONTWAIT
,
569 (struct sockaddr
*) &sender_address
, &senderlen
);
574 if (pcache
->master_address_valid
) {
575 // Verify its from our master.
576 if (memcmp (&sender_address
, &(pcache
->master_address
), senderlen
) != 0)
579 // Setup this one as master
580 //printf( "setup master...\n" );
581 memcpy ( &(pcache
->master_address
), &sender_address
, senderlen
);
582 pcache
->master_address_valid
= 1;
585 framecnt
= ntohl (pkthdr
->framecnt
);
586 if( pcache
->last_framecnt_retreived_valid
&& (framecnt
<= pcache
->last_framecnt_retreived
))
589 cpack
= packet_cache_get_packet (global_packcache
, framecnt
);
590 cache_packet_add_fragment (cpack
, rx_packet
, rcv_len
);
591 cpack
->recv_timestamp
= jack_get_time();
596 packet_cache_reset_master_address( packet_cache
*pcache
)
598 pcache
->master_address_valid
= 0;
599 pcache
->last_framecnt_retreived
= 0;
600 pcache
->last_framecnt_retreived_valid
= 0;
604 packet_cache_clear_old_packets (packet_cache
*pcache
, jack_nframes_t framecnt
)
608 for (i
= 0; i
< pcache
->size
; i
++)
610 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< framecnt
))
612 cache_packet_reset (&(pcache
->packets
[i
]));
618 packet_cache_retreive_packet_pointer( packet_cache
*pcache
, jack_nframes_t framecnt
, char **packet_buf
, int pkt_size
, jack_time_t
*timestamp
)
621 cache_packet
*cpack
= NULL
;
624 for (i
= 0; i
< pcache
->size
; i
++) {
625 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
626 cpack
= &(pcache
->packets
[i
]);
631 if( cpack
== NULL
) {
632 //printf( "retreive packet: %d....not found\n", framecnt );
636 if( !cache_packet_is_complete( cpack
) ) {
640 // ok. cpack is the one we want and its complete.
641 *packet_buf
= cpack
->packet_buf
;
643 *timestamp
= cpack
->recv_timestamp
;
645 pcache
->last_framecnt_retreived_valid
= 1;
646 pcache
->last_framecnt_retreived
= framecnt
;
652 packet_cache_release_packet( packet_cache
*pcache
, jack_nframes_t framecnt
)
655 cache_packet
*cpack
= NULL
;
658 for (i
= 0; i
< pcache
->size
; i
++) {
659 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
660 cpack
= &(pcache
->packets
[i
]);
665 if( cpack
== NULL
) {
666 //printf( "retreive packet: %d....not found\n", framecnt );
670 if( !cache_packet_is_complete( cpack
) ) {
674 cache_packet_reset (cpack
);
675 packet_cache_clear_old_packets( pcache
, framecnt
);
680 packet_cache_get_fill( packet_cache
*pcache
, jack_nframes_t expected_framecnt
)
682 int num_packets_before_us
= 0;
685 for (i
= 0; i
< pcache
->size
; i
++)
687 cache_packet
*cpack
= &(pcache
->packets
[i
]);
688 if (cpack
->valid
&& cache_packet_is_complete( cpack
))
689 if( cpack
->framecnt
>= expected_framecnt
)
690 num_packets_before_us
+= 1;
693 return 100.0 * (float)num_packets_before_us
/ (float)( pcache
->size
) ;
696 // Returns 0 when no valid packet is inside the cache.
698 packet_cache_get_next_available_framecnt( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
701 jack_nframes_t best_offset
= JACK_MAX_FRAMES
/2-1;
704 for (i
= 0; i
< pcache
->size
; i
++)
706 cache_packet
*cpack
= &(pcache
->packets
[i
]);
707 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
709 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
710 //printf( "invalid\n" );
714 if( cpack
->framecnt
< expected_framecnt
)
717 if( (cpack
->framecnt
- expected_framecnt
) > best_offset
) {
721 best_offset
= cpack
->framecnt
- expected_framecnt
;
724 if( best_offset
== 0 )
727 if( retval
&& framecnt
)
728 *framecnt
= expected_framecnt
+ best_offset
;
734 packet_cache_get_highest_available_framecnt( packet_cache
*pcache
, jack_nframes_t
*framecnt
)
737 jack_nframes_t best_value
= 0;
740 for (i
= 0; i
< pcache
->size
; i
++)
742 cache_packet
*cpack
= &(pcache
->packets
[i
]);
743 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
745 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
746 //printf( "invalid\n" );
750 if (cpack
->framecnt
< best_value
) {
754 best_value
= cpack
->framecnt
;
758 if( retval
&& framecnt
)
759 *framecnt
= best_value
;
764 // Returns 0 when no valid packet is inside the cache.
766 packet_cache_find_latency( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
769 jack_nframes_t best_offset
= 0;
772 for (i
= 0; i
< pcache
->size
; i
++)
774 cache_packet
*cpack
= &(pcache
->packets
[i
]);
775 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
777 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
778 //printf( "invalid\n" );
782 if( (cpack
->framecnt
- expected_framecnt
) < best_offset
) {
786 best_offset
= cpack
->framecnt
- expected_framecnt
;
789 if( best_offset
== 0 )
792 if( retval
&& framecnt
)
793 *framecnt
= JACK_MAX_FRAMES
- best_offset
;
797 // fragmented packet IO
799 netjack_recvfrom (int sockfd
, char *packet_buf
, int pkt_size
, int flags
, struct sockaddr
*addr
, size_t *addr_size
, int mtu
)
802 socklen_t from_len
= *addr_size
;
803 if (pkt_size
<= mtu
) {
804 retval
= recvfrom (sockfd
, packet_buf
, pkt_size
, flags
, addr
, &from_len
);
805 *addr_size
= from_len
;
809 char *rx_packet
= alloca (mtu
);
810 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) rx_packet
;
812 jack_nframes_t framecnt
;
816 rcv_len
= recvfrom (sockfd
, rx_packet
, mtu
, 0, addr
, &from_len
);
819 framecnt
= ntohl (pkthdr
->framecnt
);
820 cpack
= packet_cache_get_packet (global_packcache
, framecnt
);
821 cache_packet_add_fragment (cpack
, rx_packet
, rcv_len
);
822 } while (!cache_packet_is_complete (cpack
));
823 memcpy (packet_buf
, cpack
->packet_buf
, pkt_size
);
824 cache_packet_reset (cpack
);
825 *addr_size
= from_len
;
830 netjack_recv (int sockfd
, char *packet_buf
, int pkt_size
, int flags
, int mtu
)
833 return recv (sockfd
, packet_buf
, pkt_size
, flags
);
834 char *rx_packet
= alloca (mtu
);
835 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) rx_packet
;
837 jack_nframes_t framecnt
;
841 rcv_len
= recv (sockfd
, rx_packet
, mtu
, flags
);
844 framecnt
= ntohl (pkthdr
->framecnt
);
845 cpack
= packet_cache_get_packet (global_packcache
, framecnt
);
846 cache_packet_add_fragment (cpack
, rx_packet
, rcv_len
);
847 } while (!cache_packet_is_complete (cpack
));
848 memcpy (packet_buf
, cpack
->packet_buf
, pkt_size
);
849 cache_packet_reset (cpack
);
854 netjack_sendto (int sockfd
, char *packet_buf
, int pkt_size
, int flags
, struct sockaddr
*addr
, int addr_size
, int mtu
)
857 char *tx_packet
, *dataX
;
858 jacknet_packet_header
*pkthdr
;
860 tx_packet
= alloca (mtu
+ 10);
861 dataX
= tx_packet
+ sizeof (jacknet_packet_header
);
862 pkthdr
= (jacknet_packet_header
*) tx_packet
;
864 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
866 if (pkt_size
<= mtu
) {
868 pkthdr
= (jacknet_packet_header
*) packet_buf
;
869 pkthdr
->fragment_nr
= htonl (0);
870 err
= sendto(sockfd
, packet_buf
, pkt_size
, flags
, addr
, addr_size
);
872 //printf( "error in send\n" );
879 // Copy the packet header to the tx pack first.
880 memcpy(tx_packet
, packet_buf
, sizeof (jacknet_packet_header
));
882 // Now loop and send all
883 char *packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
);
885 while (packet_bufX
< (packet_buf
+ pkt_size
- fragment_payload_size
))
887 pkthdr
->fragment_nr
= htonl (frag_cnt
++);
888 memcpy (dataX
, packet_bufX
, fragment_payload_size
);
889 sendto (sockfd
, tx_packet
, mtu
, flags
, addr
, addr_size
);
890 packet_bufX
+= fragment_payload_size
;
893 int last_payload_size
= packet_buf
+ pkt_size
- packet_bufX
;
894 memcpy (dataX
, packet_bufX
, last_payload_size
);
895 pkthdr
->fragment_nr
= htonl (frag_cnt
);
896 //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
898 // sendto(last_pack_size);
899 err
= sendto(sockfd
, tx_packet
, last_payload_size
+ sizeof(jacknet_packet_header
), flags
, addr
, addr_size
);
901 //printf( "error in send\n" );
909 decode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
912 jack_midi_clear_buffer (buf
);
913 for (i
= 0; i
< buffer_size_uint32
- 3;)
915 uint32_t payload_size
;
916 payload_size
= buffer_uint32
[i
];
917 payload_size
= ntohl (payload_size
);
920 jack_midi_event_t event
;
921 event
.time
= ntohl (buffer_uint32
[i
+1]);
922 event
.size
= ntohl (buffer_uint32
[i
+2]);
923 event
.buffer
= (jack_midi_data_t
*) (&(buffer_uint32
[i
+3]));
924 jack_midi_event_write (buf
, event
.time
, event
.buffer
, event
.size
);
926 // skip to the next event
927 unsigned int nb_data_quads
= (((event
.size
-1) & ~0x3) >> 2)+1;
928 i
+= 3+nb_data_quads
;
931 break; // no events can follow an empty event, we're done
936 encode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
939 unsigned int written
= 0;
940 // midi port, encode midi events
941 unsigned int nevents
= jack_midi_get_event_count (buf
);
942 for (i
= 0; i
< nevents
; ++i
)
944 jack_midi_event_t event
;
945 jack_midi_event_get (&event
, buf
, i
);
946 unsigned int nb_data_quads
= (((event
.size
- 1) & ~0x3) >> 2) + 1;
947 unsigned int payload_size
= 3 + nb_data_quads
;
948 // only write if we have sufficient space for the event
950 if (written
+ payload_size
< buffer_size_uint32
- 1)
953 buffer_uint32
[written
]=htonl (payload_size
);
955 buffer_uint32
[written
]=htonl (event
.time
);
957 buffer_uint32
[written
]=htonl (event
.size
);
961 jack_midi_data_t
* tmpbuff
= (jack_midi_data_t
*)(&(buffer_uint32
[written
]));
962 memcpy (tmpbuff
, event
.buffer
, event
.size
);
963 written
+= nb_data_quads
;
968 jack_error ("midi buffer overflow");
972 // now put a netjack_midi 'no-payload' event, signaling EOF
973 buffer_uint32
[written
]=0;
976 // render functions for float
978 render_payload_to_jack_ports_float ( void *packet_payload
, jack_nframes_t net_period_down
, JSList
*capture_ports
, JSList
*capture_srcs
, jack_nframes_t nframes
, int dont_htonl_floats
)
981 JSList
*node
= capture_ports
;
983 JSList
*src_node
= capture_srcs
;
986 uint32_t *packet_bufX
= (uint32_t *)packet_payload
;
988 if( !packet_payload
)
999 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1000 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1002 const char *porttype
= jack_port_type (port
);
1004 if (jack_port_is_audio (porttype
))
1007 // audio port, resample if necessary
1008 if (net_period_down
!= nframes
)
1010 SRC_STATE
*src_state
= src_node
->data
;
1011 for (i
= 0; i
< net_period_down
; i
++)
1013 packet_bufX
[i
] = ntohl (packet_bufX
[i
]);
1016 src
.data_in
= (float *) packet_bufX
;
1017 src
.input_frames
= net_period_down
;
1020 src
.output_frames
= nframes
;
1022 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1023 src
.end_of_input
= 0;
1025 src_set_ratio (src_state
, src
.src_ratio
);
1026 src_process (src_state
, &src
);
1027 src_node
= jack_slist_next (src_node
);
1032 if( dont_htonl_floats
)
1034 memcpy( buf
, packet_bufX
, net_period_down
*sizeof(jack_default_audio_sample_t
));
1038 for (i
= 0; i
< net_period_down
; i
++)
1040 val
.i
= packet_bufX
[i
];
1041 val
.i
= ntohl (val
.i
);
1047 else if (jack_port_is_midi (porttype
))
1049 // midi port, decode midi events
1050 // convert the data buffer to a standard format (uint32_t based)
1051 unsigned int buffer_size_uint32
= net_period_down
;
1052 uint32_t * buffer_uint32
= (uint32_t*)packet_bufX
;
1053 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1055 packet_bufX
= (packet_bufX
+ net_period_down
);
1056 node
= jack_slist_next (node
);
1062 render_jack_ports_to_payload_float (JSList
*playback_ports
, JSList
*playback_srcs
, jack_nframes_t nframes
, void *packet_payload
, jack_nframes_t net_period_up
, int dont_htonl_floats
)
1065 JSList
*node
= playback_ports
;
1067 JSList
*src_node
= playback_srcs
;
1070 uint32_t *packet_bufX
= (uint32_t *) packet_payload
;
1072 while (node
!= NULL
)
1079 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1080 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1082 const char *porttype
= jack_port_type (port
);
1084 if (jack_port_is_audio (porttype
))
1086 // audio port, resample if necessary
1089 if (net_period_up
!= nframes
) {
1090 SRC_STATE
*src_state
= src_node
->data
;
1092 src
.input_frames
= nframes
;
1094 src
.data_out
= (float *) packet_bufX
;
1095 src
.output_frames
= net_period_up
;
1097 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1098 src
.end_of_input
= 0;
1100 src_set_ratio (src_state
, src
.src_ratio
);
1101 src_process (src_state
, &src
);
1103 for (i
= 0; i
< net_period_up
; i
++)
1105 packet_bufX
[i
] = htonl (packet_bufX
[i
]);
1107 src_node
= jack_slist_next (src_node
);
1112 if( dont_htonl_floats
)
1114 memcpy( packet_bufX
, buf
, net_period_up
*sizeof(jack_default_audio_sample_t
) );
1118 for (i
= 0; i
< net_period_up
; i
++)
1121 val
.i
= htonl (val
.i
);
1122 packet_bufX
[i
] = val
.i
;
1127 else if (jack_port_is_midi (porttype
))
1129 // encode midi events from port to packet
1130 // convert the data buffer to a standard format (uint32_t based)
1131 unsigned int buffer_size_uint32
= net_period_up
;
1132 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1133 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1135 packet_bufX
= (packet_bufX
+ net_period_up
);
1136 node
= jack_slist_next (node
);
1141 // render functions for 16bit
1143 render_payload_to_jack_ports_16bit (void *packet_payload
, jack_nframes_t net_period_down
, JSList
*capture_ports
, JSList
*capture_srcs
, jack_nframes_t nframes
)
1146 JSList
*node
= capture_ports
;
1148 JSList
*src_node
= capture_srcs
;
1151 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1153 if( !packet_payload
)
1156 while (node
!= NULL
)
1164 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1165 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1168 float *floatbuf
= alloca (sizeof(float) * net_period_down
);
1170 const char *porttype
= jack_port_type (port
);
1172 if (jack_port_is_audio (porttype
))
1174 // audio port, resample if necessary
1177 if (net_period_down
!= nframes
)
1179 SRC_STATE
*src_state
= src_node
->data
;
1180 for (i
= 0; i
< net_period_down
; i
++)
1182 floatbuf
[i
] = ((float) ntohs(packet_bufX
[i
])) / 32767.0 - 1.0;
1185 src
.data_in
= floatbuf
;
1186 src
.input_frames
= net_period_down
;
1189 src
.output_frames
= nframes
;
1191 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1192 src
.end_of_input
= 0;
1194 src_set_ratio (src_state
, src
.src_ratio
);
1195 src_process (src_state
, &src
);
1196 src_node
= jack_slist_next (src_node
);
1200 for (i
= 0; i
< net_period_down
; i
++)
1201 buf
[i
] = ((float) ntohs (packet_bufX
[i
])) / 32768.0 - 1.0;
1203 else if (jack_port_is_midi (porttype
))
1205 // midi port, decode midi events
1206 // convert the data buffer to a standard format (uint32_t based)
1207 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1208 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1209 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1211 packet_bufX
= (packet_bufX
+ net_period_down
);
1212 node
= jack_slist_next (node
);
1218 render_jack_ports_to_payload_16bit (JSList
*playback_ports
, JSList
*playback_srcs
, jack_nframes_t nframes
, void *packet_payload
, jack_nframes_t net_period_up
)
1221 JSList
*node
= playback_ports
;
1223 JSList
*src_node
= playback_srcs
;
1226 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1228 while (node
!= NULL
)
1234 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1235 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1236 const char *porttype
= jack_port_type (port
);
1238 if (jack_port_is_audio (porttype
))
1240 // audio port, resample if necessary
1243 if (net_period_up
!= nframes
)
1245 SRC_STATE
*src_state
= src_node
->data
;
1247 float *floatbuf
= alloca (sizeof(float) * net_period_up
);
1250 src
.input_frames
= nframes
;
1252 src
.data_out
= floatbuf
;
1253 src
.output_frames
= net_period_up
;
1255 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1256 src
.end_of_input
= 0;
1258 src_set_ratio (src_state
, src
.src_ratio
);
1259 src_process (src_state
, &src
);
1261 for (i
= 0; i
< net_period_up
; i
++)
1263 packet_bufX
[i
] = htons (((uint16_t)((floatbuf
[i
] + 1.0) * 32767.0)));
1265 src_node
= jack_slist_next (src_node
);
1269 for (i
= 0; i
< net_period_up
; i
++)
1270 packet_bufX
[i
] = htons(((uint16_t)((buf
[i
] + 1.0) * 32767.0)));
1272 else if (jack_port_is_midi (porttype
))
1274 // encode midi events from port to packet
1275 // convert the data buffer to a standard format (uint32_t based)
1276 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1277 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1278 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1280 packet_bufX
= (packet_bufX
+ net_period_up
);
1281 node
= jack_slist_next (node
);
1286 // render functions for 8bit
1288 render_payload_to_jack_ports_8bit (void *packet_payload
, jack_nframes_t net_period_down
, JSList
*capture_ports
, JSList
*capture_srcs
, jack_nframes_t nframes
)
1291 JSList
*node
= capture_ports
;
1294 JSList
*src_node
= capture_srcs
;
1297 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1299 if( !packet_payload
)
1302 while (node
!= NULL
)
1310 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1311 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1314 float *floatbuf
= alloca (sizeof (float) * net_period_down
);
1316 const char *porttype
= jack_port_type (port
);
1318 if (jack_port_is_audio(porttype
))
1321 // audio port, resample if necessary
1322 if (net_period_down
!= nframes
)
1324 SRC_STATE
*src_state
= src_node
->data
;
1325 for (i
= 0; i
< net_period_down
; i
++)
1326 floatbuf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1328 src
.data_in
= floatbuf
;
1329 src
.input_frames
= net_period_down
;
1332 src
.output_frames
= nframes
;
1334 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1335 src
.end_of_input
= 0;
1337 src_set_ratio (src_state
, src
.src_ratio
);
1338 src_process (src_state
, &src
);
1339 src_node
= jack_slist_next (src_node
);
1343 for (i
= 0; i
< net_period_down
; i
++)
1344 buf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1346 else if (jack_port_is_midi (porttype
))
1348 // midi port, decode midi events
1349 // convert the data buffer to a standard format (uint32_t based)
1350 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1351 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1352 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1354 packet_bufX
= (packet_bufX
+ net_period_down
);
1355 node
= jack_slist_next (node
);
1361 render_jack_ports_to_payload_8bit (JSList
*playback_ports
, JSList
*playback_srcs
, jack_nframes_t nframes
, void *packet_payload
, jack_nframes_t net_period_up
)
1364 JSList
*node
= playback_ports
;
1366 JSList
*src_node
= playback_srcs
;
1369 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1371 while (node
!= NULL
)
1377 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1379 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1380 const char *porttype
= jack_port_type (port
);
1382 if (jack_port_is_audio (porttype
))
1385 // audio port, resample if necessary
1386 if (net_period_up
!= nframes
)
1389 SRC_STATE
*src_state
= src_node
->data
;
1391 float *floatbuf
= alloca (sizeof (float) * net_period_up
);
1394 src
.input_frames
= nframes
;
1396 src
.data_out
= floatbuf
;
1397 src
.output_frames
= net_period_up
;
1399 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1400 src
.end_of_input
= 0;
1402 src_set_ratio (src_state
, src
.src_ratio
);
1403 src_process (src_state
, &src
);
1405 for (i
= 0; i
< net_period_up
; i
++)
1406 packet_bufX
[i
] = floatbuf
[i
] * 127.0;
1407 src_node
= jack_slist_next (src_node
);
1411 for (i
= 0; i
< net_period_up
; i
++)
1412 packet_bufX
[i
] = buf
[i
] * 127.0;
1414 else if (jack_port_is_midi (porttype
))
1416 // encode midi events from port to packet
1417 // convert the data buffer to a standard format (uint32_t based)
1418 unsigned int buffer_size_uint32
= net_period_up
/ 4;
1419 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1420 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1422 packet_bufX
= (packet_bufX
+ net_period_up
);
1423 node
= jack_slist_next (node
);
1429 // render functions for celt.
1431 render_payload_to_jack_ports_celt (void *packet_payload
, jack_nframes_t net_period_down
, JSList
*capture_ports
, JSList
*capture_srcs
, jack_nframes_t nframes
)
1434 JSList
*node
= capture_ports
;
1435 JSList
*src_node
= capture_srcs
;
1437 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1439 while (node
!= NULL
)
1441 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1442 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1444 const char *porttype
= jack_port_type (port
);
1446 if (jack_port_is_audio (porttype
))
1448 // audio port, decode celt data.
1450 CELTDecoder
*decoder
= src_node
->data
;
1451 if( !packet_payload
)
1452 celt_decode_float( decoder
, NULL
, net_period_down
, buf
);
1454 celt_decode_float( decoder
, packet_bufX
, net_period_down
, buf
);
1456 src_node
= jack_slist_next (src_node
);
1458 else if (jack_port_is_midi (porttype
))
1460 // midi port, decode midi events
1461 // convert the data buffer to a standard format (uint32_t based)
1462 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1463 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1464 if( packet_payload
)
1465 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1467 packet_bufX
= (packet_bufX
+ net_period_down
);
1468 node
= jack_slist_next (node
);
1474 render_jack_ports_to_payload_celt (JSList
*playback_ports
, JSList
*playback_srcs
, jack_nframes_t nframes
, void *packet_payload
, jack_nframes_t net_period_up
)
1477 JSList
*node
= playback_ports
;
1478 JSList
*src_node
= playback_srcs
;
1480 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1482 while (node
!= NULL
)
1484 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1485 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1486 const char *porttype
= jack_port_type (port
);
1488 if (jack_port_is_audio (porttype
))
1490 // audio port, encode celt data.
1493 float *floatbuf
= alloca (sizeof(float) * nframes
);
1494 memcpy( floatbuf
, buf
, nframes
*sizeof(float) );
1495 CELTEncoder
*encoder
= src_node
->data
;
1496 encoded_bytes
= celt_encode_float( encoder
, floatbuf
, NULL
, packet_bufX
, net_period_up
);
1497 if( encoded_bytes
!= net_period_up
)
1498 printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
1499 src_node
= jack_slist_next( src_node
);
1501 else if (jack_port_is_midi (porttype
))
1503 // encode midi events from port to packet
1504 // convert the data buffer to a standard format (uint32_t based)
1505 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1506 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1507 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1509 packet_bufX
= (packet_bufX
+ net_period_up
);
1510 node
= jack_slist_next (node
);
1516 /* Wrapper functions with bitdepth argument... */
1518 render_payload_to_jack_ports (int bitdepth
, void *packet_payload
, jack_nframes_t net_period_down
, JSList
*capture_ports
, JSList
*capture_srcs
, jack_nframes_t nframes
, int dont_htonl_floats
)
1521 render_payload_to_jack_ports_8bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1522 else if (bitdepth
== 16)
1523 render_payload_to_jack_ports_16bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1525 else if (bitdepth
== CELT_MODE
)
1526 render_payload_to_jack_ports_celt (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1529 render_payload_to_jack_ports_float (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
, dont_htonl_floats
);
1533 render_jack_ports_to_payload (int bitdepth
, JSList
*playback_ports
, JSList
*playback_srcs
, jack_nframes_t nframes
, void *packet_payload
, jack_nframes_t net_period_up
, int dont_htonl_floats
)
1536 render_jack_ports_to_payload_8bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1537 else if (bitdepth
== 16)
1538 render_jack_ports_to_payload_16bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1540 else if (bitdepth
== CELT_MODE
)
1541 render_jack_ports_to_payload_celt (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1544 render_jack_ports_to_payload_float (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
, dont_htonl_floats
);