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

1431 lines
38KB

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