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.

1252 lines
39KB

  1. /*
  2. * NetJack - Packet Handling functions
  3. *
  4. * used by the driver and the jacknet_client
  5. *
  6. * Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
  7. * Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  8. * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
  25. *
  26. */
  27. #include "config.h"
  28. #if HAVE_PPOLL
  29. #define _GNU_SOURCE
  30. #endif
  31. #include <math.h>
  32. #include <stdio.h>
  33. #include <memory.h>
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <stdarg.h>
  38. #include <jack/types.h>
  39. #include <jack/engine.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <poll.h>
  44. #include <errno.h>
  45. #include <signal.h>
  46. #include <samplerate.h>
  47. #if HAVE_CELT
  48. #include <celt/celt.h>
  49. #endif
  50. #include "net_driver.h"
  51. #include "netjack_packet.h"
  52. int fraggo = 0;
  53. packet_cache *global_packcache;
  54. void
  55. packet_header_hton (jacknet_packet_header *pkthdr)
  56. {
  57. pkthdr->capture_channels_audio = htonl(pkthdr->capture_channels_audio);
  58. pkthdr->playback_channels_audio = htonl(pkthdr->playback_channels_audio);
  59. pkthdr->capture_channels_midi = htonl(pkthdr->capture_channels_midi);
  60. pkthdr->playback_channels_midi = htonl(pkthdr->playback_channels_midi);
  61. pkthdr->period_size = htonl(pkthdr->period_size);
  62. pkthdr->sample_rate = htonl(pkthdr->sample_rate);
  63. pkthdr->sync_state = htonl(pkthdr->sync_state);
  64. pkthdr->transport_frame = htonl(pkthdr->transport_frame);
  65. pkthdr->transport_state = htonl(pkthdr->transport_state);
  66. pkthdr->framecnt = htonl(pkthdr->framecnt);
  67. pkthdr->latency = htonl(pkthdr->latency);
  68. pkthdr->reply_port = htonl(pkthdr->reply_port);
  69. pkthdr->mtu = htonl(pkthdr->mtu);
  70. pkthdr->fragment_nr = htonl(pkthdr->fragment_nr);
  71. }
  72. void
  73. packet_header_ntoh (jacknet_packet_header *pkthdr)
  74. {
  75. pkthdr->capture_channels_audio = ntohl(pkthdr->capture_channels_audio);
  76. pkthdr->playback_channels_audio = ntohl(pkthdr->playback_channels_audio);
  77. pkthdr->capture_channels_midi = ntohl(pkthdr->capture_channels_midi);
  78. pkthdr->playback_channels_midi = ntohl(pkthdr->playback_channels_midi);
  79. pkthdr->period_size = ntohl(pkthdr->period_size);
  80. pkthdr->sample_rate = ntohl(pkthdr->sample_rate);
  81. pkthdr->sync_state = ntohl(pkthdr->sync_state);
  82. pkthdr->transport_frame = ntohl(pkthdr->transport_frame);
  83. pkthdr->transport_state = ntohl(pkthdr->transport_state);
  84. pkthdr->framecnt = ntohl(pkthdr->framecnt);
  85. pkthdr->latency = ntohl(pkthdr->latency);
  86. pkthdr->reply_port = ntohl(pkthdr->reply_port);
  87. pkthdr->mtu = ntohl(pkthdr->mtu);
  88. pkthdr->fragment_nr = ntohl(pkthdr->fragment_nr);
  89. }
  90. int get_sample_size (int bitdepth)
  91. {
  92. if (bitdepth == 8)
  93. return sizeof (int8_t);
  94. if (bitdepth == 16)
  95. return sizeof (int16_t);
  96. if( bitdepth == 1000 )
  97. return sizeof( unsigned char );
  98. return sizeof (int32_t);
  99. }
  100. // fragment management functions.
  101. packet_cache
  102. *packet_cache_new (int num_packets, int pkt_size, int mtu)
  103. {
  104. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  105. int fragment_number = (pkt_size - sizeof (jacknet_packet_header) - 1) / fragment_payload_size + 1;
  106. int i;
  107. packet_cache *pcache = malloc (sizeof (packet_cache));
  108. if (pcache == NULL)
  109. {
  110. jack_error ("could not allocate packet cache (1)\n");
  111. return NULL;
  112. }
  113. pcache->size = num_packets;
  114. pcache->packets = malloc (sizeof (cache_packet) * num_packets);
  115. if (pcache->packets == NULL)
  116. {
  117. jack_error ("could not allocate packet cache (2)\n");
  118. return NULL;
  119. }
  120. for (i = 0; i < num_packets; i++)
  121. {
  122. pcache->packets[i].valid = 0;
  123. pcache->packets[i].num_fragments = fragment_number;
  124. pcache->packets[i].packet_size = pkt_size;
  125. pcache->packets[i].mtu = mtu;
  126. pcache->packets[i].framecnt = 0;
  127. pcache->packets[i].fragment_array = malloc (sizeof (char) * fragment_number);
  128. pcache->packets[i].packet_buf = malloc (pkt_size);
  129. if ((pcache->packets[i].fragment_array == NULL) || (pcache->packets[i].packet_buf == NULL))
  130. {
  131. jack_error ("could not allocate packet cache (3)\n");
  132. return NULL;
  133. }
  134. }
  135. pcache->mtu = mtu;
  136. return pcache;
  137. }
  138. void
  139. packet_cache_free (packet_cache *pcache)
  140. {
  141. int i;
  142. for (i = 0; i < pcache->size; i++)
  143. {
  144. free (pcache->packets[i].fragment_array);
  145. free (pcache->packets[i].packet_buf);
  146. }
  147. free (pcache->packets);
  148. free (pcache);
  149. }
  150. cache_packet
  151. *packet_cache_get_packet (packet_cache *pcache, jack_nframes_t framecnt)
  152. {
  153. int i;
  154. cache_packet *retval;
  155. for (i = 0; i < pcache->size; i++)
  156. {
  157. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt))
  158. return &(pcache->packets[i]);
  159. }
  160. // The Packet is not in the packet cache.
  161. // find a free packet.
  162. retval = packet_cache_get_free_packet (pcache);
  163. if (retval != NULL)
  164. {
  165. cache_packet_set_framecnt (retval, framecnt);
  166. return retval;
  167. }
  168. // No Free Packet available
  169. // Get The Oldest packet and reset it.
  170. retval = packet_cache_get_oldest_packet (pcache);
  171. cache_packet_reset (retval);
  172. cache_packet_set_framecnt (retval, framecnt);
  173. return retval;
  174. }
  175. // TODO: fix wrapping case... need to pass
  176. // current expected frame here.
  177. //
  178. // or just save framecount into packet_cache.
  179. cache_packet
  180. *packet_cache_get_oldest_packet (packet_cache *pcache)
  181. {
  182. jack_nframes_t minimal_frame = JACK_MAX_FRAMES;
  183. cache_packet *retval = &(pcache->packets[0]);
  184. int i;
  185. for (i = 0; i < pcache->size; i++)
  186. {
  187. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < minimal_frame))
  188. {
  189. minimal_frame = pcache->packets[i].framecnt;
  190. retval = &(pcache->packets[i]);
  191. }
  192. }
  193. return retval;
  194. }
  195. cache_packet
  196. *packet_cache_get_free_packet (packet_cache *pcache)
  197. {
  198. int i;
  199. for (i = 0; i < pcache->size; i++)
  200. {
  201. if (pcache->packets[i].valid == 0)
  202. return &(pcache->packets[i]);
  203. }
  204. return NULL;
  205. }
  206. void
  207. cache_packet_reset (cache_packet *pack)
  208. {
  209. int i;
  210. pack->valid = 0;
  211. // XXX: i dont think this is necessary here...
  212. // fragement array is cleared in _set_framecnt()
  213. for (i = 0; i < pack->num_fragments; i++)
  214. pack->fragment_array[i] = 0;
  215. }
  216. void
  217. cache_packet_set_framecnt (cache_packet *pack, jack_nframes_t framecnt)
  218. {
  219. int i;
  220. pack->framecnt = framecnt;
  221. for (i = 0; i < pack->num_fragments; i++)
  222. pack->fragment_array[i] = 0;
  223. pack->valid = 1;
  224. }
  225. void
  226. cache_packet_add_fragment (cache_packet *pack, char *packet_buf, int rcv_len)
  227. {
  228. jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
  229. int fragment_payload_size = pack->mtu - sizeof (jacknet_packet_header);
  230. char *packet_bufX = pack->packet_buf + sizeof (jacknet_packet_header);
  231. char *dataX = packet_buf + sizeof (jacknet_packet_header);
  232. jack_nframes_t fragment_nr = ntohl (pkthdr->fragment_nr);
  233. jack_nframes_t framecnt = ntohl (pkthdr->framecnt);
  234. if (framecnt != pack->framecnt)
  235. {
  236. jack_error ("errror. framecnts dont match\n");
  237. return;
  238. }
  239. if (fragment_nr == 0)
  240. {
  241. memcpy (pack->packet_buf, packet_buf, rcv_len);
  242. pack->fragment_array[0] = 1;
  243. return;
  244. }
  245. if ((fragment_nr < pack->num_fragments) && (fragment_nr > 0))
  246. {
  247. if ((fragment_nr * fragment_payload_size + rcv_len - sizeof (jacknet_packet_header)) <= (pack->packet_size - sizeof (jacknet_packet_header)))
  248. {
  249. memcpy (packet_bufX + fragment_nr * fragment_payload_size, dataX, rcv_len - sizeof (jacknet_packet_header));
  250. pack->fragment_array[fragment_nr] = 1;
  251. }
  252. else
  253. jack_error ("too long packet received...");
  254. }
  255. }
  256. int
  257. cache_packet_is_complete (cache_packet *pack)
  258. {
  259. int i;
  260. for (i = 0; i < pack->num_fragments; i++)
  261. if (pack->fragment_array[i] == 0)
  262. return FALSE;
  263. return TRUE;
  264. }
  265. // new poll using nanoseconds resolution and
  266. // not waiting forever.
  267. int
  268. netjack_poll_deadline (int sockfd, jack_time_t deadline)
  269. {
  270. struct pollfd fds;
  271. int i, poll_err = 0;
  272. sigset_t sigmask;
  273. struct sigaction action;
  274. #if HAVE_PPOLL
  275. struct timespec timeout_spec = { 0, 0 };
  276. #else
  277. sigset_t rsigmask;
  278. int timeout;
  279. #endif
  280. jack_time_t now = jack_get_microseconds();
  281. if( now >= deadline )
  282. return 0;
  283. #if HAVE_PPOLL
  284. timeout_spec.tv_nsec = (deadline - now) * 1000;
  285. #else
  286. timeout = lrintf( (float)(deadline - now) / 1000.0 );
  287. #endif
  288. sigemptyset(&sigmask);
  289. sigaddset(&sigmask, SIGHUP);
  290. sigaddset(&sigmask, SIGINT);
  291. sigaddset(&sigmask, SIGQUIT);
  292. sigaddset(&sigmask, SIGPIPE);
  293. sigaddset(&sigmask, SIGTERM);
  294. sigaddset(&sigmask, SIGUSR1);
  295. sigaddset(&sigmask, SIGUSR2);
  296. action.sa_handler = SIG_DFL;
  297. action.sa_mask = sigmask;
  298. action.sa_flags = SA_RESTART;
  299. for (i = 1; i < NSIG; i++)
  300. if (sigismember (&sigmask, i))
  301. sigaction (i, &action, 0);
  302. fds.fd = sockfd;
  303. fds.events = POLLIN;
  304. #if HAVE_PPOLL
  305. poll_err = ppoll (&fds, 1, &timeout_spec, &sigmask);
  306. #else
  307. sigprocmask (SIG_UNBLOCK, &sigmask, &rsigmask);
  308. poll_err = poll (&fds, 1, timeout);
  309. sigprocmask (SIG_SETMASK, &rsigmask, NULL);
  310. #endif
  311. if (poll_err == -1)
  312. {
  313. switch (errno)
  314. {
  315. case EBADF:
  316. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  317. break;
  318. case EFAULT:
  319. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  320. break;
  321. case EINTR:
  322. jack_error ("Error %d: A signal occurred before any requested event", errno);
  323. break;
  324. case EINVAL:
  325. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  326. break;
  327. case ENOMEM:
  328. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  329. break;
  330. }
  331. }
  332. return poll_err;
  333. }
  334. int
  335. netjack_poll (int sockfd, int timeout)
  336. {
  337. struct pollfd fds;
  338. int i, poll_err = 0;
  339. sigset_t sigmask, rsigmask;
  340. struct sigaction action;
  341. sigemptyset(&sigmask);
  342. sigaddset(&sigmask, SIGHUP);
  343. sigaddset(&sigmask, SIGINT);
  344. sigaddset(&sigmask, SIGQUIT);
  345. sigaddset(&sigmask, SIGPIPE);
  346. sigaddset(&sigmask, SIGTERM);
  347. sigaddset(&sigmask, SIGUSR1);
  348. sigaddset(&sigmask, SIGUSR2);
  349. action.sa_handler = SIG_DFL;
  350. action.sa_mask = sigmask;
  351. action.sa_flags = SA_RESTART;
  352. for (i = 1; i < NSIG; i++)
  353. if (sigismember (&sigmask, i))
  354. sigaction (i, &action, 0);
  355. fds.fd = sockfd;
  356. fds.events = POLLIN;
  357. sigprocmask(SIG_UNBLOCK, &sigmask, &rsigmask);
  358. while (poll_err == 0)
  359. {
  360. poll_err = poll (&fds, 1, timeout);
  361. }
  362. sigprocmask(SIG_SETMASK, &rsigmask, NULL);
  363. if (poll_err == -1)
  364. {
  365. switch (errno)
  366. {
  367. case EBADF:
  368. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  369. break;
  370. case EFAULT:
  371. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  372. break;
  373. case EINTR:
  374. jack_error ("Error %d: A signal occurred before any requested event", errno);
  375. break;
  376. case EINVAL:
  377. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  378. break;
  379. case ENOMEM:
  380. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  381. break;
  382. }
  383. return FALSE;
  384. }
  385. return TRUE;
  386. }
  387. // This now reads all a socket has into the cache.
  388. // replacing netjack_recv functions.
  389. void
  390. packet_cache_drain_socket( packet_cache *pcache, int sockfd )
  391. {
  392. char *rx_packet = alloca (pcache->mtu);
  393. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  394. int rcv_len;
  395. jack_nframes_t framecnt;
  396. cache_packet *cpack;
  397. while (1)
  398. {
  399. rcv_len = recv (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT);
  400. if (rcv_len < 0)
  401. return;
  402. framecnt = ntohl (pkthdr->framecnt);
  403. //printf( "Got Packet %d\n", framecnt );
  404. cpack = packet_cache_get_packet (global_packcache, framecnt);
  405. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  406. }
  407. }
  408. int
  409. packet_cache_retreive_packet( packet_cache *pcache, jack_nframes_t framecnt, char *packet_buf, int pkt_size )
  410. {
  411. int i;
  412. cache_packet *cpack = NULL;
  413. for (i = 0; i < pcache->size; i++) {
  414. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  415. cpack = &(pcache->packets[i]);
  416. break;
  417. }
  418. }
  419. if( cpack == NULL )
  420. return -1;
  421. if( !cache_packet_is_complete( cpack ) )
  422. return -1;
  423. // ok. cpack is the one we want and its complete.
  424. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  425. cache_packet_reset (cpack);
  426. return pkt_size;
  427. }
  428. // Returns 0 when no valid packet is inside the cache.
  429. int
  430. packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  431. {
  432. int i;
  433. jack_nframes_t best_offset = JACK_MAX_FRAMES/2-1;
  434. int retval = 0;
  435. for (i = 0; i < pcache->size; i++)
  436. {
  437. cache_packet *cpack = &(pcache->packets[i]);
  438. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  439. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  440. //printf( "invalid\n" );
  441. continue;
  442. }
  443. if( (cpack->framecnt - expected_framecnt) > best_offset ) {
  444. continue;
  445. }
  446. best_offset = cpack->framecnt - expected_framecnt;
  447. retval = 1;
  448. if( best_offset == 0 )
  449. break;
  450. }
  451. if( retval && framecnt )
  452. *framecnt = expected_framecnt + best_offset;
  453. return retval;
  454. }
  455. // Returns 0 when no valid packet is inside the cache.
  456. int
  457. packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  458. {
  459. int i;
  460. jack_nframes_t best_offset = 0;
  461. int retval = 0;
  462. for (i = 0; i < pcache->size; i++)
  463. {
  464. cache_packet *cpack = &(pcache->packets[i]);
  465. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  466. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  467. //printf( "invalid\n" );
  468. continue;
  469. }
  470. if( (cpack->framecnt - expected_framecnt) < best_offset ) {
  471. continue;
  472. }
  473. best_offset = cpack->framecnt - expected_framecnt;
  474. retval = 1;
  475. if( best_offset == 0 )
  476. break;
  477. }
  478. if( retval && framecnt )
  479. *framecnt = JACK_MAX_FRAMES - best_offset;
  480. return retval;
  481. }
  482. // fragmented packet IO
  483. int
  484. netjack_recvfrom (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, socklen_t *addr_size, int mtu)
  485. {
  486. if (pkt_size <= mtu)
  487. return recvfrom (sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  488. char *rx_packet = alloca (mtu);
  489. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  490. int rcv_len;
  491. jack_nframes_t framecnt;
  492. cache_packet *cpack;
  493. do
  494. {
  495. rcv_len = recvfrom (sockfd, rx_packet, mtu, 0, addr, addr_size);
  496. if (rcv_len < 0)
  497. return rcv_len;
  498. framecnt = ntohl (pkthdr->framecnt);
  499. cpack = packet_cache_get_packet (global_packcache, framecnt);
  500. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  501. } while (!cache_packet_is_complete (cpack));
  502. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  503. cache_packet_reset (cpack);
  504. return pkt_size;
  505. }
  506. int
  507. netjack_recv (int sockfd, char *packet_buf, int pkt_size, int flags, int mtu)
  508. {
  509. if (pkt_size <= mtu)
  510. return recv (sockfd, packet_buf, pkt_size, flags);
  511. char *rx_packet = alloca (mtu);
  512. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  513. int rcv_len;
  514. jack_nframes_t framecnt;
  515. cache_packet *cpack;
  516. do
  517. {
  518. rcv_len = recv (sockfd, rx_packet, mtu, flags);
  519. if (rcv_len < 0)
  520. return rcv_len;
  521. framecnt = ntohl (pkthdr->framecnt);
  522. cpack = packet_cache_get_packet (global_packcache, framecnt);
  523. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  524. } while (!cache_packet_is_complete (cpack));
  525. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  526. cache_packet_reset (cpack);
  527. return pkt_size;
  528. }
  529. void
  530. netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
  531. {
  532. int frag_cnt = 0;
  533. char *tx_packet, *dataX;
  534. jacknet_packet_header *pkthdr;
  535. tx_packet = alloca (mtu + 10);
  536. dataX = tx_packet + sizeof (jacknet_packet_header);
  537. pkthdr = (jacknet_packet_header *) tx_packet;
  538. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  539. if (pkt_size <= mtu) {
  540. pkthdr = (jacknet_packet_header *) packet_buf;
  541. pkthdr->fragment_nr = htonl (0);
  542. sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  543. }
  544. else
  545. {
  546. // Copy the packet header to the tx pack first.
  547. memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
  548. // Now loop and send all
  549. char *packet_bufX = packet_buf + sizeof (jacknet_packet_header);
  550. while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size))
  551. {
  552. pkthdr->fragment_nr = htonl (frag_cnt++);
  553. memcpy (dataX, packet_bufX, fragment_payload_size);
  554. sendto (sockfd, tx_packet, mtu, flags, addr, addr_size);
  555. packet_bufX += fragment_payload_size;
  556. }
  557. int last_payload_size = packet_buf + pkt_size - packet_bufX;
  558. memcpy (dataX, packet_bufX, last_payload_size);
  559. pkthdr->fragment_nr = htonl (frag_cnt);
  560. //jack_error("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
  561. // sendto(last_pack_size);
  562. sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
  563. }
  564. }
  565. void
  566. decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  567. {
  568. int i;
  569. jack_midi_clear_buffer (buf);
  570. for (i = 0; i < buffer_size_uint32 - 3;)
  571. {
  572. uint32_t payload_size;
  573. payload_size = buffer_uint32[i];
  574. payload_size = ntohl (payload_size);
  575. if (payload_size)
  576. {
  577. jack_midi_event_t event;
  578. event.time = ntohl (buffer_uint32[i+1]);
  579. event.size = ntohl (buffer_uint32[i+2]);
  580. event.buffer = (jack_midi_data_t*) (&(buffer_uint32[i+3]));
  581. jack_midi_event_write (buf, event.time, event.buffer, event.size);
  582. // skip to the next event
  583. unsigned int nb_data_quads = (((event.size-1) & ~0x3) >> 2)+1;
  584. i += 3+nb_data_quads;
  585. }
  586. else
  587. break; // no events can follow an empty event, we're done
  588. }
  589. }
  590. void
  591. encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  592. {
  593. int i;
  594. unsigned int written = 0;
  595. // midi port, encode midi events
  596. unsigned int nevents = jack_midi_get_event_count (buf);
  597. for (i = 0; i < nevents; ++i)
  598. {
  599. jack_midi_event_t event;
  600. jack_midi_event_get (&event, buf, i);
  601. unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
  602. unsigned int payload_size = 3 + nb_data_quads;
  603. // only write if we have sufficient space for the event
  604. // otherwise drop it
  605. if (written + payload_size < buffer_size_uint32 - 1)
  606. {
  607. // write header
  608. buffer_uint32[written]=htonl (payload_size);
  609. written++;
  610. buffer_uint32[written]=htonl (event.time);
  611. written++;
  612. buffer_uint32[written]=htonl (event.size);
  613. written++;
  614. // write data
  615. jack_midi_data_t* tmpbuff = (jack_midi_data_t*)(&(buffer_uint32[written]));
  616. memcpy (tmpbuff, event.buffer, event.size);
  617. written += nb_data_quads;
  618. }
  619. else
  620. {
  621. // buffer overflow
  622. jack_error ("midi buffer overflow");
  623. break;
  624. }
  625. }
  626. // now put a netjack_midi 'no-payload' event, signaling EOF
  627. buffer_uint32[written]=0;
  628. }
  629. // render functions for float
  630. void
  631. render_payload_to_jack_ports_float ( void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  632. {
  633. channel_t chn = 0;
  634. JSList *node = capture_ports;
  635. JSList *src_node = capture_srcs;
  636. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  637. if( !packet_payload )
  638. return;
  639. while (node != NULL)
  640. {
  641. int i;
  642. int_float_t val;
  643. SRC_DATA src;
  644. jack_port_t *port = (jack_port_t *) node->data;
  645. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  646. const char *porttype = jack_port_type (port);
  647. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  648. {
  649. // audio port, resample if necessary
  650. if (net_period_down != nframes)
  651. {
  652. SRC_STATE *src_state = src_node->data;
  653. for (i = 0; i < net_period_down; i++)
  654. {
  655. packet_bufX[i] = ntohl (packet_bufX[i]);
  656. }
  657. src.data_in = (float *) packet_bufX;
  658. src.input_frames = net_period_down;
  659. src.data_out = buf;
  660. src.output_frames = nframes;
  661. src.src_ratio = (float) nframes / (float) net_period_down;
  662. src.end_of_input = 0;
  663. src_set_ratio (src_state, src.src_ratio);
  664. src_process (src_state, &src);
  665. src_node = jack_slist_next (src_node);
  666. }
  667. else
  668. {
  669. for (i = 0; i < net_period_down; i++)
  670. {
  671. val.i = packet_bufX[i];
  672. val.i = ntohl (val.i);
  673. buf[i] = val.f;
  674. }
  675. }
  676. }
  677. else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  678. {
  679. // midi port, decode midi events
  680. // convert the data buffer to a standard format (uint32_t based)
  681. unsigned int buffer_size_uint32 = net_period_down;
  682. uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
  683. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  684. }
  685. packet_bufX = (packet_bufX + net_period_down);
  686. node = jack_slist_next (node);
  687. chn++;
  688. }
  689. }
  690. void
  691. render_jack_ports_to_payload_float (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  692. {
  693. channel_t chn = 0;
  694. JSList *node = playback_ports;
  695. JSList *src_node = playback_srcs;
  696. uint32_t *packet_bufX = (uint32_t *) packet_payload;
  697. while (node != NULL)
  698. {
  699. SRC_DATA src;
  700. int i;
  701. int_float_t val;
  702. jack_port_t *port = (jack_port_t *) node->data;
  703. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  704. const char *porttype = jack_port_type (port);
  705. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  706. {
  707. // audio port, resample if necessary
  708. if (net_period_up != nframes) {
  709. SRC_STATE *src_state = src_node->data;
  710. src.data_in = buf;
  711. src.input_frames = nframes;
  712. src.data_out = (float *) packet_bufX;
  713. src.output_frames = net_period_up;
  714. src.src_ratio = (float) net_period_up / (float) nframes;
  715. src.end_of_input = 0;
  716. src_set_ratio (src_state, src.src_ratio);
  717. src_process (src_state, &src);
  718. for (i = 0; i < net_period_up; i++)
  719. {
  720. packet_bufX[i] = htonl (packet_bufX[i]);
  721. }
  722. src_node = jack_slist_next (src_node);
  723. }
  724. else
  725. {
  726. for (i = 0; i < net_period_up; i++)
  727. {
  728. val.f = buf[i];
  729. val.i = htonl (val.i);
  730. packet_bufX[i] = val.i;
  731. }
  732. }
  733. }
  734. else if (strncmp(porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  735. {
  736. // encode midi events from port to packet
  737. // convert the data buffer to a standard format (uint32_t based)
  738. unsigned int buffer_size_uint32 = net_period_up;
  739. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  740. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  741. }
  742. packet_bufX = (packet_bufX + net_period_up);
  743. node = jack_slist_next (node);
  744. chn++;
  745. }
  746. }
  747. // render functions for 16bit
  748. void
  749. render_payload_to_jack_ports_16bit (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  750. {
  751. channel_t chn = 0;
  752. JSList *node = capture_ports;
  753. JSList *src_node = capture_srcs;
  754. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  755. if( !packet_payload )
  756. return;
  757. while (node != NULL)
  758. {
  759. int i;
  760. //uint32_t val;
  761. SRC_DATA src;
  762. jack_port_t *port = (jack_port_t *) node->data;
  763. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  764. float *floatbuf = alloca (sizeof(float) * net_period_down);
  765. const char *portname = jack_port_type (port);
  766. if (strncmp(portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  767. {
  768. // audio port, resample if necessary
  769. if (net_period_down != nframes)
  770. {
  771. SRC_STATE *src_state = src_node->data;
  772. for (i = 0; i < net_period_down; i++)
  773. {
  774. floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
  775. }
  776. src.data_in = floatbuf;
  777. src.input_frames = net_period_down;
  778. src.data_out = buf;
  779. src.output_frames = nframes;
  780. src.src_ratio = (float) nframes / (float) net_period_down;
  781. src.end_of_input = 0;
  782. src_set_ratio (src_state, src.src_ratio);
  783. src_process (src_state, &src);
  784. src_node = jack_slist_next (src_node);
  785. }
  786. else
  787. for (i = 0; i < net_period_down; i++)
  788. buf[i] = ((float) ntohs (packet_bufX[i])) / 32768.0 - 1.0;
  789. }
  790. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  791. {
  792. // midi port, decode midi events
  793. // convert the data buffer to a standard format (uint32_t based)
  794. unsigned int buffer_size_uint32 = net_period_down / 2;
  795. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  796. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  797. }
  798. packet_bufX = (packet_bufX + net_period_down);
  799. node = jack_slist_next (node);
  800. chn++;
  801. }
  802. }
  803. void
  804. render_jack_ports_to_payload_16bit (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  805. {
  806. channel_t chn = 0;
  807. JSList *node = playback_ports;
  808. JSList *src_node = playback_srcs;
  809. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  810. while (node != NULL)
  811. {
  812. SRC_DATA src;
  813. int i;
  814. jack_port_t *port = (jack_port_t *) node->data;
  815. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  816. const char *portname = jack_port_type (port);
  817. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  818. {
  819. // audio port, resample if necessary
  820. if (net_period_up != nframes)
  821. {
  822. SRC_STATE *src_state = src_node->data;
  823. float *floatbuf = alloca (sizeof(float) * net_period_up);
  824. src.data_in = buf;
  825. src.input_frames = nframes;
  826. src.data_out = floatbuf;
  827. src.output_frames = net_period_up;
  828. src.src_ratio = (float) net_period_up / (float) nframes;
  829. src.end_of_input = 0;
  830. src_set_ratio (src_state, src.src_ratio);
  831. src_process (src_state, &src);
  832. for (i = 0; i < net_period_up; i++)
  833. {
  834. packet_bufX[i] = htons (((uint16_t)((floatbuf[i] + 1.0) * 32767.0)));
  835. }
  836. src_node = jack_slist_next (src_node);
  837. }
  838. else
  839. for (i = 0; i < net_period_up; i++)
  840. packet_bufX[i] = htons(((uint16_t)((buf[i] + 1.0) * 32767.0)));
  841. }
  842. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  843. {
  844. // encode midi events from port to packet
  845. // convert the data buffer to a standard format (uint32_t based)
  846. unsigned int buffer_size_uint32 = net_period_up / 2;
  847. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  848. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  849. }
  850. packet_bufX = (packet_bufX + net_period_up);
  851. node = jack_slist_next (node);
  852. chn++;
  853. }
  854. }
  855. // render functions for 8bit
  856. void
  857. render_payload_to_jack_ports_8bit (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  858. {
  859. channel_t chn = 0;
  860. JSList *node = capture_ports;
  861. JSList *src_node = capture_srcs;
  862. int8_t *packet_bufX = (int8_t *)packet_payload;
  863. if( !packet_payload )
  864. return;
  865. while (node != NULL)
  866. {
  867. int i;
  868. //uint32_t val;
  869. SRC_DATA src;
  870. jack_port_t *port = (jack_port_t *) node->data;
  871. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  872. float *floatbuf = alloca (sizeof (float) * net_period_down);
  873. const char *portname = jack_port_type (port);
  874. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  875. {
  876. // audio port, resample if necessary
  877. if (net_period_down != nframes)
  878. {
  879. SRC_STATE *src_state = src_node->data;
  880. for (i = 0; i < net_period_down; i++)
  881. floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
  882. src.data_in = floatbuf;
  883. src.input_frames = net_period_down;
  884. src.data_out = buf;
  885. src.output_frames = nframes;
  886. src.src_ratio = (float) nframes / (float) net_period_down;
  887. src.end_of_input = 0;
  888. src_set_ratio (src_state, src.src_ratio);
  889. src_process (src_state, &src);
  890. src_node = jack_slist_next (src_node);
  891. }
  892. else
  893. for (i = 0; i < net_period_down; i++)
  894. buf[i] = ((float) packet_bufX[i]) / 127.0;
  895. }
  896. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  897. {
  898. // midi port, decode midi events
  899. // convert the data buffer to a standard format (uint32_t based)
  900. unsigned int buffer_size_uint32 = net_period_down / 2;
  901. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  902. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  903. }
  904. packet_bufX = (packet_bufX + net_period_down);
  905. node = jack_slist_next (node);
  906. chn++;
  907. }
  908. }
  909. void
  910. render_jack_ports_to_payload_8bit (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  911. {
  912. channel_t chn = 0;
  913. JSList *node = playback_ports;
  914. JSList *src_node = playback_srcs;
  915. int8_t *packet_bufX = (int8_t *)packet_payload;
  916. while (node != NULL)
  917. {
  918. SRC_DATA src;
  919. int i;
  920. jack_port_t *port = (jack_port_t *) node->data;
  921. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  922. const char *portname = jack_port_type (port);
  923. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  924. {
  925. // audio port, resample if necessary
  926. if (net_period_up != nframes)
  927. {
  928. SRC_STATE *src_state = src_node->data;
  929. float *floatbuf = alloca (sizeof (float) * net_period_up);
  930. src.data_in = buf;
  931. src.input_frames = nframes;
  932. src.data_out = floatbuf;
  933. src.output_frames = net_period_up;
  934. src.src_ratio = (float) net_period_up / (float) nframes;
  935. src.end_of_input = 0;
  936. src_set_ratio (src_state, src.src_ratio);
  937. src_process (src_state, &src);
  938. for (i = 0; i < net_period_up; i++)
  939. packet_bufX[i] = floatbuf[i] * 127.0;
  940. src_node = jack_slist_next (src_node);
  941. }
  942. else
  943. for (i = 0; i < net_period_up; i++)
  944. packet_bufX[i] = buf[i] * 127.0;
  945. }
  946. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  947. {
  948. // encode midi events from port to packet
  949. // convert the data buffer to a standard format (uint32_t based)
  950. unsigned int buffer_size_uint32 = net_period_up / 4;
  951. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  952. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  953. }
  954. packet_bufX = (packet_bufX + net_period_up);
  955. node = jack_slist_next (node);
  956. chn++;
  957. }
  958. }
  959. #if HAVE_CELT
  960. // render functions for celt.
  961. void
  962. render_payload_to_jack_ports_celt (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  963. {
  964. channel_t chn = 0;
  965. JSList *node = capture_ports;
  966. JSList *src_node = capture_srcs;
  967. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  968. while (node != NULL)
  969. {
  970. jack_port_t *port = (jack_port_t *) node->data;
  971. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  972. const char *portname = jack_port_type (port);
  973. if (strncmp(portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  974. {
  975. // audio port, decode celt data.
  976. CELTDecoder *decoder = src_node->data;
  977. if( !packet_payload )
  978. celt_decode_float( decoder, NULL, net_period_down, buf );
  979. else
  980. celt_decode_float( decoder, packet_bufX, net_period_down, buf );
  981. src_node = jack_slist_next (src_node);
  982. }
  983. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  984. {
  985. // midi port, decode midi events
  986. // convert the data buffer to a standard format (uint32_t based)
  987. unsigned int buffer_size_uint32 = net_period_down / 2;
  988. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  989. if( packet_payload )
  990. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  991. }
  992. packet_bufX = (packet_bufX + net_period_down);
  993. node = jack_slist_next (node);
  994. chn++;
  995. }
  996. }
  997. void
  998. render_jack_ports_to_payload_celt (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  999. {
  1000. channel_t chn = 0;
  1001. JSList *node = playback_ports;
  1002. JSList *src_node = playback_srcs;
  1003. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1004. while (node != NULL)
  1005. {
  1006. jack_port_t *port = (jack_port_t *) node->data;
  1007. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1008. const char *portname = jack_port_type (port);
  1009. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  1010. {
  1011. // audio port, encode celt data.
  1012. int encoded_bytes;
  1013. float *floatbuf = alloca (sizeof(float) * nframes );
  1014. memcpy( floatbuf, buf, nframes*sizeof(float) );
  1015. CELTEncoder *encoder = src_node->data;
  1016. encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
  1017. if( encoded_bytes != net_period_up )
  1018. printf( "bah... they are not the same\n" );
  1019. src_node = jack_slist_next( src_node );
  1020. }
  1021. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  1022. {
  1023. // encode midi events from port to packet
  1024. // convert the data buffer to a standard format (uint32_t based)
  1025. unsigned int buffer_size_uint32 = net_period_up / 2;
  1026. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1027. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1028. }
  1029. packet_bufX = (packet_bufX + net_period_up);
  1030. node = jack_slist_next (node);
  1031. chn++;
  1032. }
  1033. }
  1034. #endif
  1035. /* Wrapper functions with bitdepth argument... */
  1036. void
  1037. render_payload_to_jack_ports (int bitdepth, void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  1038. {
  1039. if (bitdepth == 8)
  1040. render_payload_to_jack_ports_8bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1041. else if (bitdepth == 16)
  1042. render_payload_to_jack_ports_16bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1043. #if HAVE_CELT
  1044. else if (bitdepth == 1000)
  1045. render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1046. #endif
  1047. else
  1048. render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1049. }
  1050. void
  1051. render_jack_ports_to_payload (int bitdepth, JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  1052. {
  1053. if (bitdepth == 8)
  1054. render_jack_ports_to_payload_8bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1055. else if (bitdepth == 16)
  1056. render_jack_ports_to_payload_16bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1057. #if HAVE_CELT
  1058. else if (bitdepth == 1000)
  1059. render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1060. #endif
  1061. else
  1062. render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1063. }