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.

1483 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. #ifndef __QNXNTO__
  362. action.sa_flags = SA_RESTART;
  363. #endif
  364. for (i = 1; i < NSIG; i++)
  365. if (sigismember (&sigmask, i))
  366. sigaction (i, &action, 0);
  367. fds.fd = sockfd;
  368. fds.events = POLLIN;
  369. sigprocmask(SIG_UNBLOCK, &sigmask, &rsigmask);
  370. while (poll_err == 0) {
  371. poll_err = poll (&fds, 1, timeout);
  372. }
  373. sigprocmask(SIG_SETMASK, &rsigmask, NULL);
  374. if (poll_err == -1) {
  375. switch (errno) {
  376. case EBADF:
  377. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  378. break;
  379. case EFAULT:
  380. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  381. break;
  382. case EINTR:
  383. jack_error ("Error %d: A signal occurred before any requested event", errno);
  384. break;
  385. case EINVAL:
  386. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  387. break;
  388. case ENOMEM:
  389. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  390. break;
  391. }
  392. return 0;
  393. }
  394. return 1;
  395. }
  396. #else
  397. int
  398. netjack_poll (int sockfd, int timeout)
  399. {
  400. jack_error( "netjack_poll not implemented" );
  401. return 0;
  402. }
  403. int
  404. netjack_poll_deadline (int sockfd, jack_time_t deadline)
  405. {
  406. fd_set fds;
  407. FD_ZERO( &fds );
  408. FD_SET( sockfd, &fds );
  409. struct timeval timeout;
  410. while( 1 ) {
  411. jack_time_t now = jack_get_time();
  412. if( now >= deadline )
  413. return 0;
  414. int timeout_usecs = (deadline - now);
  415. //jack_error( "timeout = %d", timeout_usecs );
  416. timeout.tv_sec = 0;
  417. timeout.tv_usec = (timeout_usecs < 500) ? 500 : timeout_usecs;
  418. timeout.tv_usec = (timeout_usecs > 1000000) ? 500000 : timeout_usecs;
  419. int poll_err = select (0, &fds, NULL, NULL, &timeout);
  420. if( poll_err != 0 )
  421. return poll_err;
  422. }
  423. return 0;
  424. }
  425. #endif
  426. // This now reads all a socket has into the cache.
  427. // replacing netjack_recv functions.
  428. void
  429. packet_cache_drain_socket( packet_cache *pcache, int sockfd )
  430. {
  431. char *rx_packet = alloca (pcache->mtu);
  432. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  433. int rcv_len;
  434. jack_nframes_t framecnt;
  435. cache_packet *cpack;
  436. struct sockaddr_in sender_address;
  437. #ifdef WIN32
  438. int senderlen = sizeof( struct sockaddr_in );
  439. u_long parm = 1;
  440. ioctlsocket( sockfd, FIONBIO, &parm );
  441. #else
  442. unsigned int senderlen = sizeof( struct sockaddr_in );
  443. #endif
  444. while (1) {
  445. #ifdef WIN32
  446. rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, 0,
  447. (struct sockaddr*) &sender_address, &senderlen);
  448. #else
  449. rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT,
  450. (struct sockaddr*) &sender_address, &senderlen);
  451. #endif
  452. if (rcv_len < 0)
  453. return;
  454. if (pcache->master_address_valid) {
  455. // Verify its from our master.
  456. if (memcmp (&sender_address, &(pcache->master_address), senderlen) != 0)
  457. continue;
  458. } else {
  459. // Setup this one as master
  460. //printf( "setup master...\n" );
  461. memcpy ( &(pcache->master_address), &sender_address, senderlen );
  462. pcache->master_address_valid = 1;
  463. }
  464. framecnt = ntohl (pkthdr->framecnt);
  465. if( pcache->last_framecnt_retreived_valid && (framecnt <= pcache->last_framecnt_retreived ))
  466. continue;
  467. cpack = packet_cache_get_packet (pcache, framecnt);
  468. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  469. cpack->recv_timestamp = jack_get_time();
  470. }
  471. }
  472. void
  473. packet_cache_reset_master_address( packet_cache *pcache )
  474. {
  475. pcache->master_address_valid = 0;
  476. pcache->last_framecnt_retreived = 0;
  477. pcache->last_framecnt_retreived_valid = 0;
  478. }
  479. void
  480. packet_cache_clear_old_packets (packet_cache *pcache, jack_nframes_t framecnt )
  481. {
  482. int i;
  483. for (i = 0; i < pcache->size; i++) {
  484. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < framecnt)) {
  485. cache_packet_reset (&(pcache->packets[i]));
  486. }
  487. }
  488. }
  489. int
  490. packet_cache_retreive_packet_pointer( packet_cache *pcache, jack_nframes_t framecnt, char **packet_buf, int pkt_size, jack_time_t *timestamp )
  491. {
  492. int i;
  493. cache_packet *cpack = NULL;
  494. for (i = 0; i < pcache->size; i++) {
  495. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  496. cpack = &(pcache->packets[i]);
  497. break;
  498. }
  499. }
  500. if( cpack == NULL ) {
  501. //printf( "retrieve packet: %d....not found\n", framecnt );
  502. return -1;
  503. }
  504. if( !cache_packet_is_complete( cpack ) ) {
  505. return -1;
  506. }
  507. // ok. cpack is the one we want and its complete.
  508. *packet_buf = cpack->packet_buf;
  509. if( timestamp )
  510. *timestamp = cpack->recv_timestamp;
  511. pcache->last_framecnt_retreived_valid = 1;
  512. pcache->last_framecnt_retreived = framecnt;
  513. return pkt_size;
  514. }
  515. int
  516. packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt )
  517. {
  518. int i;
  519. cache_packet *cpack = NULL;
  520. for (i = 0; i < pcache->size; i++) {
  521. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  522. cpack = &(pcache->packets[i]);
  523. break;
  524. }
  525. }
  526. if( cpack == NULL ) {
  527. //printf( "retrieve packet: %d....not found\n", framecnt );
  528. return -1;
  529. }
  530. if( !cache_packet_is_complete( cpack ) ) {
  531. return -1;
  532. }
  533. cache_packet_reset (cpack);
  534. packet_cache_clear_old_packets( pcache, framecnt );
  535. return 0;
  536. }
  537. float
  538. packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
  539. {
  540. int num_packets_before_us = 0;
  541. int i;
  542. for (i = 0; i < pcache->size; i++) {
  543. cache_packet *cpack = &(pcache->packets[i]);
  544. if (cpack->valid && cache_packet_is_complete( cpack ))
  545. if( cpack->framecnt >= expected_framecnt )
  546. num_packets_before_us += 1;
  547. }
  548. return 100.0 * (float)num_packets_before_us / (float)( pcache->size );
  549. }
  550. // Returns 0 when no valid packet is inside the cache.
  551. int
  552. packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  553. {
  554. int i;
  555. jack_nframes_t best_offset = JACK_MAX_FRAMES / 2 - 1;
  556. int retval = 0;
  557. for (i = 0; i < pcache->size; i++) {
  558. cache_packet *cpack = &(pcache->packets[i]);
  559. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  560. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  561. //printf( "invalid\n" );
  562. continue;
  563. }
  564. if( cpack->framecnt < expected_framecnt )
  565. continue;
  566. if( (cpack->framecnt - expected_framecnt) > best_offset ) {
  567. continue;
  568. }
  569. best_offset = cpack->framecnt - expected_framecnt;
  570. retval = 1;
  571. if (best_offset == 0)
  572. break;
  573. }
  574. if (retval && framecnt)
  575. *framecnt = expected_framecnt + best_offset;
  576. return retval;
  577. }
  578. int
  579. packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt )
  580. {
  581. int i;
  582. jack_nframes_t best_value = 0;
  583. int retval = 0;
  584. for (i = 0; i < pcache->size; i++) {
  585. cache_packet *cpack = &(pcache->packets[i]);
  586. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  587. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  588. //printf( "invalid\n" );
  589. continue;
  590. }
  591. if (cpack->framecnt < best_value) {
  592. continue;
  593. }
  594. best_value = cpack->framecnt;
  595. retval = 1;
  596. }
  597. if (retval && framecnt)
  598. *framecnt = best_value;
  599. return retval;
  600. }
  601. // Returns 0 when no valid packet is inside the cache.
  602. int
  603. packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  604. {
  605. int i;
  606. jack_nframes_t best_offset = 0;
  607. int retval = 0;
  608. for (i = 0; i < pcache->size; i++) {
  609. cache_packet *cpack = &(pcache->packets[i]);
  610. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  611. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  612. //printf( "invalid\n" );
  613. continue;
  614. }
  615. if ((cpack->framecnt - expected_framecnt) < best_offset) {
  616. continue;
  617. }
  618. best_offset = cpack->framecnt - expected_framecnt;
  619. retval = 1;
  620. if( best_offset == 0 )
  621. break;
  622. }
  623. if (retval && framecnt)
  624. *framecnt = JACK_MAX_FRAMES - best_offset;
  625. return retval;
  626. }
  627. // fragmented packet IO
  628. void
  629. netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
  630. {
  631. int frag_cnt = 0;
  632. char *tx_packet, *dataX;
  633. jacknet_packet_header *pkthdr;
  634. tx_packet = alloca (mtu + 10);
  635. dataX = tx_packet + sizeof (jacknet_packet_header);
  636. pkthdr = (jacknet_packet_header *) tx_packet;
  637. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  638. if (pkt_size <= mtu) {
  639. int err;
  640. pkthdr = (jacknet_packet_header *) packet_buf;
  641. pkthdr->fragment_nr = htonl (0);
  642. err = sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  643. if( err < 0 ) {
  644. //printf( "error in send\n" );
  645. perror( "send" );
  646. }
  647. } else {
  648. int err;
  649. // Copy the packet header to the tx pack first.
  650. memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
  651. // Now loop and send all
  652. char *packet_bufX = packet_buf + sizeof (jacknet_packet_header);
  653. while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size)) {
  654. pkthdr->fragment_nr = htonl (frag_cnt++);
  655. memcpy (dataX, packet_bufX, fragment_payload_size);
  656. sendto (sockfd, tx_packet, mtu, flags, addr, addr_size);
  657. packet_bufX += fragment_payload_size;
  658. }
  659. int last_payload_size = packet_buf + pkt_size - packet_bufX;
  660. memcpy (dataX, packet_bufX, last_payload_size);
  661. pkthdr->fragment_nr = htonl (frag_cnt);
  662. //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
  663. // sendto(last_pack_size);
  664. err = sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
  665. if( err < 0 ) {
  666. //printf( "error in send\n" );
  667. perror( "send" );
  668. }
  669. }
  670. }
  671. void
  672. decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  673. {
  674. int i;
  675. jack_midi_clear_buffer (buf);
  676. for (i = 0; i < buffer_size_uint32 - 3;) {
  677. uint32_t payload_size;
  678. payload_size = buffer_uint32[i];
  679. payload_size = ntohl (payload_size);
  680. if (payload_size) {
  681. jack_midi_event_t event;
  682. event.time = ntohl (buffer_uint32[i + 1]);
  683. event.size = ntohl (buffer_uint32[i + 2]);
  684. event.buffer = (jack_midi_data_t*) (&(buffer_uint32[i + 3]));
  685. jack_midi_event_write (buf, event.time, event.buffer, event.size);
  686. // skip to the next event
  687. unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
  688. i += 3 + nb_data_quads;
  689. } else
  690. break; // no events can follow an empty event, we're done
  691. }
  692. }
  693. void
  694. encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  695. {
  696. int i;
  697. unsigned int written = 0;
  698. // midi port, encode midi events
  699. unsigned int nevents = jack_midi_get_event_count (buf);
  700. for (i = 0; i < nevents; ++i) {
  701. jack_midi_event_t event;
  702. jack_midi_event_get (&event, buf, i);
  703. unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
  704. unsigned int payload_size = 3 + nb_data_quads;
  705. // only write if we have sufficient space for the event
  706. // otherwise drop it
  707. if (written + payload_size < buffer_size_uint32 - 1) {
  708. // write header
  709. buffer_uint32[written] = htonl (payload_size);
  710. written++;
  711. buffer_uint32[written] = htonl (event.time);
  712. written++;
  713. buffer_uint32[written] = htonl (event.size);
  714. written++;
  715. // write data
  716. jack_midi_data_t* tmpbuff = (jack_midi_data_t*)(&(buffer_uint32[written]));
  717. memcpy (tmpbuff, event.buffer, event.size);
  718. written += nb_data_quads;
  719. } else {
  720. // buffer overflow
  721. jack_error ("midi buffer overflow");
  722. break;
  723. }
  724. }
  725. // now put a netjack_midi 'no-payload' event, signaling EOF
  726. buffer_uint32[written] = 0;
  727. }
  728. // render functions for float
  729. void
  730. 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)
  731. {
  732. int chn = 0;
  733. JSList *node = capture_ports;
  734. #if HAVE_SAMPLERATE
  735. JSList *src_node = capture_srcs;
  736. #endif
  737. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  738. if (!packet_payload)
  739. return;
  740. while (node != NULL) {
  741. int i;
  742. int_float_t val;
  743. #if HAVE_SAMPLERATE
  744. SRC_DATA src;
  745. #endif
  746. jack_port_t *port = (jack_port_t *) node->data;
  747. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  748. const char *porttype = jack_port_type (port);
  749. if (jack_port_is_audio (porttype)) {
  750. #if HAVE_SAMPLERATE
  751. // audio port, resample if necessary
  752. if (net_period_down != nframes) {
  753. SRC_STATE *src_state = src_node->data;
  754. for (i = 0; i < net_period_down; i++) {
  755. packet_bufX[i] = ntohl (packet_bufX[i]);
  756. }
  757. src.data_in = (float *) packet_bufX;
  758. src.input_frames = net_period_down;
  759. src.data_out = buf;
  760. src.output_frames = nframes;
  761. src.src_ratio = (float) nframes / (float) net_period_down;
  762. src.end_of_input = 0;
  763. src_set_ratio (src_state, src.src_ratio);
  764. src_process (src_state, &src);
  765. src_node = jack_slist_next (src_node);
  766. } else
  767. #endif
  768. {
  769. if( dont_htonl_floats ) {
  770. memcpy( buf, packet_bufX, net_period_down * sizeof(jack_default_audio_sample_t));
  771. } else {
  772. for (i = 0; i < net_period_down; i++) {
  773. val.i = packet_bufX[i];
  774. val.i = ntohl (val.i);
  775. buf[i] = val.f;
  776. }
  777. }
  778. }
  779. } else if (jack_port_is_midi (porttype)) {
  780. // midi port, decode midi events
  781. // convert the data buffer to a standard format (uint32_t based)
  782. unsigned int buffer_size_uint32 = net_period_down;
  783. uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
  784. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  785. }
  786. packet_bufX = (packet_bufX + net_period_down);
  787. node = jack_slist_next (node);
  788. chn++;
  789. }
  790. }
  791. void
  792. 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 )
  793. {
  794. int chn = 0;
  795. JSList *node = playback_ports;
  796. #if HAVE_SAMPLERATE
  797. JSList *src_node = playback_srcs;
  798. #endif
  799. uint32_t *packet_bufX = (uint32_t *) packet_payload;
  800. while (node != NULL) {
  801. #if HAVE_SAMPLERATE
  802. SRC_DATA src;
  803. #endif
  804. int i;
  805. int_float_t val;
  806. jack_port_t *port = (jack_port_t *) node->data;
  807. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  808. const char *porttype = jack_port_type (port);
  809. if (jack_port_is_audio (porttype)) {
  810. // audio port, resample if necessary
  811. #if HAVE_SAMPLERATE
  812. if (net_period_up != nframes) {
  813. SRC_STATE *src_state = src_node->data;
  814. src.data_in = buf;
  815. src.input_frames = nframes;
  816. src.data_out = (float *) packet_bufX;
  817. src.output_frames = net_period_up;
  818. src.src_ratio = (float) net_period_up / (float) nframes;
  819. src.end_of_input = 0;
  820. src_set_ratio (src_state, src.src_ratio);
  821. src_process (src_state, &src);
  822. for (i = 0; i < net_period_up; i++) {
  823. packet_bufX[i] = htonl (packet_bufX[i]);
  824. }
  825. src_node = jack_slist_next (src_node);
  826. } else
  827. #endif
  828. {
  829. if( dont_htonl_floats ) {
  830. memcpy( packet_bufX, buf, net_period_up * sizeof(jack_default_audio_sample_t) );
  831. } else {
  832. for (i = 0; i < net_period_up; i++) {
  833. val.f = buf[i];
  834. val.i = htonl (val.i);
  835. packet_bufX[i] = val.i;
  836. }
  837. }
  838. }
  839. } else if (jack_port_is_midi (porttype)) {
  840. // encode midi events from port to packet
  841. // convert the data buffer to a standard format (uint32_t based)
  842. unsigned int buffer_size_uint32 = net_period_up;
  843. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  844. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  845. }
  846. packet_bufX = (packet_bufX + net_period_up);
  847. node = jack_slist_next (node);
  848. chn++;
  849. }
  850. }
  851. // render functions for 16bit
  852. void
  853. 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)
  854. {
  855. int chn = 0;
  856. JSList *node = capture_ports;
  857. #if HAVE_SAMPLERATE
  858. JSList *src_node = capture_srcs;
  859. #endif
  860. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  861. if( !packet_payload )
  862. return;
  863. while (node != NULL) {
  864. int i;
  865. //uint32_t val;
  866. #if HAVE_SAMPLERATE
  867. SRC_DATA src;
  868. #endif
  869. jack_port_t *port = (jack_port_t *) node->data;
  870. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  871. #if HAVE_SAMPLERATE
  872. float *floatbuf = alloca (sizeof(float) * net_period_down);
  873. #endif
  874. const char *porttype = jack_port_type (port);
  875. if (jack_port_is_audio (porttype)) {
  876. // audio port, resample if necessary
  877. #if HAVE_SAMPLERATE
  878. if (net_period_down != nframes) {
  879. SRC_STATE *src_state = src_node->data;
  880. for (i = 0; i < net_period_down; i++) {
  881. floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
  882. }
  883. src.data_in = floatbuf;
  884. src.input_frames = net_period_down;
  885. src.data_out = buf;
  886. src.output_frames = nframes;
  887. src.src_ratio = (float) nframes / (float) net_period_down;
  888. src.end_of_input = 0;
  889. src_set_ratio (src_state, src.src_ratio);
  890. src_process (src_state, &src);
  891. src_node = jack_slist_next (src_node);
  892. } else
  893. #endif
  894. for (i = 0; i < net_period_down; i++)
  895. buf[i] = ((float) ntohs (packet_bufX[i])) / 32768.0 - 1.0;
  896. } else if (jack_port_is_midi (porttype)) {
  897. // midi port, decode midi events
  898. // convert the data buffer to a standard format (uint32_t based)
  899. unsigned int buffer_size_uint32 = net_period_down / 2;
  900. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  901. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  902. }
  903. packet_bufX = (packet_bufX + net_period_down);
  904. node = jack_slist_next (node);
  905. chn++;
  906. }
  907. }
  908. void
  909. 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)
  910. {
  911. int chn = 0;
  912. JSList *node = playback_ports;
  913. #if HAVE_SAMPLERATE
  914. JSList *src_node = playback_srcs;
  915. #endif
  916. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  917. while (node != NULL) {
  918. #if HAVE_SAMPLERATE
  919. SRC_DATA src;
  920. #endif
  921. int i;
  922. jack_port_t *port = (jack_port_t *) node->data;
  923. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  924. const char *porttype = jack_port_type (port);
  925. if (jack_port_is_audio (porttype)) {
  926. // audio port, resample if necessary
  927. #if HAVE_SAMPLERATE
  928. if (net_period_up != nframes) {
  929. SRC_STATE *src_state = src_node->data;
  930. float *floatbuf = alloca (sizeof(float) * net_period_up);
  931. src.data_in = buf;
  932. src.input_frames = nframes;
  933. src.data_out = floatbuf;
  934. src.output_frames = net_period_up;
  935. src.src_ratio = (float) net_period_up / (float) nframes;
  936. src.end_of_input = 0;
  937. src_set_ratio (src_state, src.src_ratio);
  938. src_process (src_state, &src);
  939. for (i = 0; i < net_period_up; i++) {
  940. packet_bufX[i] = htons (((uint16_t)((floatbuf[i] + 1.0) * 32767.0)));
  941. }
  942. src_node = jack_slist_next (src_node);
  943. } else
  944. #endif
  945. for (i = 0; i < net_period_up; i++)
  946. packet_bufX[i] = htons(((uint16_t)((buf[i] + 1.0) * 32767.0)));
  947. } else if (jack_port_is_midi (porttype)) {
  948. // encode midi events from port to packet
  949. // convert the data buffer to a standard format (uint32_t based)
  950. unsigned int buffer_size_uint32 = net_period_up / 2;
  951. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  952. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  953. }
  954. packet_bufX = (packet_bufX + net_period_up);
  955. node = jack_slist_next (node);
  956. chn++;
  957. }
  958. }
  959. // render functions for 8bit
  960. void
  961. 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)
  962. {
  963. int chn = 0;
  964. JSList *node = capture_ports;
  965. #if HAVE_SAMPLERATE
  966. JSList *src_node = capture_srcs;
  967. #endif
  968. int8_t *packet_bufX = (int8_t *)packet_payload;
  969. if (!packet_payload)
  970. return;
  971. while (node != NULL) {
  972. int i;
  973. //uint32_t val;
  974. #if HAVE_SAMPLERATE
  975. SRC_DATA src;
  976. #endif
  977. jack_port_t *port = (jack_port_t *) node->data;
  978. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  979. #if HAVE_SAMPLERATE
  980. float *floatbuf = alloca (sizeof (float) * net_period_down);
  981. #endif
  982. const char *porttype = jack_port_type (port);
  983. if (jack_port_is_audio(porttype)) {
  984. #if HAVE_SAMPLERATE
  985. // audio port, resample if necessary
  986. if (net_period_down != nframes) {
  987. SRC_STATE *src_state = src_node->data;
  988. for (i = 0; i < net_period_down; i++)
  989. floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
  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. } else
  1000. #endif
  1001. for (i = 0; i < net_period_down; i++)
  1002. buf[i] = ((float) packet_bufX[i]) / 127.0;
  1003. } else if (jack_port_is_midi (porttype)) {
  1004. // midi port, decode midi events
  1005. // convert the data buffer to a standard format (uint32_t based)
  1006. unsigned int buffer_size_uint32 = net_period_down / 2;
  1007. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1008. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1009. }
  1010. packet_bufX = (packet_bufX + net_period_down);
  1011. node = jack_slist_next (node);
  1012. chn++;
  1013. }
  1014. }
  1015. void
  1016. 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)
  1017. {
  1018. int chn = 0;
  1019. JSList *node = playback_ports;
  1020. #if HAVE_SAMPLERATE
  1021. JSList *src_node = playback_srcs;
  1022. #endif
  1023. int8_t *packet_bufX = (int8_t *)packet_payload;
  1024. while (node != NULL) {
  1025. #if HAVE_SAMPLERATE
  1026. SRC_DATA src;
  1027. #endif
  1028. int i;
  1029. jack_port_t *port = (jack_port_t *) node->data;
  1030. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1031. const char *porttype = jack_port_type (port);
  1032. if (jack_port_is_audio (porttype)) {
  1033. #if HAVE_SAMPLERATE
  1034. // audio port, resample if necessary
  1035. if (net_period_up != nframes) {
  1036. SRC_STATE *src_state = src_node->data;
  1037. float *floatbuf = alloca (sizeof (float) * net_period_up);
  1038. src.data_in = buf;
  1039. src.input_frames = nframes;
  1040. src.data_out = floatbuf;
  1041. src.output_frames = net_period_up;
  1042. src.src_ratio = (float) net_period_up / (float) nframes;
  1043. src.end_of_input = 0;
  1044. src_set_ratio (src_state, src.src_ratio);
  1045. src_process (src_state, &src);
  1046. for (i = 0; i < net_period_up; i++)
  1047. packet_bufX[i] = floatbuf[i] * 127.0;
  1048. src_node = jack_slist_next (src_node);
  1049. } else
  1050. #endif
  1051. for (i = 0; i < net_period_up; i++)
  1052. packet_bufX[i] = buf[i] * 127.0;
  1053. } else if (jack_port_is_midi (porttype)) {
  1054. // encode midi events from port to packet
  1055. // convert the data buffer to a standard format (uint32_t based)
  1056. unsigned int buffer_size_uint32 = net_period_up / 4;
  1057. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1058. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1059. }
  1060. packet_bufX = (packet_bufX + net_period_up);
  1061. node = jack_slist_next (node);
  1062. chn++;
  1063. }
  1064. }
  1065. #if HAVE_CELT
  1066. // render functions for celt.
  1067. void
  1068. 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)
  1069. {
  1070. int chn = 0;
  1071. JSList *node = capture_ports;
  1072. JSList *src_node = capture_srcs;
  1073. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1074. while (node != NULL) {
  1075. jack_port_t *port = (jack_port_t *) node->data;
  1076. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1077. const char *porttype = jack_port_type (port);
  1078. if (jack_port_is_audio (porttype)) {
  1079. // audio port, decode celt data.
  1080. CELTDecoder *decoder = src_node->data;
  1081. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  1082. if( !packet_payload )
  1083. celt_decode_float( decoder, NULL, net_period_down, buf, nframes );
  1084. else
  1085. celt_decode_float( decoder, packet_bufX, net_period_down, buf, nframes );
  1086. #else
  1087. if( !packet_payload )
  1088. celt_decode_float( decoder, NULL, net_period_down, buf );
  1089. else
  1090. celt_decode_float( decoder, packet_bufX, net_period_down, buf );
  1091. #endif
  1092. src_node = jack_slist_next (src_node);
  1093. } else if (jack_port_is_midi (porttype)) {
  1094. // midi port, decode midi events
  1095. // convert the data buffer to a standard format (uint32_t based)
  1096. unsigned int buffer_size_uint32 = net_period_down / 2;
  1097. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1098. if( packet_payload )
  1099. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1100. }
  1101. packet_bufX = (packet_bufX + net_period_down);
  1102. node = jack_slist_next (node);
  1103. chn++;
  1104. }
  1105. }
  1106. void
  1107. 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)
  1108. {
  1109. int chn = 0;
  1110. JSList *node = playback_ports;
  1111. JSList *src_node = playback_srcs;
  1112. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1113. while (node != NULL) {
  1114. jack_port_t *port = (jack_port_t *) node->data;
  1115. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1116. const char *porttype = jack_port_type (port);
  1117. if (jack_port_is_audio (porttype)) {
  1118. // audio port, encode celt data.
  1119. int encoded_bytes;
  1120. float *floatbuf = alloca (sizeof(float) * nframes );
  1121. memcpy( floatbuf, buf, nframes * sizeof(float) );
  1122. CELTEncoder *encoder = src_node->data;
  1123. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  1124. encoded_bytes = celt_encode_float( encoder, floatbuf, nframes, packet_bufX, net_period_up );
  1125. #else
  1126. encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
  1127. #endif
  1128. if( encoded_bytes != net_period_up )
  1129. printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
  1130. src_node = jack_slist_next( src_node );
  1131. } else if (jack_port_is_midi (porttype)) {
  1132. // encode midi events from port to packet
  1133. // convert the data buffer to a standard format (uint32_t based)
  1134. unsigned int buffer_size_uint32 = net_period_up / 2;
  1135. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1136. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1137. }
  1138. packet_bufX = (packet_bufX + net_period_up);
  1139. node = jack_slist_next (node);
  1140. chn++;
  1141. }
  1142. }
  1143. #endif
  1144. #if HAVE_OPUS
  1145. #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
  1146. // render functions for Opus.
  1147. void
  1148. 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)
  1149. {
  1150. int chn = 0;
  1151. JSList *node = capture_ports;
  1152. JSList *src_node = capture_srcs;
  1153. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1154. while (node != NULL) {
  1155. jack_port_t *port = (jack_port_t *) node->data;
  1156. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1157. const char *porttype = jack_port_type (port);
  1158. if (jack_port_is_audio (porttype)) {
  1159. // audio port, decode opus data.
  1160. OpusCustomDecoder *decoder = (OpusCustomDecoder*) src_node->data;
  1161. if( !packet_payload )
  1162. memset(buf, 0, nframes * sizeof(float));
  1163. else {
  1164. unsigned short len;
  1165. memcpy(&len, packet_bufX, CDO);
  1166. len = ntohs(len);
  1167. opus_custom_decode_float( decoder, packet_bufX + CDO, len, buf, nframes );
  1168. }
  1169. src_node = jack_slist_next (src_node);
  1170. } else if (jack_port_is_midi (porttype)) {
  1171. // midi port, decode midi events
  1172. // convert the data buffer to a standard format (uint32_t based)
  1173. unsigned int buffer_size_uint32 = net_period_down / 2;
  1174. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1175. if( packet_payload )
  1176. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1177. }
  1178. packet_bufX = (packet_bufX + net_period_down);
  1179. node = jack_slist_next (node);
  1180. chn++;
  1181. }
  1182. }
  1183. void
  1184. 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)
  1185. {
  1186. int chn = 0;
  1187. JSList *node = playback_ports;
  1188. JSList *src_node = playback_srcs;
  1189. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1190. while (node != NULL) {
  1191. jack_port_t *port = (jack_port_t *) node->data;
  1192. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1193. const char *porttype = jack_port_type (port);
  1194. if (jack_port_is_audio (porttype)) {
  1195. // audio port, encode opus data.
  1196. int encoded_bytes;
  1197. float *floatbuf = alloca (sizeof(float) * nframes );
  1198. memcpy( floatbuf, buf, nframes * sizeof(float) );
  1199. OpusCustomEncoder *encoder = (OpusCustomEncoder*) src_node->data;
  1200. encoded_bytes = opus_custom_encode_float( encoder, floatbuf, nframes, packet_bufX + CDO, net_period_up - CDO );
  1201. unsigned short len = htons(encoded_bytes);
  1202. memcpy(packet_bufX, &len, CDO);
  1203. src_node = jack_slist_next( src_node );
  1204. } else if (jack_port_is_midi (porttype)) {
  1205. // encode midi events from port to packet
  1206. // convert the data buffer to a standard format (uint32_t based)
  1207. unsigned int buffer_size_uint32 = net_period_up / 2;
  1208. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1209. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1210. }
  1211. packet_bufX = (packet_bufX + net_period_up);
  1212. node = jack_slist_next (node);
  1213. chn++;
  1214. }
  1215. }
  1216. #endif
  1217. /* Wrapper functions with bitdepth argument... */
  1218. void
  1219. 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)
  1220. {
  1221. if (bitdepth == 8)
  1222. render_payload_to_jack_ports_8bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1223. else if (bitdepth == 16)
  1224. render_payload_to_jack_ports_16bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1225. #if HAVE_CELT
  1226. else if (bitdepth == CELT_MODE)
  1227. render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1228. #endif
  1229. #if HAVE_OPUS
  1230. else if (bitdepth == OPUS_MODE)
  1231. render_payload_to_jack_ports_opus (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1232. #endif
  1233. else
  1234. render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes, dont_htonl_floats);
  1235. }
  1236. void
  1237. 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)
  1238. {
  1239. if (bitdepth == 8)
  1240. render_jack_ports_to_payload_8bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1241. else if (bitdepth == 16)
  1242. render_jack_ports_to_payload_16bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1243. #if HAVE_CELT
  1244. else if (bitdepth == CELT_MODE)
  1245. render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1246. #endif
  1247. #if HAVE_OPUS
  1248. else if (bitdepth == OPUS_MODE)
  1249. render_jack_ports_to_payload_opus (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1250. #endif
  1251. else
  1252. render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);
  1253. }