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.

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