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.

770 lines
23KB

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