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.

1471 lines
43KB

  1. /*
  2. * NetJack - Packet Handling functions
  3. *
  4. * used by the driver and the jacknet_client
  5. *
  6. * Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
  7. * Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  8. * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
  25. *
  26. */
  27. #ifdef __linux__
  28. #include "config.h"
  29. #endif
  30. #ifdef __APPLE__
  31. #define _DARWIN_C_SOURCE
  32. #endif
  33. #if HAVE_PPOLL
  34. #define _GNU_SOURCE
  35. #endif
  36. #include <math.h>
  37. #include <stdio.h>
  38. #include <memory.h>
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. #include <errno.h>
  42. #include <stdarg.h>
  43. #include <jack/types.h>
  44. #include <sys/types.h>
  45. #ifdef WIN32
  46. #include <winsock2.h>
  47. #define socklen_t int
  48. #include <malloc.h>
  49. #define socklen_t int
  50. #else
  51. #include <sys/socket.h>
  52. #include <netinet/in.h>
  53. #include <poll.h>
  54. #endif
  55. #include <errno.h>
  56. #include <signal.h>
  57. #if HAVE_SAMPLERATE
  58. #include <samplerate.h>
  59. #endif
  60. #if HAVE_CELT
  61. #include <celt/celt.h>
  62. #endif
  63. #include "netjack_packet.h"
  64. #ifdef NO_JACK_ERROR
  65. #define jack_error printf
  66. #endif
  67. int fraggo = 0;
  68. void
  69. packet_header_hton (jacknet_packet_header *pkthdr)
  70. {
  71. pkthdr->capture_channels_audio = htonl(pkthdr->capture_channels_audio);
  72. pkthdr->playback_channels_audio = htonl(pkthdr->playback_channels_audio);
  73. pkthdr->capture_channels_midi = htonl(pkthdr->capture_channels_midi);
  74. pkthdr->playback_channels_midi = htonl(pkthdr->playback_channels_midi);
  75. pkthdr->period_size = htonl(pkthdr->period_size);
  76. pkthdr->sample_rate = htonl(pkthdr->sample_rate);
  77. pkthdr->sync_state = htonl(pkthdr->sync_state);
  78. pkthdr->transport_frame = htonl(pkthdr->transport_frame);
  79. pkthdr->transport_state = htonl(pkthdr->transport_state);
  80. pkthdr->framecnt = htonl(pkthdr->framecnt);
  81. pkthdr->latency = htonl(pkthdr->latency);
  82. pkthdr->reply_port = htonl(pkthdr->reply_port);
  83. pkthdr->mtu = htonl(pkthdr->mtu);
  84. pkthdr->fragment_nr = htonl(pkthdr->fragment_nr);
  85. }
  86. void
  87. packet_header_ntoh (jacknet_packet_header *pkthdr)
  88. {
  89. pkthdr->capture_channels_audio = ntohl(pkthdr->capture_channels_audio);
  90. pkthdr->playback_channels_audio = ntohl(pkthdr->playback_channels_audio);
  91. pkthdr->capture_channels_midi = ntohl(pkthdr->capture_channels_midi);
  92. pkthdr->playback_channels_midi = ntohl(pkthdr->playback_channels_midi);
  93. pkthdr->period_size = ntohl(pkthdr->period_size);
  94. pkthdr->sample_rate = ntohl(pkthdr->sample_rate);
  95. pkthdr->sync_state = ntohl(pkthdr->sync_state);
  96. pkthdr->transport_frame = ntohl(pkthdr->transport_frame);
  97. pkthdr->transport_state = ntohl(pkthdr->transport_state);
  98. pkthdr->framecnt = ntohl(pkthdr->framecnt);
  99. pkthdr->latency = ntohl(pkthdr->latency);
  100. pkthdr->reply_port = ntohl(pkthdr->reply_port);
  101. pkthdr->mtu = ntohl(pkthdr->mtu);
  102. pkthdr->fragment_nr = ntohl(pkthdr->fragment_nr);
  103. }
  104. int get_sample_size (int bitdepth)
  105. {
  106. if (bitdepth == 8)
  107. return sizeof (int8_t);
  108. 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 = lrintf( (float)(deadline - now) / 1000.0 );
  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 (pcache, 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. void
  646. netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
  647. {
  648. int frag_cnt = 0;
  649. char *tx_packet, *dataX;
  650. jacknet_packet_header *pkthdr;
  651. tx_packet = alloca (mtu + 10);
  652. dataX = tx_packet + sizeof (jacknet_packet_header);
  653. pkthdr = (jacknet_packet_header *) tx_packet;
  654. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  655. if (pkt_size <= mtu) {
  656. int err;
  657. pkthdr = (jacknet_packet_header *) packet_buf;
  658. pkthdr->fragment_nr = htonl (0);
  659. err = sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  660. if( err<0 ) {
  661. //printf( "error in send\n" );
  662. perror( "send" );
  663. }
  664. }
  665. else
  666. {
  667. int err;
  668. // Copy the packet header to the tx pack first.
  669. memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
  670. // Now loop and send all
  671. char *packet_bufX = packet_buf + sizeof (jacknet_packet_header);
  672. while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size))
  673. {
  674. pkthdr->fragment_nr = htonl (frag_cnt++);
  675. memcpy (dataX, packet_bufX, fragment_payload_size);
  676. sendto (sockfd, tx_packet, mtu, flags, addr, addr_size);
  677. packet_bufX += fragment_payload_size;
  678. }
  679. int last_payload_size = packet_buf + pkt_size - packet_bufX;
  680. memcpy (dataX, packet_bufX, last_payload_size);
  681. pkthdr->fragment_nr = htonl (frag_cnt);
  682. //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
  683. // sendto(last_pack_size);
  684. err = sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
  685. if( err<0 ) {
  686. //printf( "error in send\n" );
  687. perror( "send" );
  688. }
  689. }
  690. }
  691. void
  692. decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  693. {
  694. int i;
  695. jack_midi_clear_buffer (buf);
  696. for (i = 0; i < buffer_size_uint32 - 3;)
  697. {
  698. uint32_t payload_size;
  699. payload_size = buffer_uint32[i];
  700. payload_size = ntohl (payload_size);
  701. if (payload_size)
  702. {
  703. jack_midi_event_t event;
  704. event.time = ntohl (buffer_uint32[i+1]);
  705. event.size = ntohl (buffer_uint32[i+2]);
  706. event.buffer = (jack_midi_data_t*) (&(buffer_uint32[i+3]));
  707. jack_midi_event_write (buf, event.time, event.buffer, event.size);
  708. // skip to the next event
  709. unsigned int nb_data_quads = (((event.size-1) & ~0x3) >> 2)+1;
  710. i += 3+nb_data_quads;
  711. }
  712. else
  713. break; // no events can follow an empty event, we're done
  714. }
  715. }
  716. void
  717. encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  718. {
  719. int i;
  720. unsigned int written = 0;
  721. // midi port, encode midi events
  722. unsigned int nevents = jack_midi_get_event_count (buf);
  723. for (i = 0; i < nevents; ++i)
  724. {
  725. jack_midi_event_t event;
  726. jack_midi_event_get (&event, buf, i);
  727. unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
  728. unsigned int payload_size = 3 + nb_data_quads;
  729. // only write if we have sufficient space for the event
  730. // otherwise drop it
  731. if (written + payload_size < buffer_size_uint32 - 1)
  732. {
  733. // write header
  734. buffer_uint32[written]=htonl (payload_size);
  735. written++;
  736. buffer_uint32[written]=htonl (event.time);
  737. written++;
  738. buffer_uint32[written]=htonl (event.size);
  739. written++;
  740. // write data
  741. jack_midi_data_t* tmpbuff = (jack_midi_data_t*)(&(buffer_uint32[written]));
  742. memcpy (tmpbuff, event.buffer, event.size);
  743. written += nb_data_quads;
  744. }
  745. else
  746. {
  747. // buffer overflow
  748. jack_error ("midi buffer overflow");
  749. break;
  750. }
  751. }
  752. // now put a netjack_midi 'no-payload' event, signaling EOF
  753. buffer_uint32[written]=0;
  754. }
  755. // render functions for float
  756. void
  757. 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)
  758. {
  759. int chn = 0;
  760. JSList *node = capture_ports;
  761. #if HAVE_SAMPLERATE
  762. JSList *src_node = capture_srcs;
  763. #endif
  764. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  765. if (!packet_payload)
  766. return;
  767. while (node != NULL)
  768. {
  769. int i;
  770. int_float_t val;
  771. #if HAVE_SAMPLERATE
  772. SRC_DATA src;
  773. #endif
  774. jack_port_t *port = (jack_port_t *) node->data;
  775. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  776. const char *porttype = jack_port_type (port);
  777. if (jack_port_is_audio (porttype))
  778. {
  779. #if HAVE_SAMPLERATE
  780. // audio port, resample if necessary
  781. if (net_period_down != nframes)
  782. {
  783. SRC_STATE *src_state = src_node->data;
  784. for (i = 0; i < net_period_down; i++)
  785. {
  786. packet_bufX[i] = ntohl (packet_bufX[i]);
  787. }
  788. src.data_in = (float *) packet_bufX;
  789. src.input_frames = net_period_down;
  790. src.data_out = buf;
  791. src.output_frames = nframes;
  792. src.src_ratio = (float) nframes / (float) net_period_down;
  793. src.end_of_input = 0;
  794. src_set_ratio (src_state, src.src_ratio);
  795. src_process (src_state, &src);
  796. src_node = jack_slist_next (src_node);
  797. }
  798. else
  799. #endif
  800. {
  801. if( dont_htonl_floats )
  802. {
  803. memcpy( buf, packet_bufX, net_period_down*sizeof(jack_default_audio_sample_t));
  804. }
  805. else
  806. {
  807. for (i = 0; i < net_period_down; i++)
  808. {
  809. val.i = packet_bufX[i];
  810. val.i = ntohl (val.i);
  811. buf[i] = val.f;
  812. }
  813. }
  814. }
  815. }
  816. else if (jack_port_is_midi (porttype))
  817. {
  818. // midi port, decode midi events
  819. // convert the data buffer to a standard format (uint32_t based)
  820. unsigned int buffer_size_uint32 = net_period_down;
  821. uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
  822. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  823. }
  824. packet_bufX = (packet_bufX + net_period_down);
  825. node = jack_slist_next (node);
  826. chn++;
  827. }
  828. }
  829. void
  830. 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 )
  831. {
  832. int chn = 0;
  833. JSList *node = playback_ports;
  834. #if HAVE_SAMPLERATE
  835. JSList *src_node = playback_srcs;
  836. #endif
  837. uint32_t *packet_bufX = (uint32_t *) packet_payload;
  838. while (node != NULL)
  839. {
  840. #if HAVE_SAMPLERATE
  841. SRC_DATA src;
  842. #endif
  843. int i;
  844. int_float_t val;
  845. jack_port_t *port = (jack_port_t *) node->data;
  846. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  847. const char *porttype = jack_port_type (port);
  848. if (jack_port_is_audio (porttype))
  849. {
  850. // audio port, resample if necessary
  851. #if HAVE_SAMPLERATE
  852. if (net_period_up != nframes) {
  853. SRC_STATE *src_state = src_node->data;
  854. src.data_in = buf;
  855. src.input_frames = nframes;
  856. src.data_out = (float *) packet_bufX;
  857. src.output_frames = net_period_up;
  858. src.src_ratio = (float) net_period_up / (float) nframes;
  859. src.end_of_input = 0;
  860. src_set_ratio (src_state, src.src_ratio);
  861. src_process (src_state, &src);
  862. for (i = 0; i < net_period_up; i++)
  863. {
  864. packet_bufX[i] = htonl (packet_bufX[i]);
  865. }
  866. src_node = jack_slist_next (src_node);
  867. }
  868. else
  869. #endif
  870. {
  871. if( dont_htonl_floats )
  872. {
  873. memcpy( packet_bufX, buf, net_period_up*sizeof(jack_default_audio_sample_t) );
  874. }
  875. else
  876. {
  877. for (i = 0; i < net_period_up; i++)
  878. {
  879. val.f = buf[i];
  880. val.i = htonl (val.i);
  881. packet_bufX[i] = val.i;
  882. }
  883. }
  884. }
  885. }
  886. else if (jack_port_is_midi (porttype))
  887. {
  888. // encode midi events from port to packet
  889. // convert the data buffer to a standard format (uint32_t based)
  890. unsigned int buffer_size_uint32 = net_period_up;
  891. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  892. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  893. }
  894. packet_bufX = (packet_bufX + net_period_up);
  895. node = jack_slist_next (node);
  896. chn++;
  897. }
  898. }
  899. // render functions for 16bit
  900. void
  901. 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)
  902. {
  903. int chn = 0;
  904. JSList *node = capture_ports;
  905. #if HAVE_SAMPLERATE
  906. JSList *src_node = capture_srcs;
  907. #endif
  908. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  909. if( !packet_payload )
  910. return;
  911. while (node != NULL)
  912. {
  913. int i;
  914. //uint32_t val;
  915. #if HAVE_SAMPLERATE
  916. SRC_DATA src;
  917. #endif
  918. jack_port_t *port = (jack_port_t *) node->data;
  919. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  920. #if HAVE_SAMPLERATE
  921. float *floatbuf = alloca (sizeof(float) * net_period_down);
  922. #endif
  923. const char *porttype = jack_port_type (port);
  924. if (jack_port_is_audio (porttype))
  925. {
  926. // audio port, resample if necessary
  927. #if HAVE_SAMPLERATE
  928. if (net_period_down != nframes)
  929. {
  930. SRC_STATE *src_state = src_node->data;
  931. for (i = 0; i < net_period_down; i++)
  932. {
  933. floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
  934. }
  935. src.data_in = floatbuf;
  936. src.input_frames = net_period_down;
  937. src.data_out = buf;
  938. src.output_frames = nframes;
  939. src.src_ratio = (float) nframes / (float) net_period_down;
  940. src.end_of_input = 0;
  941. src_set_ratio (src_state, src.src_ratio);
  942. src_process (src_state, &src);
  943. src_node = jack_slist_next (src_node);
  944. }
  945. else
  946. #endif
  947. for (i = 0; i < net_period_down; i++)
  948. buf[i] = ((float) ntohs (packet_bufX[i])) / 32768.0 - 1.0;
  949. }
  950. else if (jack_port_is_midi (porttype))
  951. {
  952. // midi port, decode midi events
  953. // convert the data buffer to a standard format (uint32_t based)
  954. unsigned int buffer_size_uint32 = net_period_down / 2;
  955. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  956. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  957. }
  958. packet_bufX = (packet_bufX + net_period_down);
  959. node = jack_slist_next (node);
  960. chn++;
  961. }
  962. }
  963. void
  964. 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)
  965. {
  966. int chn = 0;
  967. JSList *node = playback_ports;
  968. #if HAVE_SAMPLERATE
  969. JSList *src_node = playback_srcs;
  970. #endif
  971. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  972. while (node != NULL)
  973. {
  974. #if HAVE_SAMPLERATE
  975. SRC_DATA src;
  976. #endif
  977. int i;
  978. jack_port_t *port = (jack_port_t *) node->data;
  979. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  980. const char *porttype = jack_port_type (port);
  981. if (jack_port_is_audio (porttype))
  982. {
  983. // audio port, resample if necessary
  984. #if HAVE_SAMPLERATE
  985. if (net_period_up != nframes)
  986. {
  987. SRC_STATE *src_state = src_node->data;
  988. float *floatbuf = alloca (sizeof(float) * net_period_up);
  989. src.data_in = buf;
  990. src.input_frames = nframes;
  991. src.data_out = floatbuf;
  992. src.output_frames = net_period_up;
  993. src.src_ratio = (float) net_period_up / (float) nframes;
  994. src.end_of_input = 0;
  995. src_set_ratio (src_state, src.src_ratio);
  996. src_process (src_state, &src);
  997. for (i = 0; i < net_period_up; i++)
  998. {
  999. packet_bufX[i] = htons (((uint16_t)((floatbuf[i] + 1.0) * 32767.0)));
  1000. }
  1001. src_node = jack_slist_next (src_node);
  1002. }
  1003. else
  1004. #endif
  1005. for (i = 0; i < net_period_up; i++)
  1006. packet_bufX[i] = htons(((uint16_t)((buf[i] + 1.0) * 32767.0)));
  1007. }
  1008. else if (jack_port_is_midi (porttype))
  1009. {
  1010. // encode midi events from port to packet
  1011. // convert the data buffer to a standard format (uint32_t based)
  1012. unsigned int buffer_size_uint32 = net_period_up / 2;
  1013. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1014. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1015. }
  1016. packet_bufX = (packet_bufX + net_period_up);
  1017. node = jack_slist_next (node);
  1018. chn++;
  1019. }
  1020. }
  1021. // render functions for 8bit
  1022. void
  1023. 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)
  1024. {
  1025. int chn = 0;
  1026. JSList *node = capture_ports;
  1027. #if HAVE_SAMPLERATE
  1028. JSList *src_node = capture_srcs;
  1029. #endif
  1030. int8_t *packet_bufX = (int8_t *)packet_payload;
  1031. if (!packet_payload)
  1032. return;
  1033. while (node != NULL)
  1034. {
  1035. int i;
  1036. //uint32_t val;
  1037. #if HAVE_SAMPLERATE
  1038. SRC_DATA src;
  1039. #endif
  1040. jack_port_t *port = (jack_port_t *) node->data;
  1041. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1042. #if HAVE_SAMPLERATE
  1043. float *floatbuf = alloca (sizeof (float) * net_period_down);
  1044. #endif
  1045. const char *porttype = jack_port_type (port);
  1046. if (jack_port_is_audio(porttype))
  1047. {
  1048. #if HAVE_SAMPLERATE
  1049. // audio port, resample if necessary
  1050. if (net_period_down != nframes)
  1051. {
  1052. SRC_STATE *src_state = src_node->data;
  1053. for (i = 0; i < net_period_down; i++)
  1054. floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
  1055. src.data_in = floatbuf;
  1056. src.input_frames = net_period_down;
  1057. src.data_out = buf;
  1058. src.output_frames = nframes;
  1059. src.src_ratio = (float) nframes / (float) net_period_down;
  1060. src.end_of_input = 0;
  1061. src_set_ratio (src_state, src.src_ratio);
  1062. src_process (src_state, &src);
  1063. src_node = jack_slist_next (src_node);
  1064. }
  1065. else
  1066. #endif
  1067. for (i = 0; i < net_period_down; i++)
  1068. buf[i] = ((float) packet_bufX[i]) / 127.0;
  1069. }
  1070. else if (jack_port_is_midi (porttype))
  1071. {
  1072. // midi port, decode midi events
  1073. // convert the data buffer to a standard format (uint32_t based)
  1074. unsigned int buffer_size_uint32 = net_period_down / 2;
  1075. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1076. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1077. }
  1078. packet_bufX = (packet_bufX + net_period_down);
  1079. node = jack_slist_next (node);
  1080. chn++;
  1081. }
  1082. }
  1083. void
  1084. 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)
  1085. {
  1086. int chn = 0;
  1087. JSList *node = playback_ports;
  1088. #if HAVE_SAMPLERATE
  1089. JSList *src_node = playback_srcs;
  1090. #endif
  1091. int8_t *packet_bufX = (int8_t *)packet_payload;
  1092. while (node != NULL)
  1093. {
  1094. #if HAVE_SAMPLERATE
  1095. SRC_DATA src;
  1096. #endif
  1097. int i;
  1098. jack_port_t *port = (jack_port_t *) node->data;
  1099. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1100. const char *porttype = jack_port_type (port);
  1101. if (jack_port_is_audio (porttype))
  1102. {
  1103. #if HAVE_SAMPLERATE
  1104. // audio port, resample if necessary
  1105. if (net_period_up != nframes)
  1106. {
  1107. SRC_STATE *src_state = src_node->data;
  1108. float *floatbuf = alloca (sizeof (float) * net_period_up);
  1109. src.data_in = buf;
  1110. src.input_frames = nframes;
  1111. src.data_out = floatbuf;
  1112. src.output_frames = net_period_up;
  1113. src.src_ratio = (float) net_period_up / (float) nframes;
  1114. src.end_of_input = 0;
  1115. src_set_ratio (src_state, src.src_ratio);
  1116. src_process (src_state, &src);
  1117. for (i = 0; i < net_period_up; i++)
  1118. packet_bufX[i] = floatbuf[i] * 127.0;
  1119. src_node = jack_slist_next (src_node);
  1120. }
  1121. else
  1122. #endif
  1123. for (i = 0; i < net_period_up; i++)
  1124. packet_bufX[i] = buf[i] * 127.0;
  1125. }
  1126. else if (jack_port_is_midi (porttype))
  1127. {
  1128. // encode midi events from port to packet
  1129. // convert the data buffer to a standard format (uint32_t based)
  1130. unsigned int buffer_size_uint32 = net_period_up / 4;
  1131. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1132. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1133. }
  1134. packet_bufX = (packet_bufX + net_period_up);
  1135. node = jack_slist_next (node);
  1136. chn++;
  1137. }
  1138. }
  1139. #if HAVE_CELT
  1140. // render functions for celt.
  1141. void
  1142. 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)
  1143. {
  1144. int chn = 0;
  1145. JSList *node = capture_ports;
  1146. JSList *src_node = capture_srcs;
  1147. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1148. while (node != NULL)
  1149. {
  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. // audio port, decode celt data.
  1156. CELTDecoder *decoder = src_node->data;
  1157. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  1158. if( !packet_payload )
  1159. celt_decode_float( decoder, NULL, net_period_down, buf, nframes );
  1160. else
  1161. celt_decode_float( decoder, packet_bufX, net_period_down, buf, nframes );
  1162. #else
  1163. if( !packet_payload )
  1164. celt_decode_float( decoder, NULL, net_period_down, buf );
  1165. else
  1166. celt_decode_float( decoder, packet_bufX, net_period_down, buf );
  1167. #endif
  1168. src_node = jack_slist_next (src_node);
  1169. }
  1170. else if (jack_port_is_midi (porttype))
  1171. {
  1172. // midi port, decode midi events
  1173. // convert the data buffer to a standard format (uint32_t based)
  1174. unsigned int buffer_size_uint32 = net_period_down / 2;
  1175. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1176. if( packet_payload )
  1177. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1178. }
  1179. packet_bufX = (packet_bufX + net_period_down);
  1180. node = jack_slist_next (node);
  1181. chn++;
  1182. }
  1183. }
  1184. void
  1185. 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)
  1186. {
  1187. int chn = 0;
  1188. JSList *node = playback_ports;
  1189. JSList *src_node = playback_srcs;
  1190. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1191. while (node != NULL)
  1192. {
  1193. jack_port_t *port = (jack_port_t *) node->data;
  1194. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1195. const char *porttype = jack_port_type (port);
  1196. if (jack_port_is_audio (porttype))
  1197. {
  1198. // audio port, encode celt data.
  1199. int encoded_bytes;
  1200. float *floatbuf = alloca (sizeof(float) * nframes );
  1201. memcpy( floatbuf, buf, nframes*sizeof(float) );
  1202. CELTEncoder *encoder = src_node->data;
  1203. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  1204. encoded_bytes = celt_encode_float( encoder, floatbuf, nframes, packet_bufX, net_period_up );
  1205. #else
  1206. encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
  1207. #endif
  1208. if( encoded_bytes != net_period_up )
  1209. printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
  1210. src_node = jack_slist_next( src_node );
  1211. }
  1212. else if (jack_port_is_midi (porttype))
  1213. {
  1214. // encode midi events from port to packet
  1215. // convert the data buffer to a standard format (uint32_t based)
  1216. unsigned int buffer_size_uint32 = net_period_up / 2;
  1217. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1218. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1219. }
  1220. packet_bufX = (packet_bufX + net_period_up);
  1221. node = jack_slist_next (node);
  1222. chn++;
  1223. }
  1224. }
  1225. #endif
  1226. /* Wrapper functions with bitdepth argument... */
  1227. void
  1228. 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)
  1229. {
  1230. if (bitdepth == 8)
  1231. render_payload_to_jack_ports_8bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1232. else if (bitdepth == 16)
  1233. render_payload_to_jack_ports_16bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1234. #if HAVE_CELT
  1235. else if (bitdepth == CELT_MODE)
  1236. render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1237. #endif
  1238. else
  1239. render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes, dont_htonl_floats);
  1240. }
  1241. void
  1242. 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)
  1243. {
  1244. if (bitdepth == 8)
  1245. render_jack_ports_to_payload_8bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1246. else if (bitdepth == 16)
  1247. render_jack_ports_to_payload_16bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1248. #if HAVE_CELT
  1249. else if (bitdepth == CELT_MODE)
  1250. render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1251. #endif
  1252. else
  1253. render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);
  1254. }