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.

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