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.

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