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.

782 lines
24KB

  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. #include <signal.h>
  28. #ifdef WIN32
  29. #include <winsock2.h>
  30. #define socklen_t int
  31. #include <malloc.h>
  32. #else
  33. #include <netinet/in.h>
  34. #include <netdb.h>
  35. #include <sys/socket.h>
  36. #endif
  37. /* These two required by FreeBSD. */
  38. #include <sys/types.h>
  39. #include <jack/jack.h>
  40. //#include <net_driver.h>
  41. #include <netjack_packet.h>
  42. #if HAVE_SAMPLERATE
  43. #include <samplerate.h>
  44. #endif
  45. #if HAVE_CELT
  46. #include <celt/celt.h>
  47. #endif
  48. #include <math.h>
  49. JSList *capture_ports = NULL;
  50. JSList *capture_srcs = NULL;
  51. int capture_channels = 0;
  52. int capture_channels_audio = 2;
  53. int capture_channels_midi = 1;
  54. JSList *playback_ports = NULL;
  55. JSList *playback_srcs = NULL;
  56. int playback_channels = 0;
  57. int playback_channels_audio = 2;
  58. int playback_channels_midi = 1;
  59. int dont_htonl_floats = 0;
  60. int latency = 5;
  61. jack_nframes_t factor = 1;
  62. int bitdepth = 0;
  63. int mtu = 1400;
  64. int reply_port = 0;
  65. int bind_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 quit=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 * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8)&(~1) ;
  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. name->sin_family = AF_INET ;
  424. name->sin_port = htons (port);
  425. if (hostname)
  426. {
  427. struct hostent *hostinfo = gethostbyname (hostname);
  428. if (hostinfo == NULL) {
  429. fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
  430. fflush( stderr );
  431. }
  432. #ifdef WIN32
  433. name->sin_addr.s_addr = inet_addr( hostname );
  434. #else
  435. name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
  436. #endif
  437. }
  438. else
  439. name->sin_addr.s_addr = htonl (INADDR_ANY) ;
  440. }
  441. void
  442. printUsage ()
  443. {
  444. fprintf (stderr, "usage: jack_netsource [options]\n"
  445. "\n"
  446. " -h this help text\n"
  447. " -H <slave host> - Host name of the slave JACK\n"
  448. " -o <num channels> - Number of audio playback channels\n"
  449. " -i <num channels> - Number of audio capture channels\n"
  450. " -O <num channels> - Number of midi playback channels\n"
  451. " -I <num channels> - Number of midi capture channels\n"
  452. " -n <periods> - Network latency in JACK periods\n"
  453. " -p <port> - UDP port that the slave is listening on\n"
  454. " -r <reply port> - UDP port that we are listening on\n"
  455. " -B <bind port> - reply port, for use in NAT environments\n"
  456. " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
  457. " -c <kbits> - Use CELT encoding with <kbits> kbits per channel\n"
  458. " -m <mtu> - Assume this mtu for the link\n"
  459. " -R <N> - Redundancy: send out packets N times.\n"
  460. " -e - skip host-to-network endianness conversion\n"
  461. " -N <jack name> - Reports a different name to jack\n"
  462. " -s <server name> - The name of the local jack server\n"
  463. "\n");
  464. }
  465. void
  466. sigterm_handler( int signal )
  467. {
  468. quit = 1;
  469. }
  470. int
  471. main (int argc, char *argv[])
  472. {
  473. /* Some startup related basics */
  474. char *client_name, *server_name = NULL, *peer_ip;
  475. int peer_port = 3000;
  476. jack_options_t options = JackNullOption;
  477. jack_status_t status;
  478. #ifdef WIN32
  479. WSADATA wsa;
  480. int rc = WSAStartup(MAKEWORD(2,0),&wsa);
  481. #endif
  482. /* Torben's famous state variables, aka "the reporting API" ! */
  483. /* heh ? these are only the copies of them ;) */
  484. int statecopy_connected, statecopy_latency, statecopy_netxruns;
  485. jack_nframes_t net_period;
  486. /* Argument parsing stuff */
  487. extern char *optarg;
  488. extern int optind, optopt;
  489. int errflg=0, c;
  490. if (argc < 3)
  491. {
  492. printUsage ();
  493. return 1;
  494. }
  495. client_name = (char *) malloc (sizeof (char) * 10);
  496. peer_ip = (char *) malloc (sizeof (char) * 10);
  497. sprintf(client_name, "netjack");
  498. sprintf(peer_ip, "localhost");
  499. while ((c = getopt (argc, argv, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:")) != -1)
  500. {
  501. switch (c)
  502. {
  503. case 'h':
  504. printUsage();
  505. exit (0);
  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 'o':
  513. playback_channels_audio = atoi (optarg);
  514. break;
  515. case 'i':
  516. capture_channels_audio = atoi (optarg);
  517. break;
  518. case 'O':
  519. playback_channels_midi = atoi (optarg);
  520. break;
  521. case 'I':
  522. capture_channels_midi = atoi (optarg);
  523. break;
  524. case 'n':
  525. latency = atoi (optarg);
  526. break;
  527. case 'p':
  528. peer_port = atoi (optarg);
  529. break;
  530. case 'r':
  531. reply_port = atoi (optarg);
  532. break;
  533. case 'B':
  534. bind_port = atoi (optarg);
  535. break;
  536. case 'f':
  537. factor = atoi (optarg);
  538. printf("This feature is deprecated and will be removed in future netjack versions. CELT offers a superiour way to conserve bandwidth");
  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 'e':
  559. dont_htonl_floats = 1;
  560. break;
  561. case 'N':
  562. free(client_name);
  563. client_name = (char *) malloc (sizeof (char) * strlen (optarg)+1);
  564. strcpy (client_name, optarg);
  565. break;
  566. case 's':
  567. server_name = (char *) malloc (sizeof (char) * strlen (optarg)+1);
  568. strcpy (server_name, optarg);
  569. options |= JackServerName;
  570. break;
  571. case ':':
  572. fprintf (stderr, "Option -%c requires an operand\n", optopt);
  573. errflg++;
  574. break;
  575. case '?':
  576. fprintf (stderr, "Unrecognized option: -%c\n", optopt);
  577. errflg++;
  578. }
  579. }
  580. if (errflg)
  581. {
  582. printUsage ();
  583. exit (2);
  584. }
  585. capture_channels = capture_channels_audio + capture_channels_midi;
  586. playback_channels = playback_channels_audio + playback_channels_midi;
  587. outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  588. insockfd = socket (AF_INET, SOCK_DGRAM, 0);
  589. if( (outsockfd == -1) || (insockfd == -1) ) {
  590. fprintf (stderr, "cant open sockets\n" );
  591. return 1;
  592. }
  593. init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
  594. if(bind_port) {
  595. init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
  596. if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
  597. fprintf (stderr, "bind failure\n" );
  598. }
  599. }
  600. if(reply_port)
  601. {
  602. init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
  603. if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
  604. fprintf (stderr, "bind failure\n" );
  605. }
  606. }
  607. /* try to become a client of the JACK server */
  608. client = jack_client_open (client_name, options, &status, server_name);
  609. if (client == NULL)
  610. {
  611. fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
  612. "Is the JACK server running ?\n", status);
  613. return 1;
  614. }
  615. /* Set up jack callbacks */
  616. jack_set_process_callback (client, process, 0);
  617. jack_set_sync_callback (client, sync_cb, 0);
  618. jack_set_freewheel_callback (client, freewheel_cb, 0);
  619. jack_on_shutdown (client, jack_shutdown, 0);
  620. alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);
  621. if( bitdepth == 1000 )
  622. net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8)&(~1) ;
  623. else
  624. net_period = ceilf((float) jack_get_buffer_size (client) / (float) factor);
  625. int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
  626. global_packcache = packet_cache_new (latency + 50, rx_bufsize, mtu);
  627. /* tell the JACK server that we are ready to roll */
  628. if (jack_activate (client))
  629. {
  630. fprintf (stderr, "Cannot activate client");
  631. return 1;
  632. }
  633. /* Now sleep forever... and evaluate the state_ vars */
  634. signal( SIGTERM, sigterm_handler );
  635. signal( SIGINT, sigterm_handler );
  636. statecopy_connected = 2; // make it report unconnected on start.
  637. statecopy_latency = state_latency;
  638. statecopy_netxruns = state_netxruns;
  639. while ( !quit )
  640. {
  641. #ifdef WIN32
  642. Sleep (1000);
  643. #else
  644. sleep(1);
  645. #endif
  646. if (statecopy_connected != state_connected)
  647. {
  648. statecopy_connected = state_connected;
  649. if (statecopy_connected)
  650. {
  651. state_netxruns = 1; // We want to reset the netxrun count on each new connection
  652. printf ("Connected :-)\n");
  653. }
  654. else
  655. printf ("Not Connected\n");
  656. fflush(stdout);
  657. }
  658. if (statecopy_connected)
  659. {
  660. if (statecopy_netxruns != state_netxruns) {
  661. statecopy_netxruns = state_netxruns;
  662. printf ("%s: at frame %06d -> total netxruns %d (%d%%) queue time= %d\n",
  663. client_name,
  664. state_currentframe,
  665. statecopy_netxruns,
  666. 100*statecopy_netxruns/state_currentframe,
  667. state_recv_packet_queue_time);
  668. fflush(stdout);
  669. }
  670. }
  671. else
  672. {
  673. if (statecopy_latency != state_latency)
  674. {
  675. statecopy_latency = state_latency;
  676. if (statecopy_latency > 1)
  677. printf ("current latency %d\n", statecopy_latency);
  678. fflush(stdout);
  679. }
  680. }
  681. }
  682. jack_client_close (client);
  683. packet_cache_free (global_packcache);
  684. exit (0);
  685. }