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.

1480 lines
47KB

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