Correct netjack2 components help.
[jack2.git] / common / netjack_packet.c
blob6061db1f8620046df3ff353d673c2de8dbb4bcdc
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 #include "netjack_packet.h"
76 #include "JackError.h"
78 #ifdef NO_JACK_ERROR
79 #define jack_error printf
80 #endif
82 int fraggo = 0;
84 void
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);
103 void
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)
124 if (bitdepth == 8)
125 return sizeof (int8_t);
126 if (bitdepth == 16)
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.
148 packet_cache
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) )
155 fragment_number = 1;
156 else
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)");
162 return NULL;
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)");
173 return NULL;
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)");
186 return NULL;
189 pcache->mtu = mtu;
191 return pcache;
194 void
195 packet_cache_free (packet_cache *pcache)
197 int i;
198 if( pcache == NULL )
199 return;
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);
207 free (pcache);
210 cache_packet
211 *packet_cache_get_packet (packet_cache *pcache, jack_nframes_t framecnt)
213 int i;
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);
227 return retval;
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);
238 return retval;
241 // TODO: fix wrapping case... need to pass
242 // current expected frame here.
244 // or just save framecount into packet_cache.
246 cache_packet
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]);
251 int i;
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]);
260 return retval;
263 cache_packet
264 *packet_cache_get_free_packet (packet_cache *pcache)
266 int i;
268 for (i = 0; i < pcache->size; i++) {
269 if (pcache->packets[i].valid == 0)
270 return &(pcache->packets[i]);
273 return NULL;
276 void
277 cache_packet_reset (cache_packet *pack)
279 int i;
280 pack->valid = 0;
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;
289 void
290 cache_packet_set_framecnt (cache_packet *pack, jack_nframes_t framecnt)
292 int i;
294 pack->framecnt = framecnt;
296 for (i = 0; i < pack->num_fragments; i++)
297 pack->fragment_array[i] = 0;
299 pack->valid = 1;
302 void
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");
315 return;
318 if (fragment_nr == 0) {
319 memcpy (pack->packet_buf, packet_buf, rcv_len);
320 pack->fragment_array[0] = 1;
322 return;
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;
329 } else
330 jack_error ("too long packet received...");
335 cache_packet_is_complete (cache_packet *pack)
337 int i;
338 for (i = 0; i < pack->num_fragments; i++)
339 if (pack->fragment_array[i] == 0)
340 return 0;
342 return 1;
345 #ifndef WIN32
346 // new poll using nanoseconds resolution and
347 // not waiting forever.
349 netjack_poll_deadline (int sockfd, jack_time_t deadline)
351 struct pollfd fds;
352 int poll_err = 0;
353 #if HAVE_PPOLL
354 struct timespec timeout_spec = { 0, 0 };
355 #else
356 int timeout;
357 #endif
359 jack_time_t now = jack_get_time();
360 if( now >= deadline )
361 return 0;
363 if( (deadline - now) >= 1000000 ) {
364 jack_error( "deadline more than 1 second in the future, trimming it." );
365 deadline = now + 500000;
367 #if HAVE_PPOLL
368 timeout_spec.tv_nsec = (deadline - now) * 1000;
369 #else
370 timeout = lrintf( (float)(deadline - now) / 1000.0 );
371 #endif
373 fds.fd = sockfd;
374 fds.events = POLLIN;
376 #if HAVE_PPOLL
377 poll_err = ppoll (&fds, 1, &timeout_spec, NULL);
378 #else
379 poll_err = poll (&fds, 1, timeout);
380 #endif
382 if (poll_err == -1) {
383 switch (errno) {
384 case EBADF:
385 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
386 break;
387 case EFAULT:
388 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
389 break;
390 case EINTR:
391 jack_error ("Error %d: A signal occurred before any requested event", errno);
392 break;
393 case EINVAL:
394 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
395 break;
396 case ENOMEM:
397 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
398 break;
401 return poll_err;
405 netjack_poll (int sockfd, int timeout)
407 struct pollfd fds;
408 int i, poll_err = 0;
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);
429 fds.fd = sockfd;
430 fds.events = POLLIN;
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) {
439 switch (errno) {
440 case EBADF:
441 jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
442 break;
443 case EFAULT:
444 jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
445 break;
446 case EINTR:
447 jack_error ("Error %d: A signal occurred before any requested event", errno);
448 break;
449 case EINVAL:
450 jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
451 break;
452 case ENOMEM:
453 jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
454 break;
456 return 0;
458 return 1;
461 #else
463 netjack_poll (int sockfd, int timeout)
465 jack_error( "netjack_poll not implemented" );
466 return 0;
469 netjack_poll_deadline (int sockfd, jack_time_t deadline)
471 fd_set fds;
472 FD_ZERO( &fds );
473 FD_SET( sockfd, &fds );
475 struct timeval timeout;
476 while( 1 ) {
477 jack_time_t now = jack_get_time();
478 if( now >= deadline )
479 return 0;
481 int timeout_usecs = (deadline - now);
482 //jack_error( "timeout = %d", timeout_usecs );
483 timeout.tv_sec = 0;
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);
488 if( poll_err != 0 )
489 return poll_err;
492 return 0;
494 #endif
495 // This now reads all a socket has into the cache.
496 // replacing netjack_recv functions.
498 void
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;
503 int rcv_len;
504 jack_nframes_t framecnt;
505 cache_packet *cpack;
506 struct sockaddr_in sender_address;
507 #ifdef WIN32
508 int senderlen = sizeof( struct sockaddr_in );
509 u_long parm = 1;
510 ioctlsocket( sockfd, FIONBIO, &parm );
511 #else
512 int senderlen = sizeof( struct sockaddr_in );
513 #endif
514 while (1) {
515 #ifdef WIN32
516 rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, 0,
517 (struct sockaddr*) &sender_address, &senderlen);
518 #else
519 rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT,
520 (struct sockaddr*) &sender_address, &senderlen);
521 #endif
522 if (rcv_len < 0)
523 return;
525 if (pcache->master_address_valid) {
526 // Verify its from our master.
527 if (memcmp (&sender_address, &(pcache->master_address), senderlen) != 0)
528 continue;
529 } else {
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 ))
538 continue;
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();
546 void
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;
554 void
555 packet_cache_clear_old_packets (packet_cache *pcache, jack_nframes_t framecnt )
557 int i;
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 )
569 int i;
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]);
576 break;
580 if( cpack == NULL ) {
581 //printf( "retreive packet: %d....not found\n", framecnt );
582 return -1;
585 if( !cache_packet_is_complete( cpack ) ) {
586 return -1;
589 // ok. cpack is the one we want and its complete.
590 *packet_buf = cpack->packet_buf;
591 if( timestamp )
592 *timestamp = cpack->recv_timestamp;
594 pcache->last_framecnt_retreived_valid = 1;
595 pcache->last_framecnt_retreived = framecnt;
597 return pkt_size;
601 packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt )
603 int i;
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]);
610 break;
614 if( cpack == NULL ) {
615 //printf( "retreive packet: %d....not found\n", framecnt );
616 return -1;
619 if( !cache_packet_is_complete( cpack ) ) {
620 return -1;
623 cache_packet_reset (cpack);
624 packet_cache_clear_old_packets( pcache, framecnt );
626 return 0;
628 float
629 packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
631 int num_packets_before_us = 0;
632 int i;
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 )
648 int i;
649 jack_nframes_t best_offset = JACK_MAX_FRAMES / 2 - 1;
650 int retval = 0;
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" );
658 continue;
661 if( cpack->framecnt < expected_framecnt )
662 continue;
664 if( (cpack->framecnt - expected_framecnt) > best_offset ) {
665 continue;
668 best_offset = cpack->framecnt - expected_framecnt;
669 retval = 1;
671 if (best_offset == 0)
672 break;
674 if (retval && framecnt)
675 *framecnt = expected_framecnt + best_offset;
677 return retval;
681 packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt )
683 int i;
684 jack_nframes_t best_value = 0;
685 int retval = 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" );
693 continue;
696 if (cpack->framecnt < best_value) {
697 continue;
700 best_value = cpack->framecnt;
701 retval = 1;
704 if (retval && framecnt)
705 *framecnt = best_value;
707 return retval;
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 )
714 int i;
715 jack_nframes_t best_offset = 0;
716 int retval = 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" );
724 continue;
727 if ((cpack->framecnt - expected_framecnt) < best_offset) {
728 continue;
731 best_offset = cpack->framecnt - expected_framecnt;
732 retval = 1;
734 if( best_offset == 0 )
735 break;
737 if (retval && framecnt)
738 *framecnt = JACK_MAX_FRAMES - best_offset;
740 return retval;
742 // fragmented packet IO
743 void
744 netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
746 int frag_cnt = 0;
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) {
757 int err;
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);
761 if( err < 0 ) {
762 //printf( "error in send\n" );
763 perror( "send" );
765 } else {
766 int err;
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);
787 if( err < 0 ) {
788 //printf( "error in send\n" );
789 perror( "send" );
794 void
795 decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
797 int i;
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);
803 if (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;
813 } else
814 break; // no events can follow an empty event, we're done
818 void
819 encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
821 int i;
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
831 // otherwise drop it
832 if (written + payload_size < buffer_size_uint32 - 1) {
833 // write header
834 buffer_uint32[written] = htonl (payload_size);
835 written++;
836 buffer_uint32[written] = htonl (event.time);
837 written++;
838 buffer_uint32[written] = htonl (event.size);
839 written++;
841 // write data
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;
845 } else {
846 // buffer overflow
847 jack_error ("midi buffer overflow");
848 break;
851 // now put a netjack_midi 'no-payload' event, signaling EOF
852 buffer_uint32[written] = 0;
855 // render functions for float
856 void
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)
859 int chn = 0;
860 JSList *node = capture_ports;
861 #if HAVE_SAMPLERATE
862 JSList *src_node = capture_srcs;
863 #endif
865 uint32_t *packet_bufX = (uint32_t *)packet_payload;
867 if (!packet_payload)
868 return;
870 while (node != NULL) {
871 int i;
872 int_float_t val;
873 #if HAVE_SAMPLERATE
874 SRC_DATA src;
875 #endif
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)) {
883 #if HAVE_SAMPLERATE
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;
894 src.data_out = buf;
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);
903 } else
904 #endif
906 if( dont_htonl_floats ) {
907 memcpy( buf, packet_bufX, net_period_down * sizeof(jack_default_audio_sample_t));
908 } else {
909 for (i = 0; i < net_period_down; i++) {
910 val.i = packet_bufX[i];
911 val.i = ntohl (val.i);
912 buf[i] = val.f;
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);
925 chn++;
929 void
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 )
932 int chn = 0;
933 JSList *node = playback_ports;
934 #if HAVE_SAMPLERATE
935 JSList *src_node = playback_srcs;
936 #endif
938 uint32_t *packet_bufX = (uint32_t *) packet_payload;
940 while (node != NULL) {
941 #if HAVE_SAMPLERATE
942 SRC_DATA src;
943 #endif
944 int i;
945 int_float_t val;
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
954 #if HAVE_SAMPLERATE
955 if (net_period_up != nframes) {
956 SRC_STATE *src_state = src_node->data;
957 src.data_in = buf;
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);
973 } else
974 #endif
976 if( dont_htonl_floats ) {
977 memcpy( packet_bufX, buf, net_period_up * sizeof(jack_default_audio_sample_t) );
978 } else {
979 for (i = 0; i < net_period_up; i++) {
980 val.f = buf[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);
995 chn++;
999 // render functions for 16bit
1000 void
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)
1003 int chn = 0;
1004 JSList *node = capture_ports;
1005 #if HAVE_SAMPLERATE
1006 JSList *src_node = capture_srcs;
1007 #endif
1009 uint16_t *packet_bufX = (uint16_t *)packet_payload;
1011 if( !packet_payload )
1012 return;
1014 while (node != NULL) {
1015 int i;
1016 //uint32_t val;
1017 #if HAVE_SAMPLERATE
1018 SRC_DATA src;
1019 #endif
1021 jack_port_t *port = (jack_port_t *) node->data;
1022 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1024 #if HAVE_SAMPLERATE
1025 float *floatbuf = alloca (sizeof(float) * net_period_down);
1026 #endif
1027 const char *porttype = jack_port_type (port);
1029 if (jack_port_is_audio (porttype)) {
1030 // audio port, resample if necessary
1032 #if HAVE_SAMPLERATE
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;
1042 src.data_out = buf;
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);
1051 } else
1052 #endif
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);
1064 chn++;
1068 void
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)
1071 int chn = 0;
1072 JSList *node = playback_ports;
1073 #if HAVE_SAMPLERATE
1074 JSList *src_node = playback_srcs;
1075 #endif
1077 uint16_t *packet_bufX = (uint16_t *)packet_payload;
1079 while (node != NULL) {
1080 #if HAVE_SAMPLERATE
1081 SRC_DATA src;
1082 #endif
1083 int i;
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
1091 #if HAVE_SAMPLERATE
1092 if (net_period_up != nframes) {
1093 SRC_STATE *src_state = src_node->data;
1095 float *floatbuf = alloca (sizeof(float) * net_period_up);
1097 src.data_in = buf;
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);
1113 } else
1114 #endif
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);
1126 chn++;
1130 // render functions for 8bit
1131 void
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)
1134 int chn = 0;
1135 JSList *node = capture_ports;
1137 #if HAVE_SAMPLERATE
1138 JSList *src_node = capture_srcs;
1139 #endif
1141 int8_t *packet_bufX = (int8_t *)packet_payload;
1143 if (!packet_payload)
1144 return;
1146 while (node != NULL) {
1147 int i;
1148 //uint32_t val;
1149 #if HAVE_SAMPLERATE
1150 SRC_DATA src;
1151 #endif
1153 jack_port_t *port = (jack_port_t *) node->data;
1154 jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
1156 #if HAVE_SAMPLERATE
1157 float *floatbuf = alloca (sizeof (float) * net_period_down);
1158 #endif
1159 const char *porttype = jack_port_type (port);
1161 if (jack_port_is_audio(porttype)) {
1162 #if HAVE_SAMPLERATE
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;
1172 src.data_out = buf;
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);
1181 } else
1182 #endif
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);
1194 chn++;
1198 void
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)
1201 int chn = 0;
1202 JSList *node = playback_ports;
1203 #if HAVE_SAMPLERATE
1204 JSList *src_node = playback_srcs;
1205 #endif
1207 int8_t *packet_bufX = (int8_t *)packet_payload;
1209 while (node != NULL) {
1210 #if HAVE_SAMPLERATE
1211 SRC_DATA src;
1212 #endif
1213 int i;
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)) {
1220 #if HAVE_SAMPLERATE
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);
1228 src.data_in = buf;
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);
1243 } else
1244 #endif
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);
1256 chn++;
1260 #if HAVE_CELT
1261 // render functions for celt.
1262 void
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)
1265 int chn = 0;
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 );
1283 else
1284 celt_decode_float( decoder, packet_bufX, net_period_down, buf, nframes );
1285 #else
1286 if( !packet_payload )
1287 celt_decode_float( decoder, NULL, net_period_down, buf );
1288 else
1289 celt_decode_float( decoder, packet_bufX, net_period_down, buf );
1290 #endif
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);
1303 chn++;
1307 void
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)
1310 int chn = 0;
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.
1324 int encoded_bytes;
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 );
1330 #else
1331 encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
1332 #endif
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);
1345 chn++;
1349 #endif
1350 /* Wrapper functions with bitdepth argument... */
1351 void
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)
1354 if (bitdepth == 8)
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);
1358 #if HAVE_CELT
1359 else if (bitdepth == CELT_MODE)
1360 render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
1361 #endif
1362 else
1363 render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes, dont_htonl_floats);
1366 void
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)
1369 if (bitdepth == 8)
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);
1373 #if HAVE_CELT
1374 else if (bitdepth == CELT_MODE)
1375 render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
1376 #endif
1377 else
1378 render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);