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.

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