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.

1546 lines
45KB

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