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.

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