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.

1528 lines
45KB

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