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.

799 lines
28KB

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