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.

759 lines
23KB

  1. /*
  2. NetJack Client
  3. Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
  4. Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  5. Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /** @file netsource.c
  19. *
  20. * @brief This client connects a remote slave JACK to a local JACK server assumed to be the master
  21. */
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifdef WIN32
  28. #include <winsock2.h>
  29. #include <malloc.h>
  30. #else
  31. #include <netinet/in.h>
  32. #include <netdb.h>
  33. #include <sys/socket.h>
  34. #endif
  35. /* These two required by FreeBSD. */
  36. #include <sys/types.h>
  37. #if HAVE_ALLOCA_H
  38. #include <alloca.h>
  39. #endif
  40. #include <jack/jack.h>
  41. //#include <net_driver.h>
  42. #include <netjack_packet.h>
  43. #if HAVE_SAMPLERATE
  44. #include <samplerate.h>
  45. #endif
  46. #if HAVE_CELT
  47. #include <celt/celt.h>
  48. #endif
  49. #include <math.h>
  50. JSList *capture_ports = NULL;
  51. JSList *capture_srcs = NULL;
  52. int capture_channels = 0;
  53. int capture_channels_audio = 2;
  54. int capture_channels_midi = 1;
  55. JSList *playback_ports = NULL;
  56. JSList *playback_srcs = NULL;
  57. int playback_channels = 0;
  58. int playback_channels_audio = 2;
  59. int playback_channels_midi = 1;
  60. int dont_htonl_floats = 0;
  61. int latency = 5;
  62. jack_nframes_t factor = 1;
  63. int bitdepth = 0;
  64. int mtu = 1400;
  65. int reply_port = 0;
  66. int redundancy = 1;
  67. jack_client_t *client;
  68. int state_connected = 0;
  69. int state_latency = 0;
  70. int state_netxruns = 0;
  71. int state_currentframe = 0;
  72. int state_recv_packet_queue_time = 0;
  73. int outsockfd;
  74. int insockfd;
  75. #ifdef WIN32
  76. struct sockaddr_in destaddr;
  77. struct sockaddr_in bindaddr;
  78. #else
  79. struct sockaddr destaddr;
  80. struct sockaddr bindaddr;
  81. #endif
  82. int sync_state;
  83. jack_transport_state_t last_transport_state;
  84. int framecnt = 0;
  85. int cont_miss = 0;
  86. int freewheeling = 0;
  87. /**
  88. * This Function allocates all the I/O Ports which are added the lists.
  89. */
  90. void
  91. alloc_ports (int n_capture_audio, int n_playback_audio, int n_capture_midi, int n_playback_midi)
  92. {
  93. int port_flags = JackPortIsOutput;
  94. int chn;
  95. jack_port_t *port;
  96. char buf[32];
  97. capture_ports = NULL;
  98. /* Allocate audio capture channels */
  99. for (chn = 0; chn < n_capture_audio; chn++)
  100. {
  101. snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
  102. port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
  103. if (!port)
  104. {
  105. printf( "jack_netsource: cannot register %s port\n", buf);
  106. break;
  107. }
  108. if( bitdepth == 1000 ) {
  109. #if HAVE_CELT
  110. #if HAVE_CELT_API_0_7
  111. CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
  112. capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode, 1, NULL ) );
  113. #else
  114. CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), 1, jack_get_buffer_size(client), NULL );
  115. capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode ) );
  116. #endif
  117. #endif
  118. } else {
  119. #if HAVE_SAMPLERATE
  120. capture_srcs = jack_slist_append (capture_srcs, src_new (SRC_LINEAR, 1, NULL));
  121. #endif
  122. }
  123. capture_ports = jack_slist_append (capture_ports, port);
  124. }
  125. /* Allocate midi capture channels */
  126. for (chn = n_capture_audio; chn < n_capture_midi + n_capture_audio; chn++)
  127. {
  128. snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
  129. port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
  130. if (!port)
  131. {
  132. printf ("jack_netsource: cannot register %s port\n", buf);
  133. break;
  134. }
  135. capture_ports = jack_slist_append(capture_ports, port);
  136. }
  137. /* Allocate audio playback channels */
  138. port_flags = JackPortIsInput;
  139. playback_ports = NULL;
  140. for (chn = 0; chn < n_playback_audio; chn++)
  141. {
  142. snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
  143. port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
  144. if (!port)
  145. {
  146. printf ("jack_netsource: cannot register %s port\n", buf);
  147. break;
  148. }
  149. if( bitdepth == 1000 ) {
  150. #if HAVE_CELT
  151. #if HAVE_CELT_API_0_7
  152. CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
  153. playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
  154. #else
  155. CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), 1, jack_get_buffer_size(client), NULL );
  156. playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode ) );
  157. #endif
  158. #endif
  159. } else {
  160. #if HAVE_SAMPLERATE
  161. playback_srcs = jack_slist_append (playback_srcs, src_new (SRC_LINEAR, 1, NULL));
  162. #endif
  163. }
  164. playback_ports = jack_slist_append (playback_ports, port);
  165. }
  166. /* Allocate midi playback channels */
  167. for (chn = n_playback_audio; chn < n_playback_midi + n_playback_audio; chn++)
  168. {
  169. snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
  170. port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
  171. if (!port)
  172. {
  173. printf ("jack_netsource: cannot register %s port\n", buf);
  174. break;
  175. }
  176. playback_ports = jack_slist_append (playback_ports, port);
  177. }
  178. }
  179. /**
  180. * The Sync callback... sync state is set elsewhere...
  181. * we will see if this is working correctly.
  182. * i dont really believe in it yet.
  183. */
  184. int
  185. sync_cb (jack_transport_state_t state, jack_position_t *pos, void *arg)
  186. {
  187. static int latency_count = 0;
  188. int retval = sync_state;
  189. if (latency_count) {
  190. latency_count--;
  191. retval = 0;
  192. }
  193. else if (state == JackTransportStarting && last_transport_state != JackTransportStarting)
  194. {
  195. retval = 0;
  196. latency_count = latency - 1;
  197. }
  198. last_transport_state = state;
  199. return retval;
  200. }
  201. void
  202. freewheel_cb (int starting, void *arg)
  203. {
  204. freewheeling = starting;
  205. }
  206. int deadline_goodness=0;
  207. /**
  208. * The process callback for this JACK application.
  209. * It is called by JACK at the appropriate times.
  210. */
  211. int
  212. process (jack_nframes_t nframes, void *arg)
  213. {
  214. jack_nframes_t net_period;
  215. int rx_bufsize, tx_bufsize;
  216. jack_default_audio_sample_t *buf;
  217. jack_port_t *port;
  218. JSList *node;
  219. int chn;
  220. int size, i;
  221. const char *porttype;
  222. int input_fd;
  223. jack_position_t local_trans_pos;
  224. uint32_t *packet_buf, *packet_bufX;
  225. uint32_t *rx_packet_ptr;
  226. jack_time_t packet_recv_timestamp;
  227. if( bitdepth == 1000 )
  228. net_period = factor;
  229. else
  230. net_period = (float) nframes / (float) factor;
  231. rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
  232. tx_bufsize = get_sample_size (bitdepth) * playback_channels * net_period + sizeof (jacknet_packet_header);
  233. /* Allocate a buffer where both In and Out Buffer will fit */
  234. packet_buf = alloca ((rx_bufsize > tx_bufsize) ? rx_bufsize : tx_bufsize);
  235. jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
  236. /*
  237. * for latency==0 we need to send out the packet before we wait on the reply.
  238. * but this introduces a cycle of latency, when netsource is connected to itself.
  239. * so we send out before read only in zero latency mode.
  240. *
  241. */
  242. if( latency == 0 ) {
  243. /* reset packet_bufX... */
  244. packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
  245. /* ---------- Send ---------- */
  246. render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
  247. packet_bufX, net_period, dont_htonl_floats);
  248. /* fill in packet hdr */
  249. pkthdr->transport_state = jack_transport_query (client, &local_trans_pos);
  250. pkthdr->transport_frame = local_trans_pos.frame;
  251. pkthdr->framecnt = framecnt;
  252. pkthdr->latency = latency;
  253. pkthdr->reply_port = reply_port;
  254. pkthdr->sample_rate = jack_get_sample_rate (client);
  255. pkthdr->period_size = nframes;
  256. /* playback for us is capture on the other side */
  257. pkthdr->capture_channels_audio = playback_channels_audio;
  258. pkthdr->playback_channels_audio = capture_channels_audio;
  259. pkthdr->capture_channels_midi = playback_channels_midi;
  260. pkthdr->playback_channels_midi = capture_channels_midi;
  261. pkthdr->mtu = mtu;
  262. if( freewheeling!= 0 )
  263. pkthdr->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
  264. else
  265. pkthdr->sync_state = (jack_nframes_t)deadline_goodness;
  266. //printf("goodness=%d\n", deadline_goodness );
  267. packet_header_hton (pkthdr);
  268. if (cont_miss < 3*latency+5) {
  269. int r;
  270. for( r=0; r<redundancy; r++ )
  271. netjack_sendto (outsockfd, (char *) packet_buf, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
  272. }
  273. else if (cont_miss > 50+5*latency)
  274. {
  275. state_connected = 0;
  276. packet_cache_reset_master_address( global_packcache );
  277. //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
  278. cont_miss = 0;
  279. }
  280. }
  281. /*
  282. * ok... now the RECEIVE code.
  283. *
  284. */
  285. /* reset packet_bufX... */
  286. packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
  287. if( reply_port )
  288. input_fd = insockfd;
  289. else
  290. input_fd = outsockfd;
  291. // for latency == 0 we can poll.
  292. if( (latency == 0) || (freewheeling!=0) ) {
  293. jack_time_t deadline = jack_get_time() + 1000000 * jack_get_buffer_size(client)/jack_get_sample_rate(client);
  294. // Now loop until we get the right packet.
  295. while(1) {
  296. jack_nframes_t got_frame;
  297. if ( ! netjack_poll_deadline( input_fd, deadline ) )
  298. break;
  299. packet_cache_drain_socket(global_packcache, input_fd);
  300. if (packet_cache_get_next_available_framecnt( global_packcache, framecnt - latency, &got_frame ))
  301. if( got_frame == (framecnt - latency) )
  302. break;
  303. }
  304. } else {
  305. // normally:
  306. // only drain socket.
  307. packet_cache_drain_socket(global_packcache, input_fd);
  308. }
  309. size = packet_cache_retreive_packet_pointer( global_packcache, framecnt - latency, (char**)&rx_packet_ptr, rx_bufsize, &packet_recv_timestamp );
  310. /* First alternative : we received what we expected. Render the data
  311. * to the JACK ports so it can be played. */
  312. if (size == rx_bufsize)
  313. {
  314. packet_buf = rx_packet_ptr;
  315. pkthdr = (jacknet_packet_header *) packet_buf;
  316. packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
  317. // calculate how much time there would have been, if this packet was sent at the deadline.
  318. int recv_time_offset = (int) (jack_get_time() - packet_recv_timestamp);
  319. packet_header_ntoh (pkthdr);
  320. deadline_goodness = recv_time_offset - (int)pkthdr->latency;
  321. //printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
  322. if (cont_miss)
  323. {
  324. //printf("Frame %d \tRecovered from dropouts\n", framecnt);
  325. cont_miss = 0;
  326. }
  327. render_payload_to_jack_ports (bitdepth, packet_bufX, net_period,
  328. capture_ports, capture_srcs, nframes, dont_htonl_floats);
  329. state_currentframe = framecnt;
  330. state_recv_packet_queue_time = recv_time_offset;
  331. state_connected = 1;
  332. sync_state = pkthdr->sync_state;
  333. packet_cache_release_packet( global_packcache, framecnt - latency );
  334. }
  335. /* Second alternative : we've received something that's not
  336. * as big as expected or we missed a packet. We render silence
  337. * to the ouput ports */
  338. else
  339. {
  340. jack_nframes_t latency_estimate;
  341. if( packet_cache_find_latency( global_packcache, framecnt, &latency_estimate ) )
  342. //if( (state_latency == 0) || (latency_estimate < state_latency) )
  343. state_latency = latency_estimate;
  344. // Set the counters up.
  345. state_currentframe = framecnt;
  346. //state_latency = framecnt - pkthdr->framecnt;
  347. state_netxruns += 1;
  348. //printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
  349. //printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
  350. cont_miss += 1;
  351. chn = 0;
  352. node = capture_ports;
  353. while (node != NULL)
  354. {
  355. port = (jack_port_t *) node->data;
  356. buf = jack_port_get_buffer (port, nframes);
  357. porttype = jack_port_type (port);
  358. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size ()) == 0)
  359. for (i = 0; i < nframes; i++)
  360. buf[i] = 0.0;
  361. else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size ()) == 0)
  362. jack_midi_clear_buffer (buf);
  363. node = jack_slist_next (node);
  364. chn++;
  365. }
  366. }
  367. if( latency != 0 ) {
  368. /* reset packet_bufX... */
  369. packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
  370. /* ---------- Send ---------- */
  371. render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
  372. packet_bufX, net_period, dont_htonl_floats);
  373. /* fill in packet hdr */
  374. pkthdr->transport_state = jack_transport_query (client, &local_trans_pos);
  375. pkthdr->transport_frame = local_trans_pos.frame;
  376. pkthdr->framecnt = framecnt;
  377. pkthdr->latency = latency;
  378. pkthdr->reply_port = reply_port;
  379. pkthdr->sample_rate = jack_get_sample_rate (client);
  380. pkthdr->period_size = nframes;
  381. /* playback for us is capture on the other side */
  382. pkthdr->capture_channels_audio = playback_channels_audio;
  383. pkthdr->playback_channels_audio = capture_channels_audio;
  384. pkthdr->capture_channels_midi = playback_channels_midi;
  385. pkthdr->playback_channels_midi = capture_channels_midi;
  386. pkthdr->mtu = mtu;
  387. if( freewheeling!= 0 )
  388. pkthdr->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
  389. else
  390. pkthdr->sync_state = (jack_nframes_t)deadline_goodness;
  391. //printf("goodness=%d\n", deadline_goodness );
  392. packet_header_hton (pkthdr);
  393. if (cont_miss < 3*latency+5) {
  394. int r;
  395. for( r=0; r<redundancy; r++ )
  396. netjack_sendto (outsockfd, (char *) packet_buf, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
  397. }
  398. else if (cont_miss > 50+5*latency)
  399. {
  400. state_connected = 0;
  401. packet_cache_reset_master_address( global_packcache );
  402. //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
  403. cont_miss = 0;
  404. }
  405. }
  406. framecnt++;
  407. return 0;
  408. }
  409. /**
  410. * This is the shutdown callback for this JACK application.
  411. * It is called by JACK if the server ever shuts down or
  412. * decides to disconnect the client.
  413. */
  414. void
  415. jack_shutdown (void *arg)
  416. {
  417. exit (1);
  418. }
  419. void
  420. init_sockaddr_in (struct sockaddr_in *name , const char *hostname , uint16_t port)
  421. {
  422. printf( "still here... \n" );
  423. fflush( stdout );
  424. name->sin_family = AF_INET ;
  425. name->sin_port = htons (port);
  426. if (hostname)
  427. {
  428. struct hostent *hostinfo = gethostbyname (hostname);
  429. if (hostinfo == NULL) {
  430. fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
  431. fflush( stderr );
  432. }
  433. #ifdef WIN32
  434. name->sin_addr.s_addr = inet_addr( hostname );
  435. #else
  436. name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
  437. #endif
  438. }
  439. else
  440. name->sin_addr.s_addr = htonl (INADDR_ANY) ;
  441. }
  442. void
  443. printUsage ()
  444. {
  445. fprintf (stderr, "usage: jack_netsource -h <host peer> [options]\n"
  446. "\n"
  447. " -n <jack name> - Reports a different name to jack\n"
  448. " -s <server name> - The name of the local jack server\n"
  449. " -h <host_peer> - Host name of the slave JACK\n"
  450. " -p <port> - UDP port used by the slave JACK\n"
  451. " -P <num channels> - Number of audio playback channels\n"
  452. " -C <num channels> - Number of audio capture channels\n"
  453. " -o <num channels> - Number of midi playback channels\n"
  454. " -i <num channels> - Number of midi capture channels\n"
  455. " -l <latency> - Network latency in number of NetJack frames\n"
  456. " -r <reply port> - Local UDP port to use\n"
  457. " -f <downsample ratio> - Downsample data in the wire by this factor\n"
  458. " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
  459. " -m <mtu> - Assume this mtu for the link\n"
  460. " -c <bytes> - Use Celt and encode <bytes> per channel and packet.\n"
  461. " -R <N> - Send out packets N times.\n"
  462. "\n");
  463. }
  464. int
  465. main (int argc, char *argv[])
  466. {
  467. /* Some startup related basics */
  468. char *client_name, *server_name = NULL, *peer_ip;
  469. int peer_port = 3000;
  470. jack_options_t options = JackNullOption;
  471. jack_status_t status;
  472. #ifdef WIN32
  473. WSADATA wsa;
  474. int rc = WSAStartup(MAKEWORD(2,0),&wsa);
  475. #endif
  476. /* Torben's famous state variables, aka "the reporting API" ! */
  477. /* heh ? these are only the copies of them ;) */
  478. int statecopy_connected, statecopy_latency, statecopy_netxruns;
  479. jack_nframes_t net_period;
  480. /* Argument parsing stuff */
  481. extern char *optarg;
  482. extern int optind, optopt;
  483. int errflg=0, c;
  484. if (argc < 3)
  485. {
  486. printUsage ();
  487. return 1;
  488. }
  489. client_name = (char *) malloc (sizeof (char) * 10);
  490. peer_ip = (char *) malloc (sizeof (char) * 10);
  491. sprintf(client_name, "netsource");
  492. sprintf(peer_ip, "localhost");
  493. while ((c = getopt (argc, argv, ":H:R:n:s:h:p:C:P:i:o:l:r:f:b:m:c:")) != -1)
  494. {
  495. switch (c)
  496. {
  497. case 'n':
  498. free(client_name);
  499. client_name = (char *) malloc (sizeof (char) * strlen (optarg)+1);
  500. strcpy (client_name, optarg);
  501. break;
  502. case 's':
  503. server_name = (char *) malloc (sizeof (char) * strlen (optarg)+1);
  504. strcpy (server_name, optarg);
  505. options |= JackServerName;
  506. break;
  507. case 'h':
  508. free(peer_ip);
  509. peer_ip = (char *) malloc (sizeof (char) * strlen (optarg)+1);
  510. strcpy (peer_ip, optarg);
  511. break;
  512. case 'p':
  513. peer_port = atoi (optarg);
  514. break;
  515. case 'P':
  516. playback_channels_audio = atoi (optarg);
  517. break;
  518. case 'C':
  519. capture_channels_audio = atoi (optarg);
  520. break;
  521. case 'o':
  522. playback_channels_midi = atoi (optarg);
  523. break;
  524. case 'i':
  525. capture_channels_midi = atoi (optarg);
  526. break;
  527. case 'l':
  528. latency = atoi (optarg);
  529. break;
  530. case 'r':
  531. reply_port = atoi (optarg);
  532. break;
  533. case 'f':
  534. factor = atoi (optarg);
  535. break;
  536. case 'b':
  537. bitdepth = atoi (optarg);
  538. break;
  539. case 'c':
  540. #if HAVE_CELT
  541. bitdepth = 1000;
  542. factor = atoi (optarg);
  543. #else
  544. printf( "not built with celt supprt\n" );
  545. exit(10);
  546. #endif
  547. break;
  548. case 'm':
  549. mtu = atoi (optarg);
  550. break;
  551. case 'R':
  552. redundancy = atoi (optarg);
  553. break;
  554. case 'H':
  555. dont_htonl_floats = atoi (optarg);
  556. break;
  557. case ':':
  558. fprintf (stderr, "Option -%c requires an operand\n", optopt);
  559. errflg++;
  560. break;
  561. case '?':
  562. fprintf (stderr, "Unrecognized option: -%c\n", optopt);
  563. errflg++;
  564. }
  565. }
  566. if (errflg)
  567. {
  568. printUsage ();
  569. exit (2);
  570. }
  571. capture_channels = capture_channels_audio + capture_channels_midi;
  572. playback_channels = playback_channels_audio + playback_channels_midi;
  573. outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  574. insockfd = socket (AF_INET, SOCK_DGRAM, 0);
  575. if( (outsockfd == -1) || (insockfd == -1) ) {
  576. fprintf (stderr, "cant open sockets\n" );
  577. return 1;
  578. }
  579. init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
  580. if(reply_port)
  581. {
  582. init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
  583. if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
  584. fprintf (stderr, "bind failure\n" );
  585. }
  586. }
  587. /* try to become a client of the JACK server */
  588. client = jack_client_open (client_name, options, &status, server_name);
  589. if (client == NULL)
  590. {
  591. fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
  592. "Is the JACK server running ?\n", status);
  593. return 1;
  594. }
  595. /* Set up jack callbacks */
  596. jack_set_process_callback (client, process, 0);
  597. jack_set_sync_callback (client, sync_cb, 0);
  598. jack_set_freewheel_callback (client, freewheel_cb, 0);
  599. jack_on_shutdown (client, jack_shutdown, 0);
  600. alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);
  601. if( bitdepth == 1000 )
  602. net_period = factor;
  603. else
  604. net_period = ceilf((float) jack_get_buffer_size (client) / (float) factor);
  605. int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
  606. global_packcache = packet_cache_new (latency + 50, rx_bufsize, mtu);
  607. /* tell the JACK server that we are ready to roll */
  608. if (jack_activate (client))
  609. {
  610. fprintf (stderr, "Cannot activate client");
  611. return 1;
  612. }
  613. /* Now sleep forever... and evaluate the state_ vars */
  614. statecopy_connected = 2; // make it report unconnected on start.
  615. statecopy_latency = state_latency;
  616. statecopy_netxruns = state_netxruns;
  617. while (1)
  618. {
  619. #ifdef WIN32
  620. Sleep (1000);
  621. #else
  622. sleep(1);
  623. #endif
  624. if (statecopy_connected != state_connected)
  625. {
  626. statecopy_connected = state_connected;
  627. if (statecopy_connected)
  628. {
  629. state_netxruns = 1; // We want to reset the netxrun count on each new connection
  630. printf ("Connected :-)\n");
  631. }
  632. else
  633. printf ("Not Connected\n");
  634. fflush(stdout);
  635. }
  636. if (statecopy_connected)
  637. {
  638. if (statecopy_netxruns != state_netxruns) {
  639. statecopy_netxruns = state_netxruns;
  640. printf ("%s: at frame %06d -> total netxruns %d (%d%%) queue time= %d\n",
  641. client_name,
  642. state_currentframe,
  643. statecopy_netxruns,
  644. 100*statecopy_netxruns/state_currentframe,
  645. state_recv_packet_queue_time);
  646. fflush(stdout);
  647. }
  648. }
  649. else
  650. {
  651. if (statecopy_latency != state_latency)
  652. {
  653. statecopy_latency = state_latency;
  654. if (statecopy_latency > 1)
  655. printf ("current latency %d\n", statecopy_latency);
  656. fflush(stdout);
  657. }
  658. }
  659. }
  660. /* Never reached. Well we will be a GtkApp someday... */
  661. packet_cache_free (global_packcache);
  662. jack_client_close (client);
  663. exit (0);
  664. }