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.

826 lines
31KB

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