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>
75 #include "netjack_packet.h"
76 #include "JackError.h"
79 #define jack_error printf
85 packet_header_hton (jacknet_packet_header
*pkthdr
)
87 pkthdr
->capture_channels_audio
= htonl(pkthdr
->capture_channels_audio
);
88 pkthdr
->playback_channels_audio
= htonl(pkthdr
->playback_channels_audio
);
89 pkthdr
->capture_channels_midi
= htonl(pkthdr
->capture_channels_midi
);
90 pkthdr
->playback_channels_midi
= htonl(pkthdr
->playback_channels_midi
);
91 pkthdr
->period_size
= htonl(pkthdr
->period_size
);
92 pkthdr
->sample_rate
= htonl(pkthdr
->sample_rate
);
93 pkthdr
->sync_state
= htonl(pkthdr
->sync_state
);
94 pkthdr
->transport_frame
= htonl(pkthdr
->transport_frame
);
95 pkthdr
->transport_state
= htonl(pkthdr
->transport_state
);
96 pkthdr
->framecnt
= htonl(pkthdr
->framecnt
);
97 pkthdr
->latency
= htonl(pkthdr
->latency
);
98 pkthdr
->reply_port
= htonl(pkthdr
->reply_port
);
99 pkthdr
->mtu
= htonl(pkthdr
->mtu
);
100 pkthdr
->fragment_nr
= htonl(pkthdr
->fragment_nr
);
104 packet_header_ntoh (jacknet_packet_header
*pkthdr
)
106 pkthdr
->capture_channels_audio
= ntohl(pkthdr
->capture_channels_audio
);
107 pkthdr
->playback_channels_audio
= ntohl(pkthdr
->playback_channels_audio
);
108 pkthdr
->capture_channels_midi
= ntohl(pkthdr
->capture_channels_midi
);
109 pkthdr
->playback_channels_midi
= ntohl(pkthdr
->playback_channels_midi
);
110 pkthdr
->period_size
= ntohl(pkthdr
->period_size
);
111 pkthdr
->sample_rate
= ntohl(pkthdr
->sample_rate
);
112 pkthdr
->sync_state
= ntohl(pkthdr
->sync_state
);
113 pkthdr
->transport_frame
= ntohl(pkthdr
->transport_frame
);
114 pkthdr
->transport_state
= ntohl(pkthdr
->transport_state
);
115 pkthdr
->framecnt
= ntohl(pkthdr
->framecnt
);
116 pkthdr
->latency
= ntohl(pkthdr
->latency
);
117 pkthdr
->reply_port
= ntohl(pkthdr
->reply_port
);
118 pkthdr
->mtu
= ntohl(pkthdr
->mtu
);
119 pkthdr
->fragment_nr
= ntohl(pkthdr
->fragment_nr
);
122 int get_sample_size (int bitdepth
)
125 return sizeof (int8_t);
127 return sizeof (int16_t);
128 //JN: why? is this for buffer sizes before or after encoding?
129 //JN: if the former, why not int16_t, if the latter, shouldn't it depend on -c N?
130 if( bitdepth
== CELT_MODE
)
131 return sizeof( unsigned char );
132 return sizeof (int32_t);
135 int jack_port_is_audio(const char *porttype
)
137 return (strncmp (porttype
, JACK_DEFAULT_AUDIO_TYPE
, jack_port_type_size()) == 0);
140 int jack_port_is_midi(const char *porttype
)
142 return (strncmp (porttype
, JACK_DEFAULT_MIDI_TYPE
, jack_port_type_size()) == 0);
146 // fragment management functions.
149 *packet_cache_new (int num_packets
, int pkt_size
, int mtu
)
151 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
152 int i
, fragment_number
;
154 if( pkt_size
== sizeof(jacknet_packet_header
) )
157 fragment_number
= (pkt_size
- sizeof (jacknet_packet_header
) - 1) / fragment_payload_size
+ 1;
159 packet_cache
*pcache
= malloc (sizeof (packet_cache
));
160 if (pcache
== NULL
) {
161 jack_error ("could not allocate packet cache (1)");
165 pcache
->size
= num_packets
;
166 pcache
->packets
= malloc (sizeof (cache_packet
) * num_packets
);
167 pcache
->master_address_valid
= 0;
168 pcache
->last_framecnt_retreived
= 0;
169 pcache
->last_framecnt_retreived_valid
= 0;
171 if (pcache
->packets
== NULL
) {
172 jack_error ("could not allocate packet cache (2)");
176 for (i
= 0; i
< num_packets
; i
++) {
177 pcache
->packets
[i
].valid
= 0;
178 pcache
->packets
[i
].num_fragments
= fragment_number
;
179 pcache
->packets
[i
].packet_size
= pkt_size
;
180 pcache
->packets
[i
].mtu
= mtu
;
181 pcache
->packets
[i
].framecnt
= 0;
182 pcache
->packets
[i
].fragment_array
= malloc (sizeof (char) * fragment_number
);
183 pcache
->packets
[i
].packet_buf
= malloc (pkt_size
);
184 if ((pcache
->packets
[i
].fragment_array
== NULL
) || (pcache
->packets
[i
].packet_buf
== NULL
)) {
185 jack_error ("could not allocate packet cache (3)");
195 packet_cache_free (packet_cache
*pcache
)
201 for (i
= 0; i
< pcache
->size
; i
++) {
202 free (pcache
->packets
[i
].fragment_array
);
203 free (pcache
->packets
[i
].packet_buf
);
206 free (pcache
->packets
);
211 *packet_cache_get_packet (packet_cache
*pcache
, jack_nframes_t framecnt
)
214 cache_packet
*retval
;
216 for (i
= 0; i
< pcache
->size
; i
++) {
217 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
))
218 return &(pcache
->packets
[i
]);
221 // The Packet is not in the packet cache.
222 // find a free packet.
224 retval
= packet_cache_get_free_packet (pcache
);
225 if (retval
!= NULL
) {
226 cache_packet_set_framecnt (retval
, framecnt
);
230 // No Free Packet available
231 // Get The Oldest packet and reset it.
233 retval
= packet_cache_get_oldest_packet (pcache
);
234 //printf( "Dropping %d from Cache :S\n", retval->framecnt );
235 cache_packet_reset (retval
);
236 cache_packet_set_framecnt (retval
, framecnt
);
241 // TODO: fix wrapping case... need to pass
242 // current expected frame here.
244 // or just save framecount into packet_cache.
247 *packet_cache_get_oldest_packet (packet_cache
*pcache
)
249 jack_nframes_t minimal_frame
= JACK_MAX_FRAMES
;
250 cache_packet
*retval
= &(pcache
->packets
[0]);
253 for (i
= 0; i
< pcache
->size
; i
++) {
254 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< minimal_frame
)) {
255 minimal_frame
= pcache
->packets
[i
].framecnt
;
256 retval
= &(pcache
->packets
[i
]);
264 *packet_cache_get_free_packet (packet_cache
*pcache
)
268 for (i
= 0; i
< pcache
->size
; i
++) {
269 if (pcache
->packets
[i
].valid
== 0)
270 return &(pcache
->packets
[i
]);
277 cache_packet_reset (cache_packet
*pack
)
282 // XXX: i dont think this is necessary here...
283 // fragement array is cleared in _set_framecnt()
285 for (i
= 0; i
< pack
->num_fragments
; i
++)
286 pack
->fragment_array
[i
] = 0;
290 cache_packet_set_framecnt (cache_packet
*pack
, jack_nframes_t framecnt
)
294 pack
->framecnt
= framecnt
;
296 for (i
= 0; i
< pack
->num_fragments
; i
++)
297 pack
->fragment_array
[i
] = 0;
303 cache_packet_add_fragment (cache_packet
*pack
, char *packet_buf
, int rcv_len
)
305 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) packet_buf
;
306 int fragment_payload_size
= pack
->mtu
- sizeof (jacknet_packet_header
);
307 char *packet_bufX
= pack
->packet_buf
+ sizeof (jacknet_packet_header
);
308 char *dataX
= packet_buf
+ sizeof (jacknet_packet_header
);
310 jack_nframes_t fragment_nr
= ntohl (pkthdr
->fragment_nr
);
311 jack_nframes_t framecnt
= ntohl (pkthdr
->framecnt
);
313 if (framecnt
!= pack
->framecnt
) {
314 jack_error ("errror. framecnts dont match");
318 if (fragment_nr
== 0) {
319 memcpy (pack
->packet_buf
, packet_buf
, rcv_len
);
320 pack
->fragment_array
[0] = 1;
325 if ((fragment_nr
< pack
->num_fragments
) && (fragment_nr
> 0)) {
326 if ((fragment_nr
* fragment_payload_size
+ rcv_len
- sizeof (jacknet_packet_header
)) <= (pack
->packet_size
- sizeof (jacknet_packet_header
))) {
327 memcpy (packet_bufX
+ fragment_nr
* fragment_payload_size
, dataX
, rcv_len
- sizeof (jacknet_packet_header
));
328 pack
->fragment_array
[fragment_nr
] = 1;
330 jack_error ("too long packet received...");
335 cache_packet_is_complete (cache_packet
*pack
)
338 for (i
= 0; i
< pack
->num_fragments
; i
++)
339 if (pack
->fragment_array
[i
] == 0)
346 // new poll using nanoseconds resolution and
347 // not waiting forever.
349 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
354 struct timespec timeout_spec
= { 0, 0 };
359 jack_time_t now
= jack_get_time();
360 if( now
>= deadline
)
363 if( (deadline
- now
) >= 1000000 ) {
364 jack_error( "deadline more than 1 second in the future, trimming it." );
365 deadline
= now
+ 500000;
368 timeout_spec
.tv_nsec
= (deadline
- now
) * 1000;
370 timeout
= lrintf( (float)(deadline
- now
) / 1000.0 );
377 poll_err
= ppoll (&fds
, 1, &timeout_spec
, NULL
);
379 poll_err
= poll (&fds
, 1, timeout
);
382 if (poll_err
== -1) {
385 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
388 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
391 jack_error ("Error %d: A signal occurred before any requested event", errno
);
394 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
397 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
405 netjack_poll (int sockfd
, int timeout
)
409 sigset_t sigmask
, rsigmask
;
410 struct sigaction action
;
412 sigemptyset(&sigmask
);
413 sigaddset(&sigmask
, SIGHUP
);
414 sigaddset(&sigmask
, SIGINT
);
415 sigaddset(&sigmask
, SIGQUIT
);
416 sigaddset(&sigmask
, SIGPIPE
);
417 sigaddset(&sigmask
, SIGTERM
);
418 sigaddset(&sigmask
, SIGUSR1
);
419 sigaddset(&sigmask
, SIGUSR2
);
421 action
.sa_handler
= SIG_DFL
;
422 action
.sa_mask
= sigmask
;
423 action
.sa_flags
= SA_RESTART
;
425 for (i
= 1; i
< NSIG
; i
++)
426 if (sigismember (&sigmask
, i
))
427 sigaction (i
, &action
, 0);
432 sigprocmask(SIG_UNBLOCK
, &sigmask
, &rsigmask
);
433 while (poll_err
== 0) {
434 poll_err
= poll (&fds
, 1, timeout
);
436 sigprocmask(SIG_SETMASK
, &rsigmask
, NULL
);
438 if (poll_err
== -1) {
441 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno
);
444 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno
);
447 jack_error ("Error %d: A signal occurred before any requested event", errno
);
450 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno
);
453 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno
);
463 netjack_poll (int sockfd
, int timeout
)
465 jack_error( "netjack_poll not implemented" );
469 netjack_poll_deadline (int sockfd
, jack_time_t deadline
)
473 FD_SET( sockfd
, &fds
);
475 struct timeval timeout
;
477 jack_time_t now
= jack_get_time();
478 if( now
>= deadline
)
481 int timeout_usecs
= (deadline
- now
);
482 //jack_error( "timeout = %d", timeout_usecs );
484 timeout
.tv_usec
= (timeout_usecs
< 500) ? 500 : timeout_usecs
;
485 timeout
.tv_usec
= (timeout_usecs
> 1000000) ? 500000 : timeout_usecs
;
487 int poll_err
= select (0, &fds
, NULL
, NULL
, &timeout
);
495 // This now reads all a socket has into the cache.
496 // replacing netjack_recv functions.
499 packet_cache_drain_socket( packet_cache
*pcache
, int sockfd
)
501 char *rx_packet
= alloca (pcache
->mtu
);
502 jacknet_packet_header
*pkthdr
= (jacknet_packet_header
*) rx_packet
;
504 jack_nframes_t framecnt
;
506 struct sockaddr_in sender_address
;
508 int senderlen
= sizeof( struct sockaddr_in
);
510 ioctlsocket( sockfd
, FIONBIO
, &parm
);
512 int senderlen
= sizeof( struct sockaddr_in
);
516 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, 0,
517 (struct sockaddr
*) &sender_address
, &senderlen
);
519 rcv_len
= recvfrom (sockfd
, rx_packet
, pcache
->mtu
, MSG_DONTWAIT
,
520 (struct sockaddr
*) &sender_address
, &senderlen
);
525 if (pcache
->master_address_valid
) {
526 // Verify its from our master.
527 if (memcmp (&sender_address
, &(pcache
->master_address
), senderlen
) != 0)
530 // Setup this one as master
531 //printf( "setup master...\n" );
532 memcpy ( &(pcache
->master_address
), &sender_address
, senderlen
);
533 pcache
->master_address_valid
= 1;
536 framecnt
= ntohl (pkthdr
->framecnt
);
537 if( pcache
->last_framecnt_retreived_valid
&& (framecnt
<= pcache
->last_framecnt_retreived
))
540 cpack
= packet_cache_get_packet (pcache
, framecnt
);
541 cache_packet_add_fragment (cpack
, rx_packet
, rcv_len
);
542 cpack
->recv_timestamp
= jack_get_time();
547 packet_cache_reset_master_address( packet_cache
*pcache
)
549 pcache
->master_address_valid
= 0;
550 pcache
->last_framecnt_retreived
= 0;
551 pcache
->last_framecnt_retreived_valid
= 0;
555 packet_cache_clear_old_packets (packet_cache
*pcache
, jack_nframes_t framecnt
)
559 for (i
= 0; i
< pcache
->size
; i
++) {
560 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
< framecnt
)) {
561 cache_packet_reset (&(pcache
->packets
[i
]));
567 packet_cache_retreive_packet_pointer( packet_cache
*pcache
, jack_nframes_t framecnt
, char **packet_buf
, int pkt_size
, jack_time_t
*timestamp
)
570 cache_packet
*cpack
= NULL
;
573 for (i
= 0; i
< pcache
->size
; i
++) {
574 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
575 cpack
= &(pcache
->packets
[i
]);
580 if( cpack
== NULL
) {
581 //printf( "retreive packet: %d....not found\n", framecnt );
585 if( !cache_packet_is_complete( cpack
) ) {
589 // ok. cpack is the one we want and its complete.
590 *packet_buf
= cpack
->packet_buf
;
592 *timestamp
= cpack
->recv_timestamp
;
594 pcache
->last_framecnt_retreived_valid
= 1;
595 pcache
->last_framecnt_retreived
= framecnt
;
601 packet_cache_release_packet( packet_cache
*pcache
, jack_nframes_t framecnt
)
604 cache_packet
*cpack
= NULL
;
607 for (i
= 0; i
< pcache
->size
; i
++) {
608 if (pcache
->packets
[i
].valid
&& (pcache
->packets
[i
].framecnt
== framecnt
)) {
609 cpack
= &(pcache
->packets
[i
]);
614 if( cpack
== NULL
) {
615 //printf( "retreive packet: %d....not found\n", framecnt );
619 if( !cache_packet_is_complete( cpack
) ) {
623 cache_packet_reset (cpack
);
624 packet_cache_clear_old_packets( pcache
, framecnt
);
629 packet_cache_get_fill( packet_cache
*pcache
, jack_nframes_t expected_framecnt
)
631 int num_packets_before_us
= 0;
634 for (i
= 0; i
< pcache
->size
; i
++) {
635 cache_packet
*cpack
= &(pcache
->packets
[i
]);
636 if (cpack
->valid
&& cache_packet_is_complete( cpack
))
637 if( cpack
->framecnt
>= expected_framecnt
)
638 num_packets_before_us
+= 1;
641 return 100.0 * (float)num_packets_before_us
/ (float)( pcache
->size
);
644 // Returns 0 when no valid packet is inside the cache.
646 packet_cache_get_next_available_framecnt( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
649 jack_nframes_t best_offset
= JACK_MAX_FRAMES
/ 2 - 1;
652 for (i
= 0; i
< pcache
->size
; i
++) {
653 cache_packet
*cpack
= &(pcache
->packets
[i
]);
654 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
656 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
657 //printf( "invalid\n" );
661 if( cpack
->framecnt
< expected_framecnt
)
664 if( (cpack
->framecnt
- expected_framecnt
) > best_offset
) {
668 best_offset
= cpack
->framecnt
- expected_framecnt
;
671 if (best_offset
== 0)
674 if (retval
&& framecnt
)
675 *framecnt
= expected_framecnt
+ best_offset
;
681 packet_cache_get_highest_available_framecnt( packet_cache
*pcache
, jack_nframes_t
*framecnt
)
684 jack_nframes_t best_value
= 0;
687 for (i
= 0; i
< pcache
->size
; i
++) {
688 cache_packet
*cpack
= &(pcache
->packets
[i
]);
689 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
691 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
692 //printf( "invalid\n" );
696 if (cpack
->framecnt
< best_value
) {
700 best_value
= cpack
->framecnt
;
704 if (retval
&& framecnt
)
705 *framecnt
= best_value
;
710 // Returns 0 when no valid packet is inside the cache.
712 packet_cache_find_latency( packet_cache
*pcache
, jack_nframes_t expected_framecnt
, jack_nframes_t
*framecnt
)
715 jack_nframes_t best_offset
= 0;
718 for (i
= 0; i
< pcache
->size
; i
++) {
719 cache_packet
*cpack
= &(pcache
->packets
[i
]);
720 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
722 if (!cpack
->valid
|| !cache_packet_is_complete( cpack
)) {
723 //printf( "invalid\n" );
727 if ((cpack
->framecnt
- expected_framecnt
) < best_offset
) {
731 best_offset
= cpack
->framecnt
- expected_framecnt
;
734 if( best_offset
== 0 )
737 if (retval
&& framecnt
)
738 *framecnt
= JACK_MAX_FRAMES
- best_offset
;
742 // fragmented packet IO
744 netjack_sendto (int sockfd
, char *packet_buf
, int pkt_size
, int flags
, struct sockaddr
*addr
, int addr_size
, int mtu
)
747 char *tx_packet
, *dataX
;
748 jacknet_packet_header
*pkthdr
;
750 tx_packet
= alloca (mtu
+ 10);
751 dataX
= tx_packet
+ sizeof (jacknet_packet_header
);
752 pkthdr
= (jacknet_packet_header
*) tx_packet
;
754 int fragment_payload_size
= mtu
- sizeof (jacknet_packet_header
);
756 if (pkt_size
<= mtu
) {
758 pkthdr
= (jacknet_packet_header
*) packet_buf
;
759 pkthdr
->fragment_nr
= htonl (0);
760 err
= sendto(sockfd
, packet_buf
, pkt_size
, flags
, addr
, addr_size
);
762 //printf( "error in send\n" );
767 // Copy the packet header to the tx pack first.
768 memcpy(tx_packet
, packet_buf
, sizeof (jacknet_packet_header
));
770 // Now loop and send all
771 char *packet_bufX
= packet_buf
+ sizeof (jacknet_packet_header
);
773 while (packet_bufX
< (packet_buf
+ pkt_size
- fragment_payload_size
)) {
774 pkthdr
->fragment_nr
= htonl (frag_cnt
++);
775 memcpy (dataX
, packet_bufX
, fragment_payload_size
);
776 sendto (sockfd
, tx_packet
, mtu
, flags
, addr
, addr_size
);
777 packet_bufX
+= fragment_payload_size
;
780 int last_payload_size
= packet_buf
+ pkt_size
- packet_bufX
;
781 memcpy (dataX
, packet_bufX
, last_payload_size
);
782 pkthdr
->fragment_nr
= htonl (frag_cnt
);
783 //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
785 // sendto(last_pack_size);
786 err
= sendto(sockfd
, tx_packet
, last_payload_size
+ sizeof(jacknet_packet_header
), flags
, addr
, addr_size
);
788 //printf( "error in send\n" );
795 decode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
798 jack_midi_clear_buffer (buf
);
799 for (i
= 0; i
< buffer_size_uint32
- 3;) {
800 uint32_t payload_size
;
801 payload_size
= buffer_uint32
[i
];
802 payload_size
= ntohl (payload_size
);
804 jack_midi_event_t event
;
805 event
.time
= ntohl (buffer_uint32
[i
+ 1]);
806 event
.size
= ntohl (buffer_uint32
[i
+ 2]);
807 event
.buffer
= (jack_midi_data_t
*) (&(buffer_uint32
[i
+ 3]));
808 jack_midi_event_write (buf
, event
.time
, event
.buffer
, event
.size
);
810 // skip to the next event
811 unsigned int nb_data_quads
= (((event
.size
- 1) & ~0x3) >> 2) + 1;
812 i
+= 3 + nb_data_quads
;
814 break; // no events can follow an empty event, we're done
819 encode_midi_buffer (uint32_t *buffer_uint32
, unsigned int buffer_size_uint32
, jack_default_audio_sample_t
* buf
)
822 unsigned int written
= 0;
823 // midi port, encode midi events
824 unsigned int nevents
= jack_midi_get_event_count (buf
);
825 for (i
= 0; i
< nevents
; ++i
) {
826 jack_midi_event_t event
;
827 jack_midi_event_get (&event
, buf
, i
);
828 unsigned int nb_data_quads
= (((event
.size
- 1) & ~0x3) >> 2) + 1;
829 unsigned int payload_size
= 3 + nb_data_quads
;
830 // only write if we have sufficient space for the event
832 if (written
+ payload_size
< buffer_size_uint32
- 1) {
834 buffer_uint32
[written
] = htonl (payload_size
);
836 buffer_uint32
[written
] = htonl (event
.time
);
838 buffer_uint32
[written
] = htonl (event
.size
);
842 jack_midi_data_t
* tmpbuff
= (jack_midi_data_t
*)(&(buffer_uint32
[written
]));
843 memcpy (tmpbuff
, event
.buffer
, event
.size
);
844 written
+= nb_data_quads
;
847 jack_error ("midi buffer overflow");
851 // now put a netjack_midi 'no-payload' event, signaling EOF
852 buffer_uint32
[written
] = 0;
855 // render functions for float
857 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
)
860 JSList
*node
= capture_ports
;
862 JSList
*src_node
= capture_srcs
;
865 uint32_t *packet_bufX
= (uint32_t *)packet_payload
;
870 while (node
!= NULL
) {
877 jack_port_t
*port
= (jack_port_t
*) node
->data
;
878 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
880 const char *porttype
= jack_port_type (port
);
882 if (jack_port_is_audio (porttype
)) {
884 // audio port, resample if necessary
885 if (net_period_down
!= nframes
) {
886 SRC_STATE
*src_state
= src_node
->data
;
887 for (i
= 0; i
< net_period_down
; i
++) {
888 packet_bufX
[i
] = ntohl (packet_bufX
[i
]);
891 src
.data_in
= (float *) packet_bufX
;
892 src
.input_frames
= net_period_down
;
895 src
.output_frames
= nframes
;
897 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
898 src
.end_of_input
= 0;
900 src_set_ratio (src_state
, src
.src_ratio
);
901 src_process (src_state
, &src
);
902 src_node
= jack_slist_next (src_node
);
906 if( dont_htonl_floats
) {
907 memcpy( buf
, packet_bufX
, net_period_down
* sizeof(jack_default_audio_sample_t
));
909 for (i
= 0; i
< net_period_down
; i
++) {
910 val
.i
= packet_bufX
[i
];
911 val
.i
= ntohl (val
.i
);
916 } else if (jack_port_is_midi (porttype
)) {
917 // midi port, decode midi events
918 // convert the data buffer to a standard format (uint32_t based)
919 unsigned int buffer_size_uint32
= net_period_down
;
920 uint32_t * buffer_uint32
= (uint32_t*)packet_bufX
;
921 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
923 packet_bufX
= (packet_bufX
+ net_period_down
);
924 node
= jack_slist_next (node
);
930 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
)
933 JSList
*node
= playback_ports
;
935 JSList
*src_node
= playback_srcs
;
938 uint32_t *packet_bufX
= (uint32_t *) packet_payload
;
940 while (node
!= NULL
) {
946 jack_port_t
*port
= (jack_port_t
*) node
->data
;
947 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
949 const char *porttype
= jack_port_type (port
);
951 if (jack_port_is_audio (porttype
)) {
952 // audio port, resample if necessary
955 if (net_period_up
!= nframes
) {
956 SRC_STATE
*src_state
= src_node
->data
;
958 src
.input_frames
= nframes
;
960 src
.data_out
= (float *) packet_bufX
;
961 src
.output_frames
= net_period_up
;
963 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
964 src
.end_of_input
= 0;
966 src_set_ratio (src_state
, src
.src_ratio
);
967 src_process (src_state
, &src
);
969 for (i
= 0; i
< net_period_up
; i
++) {
970 packet_bufX
[i
] = htonl (packet_bufX
[i
]);
972 src_node
= jack_slist_next (src_node
);
976 if( dont_htonl_floats
) {
977 memcpy( packet_bufX
, buf
, net_period_up
* sizeof(jack_default_audio_sample_t
) );
979 for (i
= 0; i
< net_period_up
; i
++) {
981 val
.i
= htonl (val
.i
);
982 packet_bufX
[i
] = val
.i
;
986 } else if (jack_port_is_midi (porttype
)) {
987 // encode midi events from port to packet
988 // convert the data buffer to a standard format (uint32_t based)
989 unsigned int buffer_size_uint32
= net_period_up
;
990 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
991 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
993 packet_bufX
= (packet_bufX
+ net_period_up
);
994 node
= jack_slist_next (node
);
999 // render functions for 16bit
1001 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
)
1004 JSList
*node
= capture_ports
;
1006 JSList
*src_node
= capture_srcs
;
1009 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1011 if( !packet_payload
)
1014 while (node
!= NULL
) {
1021 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1022 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1025 float *floatbuf
= alloca (sizeof(float) * net_period_down
);
1027 const char *porttype
= jack_port_type (port
);
1029 if (jack_port_is_audio (porttype
)) {
1030 // audio port, resample if necessary
1033 if (net_period_down
!= nframes
) {
1034 SRC_STATE
*src_state
= src_node
->data
;
1035 for (i
= 0; i
< net_period_down
; i
++) {
1036 floatbuf
[i
] = ((float) ntohs(packet_bufX
[i
])) / 32767.0 - 1.0;
1039 src
.data_in
= floatbuf
;
1040 src
.input_frames
= net_period_down
;
1043 src
.output_frames
= nframes
;
1045 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1046 src
.end_of_input
= 0;
1048 src_set_ratio (src_state
, src
.src_ratio
);
1049 src_process (src_state
, &src
);
1050 src_node
= jack_slist_next (src_node
);
1053 for (i
= 0; i
< net_period_down
; i
++)
1054 buf
[i
] = ((float) ntohs (packet_bufX
[i
])) / 32768.0 - 1.0;
1055 } else if (jack_port_is_midi (porttype
)) {
1056 // midi port, decode midi events
1057 // convert the data buffer to a standard format (uint32_t based)
1058 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1059 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1060 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1062 packet_bufX
= (packet_bufX
+ net_period_down
);
1063 node
= jack_slist_next (node
);
1069 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
)
1072 JSList
*node
= playback_ports
;
1074 JSList
*src_node
= playback_srcs
;
1077 uint16_t *packet_bufX
= (uint16_t *)packet_payload
;
1079 while (node
!= NULL
) {
1084 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1085 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1086 const char *porttype
= jack_port_type (port
);
1088 if (jack_port_is_audio (porttype
)) {
1089 // audio port, resample if necessary
1092 if (net_period_up
!= nframes
) {
1093 SRC_STATE
*src_state
= src_node
->data
;
1095 float *floatbuf
= alloca (sizeof(float) * net_period_up
);
1098 src
.input_frames
= nframes
;
1100 src
.data_out
= floatbuf
;
1101 src
.output_frames
= net_period_up
;
1103 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1104 src
.end_of_input
= 0;
1106 src_set_ratio (src_state
, src
.src_ratio
);
1107 src_process (src_state
, &src
);
1109 for (i
= 0; i
< net_period_up
; i
++) {
1110 packet_bufX
[i
] = htons (((uint16_t)((floatbuf
[i
] + 1.0) * 32767.0)));
1112 src_node
= jack_slist_next (src_node
);
1115 for (i
= 0; i
< net_period_up
; i
++)
1116 packet_bufX
[i
] = htons(((uint16_t)((buf
[i
] + 1.0) * 32767.0)));
1117 } else if (jack_port_is_midi (porttype
)) {
1118 // encode midi events from port to packet
1119 // convert the data buffer to a standard format (uint32_t based)
1120 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1121 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1122 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1124 packet_bufX
= (packet_bufX
+ net_period_up
);
1125 node
= jack_slist_next (node
);
1130 // render functions for 8bit
1132 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
)
1135 JSList
*node
= capture_ports
;
1138 JSList
*src_node
= capture_srcs
;
1141 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1143 if (!packet_payload
)
1146 while (node
!= NULL
) {
1153 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1154 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1157 float *floatbuf
= alloca (sizeof (float) * net_period_down
);
1159 const char *porttype
= jack_port_type (port
);
1161 if (jack_port_is_audio(porttype
)) {
1163 // audio port, resample if necessary
1164 if (net_period_down
!= nframes
) {
1165 SRC_STATE
*src_state
= src_node
->data
;
1166 for (i
= 0; i
< net_period_down
; i
++)
1167 floatbuf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1169 src
.data_in
= floatbuf
;
1170 src
.input_frames
= net_period_down
;
1173 src
.output_frames
= nframes
;
1175 src
.src_ratio
= (float) nframes
/ (float) net_period_down
;
1176 src
.end_of_input
= 0;
1178 src_set_ratio (src_state
, src
.src_ratio
);
1179 src_process (src_state
, &src
);
1180 src_node
= jack_slist_next (src_node
);
1183 for (i
= 0; i
< net_period_down
; i
++)
1184 buf
[i
] = ((float) packet_bufX
[i
]) / 127.0;
1185 } else if (jack_port_is_midi (porttype
)) {
1186 // midi port, decode midi events
1187 // convert the data buffer to a standard format (uint32_t based)
1188 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1189 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1190 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1192 packet_bufX
= (packet_bufX
+ net_period_down
);
1193 node
= jack_slist_next (node
);
1199 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
)
1202 JSList
*node
= playback_ports
;
1204 JSList
*src_node
= playback_srcs
;
1207 int8_t *packet_bufX
= (int8_t *)packet_payload
;
1209 while (node
!= NULL
) {
1214 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1216 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1217 const char *porttype
= jack_port_type (port
);
1219 if (jack_port_is_audio (porttype
)) {
1221 // audio port, resample if necessary
1222 if (net_period_up
!= nframes
) {
1224 SRC_STATE
*src_state
= src_node
->data
;
1226 float *floatbuf
= alloca (sizeof (float) * net_period_up
);
1229 src
.input_frames
= nframes
;
1231 src
.data_out
= floatbuf
;
1232 src
.output_frames
= net_period_up
;
1234 src
.src_ratio
= (float) net_period_up
/ (float) nframes
;
1235 src
.end_of_input
= 0;
1237 src_set_ratio (src_state
, src
.src_ratio
);
1238 src_process (src_state
, &src
);
1240 for (i
= 0; i
< net_period_up
; i
++)
1241 packet_bufX
[i
] = floatbuf
[i
] * 127.0;
1242 src_node
= jack_slist_next (src_node
);
1245 for (i
= 0; i
< net_period_up
; i
++)
1246 packet_bufX
[i
] = buf
[i
] * 127.0;
1247 } else if (jack_port_is_midi (porttype
)) {
1248 // encode midi events from port to packet
1249 // convert the data buffer to a standard format (uint32_t based)
1250 unsigned int buffer_size_uint32
= net_period_up
/ 4;
1251 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1252 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1254 packet_bufX
= (packet_bufX
+ net_period_up
);
1255 node
= jack_slist_next (node
);
1261 // render functions for celt.
1263 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
)
1266 JSList
*node
= capture_ports
;
1267 JSList
*src_node
= capture_srcs
;
1269 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1271 while (node
!= NULL
) {
1272 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1273 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1275 const char *porttype
= jack_port_type (port
);
1277 if (jack_port_is_audio (porttype
)) {
1278 // audio port, decode celt data.
1279 CELTDecoder
*decoder
= src_node
->data
;
1280 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1281 if( !packet_payload
)
1282 celt_decode_float( decoder
, NULL
, net_period_down
, buf
, nframes
);
1284 celt_decode_float( decoder
, packet_bufX
, net_period_down
, buf
, nframes
);
1286 if( !packet_payload
)
1287 celt_decode_float( decoder
, NULL
, net_period_down
, buf
);
1289 celt_decode_float( decoder
, packet_bufX
, net_period_down
, buf
);
1292 src_node
= jack_slist_next (src_node
);
1293 } else if (jack_port_is_midi (porttype
)) {
1294 // midi port, decode midi events
1295 // convert the data buffer to a standard format (uint32_t based)
1296 unsigned int buffer_size_uint32
= net_period_down
/ 2;
1297 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1298 if( packet_payload
)
1299 decode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1301 packet_bufX
= (packet_bufX
+ net_period_down
);
1302 node
= jack_slist_next (node
);
1308 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
)
1311 JSList
*node
= playback_ports
;
1312 JSList
*src_node
= playback_srcs
;
1314 unsigned char *packet_bufX
= (unsigned char *)packet_payload
;
1316 while (node
!= NULL
) {
1317 jack_port_t
*port
= (jack_port_t
*) node
->data
;
1318 jack_default_audio_sample_t
* buf
= jack_port_get_buffer (port
, nframes
);
1319 const char *porttype
= jack_port_type (port
);
1321 if (jack_port_is_audio (porttype
)) {
1322 // audio port, encode celt data.
1325 float *floatbuf
= alloca (sizeof(float) * nframes
);
1326 memcpy( floatbuf
, buf
, nframes
* sizeof(float) );
1327 CELTEncoder
*encoder
= src_node
->data
;
1328 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1329 encoded_bytes
= celt_encode_float( encoder
, floatbuf
, nframes
, packet_bufX
, net_period_up
);
1331 encoded_bytes
= celt_encode_float( encoder
, floatbuf
, NULL
, packet_bufX
, net_period_up
);
1333 if( encoded_bytes
!= net_period_up
)
1334 printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
1335 src_node
= jack_slist_next( src_node
);
1336 } else if (jack_port_is_midi (porttype
)) {
1337 // encode midi events from port to packet
1338 // convert the data buffer to a standard format (uint32_t based)
1339 unsigned int buffer_size_uint32
= net_period_up
/ 2;
1340 uint32_t * buffer_uint32
= (uint32_t*) packet_bufX
;
1341 encode_midi_buffer (buffer_uint32
, buffer_size_uint32
, buf
);
1343 packet_bufX
= (packet_bufX
+ net_period_up
);
1344 node
= jack_slist_next (node
);
1350 /* Wrapper functions with bitdepth argument... */
1352 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
)
1355 render_payload_to_jack_ports_8bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1356 else if (bitdepth
== 16)
1357 render_payload_to_jack_ports_16bit (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1359 else if (bitdepth
== CELT_MODE
)
1360 render_payload_to_jack_ports_celt (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
);
1363 render_payload_to_jack_ports_float (packet_payload
, net_period_down
, capture_ports
, capture_srcs
, nframes
, dont_htonl_floats
);
1367 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
)
1370 render_jack_ports_to_payload_8bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1371 else if (bitdepth
== 16)
1372 render_jack_ports_to_payload_16bit (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1374 else if (bitdepth
== CELT_MODE
)
1375 render_jack_ports_to_payload_celt (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
);
1378 render_jack_ports_to_payload_float (playback_ports
, playback_srcs
, nframes
, packet_payload
, net_period_up
, dont_htonl_floats
);