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.

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