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.

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