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.

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