JACK tools
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.

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