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.

791 lines
25KB

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