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.

1357 lines
42KB

  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. pcache->master_address_valid = 0;
  116. if (pcache->packets == NULL)
  117. {
  118. jack_error ("could not allocate packet cache (2)\n");
  119. return NULL;
  120. }
  121. for (i = 0; i < num_packets; i++)
  122. {
  123. pcache->packets[i].valid = 0;
  124. pcache->packets[i].num_fragments = fragment_number;
  125. pcache->packets[i].packet_size = pkt_size;
  126. pcache->packets[i].mtu = mtu;
  127. pcache->packets[i].framecnt = 0;
  128. pcache->packets[i].fragment_array = malloc (sizeof (char) * fragment_number);
  129. pcache->packets[i].packet_buf = malloc (pkt_size);
  130. if ((pcache->packets[i].fragment_array == NULL) || (pcache->packets[i].packet_buf == NULL))
  131. {
  132. jack_error ("could not allocate packet cache (3)\n");
  133. return NULL;
  134. }
  135. }
  136. pcache->mtu = mtu;
  137. return pcache;
  138. }
  139. void
  140. packet_cache_free (packet_cache *pcache)
  141. {
  142. int i;
  143. for (i = 0; i < pcache->size; i++)
  144. {
  145. free (pcache->packets[i].fragment_array);
  146. free (pcache->packets[i].packet_buf);
  147. }
  148. free (pcache->packets);
  149. free (pcache);
  150. }
  151. cache_packet
  152. *packet_cache_get_packet (packet_cache *pcache, jack_nframes_t framecnt)
  153. {
  154. int i;
  155. cache_packet *retval;
  156. for (i = 0; i < pcache->size; i++)
  157. {
  158. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt))
  159. return &(pcache->packets[i]);
  160. }
  161. // The Packet is not in the packet cache.
  162. // find a free packet.
  163. retval = packet_cache_get_free_packet (pcache);
  164. if (retval != NULL)
  165. {
  166. cache_packet_set_framecnt (retval, framecnt);
  167. return retval;
  168. }
  169. // No Free Packet available
  170. // Get The Oldest packet and reset it.
  171. retval = packet_cache_get_oldest_packet (pcache);
  172. //printf( "Dropping %d from Cache :S\n", retval->framecnt );
  173. cache_packet_reset (retval);
  174. cache_packet_set_framecnt (retval, framecnt);
  175. return retval;
  176. }
  177. // TODO: fix wrapping case... need to pass
  178. // current expected frame here.
  179. //
  180. // or just save framecount into packet_cache.
  181. cache_packet
  182. *packet_cache_get_oldest_packet (packet_cache *pcache)
  183. {
  184. jack_nframes_t minimal_frame = JACK_MAX_FRAMES;
  185. cache_packet *retval = &(pcache->packets[0]);
  186. int i;
  187. for (i = 0; i < pcache->size; i++)
  188. {
  189. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < minimal_frame))
  190. {
  191. minimal_frame = pcache->packets[i].framecnt;
  192. retval = &(pcache->packets[i]);
  193. }
  194. }
  195. return retval;
  196. }
  197. cache_packet
  198. *packet_cache_get_free_packet (packet_cache *pcache)
  199. {
  200. int i;
  201. for (i = 0; i < pcache->size; i++)
  202. {
  203. if (pcache->packets[i].valid == 0)
  204. return &(pcache->packets[i]);
  205. }
  206. return NULL;
  207. }
  208. void
  209. cache_packet_reset (cache_packet *pack)
  210. {
  211. int i;
  212. pack->valid = 0;
  213. // XXX: i dont think this is necessary here...
  214. // fragement array is cleared in _set_framecnt()
  215. for (i = 0; i < pack->num_fragments; i++)
  216. pack->fragment_array[i] = 0;
  217. }
  218. void
  219. cache_packet_set_framecnt (cache_packet *pack, jack_nframes_t framecnt)
  220. {
  221. int i;
  222. pack->framecnt = framecnt;
  223. for (i = 0; i < pack->num_fragments; i++)
  224. pack->fragment_array[i] = 0;
  225. pack->valid = 1;
  226. }
  227. void
  228. cache_packet_add_fragment (cache_packet *pack, char *packet_buf, int rcv_len)
  229. {
  230. jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
  231. int fragment_payload_size = pack->mtu - sizeof (jacknet_packet_header);
  232. char *packet_bufX = pack->packet_buf + sizeof (jacknet_packet_header);
  233. char *dataX = packet_buf + sizeof (jacknet_packet_header);
  234. jack_nframes_t fragment_nr = ntohl (pkthdr->fragment_nr);
  235. jack_nframes_t framecnt = ntohl (pkthdr->framecnt);
  236. if (framecnt != pack->framecnt)
  237. {
  238. jack_error ("errror. framecnts dont match\n");
  239. return;
  240. }
  241. if (fragment_nr == 0)
  242. {
  243. memcpy (pack->packet_buf, packet_buf, rcv_len);
  244. pack->fragment_array[0] = 1;
  245. return;
  246. }
  247. if ((fragment_nr < pack->num_fragments) && (fragment_nr > 0))
  248. {
  249. if ((fragment_nr * fragment_payload_size + rcv_len - sizeof (jacknet_packet_header)) <= (pack->packet_size - sizeof (jacknet_packet_header)))
  250. {
  251. memcpy (packet_bufX + fragment_nr * fragment_payload_size, dataX, rcv_len - sizeof (jacknet_packet_header));
  252. pack->fragment_array[fragment_nr] = 1;
  253. }
  254. else
  255. jack_error ("too long packet received...");
  256. }
  257. }
  258. int
  259. cache_packet_is_complete (cache_packet *pack)
  260. {
  261. int i;
  262. for (i = 0; i < pack->num_fragments; i++)
  263. if (pack->fragment_array[i] == 0)
  264. return FALSE;
  265. return TRUE;
  266. }
  267. // new poll using nanoseconds resolution and
  268. // not waiting forever.
  269. int
  270. netjack_poll_deadline (int sockfd, jack_time_t deadline)
  271. {
  272. struct pollfd fds;
  273. int i, poll_err = 0;
  274. sigset_t sigmask;
  275. struct sigaction action;
  276. #if HAVE_PPOLL
  277. struct timespec timeout_spec = { 0, 0 };
  278. #else
  279. sigset_t rsigmask;
  280. int timeout;
  281. #endif
  282. jack_time_t now = jack_get_microseconds();
  283. if( now >= deadline )
  284. return 0;
  285. #if HAVE_PPOLL
  286. timeout_spec.tv_nsec = (deadline - now) * 1000;
  287. #else
  288. timeout = lrintf( (float)(deadline - now) / 1000.0 );
  289. #endif
  290. sigemptyset(&sigmask);
  291. sigaddset(&sigmask, SIGHUP);
  292. sigaddset(&sigmask, SIGINT);
  293. sigaddset(&sigmask, SIGQUIT);
  294. sigaddset(&sigmask, SIGPIPE);
  295. sigaddset(&sigmask, SIGTERM);
  296. sigaddset(&sigmask, SIGUSR1);
  297. sigaddset(&sigmask, SIGUSR2);
  298. action.sa_handler = SIG_DFL;
  299. action.sa_mask = sigmask;
  300. action.sa_flags = SA_RESTART;
  301. for (i = 1; i < NSIG; i++)
  302. if (sigismember (&sigmask, i))
  303. sigaction (i, &action, 0);
  304. fds.fd = sockfd;
  305. fds.events = POLLIN;
  306. #if HAVE_PPOLL
  307. poll_err = ppoll (&fds, 1, &timeout_spec, &sigmask);
  308. #else
  309. sigprocmask (SIG_UNBLOCK, &sigmask, &rsigmask);
  310. poll_err = poll (&fds, 1, timeout);
  311. sigprocmask (SIG_SETMASK, &rsigmask, NULL);
  312. #endif
  313. if (poll_err == -1)
  314. {
  315. switch (errno)
  316. {
  317. case EBADF:
  318. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  319. break;
  320. case EFAULT:
  321. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  322. break;
  323. case EINTR:
  324. jack_error ("Error %d: A signal occurred before any requested event", errno);
  325. break;
  326. case EINVAL:
  327. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  328. break;
  329. case ENOMEM:
  330. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  331. break;
  332. }
  333. }
  334. return poll_err;
  335. }
  336. int
  337. netjack_poll (int sockfd, int timeout)
  338. {
  339. struct pollfd fds;
  340. int i, poll_err = 0;
  341. sigset_t sigmask, rsigmask;
  342. struct sigaction action;
  343. sigemptyset(&sigmask);
  344. sigaddset(&sigmask, SIGHUP);
  345. sigaddset(&sigmask, SIGINT);
  346. sigaddset(&sigmask, SIGQUIT);
  347. sigaddset(&sigmask, SIGPIPE);
  348. sigaddset(&sigmask, SIGTERM);
  349. sigaddset(&sigmask, SIGUSR1);
  350. sigaddset(&sigmask, SIGUSR2);
  351. action.sa_handler = SIG_DFL;
  352. action.sa_mask = sigmask;
  353. action.sa_flags = SA_RESTART;
  354. for (i = 1; i < NSIG; i++)
  355. if (sigismember (&sigmask, i))
  356. sigaction (i, &action, 0);
  357. fds.fd = sockfd;
  358. fds.events = POLLIN;
  359. sigprocmask(SIG_UNBLOCK, &sigmask, &rsigmask);
  360. while (poll_err == 0)
  361. {
  362. poll_err = poll (&fds, 1, timeout);
  363. }
  364. sigprocmask(SIG_SETMASK, &rsigmask, NULL);
  365. if (poll_err == -1)
  366. {
  367. switch (errno)
  368. {
  369. case EBADF:
  370. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  371. break;
  372. case EFAULT:
  373. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  374. break;
  375. case EINTR:
  376. jack_error ("Error %d: A signal occurred before any requested event", errno);
  377. break;
  378. case EINVAL:
  379. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  380. break;
  381. case ENOMEM:
  382. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  383. break;
  384. }
  385. return FALSE;
  386. }
  387. return TRUE;
  388. }
  389. // This now reads all a socket has into the cache.
  390. // replacing netjack_recv functions.
  391. void
  392. packet_cache_drain_socket( packet_cache *pcache, int sockfd )
  393. {
  394. char *rx_packet = alloca (pcache->mtu);
  395. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  396. int rcv_len;
  397. jack_nframes_t framecnt;
  398. cache_packet *cpack;
  399. struct sockaddr_in sender_address;
  400. socklen_t senderlen;
  401. while (1)
  402. {
  403. rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT,
  404. &sender_address, &senderlen);
  405. if (rcv_len < 0)
  406. return;
  407. if (pcache->master_address_valid) {
  408. // Verify its from our master.
  409. if (memcmp (&sender_address, &(pcache->master_address), senderlen) != 0)
  410. continue;
  411. } else {
  412. // Setup this one as master
  413. //printf( "setup master...\n" );
  414. memcpy ( &(pcache->master_address), &sender_address, senderlen );
  415. pcache->master_address_valid = 1;
  416. }
  417. framecnt = ntohl (pkthdr->framecnt);
  418. //printf( "Got Packet %d\n", framecnt );
  419. cpack = packet_cache_get_packet (global_packcache, framecnt);
  420. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  421. cpack->recv_timestamp = jack_get_microseconds();
  422. }
  423. }
  424. void
  425. packet_cache_reset_master_address( packet_cache *pcache )
  426. {
  427. pcache->master_address_valid = 0;
  428. }
  429. void
  430. packet_cache_clear_old_packets (packet_cache *pcache, jack_nframes_t framecnt )
  431. {
  432. int i;
  433. for (i = 0; i < pcache->size; i++)
  434. {
  435. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < framecnt))
  436. {
  437. cache_packet_reset (&(pcache->packets[i]));
  438. }
  439. }
  440. }
  441. int
  442. packet_cache_retreive_packet( packet_cache *pcache, jack_nframes_t framecnt, char *packet_buf, int pkt_size, jack_time_t *timestamp )
  443. {
  444. int i;
  445. cache_packet *cpack = NULL;
  446. for (i = 0; i < pcache->size; i++) {
  447. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  448. cpack = &(pcache->packets[i]);
  449. break;
  450. }
  451. }
  452. if( cpack == NULL ) {
  453. //printf( "retreive packet: %d....not found\n", framecnt );
  454. return -1;
  455. }
  456. if( !cache_packet_is_complete( cpack ) ) {
  457. return -1;
  458. }
  459. // ok. cpack is the one we want and its complete.
  460. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  461. if( timestamp )
  462. *timestamp = cpack->recv_timestamp;
  463. cache_packet_reset (cpack);
  464. packet_cache_clear_old_packets( pcache, framecnt );
  465. return pkt_size;
  466. }
  467. float
  468. packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
  469. {
  470. int num_packets_before_us = 0;
  471. int i;
  472. for (i = 0; i < pcache->size; i++)
  473. {
  474. cache_packet *cpack = &(pcache->packets[i]);
  475. if (cpack->valid && cache_packet_is_complete( cpack ))
  476. if( cpack->framecnt >= expected_framecnt )
  477. num_packets_before_us += 1;
  478. }
  479. return 100.0 * (float)num_packets_before_us / (float)( pcache->size ) ;
  480. }
  481. // Returns 0 when no valid packet is inside the cache.
  482. int
  483. packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  484. {
  485. int i;
  486. jack_nframes_t best_offset = JACK_MAX_FRAMES/2-1;
  487. int retval = 0;
  488. for (i = 0; i < pcache->size; i++)
  489. {
  490. cache_packet *cpack = &(pcache->packets[i]);
  491. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  492. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  493. //printf( "invalid\n" );
  494. continue;
  495. }
  496. if( (cpack->framecnt - expected_framecnt) > best_offset ) {
  497. continue;
  498. }
  499. best_offset = cpack->framecnt - expected_framecnt;
  500. retval = 1;
  501. if( best_offset == 0 )
  502. break;
  503. }
  504. if( retval && framecnt )
  505. *framecnt = expected_framecnt + best_offset;
  506. return retval;
  507. }
  508. int
  509. packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt )
  510. {
  511. int i;
  512. jack_nframes_t best_value = 0;
  513. int retval = 0;
  514. for (i = 0; i < pcache->size; i++)
  515. {
  516. cache_packet *cpack = &(pcache->packets[i]);
  517. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  518. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  519. //printf( "invalid\n" );
  520. continue;
  521. }
  522. if (cpack->framecnt < best_value) {
  523. continue;
  524. }
  525. best_value = cpack->framecnt;
  526. retval = 1;
  527. }
  528. if( retval && framecnt )
  529. *framecnt = best_value;
  530. return retval;
  531. }
  532. // Returns 0 when no valid packet is inside the cache.
  533. int
  534. packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  535. {
  536. int i;
  537. jack_nframes_t best_offset = 0;
  538. int retval = 0;
  539. for (i = 0; i < pcache->size; i++)
  540. {
  541. cache_packet *cpack = &(pcache->packets[i]);
  542. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  543. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  544. //printf( "invalid\n" );
  545. continue;
  546. }
  547. if( (cpack->framecnt - expected_framecnt) < best_offset ) {
  548. continue;
  549. }
  550. best_offset = cpack->framecnt - expected_framecnt;
  551. retval = 1;
  552. if( best_offset == 0 )
  553. break;
  554. }
  555. if( retval && framecnt )
  556. *framecnt = JACK_MAX_FRAMES - best_offset;
  557. return retval;
  558. }
  559. // fragmented packet IO
  560. int
  561. netjack_recvfrom (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, socklen_t *addr_size, int mtu)
  562. {
  563. if (pkt_size <= mtu)
  564. return recvfrom (sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  565. char *rx_packet = alloca (mtu);
  566. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  567. int rcv_len;
  568. jack_nframes_t framecnt;
  569. cache_packet *cpack;
  570. do
  571. {
  572. rcv_len = recvfrom (sockfd, rx_packet, mtu, 0, addr, addr_size);
  573. if (rcv_len < 0)
  574. return rcv_len;
  575. framecnt = ntohl (pkthdr->framecnt);
  576. cpack = packet_cache_get_packet (global_packcache, framecnt);
  577. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  578. } while (!cache_packet_is_complete (cpack));
  579. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  580. cache_packet_reset (cpack);
  581. return pkt_size;
  582. }
  583. int
  584. netjack_recv (int sockfd, char *packet_buf, int pkt_size, int flags, int mtu)
  585. {
  586. if (pkt_size <= mtu)
  587. return recv (sockfd, packet_buf, pkt_size, flags);
  588. char *rx_packet = alloca (mtu);
  589. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  590. int rcv_len;
  591. jack_nframes_t framecnt;
  592. cache_packet *cpack;
  593. do
  594. {
  595. rcv_len = recv (sockfd, rx_packet, mtu, flags);
  596. if (rcv_len < 0)
  597. return rcv_len;
  598. framecnt = ntohl (pkthdr->framecnt);
  599. cpack = packet_cache_get_packet (global_packcache, framecnt);
  600. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  601. } while (!cache_packet_is_complete (cpack));
  602. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  603. cache_packet_reset (cpack);
  604. return pkt_size;
  605. }
  606. void
  607. netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
  608. {
  609. int frag_cnt = 0;
  610. char *tx_packet, *dataX;
  611. jacknet_packet_header *pkthdr;
  612. tx_packet = alloca (mtu + 10);
  613. dataX = tx_packet + sizeof (jacknet_packet_header);
  614. pkthdr = (jacknet_packet_header *) tx_packet;
  615. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  616. if (pkt_size <= mtu) {
  617. int err;
  618. pkthdr = (jacknet_packet_header *) packet_buf;
  619. pkthdr->fragment_nr = htonl (0);
  620. err = sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  621. if( err<0 ) {
  622. printf( "error in send\n" );
  623. perror( "send" );
  624. }
  625. }
  626. else
  627. {
  628. int err;
  629. // Copy the packet header to the tx pack first.
  630. memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
  631. // Now loop and send all
  632. char *packet_bufX = packet_buf + sizeof (jacknet_packet_header);
  633. while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size))
  634. {
  635. pkthdr->fragment_nr = htonl (frag_cnt++);
  636. memcpy (dataX, packet_bufX, fragment_payload_size);
  637. sendto (sockfd, tx_packet, mtu, flags, addr, addr_size);
  638. packet_bufX += fragment_payload_size;
  639. }
  640. int last_payload_size = packet_buf + pkt_size - packet_bufX;
  641. memcpy (dataX, packet_bufX, last_payload_size);
  642. pkthdr->fragment_nr = htonl (frag_cnt);
  643. //jack_error("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
  644. // sendto(last_pack_size);
  645. sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
  646. if( err<0 ) {
  647. printf( "error in send\n" );
  648. perror( "send" );
  649. }
  650. }
  651. }
  652. void
  653. decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  654. {
  655. int i;
  656. jack_midi_clear_buffer (buf);
  657. for (i = 0; i < buffer_size_uint32 - 3;)
  658. {
  659. uint32_t payload_size;
  660. payload_size = buffer_uint32[i];
  661. payload_size = ntohl (payload_size);
  662. if (payload_size)
  663. {
  664. jack_midi_event_t event;
  665. event.time = ntohl (buffer_uint32[i+1]);
  666. event.size = ntohl (buffer_uint32[i+2]);
  667. event.buffer = (jack_midi_data_t*) (&(buffer_uint32[i+3]));
  668. jack_midi_event_write (buf, event.time, event.buffer, event.size);
  669. // skip to the next event
  670. unsigned int nb_data_quads = (((event.size-1) & ~0x3) >> 2)+1;
  671. i += 3+nb_data_quads;
  672. }
  673. else
  674. break; // no events can follow an empty event, we're done
  675. }
  676. }
  677. void
  678. encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  679. {
  680. int i;
  681. unsigned int written = 0;
  682. // midi port, encode midi events
  683. unsigned int nevents = jack_midi_get_event_count (buf);
  684. for (i = 0; i < nevents; ++i)
  685. {
  686. jack_midi_event_t event;
  687. jack_midi_event_get (&event, buf, i);
  688. unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
  689. unsigned int payload_size = 3 + nb_data_quads;
  690. // only write if we have sufficient space for the event
  691. // otherwise drop it
  692. if (written + payload_size < buffer_size_uint32 - 1)
  693. {
  694. // write header
  695. buffer_uint32[written]=htonl (payload_size);
  696. written++;
  697. buffer_uint32[written]=htonl (event.time);
  698. written++;
  699. buffer_uint32[written]=htonl (event.size);
  700. written++;
  701. // write data
  702. jack_midi_data_t* tmpbuff = (jack_midi_data_t*)(&(buffer_uint32[written]));
  703. memcpy (tmpbuff, event.buffer, event.size);
  704. written += nb_data_quads;
  705. }
  706. else
  707. {
  708. // buffer overflow
  709. jack_error ("midi buffer overflow");
  710. break;
  711. }
  712. }
  713. // now put a netjack_midi 'no-payload' event, signaling EOF
  714. buffer_uint32[written]=0;
  715. }
  716. // render functions for float
  717. void
  718. 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)
  719. {
  720. channel_t chn = 0;
  721. JSList *node = capture_ports;
  722. JSList *src_node = capture_srcs;
  723. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  724. if( !packet_payload )
  725. return;
  726. while (node != NULL)
  727. {
  728. int i;
  729. int_float_t val;
  730. SRC_DATA src;
  731. jack_port_t *port = (jack_port_t *) node->data;
  732. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  733. const char *porttype = jack_port_type (port);
  734. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  735. {
  736. // audio port, resample if necessary
  737. if (net_period_down != nframes)
  738. {
  739. SRC_STATE *src_state = src_node->data;
  740. for (i = 0; i < net_period_down; i++)
  741. {
  742. packet_bufX[i] = ntohl (packet_bufX[i]);
  743. }
  744. src.data_in = (float *) packet_bufX;
  745. src.input_frames = net_period_down;
  746. src.data_out = buf;
  747. src.output_frames = nframes;
  748. src.src_ratio = (float) nframes / (float) net_period_down;
  749. src.end_of_input = 0;
  750. src_set_ratio (src_state, src.src_ratio);
  751. src_process (src_state, &src);
  752. src_node = jack_slist_next (src_node);
  753. }
  754. else
  755. {
  756. for (i = 0; i < net_period_down; i++)
  757. {
  758. val.i = packet_bufX[i];
  759. val.i = ntohl (val.i);
  760. buf[i] = val.f;
  761. }
  762. }
  763. }
  764. else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  765. {
  766. // midi port, decode midi events
  767. // convert the data buffer to a standard format (uint32_t based)
  768. unsigned int buffer_size_uint32 = net_period_down;
  769. uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
  770. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  771. }
  772. packet_bufX = (packet_bufX + net_period_down);
  773. node = jack_slist_next (node);
  774. chn++;
  775. }
  776. }
  777. void
  778. 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)
  779. {
  780. channel_t chn = 0;
  781. JSList *node = playback_ports;
  782. JSList *src_node = playback_srcs;
  783. uint32_t *packet_bufX = (uint32_t *) packet_payload;
  784. while (node != NULL)
  785. {
  786. SRC_DATA src;
  787. int i;
  788. int_float_t val;
  789. jack_port_t *port = (jack_port_t *) node->data;
  790. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  791. const char *porttype = jack_port_type (port);
  792. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  793. {
  794. // audio port, resample if necessary
  795. if (net_period_up != nframes) {
  796. SRC_STATE *src_state = src_node->data;
  797. src.data_in = buf;
  798. src.input_frames = nframes;
  799. src.data_out = (float *) packet_bufX;
  800. src.output_frames = net_period_up;
  801. src.src_ratio = (float) net_period_up / (float) nframes;
  802. src.end_of_input = 0;
  803. src_set_ratio (src_state, src.src_ratio);
  804. src_process (src_state, &src);
  805. for (i = 0; i < net_period_up; i++)
  806. {
  807. packet_bufX[i] = htonl (packet_bufX[i]);
  808. }
  809. src_node = jack_slist_next (src_node);
  810. }
  811. else
  812. {
  813. for (i = 0; i < net_period_up; i++)
  814. {
  815. val.f = buf[i];
  816. val.i = htonl (val.i);
  817. packet_bufX[i] = val.i;
  818. }
  819. }
  820. }
  821. else if (strncmp(porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  822. {
  823. // encode midi events from port to packet
  824. // convert the data buffer to a standard format (uint32_t based)
  825. unsigned int buffer_size_uint32 = net_period_up;
  826. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  827. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  828. }
  829. packet_bufX = (packet_bufX + net_period_up);
  830. node = jack_slist_next (node);
  831. chn++;
  832. }
  833. }
  834. // render functions for 16bit
  835. void
  836. 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)
  837. {
  838. channel_t chn = 0;
  839. JSList *node = capture_ports;
  840. JSList *src_node = capture_srcs;
  841. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  842. if( !packet_payload )
  843. return;
  844. while (node != NULL)
  845. {
  846. int i;
  847. //uint32_t val;
  848. SRC_DATA src;
  849. jack_port_t *port = (jack_port_t *) node->data;
  850. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  851. float *floatbuf = alloca (sizeof(float) * net_period_down);
  852. const char *portname = jack_port_type (port);
  853. if (strncmp(portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  854. {
  855. // audio port, resample if necessary
  856. if (net_period_down != nframes)
  857. {
  858. SRC_STATE *src_state = src_node->data;
  859. for (i = 0; i < net_period_down; i++)
  860. {
  861. floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
  862. }
  863. src.data_in = floatbuf;
  864. src.input_frames = net_period_down;
  865. src.data_out = buf;
  866. src.output_frames = nframes;
  867. src.src_ratio = (float) nframes / (float) net_period_down;
  868. src.end_of_input = 0;
  869. src_set_ratio (src_state, src.src_ratio);
  870. src_process (src_state, &src);
  871. src_node = jack_slist_next (src_node);
  872. }
  873. else
  874. for (i = 0; i < net_period_down; i++)
  875. buf[i] = ((float) ntohs (packet_bufX[i])) / 32768.0 - 1.0;
  876. }
  877. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  878. {
  879. // midi port, decode midi events
  880. // convert the data buffer to a standard format (uint32_t based)
  881. unsigned int buffer_size_uint32 = net_period_down / 2;
  882. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  883. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  884. }
  885. packet_bufX = (packet_bufX + net_period_down);
  886. node = jack_slist_next (node);
  887. chn++;
  888. }
  889. }
  890. void
  891. 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)
  892. {
  893. channel_t chn = 0;
  894. JSList *node = playback_ports;
  895. JSList *src_node = playback_srcs;
  896. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  897. while (node != NULL)
  898. {
  899. SRC_DATA src;
  900. int i;
  901. jack_port_t *port = (jack_port_t *) node->data;
  902. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  903. const char *portname = jack_port_type (port);
  904. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  905. {
  906. // audio port, resample if necessary
  907. if (net_period_up != nframes)
  908. {
  909. SRC_STATE *src_state = src_node->data;
  910. float *floatbuf = alloca (sizeof(float) * net_period_up);
  911. src.data_in = buf;
  912. src.input_frames = nframes;
  913. src.data_out = floatbuf;
  914. src.output_frames = net_period_up;
  915. src.src_ratio = (float) net_period_up / (float) nframes;
  916. src.end_of_input = 0;
  917. src_set_ratio (src_state, src.src_ratio);
  918. src_process (src_state, &src);
  919. for (i = 0; i < net_period_up; i++)
  920. {
  921. packet_bufX[i] = htons (((uint16_t)((floatbuf[i] + 1.0) * 32767.0)));
  922. }
  923. src_node = jack_slist_next (src_node);
  924. }
  925. else
  926. for (i = 0; i < net_period_up; i++)
  927. packet_bufX[i] = htons(((uint16_t)((buf[i] + 1.0) * 32767.0)));
  928. }
  929. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  930. {
  931. // encode midi events from port to packet
  932. // convert the data buffer to a standard format (uint32_t based)
  933. unsigned int buffer_size_uint32 = net_period_up / 2;
  934. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  935. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  936. }
  937. packet_bufX = (packet_bufX + net_period_up);
  938. node = jack_slist_next (node);
  939. chn++;
  940. }
  941. }
  942. // render functions for 8bit
  943. void
  944. 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)
  945. {
  946. channel_t chn = 0;
  947. JSList *node = capture_ports;
  948. JSList *src_node = capture_srcs;
  949. int8_t *packet_bufX = (int8_t *)packet_payload;
  950. if( !packet_payload )
  951. return;
  952. while (node != NULL)
  953. {
  954. int i;
  955. //uint32_t val;
  956. SRC_DATA src;
  957. jack_port_t *port = (jack_port_t *) node->data;
  958. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  959. float *floatbuf = alloca (sizeof (float) * net_period_down);
  960. const char *portname = jack_port_type (port);
  961. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  962. {
  963. // audio port, resample if necessary
  964. if (net_period_down != nframes)
  965. {
  966. SRC_STATE *src_state = src_node->data;
  967. for (i = 0; i < net_period_down; i++)
  968. floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
  969. src.data_in = floatbuf;
  970. src.input_frames = net_period_down;
  971. src.data_out = buf;
  972. src.output_frames = nframes;
  973. src.src_ratio = (float) nframes / (float) net_period_down;
  974. src.end_of_input = 0;
  975. src_set_ratio (src_state, src.src_ratio);
  976. src_process (src_state, &src);
  977. src_node = jack_slist_next (src_node);
  978. }
  979. else
  980. for (i = 0; i < net_period_down; i++)
  981. buf[i] = ((float) packet_bufX[i]) / 127.0;
  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. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  990. }
  991. packet_bufX = (packet_bufX + net_period_down);
  992. node = jack_slist_next (node);
  993. chn++;
  994. }
  995. }
  996. void
  997. 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)
  998. {
  999. channel_t chn = 0;
  1000. JSList *node = playback_ports;
  1001. JSList *src_node = playback_srcs;
  1002. int8_t *packet_bufX = (int8_t *)packet_payload;
  1003. while (node != NULL)
  1004. {
  1005. SRC_DATA src;
  1006. int i;
  1007. jack_port_t *port = (jack_port_t *) node->data;
  1008. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1009. const char *portname = jack_port_type (port);
  1010. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  1011. {
  1012. // audio port, resample if necessary
  1013. if (net_period_up != nframes)
  1014. {
  1015. SRC_STATE *src_state = src_node->data;
  1016. float *floatbuf = alloca (sizeof (float) * net_period_up);
  1017. src.data_in = buf;
  1018. src.input_frames = nframes;
  1019. src.data_out = floatbuf;
  1020. src.output_frames = net_period_up;
  1021. src.src_ratio = (float) net_period_up / (float) nframes;
  1022. src.end_of_input = 0;
  1023. src_set_ratio (src_state, src.src_ratio);
  1024. src_process (src_state, &src);
  1025. for (i = 0; i < net_period_up; i++)
  1026. packet_bufX[i] = floatbuf[i] * 127.0;
  1027. src_node = jack_slist_next (src_node);
  1028. }
  1029. else
  1030. for (i = 0; i < net_period_up; i++)
  1031. packet_bufX[i] = buf[i] * 127.0;
  1032. }
  1033. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  1034. {
  1035. // encode midi events from port to packet
  1036. // convert the data buffer to a standard format (uint32_t based)
  1037. unsigned int buffer_size_uint32 = net_period_up / 4;
  1038. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1039. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1040. }
  1041. packet_bufX = (packet_bufX + net_period_up);
  1042. node = jack_slist_next (node);
  1043. chn++;
  1044. }
  1045. }
  1046. #if HAVE_CELT
  1047. // render functions for celt.
  1048. void
  1049. 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)
  1050. {
  1051. channel_t chn = 0;
  1052. JSList *node = capture_ports;
  1053. JSList *src_node = capture_srcs;
  1054. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1055. while (node != NULL)
  1056. {
  1057. jack_port_t *port = (jack_port_t *) node->data;
  1058. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1059. const char *portname = jack_port_type (port);
  1060. if (strncmp(portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  1061. {
  1062. // audio port, decode celt data.
  1063. CELTDecoder *decoder = src_node->data;
  1064. if( !packet_payload )
  1065. celt_decode_float( decoder, NULL, net_period_down, buf );
  1066. else
  1067. celt_decode_float( decoder, packet_bufX, net_period_down, buf );
  1068. src_node = jack_slist_next (src_node);
  1069. }
  1070. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  1071. {
  1072. // midi port, decode midi events
  1073. // convert the data buffer to a standard format (uint32_t based)
  1074. unsigned int buffer_size_uint32 = net_period_down / 2;
  1075. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1076. if( packet_payload )
  1077. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1078. }
  1079. packet_bufX = (packet_bufX + net_period_down);
  1080. node = jack_slist_next (node);
  1081. chn++;
  1082. }
  1083. }
  1084. void
  1085. 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)
  1086. {
  1087. channel_t chn = 0;
  1088. JSList *node = playback_ports;
  1089. JSList *src_node = playback_srcs;
  1090. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1091. while (node != NULL)
  1092. {
  1093. jack_port_t *port = (jack_port_t *) node->data;
  1094. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1095. const char *portname = jack_port_type (port);
  1096. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  1097. {
  1098. // audio port, encode celt data.
  1099. int encoded_bytes;
  1100. float *floatbuf = alloca (sizeof(float) * nframes );
  1101. memcpy( floatbuf, buf, nframes*sizeof(float) );
  1102. CELTEncoder *encoder = src_node->data;
  1103. encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
  1104. if( encoded_bytes != net_period_up )
  1105. printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
  1106. src_node = jack_slist_next( src_node );
  1107. }
  1108. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  1109. {
  1110. // encode midi events from port to packet
  1111. // convert the data buffer to a standard format (uint32_t based)
  1112. unsigned int buffer_size_uint32 = net_period_up / 2;
  1113. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1114. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1115. }
  1116. packet_bufX = (packet_bufX + net_period_up);
  1117. node = jack_slist_next (node);
  1118. chn++;
  1119. }
  1120. }
  1121. #endif
  1122. /* Wrapper functions with bitdepth argument... */
  1123. void
  1124. 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)
  1125. {
  1126. if (bitdepth == 8)
  1127. render_payload_to_jack_ports_8bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1128. else if (bitdepth == 16)
  1129. render_payload_to_jack_ports_16bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1130. #if HAVE_CELT
  1131. else if (bitdepth == 1000)
  1132. render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1133. #endif
  1134. else
  1135. render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1136. }
  1137. void
  1138. 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)
  1139. {
  1140. if (bitdepth == 8)
  1141. render_jack_ports_to_payload_8bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1142. else if (bitdepth == 16)
  1143. render_jack_ports_to_payload_16bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1144. #if HAVE_CELT
  1145. else if (bitdepth == 1000)
  1146. render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1147. #endif
  1148. else
  1149. render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1150. }