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.

1380 lines
43KB

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