meh... inverse retval...
[jack.git] / drivers / netjack / netjack.c
blobe22e7770bdd886e98314a2fe62f1020476c204a1
2 /* -*- mode: c; c-file-style: "linux"; -*- */
3 /*
4 NetJack Abstraction.
6 Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
7 Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
8 Copyright (C) 2003 Robert Ham <rah@bash.sh>
9 Copyright (C) 2001 Paul Davis
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.17 2006/04/16 20:16:10 torbenh Exp $
29 #include <math.h>
30 #include <stdio.h>
31 #include <memory.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <stdarg.h>
37 #include <jack/types.h>
38 // for jack_error in jack1
39 #include <jack/internal.h>
41 #include "jack/jslist.h"
43 #include <sys/types.h>
45 #ifdef WIN32
46 #include <winsock.h>
47 #include <malloc.h>
48 #else
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #endif
53 #include "netjack.h"
55 #include "config.h"
57 #if HAVE_SAMPLERATE
58 #include <samplerate.h>
59 #endif
61 #if HAVE_CELT
62 #include <celt/celt.h>
63 #endif
65 #include "netjack.h"
66 #include "netjack_packet.h"
68 // JACK2
69 //#include "jack/control.h"
71 #define MIN(x,y) ((x)<(y) ? (x) : (y))
73 static int sync_state = 1;
74 static jack_transport_state_t last_transport_state;
76 static int
77 net_driver_sync_cb(jack_transport_state_t state, jack_position_t *pos, void *data)
79 int retval = sync_state;
81 if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
82 retval = 0;
84 // if (state == JackTransportStarting)
85 // jack_info("Starting sync_state = %d", sync_state);
86 last_transport_state = state;
87 return retval;
90 int netjack_wait( netjack_driver_state_t *netj )
92 int we_have_the_expected_frame = 0;
93 jack_nframes_t next_frame_avail;
94 jack_time_t packet_recv_time_stamp;
95 jacknet_packet_header *pkthdr;
97 if( !netj->next_deadline_valid ) {
98 netj->next_deadline = jack_get_time() + netj->deadline_offset;
99 netj->next_deadline_valid = 1;
102 // Increment expected frame here.
104 if( netj->expected_framecnt_valid ) {
105 netj->expected_framecnt += 1;
106 } else {
107 // starting up.... lets look into the packetcache, and fetch the highest packet.
108 packet_cache_drain_socket( global_packcache, netj->sockfd );
109 if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail ) ) {
110 netj->expected_framecnt = next_frame_avail;
111 netj->expected_framecnt_valid = 1;
112 } else {
113 // no packets there... start normally.
114 netj->expected_framecnt = 0;
115 netj->expected_framecnt_valid = 1;
120 //jack_log( "expect %d", netj->expected_framecnt );
121 // Now check if required packet is already in the cache.
122 // then poll (have deadline calculated)
123 // then drain socket, rinse and repeat.
124 while(1) {
125 if( packet_cache_get_next_available_framecnt( global_packcache, netj->expected_framecnt, &next_frame_avail) ) {
126 if( next_frame_avail == netj->expected_framecnt ) {
127 we_have_the_expected_frame = 1;
128 if( !netj->always_deadline )
129 break;
132 if( ! netjack_poll_deadline( netj->sockfd, netj->next_deadline ) ) {
133 break;
136 packet_cache_drain_socket( global_packcache, netj->sockfd );
139 // check if we know who to send our packets too.
140 if (!netj->srcaddress_valid)
141 if( global_packcache->master_address_valid ) {
142 memcpy (&(netj->syncsource_address), &(global_packcache->master_address), sizeof( struct sockaddr_in ) );
143 netj->srcaddress_valid = 1;
146 // XXX: switching mode unconditionally is stupid.
147 // if we were running free perhaps we like to behave differently
148 // ie. fastforward one packet etc.
149 // well... this is the first packet we see. hmm.... dunno ;S
150 // it works... so...
151 netj->running_free = 0;
153 //if( !we_have_the_expected_frame )
154 // jack_error( "netxrun... %d", netj->expected_framecnt );
156 if( we_have_the_expected_frame ) {
158 jack_time_t now = jack_get_time();
159 if( now < netj->next_deadline )
160 netj->time_to_deadline = netj->next_deadline - now;
161 else
162 netj->time_to_deadline = 0;
164 packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize , &packet_recv_time_stamp);
165 pkthdr = (jacknet_packet_header *) netj->rx_buf;
166 packet_header_ntoh(pkthdr);
167 netj->deadline_goodness = (int)pkthdr->sync_state;
168 netj->packet_data_valid = 1;
170 int want_deadline;
171 if( netj->latency < 4 )
172 want_deadline = -netj->period_usecs/2;
173 else
174 want_deadline = (netj->period_usecs/4+10*(int)netj->period_usecs*netj->latency/100);
176 if( netj->deadline_goodness != MASTER_FREEWHEELS ) {
177 if( netj->deadline_goodness < want_deadline ) {
178 netj->deadline_offset -= netj->period_usecs/100;
179 //jack_log( "goodness: %d, Adjust deadline: --- %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
181 if( netj->deadline_goodness > want_deadline ) {
182 netj->deadline_offset += netj->period_usecs/100;
183 //jack_log( "goodness: %d, Adjust deadline: +++ %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
186 if( netj->deadline_offset < (netj->period_usecs*70/100) ) {
187 jack_error( "master is forcing deadline_offset to below 70%% of period_usecs... increase latency setting on master" );
188 netj->deadline_offset = (netj->period_usecs*90/100);
191 netj->next_deadline = jack_get_time() + netj->deadline_offset;
192 } else {
193 netj->time_to_deadline = 0;
194 netj->next_deadline = jack_get_time() + netj->deadline_offset;
195 // bah... the packet is not there.
196 // either
197 // - it got lost.
198 // - its late
199 // - sync source is not sending anymore.
201 // lets check if we have the next packets, we will just run a cycle without data.
202 // in that case.
204 if( packet_cache_get_next_available_framecnt( global_packcache, netj->expected_framecnt, &next_frame_avail) )
206 jack_nframes_t offset = next_frame_avail - netj->expected_framecnt;
208 //XXX: hmm... i need to remember why resync_threshold wasnt right.
209 //if( offset < netj->resync_threshold )
210 if( offset < 10 ) {
211 // ok. dont do nothing. we will run without data.
212 // this seems to be one or 2 lost packets.
214 // this can also be reordered packet jitter.
215 // (maybe this is not happening in real live)
216 // but it happens in netem.
218 netj->packet_data_valid = 0;
220 // I also found this happening, when the packet queue, is too full.
221 // but wtf ? use a smaller latency. this link can handle that ;S
222 if( packet_cache_get_fill( global_packcache, netj->expected_framecnt ) > 80.0 )
223 netj->next_deadline -= netj->period_usecs/2;
226 } else {
227 // the diff is too high. but we have a packet in the future.
228 // lets resync.
229 netj->expected_framecnt = next_frame_avail;
230 packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
231 pkthdr = (jacknet_packet_header *) netj->rx_buf;
232 packet_header_ntoh(pkthdr);
233 //netj->deadline_goodness = 0;
234 netj->deadline_goodness = (int)pkthdr->sync_state - (int)netj->period_usecs * offset;
235 netj->next_deadline_valid = 0;
236 netj->packet_data_valid = 1;
239 } else {
240 // no packets in buffer.
241 netj->packet_data_valid = 0;
243 //printf( "frame %d No Packet in queue. num_lost_packets = %d \n", netj->expected_framecnt, netj->num_lost_packets );
244 if( netj->num_lost_packets < 5 ) {
245 // ok. No Packet in queue. The packet was either lost,
246 // or we are running too fast.
248 // Adjusting the deadline unconditionally resulted in
249 // too many xruns on master.
250 // But we need to adjust for the case we are running too fast.
251 // So lets check if the last packet is there now.
253 // It would not be in the queue anymore, if it had been
254 // retrieved. This might break for redundancy, but
255 // i will make the packet cache drop redundant packets,
256 // that have already been retreived.
258 if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail) ) {
259 if( next_frame_avail == (netj->expected_framecnt - 1) ) {
260 // Ok. the last packet is there now.
261 // and it had not been retrieved.
263 // TODO: We are still dropping 2 packets.
264 // perhaps we can adjust the deadline
265 // when (num_packets lost == 0)
267 // This might still be too much.
268 netj->next_deadline += netj->period_usecs;
271 } else if( (netj->num_lost_packets <= 100) ) {
272 // lets try adjusting the deadline harder, for some packets, we might have just ran 2 fast.
273 netj->next_deadline += netj->period_usecs*netj->latency/8;
274 } else {
276 // But now we can check for any new frame available.
278 if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail) ) {
279 netj->expected_framecnt = next_frame_avail;
280 packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
281 pkthdr = (jacknet_packet_header *) netj->rx_buf;
282 packet_header_ntoh(pkthdr);
283 netj->deadline_goodness = pkthdr->sync_state;
284 netj->next_deadline_valid = 0;
285 netj->packet_data_valid = 1;
286 netj->running_free = 0;
287 jack_info( "resync after freerun... %d\n", netj->expected_framecnt );
288 } else {
289 if( netj->num_lost_packets == 101 ) {
290 jack_info( "master seems gone... entering freerun mode\n", netj->expected_framecnt );
293 netj->running_free = 1;
295 // when we really dont see packets.
296 // reset source address. and open possibility for new master.
297 // maybe dsl reconnect. Also restart of netsource without fix
298 // reply address changes port.
299 if (netj->num_lost_packets > 200 ) {
300 netj->srcaddress_valid = 0;
301 packet_cache_reset_master_address( global_packcache );
308 int retval = 0;
310 if( !netj->packet_data_valid ) {
311 netj->num_lost_packets += 1;
312 if( netj->num_lost_packets == 1 )
313 retval = netj->period_usecs;
314 } else {
315 if( (netj->num_lost_packets>1) && !netj->running_free )
316 retval = (netj->num_lost_packets-1) * netj->period_usecs;
318 netj->num_lost_packets = 0;
321 return retval;
324 void netjack_send_silence( netjack_driver_state_t *netj, int syncstate )
326 int tx_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up + sizeof(jacknet_packet_header);
327 unsigned int *packet_buf, *packet_bufX;
329 packet_buf = alloca( tx_size);
330 jacknet_packet_header *tx_pkthdr = (jacknet_packet_header *)packet_buf;
331 jacknet_packet_header *rx_pkthdr = (jacknet_packet_header *)netj->rx_buf;
333 //framecnt = rx_pkthdr->framecnt;
335 netj->reply_port = rx_pkthdr->reply_port;
337 // offset packet_bufX by the packetheader.
338 packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
340 tx_pkthdr->sync_state = syncstate;
341 tx_pkthdr->framecnt = netj->expected_framecnt;
343 // memset 0 the payload.
344 int payload_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up;
345 memset(packet_bufX, 0, payload_size);
347 packet_header_hton(tx_pkthdr);
348 if (netj->srcaddress_valid)
350 int r;
351 if (netj->reply_port)
352 netj->syncsource_address.sin_port = htons(netj->reply_port);
354 for( r=0; r<netj->redundancy; r++ )
355 netjack_sendto(netj->outsockfd, (char *)packet_buf, tx_size,
356 0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
361 void netjack_attach( netjack_driver_state_t *netj )
363 //puts ("net_driver_attach");
364 jack_port_t * port;
365 char buf[32];
366 unsigned int chn;
367 int port_flags;
370 if (netj->handle_transport_sync)
371 jack_set_sync_callback(netj->client, (JackSyncCallback) net_driver_sync_cb, NULL);
373 port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
375 for (chn = 0; chn < netj->capture_channels_audio; chn++) {
376 snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
378 port = jack_port_register (netj->client, buf,
379 JACK_DEFAULT_AUDIO_TYPE,
380 port_flags, 0);
381 if (!port) {
382 jack_error ("NET: cannot register port for %s", buf);
383 break;
386 netj->capture_ports =
387 jack_slist_append (netj->capture_ports, port);
389 if( netj->bitdepth == CELT_MODE ) {
390 #if HAVE_CELT
391 #if HAVE_CELT_API_0_7
392 celt_int32 lookahead;
393 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
394 netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( celt_mode, 1, NULL ) );
395 #else
396 celt_int32_t lookahead;
397 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
398 netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( celt_mode ) );
399 #endif
400 celt_mode_info( celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
401 netj->codec_latency = 2*lookahead;
402 #endif
403 } else {
404 #if HAVE_SAMPLERATE
405 netj->capture_srcs = jack_slist_append(netj->capture_srcs, src_new(SRC_LINEAR, 1, NULL));
406 #endif
409 for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
410 snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
412 port = jack_port_register (netj->client, buf,
413 JACK_DEFAULT_MIDI_TYPE,
414 port_flags, 0);
415 if (!port) {
416 jack_error ("NET: cannot register port for %s", buf);
417 break;
420 netj->capture_ports =
421 jack_slist_append (netj->capture_ports, port);
424 port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
426 for (chn = 0; chn < netj->playback_channels_audio; chn++) {
427 snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
429 port = jack_port_register (netj->client, buf,
430 JACK_DEFAULT_AUDIO_TYPE,
431 port_flags, 0);
433 if (!port) {
434 jack_error ("NET: cannot register port for %s", buf);
435 break;
438 netj->playback_ports =
439 jack_slist_append (netj->playback_ports, port);
440 if( netj->bitdepth == CELT_MODE ) {
441 #if HAVE_CELT
442 #if HAVE_CELT_API_0_7
443 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
444 netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
445 #else
446 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
447 netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode ) );
448 #endif
449 #endif
450 } else {
451 #if HAVE_SAMPLERATE
452 netj->playback_srcs = jack_slist_append(netj->playback_srcs, src_new(SRC_LINEAR, 1, NULL));
453 #endif
456 for (chn = netj->playback_channels_audio; chn < netj->playback_channels; chn++) {
457 snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
459 port = jack_port_register (netj->client, buf,
460 JACK_DEFAULT_MIDI_TYPE,
461 port_flags, 0);
463 if (!port) {
464 jack_error ("NET: cannot register port for %s", buf);
465 break;
468 netj->playback_ports =
469 jack_slist_append (netj->playback_ports, port);
472 jack_activate (netj->client);
476 void netjack_detach( netjack_driver_state_t *netj )
478 JSList * node;
481 for (node = netj->capture_ports; node; node = jack_slist_next (node))
482 jack_port_unregister (netj->client,
483 ((jack_port_t *) node->data));
485 jack_slist_free (netj->capture_ports);
486 netj->capture_ports = NULL;
488 for (node = netj->playback_ports; node; node = jack_slist_next (node))
489 jack_port_unregister (netj->client,
490 ((jack_port_t *) node->data));
492 jack_slist_free (netj->playback_ports);
493 netj->playback_ports = NULL;
497 netjack_driver_state_t *netjack_init (netjack_driver_state_t *netj,
498 jack_client_t * client,
499 const char *name,
500 unsigned int capture_ports,
501 unsigned int playback_ports,
502 unsigned int capture_ports_midi,
503 unsigned int playback_ports_midi,
504 jack_nframes_t sample_rate,
505 jack_nframes_t period_size,
506 unsigned int listen_port,
507 unsigned int transport_sync,
508 unsigned int resample_factor,
509 unsigned int resample_factor_up,
510 unsigned int bitdepth,
511 unsigned int use_autoconfig,
512 unsigned int latency,
513 unsigned int redundancy,
514 int dont_htonl_floats,
515 int always_deadline)
518 // Fill in netj values.
519 // might be subject to autoconfig...
520 // so dont calculate anything with them...
523 netj->sample_rate = sample_rate;
524 netj->period_size = period_size;
525 netj->dont_htonl_floats = dont_htonl_floats;
527 netj->listen_port = listen_port;
529 netj->capture_channels = capture_ports + capture_ports_midi;
530 netj->capture_channels_audio = capture_ports;
531 netj->capture_channels_midi = capture_ports_midi;
532 netj->capture_ports = NULL;
533 netj->playback_channels = playback_ports + playback_ports_midi;
534 netj->playback_channels_audio = playback_ports;
535 netj->playback_channels_midi = playback_ports_midi;
536 netj->playback_ports = NULL;
537 netj->codec_latency = 0;
539 netj->handle_transport_sync = transport_sync;
540 netj->mtu = 1400;
541 netj->latency = latency;
542 netj->redundancy = redundancy;
543 netj->use_autoconfig = use_autoconfig;
544 netj->always_deadline = always_deadline;
547 netj->client = client;
550 if ((bitdepth != 0) && (bitdepth != 8) && (bitdepth != 16) && (bitdepth != CELT_MODE))
552 jack_info ("Invalid bitdepth: %d (8, 16 or 0 for float) !!!", bitdepth);
553 return NULL;
555 netj->bitdepth = bitdepth;
558 if (resample_factor_up == 0)
559 resample_factor_up = resample_factor;
561 netj->resample_factor = resample_factor;
562 netj->resample_factor_up = resample_factor_up;
565 return netj;
568 void netjack_release( netjack_driver_state_t *netj )
570 close( netj->sockfd );
571 close( netj->outsockfd );
573 packet_cache_free( global_packcache );
574 global_packcache = NULL;
578 netjack_startup( netjack_driver_state_t *netj )
580 int first_pack_len;
581 struct sockaddr_in address;
582 // Now open the socket, and wait for the first packet to arrive...
583 netj->sockfd = socket (AF_INET, SOCK_DGRAM, 0);
584 #ifdef WIN32
585 if (netj->sockfd == INVALID_SOCKET)
586 #else
587 if (netj->sockfd == -1)
588 #endif
590 jack_info ("socket error");
591 return -1;
593 address.sin_family = AF_INET;
594 address.sin_port = htons(netj->listen_port);
595 address.sin_addr.s_addr = htonl(INADDR_ANY);
596 if (bind (netj->sockfd, (struct sockaddr *) &address, sizeof (address)) < 0)
598 jack_info("bind error");
599 return -1;
602 netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
603 #ifdef WIN32
604 if (netj->outsockfd == INVALID_SOCKET)
605 #else
606 if (netj->outsockfd == -1)
607 #endif
609 jack_info ("socket error");
610 return -1;
612 netj->srcaddress_valid = 0;
613 if (netj->use_autoconfig)
615 jacknet_packet_header *first_packet = alloca (sizeof (jacknet_packet_header));
616 #ifdef WIN32
617 int address_size = sizeof( struct sockaddr_in );
618 #else
619 socklen_t address_size = sizeof (struct sockaddr_in);
620 #endif
621 //jack_info ("Waiting for an incoming packet !!!");
622 //jack_info ("*** IMPORTANT *** Dont connect a client to jackd until the driver is attached to a clock source !!!");
624 while(1) {
625 if( ! netjack_poll( netj->sockfd, 1000 ) ) {
626 jack_info ("Waiting aborted");
627 return -1;
629 first_pack_len = recvfrom (netj->sockfd, (char *)first_packet, sizeof (jacknet_packet_header), 0, (struct sockaddr*) & netj->syncsource_address, &address_size);
630 #ifdef WIN32
631 if( first_pack_len == -1 ) {
632 first_pack_len = sizeof(jacknet_packet_header);
633 break;
635 #else
636 if (first_pack_len == sizeof (jacknet_packet_header))
637 break;
638 #endif
640 netj->srcaddress_valid = 1;
642 if (first_pack_len == sizeof (jacknet_packet_header))
644 packet_header_ntoh (first_packet);
646 jack_info ("AutoConfig Override !!!");
647 if (netj->sample_rate != first_packet->sample_rate)
649 jack_info ("AutoConfig Override: Master JACK sample rate = %d", first_packet->sample_rate);
650 netj->sample_rate = first_packet->sample_rate;
653 if (netj->period_size != first_packet->period_size)
655 jack_info ("AutoConfig Override: Master JACK period size is %d", first_packet->period_size);
656 netj->period_size = first_packet->period_size;
658 if (netj->capture_channels_audio != first_packet->capture_channels_audio)
660 jack_info ("AutoConfig Override: capture_channels_audio = %d", first_packet->capture_channels_audio);
661 netj->capture_channels_audio = first_packet->capture_channels_audio;
663 if (netj->capture_channels_midi != first_packet->capture_channels_midi)
665 jack_info ("AutoConfig Override: capture_channels_midi = %d", first_packet->capture_channels_midi);
666 netj->capture_channels_midi = first_packet->capture_channels_midi;
668 if (netj->playback_channels_audio != first_packet->playback_channels_audio)
670 jack_info ("AutoConfig Override: playback_channels_audio = %d", first_packet->playback_channels_audio);
671 netj->playback_channels_audio = first_packet->playback_channels_audio;
673 if (netj->playback_channels_midi != first_packet->playback_channels_midi)
675 jack_info ("AutoConfig Override: playback_channels_midi = %d", first_packet->playback_channels_midi);
676 netj->playback_channels_midi = first_packet->playback_channels_midi;
679 netj->mtu = first_packet->mtu;
680 jack_info ("MTU is set to %d bytes", first_packet->mtu);
681 netj->latency = first_packet->latency;
684 netj->capture_channels = netj->capture_channels_audio + netj->capture_channels_midi;
685 netj->playback_channels = netj->playback_channels_audio + netj->playback_channels_midi;
687 // After possible Autoconfig: do all calculations...
688 netj->period_usecs =
689 (jack_time_t) floor ((((float) netj->period_size) / (float)netj->sample_rate)
690 * 1000000.0f);
692 if( netj->latency == 0 )
693 netj->deadline_offset = 50*netj->period_usecs;
694 else
695 netj->deadline_offset = netj->period_usecs + 10*netj->latency*netj->period_usecs/100;
697 if( netj->bitdepth == CELT_MODE ) {
698 // celt mode.
699 // TODO: this is a hack. But i dont want to change the packet header.
700 netj->net_period_down = netj->resample_factor;
701 netj->net_period_up = netj->resample_factor_up;
702 } else {
703 netj->net_period_down = (float) netj->period_size / (float) netj->resample_factor;
704 netj->net_period_up = (float) netj->period_size / (float) netj->resample_factor_up;
707 netj->rx_bufsize = sizeof (jacknet_packet_header) + netj->net_period_down * netj->capture_channels * get_sample_size (netj->bitdepth);
708 netj->pkt_buf = malloc (netj->rx_bufsize);
709 global_packcache = packet_cache_new (netj->latency + 50, netj->rx_bufsize, netj->mtu);
711 netj->expected_framecnt_valid = 0;
712 netj->num_lost_packets = 0;
713 netj->next_deadline_valid = 0;
714 netj->deadline_goodness = 0;
715 netj->time_to_deadline = 0;
717 // Special handling for latency=0
718 if( netj->latency == 0 )
719 netj->resync_threshold = 0;
720 else
721 netj->resync_threshold = MIN( 15, netj->latency-1 );
723 netj->running_free = 0;
725 return 0;