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.

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