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.

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