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.

1481 lines
47KB

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