jack1 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.

1549 lines
45KB

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