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.

702 lines
23KB

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