jack1 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.

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