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.

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