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