jack2 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.

1525 lines
44KB

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