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.

1131 lines
43KB

  1. /*
  2. Copyright (C) 2018 Karl Linden <karl.j.linden@gmail.com>
  3. Copyright (C) 2008-2011 Torben Horn
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <alloca.h>
  17. #include "JackNetOneDriver.h"
  18. #include "JackEngineControl.h"
  19. #include "JackLockedEngine.h"
  20. #include "JackGraphManager.h"
  21. #include "JackWaitThreadedDriver.h"
  22. #include "JackTools.h"
  23. #include "driver_interface.h"
  24. #include "netjack.h"
  25. #include "netjack_packet.h"
  26. #if HAVE_SAMPLERATE
  27. #include <samplerate.h>
  28. #endif
  29. #if HAVE_CELT
  30. #include <celt/celt.h>
  31. #endif
  32. #if HAVE_OPUS
  33. #include <opus/opus.h>
  34. #include <opus/opus_custom.h>
  35. #endif
  36. #define MIN(x,y) ((x)<(y) ? (x) : (y))
  37. using namespace std;
  38. namespace Jack
  39. {
  40. JackNetOneDriver::JackNetOneDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table,
  41. int port, int mtu, int capture_ports, int playback_ports, int midi_input_ports, int midi_output_ports,
  42. int sample_rate, int period_size, int resample_factor,
  43. const char* net_name, uint transport_sync, int bitdepth, int use_autoconfig,
  44. int latency, int redundancy, int dont_htonl_floats, int always_deadline, int jitter_val)
  45. : JackWaiterDriver(name, alias, engine, table)
  46. {
  47. jack_log("JackNetOneDriver::JackNetOneDriver port %d", port);
  48. #ifdef WIN32
  49. WSADATA wsa;
  50. WSAStartup(MAKEWORD(2, 0), &wsa);
  51. #endif
  52. netjack_init(& (this->netj),
  53. NULL, // client
  54. name,
  55. capture_ports,
  56. playback_ports,
  57. midi_input_ports,
  58. midi_output_ports,
  59. sample_rate,
  60. period_size,
  61. port,
  62. transport_sync,
  63. resample_factor,
  64. 0,
  65. bitdepth,
  66. use_autoconfig,
  67. latency,
  68. redundancy,
  69. dont_htonl_floats,
  70. always_deadline,
  71. jitter_val);
  72. }
  73. JackNetOneDriver::~JackNetOneDriver()
  74. {
  75. // No destructor yet.
  76. }
  77. //open, close, attach and detach------------------------------------------------------
  78. int JackNetOneDriver::Close()
  79. {
  80. // Generic audio driver close
  81. int res = JackWaiterDriver::Close();
  82. FreePorts();
  83. netjack_release(&netj);
  84. return res;
  85. }
  86. int JackNetOneDriver::Attach()
  87. {
  88. return 0;
  89. }
  90. int JackNetOneDriver::Detach()
  91. {
  92. return 0;
  93. }
  94. int JackNetOneDriver::AllocPorts()
  95. {
  96. jack_port_id_t port_index;
  97. char buf[64];
  98. unsigned int chn;
  99. //if (netj.handle_transport_sync)
  100. // jack_set_sync_callback(netj.client, (JackSyncCallback) net_driver_sync_cb, NULL);
  101. for (chn = 0; chn < netj.capture_channels_audio; chn++) {
  102. snprintf (buf, sizeof(buf) - 1, "system:capture_%u", chn + 1);
  103. if (fEngine->PortRegister(fClientControl.fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE,
  104. CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  105. jack_error("driver: cannot register port for %s", buf);
  106. return -1;
  107. }
  108. //port = fGraphManager->GetPort(port_index);
  109. netj.capture_ports = jack_slist_append (netj.capture_ports, (void *)(intptr_t)port_index);
  110. if (netj.bitdepth == CELT_MODE) {
  111. #if HAVE_CELT
  112. #if HAVE_CELT_API_0_11
  113. celt_int32 lookahead;
  114. CELTMode *celt_mode = celt_mode_create(netj.sample_rate, netj.period_size, NULL);
  115. netj.capture_srcs = jack_slist_append(netj.capture_srcs, celt_decoder_create_custom(celt_mode, 1, NULL));
  116. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  117. celt_int32 lookahead;
  118. CELTMode *celt_mode = celt_mode_create(netj.sample_rate, netj.period_size, NULL);
  119. netj.capture_srcs = jack_slist_append(netj.capture_srcs, celt_decoder_create(celt_mode, 1, NULL));
  120. #else
  121. celt_int32_t lookahead;
  122. CELTMode *celt_mode = celt_mode_create(netj.sample_rate, 1, netj.period_size, NULL);
  123. netj.capture_srcs = jack_slist_append(netj.capture_srcs, celt_decoder_create(celt_mode));
  124. #endif
  125. celt_mode_info(celt_mode, CELT_GET_LOOKAHEAD, &lookahead);
  126. netj.codec_latency = 2 * lookahead;
  127. #endif
  128. } else if (netj.bitdepth == OPUS_MODE) {
  129. #if HAVE_OPUS
  130. OpusCustomMode *opus_mode = opus_custom_mode_create(netj.sample_rate, netj.period_size, NULL); // XXX free me in the end
  131. OpusCustomDecoder *decoder = opus_custom_decoder_create( opus_mode, 1, NULL );
  132. netj.capture_srcs = jack_slist_append(netj.capture_srcs, decoder);
  133. #endif
  134. } else {
  135. #if HAVE_SAMPLERATE
  136. netj.capture_srcs = jack_slist_append(netj.capture_srcs, (void *)src_new(SRC_LINEAR, 1, NULL));
  137. #endif
  138. }
  139. }
  140. for (chn = netj.capture_channels_audio; chn < netj.capture_channels; chn++) {
  141. snprintf (buf, sizeof(buf) - 1, "system:capture_%u", chn + 1);
  142. if (fEngine->PortRegister(fClientControl.fRefNum, buf, JACK_DEFAULT_MIDI_TYPE,
  143. CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  144. jack_error("driver: cannot register port for %s", buf);
  145. return -1;
  146. }
  147. //port = fGraphManager->GetPort(port_index);
  148. netj.capture_ports =
  149. jack_slist_append (netj.capture_ports, (void *)(intptr_t)port_index);
  150. }
  151. for (chn = 0; chn < netj.playback_channels_audio; chn++) {
  152. snprintf (buf, sizeof(buf) - 1, "system:playback_%u", chn + 1);
  153. if (fEngine->PortRegister(fClientControl.fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE,
  154. PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  155. jack_error("driver: cannot register port for %s", buf);
  156. return -1;
  157. }
  158. //port = fGraphManager->GetPort(port_index);
  159. netj.playback_ports = jack_slist_append (netj.playback_ports, (void *)(intptr_t)port_index);
  160. if (netj.bitdepth == CELT_MODE) {
  161. #if HAVE_CELT
  162. #if HAVE_CELT_API_0_11
  163. CELTMode *celt_mode = celt_mode_create(netj.sample_rate, netj.period_size, NULL);
  164. netj.playback_srcs = jack_slist_append(netj.playback_srcs, celt_encoder_create_custom(celt_mode, 1, NULL));
  165. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  166. CELTMode *celt_mode = celt_mode_create(netj.sample_rate, netj.period_size, NULL);
  167. netj.playback_srcs = jack_slist_append(netj.playback_srcs, celt_encoder_create(celt_mode, 1, NULL));
  168. #else
  169. CELTMode *celt_mode = celt_mode_create(netj.sample_rate, 1, netj.period_size, NULL);
  170. netj.playback_srcs = jack_slist_append(netj.playback_srcs, celt_encoder_create(celt_mode));
  171. #endif
  172. #endif
  173. } else if (netj.bitdepth == OPUS_MODE) {
  174. #if HAVE_OPUS
  175. const int kbps = netj.resample_factor;
  176. jack_error("NEW ONE OPUS ENCODER 128 <> %d!!", kbps);
  177. int err;
  178. OpusCustomMode *opus_mode = opus_custom_mode_create( netj.sample_rate, netj.period_size, &err ); // XXX free me in the end
  179. if (err != OPUS_OK) { jack_error("opus mode failed"); }
  180. OpusCustomEncoder *oe = opus_custom_encoder_create( opus_mode, 1, &err );
  181. if (err != OPUS_OK) { jack_error("opus mode failed"); }
  182. opus_custom_encoder_ctl(oe, OPUS_SET_BITRATE(kbps*1024)); // bits per second
  183. opus_custom_encoder_ctl(oe, OPUS_SET_COMPLEXITY(10));
  184. opus_custom_encoder_ctl(oe, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));
  185. opus_custom_encoder_ctl(oe, OPUS_SET_SIGNAL(OPUS_APPLICATION_RESTRICTED_LOWDELAY));
  186. netj.playback_srcs = jack_slist_append(netj.playback_srcs, oe);
  187. #endif
  188. } else {
  189. #if HAVE_SAMPLERATE
  190. netj.playback_srcs = jack_slist_append(netj.playback_srcs, (void *)src_new(SRC_LINEAR, 1, NULL));
  191. #endif
  192. }
  193. }
  194. for (chn = netj.playback_channels_audio; chn < netj.playback_channels; chn++) {
  195. snprintf (buf, sizeof(buf) - 1, "system:playback_%u", chn + 1);
  196. if (fEngine->PortRegister(fClientControl.fRefNum, buf, JACK_DEFAULT_MIDI_TYPE,
  197. PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  198. jack_error("driver: cannot register port for %s", buf);
  199. return -1;
  200. }
  201. //port = fGraphManager->GetPort(port_index);
  202. netj.playback_ports =
  203. jack_slist_append (netj.playback_ports, (void *)(intptr_t)port_index);
  204. }
  205. return 0;
  206. }
  207. //init and restart--------------------------------------------------------------------
  208. bool JackNetOneDriver::Initialize()
  209. {
  210. jack_log("JackNetOneDriver::Init");
  211. FreePorts();
  212. netjack_release(&netj);
  213. //display some additional infos
  214. jack_info("NetOne driver started");
  215. if (netjack_startup(&netj)) {
  216. return false;
  217. }
  218. //register jack ports
  219. if (AllocPorts() != 0) {
  220. jack_error("Can't allocate ports.");
  221. return false;
  222. }
  223. //monitor
  224. //driver parametering
  225. JackTimedDriver::SetBufferSize(netj.period_size);
  226. JackTimedDriver::SetSampleRate(netj.sample_rate);
  227. JackDriver::NotifyBufferSize(netj.period_size);
  228. JackDriver::NotifySampleRate(netj.sample_rate);
  229. //transport engine parametering
  230. fEngineControl->fTransport.SetNetworkSync(true);
  231. return true;
  232. }
  233. //jack ports and buffers--------------------------------------------------------------
  234. //driver processes--------------------------------------------------------------------
  235. int JackNetOneDriver::Read()
  236. {
  237. int delay;
  238. delay = netjack_wait(&netj);
  239. if (delay) {
  240. NotifyXRun(fBeginDateUst, (float) delay);
  241. jack_error("netxruns... duration: %dms", delay / 1000);
  242. }
  243. if ((netj.num_lost_packets * netj.period_size / netj.sample_rate) > 2)
  244. throw JackNetException();
  245. //netjack_read(&netj, netj.period_size);
  246. JackDriver::CycleTakeBeginTime();
  247. jack_position_t local_trans_pos;
  248. jack_transport_state_t local_trans_state;
  249. unsigned int *packet_buf, *packet_bufX;
  250. if (! netj.packet_data_valid) {
  251. jack_log("data not valid");
  252. render_payload_to_jack_ports (netj.bitdepth, NULL, netj.net_period_down, netj.capture_ports, netj.capture_srcs, netj.period_size, netj.dont_htonl_floats);
  253. return 0;
  254. }
  255. packet_buf = netj.rx_buf;
  256. jacknet_packet_header *pkthdr = (jacknet_packet_header *)packet_buf;
  257. packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
  258. netj.reply_port = pkthdr->reply_port;
  259. netj.latency = pkthdr->latency;
  260. // Special handling for latency=0
  261. if (netj.latency == 0)
  262. netj.resync_threshold = 0;
  263. else
  264. netj.resync_threshold = MIN(15, pkthdr->latency - 1);
  265. // check whether, we should handle the transport sync stuff, or leave trnasports untouched.
  266. if (netj.handle_transport_sync) {
  267. #if 1
  268. unsigned int compensated_tranport_pos = (pkthdr->transport_frame + (pkthdr->latency * netj.period_size) + netj.codec_latency);
  269. // read local transport info....
  270. //local_trans_state = jack_transport_query(netj.client, &local_trans_pos);
  271. local_trans_state = fEngineControl->fTransport.Query(&local_trans_pos);
  272. // Now check if we have to start or stop local transport to sync to remote...
  273. switch (pkthdr->transport_state) {
  274. case JackTransportStarting:
  275. // the master transport is starting... so we set our reply to the sync_callback;
  276. if (local_trans_state == JackTransportStopped) {
  277. fEngineControl->fTransport.SetCommand(TransportCommandStart);
  278. //jack_transport_start(netj.client);
  279. //last_transport_state = JackTransportStopped;
  280. netj.sync_state = 0;
  281. jack_info("locally stopped... starting...");
  282. }
  283. if (local_trans_pos.frame != compensated_tranport_pos) {
  284. jack_position_t new_pos = local_trans_pos;
  285. new_pos.frame = compensated_tranport_pos + 2 * netj.period_size;
  286. new_pos.valid = (jack_position_bits_t) 0;
  287. fEngineControl->fTransport.RequestNewPos(&new_pos);
  288. //jack_transport_locate(netj.client, compensated_tranport_pos);
  289. //last_transport_state = JackTransportRolling;
  290. netj.sync_state = 0;
  291. jack_info("starting locate to %d", compensated_tranport_pos);
  292. }
  293. break;
  294. case JackTransportStopped:
  295. netj.sync_state = 1;
  296. if (local_trans_pos.frame != (pkthdr->transport_frame)) {
  297. jack_position_t new_pos = local_trans_pos;
  298. new_pos.frame = pkthdr->transport_frame;
  299. new_pos.valid = (jack_position_bits_t)0;
  300. fEngineControl->fTransport.RequestNewPos(&new_pos);
  301. //jack_transport_locate(netj.client, (pkthdr->transport_frame));
  302. jack_info("transport is stopped locate to %d", pkthdr->transport_frame);
  303. }
  304. if (local_trans_state != JackTransportStopped)
  305. //jack_transport_stop(netj.client);
  306. fEngineControl->fTransport.SetCommand(TransportCommandStop);
  307. break;
  308. case JackTransportRolling:
  309. netj.sync_state = 1;
  310. // if(local_trans_pos.frame != (pkthdr->transport_frame + (pkthdr->latency) * netj.period_size)) {
  311. // jack_transport_locate(netj.client, (pkthdr->transport_frame + (pkthdr->latency + 2) * netj.period_size));
  312. // jack_info("running locate to %d", pkthdr->transport_frame + (pkthdr->latency)*netj.period_size);
  313. // }
  314. if (local_trans_state != JackTransportRolling)
  315. fEngineControl->fTransport.SetState(JackTransportRolling);
  316. break;
  317. case JackTransportLooping:
  318. break;
  319. }
  320. #endif
  321. }
  322. render_payload_to_jack_ports (netj.bitdepth, packet_bufX, netj.net_period_down, netj.capture_ports, netj.capture_srcs, netj.period_size, netj.dont_htonl_floats);
  323. packet_cache_release_packet(netj.packcache, netj.expected_framecnt);
  324. return 0;
  325. }
  326. int JackNetOneDriver::Write()
  327. {
  328. int syncstate = netj.sync_state | ((fEngineControl->fTransport.GetState() == JackTransportNetStarting) ? 1 : 0);
  329. uint32_t *packet_buf, *packet_bufX;
  330. int packet_size = get_sample_size(netj.bitdepth) * netj.playback_channels * netj.net_period_up + sizeof(jacknet_packet_header);
  331. jacknet_packet_header *pkthdr;
  332. packet_buf = (uint32_t *) alloca(packet_size);
  333. pkthdr = (jacknet_packet_header *)packet_buf;
  334. if (netj.running_free) {
  335. return 0;
  336. }
  337. // offset packet_bufX by the packetheader.
  338. packet_bufX = packet_buf + sizeof(jacknet_packet_header) / sizeof(jack_default_audio_sample_t);
  339. pkthdr->sync_state = syncstate;;
  340. pkthdr->latency = netj.time_to_deadline;
  341. //printf("time to deadline = %d goodness=%d\n", (int)netj.time_to_deadline, netj.deadline_goodness);
  342. pkthdr->framecnt = netj.expected_framecnt;
  343. render_jack_ports_to_payload(netj.bitdepth, netj.playback_ports, netj.playback_srcs, netj.period_size, packet_bufX, netj.net_period_up, netj.dont_htonl_floats);
  344. packet_header_hton(pkthdr);
  345. if (netj.srcaddress_valid) {
  346. unsigned int r;
  347. static const int flag = 0;
  348. if (netj.reply_port)
  349. netj.syncsource_address.sin_port = htons(netj.reply_port);
  350. for (r = 0; r < netj.redundancy; r++)
  351. netjack_sendto(netj.sockfd, (char *)packet_buf, packet_size,
  352. flag, (struct sockaddr*) & (netj.syncsource_address), sizeof(struct sockaddr_in), netj.mtu);
  353. }
  354. return 0;
  355. }
  356. void
  357. JackNetOneDriver::FreePorts ()
  358. {
  359. JSList *node = netj.capture_ports;
  360. while (node != NULL) {
  361. JSList *this_node = node;
  362. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  363. node = jack_slist_remove_link(node, this_node);
  364. jack_slist_free_1(this_node);
  365. fEngine->PortUnRegister(fClientControl.fRefNum, port_index);
  366. }
  367. netj.capture_ports = NULL;
  368. node = netj.playback_ports;
  369. while (node != NULL) {
  370. JSList *this_node = node;
  371. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  372. node = jack_slist_remove_link(node, this_node);
  373. jack_slist_free_1(this_node);
  374. fEngine->PortUnRegister(fClientControl.fRefNum, port_index);
  375. }
  376. netj.playback_ports = NULL;
  377. if (netj.bitdepth == CELT_MODE) {
  378. #if HAVE_CELT
  379. node = netj.playback_srcs;
  380. while (node != NULL) {
  381. JSList *this_node = node;
  382. CELTEncoder *enc = (CELTEncoder *) node->data;
  383. node = jack_slist_remove_link(node, this_node);
  384. jack_slist_free_1(this_node);
  385. celt_encoder_destroy(enc);
  386. }
  387. netj.playback_srcs = NULL;
  388. node = netj.capture_srcs;
  389. while (node != NULL) {
  390. JSList *this_node = node;
  391. CELTDecoder *dec = (CELTDecoder *) node->data;
  392. node = jack_slist_remove_link(node, this_node);
  393. jack_slist_free_1(this_node);
  394. celt_decoder_destroy(dec);
  395. }
  396. netj.capture_srcs = NULL;
  397. #endif
  398. } else if (netj.bitdepth == OPUS_MODE) {
  399. #if HAVE_OPUS
  400. node = netj.playback_srcs;
  401. while (node != NULL) {
  402. JSList *this_node = node;
  403. OpusCustomEncoder *enc = (OpusCustomEncoder *) node->data;
  404. node = jack_slist_remove_link(node, this_node);
  405. jack_slist_free_1(this_node);
  406. opus_custom_encoder_destroy(enc);
  407. }
  408. netj.playback_srcs = NULL;
  409. node = netj.capture_srcs;
  410. while (node != NULL) {
  411. JSList *this_node = node;
  412. OpusCustomDecoder *dec = (OpusCustomDecoder *) node->data;
  413. node = jack_slist_remove_link(node, this_node);
  414. jack_slist_free_1(this_node);
  415. opus_custom_decoder_destroy(dec);
  416. }
  417. netj.capture_srcs = NULL;
  418. #endif
  419. } else {
  420. #if HAVE_SAMPLERATE
  421. node = netj.playback_srcs;
  422. while (node != NULL) {
  423. JSList *this_node = node;
  424. SRC_STATE *state = (SRC_STATE *) node->data;
  425. node = jack_slist_remove_link(node, this_node);
  426. jack_slist_free_1(this_node);
  427. src_delete(state);
  428. }
  429. netj.playback_srcs = NULL;
  430. node = netj.capture_srcs;
  431. while (node != NULL) {
  432. JSList *this_node = node;
  433. SRC_STATE *state = (SRC_STATE *) node->data;
  434. node = jack_slist_remove_link(node, this_node);
  435. jack_slist_free_1(this_node);
  436. src_delete(state);
  437. }
  438. netj.capture_srcs = NULL;
  439. #endif
  440. }
  441. }
  442. //Render functions--------------------------------------------------------------------
  443. // render functions for float
  444. void
  445. JackNetOneDriver::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)
  446. {
  447. uint32_t chn = 0;
  448. JSList *node = capture_ports;
  449. #if HAVE_SAMPLERATE
  450. JSList *src_node = capture_srcs;
  451. #endif
  452. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  453. if (!packet_payload)
  454. return;
  455. while (node != NULL) {
  456. unsigned int i;
  457. int_float_t val;
  458. #if HAVE_SAMPLERATE
  459. SRC_DATA src;
  460. #endif
  461. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  462. JackPort *port = fGraphManager->GetPort(port_index);
  463. jack_default_audio_sample_t* buf =
  464. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  465. const char *porttype = port->GetType();
  466. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0) {
  467. #if HAVE_SAMPLERATE
  468. // audio port, resample if necessary
  469. if (net_period_down != nframes) {
  470. SRC_STATE *src_state = (SRC_STATE *)src_node->data;
  471. for (i = 0; i < net_period_down; i++) {
  472. packet_bufX[i] = ntohl (packet_bufX[i]);
  473. }
  474. src.data_in = (float *) packet_bufX;
  475. src.input_frames = net_period_down;
  476. src.data_out = buf;
  477. src.output_frames = nframes;
  478. src.src_ratio = (float) nframes / (float) net_period_down;
  479. src.end_of_input = 0;
  480. src_set_ratio (src_state, src.src_ratio);
  481. src_process (src_state, &src);
  482. src_node = jack_slist_next (src_node);
  483. } else
  484. #endif
  485. {
  486. if (dont_htonl_floats) {
  487. memcpy(buf, packet_bufX, net_period_down * sizeof(jack_default_audio_sample_t));
  488. } else {
  489. for (i = 0; i < net_period_down; i++) {
  490. val.i = packet_bufX[i];
  491. val.i = ntohl (val.i);
  492. buf[i] = val.f;
  493. }
  494. }
  495. }
  496. } else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0) {
  497. // midi port, decode midi events
  498. // convert the data buffer to a standard format (uint32_t based)
  499. unsigned int buffer_size_uint32 = net_period_down;
  500. uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
  501. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  502. }
  503. packet_bufX = (packet_bufX + net_period_down);
  504. node = jack_slist_next (node);
  505. chn++;
  506. }
  507. }
  508. void
  509. JackNetOneDriver::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)
  510. {
  511. uint32_t chn = 0;
  512. JSList *node = playback_ports;
  513. #if HAVE_SAMPLERATE
  514. JSList *src_node = playback_srcs;
  515. #endif
  516. uint32_t *packet_bufX = (uint32_t *) packet_payload;
  517. while (node != NULL) {
  518. #if HAVE_SAMPLERATE
  519. SRC_DATA src;
  520. #endif
  521. unsigned int i;
  522. int_float_t val;
  523. jack_port_id_t port_index = (jack_port_id_t)(intptr_t) node->data;
  524. JackPort *port = fGraphManager->GetPort(port_index);
  525. jack_default_audio_sample_t* buf =
  526. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  527. const char *porttype = port->GetType();
  528. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0) {
  529. // audio port, resample if necessary
  530. #if HAVE_SAMPLERATE
  531. if (net_period_up != nframes) {
  532. SRC_STATE *src_state = (SRC_STATE *) src_node->data;
  533. src.data_in = buf;
  534. src.input_frames = nframes;
  535. src.data_out = (float *) packet_bufX;
  536. src.output_frames = net_period_up;
  537. src.src_ratio = (float) net_period_up / (float) nframes;
  538. src.end_of_input = 0;
  539. src_set_ratio (src_state, src.src_ratio);
  540. src_process (src_state, &src);
  541. for (i = 0; i < net_period_up; i++) {
  542. packet_bufX[i] = htonl (packet_bufX[i]);
  543. }
  544. src_node = jack_slist_next (src_node);
  545. } else
  546. #endif
  547. {
  548. if (dont_htonl_floats) {
  549. memcpy(packet_bufX, buf, net_period_up * sizeof(jack_default_audio_sample_t));
  550. } else {
  551. for (i = 0; i < net_period_up; i++) {
  552. val.f = buf[i];
  553. val.i = htonl (val.i);
  554. packet_bufX[i] = val.i;
  555. }
  556. }
  557. }
  558. } else if (strncmp(porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0) {
  559. // encode midi events from port to packet
  560. // convert the data buffer to a standard format (uint32_t based)
  561. unsigned int buffer_size_uint32 = net_period_up;
  562. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  563. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  564. }
  565. packet_bufX = (packet_bufX + net_period_up);
  566. node = jack_slist_next (node);
  567. chn++;
  568. }
  569. }
  570. #if HAVE_CELT
  571. // render functions for celt.
  572. void
  573. JackNetOneDriver::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)
  574. {
  575. uint32_t chn = 0;
  576. JSList *node = capture_ports;
  577. JSList *src_node = capture_srcs;
  578. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  579. while (node != NULL) {
  580. jack_port_id_t port_index = (jack_port_id_t) (intptr_t)node->data;
  581. JackPort *port = fGraphManager->GetPort(port_index);
  582. jack_default_audio_sample_t* buf =
  583. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  584. const char *portname = port->GetType();
  585. if (strncmp(portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0) {
  586. // audio port, decode celt data.
  587. CELTDecoder *decoder = (CELTDecoder *)src_node->data;
  588. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  589. if (!packet_payload)
  590. celt_decode_float(decoder, NULL, net_period_down, buf, nframes);
  591. else
  592. celt_decode_float(decoder, packet_bufX, net_period_down, buf, nframes);
  593. #else
  594. if (!packet_payload)
  595. celt_decode_float(decoder, NULL, net_period_down, buf);
  596. else
  597. celt_decode_float(decoder, packet_bufX, net_period_down, buf);
  598. #endif
  599. src_node = jack_slist_next (src_node);
  600. } else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0) {
  601. // midi port, decode midi events
  602. // convert the data buffer to a standard format (uint32_t based)
  603. unsigned int buffer_size_uint32 = net_period_down / 2;
  604. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  605. if (packet_payload)
  606. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  607. }
  608. packet_bufX = (packet_bufX + net_period_down);
  609. node = jack_slist_next (node);
  610. chn++;
  611. }
  612. }
  613. void
  614. JackNetOneDriver::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)
  615. {
  616. uint32_t chn = 0;
  617. JSList *node = playback_ports;
  618. JSList *src_node = playback_srcs;
  619. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  620. while (node != NULL) {
  621. jack_port_id_t port_index = (jack_port_id_t) (intptr_t) node->data;
  622. JackPort *port = fGraphManager->GetPort(port_index);
  623. jack_default_audio_sample_t* buf =
  624. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  625. const char *portname = port->GetType();
  626. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0) {
  627. // audio port, encode celt data.
  628. int encoded_bytes;
  629. jack_default_audio_sample_t *floatbuf = (jack_default_audio_sample_t *)alloca (sizeof(jack_default_audio_sample_t) * nframes);
  630. memcpy(floatbuf, buf, nframes * sizeof(jack_default_audio_sample_t));
  631. CELTEncoder *encoder = (CELTEncoder *)src_node->data;
  632. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  633. encoded_bytes = celt_encode_float(encoder, floatbuf, nframes, packet_bufX, net_period_up);
  634. #else
  635. encoded_bytes = celt_encode_float(encoder, floatbuf, NULL, packet_bufX, net_period_up);
  636. #endif
  637. if (encoded_bytes != (int)net_period_up)
  638. jack_error("something in celt changed. netjack needs to be changed to handle this.");
  639. src_node = jack_slist_next(src_node);
  640. } else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0) {
  641. // encode midi events from port to packet
  642. // convert the data buffer to a standard format (uint32_t based)
  643. unsigned int buffer_size_uint32 = net_period_up / 2;
  644. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  645. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  646. }
  647. packet_bufX = (packet_bufX + net_period_up);
  648. node = jack_slist_next (node);
  649. chn++;
  650. }
  651. }
  652. #endif
  653. #if HAVE_OPUS
  654. #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
  655. // render functions for Opus.
  656. void
  657. JackNetOneDriver::render_payload_to_jack_ports_opus (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  658. {
  659. int chn = 0;
  660. JSList *node = capture_ports;
  661. JSList *src_node = capture_srcs;
  662. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  663. while (node != NULL) {
  664. jack_port_id_t port_index = (jack_port_id_t) (intptr_t)node->data;
  665. JackPort *port = fGraphManager->GetPort(port_index);
  666. jack_default_audio_sample_t* buf =
  667. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  668. const char *portname = port->GetType();
  669. if (strncmp(portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0) {
  670. // audio port, decode opus data.
  671. OpusCustomDecoder *decoder = (OpusCustomDecoder*) src_node->data;
  672. if( !packet_payload )
  673. memset(buf, 0, nframes * sizeof(float));
  674. else {
  675. unsigned short len;
  676. memcpy(&len, packet_bufX, CDO);
  677. len = ntohs(len);
  678. opus_custom_decode_float( decoder, packet_bufX + CDO, len, buf, nframes );
  679. }
  680. src_node = jack_slist_next (src_node);
  681. } else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0) {
  682. // midi port, decode midi events
  683. // convert the data buffer to a standard format (uint32_t based)
  684. unsigned int buffer_size_uint32 = net_period_down / 2;
  685. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  686. if( packet_payload )
  687. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  688. }
  689. packet_bufX = (packet_bufX + net_period_down);
  690. node = jack_slist_next (node);
  691. chn++;
  692. }
  693. }
  694. void
  695. JackNetOneDriver::render_jack_ports_to_payload_opus (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  696. {
  697. int chn = 0;
  698. JSList *node = playback_ports;
  699. JSList *src_node = playback_srcs;
  700. unsigned char *packet_bufX = (unsigned char *)packet_payload;
  701. while (node != NULL) {
  702. jack_port_id_t port_index = (jack_port_id_t) (intptr_t) node->data;
  703. JackPort *port = fGraphManager->GetPort(port_index);
  704. jack_default_audio_sample_t* buf =
  705. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(port_index, fEngineControl->fBufferSize);
  706. const char *portname = port->GetType();
  707. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0) {
  708. // audio port, encode opus data.
  709. int encoded_bytes;
  710. jack_default_audio_sample_t *floatbuf = (jack_default_audio_sample_t *)alloca (sizeof(jack_default_audio_sample_t) * nframes);
  711. memcpy(floatbuf, buf, nframes * sizeof(jack_default_audio_sample_t));
  712. OpusCustomEncoder *encoder = (OpusCustomEncoder*) src_node->data;
  713. encoded_bytes = opus_custom_encode_float( encoder, floatbuf, nframes, packet_bufX + CDO, net_period_up - CDO );
  714. unsigned short len = htons(encoded_bytes);
  715. memcpy(packet_bufX, &len, CDO);
  716. src_node = jack_slist_next( src_node );
  717. } else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0) {
  718. // encode midi events from port to packet
  719. // convert the data buffer to a standard format (uint32_t based)
  720. unsigned int buffer_size_uint32 = net_period_up / 2;
  721. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  722. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  723. }
  724. packet_bufX = (packet_bufX + net_period_up);
  725. node = jack_slist_next (node);
  726. chn++;
  727. }
  728. }
  729. #endif
  730. /* Wrapper functions with bitdepth argument... */
  731. void
  732. JackNetOneDriver::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)
  733. {
  734. #if HAVE_CELT
  735. if (bitdepth == CELT_MODE)
  736. render_payload_to_jack_ports_celt (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  737. else
  738. #endif
  739. #if HAVE_OPUS
  740. if (bitdepth == OPUS_MODE)
  741. render_payload_to_jack_ports_opus (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  742. else
  743. #endif
  744. render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes, dont_htonl_floats);
  745. }
  746. void
  747. JackNetOneDriver::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)
  748. {
  749. #if HAVE_CELT
  750. if (bitdepth == CELT_MODE)
  751. render_jack_ports_to_payload_celt (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  752. else
  753. #endif
  754. #if HAVE_OPUS
  755. if (bitdepth == OPUS_MODE)
  756. render_jack_ports_to_payload_opus (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  757. else
  758. #endif
  759. render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);
  760. }
  761. //driver loader-----------------------------------------------------------------------
  762. #ifdef __cplusplus
  763. extern "C"
  764. {
  765. #endif
  766. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor ()
  767. {
  768. jack_driver_desc_t * desc;
  769. jack_driver_desc_filler_t filler;
  770. jack_driver_param_value_t value;
  771. desc = jack_driver_descriptor_construct("netone", JackDriverMaster, "netjack one slave backend component", &filler);
  772. value.ui = 2U;
  773. jack_driver_descriptor_add_parameter(desc, &filler, "audio-ins", 'i', JackDriverParamUInt, &value, NULL, "Number of capture channels (defaults to 2)", NULL);
  774. jack_driver_descriptor_add_parameter(desc, &filler, "audio-outs", 'o', JackDriverParamUInt, &value, NULL, "Number of playback channels (defaults to 2)", NULL);
  775. value.ui = 1U;
  776. jack_driver_descriptor_add_parameter(desc, &filler, "midi-ins", 'I', JackDriverParamUInt, &value, NULL, "Number of midi capture channels (defaults to 1)", NULL);
  777. jack_driver_descriptor_add_parameter(desc, &filler, "midi-outs", 'O', JackDriverParamUInt, &value, NULL, "Number of midi playback channels (defaults to 1)", NULL);
  778. value.ui = 48000U;
  779. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  780. value.ui = 1024U;
  781. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  782. value.ui = 5U;
  783. jack_driver_descriptor_add_parameter(desc, &filler, "num-periods", 'n', JackDriverParamUInt, &value, NULL, "Network latency setting in no. of periods", NULL);
  784. value.ui = 3000U;
  785. jack_driver_descriptor_add_parameter(desc, &filler, "listen-port", 'l', JackDriverParamUInt, &value, NULL, "The socket port we are listening on for sync packets", NULL);
  786. value.ui = 1U;
  787. jack_driver_descriptor_add_parameter(desc, &filler, "factor", 'f', JackDriverParamUInt, &value, NULL, "Factor for sample rate reduction", NULL);
  788. value.ui = 0U;
  789. jack_driver_descriptor_add_parameter(desc, &filler, "upstream-factor", 'u', JackDriverParamUInt, &value, NULL, "Factor for sample rate reduction on the upstream", NULL);
  790. #if HAVE_CELT
  791. value.ui = 0U;
  792. jack_driver_descriptor_add_parameter(desc, &filler, "celt", 'c', JackDriverParamUInt, &value, NULL, "Set CELT encoding and number of kbits per channel", NULL);
  793. #endif
  794. #if HAVE_OPUS
  795. value.ui = 0U;
  796. jack_driver_descriptor_add_parameter(desc, &filler, "opus", 'P', JackDriverParamUInt, &value, NULL, "Set Opus encoding and number of kbits per channel", NULL);
  797. #endif
  798. value.ui = 0U;
  799. jack_driver_descriptor_add_parameter(desc, &filler, "bit-depth", 'b', JackDriverParamUInt, &value, NULL, "Sample bit-depth (0 for float, 8 for 8bit and 16 for 16bit)", NULL);
  800. value.i = true;
  801. jack_driver_descriptor_add_parameter(desc, &filler, "transport-sync", 't', JackDriverParamBool, &value, NULL, "Whether to slave the transport to the master transport", NULL);
  802. value.ui = true;
  803. jack_driver_descriptor_add_parameter(desc, &filler, "autoconf", 'a', JackDriverParamBool, &value, NULL, "Whether to use Autoconfig, or just start", NULL);
  804. value.ui = 1U;
  805. jack_driver_descriptor_add_parameter(desc, &filler, "redundancy", 'R', JackDriverParamUInt, &value, NULL, "Send packets N times", NULL);
  806. value.ui = false;
  807. jack_driver_descriptor_add_parameter(desc, &filler, "native-endian", 'e', JackDriverParamBool, &value, NULL, "Don't convert samples to network byte order", NULL);
  808. value.i = 0;
  809. jack_driver_descriptor_add_parameter(desc, &filler, "jitterval", 'J', JackDriverParamInt, &value, NULL, "Attempted jitterbuffer microseconds on master", NULL);
  810. value.i = false;
  811. jack_driver_descriptor_add_parameter(desc, &filler, "always-deadline", 'D', JackDriverParamBool, &value, NULL, "Always use deadline", NULL);
  812. return desc;
  813. }
  814. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  815. {
  816. jack_nframes_t sample_rate = 48000;
  817. jack_nframes_t resample_factor = 1;
  818. jack_nframes_t period_size = 1024;
  819. unsigned int capture_ports = 2;
  820. unsigned int playback_ports = 2;
  821. unsigned int capture_ports_midi = 1;
  822. unsigned int playback_ports_midi = 1;
  823. unsigned int listen_port = 3000;
  824. unsigned int bitdepth = 0;
  825. unsigned int handle_transport_sync = 1;
  826. unsigned int use_autoconfig = 1;
  827. unsigned int latency = 5;
  828. unsigned int redundancy = 1;
  829. unsigned int mtu = 1400;
  830. #if HAVE_SAMPLERATE
  831. unsigned int resample_factor_up = 1;
  832. #endif
  833. int dont_htonl_floats = 0;
  834. int always_deadline = 0;
  835. int jitter_val = 0;
  836. const JSList * node;
  837. const jack_driver_param_t * param;
  838. for (node = params; node; node = jack_slist_next(node)) {
  839. param = (const jack_driver_param_t*) node->data;
  840. switch (param->character) {
  841. case 'i':
  842. capture_ports = param->value.ui;
  843. break;
  844. case 'o':
  845. playback_ports = param->value.ui;
  846. break;
  847. case 'I':
  848. capture_ports_midi = param->value.ui;
  849. break;
  850. case 'O':
  851. playback_ports_midi = param->value.ui;
  852. break;
  853. case 'r':
  854. sample_rate = param->value.ui;
  855. break;
  856. case 'p':
  857. period_size = param->value.ui;
  858. break;
  859. case 'l':
  860. listen_port = param->value.ui;
  861. break;
  862. case 'f':
  863. #if HAVE_SAMPLERATE
  864. resample_factor = param->value.ui;
  865. #else
  866. jack_error("not built with libsamplerate support");
  867. return NULL;
  868. #endif
  869. break;
  870. case 'u':
  871. #if HAVE_SAMPLERATE
  872. resample_factor_up = param->value.ui;
  873. #else
  874. jack_error("not built with libsamplerate support");
  875. return NULL;
  876. #endif
  877. break;
  878. case 'b':
  879. bitdepth = param->value.ui;
  880. break;
  881. case 'c':
  882. #if HAVE_CELT
  883. bitdepth = CELT_MODE;
  884. resample_factor = param->value.ui;
  885. #else
  886. jack_error("not built with celt support");
  887. return NULL;
  888. #endif
  889. break;
  890. case 'P':
  891. #if HAVE_OPUS
  892. bitdepth = OPUS_MODE;
  893. resample_factor = param->value.ui;
  894. jack_error("OPUS: %d\n", resample_factor);
  895. #else
  896. jack_error("not built with Opus support");
  897. return NULL;
  898. #endif
  899. break;
  900. case 't':
  901. handle_transport_sync = param->value.ui;
  902. break;
  903. case 'a':
  904. use_autoconfig = param->value.ui;
  905. break;
  906. case 'n':
  907. latency = param->value.ui;
  908. break;
  909. case 'R':
  910. redundancy = param->value.ui;
  911. break;
  912. case 'H':
  913. dont_htonl_floats = param->value.ui;
  914. break;
  915. case 'J':
  916. jitter_val = param->value.i;
  917. break;
  918. case 'D':
  919. always_deadline = param->value.ui;
  920. break;
  921. }
  922. }
  923. try {
  924. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver (
  925. new Jack::JackNetOneDriver("system", "net_pcm", engine, table, listen_port, mtu,
  926. capture_ports_midi, playback_ports_midi, capture_ports, playback_ports,
  927. sample_rate, period_size, resample_factor,
  928. "net_pcm", handle_transport_sync, bitdepth, use_autoconfig, latency, redundancy,
  929. dont_htonl_floats, always_deadline, jitter_val));
  930. if (driver->Open(period_size, sample_rate, 1, 1, capture_ports, playback_ports,
  931. 0, "from_master", "to_master", 0, 0) == 0) {
  932. return driver;
  933. } else {
  934. delete driver;
  935. return NULL;
  936. }
  937. } catch (...) {
  938. return NULL;
  939. }
  940. #if HAVE_SAMPLERATE
  941. // unused
  942. (void)resample_factor_up;
  943. #endif
  944. }
  945. #ifdef __cplusplus
  946. }
  947. #endif
  948. }