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 _XOPEN_SOURCE 600
  29. #define _BSD_SOURCE
  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 <alloca.h>
  42. #include <errno.h>
  43. #include <stdarg.h>
  44. #include <jack/types.h>
  45. #include <sys/types.h>
  46. #ifdef WIN32
  47. #include <winsock2.h>
  48. #include <malloc.h>
  49. #else
  50. #include <sys/socket.h>
  51. #include <netinet/in.h>
  52. #include <poll.h>
  53. #endif
  54. #include <errno.h>
  55. #include <signal.h>
  56. #if HAVE_SAMPLERATE
  57. #include <samplerate.h>
  58. #endif
  59. #if HAVE_CELT
  60. #include <celt/celt.h>
  61. #endif
  62. #include "netjack_packet.h"
  63. // JACK2 specific.
  64. #include "jack/control.h"
  65. #ifdef NO_JACK_ERROR
  66. #define jack_error printf
  67. #endif
  68. int fraggo = 0;
  69. packet_cache *global_packcache = NULL;
  70. void
  71. packet_header_hton (jacknet_packet_header *pkthdr)
  72. {
  73. pkthdr->capture_channels_audio = htonl(pkthdr->capture_channels_audio);
  74. pkthdr->playback_channels_audio = htonl(pkthdr->playback_channels_audio);
  75. pkthdr->capture_channels_midi = htonl(pkthdr->capture_channels_midi);
  76. pkthdr->playback_channels_midi = htonl(pkthdr->playback_channels_midi);
  77. pkthdr->period_size = htonl(pkthdr->period_size);
  78. pkthdr->sample_rate = htonl(pkthdr->sample_rate);
  79. pkthdr->sync_state = htonl(pkthdr->sync_state);
  80. pkthdr->transport_frame = htonl(pkthdr->transport_frame);
  81. pkthdr->transport_state = htonl(pkthdr->transport_state);
  82. pkthdr->framecnt = htonl(pkthdr->framecnt);
  83. pkthdr->latency = htonl(pkthdr->latency);
  84. pkthdr->reply_port = htonl(pkthdr->reply_port);
  85. pkthdr->mtu = htonl(pkthdr->mtu);
  86. pkthdr->fragment_nr = htonl(pkthdr->fragment_nr);
  87. }
  88. void
  89. packet_header_ntoh (jacknet_packet_header *pkthdr)
  90. {
  91. pkthdr->capture_channels_audio = ntohl(pkthdr->capture_channels_audio);
  92. pkthdr->playback_channels_audio = ntohl(pkthdr->playback_channels_audio);
  93. pkthdr->capture_channels_midi = ntohl(pkthdr->capture_channels_midi);
  94. pkthdr->playback_channels_midi = ntohl(pkthdr->playback_channels_midi);
  95. pkthdr->period_size = ntohl(pkthdr->period_size);
  96. pkthdr->sample_rate = ntohl(pkthdr->sample_rate);
  97. pkthdr->sync_state = ntohl(pkthdr->sync_state);
  98. pkthdr->transport_frame = ntohl(pkthdr->transport_frame);
  99. pkthdr->transport_state = ntohl(pkthdr->transport_state);
  100. pkthdr->framecnt = ntohl(pkthdr->framecnt);
  101. pkthdr->latency = ntohl(pkthdr->latency);
  102. pkthdr->reply_port = ntohl(pkthdr->reply_port);
  103. pkthdr->mtu = ntohl(pkthdr->mtu);
  104. pkthdr->fragment_nr = ntohl(pkthdr->fragment_nr);
  105. }
  106. int get_sample_size (int bitdepth)
  107. {
  108. if (bitdepth == 8)
  109. return sizeof (int8_t);
  110. if (bitdepth == 16)
  111. return sizeof (int16_t);
  112. //JN: why? is this for buffer sizes before or after encoding?
  113. //JN: if the former, why not int16_t, if the latter, shouldn't it depend on -c N?
  114. if( bitdepth == CELT_MODE )
  115. return sizeof( unsigned char );
  116. return sizeof (int32_t);
  117. }
  118. int jack_port_is_audio(const char *porttype)
  119. {
  120. return (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0);
  121. }
  122. int jack_port_is_midi(const char *porttype)
  123. {
  124. return (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0);
  125. }
  126. // fragment management functions.
  127. packet_cache
  128. *packet_cache_new (int num_packets, int pkt_size, int mtu)
  129. {
  130. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  131. int i, fragment_number;
  132. if( pkt_size == sizeof(jacknet_packet_header) )
  133. fragment_number = 1;
  134. else
  135. fragment_number = (pkt_size - sizeof (jacknet_packet_header) - 1) / fragment_payload_size + 1;
  136. packet_cache *pcache = malloc (sizeof (packet_cache));
  137. if (pcache == NULL)
  138. {
  139. jack_error ("could not allocate packet cache (1)\n");
  140. return NULL;
  141. }
  142. pcache->size = num_packets;
  143. pcache->packets = malloc (sizeof (cache_packet) * num_packets);
  144. pcache->master_address_valid = 0;
  145. pcache->last_framecnt_retreived = 0;
  146. pcache->last_framecnt_retreived_valid = 0;
  147. if (pcache->packets == NULL)
  148. {
  149. jack_error ("could not allocate packet cache (2)\n");
  150. return NULL;
  151. }
  152. for (i = 0; i < num_packets; i++)
  153. {
  154. pcache->packets[i].valid = 0;
  155. pcache->packets[i].num_fragments = fragment_number;
  156. pcache->packets[i].packet_size = pkt_size;
  157. pcache->packets[i].mtu = mtu;
  158. pcache->packets[i].framecnt = 0;
  159. pcache->packets[i].fragment_array = malloc (sizeof (char) * fragment_number);
  160. pcache->packets[i].packet_buf = malloc (pkt_size);
  161. if ((pcache->packets[i].fragment_array == NULL) || (pcache->packets[i].packet_buf == NULL))
  162. {
  163. jack_error ("could not allocate packet cache (3)\n");
  164. return NULL;
  165. }
  166. }
  167. pcache->mtu = mtu;
  168. return pcache;
  169. }
  170. void
  171. packet_cache_free (packet_cache *pcache)
  172. {
  173. int i;
  174. if( pcache == NULL )
  175. return;
  176. for (i = 0; i < pcache->size; i++)
  177. {
  178. free (pcache->packets[i].fragment_array);
  179. free (pcache->packets[i].packet_buf);
  180. }
  181. free (pcache->packets);
  182. free (pcache);
  183. }
  184. cache_packet
  185. *packet_cache_get_packet (packet_cache *pcache, jack_nframes_t framecnt)
  186. {
  187. int i;
  188. cache_packet *retval;
  189. for (i = 0; i < pcache->size; i++)
  190. {
  191. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt))
  192. return &(pcache->packets[i]);
  193. }
  194. // The Packet is not in the packet cache.
  195. // find a free packet.
  196. retval = packet_cache_get_free_packet (pcache);
  197. if (retval != NULL)
  198. {
  199. cache_packet_set_framecnt (retval, framecnt);
  200. return retval;
  201. }
  202. // No Free Packet available
  203. // Get The Oldest packet and reset it.
  204. retval = packet_cache_get_oldest_packet (pcache);
  205. //printf( "Dropping %d from Cache :S\n", retval->framecnt );
  206. cache_packet_reset (retval);
  207. cache_packet_set_framecnt (retval, framecnt);
  208. return retval;
  209. }
  210. // TODO: fix wrapping case... need to pass
  211. // current expected frame here.
  212. //
  213. // or just save framecount into packet_cache.
  214. cache_packet
  215. *packet_cache_get_oldest_packet (packet_cache *pcache)
  216. {
  217. jack_nframes_t minimal_frame = JACK_MAX_FRAMES;
  218. cache_packet *retval = &(pcache->packets[0]);
  219. int i;
  220. for (i = 0; i < pcache->size; i++)
  221. {
  222. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < minimal_frame))
  223. {
  224. minimal_frame = pcache->packets[i].framecnt;
  225. retval = &(pcache->packets[i]);
  226. }
  227. }
  228. return retval;
  229. }
  230. cache_packet
  231. *packet_cache_get_free_packet (packet_cache *pcache)
  232. {
  233. int i;
  234. for (i = 0; i < pcache->size; i++)
  235. {
  236. if (pcache->packets[i].valid == 0)
  237. return &(pcache->packets[i]);
  238. }
  239. return NULL;
  240. }
  241. void
  242. cache_packet_reset (cache_packet *pack)
  243. {
  244. int i;
  245. pack->valid = 0;
  246. // XXX: i dont think this is necessary here...
  247. // fragement array is cleared in _set_framecnt()
  248. for (i = 0; i < pack->num_fragments; i++)
  249. pack->fragment_array[i] = 0;
  250. }
  251. void
  252. cache_packet_set_framecnt (cache_packet *pack, jack_nframes_t framecnt)
  253. {
  254. int i;
  255. pack->framecnt = framecnt;
  256. for (i = 0; i < pack->num_fragments; i++)
  257. pack->fragment_array[i] = 0;
  258. pack->valid = 1;
  259. }
  260. void
  261. cache_packet_add_fragment (cache_packet *pack, char *packet_buf, int rcv_len)
  262. {
  263. jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
  264. int fragment_payload_size = pack->mtu - sizeof (jacknet_packet_header);
  265. char *packet_bufX = pack->packet_buf + sizeof (jacknet_packet_header);
  266. char *dataX = packet_buf + sizeof (jacknet_packet_header);
  267. jack_nframes_t fragment_nr = ntohl (pkthdr->fragment_nr);
  268. jack_nframes_t framecnt = ntohl (pkthdr->framecnt);
  269. if (framecnt != pack->framecnt)
  270. {
  271. jack_error ("errror. framecnts dont match\n");
  272. return;
  273. }
  274. if (fragment_nr == 0)
  275. {
  276. memcpy (pack->packet_buf, packet_buf, rcv_len);
  277. pack->fragment_array[0] = 1;
  278. return;
  279. }
  280. if ((fragment_nr < pack->num_fragments) && (fragment_nr > 0))
  281. {
  282. if ((fragment_nr * fragment_payload_size + rcv_len - sizeof (jacknet_packet_header)) <= (pack->packet_size - sizeof (jacknet_packet_header)))
  283. {
  284. memcpy (packet_bufX + fragment_nr * fragment_payload_size, dataX, rcv_len - sizeof (jacknet_packet_header));
  285. pack->fragment_array[fragment_nr] = 1;
  286. }
  287. else
  288. jack_error ("too long packet received...");
  289. }
  290. }
  291. int
  292. cache_packet_is_complete (cache_packet *pack)
  293. {
  294. int i;
  295. for (i = 0; i < pack->num_fragments; i++)
  296. if (pack->fragment_array[i] == 0)
  297. return 0;
  298. return 1;
  299. }
  300. #ifndef WIN32
  301. // new poll using nanoseconds resolution and
  302. // not waiting forever.
  303. int
  304. netjack_poll_deadline (int sockfd, jack_time_t deadline)
  305. {
  306. struct pollfd fds;
  307. int i, poll_err = 0;
  308. sigset_t sigmask;
  309. struct sigaction action;
  310. #if HAVE_PPOLL
  311. struct timespec timeout_spec = { 0, 0 };
  312. #else
  313. sigset_t rsigmask;
  314. int timeout;
  315. #endif
  316. jack_time_t now = jack_get_time();
  317. if( now >= deadline )
  318. return 0;
  319. if( (deadline-now) >= 1000000 ) {
  320. jack_error( "deadline more than 1 second in the future, trimming it." );
  321. deadline = now+500000;
  322. }
  323. #if HAVE_PPOLL
  324. timeout_spec.tv_nsec = (deadline - now) * 1000;
  325. #else
  326. timeout = lrintf( (float)(deadline - now) / 1000.0 );
  327. #endif
  328. sigemptyset(&sigmask);
  329. sigaddset(&sigmask, SIGHUP);
  330. sigaddset(&sigmask, SIGINT);
  331. sigaddset(&sigmask, SIGQUIT);
  332. sigaddset(&sigmask, SIGPIPE);
  333. sigaddset(&sigmask, SIGTERM);
  334. sigaddset(&sigmask, SIGUSR1);
  335. sigaddset(&sigmask, SIGUSR2);
  336. action.sa_handler = SIG_DFL;
  337. action.sa_mask = sigmask;
  338. action.sa_flags = SA_RESTART;
  339. for (i = 1; i < NSIG; i++)
  340. if (sigismember (&sigmask, i))
  341. sigaction (i, &action, 0);
  342. fds.fd = sockfd;
  343. fds.events = POLLIN;
  344. #if HAVE_PPOLL
  345. poll_err = ppoll (&fds, 1, &timeout_spec, &sigmask);
  346. #else
  347. sigprocmask (SIG_UNBLOCK, &sigmask, &rsigmask);
  348. poll_err = poll (&fds, 1, timeout);
  349. sigprocmask (SIG_SETMASK, &rsigmask, NULL);
  350. #endif
  351. if (poll_err == -1)
  352. {
  353. switch (errno)
  354. {
  355. case EBADF:
  356. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  357. break;
  358. case EFAULT:
  359. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  360. break;
  361. case EINTR:
  362. jack_error ("Error %d: A signal occurred before any requested event", errno);
  363. break;
  364. case EINVAL:
  365. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  366. break;
  367. case ENOMEM:
  368. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  369. break;
  370. }
  371. }
  372. return poll_err;
  373. }
  374. int
  375. netjack_poll (int sockfd, int timeout)
  376. {
  377. struct pollfd fds;
  378. int i, poll_err = 0;
  379. sigset_t sigmask, rsigmask;
  380. struct sigaction action;
  381. sigemptyset(&sigmask);
  382. sigaddset(&sigmask, SIGHUP);
  383. sigaddset(&sigmask, SIGINT);
  384. sigaddset(&sigmask, SIGQUIT);
  385. sigaddset(&sigmask, SIGPIPE);
  386. sigaddset(&sigmask, SIGTERM);
  387. sigaddset(&sigmask, SIGUSR1);
  388. sigaddset(&sigmask, SIGUSR2);
  389. action.sa_handler = SIG_DFL;
  390. action.sa_mask = sigmask;
  391. action.sa_flags = SA_RESTART;
  392. for (i = 1; i < NSIG; i++)
  393. if (sigismember (&sigmask, i))
  394. sigaction (i, &action, 0);
  395. fds.fd = sockfd;
  396. fds.events = POLLIN;
  397. sigprocmask(SIG_UNBLOCK, &sigmask, &rsigmask);
  398. while (poll_err == 0)
  399. {
  400. poll_err = poll (&fds, 1, timeout);
  401. }
  402. sigprocmask(SIG_SETMASK, &rsigmask, NULL);
  403. if (poll_err == -1)
  404. {
  405. switch (errno)
  406. {
  407. case EBADF:
  408. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  409. break;
  410. case EFAULT:
  411. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  412. break;
  413. case EINTR:
  414. jack_error ("Error %d: A signal occurred before any requested event", errno);
  415. break;
  416. case EINVAL:
  417. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  418. break;
  419. case ENOMEM:
  420. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  421. break;
  422. }
  423. return 0;
  424. }
  425. return 1;
  426. }
  427. #else
  428. int
  429. netjack_poll (int sockfd, int timeout)
  430. {
  431. jack_error( "netjack_poll not implemented\n" );
  432. return 0;
  433. }
  434. int
  435. netjack_poll_deadline (int sockfd, jack_time_t deadline)
  436. {
  437. fd_set fds;
  438. FD_ZERO( &fds );
  439. FD_SET( sockfd, &fds );
  440. struct timeval timeout;
  441. while( 1 ) {
  442. jack_time_t now = jack_get_time();
  443. if( now >= deadline )
  444. return 0;
  445. int timeout_usecs = (deadline - now);
  446. //jack_error( "timeout = %d", timeout_usecs );
  447. timeout.tv_sec = 0;
  448. timeout.tv_usec = (timeout_usecs < 500) ? 500 : timeout_usecs;
  449. timeout.tv_usec = (timeout_usecs > 1000000) ? 500000 : timeout_usecs;
  450. int poll_err = select (0, &fds, NULL, NULL, &timeout);
  451. if( poll_err != 0 )
  452. return poll_err;
  453. }
  454. return 0;
  455. }
  456. #endif
  457. // This now reads all a socket has into the cache.
  458. // replacing netjack_recv functions.
  459. void
  460. packet_cache_drain_socket( packet_cache *pcache, int sockfd )
  461. {
  462. char *rx_packet = alloca (pcache->mtu);
  463. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  464. int rcv_len;
  465. jack_nframes_t framecnt;
  466. cache_packet *cpack;
  467. struct sockaddr_in sender_address;
  468. #ifdef WIN32
  469. size_t senderlen = sizeof( struct sockaddr_in );
  470. u_long parm = 1;
  471. ioctlsocket( sockfd, FIONBIO, &parm );
  472. #else
  473. socklen_t senderlen = sizeof( struct sockaddr_in );
  474. #endif
  475. while (1)
  476. {
  477. #ifdef WIN32
  478. rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, 0,
  479. (struct sockaddr*) &sender_address, &senderlen);
  480. #else
  481. rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT,
  482. (struct sockaddr*) &sender_address, &senderlen);
  483. #endif
  484. if (rcv_len < 0)
  485. return;
  486. if (pcache->master_address_valid) {
  487. // Verify its from our master.
  488. if (memcmp (&sender_address, &(pcache->master_address), senderlen) != 0)
  489. continue;
  490. } else {
  491. // Setup this one as master
  492. //printf( "setup master...\n" );
  493. memcpy ( &(pcache->master_address), &sender_address, senderlen );
  494. pcache->master_address_valid = 1;
  495. }
  496. framecnt = ntohl (pkthdr->framecnt);
  497. if( pcache->last_framecnt_retreived_valid && (framecnt <= pcache->last_framecnt_retreived ))
  498. continue;
  499. cpack = packet_cache_get_packet (global_packcache, framecnt);
  500. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  501. cpack->recv_timestamp = jack_get_time();
  502. }
  503. }
  504. void
  505. packet_cache_reset_master_address( packet_cache *pcache )
  506. {
  507. pcache->master_address_valid = 0;
  508. pcache->last_framecnt_retreived = 0;
  509. pcache->last_framecnt_retreived_valid = 0;
  510. }
  511. void
  512. packet_cache_clear_old_packets (packet_cache *pcache, jack_nframes_t framecnt )
  513. {
  514. int i;
  515. for (i = 0; i < pcache->size; i++)
  516. {
  517. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < framecnt))
  518. {
  519. cache_packet_reset (&(pcache->packets[i]));
  520. }
  521. }
  522. }
  523. int
  524. packet_cache_retreive_packet_pointer( packet_cache *pcache, jack_nframes_t framecnt, char **packet_buf, int pkt_size, jack_time_t *timestamp )
  525. {
  526. int i;
  527. cache_packet *cpack = NULL;
  528. for (i = 0; i < pcache->size; i++) {
  529. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  530. cpack = &(pcache->packets[i]);
  531. break;
  532. }
  533. }
  534. if( cpack == NULL ) {
  535. //printf( "retreive packet: %d....not found\n", framecnt );
  536. return -1;
  537. }
  538. if( !cache_packet_is_complete( cpack ) ) {
  539. return -1;
  540. }
  541. // ok. cpack is the one we want and its complete.
  542. *packet_buf = cpack->packet_buf;
  543. if( timestamp )
  544. *timestamp = cpack->recv_timestamp;
  545. pcache->last_framecnt_retreived_valid = 1;
  546. pcache->last_framecnt_retreived = framecnt;
  547. return pkt_size;
  548. }
  549. int
  550. packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt )
  551. {
  552. int i;
  553. cache_packet *cpack = NULL;
  554. for (i = 0; i < pcache->size; i++) {
  555. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  556. cpack = &(pcache->packets[i]);
  557. break;
  558. }
  559. }
  560. if( cpack == NULL ) {
  561. //printf( "retreive packet: %d....not found\n", framecnt );
  562. return -1;
  563. }
  564. if( !cache_packet_is_complete( cpack ) ) {
  565. return -1;
  566. }
  567. cache_packet_reset (cpack);
  568. packet_cache_clear_old_packets( pcache, framecnt );
  569. return 0;
  570. }
  571. float
  572. packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
  573. {
  574. int num_packets_before_us = 0;
  575. int i;
  576. for (i = 0; i < pcache->size; i++)
  577. {
  578. cache_packet *cpack = &(pcache->packets[i]);
  579. if (cpack->valid && cache_packet_is_complete( cpack ))
  580. if( cpack->framecnt >= expected_framecnt )
  581. num_packets_before_us += 1;
  582. }
  583. return 100.0 * (float)num_packets_before_us / (float)( pcache->size ) ;
  584. }
  585. // Returns 0 when no valid packet is inside the cache.
  586. int
  587. packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  588. {
  589. int i;
  590. jack_nframes_t best_offset = JACK_MAX_FRAMES/2-1;
  591. int retval = 0;
  592. for (i = 0; i < pcache->size; i++)
  593. {
  594. cache_packet *cpack = &(pcache->packets[i]);
  595. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  596. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  597. //printf( "invalid\n" );
  598. continue;
  599. }
  600. if( cpack->framecnt < expected_framecnt )
  601. continue;
  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. }