Merge pull request #302 from sdrik/pull/promiscuous-doc
[jack2.git] / common / netjack_packet.c
blobcd3a8d6d320f80aa66226fff76b1f2f3331c3786
2 /*
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)
30 #include "config.h"
31 #endif
33 #ifdef __APPLE__
34 #define _DARWIN_C_SOURCE
35 #endif
37 #if HAVE_PPOLL
38 #define _GNU_SOURCE
39 #endif
41 #include <math.h>
42 #include <stdio.h>
43 #include <memory.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <stdarg.h>
49 #include <jack/types.h>
51 #include <sys/types.h>
53 #ifdef WIN32
54 #include <winsock2.h>
55 #define socklen_t int
56 #include <malloc.h>
57 #define socklen_t int
58 #else
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <poll.h>
62 #endif
64 #include <errno.h>
65 #include <signal.h>
67 #if HAVE_SAMPLERATE
68 #include <samplerate.h>
69 #endif
71 #if HAVE_CELT
72 #include <celt/celt.h>
73 #endif
75 #if HAVE_OPUS
76 #include <opus/opus.h>
77 #include <opus/opus_custom.h>
78 #endif
80 #include "netjack_packet.h"
81 #include "JackError.h"
83 #ifdef NO_JACK_ERROR
84 #define jack_error printf
85 #endif
87 int fraggo = 0;
89 void
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);
108 void
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)
129 if (bitdepth == 8)
130 return sizeof (int8_t);
131 if (bitdepth == 16)
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.
155 packet_cache
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) )
162 fragment_number = 1;
163 else
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)");
169 return NULL;
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)");
180 return NULL;
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)");
193 return NULL;
196 pcache->mtu = mtu;
198 return pcache;
201 void
202 packet_cache_free (packet_cache *pcache)
204 int i;
205 if( pcache == NULL )
206 return;
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);
214 free (pcache);
217 cache_packet
218 *packet_cache_get_packet (packet_cache *pcache, jack_nframes_t framecnt)
220 int i;
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);
234 return retval;
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);
245 return retval;
248 // TODO: fix wrapping case... need to pass
249 // current expected frame here.
251 // or just save framecount into packet_cache.
253 cache_packet
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]);
258 int i;
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]);
267 return retval;
270 cache_packet
271 *packet_cache_get_free_packet (packet_cache *pcache)
273 int i;
275 for (i = 0; i < pcache->size; i++) {
276 if (pcache->packets[i].valid == 0)
277 return &(pcache->packets[i]);
280 return NULL;
283 void
284 cache_packet_reset (cache_packet *pack)
286 int i;
287 pack->valid = 0;
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;
296 void
297 cache_packet_set_framecnt (cache_packet *pack, jack_nframes_t framecnt)
299 int i;
301 pack->framecnt = framecnt;
303 for (i = 0; i < pack->num_fragments; i++)
304 pack->fragment_array[i] = 0;
306 pack->valid = 1;
309 void
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");
322 return;
325 if (fragment_nr == 0) {
326 memcpy (pack->packet_buf, packet_buf, rcv_len);
327 pack->fragment_array[0] = 1;
329 return;
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;
336 } else
337 jack_error ("too long packet received...");
342 cache_packet_is_complete (cache_packet *pack)
344 int i;
345 for (i = 0; i < pack->num_fragments; i++)
346 if (pack->fragment_array[i] == 0)
347 return 0;
349 return 1;
352 #ifndef WIN32
353 // new poll using nanoseconds resolution and
354 // not waiting forever.
356 netjack_poll_deadline (int sockfd, jack_time_t deadline)
358 struct pollfd fds;
359 int poll_err = 0;
360 #if HAVE_PPOLL
361 struct timespec timeout_spec = { 0, 0 };
362 #else
363 int timeout;
364 #endif
366 jack_time_t now = jack_get_time();
367 if( now >= deadline )
368 return 0;
370 if( (deadline - now) >= 1000000 ) {
371 jack_error( "deadline more than 1 second in the future, trimming it." );
372 deadline = now + 500000;
374 #if HAVE_PPOLL
375 timeout_spec.tv_nsec = (deadline - now) * 1000;
376 #else
377 timeout = lrintf( (float)(deadline - now) / 1000.0 );
378 #endif
380 fds.fd = sockfd;
381 fds.events = POLLIN;
383 #if HAVE_PPOLL
384 poll_err = ppoll (&fds, 1, &timeout_spec, NULL);
385 #else
386 poll_err = poll (&fds, 1, timeout);
387 #endif
389 if (poll_err == -1) {
390 switch (errno) {
391 case EBADF:
392 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
393 break;
394 case EFAULT:
395 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
396 break;
397 case EINTR:
398 jack_error ("Error %d: A signal occurred before any requested event", errno);
399 break;
400 case EINVAL:
401 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
402 break;
403 case ENOMEM:
404 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
405 break;
408 return poll_err;
412 netjack_poll (int sockfd, int timeout)
414 struct pollfd fds;
415 int i, poll_err = 0;
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);
436 fds.fd = sockfd;
437 fds.events = POLLIN;
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) {
446 switch (errno) {
447 case EBADF:
448 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
449 break;
450 case EFAULT:
451 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
452 break;
453 case EINTR:
454 jack_error ("Error %d: A signal occurred before any requested event", errno);
455 break;
456 case EINVAL:
457 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
458 break;
459 case ENOMEM:
460 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
461 break;
463 return 0;
465 return 1;
468 #else
470 netjack_poll (int sockfd, int timeout)
472 jack_error( "netjack_poll not implemented" );
473 return 0;
476 netjack_poll_deadline (int sockfd, jack_time_t deadline)
478 fd_set fds;
479 FD_ZERO( &fds );
480 FD_SET( sockfd, &fds );
482 struct timeval timeout;
483 while( 1 ) {
484 jack_time_t now = jack_get_time();
485 if( now >= deadline )
486 return 0;
488 int timeout_usecs = (deadline - now);
489 //jack_error( "timeout = %d", timeout_usecs );
490 timeout.tv_sec = 0;
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);
495 if( poll_err != 0 )
496 return poll_err;
499 return 0;
501 #endif
502 // This now reads all a socket has into the cache.
503 // replacing netjack_recv functions.
505 void
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;
510 int rcv_len;
511 jack_nframes_t framecnt;
512 cache_packet *cpack;
513 struct sockaddr_in sender_address;
514 #ifdef WIN32
515 int senderlen = sizeof( struct sockaddr_in );
516 u_long parm = 1;
517 ioctlsocket( sockfd, FIONBIO, &parm );
518 #else
519 unsigned int senderlen = sizeof( struct sockaddr_in );
520 #endif
521 while (1) {
522 #ifdef WIN32
523 rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, 0,
524 (struct sockaddr*) &sender_address, &senderlen);
525 #else
526 rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT,
527 (struct sockaddr*) &sender_address, &senderlen);
528 #endif
529 if (rcv_len < 0)
530 return;
532 if (pcache->master_address_valid) {
533 // Verify its from our master.
534 if (memcmp (&sender_address, &(pcache->master_address), senderlen) != 0)
535 continue;
536 } else {
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 ))
545 continue;
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();
553 void
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;
561 void
562 packet_cache_clear_old_packets (packet_cache *pcache, jack_nframes_t framecnt )
564 int i;
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 )
576 int i;
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]);
583 break;
587 if( cpack == NULL ) {
588 //printf( "retreive packet: %d....not found\n", framecnt );
589 return -1;
592 if( !cache_packet_is_complete( cpack ) ) {
593 return -1;
596 // ok. cpack is the one we want and its complete.
597 *packet_buf = cpack->packet_buf;
598 if( timestamp )
599 *timestamp = cpack->recv_timestamp;
601 pcache->last_framecnt_retreived_valid = 1;
602 pcache->last_framecnt_retreived = framecnt;
604 return pkt_size;
608 packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt )
610 int i;
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]);
617 break;
621 if( cpack == NULL ) {
622 //printf( "retreive packet: %d....not found\n", framecnt );
623 return -1;
626 if( !cache_packet_is_complete( cpack ) ) {
627 return -1;
630 cache_packet_reset (cpack);
631 packet_cache_clear_old_packets( pcache, framecnt );
633 return 0;
635 float
636 packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
638 int num_packets_before_us = 0;
639 int i;
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 )
655 int i;
656 jack_nframes_t best_offset = JACK_MAX_FRAMES / 2 - 1;
657 int retval = 0;
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" );
665 continue;
668 if( cpack->framecnt < expected_framecnt )
669 continue;
671 if( (cpack->framecnt - expected_framecnt) > best_offset ) {
672 continue;
675 best_offset = cpack->framecnt - expected_framecnt;
676 retval = 1;
678 if (best_offset == 0)
679 break;
681 if (retval && framecnt)
682 *framecnt = expected_framecnt + best_offset;
684 return retval;
688 packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt )
690 int i;
691 jack_nframes_t best_value = 0;
692 int retval = 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" );
700 continue;
703 if (cpack->framecnt < best_value) {
704 continue;
707 best_value = cpack->framecnt;
708 retval = 1;
711 if (retval && framecnt)
712 *framecnt = best_value;
714 return retval;
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 )
721 int i;
722 jack_nframes_t best_offset = 0;
723 int retval = 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" );
731 continue;
734 if ((cpack->framecnt - expected_framecnt) < best_offset) {
735 continue;
738 best_offset = cpack->framecnt - expected_framecnt;
739 retval = 1;
741 if( best_offset == 0 )
742 break;
744 if (retval && framecnt)
745 *framecnt = JACK_MAX_FRAMES - best_offset;
747 return retval;
749 // fragmented packet IO
750 void
751 netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
753 int frag_cnt = 0;
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) {
764 int err;
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);
768 if( err < 0 ) {
769 //printf( "error in send\n" );
770 perror( "send" );
772 } else {
773 int err;
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);
794 if( err < 0 ) {
795 //printf( "error in send\n" );
796 perror( "send" );
801 void
802 decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
804 int i;
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);
810 if (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;
820 } else
821 break; // no events can follow an empty event, we're done
825 void
826 encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
828 int i;
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
838 // otherwise drop it
839 if (written + payload_size < buffer_size_uint32 - 1) {
840 // write header
841 buffer_uint32[written] = htonl (payload_size);
842 written++;
843 buffer_uint32[written] = htonl (event.time);
844 written++;
845 buffer_uint32[written] = htonl (event.size);
846 written++;
848 // write data
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;
852 } else {
853 // buffer overflow
854 jack_error ("midi buffer overflow");
855 break;
858 // now put a netjack_midi 'no-payload' event, signaling EOF
859 buffer_uint32[written] = 0;
862 // render functions for float
863 void
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)
866 int chn = 0;
867 JSList *node = capture_ports;
868 #if HAVE_SAMPLERATE
869 JSList *src_node = capture_srcs;
870 #endif
872 uint32_t *packet_bufX = (uint32_t *)packet_payload;
874 if (!packet_payload)
875 return;
877 while (node != NULL) {
878 int i;
879 int_float_t val;
880 #if HAVE_SAMPLERATE
881 SRC_DATA src;
882 #endif
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)) {
890 #if HAVE_SAMPLERATE
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;
901 src.data_out = buf;
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);
910 } else
911 #endif
913 if( dont_htonl_floats ) {
914 memcpy( buf, packet_bufX, net_period_down * sizeof(jack_default_audio_sample_t));
915 } else {
916 for (i = 0; i < net_period_down; i++) {
917 val.i = packet_bufX[i];
918 val.i = ntohl (val.i);
919 buf[i] = val.f;
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);
932 chn++;
936 void
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 )
939 int chn = 0;
940 JSList *node = playback_ports;
941 #if HAVE_SAMPLERATE
942 JSList *src_node = playback_srcs;
943 #endif
945 uint32_t *packet_bufX = (uint32_t *) packet_payload;
947 while (node != NULL) {
948 #if HAVE_SAMPLERATE
949 SRC_DATA src;
950 #endif
951 int i;
952 int_float_t val;
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
961 #if HAVE_SAMPLERATE
962 if (net_period_up != nframes) {
963 SRC_STATE *src_state = src_node->data;
964 src.data_in = buf;
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);
980 } else
981 #endif
983 if( dont_htonl_floats ) {
984 memcpy( packet_bufX, buf, net_period_up * sizeof(jack_default_audio_sample_t) );
985 } else {
986 for (i = 0; i < net_period_up; i++) {
987 val.f = buf[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);
1002 chn++;
1006 // render functions for 16bit
1007 void
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)
1010 int chn = 0;
1011 JSList *node = capture_ports;
1012 #if HAVE_SAMPLERATE
1013 JSList *src_node = capture_srcs;
1014 #endif
1016 uint16_t *packet_bufX = (uint16_t *)packet_payload;
1018 if( !packet_payload )
1019 return;
1021 while (node != NULL) {
1022 int i;
1023 //uint32_t val;
1024 #if HAVE_SAMPLERATE
1025 SRC_DATA src;
1026 #endif
1028 jack_port_t *port = (jack_port_t *) node->data;
1029 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1031 #if HAVE_SAMPLERATE
1032 float *floatbuf = alloca (sizeof(float) * net_period_down);
1033 #endif
1034 const char *porttype = jack_port_type (port);
1036 if (jack_port_is_audio (porttype)) {
1037 // audio port, resample if necessary
1039 #if HAVE_SAMPLERATE
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;
1049 src.data_out = buf;
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);
1058 } else
1059 #endif
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);
1071 chn++;
1075 void
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)
1078 int chn = 0;
1079 JSList *node = playback_ports;
1080 #if HAVE_SAMPLERATE
1081 JSList *src_node = playback_srcs;
1082 #endif
1084 uint16_t *packet_bufX = (uint16_t *)packet_payload;
1086 while (node != NULL) {
1087 #if HAVE_SAMPLERATE
1088 SRC_DATA src;
1089 #endif
1090 int i;
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
1098 #if HAVE_SAMPLERATE
1099 if (net_period_up != nframes) {
1100 SRC_STATE *src_state = src_node->data;
1102 float *floatbuf = alloca (sizeof(float) * net_period_up);
1104 src.data_in = buf;
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);
1120 } else
1121 #endif
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);
1133 chn++;
1137 // render functions for 8bit
1138 void
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)
1141 int chn = 0;
1142 JSList *node = capture_ports;
1144 #if HAVE_SAMPLERATE
1145 JSList *src_node = capture_srcs;
1146 #endif
1148 int8_t *packet_bufX = (int8_t *)packet_payload;
1150 if (!packet_payload)
1151 return;
1153 while (node != NULL) {
1154 int i;
1155 //uint32_t val;
1156 #if HAVE_SAMPLERATE
1157 SRC_DATA src;
1158 #endif
1160 jack_port_t *port = (jack_port_t *) node->data;
1161 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1163 #if HAVE_SAMPLERATE
1164 float *floatbuf = alloca (sizeof (float) * net_period_down);
1165 #endif
1166 const char *porttype = jack_port_type (port);
1168 if (jack_port_is_audio(porttype)) {
1169 #if HAVE_SAMPLERATE
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;
1179 src.data_out = buf;
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);
1188 } else
1189 #endif
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);
1201 chn++;
1205 void
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)
1208 int chn = 0;
1209 JSList *node = playback_ports;
1210 #if HAVE_SAMPLERATE
1211 JSList *src_node = playback_srcs;
1212 #endif
1214 int8_t *packet_bufX = (int8_t *)packet_payload;
1216 while (node != NULL) {
1217 #if HAVE_SAMPLERATE
1218 SRC_DATA src;
1219 #endif
1220 int i;
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)) {
1227 #if HAVE_SAMPLERATE
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);
1235 src.data_in = buf;
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);
1250 } else
1251 #endif
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);
1263 chn++;
1267 #if HAVE_CELT
1268 // render functions for celt.
1269 void
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)
1272 int chn = 0;
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 );
1290 else
1291 celt_decode_float( decoder, packet_bufX, net_period_down, buf, nframes );
1292 #else
1293 if( !packet_payload )
1294 celt_decode_float( decoder, NULL, net_period_down, buf );
1295 else
1296 celt_decode_float( decoder, packet_bufX, net_period_down, buf );
1297 #endif
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);
1310 chn++;
1314 void
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)
1317 int chn = 0;
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.
1331 int encoded_bytes;
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 );
1337 #else
1338 encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
1339 #endif
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);
1352 chn++;
1356 #endif
1358 #if HAVE_OPUS
1359 #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
1360 // render functions for Opus.
1361 void
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)
1364 int chn = 0;
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));
1381 else {
1382 unsigned short len;
1383 memcpy(&len, packet_bufX, CDO);
1384 len = ntohs(len);
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);
1399 chn++;
1403 void
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)
1406 int chn = 0;
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.
1420 int encoded_bytes;
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);
1437 chn++;
1440 #endif
1442 /* Wrapper functions with bitdepth argument... */
1443 void
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)
1446 if (bitdepth == 8)
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);
1450 #if HAVE_CELT
1451 else if (bitdepth == CELT_MODE)
1452 render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
1453 #endif
1454 #if HAVE_OPUS
1455 else if (bitdepth == OPUS_MODE)
1456 render_payload_to_jack_ports_opus (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
1457 #endif
1458 else
1459 render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes, dont_htonl_floats);
1462 void
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)
1465 if (bitdepth == 8)
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);
1469 #if HAVE_CELT
1470 else if (bitdepth == CELT_MODE)
1471 render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
1472 #endif
1473 #if HAVE_OPUS
1474 else if (bitdepth == OPUS_MODE)
1475 render_jack_ports_to_payload_opus (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
1476 #endif
1477 else
1478 render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);