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.

478 lines
15KB

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