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.

758 lines
26KB

  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 <netjack_packet.h>
  44. #if HAVE_SAMPLERATE
  45. #include <samplerate.h>
  46. #endif
  47. #if HAVE_CELT
  48. #include <celt/celt.h>
  49. #endif
  50. #include <math.h>
  51. JSList *capture_ports = NULL;
  52. JSList *capture_srcs = NULL;
  53. int capture_channels = 0;
  54. int capture_channels_audio = 2;
  55. int capture_channels_midi = 1;
  56. JSList *playback_ports = NULL;
  57. JSList *playback_srcs = NULL;
  58. int playback_channels = 0;
  59. int playback_channels_audio = 2;
  60. int playback_channels_midi = 1;
  61. int dont_htonl_floats = 0;
  62. int latency = 5;
  63. jack_nframes_t factor = 1;
  64. int bitdepth = 0;
  65. int mtu = 1400;
  66. int reply_port = 0;
  67. int bind_port = 0;
  68. int redundancy = 1;
  69. jack_client_t *client;
  70. packet_cache * packcache = 0;
  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. snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
  105. port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
  106. if (!port) {
  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_11
  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_custom( celt_mode, 1, NULL ) );
  115. #elif 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. 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. printf ("jack_netsource: cannot register %s port\n", buf);
  136. break;
  137. }
  138. capture_ports = jack_slist_append(capture_ports, port);
  139. }
  140. /* Allocate audio playback channels */
  141. port_flags = JackPortIsInput;
  142. playback_ports = NULL;
  143. for (chn = 0; chn < n_playback_audio; chn++) {
  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. printf ("jack_netsource: cannot register %s port\n", buf);
  148. break;
  149. }
  150. if( bitdepth == 1000 ) {
  151. #if HAVE_CELT
  152. #if HAVE_CELT_API_0_11
  153. CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
  154. playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create_custom( celt_mode, 1, NULL ) );
  155. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  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. snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
  173. port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
  174. if (!port) {
  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. 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_tx, *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_tx = alloca (tx_bufsize);
  239. jacknet_packet_header *pkthdr_tx = (jacknet_packet_header *) packet_buf_tx;
  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_tx + 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_tx->transport_state = jack_transport_query (client, &local_trans_pos);
  254. pkthdr_tx->transport_frame = local_trans_pos.frame;
  255. pkthdr_tx->framecnt = framecnt;
  256. pkthdr_tx->latency = latency;
  257. pkthdr_tx->reply_port = reply_port;
  258. pkthdr_tx->sample_rate = jack_get_sample_rate (client);
  259. pkthdr_tx->period_size = nframes;
  260. /* playback for us is capture on the other side */
  261. pkthdr_tx->capture_channels_audio = playback_channels_audio;
  262. pkthdr_tx->playback_channels_audio = capture_channels_audio;
  263. pkthdr_tx->capture_channels_midi = playback_channels_midi;
  264. pkthdr_tx->playback_channels_midi = capture_channels_midi;
  265. pkthdr_tx->mtu = mtu;
  266. if( freewheeling != 0 )
  267. pkthdr_tx->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
  268. else
  269. pkthdr_tx->sync_state = (jack_nframes_t)deadline_goodness;
  270. //printf("goodness=%d\n", deadline_goodness );
  271. packet_header_hton (pkthdr_tx);
  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, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
  276. } else if (cont_miss > 50 + 5 * latency) {
  277. state_connected = 0;
  278. packet_cache_reset_master_address( packcache );
  279. //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
  280. cont_miss = 0;
  281. }
  282. }
  283. /*
  284. * ok... now the RECEIVE code.
  285. *
  286. */
  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(packcache, input_fd);
  300. if (packet_cache_get_next_available_framecnt( 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(packcache, input_fd);
  308. }
  309. size = packet_cache_retreive_packet_pointer( 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. uint32_t *packet_buf_rx = rx_packet_ptr;
  314. jacknet_packet_header *pkthdr_rx = (jacknet_packet_header *) packet_buf_rx;
  315. packet_bufX = packet_buf_rx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
  316. // calculate how much time there would have been, if this packet was sent at the deadline.
  317. int recv_time_offset = (int) (jack_get_time() - packet_recv_timestamp);
  318. packet_header_ntoh (pkthdr_rx);
  319. deadline_goodness = recv_time_offset - (int)pkthdr_rx->latency;
  320. //printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
  321. if (cont_miss) {
  322. //printf("Frame %d \tRecovered from dropouts\n", framecnt);
  323. cont_miss = 0;
  324. }
  325. render_payload_to_jack_ports (bitdepth, packet_bufX, net_period,
  326. capture_ports, capture_srcs, nframes, dont_htonl_floats);
  327. state_currentframe = framecnt;
  328. state_recv_packet_queue_time = recv_time_offset;
  329. state_connected = 1;
  330. sync_state = pkthdr_rx->sync_state;
  331. packet_cache_release_packet( packcache, framecnt - latency );
  332. }
  333. /* Second alternative : we've received something that's not
  334. * as big as expected or we missed a packet. We render silence
  335. * to the ouput ports */
  336. else {
  337. jack_nframes_t latency_estimate;
  338. if( packet_cache_find_latency( packcache, framecnt, &latency_estimate ) )
  339. //if( (state_latency == 0) || (latency_estimate < state_latency) )
  340. state_latency = latency_estimate;
  341. // Set the counters up.
  342. state_currentframe = framecnt;
  343. //state_latency = framecnt - pkthdr->framecnt;
  344. state_netxruns += 1;
  345. //printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
  346. //printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
  347. cont_miss += 1;
  348. chn = 0;
  349. node = capture_ports;
  350. while (node != NULL) {
  351. port = (jack_port_t *) node->data;
  352. buf = jack_port_get_buffer (port, nframes);
  353. porttype = jack_port_type (port);
  354. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size ()) == 0)
  355. for (i = 0; i < nframes; i++)
  356. buf[i] = 0.0;
  357. else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size ()) == 0)
  358. jack_midi_clear_buffer (buf);
  359. node = jack_slist_next (node);
  360. chn++;
  361. }
  362. }
  363. if (latency != 0) {
  364. /* reset packet_bufX... */
  365. packet_bufX = packet_buf_tx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
  366. /* ---------- Send ---------- */
  367. render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes,
  368. packet_bufX, net_period, dont_htonl_floats);
  369. /* fill in packet hdr */
  370. pkthdr_tx->transport_state = jack_transport_query (client, &local_trans_pos);
  371. pkthdr_tx->transport_frame = local_trans_pos.frame;
  372. pkthdr_tx->framecnt = framecnt;
  373. pkthdr_tx->latency = latency;
  374. pkthdr_tx->reply_port = reply_port;
  375. pkthdr_tx->sample_rate = jack_get_sample_rate (client);
  376. pkthdr_tx->period_size = nframes;
  377. /* playback for us is capture on the other side */
  378. pkthdr_tx->capture_channels_audio = playback_channels_audio;
  379. pkthdr_tx->playback_channels_audio = capture_channels_audio;
  380. pkthdr_tx->capture_channels_midi = playback_channels_midi;
  381. pkthdr_tx->playback_channels_midi = capture_channels_midi;
  382. pkthdr_tx->mtu = mtu;
  383. if( freewheeling != 0 )
  384. pkthdr_tx->sync_state = (jack_nframes_t)MASTER_FREEWHEELS;
  385. else
  386. pkthdr_tx->sync_state = (jack_nframes_t)deadline_goodness;
  387. //printf("goodness=%d\n", deadline_goodness );
  388. packet_header_hton (pkthdr_tx);
  389. if (cont_miss < 3 * latency + 5) {
  390. int r;
  391. for( r = 0; r < redundancy; r++ )
  392. netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
  393. } else if (cont_miss > 50 + 5 * latency) {
  394. state_connected = 0;
  395. packet_cache_reset_master_address( packcache );
  396. //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
  397. cont_miss = 0;
  398. }
  399. }
  400. framecnt++;
  401. return 0;
  402. }
  403. /**
  404. * This is the shutdown callback for this JACK application.
  405. * It is called by JACK if the server ever shuts down or
  406. * decides to disconnect the client.
  407. */
  408. void
  409. jack_shutdown (void *arg)
  410. {
  411. fprintf(stderr, "JACK shut down, exiting ...\n");
  412. exit (1);
  413. }
  414. void
  415. init_sockaddr_in (struct sockaddr_in *name , const char *hostname , uint16_t port)
  416. {
  417. name->sin_family = AF_INET ;
  418. name->sin_port = htons (port);
  419. if (hostname) {
  420. struct hostent *hostinfo = gethostbyname (hostname);
  421. if (hostinfo == NULL) {
  422. fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
  423. fflush( stderr );
  424. }
  425. #ifdef WIN32
  426. name->sin_addr.s_addr = inet_addr( hostname );
  427. #else
  428. name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
  429. #endif
  430. } else
  431. name->sin_addr.s_addr = htonl (INADDR_ANY) ;
  432. }
  433. void
  434. printUsage ()
  435. {
  436. fprintf (stderr, "usage: jack_netsource [options]\n"
  437. "\n"
  438. " -h this help text\n"
  439. " -H <slave host> - Host name of the slave JACK\n"
  440. " -o <num channels> - Number of audio playback channels\n"
  441. " -i <num channels> - Number of audio capture channels\n"
  442. " -O <num channels> - Number of midi playback channels\n"
  443. " -I <num channels> - Number of midi capture channels\n"
  444. " -n <periods> - Network latency in JACK periods\n"
  445. " -p <port> - UDP port that the slave is listening on\n"
  446. " -r <reply port> - UDP port that we are listening on\n"
  447. " -B <bind port> - reply port, for use in NAT environments\n"
  448. " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
  449. " -c <kbits> - Use CELT encoding with <kbits> kbits per channel\n"
  450. " -m <mtu> - Assume this mtu for the link\n"
  451. " -R <N> - Redundancy: send out packets N times.\n"
  452. " -e - skip host-to-network endianness conversion\n"
  453. " -N <jack name> - Reports a different name to jack\n"
  454. " -s <server name> - The name of the local jack server\n"
  455. "\n");
  456. }
  457. void
  458. sigterm_handler( int signal )
  459. {
  460. quit = 1;
  461. }
  462. int
  463. main (int argc, char *argv[])
  464. {
  465. /* Some startup related basics */
  466. char *client_name, *server_name = NULL, *peer_ip;
  467. int peer_port = 3000;
  468. jack_options_t options = JackNullOption;
  469. jack_status_t status;
  470. #ifdef WIN32
  471. WSADATA wsa;
  472. int rc = WSAStartup(MAKEWORD(2, 0), &wsa);
  473. #endif
  474. /* Torben's famous state variables, aka "the reporting API" ! */
  475. /* heh ? these are only the copies of them ;) */
  476. int statecopy_connected, statecopy_latency, statecopy_netxruns;
  477. jack_nframes_t net_period;
  478. /* Argument parsing stuff */
  479. extern char *optarg;
  480. extern int optind, optopt;
  481. int errflg = 0, c;
  482. if (argc < 3) {
  483. printUsage ();
  484. return 1;
  485. }
  486. client_name = (char *) malloc (sizeof (char) * 10);
  487. peer_ip = (char *) malloc (sizeof (char) * 10);
  488. sprintf(client_name, "netjack");
  489. sprintf(peer_ip, "localhost");
  490. while ((c = getopt (argc, argv, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:")) != -1) {
  491. switch (c) {
  492. case 'h':
  493. printUsage();
  494. exit (0);
  495. break;
  496. case 'H':
  497. free(peer_ip);
  498. peer_ip = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
  499. strcpy (peer_ip, optarg);
  500. break;
  501. case 'o':
  502. playback_channels_audio = atoi (optarg);
  503. break;
  504. case 'i':
  505. capture_channels_audio = atoi (optarg);
  506. break;
  507. case 'O':
  508. playback_channels_midi = atoi (optarg);
  509. break;
  510. case 'I':
  511. capture_channels_midi = atoi (optarg);
  512. break;
  513. case 'n':
  514. latency = atoi (optarg);
  515. break;
  516. case 'p':
  517. peer_port = atoi (optarg);
  518. break;
  519. case 'r':
  520. reply_port = atoi (optarg);
  521. break;
  522. case 'B':
  523. bind_port = atoi (optarg);
  524. break;
  525. case 'f':
  526. factor = atoi (optarg);
  527. printf("This feature is deprecated and will be removed in future netjack versions. CELT offers a superiour way to conserve bandwidth");
  528. break;
  529. case 'b':
  530. bitdepth = atoi (optarg);
  531. break;
  532. case 'c':
  533. #if HAVE_CELT
  534. bitdepth = 1000;
  535. factor = atoi (optarg);
  536. #else
  537. printf( "not built with celt support\n" );
  538. exit(10);
  539. #endif
  540. break;
  541. case 'm':
  542. mtu = atoi (optarg);
  543. break;
  544. case 'R':
  545. redundancy = atoi (optarg);
  546. break;
  547. case 'e':
  548. dont_htonl_floats = 1;
  549. break;
  550. case 'N':
  551. free(client_name);
  552. client_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
  553. strcpy (client_name, optarg);
  554. break;
  555. case 's':
  556. server_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
  557. strcpy (server_name, optarg);
  558. options |= JackServerName;
  559. break;
  560. case ':':
  561. fprintf (stderr, "Option -%c requires an operand\n", optopt);
  562. errflg++;
  563. break;
  564. case '?':
  565. fprintf (stderr, "Unrecognized option: -%c\n", optopt);
  566. errflg++;
  567. }
  568. }
  569. if (errflg) {
  570. printUsage ();
  571. exit (2);
  572. }
  573. capture_channels = capture_channels_audio + capture_channels_midi;
  574. playback_channels = playback_channels_audio + playback_channels_midi;
  575. outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  576. insockfd = socket (AF_INET, SOCK_DGRAM, 0);
  577. if ((outsockfd == -1) || (insockfd == -1)) {
  578. fprintf (stderr, "cant open sockets\n" );
  579. return 1;
  580. }
  581. init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
  582. if (bind_port) {
  583. init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
  584. if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
  585. fprintf (stderr, "bind failure\n" );
  586. }
  587. }
  588. if (reply_port) {
  589. init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
  590. if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
  591. fprintf (stderr, "bind failure\n" );
  592. }
  593. }
  594. /* try to become a client of the JACK server */
  595. client = jack_client_open (client_name, options, &status, server_name);
  596. if (client == NULL) {
  597. fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
  598. "Is the JACK server running ?\n", status);
  599. return 1;
  600. }
  601. /* Set up jack callbacks */
  602. jack_set_process_callback (client, process, 0);
  603. jack_set_sync_callback (client, sync_cb, 0);
  604. jack_set_freewheel_callback (client, freewheel_cb, 0);
  605. jack_on_shutdown (client, jack_shutdown, 0);
  606. alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);
  607. if( bitdepth == 1000 )
  608. net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8) & (~1) ;
  609. else
  610. net_period = ceilf((float) jack_get_buffer_size (client) / (float) factor);
  611. int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
  612. packcache = packet_cache_new (latency + 50, rx_bufsize, mtu);
  613. /* tell the JACK server that we are ready to roll */
  614. if (jack_activate (client)) {
  615. fprintf (stderr, "Cannot activate client");
  616. return 1;
  617. }
  618. /* Now sleep forever... and evaluate the state_ vars */
  619. signal( SIGTERM, sigterm_handler );
  620. signal( SIGINT, sigterm_handler );
  621. statecopy_connected = 2; // make it report unconnected on start.
  622. statecopy_latency = state_latency;
  623. statecopy_netxruns = state_netxruns;
  624. while ( !quit ) {
  625. #ifdef WIN32
  626. Sleep (1000);
  627. #else
  628. sleep(1);
  629. #endif
  630. if (statecopy_connected != state_connected) {
  631. statecopy_connected = state_connected;
  632. if (statecopy_connected) {
  633. state_netxruns = 1; // We want to reset the netxrun count on each new connection
  634. printf ("Connected :-)\n");
  635. } else
  636. printf ("Not Connected\n");
  637. fflush(stdout);
  638. }
  639. if (statecopy_connected) {
  640. if (statecopy_netxruns != state_netxruns) {
  641. statecopy_netxruns = state_netxruns;
  642. printf ("%s: at frame %06d -> total netxruns %d (%d%%) queue time= %d\n",
  643. client_name,
  644. state_currentframe,
  645. statecopy_netxruns,
  646. 100 * statecopy_netxruns / state_currentframe,
  647. state_recv_packet_queue_time);
  648. fflush(stdout);
  649. }
  650. } else {
  651. if (statecopy_latency != state_latency) {
  652. statecopy_latency = state_latency;
  653. if (statecopy_latency > 1)
  654. printf ("current latency %d\n", statecopy_latency);
  655. fflush(stdout);
  656. }
  657. }
  658. }
  659. jack_client_close (client);
  660. packet_cache_free (packcache);
  661. exit (0);
  662. }