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.

1522 lines
44KB

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