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.

828 lines
31KB

  1. /* -*- mode: c; c-file-style: "linux"; -*- */
  2. /*
  3. NetJack Abstraction.
  4. Copyright (C) 2018 Karl Linden <karl.j.linden@gmail.com>
  5. Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  6. Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  7. Copyright (C) 2003 Robert Ham <rah@bash.sh>
  8. Copyright (C) 2001 Paul Davis
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. $Id: net_driver.c,v 1.17 2006/04/16 20:16:10 torbenh Exp $
  21. */
  22. #include <alloca.h>
  23. #include <math.h>
  24. #include <stdio.h>
  25. #include <memory.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <stdarg.h>
  30. #include <jack/types.h>
  31. #include "jack/jslist.h"
  32. #include <sys/types.h>
  33. #ifdef WIN32
  34. #include <winsock2.h>
  35. #include <malloc.h>
  36. #define socklen_t int
  37. #else
  38. #include <sys/socket.h>
  39. #include <netinet/in.h>
  40. #endif
  41. #if defined(HAVE_CONFIG_H)
  42. #include "config.h"
  43. #endif
  44. #if HAVE_SAMPLERATE
  45. #include <samplerate.h>
  46. #endif
  47. #include "netjack.h"
  48. #include "netjack_packet.h"
  49. #include "JackError.h"
  50. #define MIN(x,y) ((x)<(y) ? (x) : (y))
  51. static int sync_state = 1;
  52. static jack_transport_state_t last_transport_state;
  53. static int
  54. net_driver_sync_cb(jack_transport_state_t state, jack_position_t *pos, void *data)
  55. {
  56. int retval = sync_state;
  57. if (state == JackTransportStarting && last_transport_state != JackTransportStarting) {
  58. retval = 0;
  59. }
  60. // if (state == JackTransportStarting)
  61. // jack_info("Starting sync_state = %d", sync_state);
  62. last_transport_state = state;
  63. return retval;
  64. }
  65. int netjack_wait( netjack_driver_state_t *netj )
  66. {
  67. int we_have_the_expected_frame = 0;
  68. jack_nframes_t next_frame_avail;
  69. jack_time_t packet_recv_time_stamp;
  70. jacknet_packet_header *pkthdr;
  71. if( !netj->next_deadline_valid ) {
  72. netj->next_deadline = jack_get_time() + netj->period_usecs;
  73. netj->next_deadline_valid = 1;
  74. }
  75. // Increment expected frame here.
  76. if( netj->expected_framecnt_valid ) {
  77. netj->expected_framecnt += 1;
  78. } else {
  79. // starting up.... lets look into the packetcache, and fetch the highest packet.
  80. packet_cache_drain_socket( netj->packcache, netj->sockfd );
  81. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail ) ) {
  82. netj->expected_framecnt = next_frame_avail;
  83. netj->expected_framecnt_valid = 1;
  84. } else {
  85. // no packets there... start normally.
  86. netj->expected_framecnt = 0;
  87. netj->expected_framecnt_valid = 1;
  88. }
  89. }
  90. //jack_log( "expect %d", netj->expected_framecnt );
  91. // Now check if required packet is already in the cache.
  92. // then poll (have deadline calculated)
  93. // then drain socket, rinse and repeat.
  94. while(1) {
  95. if( packet_cache_get_next_available_framecnt( netj->packcache, netj->expected_framecnt, &next_frame_avail) ) {
  96. if( next_frame_avail == netj->expected_framecnt ) {
  97. we_have_the_expected_frame = 1;
  98. if( !netj->always_deadline )
  99. break;
  100. }
  101. }
  102. if( ! netjack_poll_deadline( netj->sockfd, netj->next_deadline ) ) {
  103. break;
  104. }
  105. packet_cache_drain_socket( netj->packcache, netj->sockfd );
  106. }
  107. // check if we know who to send our packets too.
  108. if (!netj->srcaddress_valid)
  109. if( netj->packcache->master_address_valid ) {
  110. memcpy (&(netj->syncsource_address), &(netj->packcache->master_address), sizeof( struct sockaddr_in ) );
  111. netj->srcaddress_valid = 1;
  112. }
  113. // XXX: switching mode unconditionally is stupid.
  114. // if we were running free perhaps we like to behave differently
  115. // ie. fastforward one packet etc.
  116. // well... this is the first packet we see. hmm.... dunno ;S
  117. // it works... so...
  118. netj->running_free = 0;
  119. //if( !we_have_the_expected_frame )
  120. // jack_error( "netxrun... %d", netj->expected_framecnt );
  121. if( we_have_the_expected_frame ) {
  122. jack_time_t now = jack_get_time();
  123. if( now < netj->next_deadline )
  124. netj->time_to_deadline = netj->next_deadline - now;
  125. else
  126. netj->time_to_deadline = 0;
  127. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) & (netj->rx_buf), netj->rx_bufsize , &packet_recv_time_stamp);
  128. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  129. packet_header_ntoh(pkthdr);
  130. netj->deadline_goodness = (int)pkthdr->sync_state;
  131. netj->packet_data_valid = 1;
  132. int want_deadline;
  133. if( netj->jitter_val != 0 )
  134. want_deadline = netj->jitter_val;
  135. else if( netj->latency < 4 )
  136. want_deadline = -netj->period_usecs / 2;
  137. else
  138. want_deadline = (netj->period_usecs / 4 + 10 * (int)netj->period_usecs * netj->latency / 100);
  139. if( netj->deadline_goodness != MASTER_FREEWHEELS ) {
  140. if( netj->deadline_goodness < want_deadline ) {
  141. netj->next_deadline -= netj->period_usecs / 100;
  142. //jack_log( "goodness: %d, Adjust deadline: --- %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  143. }
  144. if( netj->deadline_goodness > want_deadline ) {
  145. netj->next_deadline += netj->period_usecs / 100;
  146. //jack_log( "goodness: %d, Adjust deadline: +++ %d\n", netj->deadline_goodness, (int) netj->period_usecs*netj->latency/100 );
  147. }
  148. }
  149. // if( netj->next_deadline < (netj->period_usecs*70/100) ) {
  150. // jack_error( "master is forcing deadline_offset to below 70%% of period_usecs... increase latency setting on master" );
  151. // netj->deadline_offset = (netj->period_usecs*90/100);
  152. // }
  153. netj->next_deadline += netj->period_usecs;
  154. } else {
  155. netj->time_to_deadline = 0;
  156. netj->next_deadline += netj->period_usecs;
  157. // bah... the packet is not there.
  158. // either
  159. // - it got lost.
  160. // - its late
  161. // - sync source is not sending anymore.
  162. // lets check if we have the next packets, we will just run a cycle without data.
  163. // in that case.
  164. if( packet_cache_get_next_available_framecnt( netj->packcache, netj->expected_framecnt, &next_frame_avail) ) {
  165. jack_nframes_t offset = next_frame_avail - netj->expected_framecnt;
  166. //XXX: hmm... i need to remember why resync_threshold wasn't right.
  167. //if( offset < netj->resync_threshold )
  168. if( offset < 10 ) {
  169. // ok. don't do nothing. we will run without data.
  170. // this seems to be one or 2 lost packets.
  171. //
  172. // this can also be reordered packet jitter.
  173. // (maybe this is not happening in real live)
  174. // but it happens in netem.
  175. netj->packet_data_valid = 0;
  176. // I also found this happening, when the packet queue, is too full.
  177. // but wtf ? use a smaller latency. this link can handle that ;S
  178. if( packet_cache_get_fill( netj->packcache, netj->expected_framecnt ) > 80.0 )
  179. netj->next_deadline -= netj->period_usecs / 2;
  180. } else {
  181. // the diff is too high. but we have a packet in the future.
  182. // lets resync.
  183. netj->expected_framecnt = next_frame_avail;
  184. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) & (netj->rx_buf), netj->rx_bufsize, NULL );
  185. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  186. packet_header_ntoh(pkthdr);
  187. //netj->deadline_goodness = 0;
  188. netj->deadline_goodness = (int)pkthdr->sync_state - (int)netj->period_usecs * offset;
  189. netj->next_deadline_valid = 0;
  190. netj->packet_data_valid = 1;
  191. }
  192. } else {
  193. // no packets in buffer.
  194. netj->packet_data_valid = 0;
  195. //printf( "frame %d No Packet in queue. num_lost_packets = %d \n", netj->expected_framecnt, netj->num_lost_packets );
  196. if( netj->num_lost_packets < 5 ) {
  197. // ok. No Packet in queue. The packet was either lost,
  198. // or we are running too fast.
  199. //
  200. // Adjusting the deadline unconditionally resulted in
  201. // too many xruns on master.
  202. // But we need to adjust for the case we are running too fast.
  203. // So lets check if the last packet is there now.
  204. //
  205. // It would not be in the queue anymore, if it had been
  206. // retrieved. This might break for redundancy, but
  207. // i will make the packet cache drop redundant packets,
  208. // that have already been retrieved.
  209. //
  210. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail) ) {
  211. if( next_frame_avail == (netj->expected_framecnt - 1) ) {
  212. // Ok. the last packet is there now.
  213. // and it had not been retrieved.
  214. //
  215. // TODO: We are still dropping 2 packets.
  216. // perhaps we can adjust the deadline
  217. // when (num_packets lost == 0)
  218. // This might still be too much.
  219. netj->next_deadline += netj->period_usecs;
  220. }
  221. }
  222. } else if( (netj->num_lost_packets <= 100) ) {
  223. // lets try adjusting the deadline harder, for some packets, we might have just ran 2 fast.
  224. netj->next_deadline += netj->period_usecs * netj->latency / 8;
  225. } else {
  226. // But now we can check for any new frame available.
  227. //
  228. if( packet_cache_get_highest_available_framecnt( netj->packcache, &next_frame_avail) ) {
  229. netj->expected_framecnt = next_frame_avail;
  230. packet_cache_retreive_packet_pointer( netj->packcache, netj->expected_framecnt, (char **) & (netj->rx_buf), netj->rx_bufsize, NULL );
  231. pkthdr = (jacknet_packet_header *) netj->rx_buf;
  232. packet_header_ntoh(pkthdr);
  233. netj->deadline_goodness = pkthdr->sync_state;
  234. netj->next_deadline_valid = 0;
  235. netj->packet_data_valid = 1;
  236. netj->running_free = 0;
  237. jack_info( "resync after freerun... %d", netj->expected_framecnt );
  238. } else {
  239. if( netj->num_lost_packets == 101 ) {
  240. jack_info( "master seems gone... entering freerun mode", netj->expected_framecnt );
  241. }
  242. netj->running_free = 1;
  243. // when we really don't see packets.
  244. // reset source address. and open possibility for new master.
  245. // maybe dsl reconnect. Also restart of netsource without fix
  246. // reply address changes port.
  247. if (netj->num_lost_packets > 200 ) {
  248. netj->srcaddress_valid = 0;
  249. packet_cache_reset_master_address( netj->packcache );
  250. }
  251. }
  252. }
  253. }
  254. }
  255. int retval = 0;
  256. if( !netj->packet_data_valid ) {
  257. netj->num_lost_packets += 1;
  258. if( netj->num_lost_packets == 1 )
  259. retval = netj->period_usecs;
  260. } else {
  261. if( (netj->num_lost_packets > 1) && !netj->running_free )
  262. retval = (netj->num_lost_packets - 1) * netj->period_usecs;
  263. netj->num_lost_packets = 0;
  264. }
  265. return retval;
  266. }
  267. void netjack_send_silence( netjack_driver_state_t *netj, int syncstate )
  268. {
  269. int tx_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up + sizeof(jacknet_packet_header);
  270. unsigned int *packet_buf, *packet_bufX;
  271. packet_buf = alloca( tx_size);
  272. jacknet_packet_header *tx_pkthdr = (jacknet_packet_header *)packet_buf;
  273. jacknet_packet_header *rx_pkthdr = (jacknet_packet_header *)netj->rx_buf;
  274. //framecnt = rx_pkthdr->framecnt;
  275. netj->reply_port = rx_pkthdr->reply_port;
  276. // offset packet_bufX by the packetheader.
  277. packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
  278. tx_pkthdr->sync_state = syncstate;
  279. tx_pkthdr->framecnt = netj->expected_framecnt;
  280. // memset 0 the payload.
  281. int payload_size = get_sample_size(netj->bitdepth) * netj->playback_channels * netj->net_period_up;
  282. memset(packet_bufX, 0, payload_size);
  283. packet_header_hton(tx_pkthdr);
  284. if (netj->srcaddress_valid) {
  285. int r;
  286. if (netj->reply_port)
  287. netj->syncsource_address.sin_port = htons(netj->reply_port);
  288. for( r = 0; r < netj->redundancy; r++ )
  289. netjack_sendto(netj->outsockfd, (char *)packet_buf, tx_size,
  290. 0, (struct sockaddr*) & (netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
  291. }
  292. }
  293. void netjack_attach( netjack_driver_state_t *netj )
  294. {
  295. //puts ("net_driver_attach");
  296. jack_port_t * port;
  297. char buf[32];
  298. unsigned int chn;
  299. int port_flags;
  300. if( netj->bitdepth == CELT_MODE ) {
  301. #if HAVE_CELT
  302. #if HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  303. celt_int32 lookahead;
  304. netj->celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  305. #else
  306. celt_int32_t lookahead;
  307. netj->celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  308. #endif
  309. celt_mode_info( netj->celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
  310. netj->codec_latency = 2 * lookahead;
  311. #endif
  312. }
  313. if( netj->bitdepth == OPUS_MODE ) {
  314. #if HAVE_OPUS
  315. netj->opus_mode = opus_custom_mode_create(netj->sample_rate, netj->period_size, NULL);
  316. #endif
  317. }
  318. if (netj->handle_transport_sync)
  319. jack_set_sync_callback(netj->client, (JackSyncCallback) net_driver_sync_cb, NULL);
  320. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  321. for (chn = 0; chn < netj->capture_channels_audio; chn++) {
  322. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  323. port = jack_port_register (netj->client, buf,
  324. JACK_DEFAULT_AUDIO_TYPE,
  325. port_flags, 0);
  326. if (!port) {
  327. jack_error ("NET: cannot register port for %s", buf);
  328. break;
  329. }
  330. netj->capture_ports =
  331. jack_slist_append (netj->capture_ports, port);
  332. if( netj->bitdepth == CELT_MODE ) {
  333. #if HAVE_CELT
  334. #if HAVE_CELT_API_0_11
  335. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create_custom( netj->celt_mode, 1, NULL ) );
  336. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  337. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode, 1, NULL ) );
  338. #else
  339. netj->capture_srcs = jack_slist_append(netj->capture_srcs, celt_decoder_create( netj->celt_mode ) );
  340. #endif
  341. #endif
  342. } else if( netj->bitdepth == OPUS_MODE ) {
  343. #if HAVE_OPUS
  344. OpusCustomDecoder *decoder = opus_custom_decoder_create( netj->opus_mode, 1, NULL );
  345. netj->capture_srcs = jack_slist_append(netj->capture_srcs, decoder );
  346. #endif
  347. } else {
  348. #if HAVE_SAMPLERATE
  349. netj->capture_srcs = jack_slist_append(netj->capture_srcs, src_new(SRC_LINEAR, 1, NULL));
  350. #endif
  351. }
  352. }
  353. for (chn = netj->capture_channels_audio; chn < netj->capture_channels; chn++) {
  354. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn + 1);
  355. port = jack_port_register (netj->client, buf,
  356. JACK_DEFAULT_MIDI_TYPE,
  357. port_flags, 0);
  358. if (!port) {
  359. jack_error ("NET: cannot register port for %s", buf);
  360. break;
  361. }
  362. netj->capture_ports =
  363. jack_slist_append (netj->capture_ports, port);
  364. }
  365. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  366. for (chn = 0; chn < netj->playback_channels_audio; chn++) {
  367. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  368. port = jack_port_register (netj->client, buf,
  369. JACK_DEFAULT_AUDIO_TYPE,
  370. port_flags, 0);
  371. if (!port) {
  372. jack_error ("NET: cannot register port for %s", buf);
  373. break;
  374. }
  375. netj->playback_ports =
  376. jack_slist_append (netj->playback_ports, port);
  377. if( netj->bitdepth == CELT_MODE ) {
  378. #if HAVE_CELT
  379. #if HAVE_CELT_API_0_11
  380. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  381. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_decoder_create_custom( celt_mode, 1, NULL ) );
  382. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  383. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, netj->period_size, NULL );
  384. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
  385. #else
  386. CELTMode *celt_mode = celt_mode_create( netj->sample_rate, 1, netj->period_size, NULL );
  387. netj->playback_srcs = jack_slist_append(netj->playback_srcs, celt_encoder_create( celt_mode ) );
  388. #endif
  389. #endif
  390. } else if( netj->bitdepth == OPUS_MODE ) {
  391. #if HAVE_OPUS
  392. const int kbps = netj->resample_factor;
  393. jack_log( "OPUS %dkbps\n", kbps);
  394. OpusCustomMode *opus_mode = opus_custom_mode_create( netj->sample_rate, netj->period_size, NULL ); // XXX free me in the end
  395. OpusCustomEncoder *oe = opus_custom_encoder_create( opus_mode, 1, NULL );
  396. opus_custom_encoder_ctl(oe, OPUS_SET_BITRATE(kbps*1024)); // bits per second
  397. opus_custom_encoder_ctl(oe, OPUS_SET_COMPLEXITY(10));
  398. opus_custom_encoder_ctl(oe, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));
  399. opus_custom_encoder_ctl(oe, OPUS_SET_SIGNAL(OPUS_APPLICATION_RESTRICTED_LOWDELAY));
  400. netj->playback_srcs = jack_slist_append(netj->playback_srcs, oe );
  401. #endif
  402. } else {
  403. #if HAVE_SAMPLERATE
  404. netj->playback_srcs = jack_slist_append(netj->playback_srcs, src_new(SRC_LINEAR, 1, NULL));
  405. #endif
  406. }
  407. }
  408. for (chn = netj->playback_channels_audio; chn < netj->playback_channels; chn++) {
  409. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn + 1);
  410. port = jack_port_register (netj->client, buf,
  411. JACK_DEFAULT_MIDI_TYPE,
  412. port_flags, 0);
  413. if (!port) {
  414. jack_error ("NET: cannot register port for %s", buf);
  415. break;
  416. }
  417. netj->playback_ports =
  418. jack_slist_append (netj->playback_ports, port);
  419. }
  420. jack_activate (netj->client);
  421. }
  422. void netjack_detach( netjack_driver_state_t *netj )
  423. {
  424. JSList * node;
  425. for (node = netj->capture_ports; node; node = jack_slist_next (node))
  426. jack_port_unregister (netj->client,
  427. ((jack_port_t *) node->data));
  428. jack_slist_free (netj->capture_ports);
  429. netj->capture_ports = NULL;
  430. for (node = netj->capture_srcs; node; node = jack_slist_next (node)) {
  431. #if HAVE_CELT
  432. if( netj->bitdepth == CELT_MODE ) {
  433. CELTDecoder * decoder = node->data;
  434. celt_decoder_destroy(decoder);
  435. } else
  436. #endif
  437. #if HAVE_OPUS
  438. if ( netj->bitdepth == OPUS_MODE ) {
  439. OpusCustomDecoder * decoder = node->data;
  440. opus_custom_decoder_destroy(decoder);
  441. } else
  442. #endif
  443. {
  444. #if HAVE_SAMPLERATE
  445. SRC_STATE * src = node->data;
  446. src_delete(src);
  447. #endif
  448. }
  449. }
  450. jack_slist_free (netj->capture_srcs);
  451. netj->playback_srcs = NULL;
  452. for (node = netj->playback_ports; node; node = jack_slist_next (node))
  453. jack_port_unregister (netj->client,
  454. ((jack_port_t *) node->data));
  455. jack_slist_free (netj->playback_ports);
  456. netj->playback_ports = NULL;
  457. for (node = netj->playback_srcs; node; node = jack_slist_next (node)) {
  458. #if HAVE_CELT
  459. if( netj->bitdepth == CELT_MODE ) {
  460. CELTEncoder * encoder = node->data;
  461. celt_encoder_destroy(encoder);
  462. } else
  463. #endif
  464. #if HAVE_OPUS
  465. if ( netj->bitdepth == OPUS_MODE ) {
  466. OpusCustomEncoder * encoder = node->data;
  467. opus_custom_encoder_destroy(encoder);
  468. } else
  469. #endif
  470. {
  471. #if HAVE_SAMPLERATE
  472. SRC_STATE * src = node->data;
  473. src_delete(src);
  474. #endif
  475. }
  476. }
  477. jack_slist_free (netj->playback_srcs);
  478. netj->playback_srcs = NULL;
  479. #if HAVE_CELT
  480. if( netj->bitdepth == CELT_MODE )
  481. celt_mode_destroy(netj->celt_mode);
  482. #endif
  483. #if HAVE_OPUS
  484. if( netj->bitdepth == OPUS_MODE )
  485. opus_custom_mode_destroy(netj->opus_mode);
  486. #endif
  487. }
  488. netjack_driver_state_t *netjack_init (netjack_driver_state_t *netj,
  489. jack_client_t * client,
  490. const char *name,
  491. unsigned int capture_ports,
  492. unsigned int playback_ports,
  493. unsigned int capture_ports_midi,
  494. unsigned int playback_ports_midi,
  495. jack_nframes_t sample_rate,
  496. jack_nframes_t period_size,
  497. unsigned int listen_port,
  498. unsigned int transport_sync,
  499. unsigned int resample_factor,
  500. unsigned int resample_factor_up,
  501. unsigned int bitdepth,
  502. unsigned int use_autoconfig,
  503. unsigned int latency,
  504. unsigned int redundancy,
  505. int dont_htonl_floats,
  506. int always_deadline,
  507. int jitter_val )
  508. {
  509. // Fill in netj values.
  510. // might be subject to autoconfig...
  511. // so don't calculate anything with them...
  512. netj->sample_rate = sample_rate;
  513. netj->period_size = period_size;
  514. netj->dont_htonl_floats = dont_htonl_floats;
  515. netj->listen_port = listen_port;
  516. netj->capture_channels = capture_ports + capture_ports_midi;
  517. netj->capture_channels_audio = capture_ports;
  518. netj->capture_channels_midi = capture_ports_midi;
  519. netj->capture_ports = NULL;
  520. netj->playback_channels = playback_ports + playback_ports_midi;
  521. netj->playback_channels_audio = playback_ports;
  522. netj->playback_channels_midi = playback_ports_midi;
  523. netj->playback_ports = NULL;
  524. netj->codec_latency = 0;
  525. netj->handle_transport_sync = transport_sync;
  526. netj->mtu = 1400;
  527. netj->latency = latency;
  528. netj->redundancy = redundancy;
  529. netj->use_autoconfig = use_autoconfig;
  530. netj->always_deadline = always_deadline;
  531. netj->client = client;
  532. if ((bitdepth != 0) && (bitdepth != 8) && (bitdepth != 16) && (bitdepth != CELT_MODE) && (bitdepth != OPUS_MODE)) {
  533. jack_info ("Invalid bitdepth: %d (8, 16 or 0 for float) !!!", bitdepth);
  534. return NULL;
  535. }
  536. netj->bitdepth = bitdepth;
  537. if (resample_factor_up == 0) {
  538. resample_factor_up = resample_factor;
  539. }
  540. netj->resample_factor = resample_factor;
  541. netj->resample_factor_up = resample_factor_up;
  542. netj->jitter_val = jitter_val;
  543. netj->playback_srcs = NULL;
  544. netj->capture_srcs = NULL;
  545. return netj;
  546. }
  547. void netjack_release( netjack_driver_state_t *netj )
  548. {
  549. close( netj->sockfd );
  550. close( netj->outsockfd );
  551. packet_cache_free( netj->packcache );
  552. netj->packcache = NULL;
  553. }
  554. int
  555. netjack_startup( netjack_driver_state_t *netj )
  556. {
  557. int first_pack_len;
  558. struct sockaddr_in address;
  559. // Now open the socket, and wait for the first packet to arrive...
  560. netj->sockfd = socket (AF_INET, SOCK_DGRAM, 0);
  561. #ifdef WIN32
  562. if (netj->sockfd == INVALID_SOCKET)
  563. #else
  564. if (netj->sockfd == -1)
  565. #endif
  566. {
  567. jack_info ("socket error");
  568. return -1;
  569. }
  570. address.sin_family = AF_INET;
  571. address.sin_port = htons(netj->listen_port);
  572. address.sin_addr.s_addr = htonl(INADDR_ANY);
  573. if (bind (netj->sockfd, (struct sockaddr *) &address, sizeof (address)) < 0) {
  574. jack_info("bind error");
  575. return -1;
  576. }
  577. netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
  578. #ifdef WIN32
  579. if (netj->outsockfd == INVALID_SOCKET)
  580. #else
  581. if (netj->outsockfd == -1)
  582. #endif
  583. {
  584. jack_info ("socket error");
  585. return -1;
  586. }
  587. netj->srcaddress_valid = 0;
  588. if (netj->use_autoconfig) {
  589. jacknet_packet_header *first_packet = alloca (sizeof (jacknet_packet_header));
  590. #ifdef WIN32
  591. int address_size = sizeof( struct sockaddr_in );
  592. #else
  593. socklen_t address_size = sizeof (struct sockaddr_in);
  594. #endif
  595. //jack_info ("Waiting for an incoming packet !!!");
  596. //jack_info ("*** IMPORTANT *** Don't connect a client to jackd until the driver is attached to a clock source !!!");
  597. while(1) {
  598. if( ! netjack_poll( netj->sockfd, 1000 ) ) {
  599. jack_info ("Waiting aborted");
  600. return -1;
  601. }
  602. first_pack_len = recvfrom (netj->sockfd, (char *)first_packet, sizeof (jacknet_packet_header), 0, (struct sockaddr*) & netj->syncsource_address, &address_size);
  603. #ifdef WIN32
  604. if( first_pack_len == -1 ) {
  605. first_pack_len = sizeof(jacknet_packet_header);
  606. break;
  607. }
  608. #else
  609. if (first_pack_len == sizeof (jacknet_packet_header))
  610. break;
  611. #endif
  612. }
  613. netj->srcaddress_valid = 1;
  614. if (first_pack_len == sizeof (jacknet_packet_header)) {
  615. packet_header_ntoh (first_packet);
  616. jack_info ("AutoConfig Override !!!");
  617. if (netj->sample_rate != first_packet->sample_rate) {
  618. jack_info ("AutoConfig Override: Master JACK sample rate = %d", first_packet->sample_rate);
  619. netj->sample_rate = first_packet->sample_rate;
  620. }
  621. if (netj->period_size != first_packet->period_size) {
  622. jack_info ("AutoConfig Override: Master JACK period size is %d", first_packet->period_size);
  623. netj->period_size = first_packet->period_size;
  624. }
  625. if (netj->capture_channels_audio != first_packet->capture_channels_audio) {
  626. jack_info ("AutoConfig Override: capture_channels_audio = %d", first_packet->capture_channels_audio);
  627. netj->capture_channels_audio = first_packet->capture_channels_audio;
  628. }
  629. if (netj->capture_channels_midi != first_packet->capture_channels_midi) {
  630. jack_info ("AutoConfig Override: capture_channels_midi = %d", first_packet->capture_channels_midi);
  631. netj->capture_channels_midi = first_packet->capture_channels_midi;
  632. }
  633. if (netj->playback_channels_audio != first_packet->playback_channels_audio) {
  634. jack_info ("AutoConfig Override: playback_channels_audio = %d", first_packet->playback_channels_audio);
  635. netj->playback_channels_audio = first_packet->playback_channels_audio;
  636. }
  637. if (netj->playback_channels_midi != first_packet->playback_channels_midi) {
  638. jack_info ("AutoConfig Override: playback_channels_midi = %d", first_packet->playback_channels_midi);
  639. netj->playback_channels_midi = first_packet->playback_channels_midi;
  640. }
  641. netj->mtu = first_packet->mtu;
  642. jack_info ("MTU is set to %d bytes", first_packet->mtu);
  643. netj->latency = first_packet->latency;
  644. }
  645. }
  646. netj->capture_channels = netj->capture_channels_audio + netj->capture_channels_midi;
  647. netj->playback_channels = netj->playback_channels_audio + netj->playback_channels_midi;
  648. if( (netj->capture_channels * netj->period_size * netj->latency * 4) > 100000000 ) {
  649. jack_error( "autoconfig requests more than 100MB packet cache... bailing out" );
  650. exit(1);
  651. }
  652. if( netj->playback_channels > 1000 ) {
  653. jack_error( "autoconfig requests more than 1000 playback channels... bailing out" );
  654. exit(1);
  655. }
  656. if( netj->mtu < (2 * sizeof( jacknet_packet_header )) ) {
  657. jack_error( "bullshit mtu requested by autoconfig" );
  658. exit(1);
  659. }
  660. if( netj->sample_rate == 0 ) {
  661. jack_error( "sample_rate 0 requested by autoconfig" );
  662. exit(1);
  663. }
  664. // After possible Autoconfig: do all calculations...
  665. netj->period_usecs =
  666. (jack_time_t) floor ((((float) netj->period_size) / (float)netj->sample_rate)
  667. * 1000000.0f);
  668. if( netj->latency == 0 )
  669. netj->deadline_offset = 50 * netj->period_usecs;
  670. else
  671. netj->deadline_offset = netj->period_usecs + 10 * netj->latency * netj->period_usecs / 100;
  672. if( netj->bitdepth == CELT_MODE ) {
  673. // celt mode.
  674. // TODO: this is a hack. But i don't want to change the packet header.
  675. netj->resample_factor = (netj->resample_factor * netj->period_size * 1024 / netj->sample_rate / 8) & (~1);
  676. netj->resample_factor_up = (netj->resample_factor_up * netj->period_size * 1024 / netj->sample_rate / 8) & (~1);
  677. netj->net_period_down = netj->resample_factor;
  678. netj->net_period_up = netj->resample_factor_up;
  679. } else if( netj->bitdepth == OPUS_MODE ) {
  680. // Opus mode.
  681. // TODO: this is a hack. But i don't want to change the packet header, either
  682. netj->net_period_down = (netj->resample_factor * netj->period_size * 1024 / netj->sample_rate / 8) & (~1);
  683. netj->net_period_up = (netj->resample_factor_up * netj->period_size * 1024 / netj->sample_rate / 8) & (~1);
  684. } else {
  685. netj->net_period_down = (float) netj->period_size / (float) netj->resample_factor;
  686. netj->net_period_up = (float) netj->period_size / (float) netj->resample_factor_up;
  687. }
  688. netj->rx_bufsize = sizeof (jacknet_packet_header) + netj->net_period_down * netj->capture_channels * get_sample_size (netj->bitdepth);
  689. netj->packcache = packet_cache_new (netj->latency + 50, netj->rx_bufsize, netj->mtu);
  690. netj->expected_framecnt_valid = 0;
  691. netj->num_lost_packets = 0;
  692. netj->next_deadline_valid = 0;
  693. netj->deadline_goodness = 0;
  694. netj->time_to_deadline = 0;
  695. // Special handling for latency=0
  696. if( netj->latency == 0 )
  697. netj->resync_threshold = 0;
  698. else
  699. netj->resync_threshold = MIN( 15, netj->latency - 1 );
  700. netj->running_free = 0;
  701. return 0;
  702. }