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.

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