3 * NetJack - Packet Handling functions
5 * used by the driver and the jacknet_client
7 * Copyright (C) 2019 Karl Linden <karl.j.linden@gmail.com>
8 * Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
9 * Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
10 * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
30 #if defined(HAVE_CONFIG_H)
35 #define _DARWIN_C_SOURCE
51 #include <jack/types.h>
53 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
69 #include <samplerate.h>
73 #include <celt/celt.h>
77 #include <opus/opus.h>
78 #include <opus/opus_custom.h>
81 #include "netjack_packet.h"
82 #include "JackError.h"
85 #define jack_error printf
91 packet_header_hton (jacknet_packet_header
*pkthdr
)
93 pkthdr
->capture_channels_audio
= htonl(pkthdr
->capture_channels_audio
);
94 pkthdr
->playback_channels_audio
= htonl(pkthdr
->playback_channels_audio
);
95 pkthdr
->capture_channels_midi
= htonl(pkthdr
->capture_channels_midi
);
96 pkthdr
->playback_channels_midi
= htonl(pkthdr
->playback_channels_midi
);
97 pkthdr
->period_size
= htonl(pkthdr
->period_size
);
98 pkthdr
->sample_rate
= htonl(pkthdr
->sample_rate
);
99 pkthdr
->sync_state
= htonl(pkthdr
->sync_state
);
100 pkthdr
->transport_frame
= htonl(pkthdr
->transport_frame
);
101 pkthdr
->transport_state
= htonl(pkthdr
->transport_state
);
102 pkthdr
->framecnt
= htonl(pkthdr
->framecnt
);
103 pkthdr
->latency
= htonl(pkthdr
->latency
);
104 pkthdr
->reply_port
= htonl(pkthdr
->reply_port
);
105 pkthdr
->mtu
= htonl(pkthdr
->mtu
);
106 pkthdr
->fragment_nr
= htonl(pkthdr
->fragment_nr
);
110 packet_header_ntoh (jacknet_packet_header
*pkthdr
)
112 pkthdr
->capture_channels_audio
= ntohl(pkthdr
->capture_channels_audio
);
113 pkthdr
->playback_channels_audio
= ntohl(pkthdr
->playback_channels_audio
);
114 pkthdr
->capture_channels_midi
= ntohl(pkthdr
->capture_channels_midi
);
115 pkthdr
->playback_channels_midi
= ntohl(pkthdr
->playback_channels_midi
);
116 pkthdr
->period_size
= ntohl(pkthdr
->period_size
);
117 pkthdr
->sample_rate
= ntohl(pkthdr
->sample_rate
);
118 pkthdr
->sync_state
= ntohl(pkthdr
->sync_state
);
119 pkthdr
->transport_frame
= ntohl(pkthdr
->transport_frame
);
120 pkthdr
->transport_state
= ntohl(pkthdr
->transport_state
);
121 pkthdr
->framecnt
= ntohl(pkthdr
->framecnt
);
122 pkthdr
->latency
= ntohl(pkthdr
->latency
);
123 pkthdr
->reply_port
= ntohl(pkthdr
->reply_port
);
124 pkthdr
->mtu
= ntohl(pkthdr
->mtu
);
125 pkthdr
->fragment_nr
= ntohl(pkthdr
->fragment_nr
);
128 int get_sample_size (int bitdepth
)
131 return sizeof (int8_t);
133 return sizeof (int16_t);
134 //JN: why? is this for buffer sizes before or after encoding?
135 //JN: if the former, why not int16_t, if the latter, shouldn't it depend on -c N?
136 if( bitdepth
== CELT_MODE
)
137 return sizeof( unsigned char );
138 if( bitdepth
== OPUS_MODE
)
139 return sizeof( unsigned char );
140 return sizeof (int32_t);
143 int jack_port_is_audio(const char *porttype
)
145 return (strncmp (porttype
, JACK_DEFAULT_AUDIO_TYPE
, jack_port_type_size()) == 0);
148 int jack_port_is_midi(const char *porttype
)
150 return (strncmp (porttype
, JACK_DEFAULT_MIDI_TYPE
, jack_port_type_size()) == 0);
154 // fragment management functions.
157 *packet_cache_new (int num_packets
, int pkt_size
, int mtu
)
159 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
160 int i
, fragment_number
;
162 if( pkt_size
== sizeof(jacknet_packet_header
) )
165 fragment_number
= (pkt_size
- sizeof (jacknet_packet_header
) - 1) / fragment_payload_size
+ 1;
167 packet_cache
*pcache
= malloc (sizeof (packet_cache
));
168 if (pcache
== NULL
) {
169 jack_error ("could not allocate packet cache (1)");
173 pcache
->size
= num_packets
;
174 pcache
->packets
= malloc (sizeof (cache_packet
) * num_packets
);
175 pcache
->master_address_valid
= 0;
176 pcache
->last_framecnt_retreived
= 0;
177 pcache
->last_framecnt_retreived_valid
= 0;
179 if (pcache
->packets
== NULL
) {
180 jack_error ("could not allocate packet cache (2)");
184 for (i
= 0; i
< num_packets
; i
++) {
185 pcache
->packets
[i
].valid
= 0;
186 pcache
->packets
[i
].num_fragments
= fragment_number
;
187 pcache
->packets
[i
].packet_size
= pkt_size
;
188 pcache
->packets
[i
].mtu
= mtu
;
189 pcache
->packets
[i
].framecnt
= 0;
190 pcache
->packets
[i
].fragment_array
= malloc (sizeof (char) * fragment_number
);
191 pcache
->packets
[i
].packet_buf
= malloc (pkt_size
);
192 if ((pcache
->packets
[i
].fragment_array
== NULL
) || (pcache
->packets
[i
].packet_buf
== NULL
)) {
193 jack_error ("could not allocate packet cache (3)");
203 packet_cache_free (packet_cache
*pcache
)
209 for (i
= 0; i
< pcache
->size
; i
++) {
210 free (pcache
->packets
[i
].fragment_array
);
211 free (pcache
->packets
[i
].packet_buf
);
214 free (pcache
->packets
);
219 *packet_cache_get_packet (packet_cache
*pcache
, jack_nframes_t framecnt
)
222 cache_packet
*retval
;
224 for (i
= 0; i
< pcache
->size
; i
++) {
225 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
))
226 return &(pcache
->packets
[i
]);
229 // The Packet is not in the packet cache.
230 // find a free packet.
232 retval
= packet_cache_get_free_packet (pcache
);
233 if (retval
!= NULL
) {
234 cache_packet_set_framecnt (retval
, framecnt
);
238 // No Free Packet available
239 // Get The Oldest packet and reset it.
241 retval
= packet_cache_get_oldest_packet (pcache
);
242 //printf( "Dropping %d from Cache :S\n", retval->framecnt );
243 cache_packet_reset (retval
);
244 cache_packet_set_framecnt (retval
, framecnt
);
249 // TODO: fix wrapping case... need to pass
250 // current expected frame here.
252 // or just save framecount into packet_cache.
255 *packet_cache_get_oldest_packet (packet_cache
*pcache
)
257 jack_nframes_t minimal_frame
= JACK_MAX_FRAMES
;
258 cache_packet
*retval
= &(pcache
->packets
[0]);
261 for (i
= 0; i
< pcache
->size
; i
++) {
262 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< minimal_frame
)) {
263 minimal_frame
= pcache
->packets
[i
].framecnt
;
264 retval
= &(pcache
->packets
[i
]);
272 *packet_cache_get_free_packet (packet_cache
*pcache
)
276 for (i
= 0; i
< pcache
->size
; i
++) {
277 if (pcache
->packets
[i
].valid
== 0)
278 return &(pcache
->packets
[i
]);
285 cache_packet_reset (cache_packet
*pack
)
290 // XXX: i don't think this is necessary here...
291 // fragment array is cleared in _set_framecnt()
293 for (i
= 0; i
< pack
->num_fragments
; i
++)
294 pack
->fragment_array
[i
] = 0;
298 cache_packet_set_framecnt (cache_packet
*pack
, jack_nframes_t framecnt
)
302 pack
->framecnt
= framecnt
;
304 for (i
= 0; i
< pack
->num_fragments
; i
++)
305 pack
->fragment_array
[i
] = 0;
311 cache_packet_add_fragment (cache_packet
*pack
, char *packet_buf
, int rcv_len
)
313 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) packet_buf
;
314 int fragment_payload_size
= pack
->mtu
- sizeof (jacknet_packet_header
);
315 char *packet_bufX
= pack
->packet_buf
+ sizeof (jacknet_packet_header
);
316 char *dataX
= packet_buf
+ sizeof (jacknet_packet_header
);
318 jack_nframes_t fragment_nr
= ntohl (pkthdr
->fragment_nr
);
319 jack_nframes_t framecnt
= ntohl (pkthdr
->framecnt
);
321 if (framecnt
!= pack
->framecnt
) {
322 jack_error ("error. framecnts don't match");
326 if (fragment_nr
== 0) {
327 memcpy (pack
->packet_buf
, packet_buf
, rcv_len
);
328 pack
->fragment_array
[0] = 1;
333 if ((fragment_nr
< pack
->num_fragments
) && (fragment_nr
> 0)) {
334 if ((fragment_nr
* fragment_payload_size
+ rcv_len
- sizeof (jacknet_packet_header
)) <= (pack
->packet_size
- sizeof (jacknet_packet_header
))) {
335 memcpy (packet_bufX
+ fragment_nr
* fragment_payload_size
, dataX
, rcv_len
- sizeof (jacknet_packet_header
));
336 pack
->fragment_array
[fragment_nr
] = 1;
338 jack_error ("too long packet received...");
343 cache_packet_is_complete (cache_packet
*pack
)
346 for (i
= 0; i
< pack
->num_fragments
; i
++)
347 if (pack
->fragment_array
[i
] == 0)
354 // new poll using nanoseconds resolution and
355 // not waiting forever.
357 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
362 struct timespec timeout_spec
= { 0, 0 };
367 jack_time_t now
= jack_get_time();
368 if( now
>= deadline
)
371 if( (deadline
- now
) >= 1000000 ) {
372 jack_error( "deadline more than 1 second in the future, trimming it." );
373 deadline
= now
+ 500000;
376 timeout_spec
.tv_nsec
= (deadline
- now
) * 1000;
378 timeout
= lrintf( (float)(deadline
- now
) / 1000.0 );
385 poll_err
= ppoll (&fds
, 1, &timeout_spec
, NULL
);
387 poll_err
= poll (&fds
, 1, timeout
);
390 if (poll_err
== -1) {
393 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
396 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
399 jack_error ("Error %d: A signal occurred before any requested event", errno
);
402 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
405 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
413 netjack_poll (int sockfd
, int timeout
)
417 sigset_t sigmask
, rsigmask
;
418 struct sigaction action
;
420 sigemptyset(&sigmask
);
421 sigaddset(&sigmask
, SIGHUP
);
422 sigaddset(&sigmask
, SIGINT
);
423 sigaddset(&sigmask
, SIGQUIT
);
424 sigaddset(&sigmask
, SIGPIPE
);
425 sigaddset(&sigmask
, SIGTERM
);
426 sigaddset(&sigmask
, SIGUSR1
);
427 sigaddset(&sigmask
, SIGUSR2
);
429 action
.sa_handler
= SIG_DFL
;
430 action
.sa_mask
= sigmask
;
431 action
.sa_flags
= SA_RESTART
;
433 for (i
= 1; i
< NSIG
; i
++)
434 if (sigismember (&sigmask
, i
))
435 sigaction (i
, &action
, 0);
440 sigprocmask(SIG_UNBLOCK
, &sigmask
, &rsigmask
);
441 while (poll_err
== 0) {
442 poll_err
= poll (&fds
, 1, timeout
);
444 sigprocmask(SIG_SETMASK
, &rsigmask
, NULL
);
446 if (poll_err
== -1) {
449 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
452 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
455 jack_error ("Error %d: A signal occurred before any requested event", errno
);
458 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
461 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
471 netjack_poll (int sockfd
, int timeout
)
473 jack_error( "netjack_poll not implemented" );
477 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
481 FD_SET( sockfd
, &fds
);
483 struct timeval timeout
;
485 jack_time_t now
= jack_get_time();
486 if( now
>= deadline
)
489 int timeout_usecs
= (deadline
- now
);
490 //jack_error( "timeout = %d", timeout_usecs );
492 timeout
.tv_usec
= (timeout_usecs
< 500) ? 500 : timeout_usecs
;
493 timeout
.tv_usec
= (timeout_usecs
> 1000000) ? 500000 : timeout_usecs
;
495 int poll_err
= select (0, &fds
, NULL
, NULL
, &timeout
);
503 // This now reads all a socket has into the cache.
504 // replacing netjack_recv functions.
507 packet_cache_drain_socket( packet_cache
*pcache
, int sockfd
)
509 char *rx_packet
= alloca (pcache
->mtu
);
510 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) rx_packet
;
512 jack_nframes_t framecnt
;
514 struct sockaddr_in sender_address
;
516 int senderlen
= sizeof( struct sockaddr_in
);
518 ioctlsocket( sockfd
, FIONBIO
, &parm
);
520 unsigned int senderlen
= sizeof( struct sockaddr_in
);
524 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, 0,
525 (struct sockaddr
*) &sender_address
, &senderlen
);
527 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, MSG_DONTWAIT
,
528 (struct sockaddr
*) &sender_address
, &senderlen
);
533 if (pcache
->master_address_valid
) {
534 // Verify its from our master.
535 if (memcmp (&sender_address
, &(pcache
->master_address
), senderlen
) != 0)
538 // Setup this one as master
539 //printf( "setup master...\n" );
540 memcpy ( &(pcache
->master_address
), &sender_address
, senderlen
);
541 pcache
->master_address_valid
= 1;
544 framecnt
= ntohl (pkthdr
->framecnt
);
545 if( pcache
->last_framecnt_retreived_valid
&& (framecnt
<= pcache
->last_framecnt_retreived
))
548 cpack
= packet_cache_get_packet (pcache
, framecnt
);
549 cache_packet_add_fragment (cpack
, rx_packet
, rcv_len
);
550 cpack
->recv_timestamp
= jack_get_time();
555 packet_cache_reset_master_address( packet_cache
*pcache
)
557 pcache
->master_address_valid
= 0;
558 pcache
->last_framecnt_retreived
= 0;
559 pcache
->last_framecnt_retreived_valid
= 0;
563 packet_cache_clear_old_packets (packet_cache
*pcache
, jack_nframes_t framecnt
)
567 for (i
= 0; i
< pcache
->size
; i
++) {
568 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< framecnt
)) {
569 cache_packet_reset (&(pcache
->packets
[i
]));
575 packet_cache_retreive_packet_pointer( packet_cache
*pcache
, jack_nframes_t framecnt
, char **packet_buf
, int pkt_size
, jack_time_t
*timestamp
)
578 cache_packet
*cpack
= NULL
;
581 for (i
= 0; i
< pcache
->size
; i
++) {
582 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
583 cpack
= &(pcache
->packets
[i
]);
588 if( cpack
== NULL
) {
589 //printf( "retrieve packet: %d....not found\n", framecnt );
593 if( !cache_packet_is_complete( cpack
) ) {
597 // ok. cpack is the one we want and its complete.
598 *packet_buf
= cpack
->packet_buf
;
600 *timestamp
= cpack
->recv_timestamp
;
602 pcache
->last_framecnt_retreived_valid
= 1;
603 pcache
->last_framecnt_retreived
= framecnt
;
609 packet_cache_release_packet( packet_cache
*pcache
, jack_nframes_t framecnt
)
612 cache_packet
*cpack
= NULL
;
615 for (i
= 0; i
< pcache
->size
; i
++) {
616 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
617 cpack
= &(pcache
->packets
[i
]);
622 if( cpack
== NULL
) {
623 //printf( "retrieve packet: %d....not found\n", framecnt );
627 if( !cache_packet_is_complete( cpack
) ) {
631 cache_packet_reset (cpack
);
632 packet_cache_clear_old_packets( pcache
, framecnt
);
637 packet_cache_get_fill( packet_cache
*pcache
, jack_nframes_t expected_framecnt
)
639 int num_packets_before_us
= 0;
642 for (i
= 0; i
< pcache
->size
; i
++) {
643 cache_packet
*cpack
= &(pcache
->packets
[i
]);
644 if (cpack
->valid
&& cache_packet_is_complete( cpack
))
645 if( cpack
->framecnt
>= expected_framecnt
)
646 num_packets_before_us
+= 1;
649 return 100.0 * (float)num_packets_before_us
/ (float)( pcache
->size
);
652 // Returns 0 when no valid packet is inside the cache.
654 packet_cache_get_next_available_framecnt( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
657 jack_nframes_t best_offset
= JACK_MAX_FRAMES
/ 2 - 1;
660 for (i
= 0; i
< pcache
->size
; i
++) {
661 cache_packet
*cpack
= &(pcache
->packets
[i
]);
662 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
664 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
665 //printf( "invalid\n" );
669 if( cpack
->framecnt
< expected_framecnt
)
672 if( (cpack
->framecnt
- expected_framecnt
) > best_offset
) {
676 best_offset
= cpack
->framecnt
- expected_framecnt
;
679 if (best_offset
== 0)
682 if (retval
&& framecnt
)
683 *framecnt
= expected_framecnt
+ best_offset
;
689 packet_cache_get_highest_available_framecnt( packet_cache
*pcache
, jack_nframes_t
*framecnt
)
692 jack_nframes_t best_value
= 0;
695 for (i
= 0; i
< pcache
->size
; i
++) {
696 cache_packet
*cpack
= &(pcache
->packets
[i
]);
697 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
699 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
700 //printf( "invalid\n" );
704 if (cpack
->framecnt
< best_value
) {
708 best_value
= cpack
->framecnt
;
712 if (retval
&& framecnt
)
713 *framecnt
= best_value
;
718 // Returns 0 when no valid packet is inside the cache.
720 packet_cache_find_latency( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
723 jack_nframes_t best_offset
= 0;
726 for (i
= 0; i
< pcache
->size
; i
++) {
727 cache_packet
*cpack
= &(pcache
->packets
[i
]);
728 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
730 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
731 //printf( "invalid\n" );
735 if ((cpack
->framecnt
- expected_framecnt
) < best_offset
) {
739 best_offset
= cpack
->framecnt
- expected_framecnt
;
742 if( best_offset
== 0 )
745 if (retval
&& framecnt
)
746 *framecnt
= JACK_MAX_FRAMES
- best_offset
;
750 // fragmented packet IO
752 netjack_sendto (int sockfd
, char *packet_buf
, int pkt_size
, int flags
, struct sockaddr
*addr
, int addr_size
, int mtu
)
755 char *tx_packet
, *dataX
;
756 jacknet_packet_header
*pkthdr
;
758 tx_packet
= alloca (mtu
+ 10);
759 dataX
= tx_packet
+ sizeof (jacknet_packet_header
);
760 pkthdr
= (jacknet_packet_header
*) tx_packet
;
762 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
764 if (pkt_size
<= mtu
) {
766 pkthdr
= (jacknet_packet_header
*) packet_buf
;
767 pkthdr
->fragment_nr
= htonl (0);
768 err
= sendto(sockfd
, packet_buf
, pkt_size
, flags
, addr
, addr_size
);
770 //printf( "error in send\n" );
775 // Copy the packet header to the tx pack first.
776 memcpy(tx_packet
, packet_buf
, sizeof (jacknet_packet_header
));
778 // Now loop and send all
779 char *packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
);
781 while (packet_bufX
< (packet_buf
+ pkt_size
- fragment_payload_size
)) {
782 pkthdr
->fragment_nr
= htonl (frag_cnt
++);
783 memcpy (dataX
, packet_bufX
, fragment_payload_size
);
784 sendto (sockfd
, tx_packet
, mtu
, flags
, addr
, addr_size
);
785 packet_bufX
+= fragment_payload_size
;
788 int last_payload_size
= packet_buf
+ pkt_size
- packet_bufX
;
789 memcpy (dataX
, packet_bufX
, last_payload_size
);
790 pkthdr
->fragment_nr
= htonl (frag_cnt
);
791 //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
793 // sendto(last_pack_size);
794 err
= sendto(sockfd
, tx_packet
, last_payload_size
+ sizeof(jacknet_packet_header
), flags
, addr
, addr_size
);
796 //printf( "error in send\n" );
803 decode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
806 jack_midi_clear_buffer (buf
);
807 for (i
= 0; i
< buffer_size_uint32
- 3;) {
808 uint32_t payload_size
;
809 payload_size
= buffer_uint32
[i
];
810 payload_size
= ntohl (payload_size
);
812 jack_midi_event_t event
;
813 event
.time
= ntohl (buffer_uint32
[i
+ 1]);
814 event
.size
= ntohl (buffer_uint32
[i
+ 2]);
815 event
.buffer
= (jack_midi_data_t
*) (&(buffer_uint32
[i
+ 3]));
816 jack_midi_event_write (buf
, event
.time
, event
.buffer
, event
.size
);
818 // skip to the next event
819 unsigned int nb_data_quads
= (((event
.size
- 1) & ~0x3) >> 2) + 1;
820 i
+= 3 + nb_data_quads
;
822 break; // no events can follow an empty event, we're done
827 encode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
830 unsigned int written
= 0;
831 // midi port, encode midi events
832 unsigned int nevents
= jack_midi_get_event_count (buf
);
833 for (i
= 0; i
< nevents
; ++i
) {
834 jack_midi_event_t event
;
835 jack_midi_event_get (&event
, buf
, i
);
836 unsigned int nb_data_quads
= (((event
.size
- 1) & ~0x3) >> 2) + 1;
837 unsigned int payload_size
= 3 + nb_data_quads
;
838 // only write if we have sufficient space for the event
840 if (written
+ payload_size
< buffer_size_uint32
- 1) {
842 buffer_uint32
[written
] = htonl (payload_size
);
844 buffer_uint32
[written
] = htonl (event
.time
);
846 buffer_uint32
[written
] = htonl (event
.size
);
850 jack_midi_data_t
* tmpbuff
= (jack_midi_data_t
*)(&(buffer_uint32
[written
]));
851 memcpy (tmpbuff
, event
.buffer
, event
.size
);
852 written
+= nb_data_quads
;
855 jack_error ("midi buffer overflow");
859 // now put a netjack_midi 'no-payload' event, signaling EOF
860 buffer_uint32
[written
] = 0;
863 // render functions for float
865 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
)
868 JSList
*node
= capture_ports
;
870 JSList
*src_node
= capture_srcs
;
873 uint32_t *packet_bufX
= (uint32_t *)packet_payload
;
878 while (node
!= NULL
) {
885 jack_port_t
*port
= (jack_port_t
*) node
->data
;
886 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
888 const char *porttype
= jack_port_type (port
);
890 if (jack_port_is_audio (porttype
)) {
892 // audio port, resample if necessary
893 if (net_period_down
!= nframes
) {
894 SRC_STATE
*src_state
= src_node
->data
;
895 for (i
= 0; i
< net_period_down
; i
++) {
896 packet_bufX
[i
] = ntohl (packet_bufX
[i
]);
899 src
.data_in
= (float *) packet_bufX
;
900 src
.input_frames
= net_period_down
;
903 src
.output_frames
= nframes
;
905 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
906 src
.end_of_input
= 0;
908 src_set_ratio (src_state
, src
.src_ratio
);
909 src_process (src_state
, &src
);
910 src_node
= jack_slist_next (src_node
);
914 if( dont_htonl_floats
) {
915 memcpy( buf
, packet_bufX
, net_period_down
* sizeof(jack_default_audio_sample_t
));
917 for (i
= 0; i
< net_period_down
; i
++) {
918 val
.i
= packet_bufX
[i
];
919 val
.i
= ntohl (val
.i
);
924 } else if (jack_port_is_midi (porttype
)) {
925 // midi port, decode midi events
926 // convert the data buffer to a standard format (uint32_t based)
927 unsigned int buffer_size_uint32
= net_period_down
;
928 uint32_t * buffer_uint32
= (uint32_t*)packet_bufX
;
929 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
931 packet_bufX
= (packet_bufX
+ net_period_down
);
932 node
= jack_slist_next (node
);
938 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
)
941 JSList
*node
= playback_ports
;
943 JSList
*src_node
= playback_srcs
;
946 uint32_t *packet_bufX
= (uint32_t *) packet_payload
;
948 while (node
!= NULL
) {
954 jack_port_t
*port
= (jack_port_t
*) node
->data
;
955 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
957 const char *porttype
= jack_port_type (port
);
959 if (jack_port_is_audio (porttype
)) {
960 // audio port, resample if necessary
963 if (net_period_up
!= nframes
) {
964 SRC_STATE
*src_state
= src_node
->data
;
966 src
.input_frames
= nframes
;
968 src
.data_out
= (float *) packet_bufX
;
969 src
.output_frames
= net_period_up
;
971 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
972 src
.end_of_input
= 0;
974 src_set_ratio (src_state
, src
.src_ratio
);
975 src_process (src_state
, &src
);
977 for (i
= 0; i
< net_period_up
; i
++) {
978 packet_bufX
[i
] = htonl (packet_bufX
[i
]);
980 src_node
= jack_slist_next (src_node
);
984 if( dont_htonl_floats
) {
985 memcpy( packet_bufX
, buf
, net_period_up
* sizeof(jack_default_audio_sample_t
) );
987 for (i
= 0; i
< net_period_up
; i
++) {
989 val
.i
= htonl (val
.i
);
990 packet_bufX
[i
] = val
.i
;
994 } else if (jack_port_is_midi (porttype
)) {
995 // encode midi events from port to packet
996 // convert the data buffer to a standard format (uint32_t based)
997 unsigned int buffer_size_uint32
= net_period_up
;
998 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
999 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1001 packet_bufX
= (packet_bufX
+ net_period_up
);
1002 node
= jack_slist_next (node
);
1007 // render functions for 16bit
1009 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
)
1012 JSList
*node
= capture_ports
;
1014 JSList
*src_node
= capture_srcs
;
1017 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1019 if( !packet_payload
)
1022 while (node
!= NULL
) {
1029 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1030 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1033 float *floatbuf
= alloca (sizeof(float) * net_period_down
);
1035 const char *porttype
= jack_port_type (port
);
1037 if (jack_port_is_audio (porttype
)) {
1038 // audio port, resample if necessary
1041 if (net_period_down
!= nframes
) {
1042 SRC_STATE
*src_state
= src_node
->data
;
1043 for (i
= 0; i
< net_period_down
; i
++) {
1044 floatbuf
[i
] = ((float) ntohs(packet_bufX
[i
])) / 32767.0 - 1.0;
1047 src
.data_in
= floatbuf
;
1048 src
.input_frames
= net_period_down
;
1051 src
.output_frames
= nframes
;
1053 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1054 src
.end_of_input
= 0;
1056 src_set_ratio (src_state
, src
.src_ratio
);
1057 src_process (src_state
, &src
);
1058 src_node
= jack_slist_next (src_node
);
1061 for (i
= 0; i
< net_period_down
; i
++)
1062 buf
[i
] = ((float) ntohs (packet_bufX
[i
])) / 32768.0 - 1.0;
1063 } else if (jack_port_is_midi (porttype
)) {
1064 // midi port, decode midi events
1065 // convert the data buffer to a standard format (uint32_t based)
1066 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1067 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1068 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1070 packet_bufX
= (packet_bufX
+ net_period_down
);
1071 node
= jack_slist_next (node
);
1077 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
)
1080 JSList
*node
= playback_ports
;
1082 JSList
*src_node
= playback_srcs
;
1085 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1087 while (node
!= NULL
) {
1092 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1093 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1094 const char *porttype
= jack_port_type (port
);
1096 if (jack_port_is_audio (porttype
)) {
1097 // audio port, resample if necessary
1100 if (net_period_up
!= nframes
) {
1101 SRC_STATE
*src_state
= src_node
->data
;
1103 float *floatbuf
= alloca (sizeof(float) * net_period_up
);
1106 src
.input_frames
= nframes
;
1108 src
.data_out
= floatbuf
;
1109 src
.output_frames
= net_period_up
;
1111 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1112 src
.end_of_input
= 0;
1114 src_set_ratio (src_state
, src
.src_ratio
);
1115 src_process (src_state
, &src
);
1117 for (i
= 0; i
< net_period_up
; i
++) {
1118 packet_bufX
[i
] = htons (((uint16_t)((floatbuf
[i
] + 1.0) * 32767.0)));
1120 src_node
= jack_slist_next (src_node
);
1123 for (i
= 0; i
< net_period_up
; i
++)
1124 packet_bufX
[i
] = htons(((uint16_t)((buf
[i
] + 1.0) * 32767.0)));
1125 } else if (jack_port_is_midi (porttype
)) {
1126 // encode midi events from port to packet
1127 // convert the data buffer to a standard format (uint32_t based)
1128 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1129 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1130 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1132 packet_bufX
= (packet_bufX
+ net_period_up
);
1133 node
= jack_slist_next (node
);
1138 // render functions for 8bit
1140 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
)
1143 JSList
*node
= capture_ports
;
1146 JSList
*src_node
= capture_srcs
;
1149 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1151 if (!packet_payload
)
1154 while (node
!= NULL
) {
1161 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1162 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1165 float *floatbuf
= alloca (sizeof (float) * net_period_down
);
1167 const char *porttype
= jack_port_type (port
);
1169 if (jack_port_is_audio(porttype
)) {
1171 // audio port, resample if necessary
1172 if (net_period_down
!= nframes
) {
1173 SRC_STATE
*src_state
= src_node
->data
;
1174 for (i
= 0; i
< net_period_down
; i
++)
1175 floatbuf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1177 src
.data_in
= floatbuf
;
1178 src
.input_frames
= net_period_down
;
1181 src
.output_frames
= nframes
;
1183 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1184 src
.end_of_input
= 0;
1186 src_set_ratio (src_state
, src
.src_ratio
);
1187 src_process (src_state
, &src
);
1188 src_node
= jack_slist_next (src_node
);
1191 for (i
= 0; i
< net_period_down
; i
++)
1192 buf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1193 } else if (jack_port_is_midi (porttype
)) {
1194 // midi port, decode midi events
1195 // convert the data buffer to a standard format (uint32_t based)
1196 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1197 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1198 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1200 packet_bufX
= (packet_bufX
+ net_period_down
);
1201 node
= jack_slist_next (node
);
1207 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
)
1210 JSList
*node
= playback_ports
;
1212 JSList
*src_node
= playback_srcs
;
1215 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1217 while (node
!= NULL
) {
1222 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1224 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1225 const char *porttype
= jack_port_type (port
);
1227 if (jack_port_is_audio (porttype
)) {
1229 // audio port, resample if necessary
1230 if (net_period_up
!= nframes
) {
1232 SRC_STATE
*src_state
= src_node
->data
;
1234 float *floatbuf
= alloca (sizeof (float) * net_period_up
);
1237 src
.input_frames
= nframes
;
1239 src
.data_out
= floatbuf
;
1240 src
.output_frames
= net_period_up
;
1242 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1243 src
.end_of_input
= 0;
1245 src_set_ratio (src_state
, src
.src_ratio
);
1246 src_process (src_state
, &src
);
1248 for (i
= 0; i
< net_period_up
; i
++)
1249 packet_bufX
[i
] = floatbuf
[i
] * 127.0;
1250 src_node
= jack_slist_next (src_node
);
1253 for (i
= 0; i
< net_period_up
; i
++)
1254 packet_bufX
[i
] = buf
[i
] * 127.0;
1255 } else if (jack_port_is_midi (porttype
)) {
1256 // encode midi events from port to packet
1257 // convert the data buffer to a standard format (uint32_t based)
1258 unsigned int buffer_size_uint32
= net_period_up
/ 4;
1259 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1260 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1262 packet_bufX
= (packet_bufX
+ net_period_up
);
1263 node
= jack_slist_next (node
);
1269 // render functions for celt.
1271 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
)
1274 JSList
*node
= capture_ports
;
1275 JSList
*src_node
= capture_srcs
;
1277 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1279 while (node
!= NULL
) {
1280 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1281 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1283 const char *porttype
= jack_port_type (port
);
1285 if (jack_port_is_audio (porttype
)) {
1286 // audio port, decode celt data.
1287 CELTDecoder
*decoder
= src_node
->data
;
1288 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1289 if( !packet_payload
)
1290 celt_decode_float( decoder
, NULL
, net_period_down
, buf
, nframes
);
1292 celt_decode_float( decoder
, packet_bufX
, net_period_down
, buf
, nframes
);
1294 if( !packet_payload
)
1295 celt_decode_float( decoder
, NULL
, net_period_down
, buf
);
1297 celt_decode_float( decoder
, packet_bufX
, net_period_down
, buf
);
1300 src_node
= jack_slist_next (src_node
);
1301 } else if (jack_port_is_midi (porttype
)) {
1302 // midi port, decode midi events
1303 // convert the data buffer to a standard format (uint32_t based)
1304 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1305 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1306 if( packet_payload
)
1307 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1309 packet_bufX
= (packet_bufX
+ net_period_down
);
1310 node
= jack_slist_next (node
);
1316 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
)
1319 JSList
*node
= playback_ports
;
1320 JSList
*src_node
= playback_srcs
;
1322 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1324 while (node
!= NULL
) {
1325 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1326 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1327 const char *porttype
= jack_port_type (port
);
1329 if (jack_port_is_audio (porttype
)) {
1330 // audio port, encode celt data.
1333 float *floatbuf
= alloca (sizeof(float) * nframes
);
1334 memcpy( floatbuf
, buf
, nframes
* sizeof(float) );
1335 CELTEncoder
*encoder
= src_node
->data
;
1336 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1337 encoded_bytes
= celt_encode_float( encoder
, floatbuf
, nframes
, packet_bufX
, net_period_up
);
1339 encoded_bytes
= celt_encode_float( encoder
, floatbuf
, NULL
, packet_bufX
, net_period_up
);
1341 if( encoded_bytes
!= net_period_up
)
1342 printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
1343 src_node
= jack_slist_next( src_node
);
1344 } else if (jack_port_is_midi (porttype
)) {
1345 // encode midi events from port to packet
1346 // convert the data buffer to a standard format (uint32_t based)
1347 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1348 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1349 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1351 packet_bufX
= (packet_bufX
+ net_period_up
);
1352 node
= jack_slist_next (node
);
1360 #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
1361 // render functions for Opus.
1363 render_payload_to_jack_ports_opus (void *packet_payload
, jack_nframes_t net_period_down
, JSList
*capture_ports
, JSList
*capture_srcs
, jack_nframes_t nframes
)
1366 JSList
*node
= capture_ports
;
1367 JSList
*src_node
= capture_srcs
;
1369 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1371 while (node
!= NULL
) {
1372 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1373 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1375 const char *porttype
= jack_port_type (port
);
1377 if (jack_port_is_audio (porttype
)) {
1378 // audio port, decode opus data.
1379 OpusCustomDecoder
*decoder
= (OpusCustomDecoder
*) src_node
->data
;
1380 if( !packet_payload
)
1381 memset(buf
, 0, nframes
* sizeof(float));
1384 memcpy(&len
, packet_bufX
, CDO
);
1386 opus_custom_decode_float( decoder
, packet_bufX
+ CDO
, len
, buf
, nframes
);
1389 src_node
= jack_slist_next (src_node
);
1390 } else if (jack_port_is_midi (porttype
)) {
1391 // midi port, decode midi events
1392 // convert the data buffer to a standard format (uint32_t based)
1393 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1394 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1395 if( packet_payload
)
1396 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1398 packet_bufX
= (packet_bufX
+ net_period_down
);
1399 node
= jack_slist_next (node
);
1405 render_jack_ports_to_payload_opus (JSList
*playback_ports
, JSList
*playback_srcs
, jack_nframes_t nframes
, void *packet_payload
, jack_nframes_t net_period_up
)
1408 JSList
*node
= playback_ports
;
1409 JSList
*src_node
= playback_srcs
;
1411 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1413 while (node
!= NULL
) {
1414 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1415 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1416 const char *porttype
= jack_port_type (port
);
1418 if (jack_port_is_audio (porttype
)) {
1419 // audio port, encode opus data.
1422 float *floatbuf
= alloca (sizeof(float) * nframes
);
1423 memcpy( floatbuf
, buf
, nframes
* sizeof(float) );
1424 OpusCustomEncoder
*encoder
= (OpusCustomEncoder
*) src_node
->data
;
1425 encoded_bytes
= opus_custom_encode_float( encoder
, floatbuf
, nframes
, packet_bufX
+ CDO
, net_period_up
- CDO
);
1426 unsigned short len
= htons(encoded_bytes
);
1427 memcpy(packet_bufX
, &len
, CDO
);
1428 src_node
= jack_slist_next( src_node
);
1429 } else if (jack_port_is_midi (porttype
)) {
1430 // encode midi events from port to packet
1431 // convert the data buffer to a standard format (uint32_t based)
1432 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1433 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1434 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1436 packet_bufX
= (packet_bufX
+ net_period_up
);
1437 node
= jack_slist_next (node
);
1443 /* Wrapper functions with bitdepth argument... */
1445 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
)
1448 render_payload_to_jack_ports_8bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1449 else if (bitdepth
== 16)
1450 render_payload_to_jack_ports_16bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1452 else if (bitdepth
== CELT_MODE
)
1453 render_payload_to_jack_ports_celt (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1456 else if (bitdepth
== OPUS_MODE
)
1457 render_payload_to_jack_ports_opus (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1460 render_payload_to_jack_ports_float (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
, dont_htonl_floats
);
1464 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
)
1467 render_jack_ports_to_payload_8bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1468 else if (bitdepth
== 16)
1469 render_jack_ports_to_payload_16bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1471 else if (bitdepth
== CELT_MODE
)
1472 render_jack_ports_to_payload_celt (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1475 else if (bitdepth
== OPUS_MODE
)
1476 render_jack_ports_to_payload_opus (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1479 render_jack_ports_to_payload_float (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
, dont_htonl_floats
);