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.

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