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.

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