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.

1541 lines
44KB

  1. /*
  2. * NetJack - Packet Handling functions
  3. *
  4. * used by the driver and the jacknet_client
  5. *
  6. * Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
  7. * Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  8. * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
  25. *
  26. */
  27. //#include "config.h"
  28. #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 HAVE_PPOLL
  322. timeout_spec.tv_nsec = (deadline - now) * 1000;
  323. #else
  324. timeout = lrintf( (float)(deadline - now) / 1000.0 );
  325. #endif
  326. sigemptyset(&sigmask);
  327. sigaddset(&sigmask, SIGHUP);
  328. sigaddset(&sigmask, SIGINT);
  329. sigaddset(&sigmask, SIGQUIT);
  330. sigaddset(&sigmask, SIGPIPE);
  331. sigaddset(&sigmask, SIGTERM);
  332. sigaddset(&sigmask, SIGUSR1);
  333. sigaddset(&sigmask, SIGUSR2);
  334. action.sa_handler = SIG_DFL;
  335. action.sa_mask = sigmask;
  336. action.sa_flags = SA_RESTART;
  337. for (i = 1; i < NSIG; i++)
  338. if (sigismember (&sigmask, i))
  339. sigaction (i, &action, 0);
  340. fds.fd = sockfd;
  341. fds.events = POLLIN;
  342. #if HAVE_PPOLL
  343. poll_err = ppoll (&fds, 1, &timeout_spec, &sigmask);
  344. #else
  345. sigprocmask (SIG_UNBLOCK, &sigmask, &rsigmask);
  346. poll_err = poll (&fds, 1, timeout);
  347. sigprocmask (SIG_SETMASK, &rsigmask, NULL);
  348. #endif
  349. if (poll_err == -1)
  350. {
  351. switch (errno)
  352. {
  353. case EBADF:
  354. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  355. break;
  356. case EFAULT:
  357. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  358. break;
  359. case EINTR:
  360. jack_error ("Error %d: A signal occurred before any requested event", errno);
  361. break;
  362. case EINVAL:
  363. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  364. break;
  365. case ENOMEM:
  366. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  367. break;
  368. }
  369. }
  370. return poll_err;
  371. }
  372. int
  373. netjack_poll (int sockfd, int timeout)
  374. {
  375. struct pollfd fds;
  376. int i, poll_err = 0;
  377. sigset_t sigmask, rsigmask;
  378. struct sigaction action;
  379. sigemptyset(&sigmask);
  380. sigaddset(&sigmask, SIGHUP);
  381. sigaddset(&sigmask, SIGINT);
  382. sigaddset(&sigmask, SIGQUIT);
  383. sigaddset(&sigmask, SIGPIPE);
  384. sigaddset(&sigmask, SIGTERM);
  385. sigaddset(&sigmask, SIGUSR1);
  386. sigaddset(&sigmask, SIGUSR2);
  387. action.sa_handler = SIG_DFL;
  388. action.sa_mask = sigmask;
  389. action.sa_flags = SA_RESTART;
  390. for (i = 1; i < NSIG; i++)
  391. if (sigismember (&sigmask, i))
  392. sigaction (i, &action, 0);
  393. fds.fd = sockfd;
  394. fds.events = POLLIN;
  395. sigprocmask(SIG_UNBLOCK, &sigmask, &rsigmask);
  396. while (poll_err == 0)
  397. {
  398. poll_err = poll (&fds, 1, timeout);
  399. }
  400. sigprocmask(SIG_SETMASK, &rsigmask, NULL);
  401. if (poll_err == -1)
  402. {
  403. switch (errno)
  404. {
  405. case EBADF:
  406. jack_error ("Error %d: An invalid file descriptor was given in one of the sets", errno);
  407. break;
  408. case EFAULT:
  409. jack_error ("Error %d: The array given as argument was not contained in the calling program's address space", errno);
  410. break;
  411. case EINTR:
  412. jack_error ("Error %d: A signal occurred before any requested event", errno);
  413. break;
  414. case EINVAL:
  415. jack_error ("Error %d: The nfds value exceeds the RLIMIT_NOFILE value", errno);
  416. break;
  417. case ENOMEM:
  418. jack_error ("Error %d: There was no space to allocate file descriptor tables", errno);
  419. break;
  420. }
  421. return 0;
  422. }
  423. return 1;
  424. }
  425. #else
  426. int
  427. netjack_poll (int sockfd, int timeout)
  428. {
  429. jack_error( "netjack_poll not implemented\n" );
  430. return 0;
  431. }
  432. int
  433. netjack_poll_deadline (int sockfd, jack_time_t deadline)
  434. {
  435. fd_set fds;
  436. FD_ZERO( &fds );
  437. FD_SET( sockfd, &fds );
  438. struct timeval timeout;
  439. while( 1 ) {
  440. jack_time_t now = jack_get_time();
  441. if( now >= deadline )
  442. return 0;
  443. int timeout_usecs = (deadline - now);
  444. //jack_error( "timeout = %d", timeout_usecs );
  445. timeout.tv_sec = 0;
  446. timeout.tv_usec = (timeout_usecs < 500) ? 500 : timeout_usecs;
  447. int poll_err = select (0, &fds, NULL, NULL, &timeout);
  448. if( poll_err != 0 )
  449. return poll_err;
  450. }
  451. return 0;
  452. }
  453. #endif
  454. // This now reads all a socket has into the cache.
  455. // replacing netjack_recv functions.
  456. void
  457. packet_cache_drain_socket( packet_cache *pcache, int sockfd )
  458. {
  459. char *rx_packet = alloca (pcache->mtu);
  460. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  461. int rcv_len;
  462. jack_nframes_t framecnt;
  463. cache_packet *cpack;
  464. struct sockaddr_in sender_address;
  465. #ifdef WIN32
  466. size_t senderlen = sizeof( struct sockaddr_in );
  467. u_long parm = 1;
  468. ioctlsocket( sockfd, FIONBIO, &parm );
  469. #else
  470. socklen_t senderlen = sizeof( struct sockaddr_in );
  471. #endif
  472. while (1)
  473. {
  474. #ifdef WIN32
  475. rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, 0,
  476. (struct sockaddr*) &sender_address, &senderlen);
  477. #else
  478. rcv_len = recvfrom (sockfd, rx_packet, pcache->mtu, MSG_DONTWAIT,
  479. (struct sockaddr*) &sender_address, &senderlen);
  480. #endif
  481. if (rcv_len < 0)
  482. return;
  483. if (pcache->master_address_valid) {
  484. // Verify its from our master.
  485. if (memcmp (&sender_address, &(pcache->master_address), senderlen) != 0)
  486. continue;
  487. } else {
  488. // Setup this one as master
  489. //printf( "setup master...\n" );
  490. memcpy ( &(pcache->master_address), &sender_address, senderlen );
  491. pcache->master_address_valid = 1;
  492. }
  493. framecnt = ntohl (pkthdr->framecnt);
  494. if( pcache->last_framecnt_retreived_valid && (framecnt <= pcache->last_framecnt_retreived ))
  495. continue;
  496. cpack = packet_cache_get_packet (global_packcache, framecnt);
  497. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  498. cpack->recv_timestamp = jack_get_time();
  499. }
  500. }
  501. void
  502. packet_cache_reset_master_address( packet_cache *pcache )
  503. {
  504. pcache->master_address_valid = 0;
  505. pcache->last_framecnt_retreived = 0;
  506. pcache->last_framecnt_retreived_valid = 0;
  507. }
  508. void
  509. packet_cache_clear_old_packets (packet_cache *pcache, jack_nframes_t framecnt )
  510. {
  511. int i;
  512. for (i = 0; i < pcache->size; i++)
  513. {
  514. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < framecnt))
  515. {
  516. cache_packet_reset (&(pcache->packets[i]));
  517. }
  518. }
  519. }
  520. int
  521. packet_cache_retreive_packet_pointer( packet_cache *pcache, jack_nframes_t framecnt, char **packet_buf, int pkt_size, jack_time_t *timestamp )
  522. {
  523. int i;
  524. cache_packet *cpack = NULL;
  525. for (i = 0; i < pcache->size; i++) {
  526. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  527. cpack = &(pcache->packets[i]);
  528. break;
  529. }
  530. }
  531. if( cpack == NULL ) {
  532. //printf( "retreive packet: %d....not found\n", framecnt );
  533. return -1;
  534. }
  535. if( !cache_packet_is_complete( cpack ) ) {
  536. return -1;
  537. }
  538. // ok. cpack is the one we want and its complete.
  539. *packet_buf = cpack->packet_buf;
  540. if( timestamp )
  541. *timestamp = cpack->recv_timestamp;
  542. pcache->last_framecnt_retreived_valid = 1;
  543. pcache->last_framecnt_retreived = framecnt;
  544. return pkt_size;
  545. }
  546. int
  547. packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt )
  548. {
  549. int i;
  550. cache_packet *cpack = NULL;
  551. for (i = 0; i < pcache->size; i++) {
  552. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
  553. cpack = &(pcache->packets[i]);
  554. break;
  555. }
  556. }
  557. if( cpack == NULL ) {
  558. //printf( "retreive packet: %d....not found\n", framecnt );
  559. return -1;
  560. }
  561. if( !cache_packet_is_complete( cpack ) ) {
  562. return -1;
  563. }
  564. cache_packet_reset (cpack);
  565. packet_cache_clear_old_packets( pcache, framecnt );
  566. return 0;
  567. }
  568. float
  569. packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
  570. {
  571. int num_packets_before_us = 0;
  572. int i;
  573. for (i = 0; i < pcache->size; i++)
  574. {
  575. cache_packet *cpack = &(pcache->packets[i]);
  576. if (cpack->valid && cache_packet_is_complete( cpack ))
  577. if( cpack->framecnt >= expected_framecnt )
  578. num_packets_before_us += 1;
  579. }
  580. return 100.0 * (float)num_packets_before_us / (float)( pcache->size ) ;
  581. }
  582. // Returns 0 when no valid packet is inside the cache.
  583. int
  584. packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  585. {
  586. int i;
  587. jack_nframes_t best_offset = JACK_MAX_FRAMES/2-1;
  588. int retval = 0;
  589. for (i = 0; i < pcache->size; i++)
  590. {
  591. cache_packet *cpack = &(pcache->packets[i]);
  592. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  593. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  594. //printf( "invalid\n" );
  595. continue;
  596. }
  597. if( (cpack->framecnt - expected_framecnt) > best_offset ) {
  598. continue;
  599. }
  600. best_offset = cpack->framecnt - expected_framecnt;
  601. retval = 1;
  602. if( best_offset == 0 )
  603. break;
  604. }
  605. if( retval && framecnt )
  606. *framecnt = expected_framecnt + best_offset;
  607. return retval;
  608. }
  609. int
  610. packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_t *framecnt )
  611. {
  612. int i;
  613. jack_nframes_t best_value = 0;
  614. int retval = 0;
  615. for (i = 0; i < pcache->size; i++)
  616. {
  617. cache_packet *cpack = &(pcache->packets[i]);
  618. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  619. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  620. //printf( "invalid\n" );
  621. continue;
  622. }
  623. if (cpack->framecnt < best_value) {
  624. continue;
  625. }
  626. best_value = cpack->framecnt;
  627. retval = 1;
  628. }
  629. if( retval && framecnt )
  630. *framecnt = best_value;
  631. return retval;
  632. }
  633. // Returns 0 when no valid packet is inside the cache.
  634. int
  635. packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecnt, jack_nframes_t *framecnt )
  636. {
  637. int i;
  638. jack_nframes_t best_offset = 0;
  639. int retval = 0;
  640. for (i = 0; i < pcache->size; i++)
  641. {
  642. cache_packet *cpack = &(pcache->packets[i]);
  643. //printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
  644. if (!cpack->valid || !cache_packet_is_complete( cpack )) {
  645. //printf( "invalid\n" );
  646. continue;
  647. }
  648. if( (cpack->framecnt - expected_framecnt) < best_offset ) {
  649. continue;
  650. }
  651. best_offset = cpack->framecnt - expected_framecnt;
  652. retval = 1;
  653. if( best_offset == 0 )
  654. break;
  655. }
  656. if( retval && framecnt )
  657. *framecnt = JACK_MAX_FRAMES - best_offset;
  658. return retval;
  659. }
  660. // fragmented packet IO
  661. int
  662. netjack_recvfrom (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, size_t *addr_size, int mtu)
  663. {
  664. int retval;
  665. socklen_t from_len = *addr_size;
  666. if (pkt_size <= mtu) {
  667. retval = recvfrom (sockfd, packet_buf, pkt_size, flags, addr, &from_len);
  668. *addr_size = from_len;
  669. return retval;
  670. }
  671. char *rx_packet = alloca (mtu);
  672. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  673. int rcv_len;
  674. jack_nframes_t framecnt;
  675. cache_packet *cpack;
  676. do
  677. {
  678. rcv_len = recvfrom (sockfd, rx_packet, mtu, 0, addr, &from_len);
  679. if (rcv_len < 0)
  680. return rcv_len;
  681. framecnt = ntohl (pkthdr->framecnt);
  682. cpack = packet_cache_get_packet (global_packcache, framecnt);
  683. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  684. } while (!cache_packet_is_complete (cpack));
  685. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  686. cache_packet_reset (cpack);
  687. *addr_size = from_len;
  688. return pkt_size;
  689. }
  690. int
  691. netjack_recv (int sockfd, char *packet_buf, int pkt_size, int flags, int mtu)
  692. {
  693. if (pkt_size <= mtu)
  694. return recv (sockfd, packet_buf, pkt_size, flags);
  695. char *rx_packet = alloca (mtu);
  696. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  697. int rcv_len;
  698. jack_nframes_t framecnt;
  699. cache_packet *cpack;
  700. do
  701. {
  702. rcv_len = recv (sockfd, rx_packet, mtu, flags);
  703. if (rcv_len < 0)
  704. return rcv_len;
  705. framecnt = ntohl (pkthdr->framecnt);
  706. cpack = packet_cache_get_packet (global_packcache, framecnt);
  707. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  708. } while (!cache_packet_is_complete (cpack));
  709. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  710. cache_packet_reset (cpack);
  711. return pkt_size;
  712. }
  713. void
  714. netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
  715. {
  716. int frag_cnt = 0;
  717. char *tx_packet, *dataX;
  718. jacknet_packet_header *pkthdr;
  719. tx_packet = alloca (mtu + 10);
  720. dataX = tx_packet + sizeof (jacknet_packet_header);
  721. pkthdr = (jacknet_packet_header *) tx_packet;
  722. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  723. if (pkt_size <= mtu) {
  724. int err;
  725. pkthdr = (jacknet_packet_header *) packet_buf;
  726. pkthdr->fragment_nr = htonl (0);
  727. err = sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  728. if( err<0 ) {
  729. //printf( "error in send\n" );
  730. perror( "send" );
  731. }
  732. }
  733. else
  734. {
  735. int err;
  736. // Copy the packet header to the tx pack first.
  737. memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
  738. // Now loop and send all
  739. char *packet_bufX = packet_buf + sizeof (jacknet_packet_header);
  740. while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size))
  741. {
  742. pkthdr->fragment_nr = htonl (frag_cnt++);
  743. memcpy (dataX, packet_bufX, fragment_payload_size);
  744. sendto (sockfd, tx_packet, mtu, flags, addr, addr_size);
  745. packet_bufX += fragment_payload_size;
  746. }
  747. int last_payload_size = packet_buf + pkt_size - packet_bufX;
  748. memcpy (dataX, packet_bufX, last_payload_size);
  749. pkthdr->fragment_nr = htonl (frag_cnt);
  750. //jack_log("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
  751. // sendto(last_pack_size);
  752. err = sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
  753. if( err<0 ) {
  754. //printf( "error in send\n" );
  755. perror( "send" );
  756. }
  757. }
  758. }
  759. void
  760. decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  761. {
  762. int i;
  763. jack_midi_clear_buffer (buf);
  764. for (i = 0; i < buffer_size_uint32 - 3;)
  765. {
  766. uint32_t payload_size;
  767. payload_size = buffer_uint32[i];
  768. payload_size = ntohl (payload_size);
  769. if (payload_size)
  770. {
  771. jack_midi_event_t event;
  772. event.time = ntohl (buffer_uint32[i+1]);
  773. event.size = ntohl (buffer_uint32[i+2]);
  774. event.buffer = (jack_midi_data_t*) (&(buffer_uint32[i+3]));
  775. jack_midi_event_write (buf, event.time, event.buffer, event.size);
  776. // skip to the next event
  777. unsigned int nb_data_quads = (((event.size-1) & ~0x3) >> 2)+1;
  778. i += 3+nb_data_quads;
  779. }
  780. else
  781. break; // no events can follow an empty event, we're done
  782. }
  783. }
  784. void
  785. encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  786. {
  787. int i;
  788. unsigned int written = 0;
  789. // midi port, encode midi events
  790. unsigned int nevents = jack_midi_get_event_count (buf);
  791. for (i = 0; i < nevents; ++i)
  792. {
  793. jack_midi_event_t event;
  794. jack_midi_event_get (&event, buf, i);
  795. unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
  796. unsigned int payload_size = 3 + nb_data_quads;
  797. // only write if we have sufficient space for the event
  798. // otherwise drop it
  799. if (written + payload_size < buffer_size_uint32 - 1)
  800. {
  801. // write header
  802. buffer_uint32[written]=htonl (payload_size);
  803. written++;
  804. buffer_uint32[written]=htonl (event.time);
  805. written++;
  806. buffer_uint32[written]=htonl (event.size);
  807. written++;
  808. // write data
  809. jack_midi_data_t* tmpbuff = (jack_midi_data_t*)(&(buffer_uint32[written]));
  810. memcpy (tmpbuff, event.buffer, event.size);
  811. written += nb_data_quads;
  812. }
  813. else
  814. {
  815. // buffer overflow
  816. jack_error ("midi buffer overflow");
  817. break;
  818. }
  819. }
  820. // now put a netjack_midi 'no-payload' event, signaling EOF
  821. buffer_uint32[written]=0;
  822. }
  823. // render functions for float
  824. void
  825. 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)
  826. {
  827. int chn = 0;
  828. JSList *node = capture_ports;
  829. #if HAVE_SAMPLERATE
  830. JSList *src_node = capture_srcs;
  831. #endif
  832. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  833. if( !packet_payload )
  834. return;
  835. while (node != NULL)
  836. {
  837. int i;
  838. int_float_t val;
  839. #if HAVE_SAMPLERATE
  840. SRC_DATA src;
  841. #endif
  842. jack_port_t *port = (jack_port_t *) node->data;
  843. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  844. const char *porttype = jack_port_type (port);
  845. if (jack_port_is_audio (porttype))
  846. {
  847. #if HAVE_SAMPLERATE
  848. // audio port, resample if necessary
  849. if (net_period_down != nframes)
  850. {
  851. SRC_STATE *src_state = src_node->data;
  852. for (i = 0; i < net_period_down; i++)
  853. {
  854. packet_bufX[i] = ntohl (packet_bufX[i]);
  855. }
  856. src.data_in = (float *) packet_bufX;
  857. src.input_frames = net_period_down;
  858. src.data_out = buf;
  859. src.output_frames = nframes;
  860. src.src_ratio = (float) nframes / (float) net_period_down;
  861. src.end_of_input = 0;
  862. src_set_ratio (src_state, src.src_ratio);
  863. src_process (src_state, &src);
  864. src_node = jack_slist_next (src_node);
  865. }
  866. else
  867. #endif
  868. {
  869. if( dont_htonl_floats )
  870. {
  871. memcpy( buf, packet_bufX, net_period_down*sizeof(jack_default_audio_sample_t));
  872. }
  873. else
  874. {
  875. for (i = 0; i < net_period_down; i++)
  876. {
  877. val.i = packet_bufX[i];
  878. val.i = ntohl (val.i);
  879. buf[i] = val.f;
  880. }
  881. }
  882. }
  883. }
  884. else if (jack_port_is_midi (porttype))
  885. {
  886. // midi port, decode midi events
  887. // convert the data buffer to a standard format (uint32_t based)
  888. unsigned int buffer_size_uint32 = net_period_down;
  889. uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
  890. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  891. }
  892. packet_bufX = (packet_bufX + net_period_down);
  893. node = jack_slist_next (node);
  894. chn++;
  895. }
  896. }
  897. void
  898. 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 )
  899. {
  900. int chn = 0;
  901. JSList *node = playback_ports;
  902. #if HAVE_SAMPLERATE
  903. JSList *src_node = playback_srcs;
  904. #endif
  905. uint32_t *packet_bufX = (uint32_t *) packet_payload;
  906. while (node != NULL)
  907. {
  908. #if HAVE_SAMPLERATE
  909. SRC_DATA src;
  910. #endif
  911. int i;
  912. int_float_t val;
  913. jack_port_t *port = (jack_port_t *) node->data;
  914. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  915. const char *porttype = jack_port_type (port);
  916. if (jack_port_is_audio (porttype))
  917. {
  918. // audio port, resample if necessary
  919. #if HAVE_SAMPLERATE
  920. if (net_period_up != nframes) {
  921. SRC_STATE *src_state = src_node->data;
  922. src.data_in = buf;
  923. src.input_frames = nframes;
  924. src.data_out = (float *) packet_bufX;
  925. src.output_frames = net_period_up;
  926. src.src_ratio = (float) net_period_up / (float) nframes;
  927. src.end_of_input = 0;
  928. src_set_ratio (src_state, src.src_ratio);
  929. src_process (src_state, &src);
  930. for (i = 0; i < net_period_up; i++)
  931. {
  932. packet_bufX[i] = htonl (packet_bufX[i]);
  933. }
  934. src_node = jack_slist_next (src_node);
  935. }
  936. else
  937. #endif
  938. {
  939. if( dont_htonl_floats )
  940. {
  941. memcpy( packet_bufX, buf, net_period_up*sizeof(jack_default_audio_sample_t) );
  942. }
  943. else
  944. {
  945. for (i = 0; i < net_period_up; i++)
  946. {
  947. val.f = buf[i];
  948. val.i = htonl (val.i);
  949. packet_bufX[i] = val.i;
  950. }
  951. }
  952. }
  953. }
  954. else if (jack_port_is_midi (porttype))
  955. {
  956. // encode midi events from port to packet
  957. // convert the data buffer to a standard format (uint32_t based)
  958. unsigned int buffer_size_uint32 = net_period_up;
  959. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  960. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  961. }
  962. packet_bufX = (packet_bufX + net_period_up);
  963. node = jack_slist_next (node);
  964. chn++;
  965. }
  966. }
  967. // render functions for 16bit
  968. void
  969. 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)
  970. {
  971. int chn = 0;
  972. JSList *node = capture_ports;
  973. #if HAVE_SAMPLERATE
  974. JSList *src_node = capture_srcs;
  975. #endif
  976. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  977. if( !packet_payload )
  978. return;
  979. while (node != NULL)
  980. {
  981. int i;
  982. //uint32_t val;
  983. #if HAVE_SAMPLERATE
  984. SRC_DATA src;
  985. #endif
  986. jack_port_t *port = (jack_port_t *) node->data;
  987. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  988. #if HAVE_SAMPLERATE
  989. float *floatbuf = alloca (sizeof(float) * net_period_down);
  990. #endif
  991. const char *porttype = jack_port_type (port);
  992. if (jack_port_is_audio (porttype))
  993. {
  994. // audio port, resample if necessary
  995. #if HAVE_SAMPLERATE
  996. if (net_period_down != nframes)
  997. {
  998. SRC_STATE *src_state = src_node->data;
  999. for (i = 0; i < net_period_down; i++)
  1000. {
  1001. floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
  1002. }
  1003. src.data_in = floatbuf;
  1004. src.input_frames = net_period_down;
  1005. src.data_out = buf;
  1006. src.output_frames = nframes;
  1007. src.src_ratio = (float) nframes / (float) net_period_down;
  1008. src.end_of_input = 0;
  1009. src_set_ratio (src_state, src.src_ratio);
  1010. src_process (src_state, &src);
  1011. src_node = jack_slist_next (src_node);
  1012. }
  1013. else
  1014. #endif
  1015. for (i = 0; i < net_period_down; i++)
  1016. buf[i] = ((float) ntohs (packet_bufX[i])) / 32768.0 - 1.0;
  1017. }
  1018. else if (jack_port_is_midi (porttype))
  1019. {
  1020. // midi port, decode midi events
  1021. // convert the data buffer to a standard format (uint32_t based)
  1022. unsigned int buffer_size_uint32 = net_period_down / 2;
  1023. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1024. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1025. }
  1026. packet_bufX = (packet_bufX + net_period_down);
  1027. node = jack_slist_next (node);
  1028. chn++;
  1029. }
  1030. }
  1031. void
  1032. 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)
  1033. {
  1034. int chn = 0;
  1035. JSList *node = playback_ports;
  1036. #if HAVE_SAMPLERATE
  1037. JSList *src_node = playback_srcs;
  1038. #endif
  1039. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  1040. while (node != NULL)
  1041. {
  1042. #if HAVE_SAMPLERATE
  1043. SRC_DATA src;
  1044. #endif
  1045. int i;
  1046. jack_port_t *port = (jack_port_t *) node->data;
  1047. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1048. const char *porttype = jack_port_type (port);
  1049. if (jack_port_is_audio (porttype))
  1050. {
  1051. // audio port, resample if necessary
  1052. #if HAVE_SAMPLERATE
  1053. if (net_period_up != nframes)
  1054. {
  1055. SRC_STATE *src_state = src_node->data;
  1056. float *floatbuf = alloca (sizeof(float) * net_period_up);
  1057. src.data_in = buf;
  1058. src.input_frames = nframes;
  1059. src.data_out = floatbuf;
  1060. src.output_frames = net_period_up;
  1061. src.src_ratio = (float) net_period_up / (float) nframes;
  1062. src.end_of_input = 0;
  1063. src_set_ratio (src_state, src.src_ratio);
  1064. src_process (src_state, &src);
  1065. for (i = 0; i < net_period_up; i++)
  1066. {
  1067. packet_bufX[i] = htons (((uint16_t)((floatbuf[i] + 1.0) * 32767.0)));
  1068. }
  1069. src_node = jack_slist_next (src_node);
  1070. }
  1071. else
  1072. #endif
  1073. for (i = 0; i < net_period_up; i++)
  1074. packet_bufX[i] = htons(((uint16_t)((buf[i] + 1.0) * 32767.0)));
  1075. }
  1076. else if (jack_port_is_midi (porttype))
  1077. {
  1078. // encode midi events from port to packet
  1079. // convert the data buffer to a standard format (uint32_t based)
  1080. unsigned int buffer_size_uint32 = net_period_up / 2;
  1081. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1082. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1083. }
  1084. packet_bufX = (packet_bufX + net_period_up);
  1085. node = jack_slist_next (node);
  1086. chn++;
  1087. }
  1088. }
  1089. // render functions for 8bit
  1090. void
  1091. 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)
  1092. {
  1093. int chn = 0;
  1094. JSList *node = capture_ports;
  1095. #if HAVE_SAMPLERATE
  1096. JSList *src_node = capture_srcs;
  1097. #endif
  1098. int8_t *packet_bufX = (int8_t *)packet_payload;
  1099. if( !packet_payload )
  1100. return;
  1101. while (node != NULL)
  1102. {
  1103. int i;
  1104. //uint32_t val;
  1105. #if HAVE_SAMPLERATE
  1106. SRC_DATA src;
  1107. #endif
  1108. jack_port_t *port = (jack_port_t *) node->data;
  1109. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1110. #if HAVE_SAMPLERATE
  1111. float *floatbuf = alloca (sizeof (float) * net_period_down);
  1112. #endif
  1113. const char *porttype = jack_port_type (port);
  1114. if (jack_port_is_audio(porttype))
  1115. {
  1116. #if HAVE_SAMPLERATE
  1117. // audio port, resample if necessary
  1118. if (net_period_down != nframes)
  1119. {
  1120. SRC_STATE *src_state = src_node->data;
  1121. for (i = 0; i < net_period_down; i++)
  1122. floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
  1123. src.data_in = floatbuf;
  1124. src.input_frames = net_period_down;
  1125. src.data_out = buf;
  1126. src.output_frames = nframes;
  1127. src.src_ratio = (float) nframes / (float) net_period_down;
  1128. src.end_of_input = 0;
  1129. src_set_ratio (src_state, src.src_ratio);
  1130. src_process (src_state, &src);
  1131. src_node = jack_slist_next (src_node);
  1132. }
  1133. else
  1134. #endif
  1135. for (i = 0; i < net_period_down; i++)
  1136. buf[i] = ((float) packet_bufX[i]) / 127.0;
  1137. }
  1138. else if (jack_port_is_midi (porttype))
  1139. {
  1140. // midi port, decode midi events
  1141. // convert the data buffer to a standard format (uint32_t based)
  1142. unsigned int buffer_size_uint32 = net_period_down / 2;
  1143. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1144. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1145. }
  1146. packet_bufX = (packet_bufX + net_period_down);
  1147. node = jack_slist_next (node);
  1148. chn++;
  1149. }
  1150. }
  1151. void
  1152. 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)
  1153. {
  1154. int chn = 0;
  1155. JSList *node = playback_ports;
  1156. #if HAVE_SAMPLERATE
  1157. JSList *src_node = playback_srcs;
  1158. #endif
  1159. int8_t *packet_bufX = (int8_t *)packet_payload;
  1160. while (node != NULL)
  1161. {
  1162. #if HAVE_SAMPLERATE
  1163. SRC_DATA src;
  1164. #endif
  1165. int i;
  1166. jack_port_t *port = (jack_port_t *) node->data;
  1167. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1168. const char *porttype = jack_port_type (port);
  1169. if (jack_port_is_audio (porttype))
  1170. {
  1171. #if HAVE_SAMPLERATE
  1172. // audio port, resample if necessary
  1173. if (net_period_up != nframes)
  1174. {
  1175. SRC_STATE *src_state = src_node->data;
  1176. float *floatbuf = alloca (sizeof (float) * net_period_up);
  1177. src.data_in = buf;
  1178. src.input_frames = nframes;
  1179. src.data_out = floatbuf;
  1180. src.output_frames = net_period_up;
  1181. src.src_ratio = (float) net_period_up / (float) nframes;
  1182. src.end_of_input = 0;
  1183. src_set_ratio (src_state, src.src_ratio);
  1184. src_process (src_state, &src);
  1185. for (i = 0; i < net_period_up; i++)
  1186. packet_bufX[i] = floatbuf[i] * 127.0;
  1187. src_node = jack_slist_next (src_node);
  1188. }
  1189. else
  1190. #endif
  1191. for (i = 0; i < net_period_up; i++)
  1192. packet_bufX[i] = buf[i] * 127.0;
  1193. }
  1194. else if (jack_port_is_midi (porttype))
  1195. {
  1196. // encode midi events from port to packet
  1197. // convert the data buffer to a standard format (uint32_t based)
  1198. unsigned int buffer_size_uint32 = net_period_up / 4;
  1199. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1200. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1201. }
  1202. packet_bufX = (packet_bufX + net_period_up);
  1203. node = jack_slist_next (node);
  1204. chn++;
  1205. }
  1206. }
  1207. #if HAVE_CELT
  1208. // render functions for celt.
  1209. void
  1210. 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)
  1211. {
  1212. int chn = 0;
  1213. JSList *node = capture_ports;
  1214. JSList *src_node = capture_srcs;
  1215. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1216. while (node != NULL)
  1217. {
  1218. jack_port_t *port = (jack_port_t *) node->data;
  1219. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1220. const char *porttype = jack_port_type (port);
  1221. if (jack_port_is_audio (porttype))
  1222. {
  1223. // audio port, decode celt data.
  1224. CELTDecoder *decoder = src_node->data;
  1225. if( !packet_payload )
  1226. celt_decode_float( decoder, NULL, net_period_down, buf );
  1227. else
  1228. celt_decode_float( decoder, packet_bufX, net_period_down, buf );
  1229. src_node = jack_slist_next (src_node);
  1230. }
  1231. else if (jack_port_is_midi (porttype))
  1232. {
  1233. // midi port, decode midi events
  1234. // convert the data buffer to a standard format (uint32_t based)
  1235. unsigned int buffer_size_uint32 = net_period_down / 2;
  1236. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1237. if( packet_payload )
  1238. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1239. }
  1240. packet_bufX = (packet_bufX + net_period_down);
  1241. node = jack_slist_next (node);
  1242. chn++;
  1243. }
  1244. }
  1245. void
  1246. 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)
  1247. {
  1248. int chn = 0;
  1249. JSList *node = playback_ports;
  1250. JSList *src_node = playback_srcs;
  1251. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  1252. while (node != NULL)
  1253. {
  1254. jack_port_t *port = (jack_port_t *) node->data;
  1255. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  1256. const char *porttype = jack_port_type (port);
  1257. if (jack_port_is_audio (porttype))
  1258. {
  1259. // audio port, encode celt data.
  1260. int encoded_bytes;
  1261. float *floatbuf = alloca (sizeof(float) * nframes );
  1262. memcpy( floatbuf, buf, nframes*sizeof(float) );
  1263. CELTEncoder *encoder = src_node->data;
  1264. encoded_bytes = celt_encode_float( encoder, floatbuf, NULL, packet_bufX, net_period_up );
  1265. if( encoded_bytes != net_period_up )
  1266. printf( "something in celt changed. netjack needs to be changed to handle this.\n" );
  1267. src_node = jack_slist_next( src_node );
  1268. }
  1269. else if (jack_port_is_midi (porttype))
  1270. {
  1271. // encode midi events from port to packet
  1272. // convert the data buffer to a standard format (uint32_t based)
  1273. unsigned int buffer_size_uint32 = net_period_up / 2;
  1274. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  1275. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  1276. }
  1277. packet_bufX = (packet_bufX + net_period_up);
  1278. node = jack_slist_next (node);
  1279. chn++;
  1280. }
  1281. }
  1282. #endif
  1283. /* Wrapper functions with bitdepth argument... */
  1284. void
  1285. 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)
  1286. {
  1287. if (bitdepth == 8)
  1288. render_payload_to_jack_ports_8bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1289. else if (bitdepth == 16)
  1290. render_payload_to_jack_ports_16bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1291. #if HAVE_CELT
  1292. else if (bitdepth == CELT_MODE)
  1293. render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  1294. #endif
  1295. else
  1296. render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes, dont_htonl_floats);
  1297. }
  1298. void
  1299. 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)
  1300. {
  1301. if (bitdepth == 8)
  1302. render_jack_ports_to_payload_8bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1303. else if (bitdepth == 16)
  1304. render_jack_ports_to_payload_16bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1305. #if HAVE_CELT
  1306. else if (bitdepth == CELT_MODE)
  1307. render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  1308. #endif
  1309. else
  1310. render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);
  1311. }