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.

888 lines
28KB

  1. /*
  2. * NetJack - Packet Handling functions
  3. *
  4. * used by the driver and the jacknet_client
  5. *
  6. * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
  23. *
  24. */
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <memory.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <errno.h>
  31. #include <stdarg.h>
  32. #include <jack/types.h>
  33. #include <jack/engine.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <samplerate.h>
  38. #include "net_driver.h"
  39. #include "netjack_packet.h"
  40. int fraggo = 0;
  41. packet_cache *global_packcache;
  42. void
  43. packet_header_hton(jacknet_packet_header *pkthdr)
  44. {
  45. pkthdr->channels = htonl(pkthdr->channels);
  46. pkthdr->period_size = htonl(pkthdr->period_size);
  47. pkthdr->sample_rate = htonl(pkthdr->sample_rate);
  48. pkthdr->sync_state = htonl(pkthdr->sync_state);
  49. pkthdr->transport_frame = htonl(pkthdr->transport_frame);
  50. pkthdr->transport_state = htonl(pkthdr->transport_state);
  51. pkthdr->framecnt = htonl(pkthdr->framecnt);
  52. pkthdr->latency = htonl(pkthdr->latency);
  53. pkthdr->reply_port = htonl(pkthdr->reply_port);
  54. pkthdr->mtu = htonl(pkthdr->mtu);
  55. pkthdr->fragment_nr = htonl(pkthdr->fragment_nr);
  56. }
  57. void
  58. packet_header_ntoh(jacknet_packet_header *pkthdr)
  59. {
  60. pkthdr->channels = ntohl(pkthdr->channels);
  61. pkthdr->period_size = ntohl(pkthdr->period_size);
  62. pkthdr->sample_rate = ntohl(pkthdr->sample_rate);
  63. pkthdr->sync_state = ntohl(pkthdr->sync_state);
  64. pkthdr->transport_frame = ntohl(pkthdr->transport_frame);
  65. pkthdr->transport_state = ntohl(pkthdr->transport_state);
  66. pkthdr->framecnt = ntohl(pkthdr->framecnt);
  67. pkthdr->latency = ntohl(pkthdr->latency);
  68. pkthdr->reply_port = ntohl(pkthdr->reply_port);
  69. pkthdr->mtu = ntohl(pkthdr->mtu);
  70. pkthdr->fragment_nr = ntohl(pkthdr->fragment_nr);
  71. }
  72. int get_sample_size(int bitdepth)
  73. {
  74. if (bitdepth == 8)
  75. return sizeof(int8_t);
  76. if (bitdepth == 16)
  77. return sizeof(int16_t);
  78. return sizeof(int32_t);
  79. }
  80. // fragment management functions.
  81. packet_cache *packet_cache_new(int num_packets, int pkt_size, int mtu)
  82. {
  83. int fragment_payload_size = mtu - sizeof(jacknet_packet_header);
  84. int fragment_number = (pkt_size - sizeof(jacknet_packet_header) - 1) / fragment_payload_size + 1;
  85. int i;
  86. packet_cache *pcache = malloc(sizeof(packet_cache));
  87. if (pcache == NULL) {
  88. printf("could not allocate packet cache (1)\n");
  89. return NULL;
  90. }
  91. pcache->size = num_packets;
  92. pcache->packets = malloc(sizeof(cache_packet) * num_packets);
  93. if (pcache->packets == NULL) {
  94. printf("could not allocate packet cache (2)\n");
  95. return NULL;
  96. }
  97. for (i = 0; i < num_packets; i++) {
  98. pcache->packets[i].valid = 0;
  99. pcache->packets[i].num_fragments = fragment_number;
  100. pcache->packets[i].packet_size = pkt_size;
  101. pcache->packets[i].mtu = mtu;
  102. pcache->packets[i].framecnt = 0;
  103. pcache->packets[i].fragment_array = malloc(sizeof(char) * fragment_number);
  104. pcache->packets[i].packet_buf = malloc(pkt_size);
  105. if ((pcache->packets[i].fragment_array == NULL) || (pcache->packets[i].packet_buf == NULL)) {
  106. printf("could not allocate packet cache (3)\n");
  107. return NULL;
  108. }
  109. }
  110. return pcache;
  111. }
  112. void packet_cache_free(packet_cache *pcache)
  113. {
  114. int i;
  115. for (i = 0; i < pcache->size; i++) {
  116. free(pcache->packets[i].fragment_array);
  117. free(pcache->packets[i].packet_buf);
  118. }
  119. free(pcache->packets);
  120. free(pcache);
  121. }
  122. cache_packet *packet_cache_get_packet(packet_cache *pcache, jack_nframes_t framecnt)
  123. {
  124. int i;
  125. cache_packet *retval;
  126. for (i = 0; i < pcache->size; i++) {
  127. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt))
  128. return &(pcache->packets[i]);
  129. }
  130. // The Packet is not in the packet cache.
  131. // find a free packet.
  132. retval = packet_cache_get_free_packet(pcache);
  133. if (retval != NULL) {
  134. cache_packet_set_framecnt(retval, framecnt);
  135. return retval;
  136. }
  137. // No Free Packet available
  138. // Get The Oldest packet and reset it.
  139. retval = packet_cache_get_oldest_packet(pcache);
  140. cache_packet_reset(retval);
  141. cache_packet_set_framecnt(retval, framecnt);
  142. return retval;
  143. }
  144. cache_packet *packet_cache_get_oldest_packet(packet_cache *pcache)
  145. {
  146. jack_nframes_t minimal_frame = 0;
  147. cache_packet *retval = &(pcache->packets[0]);
  148. int i;
  149. for (i = 0; i < pcache->size; i++) {
  150. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < minimal_frame)) {
  151. minimal_frame = pcache->packets[i].framecnt;
  152. retval = &(pcache->packets[i]);
  153. }
  154. }
  155. return retval;
  156. }
  157. cache_packet *packet_cache_get_free_packet(packet_cache *pcache)
  158. {
  159. int i;
  160. for (i = 0; i < pcache->size; i++) {
  161. if (pcache->packets[i].valid == 0)
  162. return &(pcache->packets[i]);
  163. }
  164. return NULL;
  165. }
  166. void cache_packet_reset(cache_packet *pack)
  167. {
  168. int i;
  169. pack->valid = 0;
  170. // XXX: i dont think this is necessary here...
  171. // fragement array is cleared in _set_framecnt()
  172. for (i = 0; i < pack->num_fragments; i++)
  173. pack->fragment_array[i] = 0;
  174. }
  175. void cache_packet_set_framecnt(cache_packet *pack, jack_nframes_t framecnt)
  176. {
  177. int i;
  178. pack->framecnt = framecnt;
  179. for (i = 0; i < pack->num_fragments; i++)
  180. pack->fragment_array[i] = 0;
  181. pack->valid = 1;
  182. }
  183. void cache_packet_add_fragment(cache_packet *pack, char *packet_buf, int rcv_len)
  184. {
  185. jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
  186. int fragment_payload_size = pack->mtu - sizeof(jacknet_packet_header);
  187. char *packet_bufX = pack->packet_buf + sizeof(jacknet_packet_header);
  188. char *dataX = packet_buf + sizeof(jacknet_packet_header);
  189. jack_nframes_t fragment_nr = ntohl(pkthdr->fragment_nr);
  190. jack_nframes_t framecnt = ntohl(pkthdr->framecnt);
  191. if (framecnt != pack->framecnt) {
  192. printf("errror. framecnts dont match\n");
  193. return;
  194. }
  195. if (fragment_nr == 0) {
  196. memcpy(pack->packet_buf, packet_buf, pack->mtu);
  197. pack->fragment_array[0] = 1;
  198. return;
  199. }
  200. if ((fragment_nr < pack->num_fragments) && (fragment_nr > 0)) {
  201. if ((fragment_nr * fragment_payload_size + rcv_len - sizeof(jacknet_packet_header)) <= (pack->packet_size - sizeof(jacknet_packet_header))) {
  202. memcpy(packet_bufX + fragment_nr * fragment_payload_size, dataX, rcv_len - sizeof(jacknet_packet_header));
  203. pack->fragment_array[fragment_nr] = 1;
  204. } else {
  205. printf("too long packet received...");
  206. }
  207. }
  208. }
  209. int cache_packet_is_complete(cache_packet *pack)
  210. {
  211. int i;
  212. for (i = 0; i < pack->num_fragments; i++)
  213. if (pack->fragment_array[i] == 0)
  214. return FALSE;
  215. return TRUE;
  216. }
  217. // fragmented packet IO
  218. int netjack_recvfrom(int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, socklen_t *addr_size, int mtu)
  219. {
  220. if (pkt_size <= mtu) {
  221. return recvfrom(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  222. } else {
  223. char *rx_packet = alloca(mtu);
  224. jacknet_packet_header *pkthdr = (jacknet_packet_header *)rx_packet;
  225. int rcv_len;
  226. jack_nframes_t framecnt;
  227. cache_packet *cpack;
  228. rx_again:
  229. rcv_len = recvfrom(sockfd, rx_packet, mtu, 0, addr, addr_size);
  230. if (rcv_len < 0)
  231. return rcv_len;
  232. framecnt = ntohl(pkthdr->framecnt);
  233. cpack = packet_cache_get_packet(global_packcache, framecnt);
  234. cache_packet_add_fragment(cpack, rx_packet, rcv_len);
  235. if (cache_packet_is_complete(cpack)) {
  236. memcpy(packet_buf, cpack->packet_buf, pkt_size);
  237. cache_packet_reset(cpack);
  238. return pkt_size;
  239. }
  240. goto rx_again;
  241. }
  242. }
  243. int netjack_recv(int sockfd, char *packet_buf, int pkt_size, int flags, int mtu)
  244. {
  245. if (pkt_size <= mtu) {
  246. return recv(sockfd, packet_buf, pkt_size, flags);
  247. } else {
  248. char *rx_packet = alloca(mtu);
  249. jacknet_packet_header *pkthdr = (jacknet_packet_header *)rx_packet;
  250. int rcv_len;
  251. jack_nframes_t framecnt;
  252. cache_packet *cpack;
  253. rx_again:
  254. rcv_len = recv(sockfd, rx_packet, mtu, flags);
  255. if (rcv_len < 0)
  256. return rcv_len;
  257. framecnt = ntohl(pkthdr->framecnt);
  258. cpack = packet_cache_get_packet(global_packcache, framecnt);
  259. cache_packet_add_fragment(cpack, rx_packet, rcv_len);
  260. if (cache_packet_is_complete(cpack)) {
  261. memcpy(packet_buf, cpack->packet_buf, pkt_size);
  262. cache_packet_reset(cpack);
  263. return pkt_size;
  264. }
  265. goto rx_again;
  266. }
  267. }
  268. #if 0
  269. int netjack_recvfrom(int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, socklen_t *addr_size, int mtu)
  270. {
  271. char *rx_packet;
  272. char *dataX;
  273. int i;
  274. int fragment_payload_size;
  275. char *fragment_array;
  276. int fragment_number;
  277. int rx_frag_count, framenum;
  278. jacknet_packet_header *pkthdr;
  279. // Now loop and send all
  280. char *packet_bufX;
  281. // wait for fragment_nr == 0
  282. int rcv_len;
  283. rx_packet = alloca(mtu);
  284. dataX = rx_packet + sizeof(jacknet_packet_header);
  285. fragment_payload_size = mtu - sizeof(jacknet_packet_header);
  286. pkthdr = (jacknet_packet_header *)rx_packet;
  287. packet_bufX = packet_buf + sizeof(jacknet_packet_header);
  288. fragment_number = (pkt_size - sizeof(jacknet_packet_header) - 1) / fragment_payload_size + 1;
  289. fragment_array = alloca(fragment_number);
  290. for (i = 0; i < fragment_number; i++)
  291. fragment_array[i] = 0;
  292. if (pkt_size <= mtu) {
  293. return recvfrom(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  294. } else {
  295. rx_again:
  296. rcv_len = recvfrom(sockfd, rx_packet, mtu, 0, addr, addr_size);
  297. if (rcv_len < 0)
  298. return rcv_len;
  299. if (rcv_len >= sizeof(jacknet_packet_header)) {
  300. //printf("got fragmentooooo_nr = %d recv_len = %d\n", ntohl(pkthdr->fragment_nr), rcv_len);
  301. if ((ntohl(pkthdr->fragment_nr)) != 0)
  302. goto rx_again;
  303. } else {
  304. goto rx_again;
  305. }
  306. // ok... we have read a fragement 0;
  307. // copy the data into the packet buffer...
  308. memcpy(packet_buf, rx_packet, mtu);
  309. rx_frag_count = 1;
  310. framenum = ntohl(pkthdr->framecnt);
  311. fragment_array[0] = 1;
  312. while (rx_frag_count < fragment_number) {
  313. rcv_len = recvfrom(sockfd, rx_packet, mtu, 0, addr, addr_size);
  314. if (rcv_len < 0)
  315. return -1;
  316. if (ntohl(pkthdr->framecnt) < framenum) {
  317. //printf("Out of Order Framecnt: i abort on this packet\n");
  318. printf("Old Fragment !!! (got: %d, exp: %d)\n", ntohl(pkthdr->framecnt), framenum);
  319. continue;
  320. }
  321. if (ntohl(pkthdr->framecnt) > framenum) {
  322. printf("Newer Fragment !!! (got: %d, exp: %d) Switching to new Packet.\n", ntohl(pkthdr->framecnt), framenum);
  323. // Copy the new Packetheader up Front
  324. memcpy(packet_buf, rx_packet, sizeof(jacknet_packet_header));
  325. rx_frag_count = 0;
  326. framenum = ntohl(pkthdr->framecnt);
  327. }
  328. #if 0
  329. if (ntohl(pkthdr->framecnt) > framenum) {
  330. printf("Newer Fragment !!! (got: %d, exp: %d) Dropping it\n", ntohl(pkthdr->framecnt), framenum);
  331. continue;
  332. }
  333. #endif
  334. // copy the payload into the packet buffer...
  335. if ((ntohl(pkthdr->fragment_nr) < fragment_number) && (ntohl(pkthdr->fragment_nr) >= 0)) {
  336. if ((ntohl(pkthdr->fragment_nr) * fragment_payload_size + rcv_len - sizeof(jacknet_packet_header)) <= (pkt_size - sizeof(jacknet_packet_header))) {
  337. memcpy(packet_bufX + (ntohl(pkthdr->fragment_nr) * fragment_payload_size), dataX, rcv_len - sizeof(jacknet_packet_header));
  338. rx_frag_count++;
  339. } else {
  340. printf("too long packet received...");
  341. }
  342. }
  343. }
  344. }
  345. return pkt_size;
  346. }
  347. #endif
  348. #if 0
  349. int netjack_recv(int sockfd, char *packet_buf, int pkt_size, int flags, int mtu)
  350. {
  351. char *rx_packet;
  352. char *dataX;
  353. int i;
  354. int fragment_payload_size;
  355. char *fragment_array;
  356. int fragment_number;
  357. int rx_frag_count, framenum;
  358. jacknet_packet_header *pkthdr;
  359. // Now loop and send all
  360. char *packet_bufX;
  361. // wait for fragment_nr == 0
  362. int rcv_len;
  363. rx_packet = alloca(mtu);
  364. dataX = rx_packet + sizeof(jacknet_packet_header);
  365. fragment_payload_size = mtu - sizeof(jacknet_packet_header);
  366. pkthdr = (jacknet_packet_header *)rx_packet;
  367. packet_bufX = packet_buf + sizeof(jacknet_packet_header);
  368. fragment_number = (pkt_size - sizeof(jacknet_packet_header) - 1) / fragment_payload_size + 1;
  369. fragment_array = alloca(fragment_number);
  370. for (i = 0; i < fragment_number; i++)
  371. fragment_array[i] = 0;
  372. if (pkt_size <= mtu) {
  373. return recv(sockfd, packet_buf, pkt_size, flags);
  374. } else {
  375. rx_again:
  376. rcv_len = recv(sockfd, rx_packet, mtu, flags);
  377. if (rcv_len < 0)
  378. return rcv_len;
  379. if (rcv_len >= sizeof(jacknet_packet_header)) {
  380. if ((ntohl(pkthdr->fragment_nr)) != 0)
  381. goto rx_again;
  382. } else {
  383. goto rx_again;
  384. }
  385. // ok... we have read a fragement 0;
  386. // copy the data into the packet buffer...
  387. memcpy(packet_buf, rx_packet, mtu);
  388. rx_frag_count = 1;
  389. framenum = ntohl(pkthdr->framecnt);
  390. fragment_array[0] = 1;
  391. while (rx_frag_count < fragment_number) {
  392. rcv_len = recv(sockfd, rx_packet, mtu, flags);
  393. if (rcv_len < 0)
  394. return -1;
  395. ///////////////////
  396. if (ntohl(pkthdr->framecnt) < framenum) {
  397. //printf("Out of Order Framecnt: i abort on this packet\n");
  398. printf("Old Fragment !!! (got: %d, exp: %d)\n", ntohl(pkthdr->framecnt), framenum);
  399. continue;
  400. }
  401. #if 0
  402. if (ntohl(pkthdr->framecnt) > framenum) {
  403. printf("Newer Fragment !!! (got: %d, exp: %d) Switching to new Packet.\n", ntohl(pkthdr->framecnt), framenum);
  404. // Copy the new Packetheader up Front
  405. memcpy(packet_buf, rx_packet, sizeof(jacknet_packet_header));
  406. rx_frag_count = 0;
  407. framenum = ntohl(pkthdr->framecnt);
  408. }
  409. #endif
  410. if (ntohl(pkthdr->framecnt) > framenum) {
  411. printf("Newer Fragment !!! (got: %d, exp: %d) Dropping it\n", ntohl(pkthdr->framecnt), framenum);
  412. continue;
  413. }
  414. /////////////////////////////////
  415. //
  416. // copy the payload into the packet buffer...
  417. if ((ntohl(pkthdr->fragment_nr) < fragment_number) && (ntohl(pkthdr->fragment_nr) >= 0)) {
  418. if ((ntohl(pkthdr->fragment_nr) * fragment_payload_size + rcv_len - sizeof(jacknet_packet_header)) <= (pkt_size - sizeof(jacknet_packet_header))) {
  419. memcpy(packet_bufX + (ntohl(pkthdr->fragment_nr) * fragment_payload_size), dataX, rcv_len - sizeof(jacknet_packet_header));
  420. rx_frag_count++;
  421. } else {
  422. printf("too long packet received...");
  423. }
  424. }
  425. }
  426. }
  427. return pkt_size;
  428. }
  429. #endif
  430. void netjack_sendto(int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
  431. {
  432. int frag_cnt = 0;
  433. char *tx_packet, *dataX;
  434. jacknet_packet_header *pkthdr;
  435. tx_packet = alloca(mtu + 10);
  436. dataX = tx_packet + sizeof(jacknet_packet_header);
  437. pkthdr = (jacknet_packet_header *)tx_packet;
  438. int fragment_payload_size = mtu - sizeof(jacknet_packet_header);
  439. if (pkt_size <= mtu) {
  440. sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  441. } else {
  442. // Copy the packet header to the tx pack first.
  443. memcpy(tx_packet, packet_buf, sizeof(jacknet_packet_header));
  444. // Now loop and send all
  445. char *packet_bufX = packet_buf + sizeof(jacknet_packet_header);
  446. while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size)) {
  447. pkthdr->fragment_nr = htonl(frag_cnt++);
  448. memcpy(dataX, packet_bufX, fragment_payload_size);
  449. sendto(sockfd, tx_packet, mtu, flags, addr, addr_size);
  450. packet_bufX += fragment_payload_size;
  451. }
  452. int last_payload_size = packet_buf + pkt_size - packet_bufX;
  453. memcpy(dataX, packet_bufX, last_payload_size);
  454. pkthdr->fragment_nr = htonl(frag_cnt);
  455. //printf("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
  456. // sendto(last_pack_size);
  457. sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
  458. }
  459. }
  460. // render functions for float
  461. void 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)
  462. {
  463. channel_t chn = 0;
  464. JSList *node = capture_ports;
  465. JSList *src_node = capture_srcs;
  466. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  467. while (node != NULL) {
  468. int i;
  469. int_float_t val;
  470. SRC_DATA src;
  471. jack_port_t *port = (jack_port_t *) node->data;
  472. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  473. if (net_period_down != nframes) {
  474. SRC_STATE *src_state = src_node->data;
  475. for (i = 0; i < net_period_down; i++) {
  476. packet_bufX[i] = ntohl(packet_bufX[i]);
  477. }
  478. src.data_in = (float *)packet_bufX;
  479. src.input_frames = net_period_down;
  480. src.data_out = buf;
  481. src.output_frames = nframes;
  482. src.src_ratio = (float) nframes / (float) net_period_down;
  483. src.end_of_input = 0;
  484. src_set_ratio(src_state, src.src_ratio);
  485. src_process(src_state, &src);
  486. src_node = jack_slist_next (src_node);
  487. } else {
  488. for (i = 0; i < net_period_down; i++) {
  489. val.i = packet_bufX[i];
  490. val.i = ntohl(val.i);
  491. buf[i] = val.f;
  492. }
  493. }
  494. packet_bufX = (packet_bufX + net_period_down);
  495. node = jack_slist_next (node);
  496. chn++;
  497. }
  498. }
  499. void 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)
  500. {
  501. channel_t chn = 0;
  502. JSList *node = playback_ports;
  503. JSList *src_node = playback_srcs;
  504. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  505. while (node != NULL) {
  506. SRC_DATA src;
  507. int i;
  508. int_float_t val;
  509. jack_port_t *port = (jack_port_t *) node->data;
  510. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  511. if (net_period_up != nframes) {
  512. SRC_STATE *src_state = src_node->data;
  513. src.data_in = buf;
  514. src.input_frames = nframes;
  515. src.data_out = (float *) packet_bufX;
  516. src.output_frames = net_period_up;
  517. src.src_ratio = (float) net_period_up / (float) nframes;
  518. src.end_of_input = 0;
  519. src_set_ratio(src_state, src.src_ratio);
  520. src_process(src_state, &src);
  521. for (i = 0; i < net_period_up; i++) {
  522. packet_bufX[i] = htonl(packet_bufX[i]);
  523. }
  524. src_node = jack_slist_next (src_node);
  525. } else {
  526. for (i = 0; i < net_period_up; i++) {
  527. val.f = buf[i];
  528. val.i = htonl(val.i);
  529. packet_bufX[i] = val.i;
  530. }
  531. }
  532. packet_bufX = (packet_bufX + net_period_up);
  533. node = jack_slist_next (node);
  534. chn++;
  535. }
  536. }
  537. // render functions for 16bit
  538. void 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)
  539. {
  540. channel_t chn = 0;
  541. JSList *node = capture_ports;
  542. JSList *src_node = capture_srcs;
  543. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  544. while (node != NULL) {
  545. int i;
  546. //uint32_t val;
  547. SRC_DATA src;
  548. jack_port_t *port = (jack_port_t *) node->data;
  549. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  550. float *floatbuf = alloca(sizeof(float) * net_period_down);
  551. if (net_period_down != nframes) {
  552. SRC_STATE *src_state = src_node->data;
  553. for (i = 0; i < net_period_down; i++) {
  554. floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
  555. }
  556. src.data_in = floatbuf;
  557. src.input_frames = net_period_down;
  558. src.data_out = buf;
  559. src.output_frames = nframes;
  560. src.src_ratio = (float) nframes / (float) net_period_down;
  561. src.end_of_input = 0;
  562. src_set_ratio(src_state, src.src_ratio);
  563. src_process(src_state, &src);
  564. src_node = jack_slist_next (src_node);
  565. } else {
  566. for (i = 0; i < net_period_down; i++) {
  567. buf[i] = ((float) ntohs(packet_bufX[i])) / 32768.0 - 1.0;
  568. }
  569. }
  570. packet_bufX = (packet_bufX + net_period_down);
  571. node = jack_slist_next (node);
  572. chn++;
  573. }
  574. }
  575. void 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)
  576. {
  577. channel_t chn = 0;
  578. JSList *node = playback_ports;
  579. JSList *src_node = playback_srcs;
  580. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  581. while (node != NULL) {
  582. SRC_DATA src;
  583. int i;
  584. jack_port_t *port = (jack_port_t *) node->data;
  585. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  586. if (net_period_up != nframes) {
  587. SRC_STATE *src_state = src_node->data;
  588. float *floatbuf = alloca(sizeof(float) * net_period_up);
  589. src.data_in = buf;
  590. src.input_frames = nframes;
  591. src.data_out = floatbuf;
  592. src.output_frames = net_period_up;
  593. src.src_ratio = (float) net_period_up / (float) nframes;
  594. src.end_of_input = 0;
  595. src_set_ratio(src_state, src.src_ratio);
  596. src_process(src_state, &src);
  597. for (i = 0; i < net_period_up; i++) {
  598. packet_bufX[i] = htons((floatbuf[i] + 1.0) * 32767.0);
  599. }
  600. src_node = jack_slist_next (src_node);
  601. } else {
  602. for (i = 0; i < net_period_up; i++) {
  603. packet_bufX[i] = htons((buf[i] + 1.0) * 32767.0);
  604. }
  605. }
  606. packet_bufX = (packet_bufX + net_period_up);
  607. node = jack_slist_next (node);
  608. chn++;
  609. }
  610. }
  611. // render functions for 8bit
  612. void 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)
  613. {
  614. channel_t chn = 0;
  615. JSList *node = capture_ports;
  616. JSList *src_node = capture_srcs;
  617. int8_t *packet_bufX = (int8_t *)packet_payload;
  618. while (node != NULL) {
  619. int i;
  620. //uint32_t val;
  621. SRC_DATA src;
  622. jack_port_t *port = (jack_port_t *) node->data;
  623. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  624. float *floatbuf = alloca(sizeof(float) * net_period_down);
  625. if (net_period_down != nframes) {
  626. SRC_STATE *src_state = src_node->data;
  627. for (i = 0; i < net_period_down; i++) {
  628. floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
  629. }
  630. src.data_in = floatbuf;
  631. src.input_frames = net_period_down;
  632. src.data_out = buf;
  633. src.output_frames = nframes;
  634. src.src_ratio = (float) nframes / (float) net_period_down;
  635. src.end_of_input = 0;
  636. src_set_ratio(src_state, src.src_ratio);
  637. src_process(src_state, &src);
  638. src_node = jack_slist_next (src_node);
  639. } else {
  640. for (i = 0; i < net_period_down; i++) {
  641. buf[i] = ((float) packet_bufX[i]) / 127.0;
  642. }
  643. }
  644. packet_bufX = (packet_bufX + net_period_down);
  645. node = jack_slist_next (node);
  646. chn++;
  647. }
  648. }
  649. void 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)
  650. {
  651. channel_t chn = 0;
  652. JSList *node = playback_ports;
  653. JSList *src_node = playback_srcs;
  654. int8_t *packet_bufX = (int8_t *)packet_payload;
  655. while (node != NULL) {
  656. SRC_DATA src;
  657. int i;
  658. jack_port_t *port = (jack_port_t *) node->data;
  659. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  660. if (net_period_up != nframes) {
  661. SRC_STATE *src_state = src_node->data;
  662. float *floatbuf = alloca(sizeof(float) * net_period_up);
  663. src.data_in = buf;
  664. src.input_frames = nframes;
  665. src.data_out = floatbuf;
  666. src.output_frames = net_period_up;
  667. src.src_ratio = (float) net_period_up / (float) nframes;
  668. src.end_of_input = 0;
  669. src_set_ratio(src_state, src.src_ratio);
  670. src_process(src_state, &src);
  671. for (i = 0; i < net_period_up; i++) {
  672. packet_bufX[i] = floatbuf[i] * 127.0;
  673. }
  674. src_node = jack_slist_next (src_node);
  675. } else {
  676. for (i = 0; i < net_period_up; i++) {
  677. packet_bufX[i] = buf[i] * 127.0;
  678. }
  679. }
  680. packet_bufX = (packet_bufX + net_period_up);
  681. node = jack_slist_next (node);
  682. chn++;
  683. }
  684. }
  685. // wrapper functions with bitdepth argument...
  686. void 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)
  687. {
  688. if (bitdepth == 8)
  689. render_payload_to_jack_ports_8bit(packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  690. else if (bitdepth == 16)
  691. render_payload_to_jack_ports_16bit(packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  692. else
  693. render_payload_to_jack_ports_float(packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  694. }
  695. void 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)
  696. {
  697. if (bitdepth == 8)
  698. render_jack_ports_to_payload_8bit(playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  699. else if (bitdepth == 16)
  700. render_jack_ports_to_payload_16bit(playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  701. else
  702. render_jack_ports_to_payload_float(playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  703. }