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.

754 lines
25KB

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