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.

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