jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

805 lines
25KB

  1. /* -*- mode: c; c-file-style: "linux"; -*- */
  2. /*
  3. NetJack Abstraction.
  4. Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  5. Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  6. Copyright (C) 2003 Robert Ham <rah@bash.sh>
  7. Copyright (C) 2001 Paul Davis
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. $Id: net_driver.c,v 1.17 2006/04/16 20:16:10 torbenh Exp $
  20. */
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <memory.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <stdarg.h>
  28. #include <jack/types.h>
  29. // for jack_error in jack1
  30. #include "internal.h"
  31. #include "jack/jslist.h"
  32. #include <sys/types.h>
  33. #ifdef WIN32
  34. #include <winsock.h>
  35. #include <malloc.h>
  36. #else
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #endif
  40. #include "config.h"
  41. #include "netjack.h"
  42. #if HAVE_SAMPLERATE
  43. #include <samplerate.h>
  44. #endif
  45. #if HAVE_CELT
  46. #include <celt/celt.h>
  47. #endif
  48. #include "netjack.h"
  49. #include "netjack_packet.h"
  50. // JACK2
  51. //#include "jack/control.h"
  52. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  53. static int sync_state = 1;
  54. static jack_transport_state_t last_transport_state;
  55. static int
  56. net_driver_sync_cb (jack_transport_state_t state, jack_position_t *pos, void *data)
  57. {
  58. int retval = sync_state;
  59. if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
  60. retval = 0;
  61. }
  62. // if (state == JackTransportStarting)
  63. // jack_info("Starting sync_state = %d", sync_state);
  64. last_transport_state = state;
  65. return retval;
  66. }
  67. int netjack_wait ( netjack_driver_state_t *netj, jack_time_t (*get_microseconds)(void) )
  68. {
  69. int we_have_the_expected_frame = 0;
  70. jack_nframes_t next_frame_avail;
  71. jack_time_t packet_recv_time_stamp;
  72. jacknet_packet_header *pkthdr;
  73. if ( !netj->next_deadline_valid ) {
  74. netj->next_deadline = get_microseconds () + netj->period_usecs;
  75. netj->next_deadline_valid = 1;
  76. }
  77. // Increment expected frame here.
  78. if ( netj->expected_framecnt_valid ) {
  79. netj->expected_framecnt += 1;
  80. } else {
  81. // starting up.... lets look into the packetcache, and fetch the highest packet.
  82. packet_cache_drain_socket ( netj->packcache, netj->sockfd, get_microseconds );
  83. if ( packet_cache_get_highest_available_framecnt ( netj->packcache, &next_frame_avail ) ) {
  84. netj->expected_framecnt = next_frame_avail;
  85. netj->expected_framecnt_valid = 1;
  86. } else {
  87. // no packets there... start normally.
  88. netj->expected_framecnt = 0;
  89. netj->expected_framecnt_valid = 1;
  90. }
  91. }
  92. //jack_log( "expect %d", netj->expected_framecnt );
  93. // Now check if required packet is already in the cache.
  94. // then poll (have deadline calculated)
  95. // then drain socket, rinse and repeat.
  96. while (1) {
  97. if ( packet_cache_get_next_available_framecnt ( netj->packcache, netj->expected_framecnt, &next_frame_avail) ) {
  98. if ( next_frame_avail == netj->expected_framecnt ) {
  99. we_have_the_expected_frame = 1;
  100. if ( !netj->always_deadline ) {
  101. break;
  102. }
  103. }
  104. }
  105. if ( !netjack_poll_deadline ( netj->sockfd, netj->next_deadline, get_microseconds ) ) {
  106. break;
  107. }
  108. packet_cache_drain_socket ( netj->packcache, netj->sockfd, get_microseconds );
  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. }
  117. // XXX: switching mode unconditionally is stupid.
  118. // if we were running free perhaps we like to behave differently
  119. // ie. fastforward one packet etc.
  120. // well... this is the first packet we see. hmm.... dunno ;S
  121. // it works... so...
  122. netj->running_free = 0;
  123. //if( !we_have_the_expected_frame )
  124. // jack_error( "netxrun... %d", netj->expected_framecnt );
  125. if ( we_have_the_expected_frame ) {
  126. jack_time_t now = get_microseconds ();
  127. if ( now < netj->next_deadline ) {
  128. netj->time_to_deadline = netj->next_deadline - now;
  129. } else {
  130. netj->time_to_deadline = 0;
  131. }
  132. packet_cache_retreive_packet_pointer ( netj->packcache, netj->expected_framecnt, (char**)&(netj->rx_buf), netj->rx_bufsize, &packet_recv_time_stamp);
  133. pkthdr = (jacknet_packet_header*)netj->rx_buf;
  134. packet_header_ntoh (pkthdr);
  135. netj->deadline_goodness = (int)pkthdr->sync_state;
  136. netj->packet_data_valid = 1;
  137. int want_deadline;
  138. if ( netj->jitter_val != 0 ) {
  139. want_deadline = netj->jitter_val;
  140. } else if ( netj->latency < 4 ) {
  141. want_deadline = -netj->period_usecs / 2;
  142. } else {
  143. want_deadline = (netj->period_usecs / 4 + 10 * (int)netj->period_usecs * netj->latency / 100);
  144. }
  145. if ( netj->deadline_goodness != MASTER_FREEWHEELS ) {
  146. if ( netj->deadline_goodness < want_deadline ) {
  147. netj->next_deadline -= netj->period_usecs / 100;
  148. //jack_log( "goodness: %d, Adjust deadline: --- %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  149. }
  150. if ( netj->deadline_goodness > want_deadline ) {
  151. netj->next_deadline += netj->period_usecs / 100;
  152. //jack_log( "goodness: %d, Adjust deadline: +++ %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  153. }
  154. }
  155. // if( netj->next_deadline < (netj->period_usecs*70/100) ) {
  156. // jack_error( "master is forcing deadline_offset to below 70%% of period_usecs... increase latency setting on master" );
  157. // netj->deadline_offset = (netj->period_usecs*90/100);
  158. // }
  159. netj->next_deadline += netj->period_usecs;
  160. } else {
  161. netj->time_to_deadline = 0;
  162. netj->next_deadline += netj->period_usecs;
  163. // bah... the packet is not there.
  164. // either
  165. // - it got lost.
  166. // - its late
  167. // - sync source is not sending anymore.
  168. // lets check if we have the next packets, we will just run a cycle without data.
  169. // in that case.
  170. if ( packet_cache_get_next_available_framecnt ( netj->packcache, netj->expected_framecnt, &next_frame_avail) ) {
  171. jack_nframes_t offset = next_frame_avail - netj->expected_framecnt;
  172. //XXX: hmm... i need to remember why resync_threshold wasnt right.
  173. //if( offset < netj->resync_threshold )
  174. if ( offset < 10 ) {
  175. // ok. dont do nothing. we will run without data.
  176. // this seems to be one or 2 lost packets.
  177. //
  178. // this can also be reordered packet jitter.
  179. // (maybe this is not happening in real live)
  180. // but it happens in netem.
  181. netj->packet_data_valid = 0;
  182. // I also found this happening, when the packet queue, is too full.
  183. // but wtf ? use a smaller latency. this link can handle that ;S
  184. if ( packet_cache_get_fill ( netj->packcache, netj->expected_framecnt ) > 80.0 ) {
  185. netj->next_deadline -= netj->period_usecs / 2;
  186. }
  187. } else {
  188. // the diff is too high. but we have a packet in the future.
  189. // lets resync.
  190. netj->expected_framecnt = next_frame_avail;
  191. packet_cache_retreive_packet_pointer ( netj->packcache, netj->expected_framecnt, (char**)&(netj->rx_buf), netj->rx_bufsize, NULL );
  192. pkthdr = (jacknet_packet_header*)netj->rx_buf;
  193. packet_header_ntoh (pkthdr);
  194. //netj->deadline_goodness = 0;
  195. netj->deadline_goodness = (int)pkthdr->sync_state - (int)netj->period_usecs * offset;
  196. netj->next_deadline_valid = 0;
  197. netj->packet_data_valid = 1;
  198. }
  199. } else {
  200. // no packets in buffer.
  201. netj->packet_data_valid = 0;
  202. //printf( "frame %d No Packet in queue. num_lost_packets = %d \n", netj->expected_framecnt, netj->num_lost_packets );
  203. if ( netj->num_lost_packets < 5 ) {
  204. // ok. No Packet in queue. The packet was either lost,
  205. // or we are running too fast.
  206. //
  207. // Adjusting the deadline unconditionally resulted in
  208. // too many xruns on master.
  209. // But we need to adjust for the case we are running too fast.
  210. // So lets check if the last packet is there now.
  211. //
  212. // It would not be in the queue anymore, if it had been
  213. // retrieved. This might break for redundancy, but
  214. // i will make the packet cache drop redundant packets,
  215. // that have already been retreived.
  216. //
  217. if ( packet_cache_get_highest_available_framecnt ( netj->packcache, &next_frame_avail) ) {
  218. if ( next_frame_avail == (netj->expected_framecnt - 1) ) {
  219. // Ok. the last packet is there now.
  220. // and it had not been retrieved.
  221. //
  222. // TODO: We are still dropping 2 packets.
  223. // perhaps we can adjust the deadline
  224. // when (num_packets lost == 0)
  225. // This might still be too much.
  226. netj->next_deadline += netj->period_usecs;
  227. }
  228. }
  229. } else if ( (netj->num_lost_packets <= 100) ) {
  230. // lets try adjusting the deadline harder, for some packets, we might have just ran 2 fast.
  231. netj->next_deadline += netj->period_usecs * netj->latency / 8;
  232. } else {
  233. // But now we can check for any new frame available.
  234. //
  235. if ( packet_cache_get_highest_available_framecnt ( netj->packcache, &next_frame_avail) ) {
  236. netj->expected_framecnt = next_frame_avail;
  237. packet_cache_retreive_packet_pointer ( netj->packcache, netj->expected_framecnt, (char**)&(netj->rx_buf), netj->rx_bufsize, NULL );
  238. pkthdr = (jacknet_packet_header*)netj->rx_buf;
  239. packet_header_ntoh (pkthdr);
  240. netj->deadline_goodness = pkthdr->sync_state;
  241. netj->next_deadline_valid = 0;
  242. netj->packet_data_valid = 1;
  243. netj->running_free = 0;
  244. jack_info ( "resync after freerun... %d", netj->expected_framecnt );
  245. } else {
  246. if ( netj->num_lost_packets == 101 ) {
  247. jack_info ( "master seems gone... entering freerun mode", netj->expected_framecnt );
  248. }
  249. netj->running_free = 1;
  250. // when we really dont see packets.
  251. // reset source address. and open possibility for new master.
  252. // maybe dsl reconnect. Also restart of netsource without fix
  253. // reply address changes port.
  254. if (netj->num_lost_packets > 200 ) {
  255. netj->srcaddress_valid = 0;
  256. packet_cache_reset_master_address ( netj->packcache );
  257. }
  258. }
  259. }
  260. }
  261. }
  262. int retval = 0;
  263. if ( !netj->packet_data_valid ) {
  264. netj->num_lost_packets += 1;
  265. if ( netj->num_lost_packets == 1 ) {
  266. retval = netj->period_usecs;
  267. }
  268. } else {
  269. if ( (netj->num_lost_packets > 1) && !netj->running_free ) {
  270. retval = (netj->num_lost_packets - 1) * netj->period_usecs;
  271. }
  272. netj->num_lost_packets = 0;
  273. }
  274. return retval;
  275. }
  276. void netjack_send_silence ( netjack_driver_state_t *netj, int syncstate )
  277. {
  278. int tx_size = get_sample_size (netj->bitdepth) * netj->playback_channels * netj->net_period_up + sizeof(jacknet_packet_header);
  279. unsigned int *packet_buf, *packet_bufX;
  280. packet_buf = alloca ( tx_size);
  281. jacknet_packet_header *tx_pkthdr = (jacknet_packet_header*)packet_buf;
  282. jacknet_packet_header *rx_pkthdr = (jacknet_packet_header*)netj->rx_buf;
  283. //framecnt = rx_pkthdr->framecnt;
  284. netj->reply_port = rx_pkthdr->reply_port;
  285. // offset packet_bufX by the packetheader.
  286. packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
  287. tx_pkthdr->sync_state = syncstate;
  288. tx_pkthdr->framecnt = netj->expected_framecnt;
  289. // memset 0 the payload.
  290. int payload_size = get_sample_size (netj->bitdepth) * netj->playback_channels * netj->net_period_up;
  291. memset (packet_bufX, 0, payload_size);
  292. packet_header_hton (tx_pkthdr);
  293. if (netj->srcaddress_valid) {
  294. int r;
  295. if (netj->reply_port) {
  296. netj->syncsource_address.sin_port = htons (netj->reply_port);
  297. }
  298. for ( r = 0; r < netj->redundancy; r++ )
  299. netjack_sendto (netj->outsockfd, (char*)packet_buf, tx_size,
  300. 0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
  301. }
  302. }
  303. void netjack_attach ( netjack_driver_state_t *netj )
  304. {
  305. //puts ("net_driver_attach");
  306. jack_port_t * port;
  307. char buf[32];
  308. unsigned int chn;
  309. int port_flags;
  310. if ( netj->bitdepth == CELT_MODE ) {
  311. #if HAVE_CELT
  312. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  313. celt_int32 lookahead;
  314. netj->celt_mode = celt_mode_create ( netj->sample_rate, netj->period_size, NULL );
  315. #else
  316. celt_int32_t lookahead;
  317. netj->celt_mode = celt_mode_create ( netj->sample_rate, 1, netj->period_size, NULL );
  318. #endif
  319. celt_mode_info ( netj->celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
  320. netj->codec_latency = 2 * lookahead;
  321. #endif
  322. }
  323. if (netj->handle_transport_sync) {
  324. jack_set_sync_callback (netj->client, (JackSyncCallback)net_driver_sync_cb, NULL);
  325. }
  326. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  327. for (chn = 0; chn < netj->capture_channels_audio; chn++) {
  328. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  329. port = jack_port_register (netj->client, buf,
  330. JACK_DEFAULT_AUDIO_TYPE,
  331. port_flags, 0);
  332. if (!port) {
  333. jack_error ("NET: cannot register port for %s", buf);
  334. break;
  335. }
  336. netj->capture_ports =
  337. jack_slist_append (netj->capture_ports, port);
  338. if ( netj->bitdepth == CELT_MODE ) {
  339. #if HAVE_CELT
  340. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  341. netj->capture_srcs = jack_slist_append (netj->capture_srcs, celt_decoder_create ( netj->celt_mode, 1, NULL ) );
  342. #else
  343. netj->capture_srcs = jack_slist_append (netj->capture_srcs, celt_decoder_create ( netj->celt_mode ) );
  344. #endif
  345. #endif
  346. } else {
  347. #if HAVE_SAMPLERATE
  348. netj->capture_srcs = jack_slist_append (netj->capture_srcs, src_new (SRC_LINEAR, 1, NULL));
  349. #endif
  350. }
  351. }
  352. for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
  353. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  354. port = jack_port_register (netj->client, buf,
  355. JACK_DEFAULT_MIDI_TYPE,
  356. port_flags, 0);
  357. if (!port) {
  358. jack_error ("NET: cannot register port for %s", buf);
  359. break;
  360. }
  361. netj->capture_ports =
  362. jack_slist_append (netj->capture_ports, port);
  363. }
  364. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  365. for (chn = 0; chn < netj->playback_channels_audio; chn++) {
  366. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  367. port = jack_port_register (netj->client, buf,
  368. JACK_DEFAULT_AUDIO_TYPE,
  369. port_flags, 0);
  370. if (!port) {
  371. jack_error ("NET: cannot register port for %s", buf);
  372. break;
  373. }
  374. netj->playback_ports =
  375. jack_slist_append (netj->playback_ports, port);
  376. if ( netj->bitdepth == CELT_MODE ) {
  377. #if HAVE_CELT
  378. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  379. CELTMode *celt_mode = celt_mode_create ( netj->sample_rate, netj->period_size, NULL );
  380. netj->playback_srcs = jack_slist_append (netj->playback_srcs, celt_encoder_create ( celt_mode, 1, NULL ) );
  381. #else
  382. CELTMode *celt_mode = celt_mode_create ( netj->sample_rate, 1, netj->period_size, NULL );
  383. netj->playback_srcs = jack_slist_append (netj->playback_srcs, celt_encoder_create ( celt_mode ) );
  384. #endif
  385. #endif
  386. } else {
  387. #if HAVE_SAMPLERATE
  388. netj->playback_srcs = jack_slist_append (netj->playback_srcs, src_new (SRC_LINEAR, 1, NULL));
  389. #endif
  390. }
  391. }
  392. for (chn = netj->playback_channels_audio; chn < netj->playback_channels; chn++) {
  393. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  394. port = jack_port_register (netj->client, buf,
  395. JACK_DEFAULT_MIDI_TYPE,
  396. port_flags, 0);
  397. if (!port) {
  398. jack_error ("NET: cannot register port for %s", buf);
  399. break;
  400. }
  401. netj->playback_ports =
  402. jack_slist_append (netj->playback_ports, port);
  403. }
  404. jack_activate (netj->client);
  405. }
  406. void netjack_detach ( netjack_driver_state_t *netj )
  407. {
  408. JSList * node;
  409. for (node = netj->capture_ports; node; node = jack_slist_next (node))
  410. jack_port_unregister (netj->client,
  411. ((jack_port_t*)node->data));
  412. jack_slist_free (netj->capture_ports);
  413. netj->capture_ports = NULL;
  414. for (node = netj->capture_srcs; node; node = jack_slist_next (node)) {
  415. #if HAVE_CELT
  416. if ( netj->bitdepth == CELT_MODE ) {
  417. CELTDecoder * decoder = node->data;
  418. celt_decoder_destroy (decoder);
  419. } else
  420. #endif
  421. {
  422. #if HAVE_SAMPLERATE
  423. SRC_STATE * src = node->data;
  424. src_delete (src);
  425. #endif
  426. }
  427. }
  428. jack_slist_free (netj->capture_srcs);
  429. netj->playback_srcs = NULL;
  430. for (node = netj->playback_ports; node; node = jack_slist_next (node))
  431. jack_port_unregister (netj->client,
  432. ((jack_port_t*)node->data));
  433. jack_slist_free (netj->playback_ports);
  434. netj->playback_ports = NULL;
  435. for (node = netj->playback_srcs; node; node = jack_slist_next (node)) {
  436. #if HAVE_CELT
  437. if ( netj->bitdepth == CELT_MODE ) {
  438. CELTEncoder * encoder = node->data;
  439. celt_encoder_destroy (encoder);
  440. } else
  441. #endif
  442. {
  443. #if HAVE_SAMPLERATE
  444. SRC_STATE * src = node->data;
  445. src_delete (src);
  446. #endif
  447. }
  448. }
  449. jack_slist_free (netj->playback_srcs);
  450. netj->playback_srcs = NULL;
  451. #if HAVE_CELT
  452. if ( netj->bitdepth == CELT_MODE ) {
  453. celt_mode_destroy (netj->celt_mode);
  454. }
  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. jack_info ("Invalid bitdepth: %d (8, 16 or 0 for float) !!!", bitdepth);
  503. return NULL;
  504. }
  505. netj->bitdepth = bitdepth;
  506. if (resample_factor_up == 0) {
  507. resample_factor_up = resample_factor;
  508. }
  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. jack_info ("bind error");
  542. return -1;
  543. }
  544. netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  545. #ifdef WIN32
  546. if (netj->outsockfd == INVALID_SOCKET)
  547. #else
  548. if (netj->outsockfd == -1)
  549. #endif
  550. {
  551. jack_info ("socket error");
  552. return -1;
  553. }
  554. netj->srcaddress_valid = 0;
  555. if (netj->use_autoconfig) {
  556. jacknet_packet_header *first_packet = alloca (sizeof(jacknet_packet_header));
  557. #ifdef WIN32
  558. int address_size = sizeof( struct sockaddr_in );
  559. #else
  560. socklen_t address_size = sizeof(struct sockaddr_in);
  561. #endif
  562. //jack_info ("Waiting for an incoming packet !!!");
  563. //jack_info ("*** IMPORTANT *** Dont connect a client to jackd until the driver is attached to a clock source !!!");
  564. while (1) {
  565. if ( !netjack_poll ( netj->sockfd, 1000 ) ) {
  566. jack_info ("Waiting aborted");
  567. return -1;
  568. }
  569. first_pack_len = recvfrom (netj->sockfd, (char*)first_packet, sizeof(jacknet_packet_header), 0, (struct sockaddr*)&netj->syncsource_address, &address_size);
  570. #ifdef WIN32
  571. if ( first_pack_len == -1 ) {
  572. first_pack_len = sizeof(jacknet_packet_header);
  573. break;
  574. }
  575. #else
  576. if (first_pack_len == sizeof(jacknet_packet_header)) {
  577. break;
  578. }
  579. #endif
  580. }
  581. netj->srcaddress_valid = 1;
  582. if (first_pack_len == sizeof(jacknet_packet_header)) {
  583. packet_header_ntoh (first_packet);
  584. jack_info ("AutoConfig Override !!!");
  585. if (netj->sample_rate != first_packet->sample_rate) {
  586. jack_info ("AutoConfig Override: Master JACK sample rate = %d", first_packet->sample_rate);
  587. netj->sample_rate = first_packet->sample_rate;
  588. }
  589. if (netj->period_size != first_packet->period_size) {
  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. jack_info ("AutoConfig Override: capture_channels_audio = %d", first_packet->capture_channels_audio);
  595. netj->capture_channels_audio = first_packet->capture_channels_audio;
  596. }
  597. if (netj->capture_channels_midi != first_packet->capture_channels_midi) {
  598. jack_info ("AutoConfig Override: capture_channels_midi = %d", first_packet->capture_channels_midi);
  599. netj->capture_channels_midi = first_packet->capture_channels_midi;
  600. }
  601. if (netj->playback_channels_audio != first_packet->playback_channels_audio) {
  602. jack_info ("AutoConfig Override: playback_channels_audio = %d", first_packet->playback_channels_audio);
  603. netj->playback_channels_audio = first_packet->playback_channels_audio;
  604. }
  605. if (netj->playback_channels_midi != first_packet->playback_channels_midi) {
  606. jack_info ("AutoConfig Override: playback_channels_midi = %d", first_packet->playback_channels_midi);
  607. netj->playback_channels_midi = first_packet->playback_channels_midi;
  608. }
  609. netj->mtu = first_packet->mtu;
  610. jack_info ("MTU is set to %d bytes", first_packet->mtu);
  611. netj->latency = first_packet->latency;
  612. }
  613. }
  614. netj->capture_channels = netj->capture_channels_audio + netj->capture_channels_midi;
  615. netj->playback_channels = netj->playback_channels_audio + netj->playback_channels_midi;
  616. if ( (netj->capture_channels * netj->period_size * netj->latency * 4) > 100000000 ) {
  617. jack_error ( "autoconfig requests more than 100MB packet cache... bailing out" );
  618. exit (1);
  619. }
  620. if ( netj->playback_channels > 1000 ) {
  621. jack_error ( "autoconfig requests more than 1000 playback channels... bailing out" );
  622. exit (1);
  623. }
  624. if ( netj->mtu < (2 * sizeof( jacknet_packet_header )) ) {
  625. jack_error ( "bullshit mtu requested by autoconfig" );
  626. exit (1);
  627. }
  628. if ( netj->sample_rate == 0 ) {
  629. jack_error ( "sample_rate 0 requested by autoconfig" );
  630. exit (1);
  631. }
  632. // After possible Autoconfig: do all calculations...
  633. netj->period_usecs =
  634. (jack_time_t)floor ((((float)netj->period_size) / (float)netj->sample_rate)
  635. * 1000000.0f);
  636. if ( netj->latency == 0 ) {
  637. netj->deadline_offset = 50 * netj->period_usecs;
  638. } else {
  639. netj->deadline_offset = netj->period_usecs + 10 * netj->latency * netj->period_usecs / 100;
  640. }
  641. if ( netj->bitdepth == CELT_MODE ) {
  642. // celt mode.
  643. // TODO: this is a hack. But i dont want to change the packet header.
  644. netj->resample_factor = (netj->resample_factor * netj->period_size * 1024 / netj->sample_rate / 8) & (~1);
  645. netj->resample_factor_up = (netj->resample_factor_up * netj->period_size * 1024 / netj->sample_rate / 8) & (~1);
  646. netj->net_period_down = netj->resample_factor;
  647. netj->net_period_up = netj->resample_factor_up;
  648. } else {
  649. netj->net_period_down = (float)netj->period_size / (float)netj->resample_factor;
  650. netj->net_period_up = (float)netj->period_size / (float)netj->resample_factor_up;
  651. }
  652. netj->rx_bufsize = sizeof(jacknet_packet_header) + netj->net_period_down * netj->capture_channels * get_sample_size(netj->bitdepth);
  653. netj->packcache = packet_cache_new (netj->latency + 50, netj->rx_bufsize, netj->mtu);
  654. netj->expected_framecnt_valid = 0;
  655. netj->num_lost_packets = 0;
  656. netj->next_deadline_valid = 0;
  657. netj->deadline_goodness = 0;
  658. netj->time_to_deadline = 0;
  659. // Special handling for latency=0
  660. if ( netj->latency == 0 ) {
  661. netj->resync_threshold = 0;
  662. } else {
  663. netj->resync_threshold = MIN ( 15, netj->latency - 1 );
  664. }
  665. netj->running_free = 0;
  666. return 0;
  667. }