Fix build under mixed mode
[jack2.git] / common / netjack_packet.c
blob4671368f33e70f66b711ac5095e3c886e959c196
2 /*
3 * NetJack - Packet Handling functions
5 * used by the driver and the jacknet_client
7 * Copyright (C) 2019 Karl Linden <karl.j.linden@gmail.com>
8 * Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
9 * Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
10 * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
30 #if defined(HAVE_CONFIG_H)
31 #include "config.h"
32 #endif
34 #ifdef __APPLE__
35 #define _DARWIN_C_SOURCE
36 #endif
38 #if HAVE_PPOLL
39 #define _GNU_SOURCE
40 #endif
42 #include <alloca.h>
43 #include <math.h>
44 #include <stdio.h>
45 #include <memory.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <stdarg.h>
51 #include <jack/types.h>
53 #include <sys/types.h>
55 #ifdef WIN32
56 #include <winsock2.h>
57 #include <malloc.h>
58 #define socklen_t int
59 #else
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <poll.h>
63 #endif
65 #include <errno.h>
66 #include <signal.h>
68 #if HAVE_SAMPLERATE
69 #include <samplerate.h>
70 #endif
72 #if HAVE_CELT
73 #include <celt/celt.h>
74 #endif
76 #if HAVE_OPUS
77 #include <opus/opus.h>
78 #include <opus/opus_custom.h>
79 #endif
81 #include "netjack_packet.h"
82 #include "JackError.h"
84 #ifdef NO_JACK_ERROR
85 #define jack_error printf
86 #endif
88 int fraggo = 0;
90 void
91 packet_header_hton (jacknet_packet_header *pkthdr)
93 pkthdr->capture_channels_audio = htonl(pkthdr->capture_channels_audio);
94 pkthdr->playback_channels_audio = htonl(pkthdr->playback_channels_audio);
95 pkthdr->capture_channels_midi = htonl(pkthdr->capture_channels_midi);
96 pkthdr->playback_channels_midi = htonl(pkthdr->playback_channels_midi);
97 pkthdr->period_size = htonl(pkthdr->period_size);
98 pkthdr->sample_rate = htonl(pkthdr->sample_rate);
99 pkthdr->sync_state = htonl(pkthdr->sync_state);
100 pkthdr->transport_frame = htonl(pkthdr->transport_frame);
101 pkthdr->transport_state = htonl(pkthdr->transport_state);
102 pkthdr->framecnt = htonl(pkthdr->framecnt);
103 pkthdr->latency = htonl(pkthdr->latency);
104 pkthdr->reply_port = htonl(pkthdr->reply_port);
105 pkthdr->mtu = htonl(pkthdr->mtu);
106 pkthdr->fragment_nr = htonl(pkthdr->fragment_nr);
109 void
110 packet_header_ntoh (jacknet_packet_header *pkthdr)
112 pkthdr->capture_channels_audio = ntohl(pkthdr->capture_channels_audio);
113 pkthdr->playback_channels_audio = ntohl(pkthdr->playback_channels_audio);
114 pkthdr->capture_channels_midi = ntohl(pkthdr->capture_channels_midi);
115 pkthdr->playback_channels_midi = ntohl(pkthdr->playback_channels_midi);
116 pkthdr->period_size = ntohl(pkthdr->period_size);
117 pkthdr->sample_rate = ntohl(pkthdr->sample_rate);
118 pkthdr->sync_state = ntohl(pkthdr->sync_state);
119 pkthdr->transport_frame = ntohl(pkthdr->transport_frame);
120 pkthdr->transport_state = ntohl(pkthdr->transport_state);
121 pkthdr->framecnt = ntohl(pkthdr->framecnt);
122 pkthdr->latency = ntohl(pkthdr->latency);
123 pkthdr->reply_port = ntohl(pkthdr->reply_port);
124 pkthdr->mtu = ntohl(pkthdr->mtu);
125 pkthdr->fragment_nr = ntohl(pkthdr->fragment_nr);
128 int get_sample_size (int bitdepth)
130 if (bitdepth == 8)
131 return sizeof (int8_t);
132 if (bitdepth == 16)
133 return sizeof (int16_t);
134 //JN: why? is this for buffer sizes before or after encoding?
135 //JN: if the former, why not int16_t, if the latter, shouldn't it depend on -c N?
136 if( bitdepth == CELT_MODE )
137 return sizeof( unsigned char );
138 if( bitdepth == OPUS_MODE )
139 return sizeof( unsigned char );
140 return sizeof (int32_t);
143 int jack_port_is_audio(const char *porttype)
145 return (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0);
148 int jack_port_is_midi(const char *porttype)
150 return (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0);
154 // fragment management functions.
156 packet_cache
157 *packet_cache_new (int num_packets, int pkt_size, int mtu)
159 int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
160 int i, fragment_number;
162 if( pkt_size == sizeof(jacknet_packet_header) )
163 fragment_number = 1;
164 else
165 fragment_number = (pkt_size - sizeof (jacknet_packet_header) - 1) / fragment_payload_size + 1;
167 packet_cache *pcache = malloc (sizeof (packet_cache));
168 if (pcache == NULL) {
169 jack_error ("could not allocate packet cache (1)");
170 return NULL;
173 pcache->size = num_packets;
174 pcache->packets = malloc (sizeof (cache_packet) * num_packets);
175 pcache->master_address_valid = 0;
176 pcache->last_framecnt_retreived = 0;
177 pcache->last_framecnt_retreived_valid = 0;
179 if (pcache->packets == NULL) {
180 jack_error ("could not allocate packet cache (2)");
181 return NULL;
184 for (i = 0; i < num_packets; i++) {
185 pcache->packets[i].valid = 0;
186 pcache->packets[i].num_fragments = fragment_number;
187 pcache->packets[i].packet_size = pkt_size;
188 pcache->packets[i].mtu = mtu;
189 pcache->packets[i].framecnt = 0;
190 pcache->packets[i].fragment_array = malloc (sizeof (char) * fragment_number);
191 pcache->packets[i].packet_buf = malloc (pkt_size);
192 if ((pcache->packets[i].fragment_array == NULL) || (pcache->packets[i].packet_buf == NULL)) {
193 jack_error ("could not allocate packet cache (3)");
194 return NULL;
197 pcache->mtu = mtu;
199 return pcache;
202 void
203 packet_cache_free (packet_cache *pcache)
205 int i;
206 if( pcache == NULL )
207 return;
209 for (i = 0; i < pcache->size; i++) {
210 free (pcache->packets[i].fragment_array);
211 free (pcache->packets[i].packet_buf);
214 free (pcache->packets);
215 free (pcache);
218 cache_packet
219 *packet_cache_get_packet (packet_cache *pcache, jack_nframes_t framecnt)
221 int i;
222 cache_packet *retval;
224 for (i = 0; i < pcache->size; i++) {
225 if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt))
226 return &(pcache->packets[i]);
229 // The Packet is not in the packet cache.
230 // find a free packet.
232 retval = packet_cache_get_free_packet (pcache);
233 if (retval != NULL) {
234 cache_packet_set_framecnt (retval, framecnt);
235 return retval;
238 // No Free Packet available
239 // Get The Oldest packet and reset it.
241 retval = packet_cache_get_oldest_packet (pcache);
242 //printf( "Dropping %d from Cache :S\n", retval->framecnt );
243 cache_packet_reset (retval);
244 cache_packet_set_framecnt (retval, framecnt);
246 return retval;
249 // TODO: fix wrapping case... need to pass
250 // current expected frame here.
252 // or just save framecount into packet_cache.
254 cache_packet
255 *packet_cache_get_oldest_packet (packet_cache *pcache)
257 jack_nframes_t minimal_frame = JACK_MAX_FRAMES;
258 cache_packet *retval = &(pcache->packets[0]);
259 int i;
261 for (i = 0; i < pcache->size; i++) {
262 if (pcache->packets[i].valid && (pcache->packets[i].framecnt < minimal_frame)) {
263 minimal_frame = pcache->packets[i].framecnt;
264 retval = &(pcache->packets[i]);
268 return retval;
271 cache_packet
272 *packet_cache_get_free_packet (packet_cache *pcache)
274 int i;
276 for (i = 0; i < pcache->size; i++) {
277 if (pcache->packets[i].valid == 0)
278 return &(pcache->packets[i]);
281 return NULL;
284 void
285 cache_packet_reset (cache_packet *pack)
287 int i;
288 pack->valid = 0;
290 // XXX: i don't think this is necessary here...
291 // fragment array is cleared in _set_framecnt()
293 for (i = 0; i < pack->num_fragments; i++)
294 pack->fragment_array[i] = 0;
297 void
298 cache_packet_set_framecnt (cache_packet *pack, jack_nframes_t framecnt)
300 int i;
302 pack->framecnt = framecnt;
304 for (i = 0; i < pack->num_fragments; i++)
305 pack->fragment_array[i] = 0;
307 pack->valid = 1;
310 void
311 cache_packet_add_fragment (cache_packet *pack, char *packet_buf, int rcv_len)
313 jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
314 int fragment_payload_size = pack->mtu - sizeof (jacknet_packet_header);
315 char *packet_bufX = pack->packet_buf + sizeof (jacknet_packet_header);
316 char *dataX = packet_buf + sizeof (jacknet_packet_header);
318 jack_nframes_t fragment_nr = ntohl (pkthdr->fragment_nr);
319 jack_nframes_t framecnt = ntohl (pkthdr->framecnt);
321 if (framecnt != pack->framecnt) {
322 jack_error ("error. framecnts don't match");
323 return;
326 if (fragment_nr == 0) {
327 memcpy (pack->packet_buf, packet_buf, rcv_len);
328 pack->fragment_array[0] = 1;
330 return;
333 if ((fragment_nr < pack->num_fragments) && (fragment_nr > 0)) {
334 if ((fragment_nr * fragment_payload_size + rcv_len - sizeof (jacknet_packet_header)) <= (pack->packet_size - sizeof (jacknet_packet_header))) {
335 memcpy (packet_bufX + fragment_nr * fragment_payload_size, dataX, rcv_len - sizeof (jacknet_packet_header));
336 pack->fragment_array[fragment_nr] = 1;
337 } else
338 jack_error ("too long packet received...");
343 cache_packet_is_complete (cache_packet *pack)
345 int i;
346 for (i = 0; i < pack->num_fragments; i++)
347 if (pack->fragment_array[i] == 0)
348 return 0;
350 return 1;
353 #ifndef WIN32
354 // new poll using nanoseconds resolution and
355 // not waiting forever.
357 netjack_poll_deadline (int sockfd, jack_time_t deadline)
359 struct pollfd fds;
360 int poll_err = 0;
361 #if HAVE_PPOLL
362 struct timespec timeout_spec = { 0, 0 };
363 #else
364 int timeout;
365 #endif
367 jack_time_t now = jack_get_time();
368 if( now >= deadline )
369 return 0;
371 if( (deadline - now) >= 1000000 ) {
372 jack_error( "deadline more than 1 second in the future, trimming it." );
373 deadline = now + 500000;
375 #if HAVE_PPOLL
376 timeout_spec.tv_nsec = (deadline - now) * 1000;
377 #else
378 timeout = lrintf( (float)(deadline - now) / 1000.0 );
379 #endif
381 fds.fd = sockfd;
382 fds.events = POLLIN;
384 #if HAVE_PPOLL
385 poll_err = ppoll (&fds, 1, &timeout_spec, NULL);
386 #else
387 poll_err = poll (&fds, 1, timeout);
388 #endif
390 if (poll_err == -1) {
391 switch (errno) {
392 case EBADF:
393 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
394 break;
395 case EFAULT:
396 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
397 break;
398 case EINTR:
399 jack_error ("Error %d: A signal occurred before any requested event", errno);
400 break;
401 case EINVAL:
402 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
403 break;
404 case ENOMEM:
405 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
406 break;
409 return poll_err;
413 netjack_poll (int sockfd, int timeout)
415 struct pollfd fds;
416 int i, poll_err = 0;
417 sigset_t sigmask, rsigmask;
418 struct sigaction action;
420 sigemptyset(&sigmask);
421 sigaddset(&sigmask, SIGHUP);
422 sigaddset(&sigmask, SIGINT);
423 sigaddset(&sigmask, SIGQUIT);
424 sigaddset(&sigmask, SIGPIPE);
425 sigaddset(&sigmask, SIGTERM);
426 sigaddset(&sigmask, SIGUSR1);
427 sigaddset(&sigmask, SIGUSR2);
429 action.sa_handler = SIG_DFL;
430 action.sa_mask = sigmask;
431 action.sa_flags = SA_RESTART;
433 for (i = 1; i < NSIG; i++)
434 if (sigismember (&sigmask, i))
435 sigaction (i, &action, 0);
437 fds.fd = sockfd;
438 fds.events = POLLIN;
440 sigprocmask(SIG_UNBLOCK, &sigmask, &rsigmask);
441 while (poll_err == 0) {
442 poll_err = poll (&fds, 1, timeout);
444 sigprocmask(SIG_SETMASK, &rsigmask, NULL);
446 if (poll_err == -1) {
447 switch (errno) {
448 case EBADF:
449 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
450 break;
451 case EFAULT:
452 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
453 break;
454 case EINTR:
455 jack_error ("Error %d: A signal occurred before any requested event", errno);
456 break;
457 case EINVAL:
458 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
459 break;
460 case ENOMEM:
461 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
462 break;
464 return 0;
466 return 1;
469 #else
471 netjack_poll (int sockfd, int timeout)
473 jack_error( "netjack_poll not implemented" );
474 return 0;
477 netjack_poll_deadline (int sockfd, jack_time_t deadline)
479 fd_set fds;
480 FD_ZERO( &fds );
481 FD_SET( sockfd, &fds );
483 struct timeval timeout;
484 while( 1 ) {
485 jack_time_t now = jack_get_time();
486 if( now >= deadline )
487 return 0;
489 int timeout_usecs = (deadline - now);
490 //jack_error( "timeout = %d", timeout_usecs );
491 timeout.tv_sec = 0;
492 timeout.tv_usec = (timeout_usecs < 500) ? 500 : timeout_usecs;
493 timeout.tv_usec = (timeout_usecs > 1000000) ? 500000 : timeout_usecs;
495 int poll_err = select (0, &fds, NULL, NULL, &timeout);
496 if( poll_err != 0 )
497 return poll_err;
500 return 0;
502 #endif
503 // This now reads all a socket has into the cache.
504 // replacing netjack_recv functions.
506 void
507 packet_cache_drain_socket( packet_cache *pcache, int sockfd )
509 char *rx_packet = alloca (pcache->mtu);
510 jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
511 int rcv_len;
512 jack_nframes_t framecnt;
513 cache_packet *cpack;
514 struct sockaddr_in sender_address;
515 #ifdef WIN32
516 int senderlen = sizeof( struct sockaddr_in );
517 u_long parm = 1;
518 ioctlsocket( sockfd, FIONBIO, &parm );
519 #else
520 unsigned int senderlen = sizeof( struct sockaddr_in );
521 #endif
522 while (1) {
523 #ifdef WIN32
524 rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, 0,
525 (struct sockaddr*) &sender_address, &senderlen);
526 #else
527 rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT,
528 (struct sockaddr*) &sender_address, &senderlen);
529 #endif
530 if (rcv_len < 0)
531 return;
533 if (pcache->master_address_valid) {
534 // Verify its from our master.
535 if (memcmp (&sender_address, &(pcache->master_address), senderlen) != 0)
536 continue;
537 } else {
538 // Setup this one as master
539 //printf( "setup master...\n" );
540 memcpy ( &(pcache->master_address), &sender_address, senderlen );
541 pcache->master_address_valid = 1;
544 framecnt = ntohl (pkthdr->framecnt);
545 if( pcache->last_framecnt_retreived_valid && (framecnt <= pcache->last_framecnt_retreived ))
546 continue;
548 cpack = packet_cache_get_packet (pcache, framecnt);
549 cache_packet_add_fragment (cpack, rx_packet, rcv_len);
550 cpack->recv_timestamp = jack_get_time();
554 void
555 packet_cache_reset_master_address( packet_cache *pcache )
557 pcache->master_address_valid = 0;
558 pcache->last_framecnt_retreived = 0;
559 pcache->last_framecnt_retreived_valid = 0;
562 void
563 packet_cache_clear_old_packets (packet_cache *pcache, jack_nframes_t framecnt )
565 int i;
567 for (i = 0; i < pcache->size; i++) {
568 if (pcache->packets[i].valid && (pcache->packets[i].framecnt < framecnt)) {
569 cache_packet_reset (&(pcache->packets[i]));
575 packet_cache_retreive_packet_pointer( packet_cache *pcache, jack_nframes_t framecnt, char **packet_buf, int pkt_size, jack_time_t *timestamp )
577 int i;
578 cache_packet *cpack = NULL;
581 for (i = 0; i < pcache->size; i++) {
582 if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
583 cpack = &(pcache->packets[i]);
584 break;
588 if( cpack == NULL ) {
589 //printf( "retrieve packet: %d....not found\n", framecnt );
590 return -1;
593 if( !cache_packet_is_complete( cpack ) ) {
594 return -1;
597 // ok. cpack is the one we want and its complete.
598 *packet_buf = cpack->packet_buf;
599 if( timestamp )
600 *timestamp = cpack->recv_timestamp;
602 pcache->last_framecnt_retreived_valid = 1;
603 pcache->last_framecnt_retreived = framecnt;
605 return pkt_size;
609 packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt )
611 int i;
612 cache_packet *cpack = NULL;
615 for (i = 0; i < pcache->size; i++) {
616 if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
617 cpack = &(pcache->packets[i]);
618 break;
622 if( cpack == NULL ) {
623 //printf( "retrieve packet: %d....not found\n", framecnt );
624 return -1;
627 if( !cache_packet_is_complete( cpack ) ) {
628 return -1;
631 cache_packet_reset (cpack);
632 packet_cache_clear_old_packets( pcache, framecnt );
634 return 0;
636 float
637 packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
639 int num_packets_before_us = 0;
640 int i;
642 for (i = 0; i < pcache->size; i++) {
643 cache_packet *cpack = &(pcache->packets[i]);
644 if (cpack->valid && cache_packet_is_complete( cpack ))
645 if( cpack->framecnt >= expected_framecnt )
646 num_packets_before_us += 1;
649 return 100.0 * (float)num_packets_before_us / (float)( pcache->size );
652 // Returns 0 when no valid packet is inside the cache.
654 packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
656 int i;
657 jack_nframes_t best_offset = JACK_MAX_FRAMES / 2 - 1;
658 int retval = 0;
660 for (i = 0; i < pcache->size; i++) {
661 cache_packet *cpack = &(pcache->packets[i]);
662 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
664 if (!cpack->valid || !cache_packet_is_complete( cpack )) {
665 //printf( "invalid\n" );
666 continue;
669 if( cpack->framecnt < expected_framecnt )
670 continue;
672 if( (cpack->framecnt - expected_framecnt) > best_offset ) {
673 continue;
676 best_offset = cpack->framecnt - expected_framecnt;
677 retval = 1;
679 if (best_offset == 0)
680 break;
682 if (retval && framecnt)
683 *framecnt = expected_framecnt + best_offset;
685 return retval;
689 packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt )
691 int i;
692 jack_nframes_t best_value = 0;
693 int retval = 0;
695 for (i = 0; i < pcache->size; i++) {
696 cache_packet *cpack = &(pcache->packets[i]);
697 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
699 if (!cpack->valid || !cache_packet_is_complete( cpack )) {
700 //printf( "invalid\n" );
701 continue;
704 if (cpack->framecnt < best_value) {
705 continue;
708 best_value = cpack->framecnt;
709 retval = 1;
712 if (retval && framecnt)
713 *framecnt = best_value;
715 return retval;
718 // Returns 0 when no valid packet is inside the cache.
720 packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
722 int i;
723 jack_nframes_t best_offset = 0;
724 int retval = 0;
726 for (i = 0; i < pcache->size; i++) {
727 cache_packet *cpack = &(pcache->packets[i]);
728 //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
730 if (!cpack->valid || !cache_packet_is_complete( cpack )) {
731 //printf( "invalid\n" );
732 continue;
735 if ((cpack->framecnt - expected_framecnt) < best_offset) {
736 continue;
739 best_offset = cpack->framecnt - expected_framecnt;
740 retval = 1;
742 if( best_offset == 0 )
743 break;
745 if (retval && framecnt)
746 *framecnt = JACK_MAX_FRAMES - best_offset;
748 return retval;
750 // fragmented packet IO
751 void
752 netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
754 int frag_cnt = 0;
755 char *tx_packet, *dataX;
756 jacknet_packet_header *pkthdr;
758 tx_packet = alloca (mtu + 10);
759 dataX = tx_packet + sizeof (jacknet_packet_header);
760 pkthdr = (jacknet_packet_header *) tx_packet;
762 int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
764 if (pkt_size <= mtu) {
765 int err;
766 pkthdr = (jacknet_packet_header *) packet_buf;
767 pkthdr->fragment_nr = htonl (0);
768 err = sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
769 if( err < 0 ) {
770 //printf( "error in send\n" );
771 perror( "send" );
773 } else {
774 int err;
775 // Copy the packet header to the tx pack first.
776 memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
778 // Now loop and send all
779 char *packet_bufX = packet_buf + sizeof (jacknet_packet_header);
781 while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size)) {
782 pkthdr->fragment_nr = htonl (frag_cnt++);
783 memcpy (dataX, packet_bufX, fragment_payload_size);
784 sendto (sockfd, tx_packet, mtu, flags, addr, addr_size);
785 packet_bufX += fragment_payload_size;
788 int last_payload_size = packet_buf + pkt_size - packet_bufX;
789 memcpy (dataX, packet_bufX, last_payload_size);
790 pkthdr->fragment_nr = htonl (frag_cnt);
791 //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
793 // sendto(last_pack_size);
794 err = sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
795 if( err < 0 ) {
796 //printf( "error in send\n" );
797 perror( "send" );
802 void
803 decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
805 int i;
806 jack_midi_clear_buffer (buf);
807 for (i = 0; i < buffer_size_uint32 - 3;) {
808 uint32_t payload_size;
809 payload_size = buffer_uint32[i];
810 payload_size = ntohl (payload_size);
811 if (payload_size) {
812 jack_midi_event_t event;
813 event.time = ntohl (buffer_uint32[i + 1]);
814 event.size = ntohl (buffer_uint32[i + 2]);
815 event.buffer = (jack_midi_data_t*) (&(buffer_uint32[i + 3]));
816 jack_midi_event_write (buf, event.time, event.buffer, event.size);
818 // skip to the next event
819 unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
820 i += 3 + nb_data_quads;
821 } else
822 break; // no events can follow an empty event, we're done
826 void
827 encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
829 int i;
830 unsigned int written = 0;
831 // midi port, encode midi events
832 unsigned int nevents = jack_midi_get_event_count (buf);
833 for (i = 0; i < nevents; ++i) {
834 jack_midi_event_t event;
835 jack_midi_event_get (&event, buf, i);
836 unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
837 unsigned int payload_size = 3 + nb_data_quads;
838 // only write if we have sufficient space for the event
839 // otherwise drop it
840 if (written + payload_size < buffer_size_uint32 - 1) {
841 // write header
842 buffer_uint32[written] = htonl (payload_size);
843 written++;
844 buffer_uint32[written] = htonl (event.time);
845 written++;
846 buffer_uint32[written] = htonl (event.size);
847 written++;
849 // write data
850 jack_midi_data_t* tmpbuff = (jack_midi_data_t*)(&(buffer_uint32[written]));
851 memcpy (tmpbuff, event.buffer, event.size);
852 written += nb_data_quads;
853 } else {
854 // buffer overflow
855 jack_error ("midi buffer overflow");
856 break;
859 // now put a netjack_midi 'no-payload' event, signaling EOF
860 buffer_uint32[written] = 0;
863 // render functions for float
864 void
865 render_payload_to_jack_ports_float ( void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes, int dont_htonl_floats)
867 int chn = 0;
868 JSList *node = capture_ports;
869 #if HAVE_SAMPLERATE
870 JSList *src_node = capture_srcs;
871 #endif
873 uint32_t *packet_bufX = (uint32_t *)packet_payload;
875 if (!packet_payload)
876 return;
878 while (node != NULL) {
879 int i;
880 int_float_t val;
881 #if HAVE_SAMPLERATE
882 SRC_DATA src;
883 #endif
885 jack_port_t *port = (jack_port_t *) node->data;
886 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
888 const char *porttype = jack_port_type (port);
890 if (jack_port_is_audio (porttype)) {
891 #if HAVE_SAMPLERATE
892 // audio port, resample if necessary
893 if (net_period_down != nframes) {
894 SRC_STATE *src_state = src_node->data;
895 for (i = 0; i < net_period_down; i++) {
896 packet_bufX[i] = ntohl (packet_bufX[i]);
899 src.data_in = (float *) packet_bufX;
900 src.input_frames = net_period_down;
902 src.data_out = buf;
903 src.output_frames = nframes;
905 src.src_ratio = (float) nframes / (float) net_period_down;
906 src.end_of_input = 0;
908 src_set_ratio (src_state, src.src_ratio);
909 src_process (src_state, &src);
910 src_node = jack_slist_next (src_node);
911 } else
912 #endif
914 if( dont_htonl_floats ) {
915 memcpy( buf, packet_bufX, net_period_down * sizeof(jack_default_audio_sample_t));
916 } else {
917 for (i = 0; i < net_period_down; i++) {
918 val.i = packet_bufX[i];
919 val.i = ntohl (val.i);
920 buf[i] = val.f;
924 } else if (jack_port_is_midi (porttype)) {
925 // midi port, decode midi events
926 // convert the data buffer to a standard format (uint32_t based)
927 unsigned int buffer_size_uint32 = net_period_down;
928 uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
929 decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
931 packet_bufX = (packet_bufX + net_period_down);
932 node = jack_slist_next (node);
933 chn++;
937 void
938 render_jack_ports_to_payload_float (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up, int dont_htonl_floats )
940 int chn = 0;
941 JSList *node = playback_ports;
942 #if HAVE_SAMPLERATE
943 JSList *src_node = playback_srcs;
944 #endif
946 uint32_t *packet_bufX = (uint32_t *) packet_payload;
948 while (node != NULL) {
949 #if HAVE_SAMPLERATE
950 SRC_DATA src;
951 #endif
952 int i;
953 int_float_t val;
954 jack_port_t *port = (jack_port_t *) node->data;
955 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
957 const char *porttype = jack_port_type (port);
959 if (jack_port_is_audio (porttype)) {
960 // audio port, resample if necessary
962 #if HAVE_SAMPLERATE
963 if (net_period_up != nframes) {
964 SRC_STATE *src_state = src_node->data;
965 src.data_in = buf;
966 src.input_frames = nframes;
968 src.data_out = (float *) packet_bufX;
969 src.output_frames = net_period_up;
971 src.src_ratio = (float) net_period_up / (float) nframes;
972 src.end_of_input = 0;
974 src_set_ratio (src_state, src.src_ratio);
975 src_process (src_state, &src);
977 for (i = 0; i < net_period_up; i++) {
978 packet_bufX[i] = htonl (packet_bufX[i]);
980 src_node = jack_slist_next (src_node);
981 } else
982 #endif
984 if( dont_htonl_floats ) {
985 memcpy( packet_bufX, buf, net_period_up * sizeof(jack_default_audio_sample_t) );
986 } else {
987 for (i = 0; i < net_period_up; i++) {
988 val.f = buf[i];
989 val.i = htonl (val.i);
990 packet_bufX[i] = val.i;
994 } else if (jack_port_is_midi (porttype)) {
995 // encode midi events from port to packet
996 // convert the data buffer to a standard format (uint32_t based)
997 unsigned int buffer_size_uint32 = net_period_up;
998 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
999 encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1001 packet_bufX = (packet_bufX + net_period_up);
1002 node = jack_slist_next (node);
1003 chn++;
1007 // render functions for 16bit
1008 void
1009 render_payload_to_jack_ports_16bit (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
1011 int chn = 0;
1012 JSList *node = capture_ports;
1013 #if HAVE_SAMPLERATE
1014 JSList *src_node = capture_srcs;
1015 #endif
1017 uint16_t *packet_bufX = (uint16_t *)packet_payload;
1019 if( !packet_payload )
1020 return;
1022 while (node != NULL) {
1023 int i;
1024 //uint32_t val;
1025 #if HAVE_SAMPLERATE
1026 SRC_DATA src;
1027 #endif
1029 jack_port_t *port = (jack_port_t *) node->data;
1030 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1032 #if HAVE_SAMPLERATE
1033 float *floatbuf = alloca (sizeof(float) * net_period_down);
1034 #endif
1035 const char *porttype = jack_port_type (port);
1037 if (jack_port_is_audio (porttype)) {
1038 // audio port, resample if necessary
1040 #if HAVE_SAMPLERATE
1041 if (net_period_down != nframes) {
1042 SRC_STATE *src_state = src_node->data;
1043 for (i = 0; i < net_period_down; i++) {
1044 floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
1047 src.data_in = floatbuf;
1048 src.input_frames = net_period_down;
1050 src.data_out = buf;
1051 src.output_frames = nframes;
1053 src.src_ratio = (float) nframes / (float) net_period_down;
1054 src.end_of_input = 0;
1056 src_set_ratio (src_state, src.src_ratio);
1057 src_process (src_state, &src);
1058 src_node = jack_slist_next (src_node);
1059 } else
1060 #endif
1061 for (i = 0; i < net_period_down; i++)
1062 buf[i] = ((float) ntohs (packet_bufX[i])) / 32768.0 - 1.0;
1063 } else if (jack_port_is_midi (porttype)) {
1064 // midi port, decode midi events
1065 // convert the data buffer to a standard format (uint32_t based)
1066 unsigned int buffer_size_uint32 = net_period_down / 2;
1067 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1068 decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1070 packet_bufX = (packet_bufX + net_period_down);
1071 node = jack_slist_next (node);
1072 chn++;
1076 void
1077 render_jack_ports_to_payload_16bit (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
1079 int chn = 0;
1080 JSList *node = playback_ports;
1081 #if HAVE_SAMPLERATE
1082 JSList *src_node = playback_srcs;
1083 #endif
1085 uint16_t *packet_bufX = (uint16_t *)packet_payload;
1087 while (node != NULL) {
1088 #if HAVE_SAMPLERATE
1089 SRC_DATA src;
1090 #endif
1091 int i;
1092 jack_port_t *port = (jack_port_t *) node->data;
1093 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1094 const char *porttype = jack_port_type (port);
1096 if (jack_port_is_audio (porttype)) {
1097 // audio port, resample if necessary
1099 #if HAVE_SAMPLERATE
1100 if (net_period_up != nframes) {
1101 SRC_STATE *src_state = src_node->data;
1103 float *floatbuf = alloca (sizeof(float) * net_period_up);
1105 src.data_in = buf;
1106 src.input_frames = nframes;
1108 src.data_out = floatbuf;
1109 src.output_frames = net_period_up;
1111 src.src_ratio = (float) net_period_up / (float) nframes;
1112 src.end_of_input = 0;
1114 src_set_ratio (src_state, src.src_ratio);
1115 src_process (src_state, &src);
1117 for (i = 0; i < net_period_up; i++) {
1118 packet_bufX[i] = htons (((uint16_t)((floatbuf[i] + 1.0) * 32767.0)));
1120 src_node = jack_slist_next (src_node);
1121 } else
1122 #endif
1123 for (i = 0; i < net_period_up; i++)
1124 packet_bufX[i] = htons(((uint16_t)((buf[i] + 1.0) * 32767.0)));
1125 } else if (jack_port_is_midi (porttype)) {
1126 // encode midi events from port to packet
1127 // convert the data buffer to a standard format (uint32_t based)
1128 unsigned int buffer_size_uint32 = net_period_up / 2;
1129 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1130 encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1132 packet_bufX = (packet_bufX + net_period_up);
1133 node = jack_slist_next (node);
1134 chn++;
1138 // render functions for 8bit
1139 void
1140 render_payload_to_jack_ports_8bit (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
1142 int chn = 0;
1143 JSList *node = capture_ports;
1145 #if HAVE_SAMPLERATE
1146 JSList *src_node = capture_srcs;
1147 #endif
1149 int8_t *packet_bufX = (int8_t *)packet_payload;
1151 if (!packet_payload)
1152 return;
1154 while (node != NULL) {
1155 int i;
1156 //uint32_t val;
1157 #if HAVE_SAMPLERATE
1158 SRC_DATA src;
1159 #endif
1161 jack_port_t *port = (jack_port_t *) node->data;
1162 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1164 #if HAVE_SAMPLERATE
1165 float *floatbuf = alloca (sizeof (float) * net_period_down);
1166 #endif
1167 const char *porttype = jack_port_type (port);
1169 if (jack_port_is_audio(porttype)) {
1170 #if HAVE_SAMPLERATE
1171 // audio port, resample if necessary
1172 if (net_period_down != nframes) {
1173 SRC_STATE *src_state = src_node->data;
1174 for (i = 0; i < net_period_down; i++)
1175 floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
1177 src.data_in = floatbuf;
1178 src.input_frames = net_period_down;
1180 src.data_out = buf;
1181 src.output_frames = nframes;
1183 src.src_ratio = (float) nframes / (float) net_period_down;
1184 src.end_of_input = 0;
1186 src_set_ratio (src_state, src.src_ratio);
1187 src_process (src_state, &src);
1188 src_node = jack_slist_next (src_node);
1189 } else
1190 #endif
1191 for (i = 0; i < net_period_down; i++)
1192 buf[i] = ((float) packet_bufX[i]) / 127.0;
1193 } else if (jack_port_is_midi (porttype)) {
1194 // midi port, decode midi events
1195 // convert the data buffer to a standard format (uint32_t based)
1196 unsigned int buffer_size_uint32 = net_period_down / 2;
1197 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1198 decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1200 packet_bufX = (packet_bufX + net_period_down);
1201 node = jack_slist_next (node);
1202 chn++;
1206 void
1207 render_jack_ports_to_payload_8bit (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
1209 int chn = 0;
1210 JSList *node = playback_ports;
1211 #if HAVE_SAMPLERATE
1212 JSList *src_node = playback_srcs;
1213 #endif
1215 int8_t *packet_bufX = (int8_t *)packet_payload;
1217 while (node != NULL) {
1218 #if HAVE_SAMPLERATE
1219 SRC_DATA src;
1220 #endif
1221 int i;
1222 jack_port_t *port = (jack_port_t *) node->data;
1224 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1225 const char *porttype = jack_port_type (port);
1227 if (jack_port_is_audio (porttype)) {
1228 #if HAVE_SAMPLERATE
1229 // audio port, resample if necessary
1230 if (net_period_up != nframes) {
1232 SRC_STATE *src_state = src_node->data;
1234 float *floatbuf = alloca (sizeof (float) * net_period_up);
1236 src.data_in = buf;
1237 src.input_frames = nframes;
1239 src.data_out = floatbuf;
1240 src.output_frames = net_period_up;
1242 src.src_ratio = (float) net_period_up / (float) nframes;
1243 src.end_of_input = 0;
1245 src_set_ratio (src_state, src.src_ratio);
1246 src_process (src_state, &src);
1248 for (i = 0; i < net_period_up; i++)
1249 packet_bufX[i] = floatbuf[i] * 127.0;
1250 src_node = jack_slist_next (src_node);
1251 } else
1252 #endif
1253 for (i = 0; i < net_period_up; i++)
1254 packet_bufX[i] = buf[i] * 127.0;
1255 } else if (jack_port_is_midi (porttype)) {
1256 // encode midi events from port to packet
1257 // convert the data buffer to a standard format (uint32_t based)
1258 unsigned int buffer_size_uint32 = net_period_up / 4;
1259 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1260 encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1262 packet_bufX = (packet_bufX + net_period_up);
1263 node = jack_slist_next (node);
1264 chn++;
1268 #if HAVE_CELT
1269 // render functions for celt.
1270 void
1271 render_payload_to_jack_ports_celt (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
1273 int chn = 0;
1274 JSList *node = capture_ports;
1275 JSList *src_node = capture_srcs;
1277 unsigned char *packet_bufX = (unsigned char *)packet_payload;
1279 while (node != NULL) {
1280 jack_port_t *port = (jack_port_t *) node->data;
1281 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1283 const char *porttype = jack_port_type (port);
1285 if (jack_port_is_audio (porttype)) {
1286 // audio port, decode celt data.
1287 CELTDecoder *decoder = src_node->data;
1288 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1289 if( !packet_payload )
1290 celt_decode_float( decoder, NULL, net_period_down, buf, nframes );
1291 else
1292 celt_decode_float( decoder, packet_bufX, net_period_down, buf, nframes );
1293 #else
1294 if( !packet_payload )
1295 celt_decode_float( decoder, NULL, net_period_down, buf );
1296 else
1297 celt_decode_float( decoder, packet_bufX, net_period_down, buf );
1298 #endif
1300 src_node = jack_slist_next (src_node);
1301 } else if (jack_port_is_midi (porttype)) {
1302 // midi port, decode midi events
1303 // convert the data buffer to a standard format (uint32_t based)
1304 unsigned int buffer_size_uint32 = net_period_down / 2;
1305 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1306 if( packet_payload )
1307 decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1309 packet_bufX = (packet_bufX + net_period_down);
1310 node = jack_slist_next (node);
1311 chn++;
1315 void
1316 render_jack_ports_to_payload_celt (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
1318 int chn = 0;
1319 JSList *node = playback_ports;
1320 JSList *src_node = playback_srcs;
1322 unsigned char *packet_bufX = (unsigned char *)packet_payload;
1324 while (node != NULL) {
1325 jack_port_t *port = (jack_port_t *) node->data;
1326 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1327 const char *porttype = jack_port_type (port);
1329 if (jack_port_is_audio (porttype)) {
1330 // audio port, encode celt data.
1332 int encoded_bytes;
1333 float *floatbuf = alloca (sizeof(float) * nframes );
1334 memcpy( floatbuf, buf, nframes * sizeof(float) );
1335 CELTEncoder *encoder = src_node->data;
1336 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
1337 encoded_bytes = celt_encode_float( encoder, floatbuf, nframes, packet_bufX, net_period_up );
1338 #else
1339 encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
1340 #endif
1341 if( encoded_bytes != net_period_up )
1342 printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
1343 src_node = jack_slist_next( src_node );
1344 } else if (jack_port_is_midi (porttype)) {
1345 // encode midi events from port to packet
1346 // convert the data buffer to a standard format (uint32_t based)
1347 unsigned int buffer_size_uint32 = net_period_up / 2;
1348 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1349 encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1351 packet_bufX = (packet_bufX + net_period_up);
1352 node = jack_slist_next (node);
1353 chn++;
1357 #endif
1359 #if HAVE_OPUS
1360 #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
1361 // render functions for Opus.
1362 void
1363 render_payload_to_jack_ports_opus (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
1365 int chn = 0;
1366 JSList *node = capture_ports;
1367 JSList *src_node = capture_srcs;
1369 unsigned char *packet_bufX = (unsigned char *)packet_payload;
1371 while (node != NULL) {
1372 jack_port_t *port = (jack_port_t *) node->data;
1373 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1375 const char *porttype = jack_port_type (port);
1377 if (jack_port_is_audio (porttype)) {
1378 // audio port, decode opus data.
1379 OpusCustomDecoder *decoder = (OpusCustomDecoder*) src_node->data;
1380 if( !packet_payload )
1381 memset(buf, 0, nframes * sizeof(float));
1382 else {
1383 unsigned short len;
1384 memcpy(&len, packet_bufX, CDO);
1385 len = ntohs(len);
1386 opus_custom_decode_float( decoder, packet_bufX + CDO, len, buf, nframes );
1389 src_node = jack_slist_next (src_node);
1390 } else if (jack_port_is_midi (porttype)) {
1391 // midi port, decode midi events
1392 // convert the data buffer to a standard format (uint32_t based)
1393 unsigned int buffer_size_uint32 = net_period_down / 2;
1394 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1395 if( packet_payload )
1396 decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1398 packet_bufX = (packet_bufX + net_period_down);
1399 node = jack_slist_next (node);
1400 chn++;
1404 void
1405 render_jack_ports_to_payload_opus (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
1407 int chn = 0;
1408 JSList *node = playback_ports;
1409 JSList *src_node = playback_srcs;
1411 unsigned char *packet_bufX = (unsigned char *)packet_payload;
1413 while (node != NULL) {
1414 jack_port_t *port = (jack_port_t *) node->data;
1415 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1416 const char *porttype = jack_port_type (port);
1418 if (jack_port_is_audio (porttype)) {
1419 // audio port, encode opus data.
1421 int encoded_bytes;
1422 float *floatbuf = alloca (sizeof(float) * nframes );
1423 memcpy( floatbuf, buf, nframes * sizeof(float) );
1424 OpusCustomEncoder *encoder = (OpusCustomEncoder*) src_node->data;
1425 encoded_bytes = opus_custom_encode_float( encoder, floatbuf, nframes, packet_bufX + CDO, net_period_up - CDO );
1426 unsigned short len = htons(encoded_bytes);
1427 memcpy(packet_bufX, &len, CDO);
1428 src_node = jack_slist_next( src_node );
1429 } else if (jack_port_is_midi (porttype)) {
1430 // encode midi events from port to packet
1431 // convert the data buffer to a standard format (uint32_t based)
1432 unsigned int buffer_size_uint32 = net_period_up / 2;
1433 uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
1434 encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
1436 packet_bufX = (packet_bufX + net_period_up);
1437 node = jack_slist_next (node);
1438 chn++;
1441 #endif
1443 /* Wrapper functions with bitdepth argument... */
1444 void
1445 render_payload_to_jack_ports (int bitdepth, void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes, int dont_htonl_floats)
1447 if (bitdepth == 8)
1448 render_payload_to_jack_ports_8bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
1449 else if (bitdepth == 16)
1450 render_payload_to_jack_ports_16bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
1451 #if HAVE_CELT
1452 else if (bitdepth == CELT_MODE)
1453 render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
1454 #endif
1455 #if HAVE_OPUS
1456 else if (bitdepth == OPUS_MODE)
1457 render_payload_to_jack_ports_opus (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
1458 #endif
1459 else
1460 render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes, dont_htonl_floats);
1463 void
1464 render_jack_ports_to_payload (int bitdepth, JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up, int dont_htonl_floats)
1466 if (bitdepth == 8)
1467 render_jack_ports_to_payload_8bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
1468 else if (bitdepth == 16)
1469 render_jack_ports_to_payload_16bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
1470 #if HAVE_CELT
1471 else if (bitdepth == CELT_MODE)
1472 render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
1473 #endif
1474 #if HAVE_OPUS
1475 else if (bitdepth == OPUS_MODE)
1476 render_jack_ports_to_payload_opus (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
1477 #endif
1478 else
1479 render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);