jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

688 lines
23KB

  1. /* -*- mode: c; c-file-style: "linux"; -*- */
  2. /*
  3. NetJack Abstraction.
  4. Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  5. Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  6. Copyright (C) 2003 Robert Ham <rah@bash.sh>
  7. Copyright (C) 2001 Paul Davis
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. $Id: net_driver.c,v 1.17 2006/04/16 20:16:10 torbenh Exp $
  20. */
  21. #define HAVE_CELT 1
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include <memory.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <stdarg.h>
  29. #include <jack/types.h>
  30. #include "jack/jslist.h"
  31. #include <sys/types.h>
  32. #ifdef WIN32
  33. #include <winsock.h>
  34. #include <malloc.h>
  35. #else
  36. #include <sys/socket.h>
  37. #include <netinet/in.h>
  38. #endif
  39. #include "netjack.h"
  40. //#include "config.h"
  41. #if HAVE_SAMPLERATE
  42. #include <samplerate.h>
  43. #endif
  44. #if HAVE_CELT
  45. #include <celt/celt.h>
  46. #endif
  47. #include "netjack.h"
  48. #include "netjack_packet.h"
  49. // JACK2
  50. #include "jack/control.h"
  51. #define MIN(x,y) ((x)<(y) ? (x) : (y))
  52. static int sync_state = 1;
  53. static jack_transport_state_t last_transport_state;
  54. static int
  55. net_driver_sync_cb(jack_transport_state_t state, jack_position_t *pos, void *data)
  56. {
  57. int retval = sync_state;
  58. if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
  59. retval = 0;
  60. }
  61. // if (state == JackTransportStarting)
  62. // jack_info("Starting sync_state = %d", sync_state);
  63. last_transport_state = state;
  64. return retval;
  65. }
  66. void netjack_wait( netjack_driver_state_t *netj )
  67. {
  68. int we_have_the_expected_frame = 0;
  69. jack_nframes_t next_frame_avail;
  70. jack_time_t packet_recv_time_stamp;
  71. jacknet_packet_header *pkthdr;
  72. if( !netj->next_deadline_valid ) {
  73. if( netj->latency == 0 )
  74. // for full sync mode... always wait for packet.
  75. netj->next_deadline = jack_get_time() + 50*netj->period_usecs;
  76. else if( netj->latency == 1 )
  77. // for normal 1 period latency mode, only 1 period for dealine.
  78. netj->next_deadline = jack_get_time() + 110 * netj->period_usecs /100;
  79. else
  80. // looks like waiting 1 period always is correct.
  81. // not 100% sure yet. with the improved resync, it might be better,
  82. // to have more than one period headroom for high latency.
  83. //netj->next_deadline = jack_get_time() + 5*netj->latency*netj->period_usecs/4;
  84. netj->next_deadline = jack_get_time() + netj->period_usecs + 10*netj->latency*netj->period_usecs/100;
  85. netj->next_deadline_valid = 1;
  86. } else {
  87. netj->next_deadline += netj->period_usecs;
  88. }
  89. // Increment expected frame here.
  90. netj->expected_framecnt += 1;
  91. //jack_log( "expect %d", netj->expected_framecnt );
  92. // Now check if required packet is already in the cache.
  93. // then poll (have deadline calculated)
  94. // then drain socket, rinse and repeat.
  95. while(1) {
  96. if( packet_cache_get_next_available_framecnt( global_packcache, netj->expected_framecnt, &next_frame_avail) ) {
  97. if( next_frame_avail == netj->expected_framecnt ) {
  98. we_have_the_expected_frame = 1;
  99. if( !netj->always_deadline )
  100. break;
  101. }
  102. }
  103. if( ! netjack_poll_deadline( netj->sockfd, netj->next_deadline ) ) {
  104. break;
  105. }
  106. packet_cache_drain_socket( global_packcache, netj->sockfd );
  107. }
  108. // check if we know who to send our packets too.
  109. if (!netj->srcaddress_valid)
  110. if( global_packcache->master_address_valid ) {
  111. memcpy (&(netj->syncsource_address), &(global_packcache->master_address), sizeof( struct sockaddr_in ) );
  112. netj->srcaddress_valid = 1;
  113. }
  114. // XXX: switching mode unconditionally is stupid.
  115. // if we were running free perhaps we like to behave differently
  116. // ie. fastforward one packet etc.
  117. // well... this is the first packet we see. hmm.... dunno ;S
  118. // it works... so...
  119. netj->running_free = 0;
  120. if( !we_have_the_expected_frame )
  121. jack_log( "xrun... %d", netj->expected_framecnt );
  122. if( we_have_the_expected_frame ) {
  123. netj->time_to_deadline = netj->next_deadline - jack_get_time() - netj->period_usecs;
  124. packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize , &packet_recv_time_stamp);
  125. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  126. packet_header_ntoh(pkthdr);
  127. netj->deadline_goodness = (int)pkthdr->sync_state;
  128. netj->packet_data_valid = 1;
  129. // TODO: Queue state could be taken into account.
  130. // But needs more processing, cause, when we are running as
  131. // fast as we can, recv_time_offset can be zero, which is
  132. // good.
  133. // need to add (now-deadline) and check that.
  134. /*
  135. if( recv_time_offset < netj->period_usecs )
  136. //netj->next_deadline -= netj->period_usecs*netj->latency/100;
  137. netj->next_deadline += netj->period_usecs/1000;
  138. */
  139. if( netj->deadline_goodness < (netj->period_usecs/4+10*(int)netj->period_usecs*netj->latency/100) ) {
  140. netj->next_deadline += netj->period_usecs/100;
  141. //jack_log( "goodness: %d, Adjust deadline: --- %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  142. }
  143. if( netj->deadline_goodness > (netj->period_usecs/4+10*(int)netj->period_usecs*netj->latency/100) ) {
  144. netj->next_deadline -= netj->period_usecs/100;
  145. //jack_log( "goodness: %d, Adjust deadline: +++ %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  146. }
  147. } else {
  148. netj->time_to_deadline = 0;
  149. // bah... the packet is not there.
  150. // either
  151. // - it got lost.
  152. // - its late
  153. // - sync source is not sending anymore.
  154. // lets check if we have the next packets, we will just run a cycle without data.
  155. // in that case.
  156. if( packet_cache_get_next_available_framecnt( global_packcache, netj->expected_framecnt, &next_frame_avail) )
  157. {
  158. jack_nframes_t offset = next_frame_avail - netj->expected_framecnt;
  159. //XXX: hmm... i need to remember why resync_threshold wasnt right.
  160. //if( offset < netj->resync_threshold )
  161. if( offset < 10 ) {
  162. // ok. dont do nothing. we will run without data.
  163. // this seems to be one or 2 lost packets.
  164. //
  165. // this can also be reordered packet jitter.
  166. // (maybe this is not happening in real live)
  167. // but it happens in netem.
  168. netj->packet_data_valid = 0;
  169. // I also found this happening, when the packet queue, is too full.
  170. // but wtf ? use a smaller latency. this link can handle that ;S
  171. if( packet_cache_get_fill( global_packcache, netj->expected_framecnt ) > 80.0 )
  172. netj->next_deadline -= netj->period_usecs/2;
  173. } else {
  174. // the diff is too high. but we have a packet in the future.
  175. // lets resync.
  176. netj->expected_framecnt = next_frame_avail;
  177. packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
  178. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  179. packet_header_ntoh(pkthdr);
  180. //netj->deadline_goodness = 0;
  181. netj->deadline_goodness = (int)pkthdr->sync_state - (int)netj->period_usecs * offset;
  182. netj->next_deadline_valid = 0;
  183. netj->packet_data_valid = 1;
  184. }
  185. } else {
  186. // no packets in buffer.
  187. netj->packet_data_valid = 0;
  188. //printf( "frame %d No Packet in queue. num_lost_packets = %d \n", netj->expected_framecnt, netj->num_lost_packets );
  189. if( netj->num_lost_packets < 5 ) {
  190. // ok. No Packet in queue. The packet was either lost,
  191. // or we are running too fast.
  192. //
  193. // Adjusting the deadline unconditionally resulted in
  194. // too many xruns on master.
  195. // But we need to adjust for the case we are running too fast.
  196. // So lets check if the last packet is there now.
  197. //
  198. // It would not be in the queue anymore, if it had been
  199. // retrieved. This might break for redundancy, but
  200. // i will make the packet cache drop redundant packets,
  201. // that have already been retreived.
  202. //
  203. if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail) ) {
  204. if( next_frame_avail == (netj->expected_framecnt - 1) ) {
  205. // Ok. the last packet is there now.
  206. // and it had not been retrieved.
  207. //
  208. // TODO: We are still dropping 2 packets.
  209. // perhaps we can adjust the deadline
  210. // when (num_packets lost == 0)
  211. // This might still be too much.
  212. netj->next_deadline += netj->period_usecs;
  213. }
  214. }
  215. } else if( (netj->num_lost_packets <= 100) ) {
  216. // lets try adjusting the deadline harder, for some packets, we might have just ran 2 fast.
  217. netj->next_deadline += netj->period_usecs*netj->latency/8;
  218. } else {
  219. // But now we can check for any new frame available.
  220. //
  221. if( packet_cache_get_highest_available_framecnt( global_packcache, &next_frame_avail) ) {
  222. netj->expected_framecnt = next_frame_avail;
  223. packet_cache_retreive_packet_pointer( global_packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
  224. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  225. packet_header_ntoh(pkthdr);
  226. netj->deadline_goodness = pkthdr->sync_state;
  227. netj->next_deadline_valid = 0;
  228. netj->packet_data_valid = 1;
  229. netj->running_free = 0;
  230. jack_info( "resync after freerun... %d\n", netj->expected_framecnt );
  231. } else {
  232. // give up. lets run freely.
  233. // XXX: hmm...
  234. netj->running_free = 1;
  235. // when we really dont see packets.
  236. // reset source address. and open possibility for new master.
  237. // maybe dsl reconnect. Also restart of netsource without fix
  238. // reply address changes port.
  239. if (netj->num_lost_packets > 200 ) {
  240. netj->srcaddress_valid = 0;
  241. packet_cache_reset_master_address( global_packcache );
  242. }
  243. }
  244. }
  245. }
  246. }
  247. if( !netj->packet_data_valid )
  248. netj->num_lost_packets += 1;
  249. else {
  250. netj->num_lost_packets = 0;
  251. }
  252. }
  253. void netjack_send_silence( netjack_driver_state_t *netj, int syncstate )
  254. {
  255. int tx_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up + sizeof(jacknet_packet_header);
  256. unsigned int *packet_buf, *packet_bufX;
  257. packet_buf = alloca( tx_size);
  258. jacknet_packet_header *tx_pkthdr = (jacknet_packet_header *)packet_buf;
  259. jacknet_packet_header *rx_pkthdr = (jacknet_packet_header *)netj->rx_buf;
  260. //framecnt = rx_pkthdr->framecnt;
  261. netj->reply_port = rx_pkthdr->reply_port;
  262. // offset packet_bufX by the packetheader.
  263. packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
  264. tx_pkthdr->sync_state = syncstate;
  265. tx_pkthdr->framecnt = netj->expected_framecnt;
  266. // memset 0 the payload.
  267. int payload_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up;
  268. memset(packet_bufX, 0, payload_size);
  269. packet_header_hton(tx_pkthdr);
  270. if (netj->srcaddress_valid)
  271. {
  272. int r;
  273. if (netj->reply_port)
  274. netj->syncsource_address.sin_port = htons(netj->reply_port);
  275. for( r=0; r<netj->redundancy; r++ )
  276. netjack_sendto(netj->outsockfd, (char *)packet_buf, tx_size,
  277. 0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
  278. }
  279. }
  280. void netjack_attach( netjack_driver_state_t *netj )
  281. {
  282. //puts ("net_driver_attach");
  283. jack_port_t * port;
  284. char buf[32];
  285. unsigned int chn;
  286. int port_flags;
  287. if (netj->handle_transport_sync)
  288. jack_set_sync_callback(netj->client, (JackSyncCallback) net_driver_sync_cb, NULL);
  289. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  290. for (chn = 0; chn < netj->capture_channels_audio; chn++) {
  291. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  292. port = jack_port_register (netj->client, buf,
  293. JACK_DEFAULT_AUDIO_TYPE,
  294. port_flags, 0);
  295. if (!port) {
  296. jack_error ("NET: cannot register port for %s", buf);
  297. break;
  298. }
  299. netj->capture_ports =
  300. jack_slist_append (netj->capture_ports, port);
  301. if( netj->bitdepth == 1000 ) {
  302. #if HAVE_CELT
  303. celt_int32_t lookahead;
  304. // XXX: memory leak
  305. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  306. celt_mode_info( celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
  307. netj->codec_latency = 2*lookahead;
  308. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( celt_mode ) );
  309. #endif
  310. } else {
  311. #if HAVE_SAMPLERATE
  312. netj->capture_srcs = jack_slist_append(netj->capture_srcs, src_new(SRC_LINEAR, 1, NULL));
  313. #endif
  314. }
  315. }
  316. for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
  317. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  318. port = jack_port_register (netj->client, buf,
  319. JACK_DEFAULT_MIDI_TYPE,
  320. port_flags, 0);
  321. if (!port) {
  322. jack_error ("NET: cannot register port for %s", buf);
  323. break;
  324. }
  325. netj->capture_ports =
  326. jack_slist_append (netj->capture_ports, port);
  327. }
  328. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  329. for (chn = 0; chn < netj->playback_channels_audio; chn++) {
  330. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  331. port = jack_port_register (netj->client, buf,
  332. JACK_DEFAULT_AUDIO_TYPE,
  333. port_flags, 0);
  334. if (!port) {
  335. jack_error ("NET: cannot register port for %s", buf);
  336. break;
  337. }
  338. netj->playback_ports =
  339. jack_slist_append (netj->playback_ports, port);
  340. if( netj->bitdepth == 1000 ) {
  341. #if HAVE_CELT
  342. // XXX: memory leak
  343. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  344. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode ) );
  345. #endif
  346. } else {
  347. #if HAVE_SAMPLERATE
  348. netj->playback_srcs = jack_slist_append(netj->playback_srcs, src_new(SRC_LINEAR, 1, NULL));
  349. #endif
  350. }
  351. }
  352. for (chn = netj->playback_channels_audio; chn < netj->playback_channels; chn++) {
  353. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  354. port = jack_port_register (netj->client, buf,
  355. JACK_DEFAULT_MIDI_TYPE,
  356. port_flags, 0);
  357. if (!port) {
  358. jack_error ("NET: cannot register port for %s", buf);
  359. break;
  360. }
  361. netj->playback_ports =
  362. jack_slist_append (netj->playback_ports, port);
  363. }
  364. jack_activate (netj->client);
  365. }
  366. void netjack_detach( netjack_driver_state_t *netj )
  367. {
  368. JSList * node;
  369. for (node = netj->capture_ports; node; node = jack_slist_next (node))
  370. jack_port_unregister (netj->client,
  371. ((jack_port_t *) node->data));
  372. jack_slist_free (netj->capture_ports);
  373. netj->capture_ports = NULL;
  374. for (node = netj->playback_ports; node; node = jack_slist_next (node))
  375. jack_port_unregister (netj->client,
  376. ((jack_port_t *) node->data));
  377. jack_slist_free (netj->playback_ports);
  378. netj->playback_ports = NULL;
  379. }
  380. netjack_driver_state_t *netjack_init (netjack_driver_state_t *netj,
  381. jack_client_t * client,
  382. const char *name,
  383. unsigned int capture_ports,
  384. unsigned int playback_ports,
  385. unsigned int capture_ports_midi,
  386. unsigned int playback_ports_midi,
  387. jack_nframes_t sample_rate,
  388. jack_nframes_t period_size,
  389. unsigned int listen_port,
  390. unsigned int transport_sync,
  391. unsigned int resample_factor,
  392. unsigned int resample_factor_up,
  393. unsigned int bitdepth,
  394. unsigned int use_autoconfig,
  395. unsigned int latency,
  396. unsigned int redundancy,
  397. int dont_htonl_floats,
  398. int always_deadline)
  399. {
  400. // Fill in netj values.
  401. // might be subject to autoconfig...
  402. // so dont calculate anything with them...
  403. netj->sample_rate = sample_rate;
  404. netj->period_size = period_size;
  405. netj->dont_htonl_floats = dont_htonl_floats;
  406. netj->listen_port = listen_port;
  407. netj->capture_channels = capture_ports + capture_ports_midi;
  408. netj->capture_channels_audio = capture_ports;
  409. netj->capture_channels_midi = capture_ports_midi;
  410. netj->capture_ports = NULL;
  411. netj->playback_channels = playback_ports + playback_ports_midi;
  412. netj->playback_channels_audio = playback_ports;
  413. netj->playback_channels_midi = playback_ports_midi;
  414. netj->playback_ports = NULL;
  415. netj->codec_latency = 0;
  416. netj->handle_transport_sync = transport_sync;
  417. netj->mtu = 1400;
  418. netj->latency = latency;
  419. netj->redundancy = redundancy;
  420. netj->use_autoconfig = use_autoconfig;
  421. netj->always_deadline = always_deadline;
  422. netj->client = client;
  423. if ((bitdepth != 0) && (bitdepth != 8) && (bitdepth != 16) && (bitdepth != 1000))
  424. {
  425. jack_info ("Invalid bitdepth: %d (8, 16 or 0 for float) !!!", bitdepth);
  426. return NULL;
  427. }
  428. netj->bitdepth = bitdepth;
  429. if (resample_factor_up == 0)
  430. resample_factor_up = resample_factor;
  431. netj->resample_factor = resample_factor;
  432. netj->resample_factor_up = resample_factor_up;
  433. return netj;
  434. }
  435. void netjack_release( netjack_driver_state_t *netj )
  436. {
  437. close( netj->sockfd );
  438. close( netj->outsockfd );
  439. packet_cache_free( global_packcache );
  440. global_packcache = NULL;
  441. }
  442. int
  443. netjack_startup( netjack_driver_state_t *netj )
  444. {
  445. int first_pack_len;
  446. struct sockaddr_in address;
  447. // Now open the socket, and wait for the first packet to arrive...
  448. netj->sockfd = socket (AF_INET, SOCK_DGRAM, 0);
  449. #ifdef WIN32
  450. if (netj->sockfd == INVALID_SOCKET)
  451. #else
  452. if (netj->sockfd == -1)
  453. #endif
  454. {
  455. jack_info ("socket error");
  456. return -1;
  457. }
  458. address.sin_family = AF_INET;
  459. address.sin_port = htons(netj->listen_port);
  460. address.sin_addr.s_addr = htonl(INADDR_ANY);
  461. if (bind (netj->sockfd, (struct sockaddr *) &address, sizeof (address)) < 0)
  462. {
  463. jack_info("bind error");
  464. return -1;
  465. }
  466. netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  467. #ifdef WIN32
  468. if (netj->outsockfd == INVALID_SOCKET)
  469. #else
  470. if (netj->outsockfd == -1)
  471. #endif
  472. {
  473. jack_info ("socket error");
  474. return -1;
  475. }
  476. netj->srcaddress_valid = 0;
  477. if (netj->use_autoconfig)
  478. {
  479. jacknet_packet_header *first_packet = alloca (sizeof (jacknet_packet_header));
  480. #ifdef WIN32
  481. int address_size = sizeof( struct sockaddr_in );
  482. #else
  483. socklen_t address_size = sizeof (struct sockaddr_in);
  484. #endif
  485. //jack_info ("Waiting for an incoming packet !!!");
  486. //jack_info ("*** IMPORTANT *** Dont connect a client to jackd until the driver is attached to a clock source !!!");
  487. while(1) {
  488. first_pack_len = recvfrom (netj->sockfd, (char *)first_packet, sizeof (jacknet_packet_header), 0, (struct sockaddr*) & netj->syncsource_address, &address_size);
  489. #ifdef WIN32
  490. if( first_pack_len == -1 ) {
  491. first_pack_len = sizeof(jacknet_packet_header);
  492. break;
  493. }
  494. #else
  495. if (first_pack_len == sizeof (jacknet_packet_header))
  496. break;
  497. #endif
  498. }
  499. netj->srcaddress_valid = 1;
  500. if (first_pack_len == sizeof (jacknet_packet_header))
  501. {
  502. packet_header_ntoh (first_packet);
  503. jack_info ("AutoConfig Override !!!");
  504. if (netj->sample_rate != first_packet->sample_rate)
  505. {
  506. jack_info ("AutoConfig Override: Master JACK sample rate = %d", first_packet->sample_rate);
  507. netj->sample_rate = first_packet->sample_rate;
  508. }
  509. if (netj->period_size != first_packet->period_size)
  510. {
  511. jack_info ("AutoConfig Override: Master JACK period size is %d", first_packet->period_size);
  512. netj->period_size = first_packet->period_size;
  513. }
  514. if (netj->capture_channels_audio != first_packet->capture_channels_audio)
  515. {
  516. jack_info ("AutoConfig Override: capture_channels_audio = %d", first_packet->capture_channels_audio);
  517. netj->capture_channels_audio = first_packet->capture_channels_audio;
  518. }
  519. if (netj->capture_channels_midi != first_packet->capture_channels_midi)
  520. {
  521. jack_info ("AutoConfig Override: capture_channels_midi = %d", first_packet->capture_channels_midi);
  522. netj->capture_channels_midi = first_packet->capture_channels_midi;
  523. }
  524. if (netj->playback_channels_audio != first_packet->playback_channels_audio)
  525. {
  526. jack_info ("AutoConfig Override: playback_channels_audio = %d", first_packet->playback_channels_audio);
  527. netj->playback_channels_audio = first_packet->playback_channels_audio;
  528. }
  529. if (netj->playback_channels_midi != first_packet->playback_channels_midi)
  530. {
  531. jack_info ("AutoConfig Override: playback_channels_midi = %d", first_packet->playback_channels_midi);
  532. netj->playback_channels_midi = first_packet->playback_channels_midi;
  533. }
  534. netj->mtu = first_packet->mtu;
  535. jack_info ("MTU is set to %d bytes", first_packet->mtu);
  536. netj->latency = first_packet->latency;
  537. }
  538. }
  539. netj->capture_channels = netj->capture_channels_audio + netj->capture_channels_midi;
  540. netj->playback_channels = netj->playback_channels_audio + netj->playback_channels_midi;
  541. // After possible Autoconfig: do all calculations...
  542. netj->period_usecs =
  543. (jack_time_t) floor ((((float) netj->period_size) / (float)netj->sample_rate)
  544. * 1000000.0f);
  545. if( netj->bitdepth == 1000 ) {
  546. // celt mode.
  547. // TODO: this is a hack. But i dont want to change the packet header.
  548. netj->net_period_down = netj->resample_factor;
  549. netj->net_period_up = netj->resample_factor_up;
  550. } else {
  551. netj->net_period_down = (float) netj->period_size / (float) netj->resample_factor;
  552. netj->net_period_up = (float) netj->period_size / (float) netj->resample_factor_up;
  553. }
  554. netj->rx_bufsize = sizeof (jacknet_packet_header) + netj->net_period_down * netj->capture_channels * get_sample_size (netj->bitdepth);
  555. netj->pkt_buf = malloc (netj->rx_bufsize);
  556. global_packcache = packet_cache_new (netj->latency + 50, netj->rx_bufsize, netj->mtu);
  557. netj->expected_framecnt_valid = 0;
  558. netj->num_lost_packets = 0;
  559. netj->next_deadline_valid = 0;
  560. netj->deadline_goodness = 0;
  561. netj->time_to_deadline = 0;
  562. // Special handling for latency=0
  563. if( netj->latency == 0 )
  564. netj->resync_threshold = 0;
  565. else
  566. netj->resync_threshold = MIN( 15, netj->latency-1 );
  567. netj->running_free = 0;
  568. return 0;
  569. }