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.

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