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.

1479 lines
42KB

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