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.

723 lines
24KB

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