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.

1472 lines
43KB

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