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 $
29 #if defined(HAVE_CONFIG_H)
34 #define _DARWIN_C_SOURCE
49 #include <jack/types.h>
51 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
68 #include <samplerate.h>
72 #include <celt/celt.h>
76 #include <opus/opus.h>
77 #include <opus/opus_custom.h>
80 #include "netjack_packet.h"
81 #include "JackError.h"
84 #define jack_error printf
90 packet_header_hton (jacknet_packet_header
*pkthdr
)
92 pkthdr
->capture_channels_audio
= htonl(pkthdr
->capture_channels_audio
);
93 pkthdr
->playback_channels_audio
= htonl(pkthdr
->playback_channels_audio
);
94 pkthdr
->capture_channels_midi
= htonl(pkthdr
->capture_channels_midi
);
95 pkthdr
->playback_channels_midi
= htonl(pkthdr
->playback_channels_midi
);
96 pkthdr
->period_size
= htonl(pkthdr
->period_size
);
97 pkthdr
->sample_rate
= htonl(pkthdr
->sample_rate
);
98 pkthdr
->sync_state
= htonl(pkthdr
->sync_state
);
99 pkthdr
->transport_frame
= htonl(pkthdr
->transport_frame
);
100 pkthdr
->transport_state
= htonl(pkthdr
->transport_state
);
101 pkthdr
->framecnt
= htonl(pkthdr
->framecnt
);
102 pkthdr
->latency
= htonl(pkthdr
->latency
);
103 pkthdr
->reply_port
= htonl(pkthdr
->reply_port
);
104 pkthdr
->mtu
= htonl(pkthdr
->mtu
);
105 pkthdr
->fragment_nr
= htonl(pkthdr
->fragment_nr
);
109 packet_header_ntoh (jacknet_packet_header
*pkthdr
)
111 pkthdr
->capture_channels_audio
= ntohl(pkthdr
->capture_channels_audio
);
112 pkthdr
->playback_channels_audio
= ntohl(pkthdr
->playback_channels_audio
);
113 pkthdr
->capture_channels_midi
= ntohl(pkthdr
->capture_channels_midi
);
114 pkthdr
->playback_channels_midi
= ntohl(pkthdr
->playback_channels_midi
);
115 pkthdr
->period_size
= ntohl(pkthdr
->period_size
);
116 pkthdr
->sample_rate
= ntohl(pkthdr
->sample_rate
);
117 pkthdr
->sync_state
= ntohl(pkthdr
->sync_state
);
118 pkthdr
->transport_frame
= ntohl(pkthdr
->transport_frame
);
119 pkthdr
->transport_state
= ntohl(pkthdr
->transport_state
);
120 pkthdr
->framecnt
= ntohl(pkthdr
->framecnt
);
121 pkthdr
->latency
= ntohl(pkthdr
->latency
);
122 pkthdr
->reply_port
= ntohl(pkthdr
->reply_port
);
123 pkthdr
->mtu
= ntohl(pkthdr
->mtu
);
124 pkthdr
->fragment_nr
= ntohl(pkthdr
->fragment_nr
);
127 int get_sample_size (int bitdepth
)
130 return sizeof (int8_t);
132 return sizeof (int16_t);
133 //JN: why? is this for buffer sizes before or after encoding?
134 //JN: if the former, why not int16_t, if the latter, shouldn't it depend on -c N?
135 if( bitdepth
== CELT_MODE
)
136 return sizeof( unsigned char );
137 if( bitdepth
== OPUS_MODE
)
138 return sizeof( unsigned char );
139 return sizeof (int32_t);
142 int jack_port_is_audio(const char *porttype
)
144 return (strncmp (porttype
, JACK_DEFAULT_AUDIO_TYPE
, jack_port_type_size()) == 0);
147 int jack_port_is_midi(const char *porttype
)
149 return (strncmp (porttype
, JACK_DEFAULT_MIDI_TYPE
, jack_port_type_size()) == 0);
153 // fragment management functions.
156 *packet_cache_new (int num_packets
, int pkt_size
, int mtu
)
158 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
159 int i
, fragment_number
;
161 if( pkt_size
== sizeof(jacknet_packet_header
) )
164 fragment_number
= (pkt_size
- sizeof (jacknet_packet_header
) - 1) / fragment_payload_size
+ 1;
166 packet_cache
*pcache
= malloc (sizeof (packet_cache
));
167 if (pcache
== NULL
) {
168 jack_error ("could not allocate packet cache (1)");
172 pcache
->size
= num_packets
;
173 pcache
->packets
= malloc (sizeof (cache_packet
) * num_packets
);
174 pcache
->master_address_valid
= 0;
175 pcache
->last_framecnt_retreived
= 0;
176 pcache
->last_framecnt_retreived_valid
= 0;
178 if (pcache
->packets
== NULL
) {
179 jack_error ("could not allocate packet cache (2)");
183 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
)) {
192 jack_error ("could not allocate packet cache (3)");
202 packet_cache_free (packet_cache
*pcache
)
208 for (i
= 0; i
< pcache
->size
; i
++) {
209 free (pcache
->packets
[i
].fragment_array
);
210 free (pcache
->packets
[i
].packet_buf
);
213 free (pcache
->packets
);
218 *packet_cache_get_packet (packet_cache
*pcache
, jack_nframes_t framecnt
)
221 cache_packet
*retval
;
223 for (i
= 0; i
< pcache
->size
; i
++) {
224 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
))
225 return &(pcache
->packets
[i
]);
228 // The Packet is not in the packet cache.
229 // find a free packet.
231 retval
= packet_cache_get_free_packet (pcache
);
232 if (retval
!= NULL
) {
233 cache_packet_set_framecnt (retval
, framecnt
);
237 // No Free Packet available
238 // Get The Oldest packet and reset it.
240 retval
= packet_cache_get_oldest_packet (pcache
);
241 //printf( "Dropping %d from Cache :S\n", retval->framecnt );
242 cache_packet_reset (retval
);
243 cache_packet_set_framecnt (retval
, framecnt
);
248 // TODO: fix wrapping case... need to pass
249 // current expected frame here.
251 // or just save framecount into packet_cache.
254 *packet_cache_get_oldest_packet (packet_cache
*pcache
)
256 jack_nframes_t minimal_frame
= JACK_MAX_FRAMES
;
257 cache_packet
*retval
= &(pcache
->packets
[0]);
260 for (i
= 0; i
< pcache
->size
; i
++) {
261 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< minimal_frame
)) {
262 minimal_frame
= pcache
->packets
[i
].framecnt
;
263 retval
= &(pcache
->packets
[i
]);
271 *packet_cache_get_free_packet (packet_cache
*pcache
)
275 for (i
= 0; i
< pcache
->size
; i
++) {
276 if (pcache
->packets
[i
].valid
== 0)
277 return &(pcache
->packets
[i
]);
284 cache_packet_reset (cache_packet
*pack
)
289 // XXX: i dont think this is necessary here...
290 // fragement array is cleared in _set_framecnt()
292 for (i
= 0; i
< pack
->num_fragments
; i
++)
293 pack
->fragment_array
[i
] = 0;
297 cache_packet_set_framecnt (cache_packet
*pack
, jack_nframes_t framecnt
)
301 pack
->framecnt
= framecnt
;
303 for (i
= 0; i
< pack
->num_fragments
; i
++)
304 pack
->fragment_array
[i
] = 0;
310 cache_packet_add_fragment (cache_packet
*pack
, char *packet_buf
, int rcv_len
)
312 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) packet_buf
;
313 int fragment_payload_size
= pack
->mtu
- sizeof (jacknet_packet_header
);
314 char *packet_bufX
= pack
->packet_buf
+ sizeof (jacknet_packet_header
);
315 char *dataX
= packet_buf
+ sizeof (jacknet_packet_header
);
317 jack_nframes_t fragment_nr
= ntohl (pkthdr
->fragment_nr
);
318 jack_nframes_t framecnt
= ntohl (pkthdr
->framecnt
);
320 if (framecnt
!= pack
->framecnt
) {
321 jack_error ("errror. framecnts dont match");
325 if (fragment_nr
== 0) {
326 memcpy (pack
->packet_buf
, packet_buf
, rcv_len
);
327 pack
->fragment_array
[0] = 1;
332 if ((fragment_nr
< pack
->num_fragments
) && (fragment_nr
> 0)) {
333 if ((fragment_nr
* fragment_payload_size
+ rcv_len
- sizeof (jacknet_packet_header
)) <= (pack
->packet_size
- sizeof (jacknet_packet_header
))) {
334 memcpy (packet_bufX
+ fragment_nr
* fragment_payload_size
, dataX
, rcv_len
- sizeof (jacknet_packet_header
));
335 pack
->fragment_array
[fragment_nr
] = 1;
337 jack_error ("too long packet received...");
342 cache_packet_is_complete (cache_packet
*pack
)
345 for (i
= 0; i
< pack
->num_fragments
; i
++)
346 if (pack
->fragment_array
[i
] == 0)
353 // new poll using nanoseconds resolution and
354 // not waiting forever.
356 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
361 struct timespec timeout_spec
= { 0, 0 };
366 jack_time_t now
= jack_get_time();
367 if( now
>= deadline
)
370 if( (deadline
- now
) >= 1000000 ) {
371 jack_error( "deadline more than 1 second in the future, trimming it." );
372 deadline
= now
+ 500000;
375 timeout_spec
.tv_nsec
= (deadline
- now
) * 1000;
377 timeout
= lrintf( (float)(deadline
- now
) / 1000.0 );
384 poll_err
= ppoll (&fds
, 1, &timeout_spec
, NULL
);
386 poll_err
= poll (&fds
, 1, timeout
);
389 if (poll_err
== -1) {
392 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
395 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
398 jack_error ("Error %d: A signal occurred before any requested event", errno
);
401 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
404 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
412 netjack_poll (int sockfd
, int timeout
)
416 sigset_t sigmask
, rsigmask
;
417 struct sigaction action
;
419 sigemptyset(&sigmask
);
420 sigaddset(&sigmask
, SIGHUP
);
421 sigaddset(&sigmask
, SIGINT
);
422 sigaddset(&sigmask
, SIGQUIT
);
423 sigaddset(&sigmask
, SIGPIPE
);
424 sigaddset(&sigmask
, SIGTERM
);
425 sigaddset(&sigmask
, SIGUSR1
);
426 sigaddset(&sigmask
, SIGUSR2
);
428 action
.sa_handler
= SIG_DFL
;
429 action
.sa_mask
= sigmask
;
430 action
.sa_flags
= SA_RESTART
;
432 for (i
= 1; i
< NSIG
; i
++)
433 if (sigismember (&sigmask
, i
))
434 sigaction (i
, &action
, 0);
439 sigprocmask(SIG_UNBLOCK
, &sigmask
, &rsigmask
);
440 while (poll_err
== 0) {
441 poll_err
= poll (&fds
, 1, timeout
);
443 sigprocmask(SIG_SETMASK
, &rsigmask
, NULL
);
445 if (poll_err
== -1) {
448 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
451 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
454 jack_error ("Error %d: A signal occurred before any requested event", errno
);
457 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
460 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
470 netjack_poll (int sockfd
, int timeout
)
472 jack_error( "netjack_poll not implemented" );
476 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
480 FD_SET( sockfd
, &fds
);
482 struct timeval timeout
;
484 jack_time_t now
= jack_get_time();
485 if( now
>= deadline
)
488 int timeout_usecs
= (deadline
- now
);
489 //jack_error( "timeout = %d", timeout_usecs );
491 timeout
.tv_usec
= (timeout_usecs
< 500) ? 500 : timeout_usecs
;
492 timeout
.tv_usec
= (timeout_usecs
> 1000000) ? 500000 : timeout_usecs
;
494 int poll_err
= select (0, &fds
, NULL
, NULL
, &timeout
);
502 // This now reads all a socket has into the cache.
503 // replacing netjack_recv functions.
506 packet_cache_drain_socket( packet_cache
*pcache
, int sockfd
)
508 char *rx_packet
= alloca (pcache
->mtu
);
509 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) rx_packet
;
511 jack_nframes_t framecnt
;
513 struct sockaddr_in sender_address
;
515 int senderlen
= sizeof( struct sockaddr_in
);
517 ioctlsocket( sockfd
, FIONBIO
, &parm
);
519 unsigned int senderlen
= sizeof( struct sockaddr_in
);
523 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, 0,
524 (struct sockaddr
*) &sender_address
, &senderlen
);
526 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, MSG_DONTWAIT
,
527 (struct sockaddr
*) &sender_address
, &senderlen
);
532 if (pcache
->master_address_valid
) {
533 // Verify its from our master.
534 if (memcmp (&sender_address
, &(pcache
->master_address
), senderlen
) != 0)
537 // Setup this one as master
538 //printf( "setup master...\n" );
539 memcpy ( &(pcache
->master_address
), &sender_address
, senderlen
);
540 pcache
->master_address_valid
= 1;
543 framecnt
= ntohl (pkthdr
->framecnt
);
544 if( pcache
->last_framecnt_retreived_valid
&& (framecnt
<= pcache
->last_framecnt_retreived
))
547 cpack
= packet_cache_get_packet (pcache
, framecnt
);
548 cache_packet_add_fragment (cpack
, rx_packet
, rcv_len
);
549 cpack
->recv_timestamp
= jack_get_time();
554 packet_cache_reset_master_address( packet_cache
*pcache
)
556 pcache
->master_address_valid
= 0;
557 pcache
->last_framecnt_retreived
= 0;
558 pcache
->last_framecnt_retreived_valid
= 0;
562 packet_cache_clear_old_packets (packet_cache
*pcache
, jack_nframes_t framecnt
)
566 for (i
= 0; i
< pcache
->size
; i
++) {
567 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< framecnt
)) {
568 cache_packet_reset (&(pcache
->packets
[i
]));
574 packet_cache_retreive_packet_pointer( packet_cache
*pcache
, jack_nframes_t framecnt
, char **packet_buf
, int pkt_size
, jack_time_t
*timestamp
)
577 cache_packet
*cpack
= NULL
;
580 for (i
= 0; i
< pcache
->size
; i
++) {
581 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
582 cpack
= &(pcache
->packets
[i
]);
587 if( cpack
== NULL
) {
588 //printf( "retreive packet: %d....not found\n", framecnt );
592 if( !cache_packet_is_complete( cpack
) ) {
596 // ok. cpack is the one we want and its complete.
597 *packet_buf
= cpack
->packet_buf
;
599 *timestamp
= cpack
->recv_timestamp
;
601 pcache
->last_framecnt_retreived_valid
= 1;
602 pcache
->last_framecnt_retreived
= framecnt
;
608 packet_cache_release_packet( packet_cache
*pcache
, jack_nframes_t framecnt
)
611 cache_packet
*cpack
= NULL
;
614 for (i
= 0; i
< pcache
->size
; i
++) {
615 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
616 cpack
= &(pcache
->packets
[i
]);
621 if( cpack
== NULL
) {
622 //printf( "retreive packet: %d....not found\n", framecnt );
626 if( !cache_packet_is_complete( cpack
) ) {
630 cache_packet_reset (cpack
);
631 packet_cache_clear_old_packets( pcache
, framecnt
);
636 packet_cache_get_fill( packet_cache
*pcache
, jack_nframes_t expected_framecnt
)
638 int num_packets_before_us
= 0;
641 for (i
= 0; i
< pcache
->size
; i
++) {
642 cache_packet
*cpack
= &(pcache
->packets
[i
]);
643 if (cpack
->valid
&& cache_packet_is_complete( cpack
))
644 if( cpack
->framecnt
>= expected_framecnt
)
645 num_packets_before_us
+= 1;
648 return 100.0 * (float)num_packets_before_us
/ (float)( pcache
->size
);
651 // Returns 0 when no valid packet is inside the cache.
653 packet_cache_get_next_available_framecnt( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
656 jack_nframes_t best_offset
= JACK_MAX_FRAMES
/ 2 - 1;
659 for (i
= 0; i
< pcache
->size
; i
++) {
660 cache_packet
*cpack
= &(pcache
->packets
[i
]);
661 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
663 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
664 //printf( "invalid\n" );
668 if( cpack
->framecnt
< expected_framecnt
)
671 if( (cpack
->framecnt
- expected_framecnt
) > best_offset
) {
675 best_offset
= cpack
->framecnt
- expected_framecnt
;
678 if (best_offset
== 0)
681 if (retval
&& framecnt
)
682 *framecnt
= expected_framecnt
+ best_offset
;
688 packet_cache_get_highest_available_framecnt( packet_cache
*pcache
, jack_nframes_t
*framecnt
)
691 jack_nframes_t best_value
= 0;
694 for (i
= 0; i
< pcache
->size
; i
++) {
695 cache_packet
*cpack
= &(pcache
->packets
[i
]);
696 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
698 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
699 //printf( "invalid\n" );
703 if (cpack
->framecnt
< best_value
) {
707 best_value
= cpack
->framecnt
;
711 if (retval
&& framecnt
)
712 *framecnt
= best_value
;
717 // Returns 0 when no valid packet is inside the cache.
719 packet_cache_find_latency( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
722 jack_nframes_t best_offset
= 0;
725 for (i
= 0; i
< pcache
->size
; i
++) {
726 cache_packet
*cpack
= &(pcache
->packets
[i
]);
727 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
729 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
730 //printf( "invalid\n" );
734 if ((cpack
->framecnt
- expected_framecnt
) < best_offset
) {
738 best_offset
= cpack
->framecnt
- expected_framecnt
;
741 if( best_offset
== 0 )
744 if (retval
&& framecnt
)
745 *framecnt
= JACK_MAX_FRAMES
- best_offset
;
749 // fragmented packet IO
751 netjack_sendto (int sockfd
, char *packet_buf
, int pkt_size
, int flags
, struct sockaddr
*addr
, int addr_size
, int mtu
)
754 char *tx_packet
, *dataX
;
755 jacknet_packet_header
*pkthdr
;
757 tx_packet
= alloca (mtu
+ 10);
758 dataX
= tx_packet
+ sizeof (jacknet_packet_header
);
759 pkthdr
= (jacknet_packet_header
*) tx_packet
;
761 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
763 if (pkt_size
<= mtu
) {
765 pkthdr
= (jacknet_packet_header
*) packet_buf
;
766 pkthdr
->fragment_nr
= htonl (0);
767 err
= sendto(sockfd
, packet_buf
, pkt_size
, flags
, addr
, addr_size
);
769 //printf( "error in send\n" );
774 // Copy the packet header to the tx pack first.
775 memcpy(tx_packet
, packet_buf
, sizeof (jacknet_packet_header
));
777 // Now loop and send all
778 char *packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
);
780 while (packet_bufX
< (packet_buf
+ pkt_size
- fragment_payload_size
)) {
781 pkthdr
->fragment_nr
= htonl (frag_cnt
++);
782 memcpy (dataX
, packet_bufX
, fragment_payload_size
);
783 sendto (sockfd
, tx_packet
, mtu
, flags
, addr
, addr_size
);
784 packet_bufX
+= fragment_payload_size
;
787 int last_payload_size
= packet_buf
+ pkt_size
- packet_bufX
;
788 memcpy (dataX
, packet_bufX
, last_payload_size
);
789 pkthdr
->fragment_nr
= htonl (frag_cnt
);
790 //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
792 // sendto(last_pack_size);
793 err
= sendto(sockfd
, tx_packet
, last_payload_size
+ sizeof(jacknet_packet_header
), flags
, addr
, addr_size
);
795 //printf( "error in send\n" );
802 decode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
805 jack_midi_clear_buffer (buf
);
806 for (i
= 0; i
< buffer_size_uint32
- 3;) {
807 uint32_t payload_size
;
808 payload_size
= buffer_uint32
[i
];
809 payload_size
= ntohl (payload_size
);
811 jack_midi_event_t event
;
812 event
.time
= ntohl (buffer_uint32
[i
+ 1]);
813 event
.size
= ntohl (buffer_uint32
[i
+ 2]);
814 event
.buffer
= (jack_midi_data_t
*) (&(buffer_uint32
[i
+ 3]));
815 jack_midi_event_write (buf
, event
.time
, event
.buffer
, event
.size
);
817 // skip to the next event
818 unsigned int nb_data_quads
= (((event
.size
- 1) & ~0x3) >> 2) + 1;
819 i
+= 3 + nb_data_quads
;
821 break; // no events can follow an empty event, we're done
826 encode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
829 unsigned int written
= 0;
830 // midi port, encode midi events
831 unsigned int nevents
= jack_midi_get_event_count (buf
);
832 for (i
= 0; i
< nevents
; ++i
) {
833 jack_midi_event_t event
;
834 jack_midi_event_get (&event
, buf
, i
);
835 unsigned int nb_data_quads
= (((event
.size
- 1) & ~0x3) >> 2) + 1;
836 unsigned int payload_size
= 3 + nb_data_quads
;
837 // only write if we have sufficient space for the event
839 if (written
+ payload_size
< buffer_size_uint32
- 1) {
841 buffer_uint32
[written
] = htonl (payload_size
);
843 buffer_uint32
[written
] = htonl (event
.time
);
845 buffer_uint32
[written
] = htonl (event
.size
);
849 jack_midi_data_t
* tmpbuff
= (jack_midi_data_t
*)(&(buffer_uint32
[written
]));
850 memcpy (tmpbuff
, event
.buffer
, event
.size
);
851 written
+= nb_data_quads
;
854 jack_error ("midi buffer overflow");
858 // now put a netjack_midi 'no-payload' event, signaling EOF
859 buffer_uint32
[written
] = 0;
862 // render functions for float
864 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
)
867 JSList
*node
= capture_ports
;
869 JSList
*src_node
= capture_srcs
;
872 uint32_t *packet_bufX
= (uint32_t *)packet_payload
;
877 while (node
!= NULL
) {
884 jack_port_t
*port
= (jack_port_t
*) node
->data
;
885 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
887 const char *porttype
= jack_port_type (port
);
889 if (jack_port_is_audio (porttype
)) {
891 // audio port, resample if necessary
892 if (net_period_down
!= nframes
) {
893 SRC_STATE
*src_state
= src_node
->data
;
894 for (i
= 0; i
< net_period_down
; i
++) {
895 packet_bufX
[i
] = ntohl (packet_bufX
[i
]);
898 src
.data_in
= (float *) packet_bufX
;
899 src
.input_frames
= net_period_down
;
902 src
.output_frames
= nframes
;
904 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
905 src
.end_of_input
= 0;
907 src_set_ratio (src_state
, src
.src_ratio
);
908 src_process (src_state
, &src
);
909 src_node
= jack_slist_next (src_node
);
913 if( dont_htonl_floats
) {
914 memcpy( buf
, packet_bufX
, net_period_down
* sizeof(jack_default_audio_sample_t
));
916 for (i
= 0; i
< net_period_down
; i
++) {
917 val
.i
= packet_bufX
[i
];
918 val
.i
= ntohl (val
.i
);
923 } else if (jack_port_is_midi (porttype
)) {
924 // midi port, decode midi events
925 // convert the data buffer to a standard format (uint32_t based)
926 unsigned int buffer_size_uint32
= net_period_down
;
927 uint32_t * buffer_uint32
= (uint32_t*)packet_bufX
;
928 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
930 packet_bufX
= (packet_bufX
+ net_period_down
);
931 node
= jack_slist_next (node
);
937 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
)
940 JSList
*node
= playback_ports
;
942 JSList
*src_node
= playback_srcs
;
945 uint32_t *packet_bufX
= (uint32_t *) packet_payload
;
947 while (node
!= NULL
) {
953 jack_port_t
*port
= (jack_port_t
*) node
->data
;
954 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
956 const char *porttype
= jack_port_type (port
);
958 if (jack_port_is_audio (porttype
)) {
959 // audio port, resample if necessary
962 if (net_period_up
!= nframes
) {
963 SRC_STATE
*src_state
= src_node
->data
;
965 src
.input_frames
= nframes
;
967 src
.data_out
= (float *) packet_bufX
;
968 src
.output_frames
= net_period_up
;
970 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
971 src
.end_of_input
= 0;
973 src_set_ratio (src_state
, src
.src_ratio
);
974 src_process (src_state
, &src
);
976 for (i
= 0; i
< net_period_up
; i
++) {
977 packet_bufX
[i
] = htonl (packet_bufX
[i
]);
979 src_node
= jack_slist_next (src_node
);
983 if( dont_htonl_floats
) {
984 memcpy( packet_bufX
, buf
, net_period_up
* sizeof(jack_default_audio_sample_t
) );
986 for (i
= 0; i
< net_period_up
; i
++) {
988 val
.i
= htonl (val
.i
);
989 packet_bufX
[i
] = val
.i
;
993 } else if (jack_port_is_midi (porttype
)) {
994 // encode midi events from port to packet
995 // convert the data buffer to a standard format (uint32_t based)
996 unsigned int buffer_size_uint32
= net_period_up
;
997 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
998 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1000 packet_bufX
= (packet_bufX
+ net_period_up
);
1001 node
= jack_slist_next (node
);
1006 // render functions for 16bit
1008 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
)
1011 JSList
*node
= capture_ports
;
1013 JSList
*src_node
= capture_srcs
;
1016 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1018 if( !packet_payload
)
1021 while (node
!= NULL
) {
1028 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1029 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1032 float *floatbuf
= alloca (sizeof(float) * net_period_down
);
1034 const char *porttype
= jack_port_type (port
);
1036 if (jack_port_is_audio (porttype
)) {
1037 // audio port, resample if necessary
1040 if (net_period_down
!= nframes
) {
1041 SRC_STATE
*src_state
= src_node
->data
;
1042 for (i
= 0; i
< net_period_down
; i
++) {
1043 floatbuf
[i
] = ((float) ntohs(packet_bufX
[i
])) / 32767.0 - 1.0;
1046 src
.data_in
= floatbuf
;
1047 src
.input_frames
= net_period_down
;
1050 src
.output_frames
= nframes
;
1052 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1053 src
.end_of_input
= 0;
1055 src_set_ratio (src_state
, src
.src_ratio
);
1056 src_process (src_state
, &src
);
1057 src_node
= jack_slist_next (src_node
);
1060 for (i
= 0; i
< net_period_down
; i
++)
1061 buf
[i
] = ((float) ntohs (packet_bufX
[i
])) / 32768.0 - 1.0;
1062 } else if (jack_port_is_midi (porttype
)) {
1063 // midi port, decode midi events
1064 // convert the data buffer to a standard format (uint32_t based)
1065 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1066 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1067 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1069 packet_bufX
= (packet_bufX
+ net_period_down
);
1070 node
= jack_slist_next (node
);
1076 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
)
1079 JSList
*node
= playback_ports
;
1081 JSList
*src_node
= playback_srcs
;
1084 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1086 while (node
!= NULL
) {
1091 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1092 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1093 const char *porttype
= jack_port_type (port
);
1095 if (jack_port_is_audio (porttype
)) {
1096 // audio port, resample if necessary
1099 if (net_period_up
!= nframes
) {
1100 SRC_STATE
*src_state
= src_node
->data
;
1102 float *floatbuf
= alloca (sizeof(float) * net_period_up
);
1105 src
.input_frames
= nframes
;
1107 src
.data_out
= floatbuf
;
1108 src
.output_frames
= net_period_up
;
1110 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1111 src
.end_of_input
= 0;
1113 src_set_ratio (src_state
, src
.src_ratio
);
1114 src_process (src_state
, &src
);
1116 for (i
= 0; i
< net_period_up
; i
++) {
1117 packet_bufX
[i
] = htons (((uint16_t)((floatbuf
[i
] + 1.0) * 32767.0)));
1119 src_node
= jack_slist_next (src_node
);
1122 for (i
= 0; i
< net_period_up
; i
++)
1123 packet_bufX
[i
] = htons(((uint16_t)((buf
[i
] + 1.0) * 32767.0)));
1124 } else if (jack_port_is_midi (porttype
)) {
1125 // encode midi events from port to packet
1126 // convert the data buffer to a standard format (uint32_t based)
1127 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1128 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1129 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1131 packet_bufX
= (packet_bufX
+ net_period_up
);
1132 node
= jack_slist_next (node
);
1137 // render functions for 8bit
1139 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
)
1142 JSList
*node
= capture_ports
;
1145 JSList
*src_node
= capture_srcs
;
1148 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1150 if (!packet_payload
)
1153 while (node
!= NULL
) {
1160 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1161 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1164 float *floatbuf
= alloca (sizeof (float) * net_period_down
);
1166 const char *porttype
= jack_port_type (port
);
1168 if (jack_port_is_audio(porttype
)) {
1170 // audio port, resample if necessary
1171 if (net_period_down
!= nframes
) {
1172 SRC_STATE
*src_state
= src_node
->data
;
1173 for (i
= 0; i
< net_period_down
; i
++)
1174 floatbuf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1176 src
.data_in
= floatbuf
;
1177 src
.input_frames
= net_period_down
;
1180 src
.output_frames
= nframes
;
1182 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1183 src
.end_of_input
= 0;
1185 src_set_ratio (src_state
, src
.src_ratio
);
1186 src_process (src_state
, &src
);
1187 src_node
= jack_slist_next (src_node
);
1190 for (i
= 0; i
< net_period_down
; i
++)
1191 buf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1192 } else if (jack_port_is_midi (porttype
)) {
1193 // midi port, decode midi events
1194 // convert the data buffer to a standard format (uint32_t based)
1195 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1196 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1197 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1199 packet_bufX
= (packet_bufX
+ net_period_down
);
1200 node
= jack_slist_next (node
);
1206 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
)
1209 JSList
*node
= playback_ports
;
1211 JSList
*src_node
= playback_srcs
;
1214 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1216 while (node
!= NULL
) {
1221 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1223 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1224 const char *porttype
= jack_port_type (port
);
1226 if (jack_port_is_audio (porttype
)) {
1228 // audio port, resample if necessary
1229 if (net_period_up
!= nframes
) {
1231 SRC_STATE
*src_state
= src_node
->data
;
1233 float *floatbuf
= alloca (sizeof (float) * net_period_up
);
1236 src
.input_frames
= nframes
;
1238 src
.data_out
= floatbuf
;
1239 src
.output_frames
= net_period_up
;
1241 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1242 src
.end_of_input
= 0;
1244 src_set_ratio (src_state
, src
.src_ratio
);
1245 src_process (src_state
, &src
);
1247 for (i
= 0; i
< net_period_up
; i
++)
1248 packet_bufX
[i
] = floatbuf
[i
] * 127.0;
1249 src_node
= jack_slist_next (src_node
);
1252 for (i
= 0; i
< net_period_up
; i
++)
1253 packet_bufX
[i
] = buf
[i
] * 127.0;
1254 } else if (jack_port_is_midi (porttype
)) {
1255 // encode midi events from port to packet
1256 // convert the data buffer to a standard format (uint32_t based)
1257 unsigned int buffer_size_uint32
= net_period_up
/ 4;
1258 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1259 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1261 packet_bufX
= (packet_bufX
+ net_period_up
);
1262 node
= jack_slist_next (node
);
1268 // render functions for celt.
1270 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
)
1273 JSList
*node
= capture_ports
;
1274 JSList
*src_node
= capture_srcs
;
1276 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1278 while (node
!= NULL
) {
1279 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1280 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1282 const char *porttype
= jack_port_type (port
);
1284 if (jack_port_is_audio (porttype
)) {
1285 // audio port, decode celt data.
1286 CELTDecoder
*decoder
= src_node
->data
;
1287 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1288 if( !packet_payload
)
1289 celt_decode_float( decoder
, NULL
, net_period_down
, buf
, nframes
);
1291 celt_decode_float( decoder
, packet_bufX
, net_period_down
, buf
, nframes
);
1293 if( !packet_payload
)
1294 celt_decode_float( decoder
, NULL
, net_period_down
, buf
);
1296 celt_decode_float( decoder
, packet_bufX
, net_period_down
, buf
);
1299 src_node
= jack_slist_next (src_node
);
1300 } else if (jack_port_is_midi (porttype
)) {
1301 // midi port, decode midi events
1302 // convert the data buffer to a standard format (uint32_t based)
1303 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1304 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1305 if( packet_payload
)
1306 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1308 packet_bufX
= (packet_bufX
+ net_period_down
);
1309 node
= jack_slist_next (node
);
1315 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
)
1318 JSList
*node
= playback_ports
;
1319 JSList
*src_node
= playback_srcs
;
1321 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1323 while (node
!= NULL
) {
1324 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1325 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1326 const char *porttype
= jack_port_type (port
);
1328 if (jack_port_is_audio (porttype
)) {
1329 // audio port, encode celt data.
1332 float *floatbuf
= alloca (sizeof(float) * nframes
);
1333 memcpy( floatbuf
, buf
, nframes
* sizeof(float) );
1334 CELTEncoder
*encoder
= src_node
->data
;
1335 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1336 encoded_bytes
= celt_encode_float( encoder
, floatbuf
, nframes
, packet_bufX
, net_period_up
);
1338 encoded_bytes
= celt_encode_float( encoder
, floatbuf
, NULL
, packet_bufX
, net_period_up
);
1340 if( encoded_bytes
!= net_period_up
)
1341 printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
1342 src_node
= jack_slist_next( src_node
);
1343 } else if (jack_port_is_midi (porttype
)) {
1344 // encode midi events from port to packet
1345 // convert the data buffer to a standard format (uint32_t based)
1346 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1347 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1348 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1350 packet_bufX
= (packet_bufX
+ net_period_up
);
1351 node
= jack_slist_next (node
);
1359 #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
1360 // render functions for Opus.
1362 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
)
1365 JSList
*node
= capture_ports
;
1366 JSList
*src_node
= capture_srcs
;
1368 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1370 while (node
!= NULL
) {
1371 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1372 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1374 const char *porttype
= jack_port_type (port
);
1376 if (jack_port_is_audio (porttype
)) {
1377 // audio port, decode opus data.
1378 OpusCustomDecoder
*decoder
= (OpusCustomDecoder
*) src_node
->data
;
1379 if( !packet_payload
)
1380 memset(buf
, 0, nframes
* sizeof(float));
1383 memcpy(&len
, packet_bufX
, CDO
);
1385 opus_custom_decode_float( decoder
, packet_bufX
+ CDO
, len
, buf
, nframes
);
1388 src_node
= jack_slist_next (src_node
);
1389 } else if (jack_port_is_midi (porttype
)) {
1390 // midi port, decode midi events
1391 // convert the data buffer to a standard format (uint32_t based)
1392 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1393 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1394 if( packet_payload
)
1395 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1397 packet_bufX
= (packet_bufX
+ net_period_down
);
1398 node
= jack_slist_next (node
);
1404 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
)
1407 JSList
*node
= playback_ports
;
1408 JSList
*src_node
= playback_srcs
;
1410 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1412 while (node
!= NULL
) {
1413 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1414 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1415 const char *porttype
= jack_port_type (port
);
1417 if (jack_port_is_audio (porttype
)) {
1418 // audio port, encode opus data.
1421 float *floatbuf
= alloca (sizeof(float) * nframes
);
1422 memcpy( floatbuf
, buf
, nframes
* sizeof(float) );
1423 OpusCustomEncoder
*encoder
= (OpusCustomEncoder
*) src_node
->data
;
1424 encoded_bytes
= opus_custom_encode_float( encoder
, floatbuf
, nframes
, packet_bufX
+ CDO
, net_period_up
- CDO
);
1425 unsigned short len
= htons(encoded_bytes
);
1426 memcpy(packet_bufX
, &len
, CDO
);
1427 src_node
= jack_slist_next( src_node
);
1428 } else if (jack_port_is_midi (porttype
)) {
1429 // encode midi events from port to packet
1430 // convert the data buffer to a standard format (uint32_t based)
1431 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1432 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1433 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1435 packet_bufX
= (packet_bufX
+ net_period_up
);
1436 node
= jack_slist_next (node
);
1442 /* Wrapper functions with bitdepth argument... */
1444 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
)
1447 render_payload_to_jack_ports_8bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1448 else if (bitdepth
== 16)
1449 render_payload_to_jack_ports_16bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1451 else if (bitdepth
== CELT_MODE
)
1452 render_payload_to_jack_ports_celt (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1455 else if (bitdepth
== OPUS_MODE
)
1456 render_payload_to_jack_ports_opus (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1459 render_payload_to_jack_ports_float (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
, dont_htonl_floats
);
1463 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
)
1466 render_jack_ports_to_payload_8bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1467 else if (bitdepth
== 16)
1468 render_jack_ports_to_payload_16bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1470 else if (bitdepth
== CELT_MODE
)
1471 render_jack_ports_to_payload_celt (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1474 else if (bitdepth
== OPUS_MODE
)
1475 render_jack_ports_to_payload_opus (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1478 render_jack_ports_to_payload_float (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
, dont_htonl_floats
);