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.

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