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.

546 lines
17KB

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