JackWeakAPI now working.
[jack2.git] / common / netjack.c
blobba0cd240be8573ffb126056d21c70efeab26fb13
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 #include "jack/jslist.h"
40 #include <sys/types.h>
42 #ifdef WIN32
43 #include <winsock.h>
44 #include <malloc.h>
45 #else
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #endif
50 #include "netjack.h"
52 //#include "config.h"
54 #if HAVE_SAMPLERATE
55 #include <samplerate.h>
56 #endif
58 #if HAVE_CELT
59 #include <celt/celt.h>
60 #endif
62 #include "netjack.h"
63 #include "netjack_packet.h"
65 // JACK2
66 #include "jack/control.h"
68 #define MIN(x,y) ((x)<(y) ? (x) : (y))
70 static int sync_state = 1;
71 static jack_transport_state_t last_transport_state;
73 static int
74 net_driver_sync_cb(jack_transport_state_t state, jack_position_t *pos, void *data)
76 int retval = sync_state;
78 if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
79 retval = 0;
81 // if (state == JackTransportStarting)
82 // jack_info("Starting sync_state = %d", sync_state);
83 last_transport_state = state;
84 return retval;
87 int netjack_wait( netjack_driver_state_t *netj )
89 int we_have_the_expected_frame = 0;
90 jack_nframes_t next_frame_avail;
91 jack_time_t packet_recv_time_stamp;
92 jacknet_packet_header *pkthdr;
94 if( !netj->next_deadline_valid ) {
95 netj->next_deadline = jack_get_time() + netj->deadline_offset;
96 netj->next_deadline_valid = 1;
99 // Increment expected frame here.
101 if( netj->expected_framecnt_valid ) {
102 netj->expected_framecnt += 1;
103 } else {
104 // starting up.... lets look into the packetcache, and fetch the highest packet.
105 packet_cache_drain_socket( global_packcache, netj->sockfd );
106 if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail ) ) {
107 netj->expected_framecnt = next_frame_avail;
108 netj->expected_framecnt_valid = 1;
109 } else {
110 // no packets there... start normally.
111 netj->expected_framecnt = 0;
112 netj->expected_framecnt_valid = 1;
117 //jack_log( "expect %d", netj->expected_framecnt );
118 // Now check if required packet is already in the cache.
119 // then poll (have deadline calculated)
120 // then drain socket, rinse and repeat.
121 while(1) {
122 if( packet_cache_get_next_available_framecnt( global_packcache, netj->expected_framecnt, &next_frame_avail) ) {
123 if( next_frame_avail == netj->expected_framecnt ) {
124 we_have_the_expected_frame = 1;
125 if( !netj->always_deadline )
126 break;
129 if( ! netjack_poll_deadline( netj->sockfd, netj->next_deadline ) ) {
130 break;
133 packet_cache_drain_socket( global_packcache, netj->sockfd );
136 // check if we know who to send our packets too.
137 if (!netj->srcaddress_valid)
138 if( global_packcache->master_address_valid ) {
139 memcpy (&(netj->syncsource_address), &(global_packcache->master_address), sizeof( struct sockaddr_in ) );
140 netj->srcaddress_valid = 1;
143 // XXX: switching mode unconditionally is stupid.
144 // if we were running free perhaps we like to behave differently
145 // ie. fastforward one packet etc.
146 // well... this is the first packet we see. hmm.... dunno ;S
147 // it works... so...
148 netj->running_free = 0;
150 //if( !we_have_the_expected_frame )
151 // jack_error( "netxrun... %d", netj->expected_framecnt );
153 if( we_have_the_expected_frame ) {
154 netj->time_to_deadline = netj->next_deadline - jack_get_time() - netj->period_usecs;
155 packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize , &packet_recv_time_stamp);
156 pkthdr = (jacknet_packet_header *) netj->rx_buf;
157 packet_header_ntoh(pkthdr);
158 netj->deadline_goodness = (int)pkthdr->sync_state;
159 netj->packet_data_valid = 1;
161 if( netj->deadline_goodness != MASTER_FREEWHEELS ) {
162 if( netj->deadline_goodness < (netj->period_usecs/4+10*(int)netj->period_usecs*netj->latency/100) ) {
163 netj->deadline_offset -= netj->period_usecs/100;
164 //jack_log( "goodness: %d, Adjust deadline: --- %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
166 if( netj->deadline_goodness > (netj->period_usecs/4+10*(int)netj->period_usecs*netj->latency/100) ) {
167 netj->deadline_offset += netj->period_usecs/100;
168 //jack_log( "goodness: %d, Adjust deadline: +++ %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
171 if( netj->deadline_offset < (netj->period_usecs*70/100) ) {
172 jack_error( "master is forcing deadline_offset to below 70%% of period_usecs... increase latency setting on master" );
173 netj->deadline_offset = (netj->period_usecs*90/100);
176 netj->next_deadline = jack_get_time() + netj->deadline_offset;
177 } else {
178 netj->time_to_deadline = 0;
179 netj->next_deadline = jack_get_time() + netj->deadline_offset;
180 // bah... the packet is not there.
181 // either
182 // - it got lost.
183 // - its late
184 // - sync source is not sending anymore.
186 // lets check if we have the next packets, we will just run a cycle without data.
187 // in that case.
189 if( packet_cache_get_next_available_framecnt( global_packcache, netj->expected_framecnt, &next_frame_avail) )
191 jack_nframes_t offset = next_frame_avail - netj->expected_framecnt;
193 //XXX: hmm... i need to remember why resync_threshold wasnt right.
194 //if( offset < netj->resync_threshold )
195 if( offset < 10 ) {
196 // ok. dont do nothing. we will run without data.
197 // this seems to be one or 2 lost packets.
199 // this can also be reordered packet jitter.
200 // (maybe this is not happening in real live)
201 // but it happens in netem.
203 netj->packet_data_valid = 0;
205 // I also found this happening, when the packet queue, is too full.
206 // but wtf ? use a smaller latency. this link can handle that ;S
207 if( packet_cache_get_fill( global_packcache, netj->expected_framecnt ) > 80.0 )
208 netj->next_deadline -= netj->period_usecs/2;
211 } else {
212 // the diff is too high. but we have a packet in the future.
213 // lets resync.
214 netj->expected_framecnt = next_frame_avail;
215 packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
216 pkthdr = (jacknet_packet_header *) netj->rx_buf;
217 packet_header_ntoh(pkthdr);
218 //netj->deadline_goodness = 0;
219 netj->deadline_goodness = (int)pkthdr->sync_state - (int)netj->period_usecs * offset;
220 netj->next_deadline_valid = 0;
221 netj->packet_data_valid = 1;
224 } else {
225 // no packets in buffer.
226 netj->packet_data_valid = 0;
228 //printf( "frame %d No Packet in queue. num_lost_packets = %d \n", netj->expected_framecnt, netj->num_lost_packets );
229 if( netj->num_lost_packets < 5 ) {
230 // ok. No Packet in queue. The packet was either lost,
231 // or we are running too fast.
233 // Adjusting the deadline unconditionally resulted in
234 // too many xruns on master.
235 // But we need to adjust for the case we are running too fast.
236 // So lets check if the last packet is there now.
238 // It would not be in the queue anymore, if it had been
239 // retrieved. This might break for redundancy, but
240 // i will make the packet cache drop redundant packets,
241 // that have already been retreived.
243 if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail) ) {
244 if( next_frame_avail == (netj->expected_framecnt - 1) ) {
245 // Ok. the last packet is there now.
246 // and it had not been retrieved.
248 // TODO: We are still dropping 2 packets.
249 // perhaps we can adjust the deadline
250 // when (num_packets lost == 0)
252 // This might still be too much.
253 netj->next_deadline += netj->period_usecs;
256 } else if( (netj->num_lost_packets <= 100) ) {
257 // lets try adjusting the deadline harder, for some packets, we might have just ran 2 fast.
258 netj->next_deadline += netj->period_usecs*netj->latency/8;
259 } else {
261 // But now we can check for any new frame available.
263 if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail) ) {
264 netj->expected_framecnt = next_frame_avail;
265 packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
266 pkthdr = (jacknet_packet_header *) netj->rx_buf;
267 packet_header_ntoh(pkthdr);
268 netj->deadline_goodness = pkthdr->sync_state;
269 netj->next_deadline_valid = 0;
270 netj->packet_data_valid = 1;
271 netj->running_free = 0;
272 jack_info( "resync after freerun... %d\n", netj->expected_framecnt );
273 } else {
274 if( netj->num_lost_packets == 101 ) {
275 jack_info( "master seems gone... entering freerun mode\n", netj->expected_framecnt );
278 netj->running_free = 1;
280 // when we really dont see packets.
281 // reset source address. and open possibility for new master.
282 // maybe dsl reconnect. Also restart of netsource without fix
283 // reply address changes port.
284 if (netj->num_lost_packets > 200 ) {
285 netj->srcaddress_valid = 0;
286 packet_cache_reset_master_address( global_packcache );
293 int retval = 0;
295 if( !netj->packet_data_valid ) {
296 netj->num_lost_packets += 1;
297 if( netj->num_lost_packets == 1 )
298 retval = netj->period_usecs;
299 } else {
300 if( (netj->num_lost_packets>1) && !netj->running_free )
301 retval = (netj->num_lost_packets-1) * netj->period_usecs;
303 netj->num_lost_packets = 0;
306 return retval;
309 void netjack_send_silence( netjack_driver_state_t *netj, int syncstate )
311 int tx_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up + sizeof(jacknet_packet_header);
312 unsigned int *packet_buf, *packet_bufX;
314 packet_buf = alloca( tx_size);
315 jacknet_packet_header *tx_pkthdr = (jacknet_packet_header *)packet_buf;
316 jacknet_packet_header *rx_pkthdr = (jacknet_packet_header *)netj->rx_buf;
318 //framecnt = rx_pkthdr->framecnt;
320 netj->reply_port = rx_pkthdr->reply_port;
322 // offset packet_bufX by the packetheader.
323 packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
325 tx_pkthdr->sync_state = syncstate;
326 tx_pkthdr->framecnt = netj->expected_framecnt;
328 // memset 0 the payload.
329 int payload_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up;
330 memset(packet_bufX, 0, payload_size);
332 packet_header_hton(tx_pkthdr);
333 if (netj->srcaddress_valid)
335 int r;
336 if (netj->reply_port)
337 netj->syncsource_address.sin_port = htons(netj->reply_port);
339 for( r=0; r<netj->redundancy; r++ )
340 netjack_sendto(netj->outsockfd, (char *)packet_buf, tx_size,
341 0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
346 void netjack_attach( netjack_driver_state_t *netj )
348 //puts ("net_driver_attach");
349 jack_port_t * port;
350 char buf[32];
351 unsigned int chn;
352 int port_flags;
355 if (netj->handle_transport_sync)
356 jack_set_sync_callback(netj->client, (JackSyncCallback) net_driver_sync_cb, NULL);
358 port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
360 for (chn = 0; chn < netj->capture_channels_audio; chn++) {
361 snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
363 port = jack_port_register (netj->client, buf,
364 JACK_DEFAULT_AUDIO_TYPE,
365 port_flags, 0);
366 if (!port) {
367 jack_error ("NET: cannot register port for %s", buf);
368 break;
371 netj->capture_ports =
372 jack_slist_append (netj->capture_ports, port);
374 if( netj->bitdepth == CELT_MODE ) {
375 #if HAVE_CELT
376 #if HAVE_CELT_API_0_7
377 celt_int32 lookahead;
378 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
379 netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( celt_mode, 1, NULL ) );
380 #else
381 celt_int32_t lookahead;
382 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
383 netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( celt_mode ) );
384 #endif
385 celt_mode_info( celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
386 netj->codec_latency = 2*lookahead;
387 #endif
388 } else {
389 #if HAVE_SAMPLERATE
390 netj->capture_srcs = jack_slist_append(netj->capture_srcs, src_new(SRC_LINEAR, 1, NULL));
391 #endif
394 for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
395 snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
397 port = jack_port_register (netj->client, buf,
398 JACK_DEFAULT_MIDI_TYPE,
399 port_flags, 0);
400 if (!port) {
401 jack_error ("NET: cannot register port for %s", buf);
402 break;
405 netj->capture_ports =
406 jack_slist_append (netj->capture_ports, port);
409 port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
411 for (chn = 0; chn < netj->playback_channels_audio; chn++) {
412 snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
414 port = jack_port_register (netj->client, buf,
415 JACK_DEFAULT_AUDIO_TYPE,
416 port_flags, 0);
418 if (!port) {
419 jack_error ("NET: cannot register port for %s", buf);
420 break;
423 netj->playback_ports =
424 jack_slist_append (netj->playback_ports, port);
425 if( netj->bitdepth == CELT_MODE ) {
426 #if HAVE_CELT
427 #if HAVE_CELT_API_0_7
428 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
429 netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
430 #else
431 CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
432 netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode ) );
433 #endif
434 #endif
435 } else {
436 #if HAVE_SAMPLERATE
437 netj->playback_srcs = jack_slist_append(netj->playback_srcs, src_new(SRC_LINEAR, 1, NULL));
438 #endif
441 for (chn = netj->playback_channels_audio; chn < netj->playback_channels; chn++) {
442 snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
444 port = jack_port_register (netj->client, buf,
445 JACK_DEFAULT_MIDI_TYPE,
446 port_flags, 0);
448 if (!port) {
449 jack_error ("NET: cannot register port for %s", buf);
450 break;
453 netj->playback_ports =
454 jack_slist_append (netj->playback_ports, port);
457 jack_activate (netj->client);
461 void netjack_detach( netjack_driver_state_t *netj )
463 JSList * node;
466 for (node = netj->capture_ports; node; node = jack_slist_next (node))
467 jack_port_unregister (netj->client,
468 ((jack_port_t *) node->data));
470 jack_slist_free (netj->capture_ports);
471 netj->capture_ports = NULL;
473 for (node = netj->playback_ports; node; node = jack_slist_next (node))
474 jack_port_unregister (netj->client,
475 ((jack_port_t *) node->data));
477 jack_slist_free (netj->playback_ports);
478 netj->playback_ports = NULL;
482 netjack_driver_state_t *netjack_init (netjack_driver_state_t *netj,
483 jack_client_t * client,
484 const char *name,
485 unsigned int capture_ports,
486 unsigned int playback_ports,
487 unsigned int capture_ports_midi,
488 unsigned int playback_ports_midi,
489 jack_nframes_t sample_rate,
490 jack_nframes_t period_size,
491 unsigned int listen_port,
492 unsigned int transport_sync,
493 unsigned int resample_factor,
494 unsigned int resample_factor_up,
495 unsigned int bitdepth,
496 unsigned int use_autoconfig,
497 unsigned int latency,
498 unsigned int redundancy,
499 int dont_htonl_floats,
500 int always_deadline)
503 // Fill in netj values.
504 // might be subject to autoconfig...
505 // so dont calculate anything with them...
508 netj->sample_rate = sample_rate;
509 netj->period_size = period_size;
510 netj->dont_htonl_floats = dont_htonl_floats;
512 netj->listen_port = listen_port;
514 netj->capture_channels = capture_ports + capture_ports_midi;
515 netj->capture_channels_audio = capture_ports;
516 netj->capture_channels_midi = capture_ports_midi;
517 netj->capture_ports = NULL;
518 netj->playback_channels = playback_ports + playback_ports_midi;
519 netj->playback_channels_audio = playback_ports;
520 netj->playback_channels_midi = playback_ports_midi;
521 netj->playback_ports = NULL;
522 netj->codec_latency = 0;
524 netj->handle_transport_sync = transport_sync;
525 netj->mtu = 1400;
526 netj->latency = latency;
527 netj->redundancy = redundancy;
528 netj->use_autoconfig = use_autoconfig;
529 netj->always_deadline = always_deadline;
532 netj->client = client;
535 if ((bitdepth != 0) && (bitdepth != 8) && (bitdepth != 16) && (bitdepth != CELT_MODE))
537 jack_info ("Invalid bitdepth: %d (8, 16 or 0 for float) !!!", bitdepth);
538 return NULL;
540 netj->bitdepth = bitdepth;
543 if (resample_factor_up == 0)
544 resample_factor_up = resample_factor;
546 netj->resample_factor = resample_factor;
547 netj->resample_factor_up = resample_factor_up;
550 return netj;
553 void netjack_release( netjack_driver_state_t *netj )
555 close( netj->sockfd );
556 close( netj->outsockfd );
558 packet_cache_free( global_packcache );
559 global_packcache = NULL;
563 netjack_startup( netjack_driver_state_t *netj )
565 int first_pack_len;
566 struct sockaddr_in address;
567 // Now open the socket, and wait for the first packet to arrive...
568 netj->sockfd = socket (AF_INET, SOCK_DGRAM, 0);
569 #ifdef WIN32
570 if (netj->sockfd == INVALID_SOCKET)
571 #else
572 if (netj->sockfd == -1)
573 #endif
575 jack_info ("socket error");
576 return -1;
578 address.sin_family = AF_INET;
579 address.sin_port = htons(netj->listen_port);
580 address.sin_addr.s_addr = htonl(INADDR_ANY);
581 if (bind (netj->sockfd, (struct sockaddr *) &address, sizeof (address)) < 0)
583 jack_info("bind error");
584 return -1;
587 netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
588 #ifdef WIN32
589 if (netj->outsockfd == INVALID_SOCKET)
590 #else
591 if (netj->outsockfd == -1)
592 #endif
594 jack_info ("socket error");
595 return -1;
597 netj->srcaddress_valid = 0;
598 if (netj->use_autoconfig)
600 jacknet_packet_header *first_packet = alloca (sizeof (jacknet_packet_header));
601 #ifdef WIN32
602 int address_size = sizeof( struct sockaddr_in );
603 #else
604 socklen_t address_size = sizeof (struct sockaddr_in);
605 #endif
606 //jack_info ("Waiting for an incoming packet !!!");
607 //jack_info ("*** IMPORTANT *** Dont connect a client to jackd until the driver is attached to a clock source !!!");
609 while(1) {
610 first_pack_len = recvfrom (netj->sockfd, (char *)first_packet, sizeof (jacknet_packet_header), 0, (struct sockaddr*) & netj->syncsource_address, &address_size);
611 #ifdef WIN32
612 if( first_pack_len == -1 ) {
613 first_pack_len = sizeof(jacknet_packet_header);
614 break;
616 #else
617 if (first_pack_len == sizeof (jacknet_packet_header))
618 break;
619 #endif
621 netj->srcaddress_valid = 1;
623 if (first_pack_len == sizeof (jacknet_packet_header))
625 packet_header_ntoh (first_packet);
627 jack_info ("AutoConfig Override !!!");
628 if (netj->sample_rate != first_packet->sample_rate)
630 jack_info ("AutoConfig Override: Master JACK sample rate = %d", first_packet->sample_rate);
631 netj->sample_rate = first_packet->sample_rate;
634 if (netj->period_size != first_packet->period_size)
636 jack_info ("AutoConfig Override: Master JACK period size is %d", first_packet->period_size);
637 netj->period_size = first_packet->period_size;
639 if (netj->capture_channels_audio != first_packet->capture_channels_audio)
641 jack_info ("AutoConfig Override: capture_channels_audio = %d", first_packet->capture_channels_audio);
642 netj->capture_channels_audio = first_packet->capture_channels_audio;
644 if (netj->capture_channels_midi != first_packet->capture_channels_midi)
646 jack_info ("AutoConfig Override: capture_channels_midi = %d", first_packet->capture_channels_midi);
647 netj->capture_channels_midi = first_packet->capture_channels_midi;
649 if (netj->playback_channels_audio != first_packet->playback_channels_audio)
651 jack_info ("AutoConfig Override: playback_channels_audio = %d", first_packet->playback_channels_audio);
652 netj->playback_channels_audio = first_packet->playback_channels_audio;
654 if (netj->playback_channels_midi != first_packet->playback_channels_midi)
656 jack_info ("AutoConfig Override: playback_channels_midi = %d", first_packet->playback_channels_midi);
657 netj->playback_channels_midi = first_packet->playback_channels_midi;
660 netj->mtu = first_packet->mtu;
661 jack_info ("MTU is set to %d bytes", first_packet->mtu);
662 netj->latency = first_packet->latency;
665 netj->capture_channels = netj->capture_channels_audio + netj->capture_channels_midi;
666 netj->playback_channels = netj->playback_channels_audio + netj->playback_channels_midi;
668 // After possible Autoconfig: do all calculations...
669 netj->period_usecs =
670 (jack_time_t) floor ((((float) netj->period_size) / (float)netj->sample_rate)
671 * 1000000.0f);
673 if( netj->latency == 0 )
674 netj->deadline_offset = 50*netj->period_usecs;
675 else
676 netj->deadline_offset = netj->period_usecs + 10*netj->latency*netj->period_usecs/100;
678 if( netj->bitdepth == CELT_MODE ) {
679 // celt mode.
680 // TODO: this is a hack. But i dont want to change the packet header.
681 netj->net_period_down = netj->resample_factor;
682 netj->net_period_up = netj->resample_factor_up;
683 } else {
684 netj->net_period_down = (float) netj->period_size / (float) netj->resample_factor;
685 netj->net_period_up = (float) netj->period_size / (float) netj->resample_factor_up;
688 netj->rx_bufsize = sizeof (jacknet_packet_header) + netj->net_period_down * netj->capture_channels * get_sample_size (netj->bitdepth);
689 netj->pkt_buf = malloc (netj->rx_bufsize);
690 global_packcache = packet_cache_new (netj->latency + 50, netj->rx_bufsize, netj->mtu);
692 netj->expected_framecnt_valid = 0;
693 netj->num_lost_packets = 0;
694 netj->next_deadline_valid = 0;
695 netj->deadline_goodness = 0;
696 netj->time_to_deadline = 0;
698 // Special handling for latency=0
699 if( netj->latency == 0 )
700 netj->resync_threshold = 0;
701 else
702 netj->resync_threshold = MIN( 15, netj->latency-1 );
704 netj->running_free = 0;
706 return 0;