jack1 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.

707 lines
22KB

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