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.

574 lines
18KB

  1. /*
  2. NetJack Client
  3. Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
  4. Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  5. Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /** @file netsource.c
  19. *
  20. * @brief This client connects a remote slave JACK to a local JACK server assumed to be the master
  21. */
  22. #include "config.h"
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <netinet/in.h>
  29. #include <netdb.h>
  30. /* These two required by FreeBSD. */
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #if HAVE_ALLOCA_H
  34. #include <alloca.h>
  35. #endif
  36. #include <jack/jack.h>
  37. #include <net_driver.h>
  38. #include <netjack_packet.h>
  39. #include <samplerate.h>
  40. #ifdef HAVE_CELT
  41. #include <celt/celt.h>
  42. #endif
  43. JSList *capture_ports = NULL;
  44. JSList *capture_srcs = NULL;
  45. int capture_channels = 0;
  46. int capture_channels_audio = 2;
  47. int capture_channels_midi = 1;
  48. JSList *playback_ports = NULL;
  49. JSList *playback_srcs = NULL;
  50. int playback_channels = 0;
  51. int playback_channels_audio = 2;
  52. int playback_channels_midi = 1;
  53. int latency = 5;
  54. jack_nframes_t factor = 1;
  55. int bitdepth = 0;
  56. int mtu = 1400;
  57. int reply_port = 0;
  58. jack_client_t *client;
  59. int state_connected = 0;
  60. int state_latency = 0;
  61. int state_netxruns = 0;
  62. int state_currentframe = 0;
  63. int outsockfd;
  64. int insockfd;
  65. struct sockaddr destaddr;
  66. struct sockaddr bindaddr;
  67. int sync_state;
  68. jack_transport_state_t last_transport_state;
  69. int framecnt = 0;
  70. int cont_miss = 0;
  71. /**
  72. * This Function allocates all the I/O Ports which are added the lists.
  73. */
  74. void
  75. alloc_ports (int n_capture_audio, int n_playback_audio, int n_capture_midi, int n_playback_midi)
  76. {
  77. int port_flags = JackPortIsOutput;
  78. int chn;
  79. jack_port_t *port;
  80. char buf[32];
  81. capture_ports = NULL;
  82. /* Allocate audio capture channels */
  83. for (chn = 0; chn < n_capture_audio; chn++)
  84. {
  85. snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
  86. port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
  87. if (!port)
  88. {
  89. printf( "jack_netsource: cannot register %s port\n", buf);
  90. break;
  91. }
  92. if( bitdepth == 1000 ) {
  93. #ifdef HAVE_CELT
  94. // XXX: memory leak
  95. CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), 1, jack_get_buffer_size(client), NULL );
  96. capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode ) );
  97. #endif
  98. } else {
  99. capture_srcs = jack_slist_append (capture_srcs, src_new (SRC_LINEAR, 1, NULL));
  100. }
  101. capture_ports = jack_slist_append (capture_ports, port);
  102. }
  103. /* Allocate midi capture channels */
  104. for (chn = n_capture_audio; chn < n_capture_midi + n_capture_audio; chn++)
  105. {
  106. snprintf (buf, sizeof (buf) - 1, "capture_%u", chn + 1);
  107. port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
  108. if (!port)
  109. {
  110. printf ("jack_netsource: cannot register %s port\n", buf);
  111. break;
  112. }
  113. capture_ports = jack_slist_append(capture_ports, port);
  114. }
  115. /* Allocate audio playback channels */
  116. port_flags = JackPortIsInput;
  117. playback_ports = NULL;
  118. for (chn = 0; chn < n_playback_audio; chn++)
  119. {
  120. snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
  121. port = jack_port_register (client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
  122. if (!port)
  123. {
  124. printf ("jack_netsource: cannot register %s port\n", buf);
  125. break;
  126. }
  127. if( bitdepth == 1000 ) {
  128. #ifdef HAVE_CELT
  129. // XXX: memory leak
  130. CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), 1, jack_get_buffer_size(client), NULL );
  131. playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode ) );
  132. #endif
  133. } else {
  134. playback_srcs = jack_slist_append (playback_srcs, src_new (SRC_LINEAR, 1, NULL));
  135. }
  136. playback_ports = jack_slist_append (playback_ports, port);
  137. }
  138. /* Allocate midi playback channels */
  139. for (chn = n_playback_audio; chn < n_playback_midi + n_playback_audio; chn++)
  140. {
  141. snprintf (buf, sizeof (buf) - 1, "playback_%u", chn + 1);
  142. port = jack_port_register (client, buf, JACK_DEFAULT_MIDI_TYPE, port_flags, 0);
  143. if (!port)
  144. {
  145. printf ("jack_netsource: cannot register %s port\n", buf);
  146. break;
  147. }
  148. playback_ports = jack_slist_append (playback_ports, port);
  149. }
  150. }
  151. /**
  152. * The Sync callback... sync state is set elsewhere...
  153. * we will see if this is working correctly.
  154. * i dont really believe in it yet.
  155. */
  156. int
  157. sync_cb (jack_transport_state_t state, jack_position_t *pos, void *arg)
  158. {
  159. static int latency_count = 0;
  160. int retval = sync_state;
  161. if (latency_count) {
  162. latency_count--;
  163. retval = 0;
  164. }
  165. else if (state == JackTransportStarting && last_transport_state != JackTransportStarting)
  166. {
  167. retval = 0;
  168. latency_count = latency - 1;
  169. }
  170. last_transport_state = state;
  171. return retval;
  172. }
  173. /**
  174. * The process callback for this JACK application.
  175. * It is called by JACK at the appropriate times.
  176. */
  177. int
  178. process (jack_nframes_t nframes, void *arg)
  179. {
  180. jack_nframes_t net_period;
  181. if( bitdepth == 1000 )
  182. net_period = factor;
  183. else
  184. net_period = (float) nframes / (float) factor;
  185. int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
  186. int tx_bufsize = get_sample_size (bitdepth) * playback_channels * net_period + sizeof (jacknet_packet_header);
  187. jack_default_audio_sample_t *buf;
  188. jack_port_t *port;
  189. JSList *node;
  190. channel_t chn;
  191. int size, i;
  192. const char *porttype;
  193. jack_position_t local_trans_pos;
  194. uint32_t *packet_buf, *packet_bufX;
  195. /* Allocate a buffer where both In and Out Buffer will fit */
  196. packet_buf = alloca ((rx_bufsize > tx_bufsize) ? rx_bufsize : tx_bufsize);
  197. jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
  198. packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (uint32_t);
  199. // New Receive Code:
  200. if (reply_port)
  201. packet_cache_drain_socket(global_packcache, insockfd);
  202. else
  203. packet_cache_drain_socket(global_packcache, outsockfd);
  204. size = packet_cache_retreive_packet( global_packcache, framecnt - latency, (char *)packet_buf, rx_bufsize );
  205. /* First alternative : we received what we expected. Render the data
  206. * to the JACK ports so it can be played. */
  207. if (size == rx_bufsize)
  208. {
  209. if (cont_miss)
  210. {
  211. //printf("Frame %d \tRecovered from dropouts\n", framecnt);
  212. cont_miss = 0;
  213. }
  214. render_payload_to_jack_ports (bitdepth, packet_bufX, net_period, capture_ports, capture_srcs, nframes);
  215. /* Now evaluate packet header */
  216. //if (sync_state != pkthdr->sync_state)
  217. // printf ("Frame %d \tSync has been set\n", framecnt);
  218. state_currentframe = framecnt;
  219. //state_latency = framecnt - pkthdr->framecnt;
  220. state_connected = 1;
  221. sync_state = pkthdr->sync_state;
  222. }
  223. /* Second alternative : we've received something that's not
  224. * as big as expected or we missed a packet. We render silence
  225. * to the ouput ports */
  226. else
  227. {
  228. jack_nframes_t latency_estimate;
  229. if( packet_cache_find_latency( global_packcache, framecnt, &latency_estimate ) )
  230. state_latency = latency_estimate;
  231. // Set the counters up.
  232. state_currentframe = framecnt;
  233. //state_latency = framecnt - pkthdr->framecnt;
  234. state_netxruns += 1;
  235. //printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
  236. //printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
  237. cont_miss += 1;
  238. chn = 0;
  239. node = capture_ports;
  240. while (node != NULL)
  241. {
  242. port = (jack_port_t *) node->data;
  243. buf = jack_port_get_buffer (port, nframes);
  244. porttype = jack_port_type (port);
  245. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size ()) == 0)
  246. for (i = 0; i < nframes; i++)
  247. buf[i] = 0.0;
  248. else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size ()) == 0)
  249. jack_midi_clear_buffer (buf);
  250. node = jack_slist_next (node);
  251. chn++;
  252. }
  253. }
  254. /* reset packet_bufX... */
  255. packet_bufX = packet_buf + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
  256. /* ---------- Send ---------- */
  257. render_jack_ports_to_payload (bitdepth, playback_ports, playback_srcs, nframes, packet_bufX, net_period);
  258. /* fill in packet hdr */
  259. pkthdr->transport_state = jack_transport_query (client, &local_trans_pos);
  260. pkthdr->transport_frame = local_trans_pos.frame;
  261. pkthdr->framecnt = framecnt;
  262. pkthdr->latency = latency;
  263. pkthdr->reply_port = reply_port;
  264. pkthdr->sample_rate = jack_get_sample_rate (client);
  265. pkthdr->period_size = nframes;
  266. /* playback for us is capture on the other side */
  267. pkthdr->capture_channels_audio = playback_channels_audio;
  268. pkthdr->playback_channels_audio = capture_channels_audio;
  269. pkthdr->capture_channels_midi = playback_channels_midi;
  270. pkthdr->playback_channels_midi = capture_channels_midi;
  271. pkthdr->mtu = mtu;
  272. packet_header_hton (pkthdr);
  273. if (cont_miss < 2*latency+5)
  274. netjack_sendto (outsockfd, (char *) packet_buf, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
  275. // else if (cont_miss >= 10 && cont_miss <= 50)
  276. // printf ("Frame %d \tToo many packets missed (%d). We have stopped sending data\n", framecnt, cont_miss);
  277. else if (cont_miss > 50+5*latency)
  278. {
  279. state_connected = 0;
  280. //printf ("Frame %d \tRealy too many packets missed (%d). Let's reset the counter\n", framecnt, cont_miss);
  281. cont_miss = 5;
  282. }
  283. framecnt++;
  284. return 0;
  285. }
  286. /**
  287. * This is the shutdown callback for this JACK application.
  288. * It is called by JACK if the server ever shuts down or
  289. * decides to disconnect the client.
  290. */
  291. void
  292. jack_shutdown (void *arg)
  293. {
  294. exit (1);
  295. }
  296. void
  297. init_sockaddr_in (struct sockaddr_in *name , const char *hostname , uint16_t port)
  298. {
  299. name->sin_family = AF_INET ;
  300. name->sin_port = htons (port);
  301. if (hostname)
  302. {
  303. struct hostent *hostinfo = gethostbyname (hostname);
  304. if (hostinfo == NULL)
  305. fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
  306. name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
  307. }
  308. else
  309. name->sin_addr.s_addr = htonl (INADDR_ANY) ;
  310. }
  311. void
  312. printUsage ()
  313. {
  314. fprintf (stderr, "usage: jack_netsource -h <host peer> [options]\n"
  315. "\n"
  316. " -n <jack name> - Reports a different name to jack\n"
  317. " -s <server name> - The name of the local jack server\n"
  318. " -h <host_peer> - Host name of the slave JACK\n"
  319. " -p <port> - UDP port used by the slave JACK\n"
  320. " -P <num channels> - Number of audio playback channels\n"
  321. " -C <num channels> - Number of audio capture channels\n"
  322. " -o <num channels> - Number of midi playback channels\n"
  323. " -i <num channels> - Number of midi capture channels\n"
  324. " -l <latency> - Network latency in number of NetJack frames\n"
  325. " -r <reply port> - Local UDP port to use\n"
  326. " -f <downsample ratio> - Downsample data in the wire by this factor\n"
  327. " -b <bitdepth> - Set transport to use 16bit or 8bit\n"
  328. " -m <mtu> - Assume this mtu for the link\n"
  329. " -c <bytes> - Use Celt and encode <bytes> per channel and packet.\n"
  330. "\n");
  331. }
  332. int
  333. main (int argc, char *argv[])
  334. {
  335. /* Some startup related basics */
  336. char *client_name, *server_name = NULL, *peer_ip;
  337. int peer_port = 3000;
  338. jack_options_t options = JackNullOption;
  339. jack_status_t status;
  340. /* Torben's famous state variables, aka "the reporting API" ! */
  341. int statecopy_connected, statecopy_latency, statecopy_netxruns;
  342. /* Argument parsing stuff */
  343. extern char *optarg;
  344. extern int optind, optopt;
  345. int errflg=0, c;
  346. if (argc < 3)
  347. {
  348. printUsage ();
  349. return 1;
  350. }
  351. client_name = (char *) malloc (sizeof (char) * 9);
  352. peer_ip = (char *) malloc (sizeof (char) * 9);
  353. sprintf(client_name, "netsource");
  354. sprintf(peer_ip, "localhost");
  355. while ((c = getopt (argc, argv, ":n:s:h:p:C:P:i:o:l:r:f:b:m:c:")) != -1)
  356. {
  357. switch (c)
  358. {
  359. case 'n':
  360. free(client_name);
  361. client_name = (char *) malloc (sizeof (char) * strlen (optarg));
  362. strcpy (client_name, optarg);
  363. break;
  364. case 's':
  365. server_name = (char *) malloc (sizeof (char) * strlen (optarg));
  366. strcpy (server_name, optarg);
  367. options |= JackServerName;
  368. break;
  369. case 'h':
  370. free(peer_ip);
  371. peer_ip = (char *) malloc (sizeof (char) * strlen (optarg));
  372. strcpy (peer_ip, optarg);
  373. break;
  374. case 'p':
  375. peer_port = atoi (optarg);
  376. break;
  377. case 'P':
  378. playback_channels_audio = atoi (optarg);
  379. break;
  380. case 'C':
  381. capture_channels_audio = atoi (optarg);
  382. break;
  383. case 'o':
  384. playback_channels_midi = atoi (optarg);
  385. break;
  386. case 'i':
  387. capture_channels_midi = atoi (optarg);
  388. break;
  389. case 'l':
  390. latency = atoi (optarg);
  391. break;
  392. case 'r':
  393. reply_port = atoi (optarg);
  394. break;
  395. case 'f':
  396. factor = atoi (optarg);
  397. break;
  398. case 'b':
  399. bitdepth = atoi (optarg);
  400. break;
  401. case 'c':
  402. #ifdef HAVE_CELT
  403. bitdepth = 1000;
  404. factor = atoi (optarg);
  405. #else
  406. printf( "not built with celt supprt\n" );
  407. exit(10);
  408. #endif
  409. break;
  410. case 'm':
  411. mtu = atoi (optarg);
  412. break;
  413. case ':':
  414. fprintf (stderr, "Option -%c requires an operand\n", optopt);
  415. errflg++;
  416. break;
  417. case '?':
  418. fprintf (stderr, "Unrecognized option: -%c\n", optopt);
  419. errflg++;
  420. }
  421. }
  422. if (errflg)
  423. {
  424. printUsage ();
  425. exit (2);
  426. }
  427. capture_channels = capture_channels_audio + capture_channels_midi;
  428. playback_channels = playback_channels_audio + playback_channels_midi;
  429. outsockfd = socket (PF_INET, SOCK_DGRAM, 0);
  430. insockfd = socket (PF_INET, SOCK_DGRAM, 0);
  431. init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
  432. if(reply_port)
  433. {
  434. init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
  435. bind (insockfd, &bindaddr, sizeof (bindaddr));
  436. }
  437. /* try to become a client of the JACK server */
  438. client = jack_client_open (client_name, options, &status, server_name);
  439. if (client == NULL)
  440. {
  441. fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
  442. "Is the JACK server running ?\n", status);
  443. return 1;
  444. }
  445. /* Set up jack callbacks */
  446. jack_set_process_callback (client, process, 0);
  447. jack_set_sync_callback (client, sync_cb, 0);
  448. jack_on_shutdown (client, jack_shutdown, 0);
  449. alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);
  450. jack_nframes_t net_period = (float) jack_get_buffer_size (client) / (float) factor;
  451. int rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
  452. global_packcache = packet_cache_new (latency + 5, rx_bufsize, mtu);
  453. /* tell the JACK server that we are ready to roll */
  454. if (jack_activate (client))
  455. {
  456. fprintf (stderr, "Cannot activate client");
  457. return 1;
  458. }
  459. /* Now sleep forever... and evaluate the state_ vars */
  460. statecopy_connected = 2; // make it report unconnected on start.
  461. statecopy_latency = state_latency;
  462. statecopy_netxruns = state_netxruns;
  463. while (1)
  464. {
  465. sleep (1);
  466. if (statecopy_connected != state_connected)
  467. {
  468. statecopy_connected = state_connected;
  469. if (statecopy_connected)
  470. {
  471. state_netxruns = 1; // We want to reset the netxrun count on each new connection
  472. printf ("Connected :-)\n");
  473. }
  474. else
  475. printf ("Not Connected\n");
  476. }
  477. if (statecopy_connected)
  478. {
  479. if (statecopy_netxruns != state_netxruns) {
  480. statecopy_netxruns = state_netxruns;
  481. printf ("at frame %06d -> total netxruns %d\n", state_currentframe, statecopy_netxruns);
  482. }
  483. }
  484. else
  485. {
  486. if (statecopy_latency != state_latency)
  487. {
  488. statecopy_latency = state_latency;
  489. if (statecopy_latency > 1)
  490. printf ("current latency %d\n", statecopy_latency);
  491. }
  492. }
  493. }
  494. /* Never reached. Well we will be a GtkApp someday... */
  495. packet_cache_free (global_packcache);
  496. jack_client_close (client);
  497. exit (0);
  498. }