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.

805 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 "JackError.h"
  46. #include "netjack.h"
  47. #include "netjack_packet.h"
  48. #define MIN(x,y) ((x)<(y) ? (x) : (y))
  49. static int sync_state = 1;
  50. static jack_transport_state_t last_transport_state;
  51. static int
  52. net_driver_sync_cb(jack_transport_state_t state, jack_position_t *pos, void *data)
  53. {
  54. int retval = sync_state;
  55. if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
  56. retval = 0;
  57. }
  58. // if (state == JackTransportStarting)
  59. // jack_info("Starting sync_state = %d", sync_state);
  60. last_transport_state = state;
  61. return retval;
  62. }
  63. int netjack_wait( netjack_driver_state_t *netj )
  64. {
  65. int we_have_the_expected_frame = 0;
  66. jack_nframes_t next_frame_avail;
  67. jack_time_t packet_recv_time_stamp;
  68. jacknet_packet_header *pkthdr;
  69. if( !netj->next_deadline_valid ) {
  70. netj->next_deadline = jack_get_time() + netj->period_usecs;
  71. netj->next_deadline_valid = 1;
  72. }
  73. // Increment expected frame here.
  74. if( netj->expected_framecnt_valid ) {
  75. netj->expected_framecnt += 1;
  76. } else {
  77. // starting up.... lets look into the packetcache, and fetch the highest packet.
  78. packet_cache_drain_socket( netj->packcache, netj->sockfd );
  79. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail ) ) {
  80. netj->expected_framecnt = next_frame_avail;
  81. netj->expected_framecnt_valid = 1;
  82. } else {
  83. // no packets there... start normally.
  84. netj->expected_framecnt = 0;
  85. netj->expected_framecnt_valid = 1;
  86. }
  87. }
  88. //jack_log( "expect %d", netj->expected_framecnt );
  89. // Now check if required packet is already in the cache.
  90. // then poll (have deadline calculated)
  91. // then drain socket, rinse and repeat.
  92. while(1) {
  93. if( packet_cache_get_next_available_framecnt( netj->packcache, netj->expected_framecnt, &next_frame_avail) ) {
  94. if( next_frame_avail == netj->expected_framecnt ) {
  95. we_have_the_expected_frame = 1;
  96. if( !netj->always_deadline )
  97. break;
  98. }
  99. }
  100. if( ! netjack_poll_deadline( netj->sockfd, netj->next_deadline ) ) {
  101. break;
  102. }
  103. packet_cache_drain_socket( netj->packcache, netj->sockfd );
  104. }
  105. // check if we know who to send our packets too.
  106. if (!netj->srcaddress_valid)
  107. if( netj->packcache->master_address_valid ) {
  108. memcpy (&(netj->syncsource_address), &(netj->packcache->master_address), sizeof( struct sockaddr_in ) );
  109. netj->srcaddress_valid = 1;
  110. }
  111. // XXX: switching mode unconditionally is stupid.
  112. // if we were running free perhaps we like to behave differently
  113. // ie. fastforward one packet etc.
  114. // well... this is the first packet we see. hmm.... dunno ;S
  115. // it works... so...
  116. netj->running_free = 0;
  117. //if( !we_have_the_expected_frame )
  118. // jack_error( "netxrun... %d", netj->expected_framecnt );
  119. if( we_have_the_expected_frame ) {
  120. jack_time_t now = jack_get_time();
  121. if( now < netj->next_deadline )
  122. netj->time_to_deadline = netj->next_deadline - now;
  123. else
  124. netj->time_to_deadline = 0;
  125. packet_cache_retreive_packet_pointer( netj->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. int want_deadline;
  131. if( netj->jitter_val != 0 )
  132. want_deadline = netj->jitter_val;
  133. else if( netj->latency < 4 )
  134. want_deadline = -netj->period_usecs/2;
  135. else
  136. want_deadline = (netj->period_usecs/4+10*(int)netj->period_usecs*netj->latency/100);
  137. if( netj->deadline_goodness != MASTER_FREEWHEELS ) {
  138. if( netj->deadline_goodness < want_deadline ) {
  139. netj->next_deadline -= netj->period_usecs/100;
  140. //jack_log( "goodness: %d, Adjust deadline: --- %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  141. }
  142. if( netj->deadline_goodness > want_deadline ) {
  143. netj->next_deadline += netj->period_usecs/100;
  144. //jack_log( "goodness: %d, Adjust deadline: +++ %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  145. }
  146. }
  147. // if( netj->next_deadline < (netj->period_usecs*70/100) ) {
  148. // jack_error( "master is forcing deadline_offset to below 70%% of period_usecs... increase latency setting on master" );
  149. // netj->deadline_offset = (netj->period_usecs*90/100);
  150. // }
  151. netj->next_deadline += netj->period_usecs;
  152. } else {
  153. netj->time_to_deadline = 0;
  154. netj->next_deadline += netj->period_usecs;
  155. // bah... the packet is not there.
  156. // either
  157. // - it got lost.
  158. // - its late
  159. // - sync source is not sending anymore.
  160. // lets check if we have the next packets, we will just run a cycle without data.
  161. // in that case.
  162. if( packet_cache_get_next_available_framecnt( netj->packcache, netj->expected_framecnt, &next_frame_avail) )
  163. {
  164. jack_nframes_t offset = next_frame_avail - netj->expected_framecnt;
  165. //XXX: hmm... i need to remember why resync_threshold wasnt right.
  166. //if( offset < netj->resync_threshold )
  167. if( offset < 10 ) {
  168. // ok. dont do nothing. we will run without data.
  169. // this seems to be one or 2 lost packets.
  170. //
  171. // this can also be reordered packet jitter.
  172. // (maybe this is not happening in real live)
  173. // but it happens in netem.
  174. netj->packet_data_valid = 0;
  175. // I also found this happening, when the packet queue, is too full.
  176. // but wtf ? use a smaller latency. this link can handle that ;S
  177. if( packet_cache_get_fill( netj->packcache, netj->expected_framecnt ) > 80.0 )
  178. netj->next_deadline -= netj->period_usecs/2;
  179. } else {
  180. // the diff is too high. but we have a packet in the future.
  181. // lets resync.
  182. netj->expected_framecnt = next_frame_avail;
  183. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
  184. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  185. packet_header_ntoh(pkthdr);
  186. //netj->deadline_goodness = 0;
  187. netj->deadline_goodness = (int)pkthdr->sync_state - (int)netj->period_usecs * offset;
  188. netj->next_deadline_valid = 0;
  189. netj->packet_data_valid = 1;
  190. }
  191. } else {
  192. // no packets in buffer.
  193. netj->packet_data_valid = 0;
  194. //printf( "frame %d No Packet in queue. num_lost_packets = %d \n", netj->expected_framecnt, netj->num_lost_packets );
  195. if( netj->num_lost_packets < 5 ) {
  196. // ok. No Packet in queue. The packet was either lost,
  197. // or we are running too fast.
  198. //
  199. // Adjusting the deadline unconditionally resulted in
  200. // too many xruns on master.
  201. // But we need to adjust for the case we are running too fast.
  202. // So lets check if the last packet is there now.
  203. //
  204. // It would not be in the queue anymore, if it had been
  205. // retrieved. This might break for redundancy, but
  206. // i will make the packet cache drop redundant packets,
  207. // that have already been retreived.
  208. //
  209. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail) ) {
  210. if( next_frame_avail == (netj->expected_framecnt - 1) ) {
  211. // Ok. the last packet is there now.
  212. // and it had not been retrieved.
  213. //
  214. // TODO: We are still dropping 2 packets.
  215. // perhaps we can adjust the deadline
  216. // when (num_packets lost == 0)
  217. // This might still be too much.
  218. netj->next_deadline += netj->period_usecs;
  219. }
  220. }
  221. } else if( (netj->num_lost_packets <= 100) ) {
  222. // lets try adjusting the deadline harder, for some packets, we might have just ran 2 fast.
  223. netj->next_deadline += netj->period_usecs*netj->latency/8;
  224. } else {
  225. // But now we can check for any new frame available.
  226. //
  227. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail) ) {
  228. netj->expected_framecnt = next_frame_avail;
  229. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) &(netj->rx_buf), netj->rx_bufsize, NULL );
  230. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  231. packet_header_ntoh(pkthdr);
  232. netj->deadline_goodness = pkthdr->sync_state;
  233. netj->next_deadline_valid = 0;
  234. netj->packet_data_valid = 1;
  235. netj->running_free = 0;
  236. jack_info( "resync after freerun... %d", netj->expected_framecnt );
  237. } else {
  238. if( netj->num_lost_packets == 101 ) {
  239. jack_info( "master seems gone... entering freerun mode", netj->expected_framecnt );
  240. }
  241. netj->running_free = 1;
  242. // when we really dont see packets.
  243. // reset source address. and open possibility for new master.
  244. // maybe dsl reconnect. Also restart of netsource without fix
  245. // reply address changes port.
  246. if (netj->num_lost_packets > 200 ) {
  247. netj->srcaddress_valid = 0;
  248. packet_cache_reset_master_address( netj->packcache );
  249. }
  250. }
  251. }
  252. }
  253. }
  254. int retval = 0;
  255. if( !netj->packet_data_valid ) {
  256. netj->num_lost_packets += 1;
  257. if( netj->num_lost_packets == 1 )
  258. retval = netj->period_usecs;
  259. } else {
  260. if( (netj->num_lost_packets>1) && !netj->running_free )
  261. retval = (netj->num_lost_packets-1) * netj->period_usecs;
  262. netj->num_lost_packets = 0;
  263. }
  264. return retval;
  265. }
  266. void netjack_send_silence( netjack_driver_state_t *netj, int syncstate )
  267. {
  268. int tx_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up + sizeof(jacknet_packet_header);
  269. unsigned int *packet_buf, *packet_bufX;
  270. packet_buf = alloca( tx_size);
  271. jacknet_packet_header *tx_pkthdr = (jacknet_packet_header *)packet_buf;
  272. jacknet_packet_header *rx_pkthdr = (jacknet_packet_header *)netj->rx_buf;
  273. //framecnt = rx_pkthdr->framecnt;
  274. netj->reply_port = rx_pkthdr->reply_port;
  275. // offset packet_bufX by the packetheader.
  276. packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
  277. tx_pkthdr->sync_state = syncstate;
  278. tx_pkthdr->framecnt = netj->expected_framecnt;
  279. // memset 0 the payload.
  280. int payload_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up;
  281. memset(packet_bufX, 0, payload_size);
  282. packet_header_hton(tx_pkthdr);
  283. if (netj->srcaddress_valid)
  284. {
  285. int r;
  286. if (netj->reply_port)
  287. netj->syncsource_address.sin_port = htons(netj->reply_port);
  288. for( r=0; r<netj->redundancy; r++ )
  289. netjack_sendto(netj->outsockfd, (char *)packet_buf, tx_size,
  290. 0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
  291. }
  292. }
  293. void netjack_attach( netjack_driver_state_t *netj )
  294. {
  295. //puts ("net_driver_attach");
  296. jack_port_t * port;
  297. char buf[32];
  298. unsigned int chn;
  299. int port_flags;
  300. if( netj->bitdepth == CELT_MODE )
  301. {
  302. #if HAVE_CELT
  303. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  304. celt_int32 lookahead;
  305. netj->celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  306. #else
  307. celt_int32_t lookahead;
  308. netj->celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  309. #endif
  310. celt_mode_info( netj->celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
  311. netj->codec_latency = 2*lookahead;
  312. #endif
  313. }
  314. if (netj->handle_transport_sync)
  315. jack_set_sync_callback(netj->client, (JackSyncCallback) net_driver_sync_cb, NULL);
  316. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  317. for (chn = 0; chn < netj->capture_channels_audio; chn++) {
  318. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  319. port = jack_port_register (netj->client, buf,
  320. JACK_DEFAULT_AUDIO_TYPE,
  321. port_flags, 0);
  322. if (!port) {
  323. jack_error ("NET: cannot register port for %s", buf);
  324. break;
  325. }
  326. netj->capture_ports =
  327. jack_slist_append (netj->capture_ports, port);
  328. if( netj->bitdepth == CELT_MODE ) {
  329. #if HAVE_CELT
  330. #if HAVE_CELT_API_0_11
  331. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create_custom( netj->celt_mode, 1, NULL ) );
  332. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  333. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode, 1, NULL ) );
  334. #else
  335. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode ) );
  336. #endif
  337. #endif
  338. } else {
  339. #if HAVE_SAMPLERATE
  340. netj->capture_srcs = jack_slist_append(netj->capture_srcs, src_new(SRC_LINEAR, 1, NULL));
  341. #endif
  342. }
  343. }
  344. for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
  345. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  346. port = jack_port_register (netj->client, buf,
  347. JACK_DEFAULT_MIDI_TYPE,
  348. port_flags, 0);
  349. if (!port) {
  350. jack_error ("NET: cannot register port for %s", buf);
  351. break;
  352. }
  353. netj->capture_ports =
  354. jack_slist_append (netj->capture_ports, port);
  355. }
  356. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  357. for (chn = 0; chn < netj->playback_channels_audio; chn++) {
  358. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  359. port = jack_port_register (netj->client, buf,
  360. JACK_DEFAULT_AUDIO_TYPE,
  361. port_flags, 0);
  362. if (!port) {
  363. jack_error ("NET: cannot register port for %s", buf);
  364. break;
  365. }
  366. netj->playback_ports =
  367. jack_slist_append (netj->playback_ports, port);
  368. if( netj->bitdepth == CELT_MODE ) {
  369. #if HAVE_CELT
  370. #if HAVE_CELT_API_0_11
  371. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  372. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_decoder_create_custom( celt_mode, 1, NULL ) );
  373. #elif 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. }