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.

801 lines
26KB

  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. #define socklen_t int
  35. #else
  36. #include <sys/socket.h>
  37. #include <netinet/in.h>
  38. #endif
  39. #ifdef __linux__
  40. #include "config.h"
  41. #endif
  42. #if HAVE_SAMPLERATE
  43. #include <samplerate.h>
  44. #endif
  45. #include "netjack.h"
  46. #include "netjack_packet.h"
  47. // JACK2
  48. #include "control.h"
  49. #define MIN(x,y) ((x)<(y) ? (x) : (y))
  50. static int sync_state = 1;
  51. static jack_transport_state_t last_transport_state;
  52. static int
  53. net_driver_sync_cb(jack_transport_state_t state, jack_position_t *pos, void *data)
  54. {
  55. int retval = sync_state;
  56. if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
  57. retval = 0;
  58. }
  59. // if (state == JackTransportStarting)
  60. // jack_info("Starting sync_state = %d", sync_state);
  61. last_transport_state = state;
  62. return retval;
  63. }
  64. int netjack_wait( netjack_driver_state_t *netj )
  65. {
  66. int we_have_the_expected_frame = 0;
  67. jack_nframes_t next_frame_avail;
  68. jack_time_t packet_recv_time_stamp;
  69. jacknet_packet_header *pkthdr;
  70. if( !netj->next_deadline_valid ) {
  71. netj->next_deadline = jack_get_time() + netj->period_usecs;
  72. netj->next_deadline_valid = 1;
  73. }
  74. // Increment expected frame here.
  75. if( netj->expected_framecnt_valid ) {
  76. netj->expected_framecnt += 1;
  77. } else {
  78. // starting up.... lets look into the packetcache, and fetch the highest packet.
  79. packet_cache_drain_socket( netj->packcache, netj->sockfd );
  80. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail ) ) {
  81. netj->expected_framecnt = next_frame_avail;
  82. netj->expected_framecnt_valid = 1;
  83. } else {
  84. // no packets there... start normally.
  85. netj->expected_framecnt = 0;
  86. netj->expected_framecnt_valid = 1;
  87. }
  88. }
  89. //jack_log( "expect %d", netj->expected_framecnt );
  90. // Now check if required packet is already in the cache.
  91. // then poll (have deadline calculated)
  92. // then drain socket, rinse and repeat.
  93. while(1) {
  94. if( packet_cache_get_next_available_framecnt( netj->packcache, netj->expected_framecnt, &next_frame_avail) ) {
  95. if( next_frame_avail == netj->expected_framecnt ) {
  96. we_have_the_expected_frame = 1;
  97. if( !netj->always_deadline )
  98. break;
  99. }
  100. }
  101. if( ! netjack_poll_deadline( netj->sockfd, netj->next_deadline ) ) {
  102. break;
  103. }
  104. packet_cache_drain_socket( netj->packcache, netj->sockfd );
  105. }
  106. // check if we know who to send our packets too.
  107. if (!netj->srcaddress_valid)
  108. if( netj->packcache->master_address_valid ) {
  109. memcpy (&(netj->syncsource_address), &(netj->packcache->master_address), sizeof( struct sockaddr_in ) );
  110. netj->srcaddress_valid = 1;
  111. }
  112. // XXX: switching mode unconditionally is stupid.
  113. // if we were running free perhaps we like to behave differently
  114. // ie. fastforward one packet etc.
  115. // well... this is the first packet we see. hmm.... dunno ;S
  116. // it works... so...
  117. netj->running_free = 0;
  118. //if( !we_have_the_expected_frame )
  119. // jack_error( "netxrun... %d", netj->expected_framecnt );
  120. if( we_have_the_expected_frame ) {
  121. jack_time_t now = jack_get_time();
  122. if( now < netj->next_deadline )
  123. netj->time_to_deadline = netj->next_deadline - now;
  124. else
  125. netj->time_to_deadline = 0;
  126. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize , &packet_recv_time_stamp);
  127. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  128. packet_header_ntoh(pkthdr);
  129. netj->deadline_goodness = (int)pkthdr->sync_state;
  130. netj->packet_data_valid = 1;
  131. int want_deadline;
  132. if( netj->jitter_val != 0 )
  133. want_deadline = netj->jitter_val;
  134. else if( netj->latency < 4 )
  135. want_deadline = -netj->period_usecs/2;
  136. else
  137. want_deadline = (netj->period_usecs/4+10*(int)netj->period_usecs*netj->latency/100);
  138. if( netj->deadline_goodness != MASTER_FREEWHEELS ) {
  139. if( netj->deadline_goodness < want_deadline ) {
  140. netj->next_deadline -= netj->period_usecs/100;
  141. //jack_log( "goodness: %d, Adjust deadline: --- %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  142. }
  143. if( netj->deadline_goodness > want_deadline ) {
  144. netj->next_deadline += netj->period_usecs/100;
  145. //jack_log( "goodness: %d, Adjust deadline: +++ %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  146. }
  147. }
  148. // if( netj->next_deadline < (netj->period_usecs*70/100) ) {
  149. // jack_error( "master is forcing deadline_offset to below 70%% of period_usecs... increase latency setting on master" );
  150. // netj->deadline_offset = (netj->period_usecs*90/100);
  151. // }
  152. netj->next_deadline += netj->period_usecs;
  153. } else {
  154. netj->time_to_deadline = 0;
  155. netj->next_deadline += netj->period_usecs;
  156. // bah... the packet is not there.
  157. // either
  158. // - it got lost.
  159. // - its late
  160. // - sync source is not sending anymore.
  161. // lets check if we have the next packets, we will just run a cycle without data.
  162. // in that case.
  163. if( packet_cache_get_next_available_framecnt( netj->packcache, netj->expected_framecnt, &next_frame_avail) )
  164. {
  165. jack_nframes_t offset = next_frame_avail - netj->expected_framecnt;
  166. //XXX: hmm... i need to remember why resync_threshold wasnt right.
  167. //if( offset < netj->resync_threshold )
  168. if( offset < 10 ) {
  169. // ok. dont do nothing. we will run without data.
  170. // this seems to be one or 2 lost packets.
  171. //
  172. // this can also be reordered packet jitter.
  173. // (maybe this is not happening in real live)
  174. // but it happens in netem.
  175. netj->packet_data_valid = 0;
  176. // I also found this happening, when the packet queue, is too full.
  177. // but wtf ? use a smaller latency. this link can handle that ;S
  178. if( packet_cache_get_fill( netj->packcache, netj->expected_framecnt ) > 80.0 )
  179. netj->next_deadline -= netj->period_usecs/2;
  180. } else {
  181. // the diff is too high. but we have a packet in the future.
  182. // lets resync.
  183. netj->expected_framecnt = next_frame_avail;
  184. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
  185. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  186. packet_header_ntoh(pkthdr);
  187. //netj->deadline_goodness = 0;
  188. netj->deadline_goodness = (int)pkthdr->sync_state - (int)netj->period_usecs * offset;
  189. netj->next_deadline_valid = 0;
  190. netj->packet_data_valid = 1;
  191. }
  192. } else {
  193. // no packets in buffer.
  194. netj->packet_data_valid = 0;
  195. //printf( "frame %d No Packet in queue. num_lost_packets = %d \n", netj->expected_framecnt, netj->num_lost_packets );
  196. if( netj->num_lost_packets < 5 ) {
  197. // ok. No Packet in queue. The packet was either lost,
  198. // or we are running too fast.
  199. //
  200. // Adjusting the deadline unconditionally resulted in
  201. // too many xruns on master.
  202. // But we need to adjust for the case we are running too fast.
  203. // So lets check if the last packet is there now.
  204. //
  205. // It would not be in the queue anymore, if it had been
  206. // retrieved. This might break for redundancy, but
  207. // i will make the packet cache drop redundant packets,
  208. // that have already been retreived.
  209. //
  210. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail) ) {
  211. if( next_frame_avail == (netj->expected_framecnt - 1) ) {
  212. // Ok. the last packet is there now.
  213. // and it had not been retrieved.
  214. //
  215. // TODO: We are still dropping 2 packets.
  216. // perhaps we can adjust the deadline
  217. // when (num_packets lost == 0)
  218. // This might still be too much.
  219. netj->next_deadline += netj->period_usecs;
  220. }
  221. }
  222. } else if( (netj->num_lost_packets <= 100) ) {
  223. // lets try adjusting the deadline harder, for some packets, we might have just ran 2 fast.
  224. netj->next_deadline += netj->period_usecs*netj->latency/8;
  225. } else {
  226. // But now we can check for any new frame available.
  227. //
  228. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail) ) {
  229. netj->expected_framecnt = next_frame_avail;
  230. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
  231. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  232. packet_header_ntoh(pkthdr);
  233. netj->deadline_goodness = pkthdr->sync_state;
  234. netj->next_deadline_valid = 0;
  235. netj->packet_data_valid = 1;
  236. netj->running_free = 0;
  237. jack_info( "resync after freerun... %d", netj->expected_framecnt );
  238. } else {
  239. if( netj->num_lost_packets == 101 ) {
  240. jack_info( "master seems gone... entering freerun mode", netj->expected_framecnt );
  241. }
  242. netj->running_free = 1;
  243. // when we really dont see packets.
  244. // reset source address. and open possibility for new master.
  245. // maybe dsl reconnect. Also restart of netsource without fix
  246. // reply address changes port.
  247. if (netj->num_lost_packets > 200 ) {
  248. netj->srcaddress_valid = 0;
  249. packet_cache_reset_master_address( netj->packcache );
  250. }
  251. }
  252. }
  253. }
  254. }
  255. int retval = 0;
  256. if( !netj->packet_data_valid ) {
  257. netj->num_lost_packets += 1;
  258. if( netj->num_lost_packets == 1 )
  259. retval = netj->period_usecs;
  260. } else {
  261. if( (netj->num_lost_packets>1) && !netj->running_free )
  262. retval = (netj->num_lost_packets-1) * netj->period_usecs;
  263. netj->num_lost_packets = 0;
  264. }
  265. return retval;
  266. }
  267. void netjack_send_silence( netjack_driver_state_t *netj, int syncstate )
  268. {
  269. int tx_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up + sizeof(jacknet_packet_header);
  270. unsigned int *packet_buf, *packet_bufX;
  271. packet_buf = alloca( tx_size);
  272. jacknet_packet_header *tx_pkthdr = (jacknet_packet_header *)packet_buf;
  273. jacknet_packet_header *rx_pkthdr = (jacknet_packet_header *)netj->rx_buf;
  274. //framecnt = rx_pkthdr->framecnt;
  275. netj->reply_port = rx_pkthdr->reply_port;
  276. // offset packet_bufX by the packetheader.
  277. packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
  278. tx_pkthdr->sync_state = syncstate;
  279. tx_pkthdr->framecnt = netj->expected_framecnt;
  280. // memset 0 the payload.
  281. int payload_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up;
  282. memset(packet_bufX, 0, payload_size);
  283. packet_header_hton(tx_pkthdr);
  284. if (netj->srcaddress_valid)
  285. {
  286. int r;
  287. if (netj->reply_port)
  288. netj->syncsource_address.sin_port = htons(netj->reply_port);
  289. for( r=0; r<netj->redundancy; r++ )
  290. netjack_sendto(netj->outsockfd, (char *)packet_buf, tx_size,
  291. 0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
  292. }
  293. }
  294. void netjack_attach( netjack_driver_state_t *netj )
  295. {
  296. //puts ("net_driver_attach");
  297. jack_port_t * port;
  298. char buf[32];
  299. unsigned int chn;
  300. int port_flags;
  301. if( netj->bitdepth == CELT_MODE )
  302. {
  303. #if HAVE_CELT
  304. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  305. celt_int32 lookahead;
  306. netj->celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  307. #else
  308. celt_int32_t lookahead;
  309. netj->celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  310. #endif
  311. celt_mode_info( netj->celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
  312. netj->codec_latency = 2*lookahead;
  313. #endif
  314. }
  315. if (netj->handle_transport_sync)
  316. jack_set_sync_callback(netj->client, (JackSyncCallback) net_driver_sync_cb, NULL);
  317. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  318. for (chn = 0; chn < netj->capture_channels_audio; chn++) {
  319. snprintf (buf, sizeof(buf) - 1, "capture_%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->capture_ports =
  328. jack_slist_append (netj->capture_ports, port);
  329. if( netj->bitdepth == CELT_MODE ) {
  330. #if HAVE_CELT
  331. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  332. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode, 1, NULL ) );
  333. #else
  334. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode ) );
  335. #endif
  336. #endif
  337. } else {
  338. #if HAVE_SAMPLERATE
  339. netj->capture_srcs = jack_slist_append(netj->capture_srcs, src_new(SRC_LINEAR, 1, NULL));
  340. #endif
  341. }
  342. }
  343. for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
  344. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  345. port = jack_port_register (netj->client, buf,
  346. JACK_DEFAULT_MIDI_TYPE,
  347. port_flags, 0);
  348. if (!port) {
  349. jack_error ("NET: cannot register port for %s", buf);
  350. break;
  351. }
  352. netj->capture_ports =
  353. jack_slist_append (netj->capture_ports, port);
  354. }
  355. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  356. for (chn = 0; chn < netj->playback_channels_audio; chn++) {
  357. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  358. port = jack_port_register (netj->client, buf,
  359. JACK_DEFAULT_AUDIO_TYPE,
  360. port_flags, 0);
  361. if (!port) {
  362. jack_error ("NET: cannot register port for %s", buf);
  363. break;
  364. }
  365. netj->playback_ports =
  366. jack_slist_append (netj->playback_ports, port);
  367. if( netj->bitdepth == CELT_MODE ) {
  368. #if HAVE_CELT
  369. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  370. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  371. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
  372. #else
  373. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  374. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode ) );
  375. #endif
  376. #endif
  377. } else {
  378. #if HAVE_SAMPLERATE
  379. netj->playback_srcs = jack_slist_append(netj->playback_srcs, src_new(SRC_LINEAR, 1, NULL));
  380. #endif
  381. }
  382. }
  383. for (chn = netj->playback_channels_audio; chn < netj->playback_channels; chn++) {
  384. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  385. port = jack_port_register (netj->client, buf,
  386. JACK_DEFAULT_MIDI_TYPE,
  387. port_flags, 0);
  388. if (!port) {
  389. jack_error ("NET: cannot register port for %s", buf);
  390. break;
  391. }
  392. netj->playback_ports =
  393. jack_slist_append (netj->playback_ports, port);
  394. }
  395. jack_activate (netj->client);
  396. }
  397. void netjack_detach( netjack_driver_state_t *netj )
  398. {
  399. JSList * node;
  400. for (node = netj->capture_ports; node; node = jack_slist_next (node))
  401. jack_port_unregister (netj->client,
  402. ((jack_port_t *) node->data));
  403. jack_slist_free (netj->capture_ports);
  404. netj->capture_ports = NULL;
  405. for (node = netj->capture_srcs; node; node = jack_slist_next (node))
  406. {
  407. #if HAVE_CELT
  408. if( netj->bitdepth == CELT_MODE )
  409. {
  410. CELTDecoder * decoder = node->data;
  411. celt_decoder_destroy(decoder);
  412. }
  413. else
  414. #endif
  415. {
  416. #if HAVE_SAMPLERATE
  417. SRC_STATE * src = node->data;
  418. src_delete(src);
  419. #endif
  420. }
  421. }
  422. jack_slist_free (netj->capture_srcs);
  423. netj->playback_srcs = NULL;
  424. for (node = netj->playback_ports; node; node = jack_slist_next (node))
  425. jack_port_unregister (netj->client,
  426. ((jack_port_t *) node->data));
  427. jack_slist_free (netj->playback_ports);
  428. netj->playback_ports = NULL;
  429. for (node = netj->playback_srcs; node; node = jack_slist_next (node))
  430. {
  431. #if HAVE_CELT
  432. if( netj->bitdepth == CELT_MODE )
  433. {
  434. CELTEncoder * encoder = node->data;
  435. celt_encoder_destroy(encoder);
  436. }
  437. else
  438. #endif
  439. {
  440. #if HAVE_SAMPLERATE
  441. SRC_STATE * src = node->data;
  442. src_delete(src);
  443. #endif
  444. }
  445. }
  446. jack_slist_free (netj->playback_srcs);
  447. netj->playback_srcs = NULL;
  448. #if HAVE_CELT
  449. if( netj->bitdepth == CELT_MODE )
  450. celt_mode_destroy(netj->celt_mode);
  451. #endif
  452. }
  453. netjack_driver_state_t *netjack_init (netjack_driver_state_t *netj,
  454. jack_client_t * client,
  455. const char *name,
  456. unsigned int capture_ports,
  457. unsigned int playback_ports,
  458. unsigned int capture_ports_midi,
  459. unsigned int playback_ports_midi,
  460. jack_nframes_t sample_rate,
  461. jack_nframes_t period_size,
  462. unsigned int listen_port,
  463. unsigned int transport_sync,
  464. unsigned int resample_factor,
  465. unsigned int resample_factor_up,
  466. unsigned int bitdepth,
  467. unsigned int use_autoconfig,
  468. unsigned int latency,
  469. unsigned int redundancy,
  470. int dont_htonl_floats,
  471. int always_deadline,
  472. int jitter_val )
  473. {
  474. // Fill in netj values.
  475. // might be subject to autoconfig...
  476. // so dont calculate anything with them...
  477. netj->sample_rate = sample_rate;
  478. netj->period_size = period_size;
  479. netj->dont_htonl_floats = dont_htonl_floats;
  480. netj->listen_port = listen_port;
  481. netj->capture_channels = capture_ports + capture_ports_midi;
  482. netj->capture_channels_audio = capture_ports;
  483. netj->capture_channels_midi = capture_ports_midi;
  484. netj->capture_ports = NULL;
  485. netj->playback_channels = playback_ports + playback_ports_midi;
  486. netj->playback_channels_audio = playback_ports;
  487. netj->playback_channels_midi = playback_ports_midi;
  488. netj->playback_ports = NULL;
  489. netj->codec_latency = 0;
  490. netj->handle_transport_sync = transport_sync;
  491. netj->mtu = 1400;
  492. netj->latency = latency;
  493. netj->redundancy = redundancy;
  494. netj->use_autoconfig = use_autoconfig;
  495. netj->always_deadline = always_deadline;
  496. netj->client = client;
  497. if ((bitdepth != 0) && (bitdepth != 8) && (bitdepth != 16) && (bitdepth != CELT_MODE))
  498. {
  499. jack_info ("Invalid bitdepth: %d (8, 16 or 0 for float) !!!", bitdepth);
  500. return NULL;
  501. }
  502. netj->bitdepth = bitdepth;
  503. if (resample_factor_up == 0)
  504. resample_factor_up = resample_factor;
  505. netj->resample_factor = resample_factor;
  506. netj->resample_factor_up = resample_factor_up;
  507. netj->jitter_val = jitter_val;
  508. return netj;
  509. }
  510. void netjack_release( netjack_driver_state_t *netj )
  511. {
  512. close( netj->sockfd );
  513. close( netj->outsockfd );
  514. packet_cache_free( netj->packcache );
  515. netj->packcache = NULL;
  516. }
  517. int
  518. netjack_startup( netjack_driver_state_t *netj )
  519. {
  520. int first_pack_len;
  521. struct sockaddr_in address;
  522. // Now open the socket, and wait for the first packet to arrive...
  523. netj->sockfd = socket (AF_INET, SOCK_DGRAM, 0);
  524. #ifdef WIN32
  525. if (netj->sockfd == INVALID_SOCKET)
  526. #else
  527. if (netj->sockfd == -1)
  528. #endif
  529. {
  530. jack_info ("socket error");
  531. return -1;
  532. }
  533. address.sin_family = AF_INET;
  534. address.sin_port = htons(netj->listen_port);
  535. address.sin_addr.s_addr = htonl(INADDR_ANY);
  536. if (bind (netj->sockfd, (struct sockaddr *) &address, sizeof (address)) < 0)
  537. {
  538. jack_info("bind error");
  539. return -1;
  540. }
  541. netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  542. #ifdef WIN32
  543. if (netj->outsockfd == INVALID_SOCKET)
  544. #else
  545. if (netj->outsockfd == -1)
  546. #endif
  547. {
  548. jack_info ("socket error");
  549. return -1;
  550. }
  551. netj->srcaddress_valid = 0;
  552. if (netj->use_autoconfig)
  553. {
  554. jacknet_packet_header *first_packet = alloca (sizeof (jacknet_packet_header));
  555. #ifdef WIN32
  556. int address_size = sizeof( struct sockaddr_in );
  557. #else
  558. socklen_t address_size = sizeof (struct sockaddr_in);
  559. #endif
  560. //jack_info ("Waiting for an incoming packet !!!");
  561. //jack_info ("*** IMPORTANT *** Dont connect a client to jackd until the driver is attached to a clock source !!!");
  562. while(1) {
  563. if( ! netjack_poll( netj->sockfd, 1000 ) ) {
  564. jack_info ("Waiting aborted");
  565. return -1;
  566. }
  567. first_pack_len = recvfrom (netj->sockfd, (char *)first_packet, sizeof (jacknet_packet_header), 0, (struct sockaddr*) & netj->syncsource_address, &address_size);
  568. #ifdef WIN32
  569. if( first_pack_len == -1 ) {
  570. first_pack_len = sizeof(jacknet_packet_header);
  571. break;
  572. }
  573. #else
  574. if (first_pack_len == sizeof (jacknet_packet_header))
  575. break;
  576. #endif
  577. }
  578. netj->srcaddress_valid = 1;
  579. if (first_pack_len == sizeof (jacknet_packet_header))
  580. {
  581. packet_header_ntoh (first_packet);
  582. jack_info ("AutoConfig Override !!!");
  583. if (netj->sample_rate != first_packet->sample_rate)
  584. {
  585. jack_info ("AutoConfig Override: Master JACK sample rate = %d", first_packet->sample_rate);
  586. netj->sample_rate = first_packet->sample_rate;
  587. }
  588. if (netj->period_size != first_packet->period_size)
  589. {
  590. jack_info ("AutoConfig Override: Master JACK period size is %d", first_packet->period_size);
  591. netj->period_size = first_packet->period_size;
  592. }
  593. if (netj->capture_channels_audio != first_packet->capture_channels_audio)
  594. {
  595. jack_info ("AutoConfig Override: capture_channels_audio = %d", first_packet->capture_channels_audio);
  596. netj->capture_channels_audio = first_packet->capture_channels_audio;
  597. }
  598. if (netj->capture_channels_midi != first_packet->capture_channels_midi)
  599. {
  600. jack_info ("AutoConfig Override: capture_channels_midi = %d", first_packet->capture_channels_midi);
  601. netj->capture_channels_midi = first_packet->capture_channels_midi;
  602. }
  603. if (netj->playback_channels_audio != first_packet->playback_channels_audio)
  604. {
  605. jack_info ("AutoConfig Override: playback_channels_audio = %d", first_packet->playback_channels_audio);
  606. netj->playback_channels_audio = first_packet->playback_channels_audio;
  607. }
  608. if (netj->playback_channels_midi != first_packet->playback_channels_midi)
  609. {
  610. jack_info ("AutoConfig Override: playback_channels_midi = %d", first_packet->playback_channels_midi);
  611. netj->playback_channels_midi = first_packet->playback_channels_midi;
  612. }
  613. netj->mtu = first_packet->mtu;
  614. jack_info ("MTU is set to %d bytes", first_packet->mtu);
  615. netj->latency = first_packet->latency;
  616. }
  617. }
  618. netj->capture_channels = netj->capture_channels_audio + netj->capture_channels_midi;
  619. netj->playback_channels = netj->playback_channels_audio + netj->playback_channels_midi;
  620. if( (netj->capture_channels * netj->period_size * netj->latency * 4) > 100000000 ) {
  621. jack_error( "autoconfig requests more than 100MB packet cache... bailing out" );
  622. exit(1);
  623. }
  624. if( netj->playback_channels > 1000 ) {
  625. jack_error( "autoconfig requests more than 1000 playback channels... bailing out" );
  626. exit(1);
  627. }
  628. if( netj->mtu < (2*sizeof( jacknet_packet_header )) ) {
  629. jack_error( "bullshit mtu requested by autoconfig" );
  630. exit(1);
  631. }
  632. if( netj->sample_rate == 0 ) {
  633. jack_error( "sample_rate 0 requested by autoconfig" );
  634. exit(1);
  635. }
  636. // After possible Autoconfig: do all calculations...
  637. netj->period_usecs =
  638. (jack_time_t) floor ((((float) netj->period_size) / (float)netj->sample_rate)
  639. * 1000000.0f);
  640. if( netj->latency == 0 )
  641. netj->deadline_offset = 50*netj->period_usecs;
  642. else
  643. netj->deadline_offset = netj->period_usecs + 10*netj->latency*netj->period_usecs/100;
  644. if( netj->bitdepth == CELT_MODE ) {
  645. // celt mode.
  646. // TODO: this is a hack. But i dont want to change the packet header.
  647. netj->resample_factor = (netj->resample_factor * netj->period_size * 1024 / netj->sample_rate / 8)&(~1);
  648. netj->resample_factor_up = (netj->resample_factor_up * netj->period_size * 1024 / netj->sample_rate / 8)&(~1);
  649. netj->net_period_down = netj->resample_factor;
  650. netj->net_period_up = netj->resample_factor_up;
  651. } else {
  652. netj->net_period_down = (float) netj->period_size / (float) netj->resample_factor;
  653. netj->net_period_up = (float) netj->period_size / (float) netj->resample_factor_up;
  654. }
  655. netj->rx_bufsize = sizeof (jacknet_packet_header) + netj->net_period_down * netj->capture_channels * get_sample_size (netj->bitdepth);
  656. netj->packcache = packet_cache_new (netj->latency + 50, netj->rx_bufsize, netj->mtu);
  657. netj->expected_framecnt_valid = 0;
  658. netj->num_lost_packets = 0;
  659. netj->next_deadline_valid = 0;
  660. netj->deadline_goodness = 0;
  661. netj->time_to_deadline = 0;
  662. // Special handling for latency=0
  663. if( netj->latency == 0 )
  664. netj->resync_threshold = 0;
  665. else
  666. netj->resync_threshold = MIN( 15, netj->latency-1 );
  667. netj->running_free = 0;
  668. return 0;
  669. }