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.

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