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.

806 lines
27KB

  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 || HAVE_CELT_API_0_11
  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_11
  332. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create_custom( netj->celt_mode, 1, NULL ) );
  333. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  334. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode, 1, NULL ) );
  335. #else
  336. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode ) );
  337. #endif
  338. #endif
  339. } else {
  340. #if HAVE_SAMPLERATE
  341. netj->capture_srcs = jack_slist_append(netj->capture_srcs, src_new(SRC_LINEAR, 1, NULL));
  342. #endif
  343. }
  344. }
  345. for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
  346. snprintf (buf, sizeof(buf) - 1, "capture_%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->capture_ports =
  355. jack_slist_append (netj->capture_ports, port);
  356. }
  357. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  358. for (chn = 0; chn < netj->playback_channels_audio; chn++) {
  359. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  360. port = jack_port_register (netj->client, buf,
  361. JACK_DEFAULT_AUDIO_TYPE,
  362. port_flags, 0);
  363. if (!port) {
  364. jack_error ("NET: cannot register port for %s", buf);
  365. break;
  366. }
  367. netj->playback_ports =
  368. jack_slist_append (netj->playback_ports, port);
  369. if( netj->bitdepth == CELT_MODE ) {
  370. #if HAVE_CELT
  371. #if HAVE_CELT_API_0_11
  372. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  373. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_decoder_create_custom( celt_mode, 1, NULL ) );
  374. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  375. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  376. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
  377. #else
  378. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  379. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode ) );
  380. #endif
  381. #endif
  382. } else {
  383. #if HAVE_SAMPLERATE
  384. netj->playback_srcs = jack_slist_append(netj->playback_srcs, src_new(SRC_LINEAR, 1, NULL));
  385. #endif
  386. }
  387. }
  388. for (chn = netj->playback_channels_audio; chn < netj->playback_channels; chn++) {
  389. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  390. port = jack_port_register (netj->client, buf,
  391. JACK_DEFAULT_MIDI_TYPE,
  392. port_flags, 0);
  393. if (!port) {
  394. jack_error ("NET: cannot register port for %s", buf);
  395. break;
  396. }
  397. netj->playback_ports =
  398. jack_slist_append (netj->playback_ports, port);
  399. }
  400. jack_activate (netj->client);
  401. }
  402. void netjack_detach( netjack_driver_state_t *netj )
  403. {
  404. JSList * node;
  405. for (node = netj->capture_ports; node; node = jack_slist_next (node))
  406. jack_port_unregister (netj->client,
  407. ((jack_port_t *) node->data));
  408. jack_slist_free (netj->capture_ports);
  409. netj->capture_ports = NULL;
  410. for (node = netj->capture_srcs; node; node = jack_slist_next (node))
  411. {
  412. #if HAVE_CELT
  413. if( netj->bitdepth == CELT_MODE )
  414. {
  415. CELTDecoder * decoder = node->data;
  416. celt_decoder_destroy(decoder);
  417. }
  418. else
  419. #endif
  420. {
  421. #if HAVE_SAMPLERATE
  422. SRC_STATE * src = node->data;
  423. src_delete(src);
  424. #endif
  425. }
  426. }
  427. jack_slist_free (netj->capture_srcs);
  428. netj->playback_srcs = NULL;
  429. for (node = netj->playback_ports; node; node = jack_slist_next (node))
  430. jack_port_unregister (netj->client,
  431. ((jack_port_t *) node->data));
  432. jack_slist_free (netj->playback_ports);
  433. netj->playback_ports = NULL;
  434. for (node = netj->playback_srcs; node; node = jack_slist_next (node))
  435. {
  436. #if HAVE_CELT
  437. if( netj->bitdepth == CELT_MODE )
  438. {
  439. CELTEncoder * encoder = node->data;
  440. celt_encoder_destroy(encoder);
  441. }
  442. else
  443. #endif
  444. {
  445. #if HAVE_SAMPLERATE
  446. SRC_STATE * src = node->data;
  447. src_delete(src);
  448. #endif
  449. }
  450. }
  451. jack_slist_free (netj->playback_srcs);
  452. netj->playback_srcs = NULL;
  453. #if HAVE_CELT
  454. if( netj->bitdepth == CELT_MODE )
  455. celt_mode_destroy(netj->celt_mode);
  456. #endif
  457. }
  458. netjack_driver_state_t *netjack_init (netjack_driver_state_t *netj,
  459. jack_client_t * client,
  460. const char *name,
  461. unsigned int capture_ports,
  462. unsigned int playback_ports,
  463. unsigned int capture_ports_midi,
  464. unsigned int playback_ports_midi,
  465. jack_nframes_t sample_rate,
  466. jack_nframes_t period_size,
  467. unsigned int listen_port,
  468. unsigned int transport_sync,
  469. unsigned int resample_factor,
  470. unsigned int resample_factor_up,
  471. unsigned int bitdepth,
  472. unsigned int use_autoconfig,
  473. unsigned int latency,
  474. unsigned int redundancy,
  475. int dont_htonl_floats,
  476. int always_deadline,
  477. int jitter_val )
  478. {
  479. // Fill in netj values.
  480. // might be subject to autoconfig...
  481. // so dont calculate anything with them...
  482. netj->sample_rate = sample_rate;
  483. netj->period_size = period_size;
  484. netj->dont_htonl_floats = dont_htonl_floats;
  485. netj->listen_port = listen_port;
  486. netj->capture_channels = capture_ports + capture_ports_midi;
  487. netj->capture_channels_audio = capture_ports;
  488. netj->capture_channels_midi = capture_ports_midi;
  489. netj->capture_ports = NULL;
  490. netj->playback_channels = playback_ports + playback_ports_midi;
  491. netj->playback_channels_audio = playback_ports;
  492. netj->playback_channels_midi = playback_ports_midi;
  493. netj->playback_ports = NULL;
  494. netj->codec_latency = 0;
  495. netj->handle_transport_sync = transport_sync;
  496. netj->mtu = 1400;
  497. netj->latency = latency;
  498. netj->redundancy = redundancy;
  499. netj->use_autoconfig = use_autoconfig;
  500. netj->always_deadline = always_deadline;
  501. netj->client = client;
  502. if ((bitdepth != 0) && (bitdepth != 8) && (bitdepth != 16) && (bitdepth != CELT_MODE))
  503. {
  504. jack_info ("Invalid bitdepth: %d (8, 16 or 0 for float) !!!", bitdepth);
  505. return NULL;
  506. }
  507. netj->bitdepth = bitdepth;
  508. if (resample_factor_up == 0)
  509. resample_factor_up = resample_factor;
  510. netj->resample_factor = resample_factor;
  511. netj->resample_factor_up = resample_factor_up;
  512. netj->jitter_val = jitter_val;
  513. return netj;
  514. }
  515. void netjack_release( netjack_driver_state_t *netj )
  516. {
  517. close( netj->sockfd );
  518. close( netj->outsockfd );
  519. packet_cache_free( netj->packcache );
  520. netj->packcache = NULL;
  521. }
  522. int
  523. netjack_startup( netjack_driver_state_t *netj )
  524. {
  525. int first_pack_len;
  526. struct sockaddr_in address;
  527. // Now open the socket, and wait for the first packet to arrive...
  528. netj->sockfd = socket (AF_INET, SOCK_DGRAM, 0);
  529. #ifdef WIN32
  530. if (netj->sockfd == INVALID_SOCKET)
  531. #else
  532. if (netj->sockfd == -1)
  533. #endif
  534. {
  535. jack_info ("socket error");
  536. return -1;
  537. }
  538. address.sin_family = AF_INET;
  539. address.sin_port = htons(netj->listen_port);
  540. address.sin_addr.s_addr = htonl(INADDR_ANY);
  541. if (bind (netj->sockfd, (struct sockaddr *) &address, sizeof (address)) < 0)
  542. {
  543. jack_info("bind error");
  544. return -1;
  545. }
  546. netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  547. #ifdef WIN32
  548. if (netj->outsockfd == INVALID_SOCKET)
  549. #else
  550. if (netj->outsockfd == -1)
  551. #endif
  552. {
  553. jack_info ("socket error");
  554. return -1;
  555. }
  556. netj->srcaddress_valid = 0;
  557. if (netj->use_autoconfig)
  558. {
  559. jacknet_packet_header *first_packet = alloca (sizeof (jacknet_packet_header));
  560. #ifdef WIN32
  561. int address_size = sizeof( struct sockaddr_in );
  562. #else
  563. socklen_t address_size = sizeof (struct sockaddr_in);
  564. #endif
  565. //jack_info ("Waiting for an incoming packet !!!");
  566. //jack_info ("*** IMPORTANT *** Dont connect a client to jackd until the driver is attached to a clock source !!!");
  567. while(1) {
  568. if( ! netjack_poll( netj->sockfd, 1000 ) ) {
  569. jack_info ("Waiting aborted");
  570. return -1;
  571. }
  572. first_pack_len = recvfrom (netj->sockfd, (char *)first_packet, sizeof (jacknet_packet_header), 0, (struct sockaddr*) & netj->syncsource_address, &address_size);
  573. #ifdef WIN32
  574. if( first_pack_len == -1 ) {
  575. first_pack_len = sizeof(jacknet_packet_header);
  576. break;
  577. }
  578. #else
  579. if (first_pack_len == sizeof (jacknet_packet_header))
  580. break;
  581. #endif
  582. }
  583. netj->srcaddress_valid = 1;
  584. if (first_pack_len == sizeof (jacknet_packet_header))
  585. {
  586. packet_header_ntoh (first_packet);
  587. jack_info ("AutoConfig Override !!!");
  588. if (netj->sample_rate != first_packet->sample_rate)
  589. {
  590. jack_info ("AutoConfig Override: Master JACK sample rate = %d", first_packet->sample_rate);
  591. netj->sample_rate = first_packet->sample_rate;
  592. }
  593. if (netj->period_size != first_packet->period_size)
  594. {
  595. jack_info ("AutoConfig Override: Master JACK period size is %d", first_packet->period_size);
  596. netj->period_size = first_packet->period_size;
  597. }
  598. if (netj->capture_channels_audio != first_packet->capture_channels_audio)
  599. {
  600. jack_info ("AutoConfig Override: capture_channels_audio = %d", first_packet->capture_channels_audio);
  601. netj->capture_channels_audio = first_packet->capture_channels_audio;
  602. }
  603. if (netj->capture_channels_midi != first_packet->capture_channels_midi)
  604. {
  605. jack_info ("AutoConfig Override: capture_channels_midi = %d", first_packet->capture_channels_midi);
  606. netj->capture_channels_midi = first_packet->capture_channels_midi;
  607. }
  608. if (netj->playback_channels_audio != first_packet->playback_channels_audio)
  609. {
  610. jack_info ("AutoConfig Override: playback_channels_audio = %d", first_packet->playback_channels_audio);
  611. netj->playback_channels_audio = first_packet->playback_channels_audio;
  612. }
  613. if (netj->playback_channels_midi != first_packet->playback_channels_midi)
  614. {
  615. jack_info ("AutoConfig Override: playback_channels_midi = %d", first_packet->playback_channels_midi);
  616. netj->playback_channels_midi = first_packet->playback_channels_midi;
  617. }
  618. netj->mtu = first_packet->mtu;
  619. jack_info ("MTU is set to %d bytes", first_packet->mtu);
  620. netj->latency = first_packet->latency;
  621. }
  622. }
  623. netj->capture_channels = netj->capture_channels_audio + netj->capture_channels_midi;
  624. netj->playback_channels = netj->playback_channels_audio + netj->playback_channels_midi;
  625. if( (netj->capture_channels * netj->period_size * netj->latency * 4) > 100000000 ) {
  626. jack_error( "autoconfig requests more than 100MB packet cache... bailing out" );
  627. exit(1);
  628. }
  629. if( netj->playback_channels > 1000 ) {
  630. jack_error( "autoconfig requests more than 1000 playback channels... bailing out" );
  631. exit(1);
  632. }
  633. if( netj->mtu < (2*sizeof( jacknet_packet_header )) ) {
  634. jack_error( "bullshit mtu requested by autoconfig" );
  635. exit(1);
  636. }
  637. if( netj->sample_rate == 0 ) {
  638. jack_error( "sample_rate 0 requested by autoconfig" );
  639. exit(1);
  640. }
  641. // After possible Autoconfig: do all calculations...
  642. netj->period_usecs =
  643. (jack_time_t) floor ((((float) netj->period_size) / (float)netj->sample_rate)
  644. * 1000000.0f);
  645. if( netj->latency == 0 )
  646. netj->deadline_offset = 50*netj->period_usecs;
  647. else
  648. netj->deadline_offset = netj->period_usecs + 10*netj->latency*netj->period_usecs/100;
  649. if( netj->bitdepth == CELT_MODE ) {
  650. // celt mode.
  651. // TODO: this is a hack. But i dont want to change the packet header.
  652. netj->resample_factor = (netj->resample_factor * netj->period_size * 1024 / netj->sample_rate / 8)&(~1);
  653. netj->resample_factor_up = (netj->resample_factor_up * netj->period_size * 1024 / netj->sample_rate / 8)&(~1);
  654. netj->net_period_down = netj->resample_factor;
  655. netj->net_period_up = netj->resample_factor_up;
  656. } else {
  657. netj->net_period_down = (float) netj->period_size / (float) netj->resample_factor;
  658. netj->net_period_up = (float) netj->period_size / (float) netj->resample_factor_up;
  659. }
  660. netj->rx_bufsize = sizeof (jacknet_packet_header) + netj->net_period_down * netj->capture_channels * get_sample_size (netj->bitdepth);
  661. netj->packcache = packet_cache_new (netj->latency + 50, netj->rx_bufsize, netj->mtu);
  662. netj->expected_framecnt_valid = 0;
  663. netj->num_lost_packets = 0;
  664. netj->next_deadline_valid = 0;
  665. netj->deadline_goodness = 0;
  666. netj->time_to_deadline = 0;
  667. // Special handling for latency=0
  668. if( netj->latency == 0 )
  669. netj->resync_threshold = 0;
  670. else
  671. netj->resync_threshold = MIN( 15, netj->latency-1 );
  672. netj->running_free = 0;
  673. return 0;
  674. }