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.

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