JackNetAPI.cpp 32.5 KB
Newer Older
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
/*
Copyright (C) 2009 Grame

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include <assert.h>
#include "JackNetInterface.h"
#include "JackPlatformPlug.h"
#include "JackError.h"
#include "JackTime.h"
#include "JackException.h"

#include "JackAudioAdapterInterface.h"

#ifdef __cplusplus
extern "C"
{
#endif
  
    // NetJack common API
    
    #define MASTER_NAME_SIZE 256

    enum JackNetMode {

        JackFastMode = 'f',
        JackNormalMode = 'n',
        JackSlowMode = 's',
    };

     typedef struct {
    
        int audio_input;
        int audio_output;
        int midi_input;
        int midi_ouput; 
        int mtu;
        int time_out;   // in millisecond, -1 means in infinite
        char mode;

    } jack_slave_t;

    typedef struct {
        
        jack_nframes_t buffer_size;
        jack_nframes_t sample_rate;
        char master_name[MASTER_NAME_SIZE];

    } jack_master_t;
    
    // NetJack slave API
     
    typedef struct _jack_net_slave jack_net_slave_t;
      
    typedef int (* JackNetSlaveProcessCallback) (jack_nframes_t buffer_size,
                                            int audio_input, 
                                            float** audio_input_buffer, 
                                            int midi_input,
                                            void** midi_input_buffer,
                                            int audio_output,
                                            float** audio_output_buffer, 
                                            int midi_output, 
                                            void** midi_output_buffer, 
                                            void* data);
       
    typedef int (*JackNetSlaveBufferSizeCallback) (jack_nframes_t nframes, void *arg);
    typedef int (*JackNetSlaveSampleRateCallback) (jack_nframes_t nframes, void *arg);
    typedef void (*JackNetSlaveShutdownCallback) (void* data);

    SERVER_EXPORT jack_net_slave_t* jack_net_slave_open(const char* ip, int port, const char* name, jack_slave_t* request, jack_master_t* result);
    SERVER_EXPORT int jack_net_slave_close(jack_net_slave_t* net);
    
    SERVER_EXPORT int jack_net_slave_activate(jack_net_slave_t* net);
    SERVER_EXPORT int jack_net_slave_deactivate(jack_net_slave_t* net);
     
    SERVER_EXPORT int jack_set_net_slave_process_callback(jack_net_slave_t * net, JackNetSlaveProcessCallback net_callback, void *arg);
    SERVER_EXPORT int jack_set_net_slave_buffer_size_callback(jack_net_slave_t *net, JackNetSlaveBufferSizeCallback bufsize_callback, void *arg);
    SERVER_EXPORT int jack_set_net_slave_sample_rate_callback(jack_net_slave_t *net, JackNetSlaveSampleRateCallback samplerate_callback, void *arg);
    SERVER_EXPORT int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg);
    
    // NetJack master API
    
    typedef struct _jack_net_master jack_net_master_t;
    
    SERVER_EXPORT jack_net_master_t* jack_net_master_open(const char* ip, int port, const char* name, jack_master_t* request, jack_slave_t* result);
    SERVER_EXPORT int jack_net_master_close(jack_net_master_t* net);
    
    SERVER_EXPORT int jack_net_master_recv(jack_net_master_t* net, int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer);
    SERVER_EXPORT int jack_net_master_send(jack_net_master_t* net, int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer);
  
    // NetJack adapter API
    
    typedef struct _jack_adapter jack_adapter_t;
    
    SERVER_EXPORT jack_adapter_t* jack_create_adapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate);
    SERVER_EXPORT int jack_destroy_adapter(jack_adapter_t* adapter);

    SERVER_EXPORT int jack_adapter_push_input(jack_adapter_t* adapter, int channels, float** buffers);
    SERVER_EXPORT int jack_adapter_pull_input(jack_adapter_t* adapter, int channels, float** buffers);
    
    SERVER_EXPORT int jack_adapter_push_output(jack_adapter_t* adapter, int channels, float** buffers);
    SERVER_EXPORT int jack_adapter_pull_output(jack_adapter_t* adapter, int channels, float** buffers);
       
#ifdef __cplusplus
}
#endif

namespace Jack
{

struct JackNetExtMaster : public JackNetMasterInterface {
        
sletz's avatar
sletz committed
127
    // Data buffers
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    float** fAudioCaptureBuffer;
    float** fAudioPlaybackBuffer;
    
    JackMidiBuffer** fMidiCaptureBuffer;
    JackMidiBuffer** fMidiPlaybackBuffer;
    
    jack_master_t fRequest;
    
    JackNetExtMaster(const char* ip, 
                    int port, 
                    const char* name, 
                    jack_master_t* request)
    {
        fRunning = true;
        assert(strlen(ip) < 32);
        strcpy(fMulticastIP, ip);
        fSocket.SetPort(port);
sletz's avatar
sletz committed
145
146
        fRequest.buffer_size = request->buffer_size;
        fRequest.sample_rate = request->sample_rate;
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
    }
                
    virtual ~JackNetExtMaster()
    {}
    
    int Open(jack_slave_t* result)
    {
        // Init socket API (win32)
        if (SocketAPIInit() < 0) {
            fprintf(stderr, "Can't init Socket API, exiting...\n");
            return -1;
        }

        // Request socket
        if (fSocket.NewSocket() == SOCKET_ERROR) {
            fprintf(stderr, "Can't create the network management input socket : %s\n", StrError(NET_ERROR_CODE));
            return -1;
        }

        // Bind the socket to the local port
        if (fSocket.Bind() == SOCKET_ERROR) {
            fprintf(stderr, "Can't bind the network manager socket : %s\n", StrError(NET_ERROR_CODE));
            fSocket.Close();
            return -1;
        }

        // Join multicast group
        if (fSocket.JoinMCastGroup(fMulticastIP) == SOCKET_ERROR)
             fprintf(stderr, "Can't join multicast group : %s\n", StrError(NET_ERROR_CODE));

        // Local loop
        if (fSocket.SetLocalLoop() == SOCKET_ERROR)
            fprintf(stderr, "Can't set local loop : %s\n", StrError(NET_ERROR_CODE));

        // Set a timeout on the multicast receive (the thread can now be cancelled)
        if (fSocket.SetTimeOut(2000000) == SOCKET_ERROR)
            fprintf(stderr, "Can't set timeout : %s\n", StrError(NET_ERROR_CODE));
   
         //main loop, wait for data, deal with it and wait again
         //utility variables
        int attempt = 0;
        int rx_bytes = 0;
        
        do
        {
            session_params_t net_params;
            rx_bytes = fSocket.CatchHost(&net_params, sizeof(session_params_t), 0);
            SessionParamsNToH(&net_params, &fParams);
            
            if ((rx_bytes == SOCKET_ERROR) && (fSocket.GetError() != NET_NO_DATA)) {
                 fprintf(stderr, "Error in receive : %s\n", StrError(NET_ERROR_CODE));
                if (++attempt == 10) {
                    fprintf(stderr, "Can't receive on the socket, exiting net manager.\n" );
                    goto error;
                }
            }
            
            if (rx_bytes == sizeof(session_params_t ))  {
            
                switch (GetPacketType(&fParams)) {
                
                    case SLAVE_AVAILABLE:
                        if (MasterInit() == 0) {
                            SessionParamsDisplay(&fParams);
                            fRunning = false;
                        } else {
                            fprintf(stderr, "Can't init new net master...\n");
                            goto error;
                        }
                        break;
                        
                    case KILL_MASTER:
                         break;
                        
                    default:
                        break;
                }
            }
        }
        while (fRunning);
        
        // Set result paramaters
        result->audio_input = fParams.fSendAudioChannels;
        result->audio_output = fParams.fReturnAudioChannels;
        result->midi_input = fParams.fSendMidiChannels;
        result->midi_ouput = fParams.fReturnMidiChannels;
        result->midi_ouput = fParams.fMtu;
        result->mode = fParams.fNetworkMode;
        return 0;
        
    error:
        fSocket.Close();
        return -1;
    }
    
    int MasterInit()
    {
        // Check MASTER <<==> SLAVE network protocol coherency
        if (fParams.fProtocolVersion != MASTER_PROTOCOL) {
            fprintf(stderr, "Error : slave is running with a different protocol %s\n", fParams.fName);
            return -1;
        }

        // Settings
        fSocket.GetName(fParams.fMasterNetName);
        fParams.fID = 1;
        fParams.fBitdepth = 0;
        fParams.fPeriodSize = fRequest.buffer_size;
        fParams.fSampleRate = fRequest.sample_rate;
     
        // Close request socket
        fSocket.Close();
        
        // Network slave init
        if (!JackNetMasterInterface::Init())
            return -1;

        // Set global parameters
        SetParams();
        AllocPorts();
        return 0;
    }
    
    int Close()
    {
        fSocket.Close();
        FreePorts();
        return 0;
    }
    
    void AllocPorts()
    {
        unsigned int port_index;
     
        // Set buffers
        fAudioPlaybackBuffer = new float*[fParams.fSendAudioChannels];
        for (port_index = 0; port_index < fParams.fSendAudioChannels; port_index++) {
           fAudioPlaybackBuffer[port_index] = new float[fParams.fPeriodSize];
           fNetAudioPlaybackBuffer->SetBuffer(port_index, fAudioPlaybackBuffer[port_index]);
        }
        
        fMidiPlaybackBuffer = new JackMidiBuffer*[fParams.fSendMidiChannels];
        for (port_index = 0; port_index < fParams.fSendMidiChannels; port_index++) {
           fMidiPlaybackBuffer[port_index] = (JackMidiBuffer*)new float[fParams.fPeriodSize];
           fNetMidiPlaybackBuffer->SetBuffer(port_index, fMidiPlaybackBuffer[port_index]);
        }
      
        fAudioCaptureBuffer = new float*[fParams.fReturnAudioChannels];
        for (port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++) {
           fAudioCaptureBuffer[port_index] = new float[fParams.fPeriodSize];
           fNetAudioCaptureBuffer->SetBuffer(port_index, fAudioCaptureBuffer[port_index]);
        }  
        
        fMidiCaptureBuffer = new JackMidiBuffer*[fParams.fReturnMidiChannels];
        for (port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++) {
           fMidiCaptureBuffer[port_index] = (JackMidiBuffer*)new float[fParams.fPeriodSize];
           fNetMidiCaptureBuffer->SetBuffer(port_index, fMidiCaptureBuffer[port_index]);
        }
    }
    
    void FreePorts()
    {
        unsigned int port_index;
         
        if (fAudioPlaybackBuffer) {
            for (port_index = 0; port_index < fParams.fSendAudioChannels; port_index++)
                delete[] fAudioPlaybackBuffer[port_index];
            delete[] fAudioPlaybackBuffer;
            fAudioPlaybackBuffer = NULL;
        }
        
        if (fMidiPlaybackBuffer) {
            for (port_index = 0; port_index < fParams.fSendMidiChannels; port_index++)
                delete[] (fMidiPlaybackBuffer[port_index]);
            delete[] fMidiPlaybackBuffer;
            fMidiPlaybackBuffer = NULL;
        }

        if (fAudioCaptureBuffer) {
            for (port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++)
                delete[] fAudioCaptureBuffer[port_index];
            delete[] fAudioCaptureBuffer;
            fAudioCaptureBuffer = NULL;
        }
        
        if (fMidiCaptureBuffer) {
            for (port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++)
                delete[] fMidiCaptureBuffer[port_index];
            delete[] fMidiCaptureBuffer;
            fMidiCaptureBuffer = NULL;
        }
    }
    
    int Read(int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer)
     {
         assert((unsigned int)audio_input == fParams.fSendAudioChannels);
         int port_index;
         
         for (port_index = 0; port_index < audio_input; port_index++) {
             fNetAudioPlaybackBuffer->SetBuffer(port_index, audio_input_buffer[port_index]);
         }
         
         for (port_index = 0; port_index < midi_input; port_index++) {
             fNetMidiPlaybackBuffer->SetBuffer(port_index, ((JackMidiBuffer**)midi_input_buffer)[port_index]);
         }
        
         if (SyncRecv() == SOCKET_ERROR)
             return 0;

         if (DecodeSyncPacket() < 0)
             return 0;

         return DataRecv();
     }

     int Write(int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer)
     {
         assert((unsigned int)audio_output == fParams.fReturnAudioChannels);
         int port_index;
       
         for (port_index = 0;  port_index < audio_output; port_index++) {
             fNetAudioCaptureBuffer->SetBuffer(port_index, audio_output_buffer[port_index]);
         }
         
         for (port_index = 0;  port_index < midi_output; port_index++) {
             fNetMidiCaptureBuffer->SetBuffer(port_index, ((JackMidiBuffer**)midi_output_buffer)[port_index]);
         }
      
         if (EncodeSyncPacket() < 0)
             return 0;

         if (SyncSend() == SOCKET_ERROR)
             return SOCKET_ERROR;

         return DataSend();
     }
     
    // Transport
    int EncodeTransportData()
    {
        return 0;
    }
    
    int DecodeTransportData()
    {
        return 0;
    }
sletz's avatar
sletz committed
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
};

struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterface {
        
    JackThread fThread;
    
    JackNetSlaveProcessCallback fProcessCallback;
    void* fProcessArg;
    
    JackNetSlaveShutdownCallback fShutdownCallback;
    void* fShutdownArg;
    
    JackNetSlaveBufferSizeCallback fBufferSizeCallback;
    void* fBufferSizeArg;
    
    JackNetSlaveSampleRateCallback fSampleRateCallback;
    void* fSampleRateArg;
    
    //sample buffers
    float** fAudioCaptureBuffer;
    float** fAudioPlaybackBuffer;
    
    JackMidiBuffer** fMidiCaptureBuffer;
    JackMidiBuffer** fMidiPlaybackBuffer;
    
    JackNetExtSlave(const char* ip, 
                int port, 
                const char* name, 
                jack_slave_t* request)
        :fThread(this),
        fProcessCallback(NULL),fProcessArg(NULL), 
        fShutdownCallback(NULL), fShutdownArg(NULL),
        fBufferSizeCallback(NULL), fBufferSizeArg(NULL),
        fSampleRateCallback(NULL), fSampleRateArg(NULL),
        fAudioCaptureBuffer(NULL), fAudioPlaybackBuffer(NULL),
        fMidiCaptureBuffer(NULL), fMidiPlaybackBuffer(NULL)
    {
        char host_name[JACK_CLIENT_NAME_SIZE];
        
        // Request parameters
        assert(strlen(ip) < 32);
        strcpy(fMulticastIP, ip);
        
        fParams.fMtu = request->mtu;
        fParams.fTransportSync = 0;
        fParams.fSendAudioChannels = request->audio_input;
        fParams.fReturnAudioChannels = request->audio_output;
        fParams.fSendMidiChannels = request->midi_input;
        fParams.fReturnMidiChannels = request->midi_ouput;
        fParams.fNetworkMode = request->mode;
        fParams.fSlaveSyncMode = 1;
       
        // Create name with hostname and client name
        GetHostName(host_name, JACK_CLIENT_NAME_SIZE);
        snprintf(fParams.fName, JACK_CLIENT_NAME_SIZE, "%s_%s", host_name, name);
        fSocket.GetName(fParams.fSlaveNetName);
        
        // Set the socket parameters
        fSocket.SetPort(port);
        fSocket.SetAddress(fMulticastIP, port);
    }
    
    virtual ~JackNetExtSlave()
    {}
    
    int Open(jack_master_t* result)
    {
        // Init network connection
        if (!JackNetSlaveInterface::InitConnection()){
            return -1;
        }
             
        // Then set global parameters
        SetParams();
        
         // Set result
         if (result != NULL) {
            result->buffer_size = fParams.fPeriodSize;
            result->sample_rate = fParams.fSampleRate;
            strcpy(result->master_name, fParams.fMasterNetName);
        }
              
        AllocPorts();
        return 0;
    }
    
     int Restart()
    {
        // If shutdown cb is set, then call it
        if (fShutdownCallback)
            fShutdownCallback(fShutdownArg);
         
        // Init complete network connection
        if (!JackNetSlaveInterface::Init())
            return -1;
               
        // Then set global parameters
        SetParams();
     
        // We need to notify possibly new buffer size and sample rate (see Execute)
        if (fBufferSizeCallback) 
            fBufferSizeCallback(fParams.fPeriodSize, fBufferSizeArg);
            
        if (fSampleRateCallback) 
            fSampleRateCallback(fParams.fSampleRate, fSampleRateArg);
           
        AllocPorts();
        return 0;
    }
      
    int Close()
    {
        fSocket.Close();
        FreePorts();
        return 0;
    }

    void AllocPorts()
    {
        unsigned int port_index;
     
        // Set buffers
        fAudioCaptureBuffer = new float*[fParams.fSendAudioChannels];
        for (port_index = 0; port_index < fParams.fSendAudioChannels; port_index++) {
           fAudioCaptureBuffer[port_index] = new float[fParams.fPeriodSize];
           fNetAudioCaptureBuffer->SetBuffer(port_index, fAudioCaptureBuffer[port_index]);
        }
        
        fMidiCaptureBuffer = new JackMidiBuffer*[fParams.fSendMidiChannels];
        for (port_index = 0; port_index < fParams.fSendMidiChannels; port_index++) {
           fMidiCaptureBuffer[port_index] = (JackMidiBuffer*)new float[fParams.fPeriodSize];
           fNetMidiCaptureBuffer->SetBuffer(port_index, fMidiCaptureBuffer[port_index]);
        }
      
        fAudioPlaybackBuffer = new float*[fParams.fReturnAudioChannels];
        for (port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++) {
           fAudioPlaybackBuffer[port_index] = new float[fParams.fPeriodSize];
           fNetAudioPlaybackBuffer->SetBuffer(port_index, fAudioPlaybackBuffer[port_index]);
        }  
        
        fMidiPlaybackBuffer = new JackMidiBuffer*[fParams.fReturnMidiChannels];
        for (port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++) {
           fMidiPlaybackBuffer[port_index] = (JackMidiBuffer*)new float[fParams.fPeriodSize];
           fNetMidiPlaybackBuffer->SetBuffer(port_index, fMidiPlaybackBuffer[port_index]);
        }
    }
    
    void FreePorts()
    {
        unsigned int port_index;
         
        if (fAudioCaptureBuffer) {
            for (port_index = 0; port_index < fParams.fSendAudioChannels; port_index++)
                delete[] fAudioCaptureBuffer[port_index];
            delete[] fAudioCaptureBuffer;
            fAudioCaptureBuffer = NULL;
        }
        
        if (fMidiCaptureBuffer) {
            for (port_index = 0; port_index < fParams.fSendMidiChannels; port_index++)
                delete[] (fMidiCaptureBuffer[port_index]);
            delete[] fMidiCaptureBuffer;
            fMidiCaptureBuffer = NULL;
        }

        if (fAudioPlaybackBuffer) {
            for (port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++)
                delete[] fAudioPlaybackBuffer[port_index];
            delete[] fAudioPlaybackBuffer;
            fAudioPlaybackBuffer = NULL;
        }
        
        if (fMidiPlaybackBuffer) {
            for (port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++)
                delete[] fMidiPlaybackBuffer[port_index];
            delete[] fMidiPlaybackBuffer;
            fMidiPlaybackBuffer = NULL;
        }
    }
       
    // Transport
    int EncodeTransportData()
    {
        return 0;
    }
    
    int DecodeTransportData()
    {
        return 0;
    }
    
    bool Init() 
    {
        // Will do "something" on OSX only...
        fThread.SetParams(float(fParams.fPeriodSize) / float(fParams.fSampleRate) * 1000000, 100 * 1000, 500 * 1000);  
        return (fThread.AcquireRealTime(80) == 0);      // TODO: get a value from the server
    }
    
    bool Execute()
    {
        try  {
            // Keep running even in case of error
            while (fThread.GetStatus() == JackThread::kRunning) {
                if (Process() == SOCKET_ERROR)
                    return false;
            }
            return false;
        } catch (JackNetException& e) {
        
            // Otherwise just restart...
            e.PrintMessage();
            fThread.DropRealTime();
            fThread.SetStatus(JackThread::kIniting);
            FreePorts();
            Restart();
            if (Init()) {
                fThread.SetStatus(JackThread::kRunning);
                return true;
            } else {
                return false;
            }
        }
    }
    
    int Read()
    {
        // Don't return -1 in case of sync recv failure
        // we need the process to continue for network error detection
        if (SyncRecv() == SOCKET_ERROR)
            return 0;

        if (DecodeSyncPacket() < 0)
            return 0;

        return DataRecv();
    }

    int Write()
    {
        if (EncodeSyncPacket() < 0)
            return 0;

        if (SyncSend() == SOCKET_ERROR)
            return SOCKET_ERROR;

        return DataSend();
    }
  
    int Process() 
    {
        // Read data from the network
        // in case of fatal network error, stop the process
        if (Read() == SOCKET_ERROR)
            return SOCKET_ERROR;
     
        fProcessCallback(fParams.fPeriodSize, 
                        fParams.fSendAudioChannels, 
                        fAudioCaptureBuffer, 
                        fParams.fSendMidiChannels,
                        (void**)fMidiCaptureBuffer, 
                        fParams.fReturnAudioChannels,
                        fAudioPlaybackBuffer, 
                        fParams.fReturnMidiChannels,
                        (void**)fMidiPlaybackBuffer, 
                        fProcessArg); 
     
        // Then write data to network
        // in case of failure, stop process
        if (Write() == SOCKET_ERROR)
            return SOCKET_ERROR;

        return 0;
    }
    
    int Start()
    {
        // Finish connection.. 
        if (!JackNetSlaveInterface::InitRendering()) {
            return -1;
        }
   
        return (fProcessCallback == 0) ? -1 : fThread.StartSync();
    }

    int Stop()
    {
        return (fProcessCallback == 0) ? -1 : fThread.Kill();
    }
    
    // Callback
    int SetProcessCallback(JackNetSlaveProcessCallback net_callback, void *arg)
    {
        if (fThread.GetStatus() == JackThread::kRunning) {
            return -1;
        } else {
            fProcessCallback = net_callback;
            fProcessArg = arg;
            return 0;
        }
    }

    int SetShutdownCallback(JackNetSlaveShutdownCallback shutdown_callback, void *arg)
    {
        if (fThread.GetStatus() == JackThread::kRunning) {
            return -1;
        } else {
            fShutdownCallback = shutdown_callback;
            fShutdownArg = arg;
            return 0;
        }
    }
    
    int SetBufferSizeCallback(JackNetSlaveBufferSizeCallback bufsize_callback, void *arg)
    {
        if (fThread.GetStatus() == JackThread::kRunning) {
            return -1;
        } else {
            fBufferSizeCallback = bufsize_callback;
            fBufferSizeArg = arg;
            return 0;
        }
    }
    
    int SetSampleRateCallback(JackNetSlaveSampleRateCallback samplerate_callback, void *arg)
    {
        if (fThread.GetStatus() == JackThread::kRunning) {
            return -1;
        } else {
            fSampleRateCallback = samplerate_callback;
            fSampleRateArg = arg;
            return 0;
        }
    }

};

struct JackNetAdapter : public JackAudioAdapterInterface {


    JackNetAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate)
        :JackAudioAdapterInterface(buffer_size, sample_rate)
    {
        fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
        fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
        
        /*
        for (i = 0; i < fCaptureChannels; i++)
            fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fAudioAdapter->GetQuality());
        for (i = 0; i < fPlaybackChannels; i++)
            fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fAudioAdapter->GetQuality());
        */
        
        int i;
        for (i = 0; i < fCaptureChannels; i++)
            fCaptureRingBuffer[i] = new JackResampler();
        for (i = 0; i < fPlaybackChannels; i++)
            fPlaybackRingBuffer[i] = new JackResampler();
    }

    virtual ~JackNetAdapter()
    {
        int i;
        for (i = 0; i < fCaptureChannels; i++)
            delete (fCaptureRingBuffer[i]);
        for (i = 0; i < fPlaybackChannels; i++)
            delete(fPlaybackRingBuffer[i] );

        delete[] fCaptureRingBuffer;
        delete[] fPlaybackRingBuffer;
    }
     
    void Reset()
    {
        int i;
        for (i = 0; i < fCaptureChannels; i++)
            fCaptureRingBuffer[i]->Reset();
        for (i = 0; i < fPlaybackChannels; i++)
            fPlaybackRingBuffer[i]->Reset();
     }
   
    int PushInput(int audio_input, float** audio_input_buffer)
    {
        bool failure = false;
        int port_index;
        
        // Get the resample factor,
        jack_nframes_t time1, time2;
        ResampleFactor(time1, time2);

        // Resample input data,
        for (port_index = 0; port_index < audio_input; port_index++) {
            fCaptureRingBuffer[port_index]->SetRatio(time1, time2);
            if (fCaptureRingBuffer[port_index]->WriteResample(audio_input_buffer[port_index], fAdaptedBufferSize) < fAdaptedBufferSize)
                failure = true;
        }
        
        if (failure) {
            ResetRingBuffers();
            return -1;
        }
        
        return 0;
    }
    
    int PullInput(int audio_input, float** audio_input_buffer)
    {
       bool failure = false;
       int port_index;
        
        // DLL
        SetCallbackTime(GetMicroSeconds());

        // Push/pull from ringbuffer
        for (port_index = 0; port_index < audio_input; port_index++) {
            if (fCaptureRingBuffer[port_index]->Read(audio_input_buffer[port_index], fHostBufferSize) < fHostBufferSize)
                failure = true;
        }
        
        // Reset all ringbuffers in case of failure
        if (failure) {
            Reset();
            return -1;
        }
        
        return 0;
    }
    
    int PushOutput(int audio_input, float** audio_input_buffer)
    {
       bool failure = false;
       int port_index;
        
        // DLL
        SetCallbackTime(GetMicroSeconds());

        // Push/pull from ringbuffer
        for (port_index = 0; port_index < audio_input; port_index++) {
            if (fPlaybackRingBuffer[port_index]->Write(audio_input_buffer[port_index], fHostBufferSize) < fHostBufferSize)
                failure = true;
        }
        
        // Reset all ringbuffers in case of failure
        if (failure) {
            Reset();
            return -1;
        }
        
        return 0;
    }

    int PullOutput(int audio_output, float** audio_output_buffer)
    {
        bool failure = false;
        int port_index;

        //get the resample factor,
        jack_nframes_t time1, time2;
        ResampleFactor(time1, time2);

        //resample output data,
        for (port_index = 0; port_index < fPlaybackChannels; port_index++) {
            fPlaybackRingBuffer[port_index]->SetRatio(time2, time1);
            if (fPlaybackRingBuffer[port_index]->ReadResample(audio_output_buffer[port_index], fAdaptedBufferSize) < fAdaptedBufferSize)
                failure = true;
        }
        
        if (failure) {
            ResetRingBuffers();
            return -1;
        }
        
        return 0;
    }

};


} // end of namespace

using namespace Jack;

SERVER_EXPORT jack_net_slave_t* jack_net_slave_open(const char* ip, int port, const char* name, jack_slave_t* request, jack_master_t* result)
{
    JackNetExtSlave* slave = new JackNetExtSlave(ip, port, name, request);    
    if (slave->Open(result) == 0) {
        return (jack_net_slave_t*)slave;
    } else {
        delete slave;
        return NULL;
    }
}

SERVER_EXPORT int jack_net_slave_close(jack_net_slave_t* net)
{
    JackNetExtSlave* slave = (JackNetExtSlave*)net;
    slave->Close();
    delete slave;
    return 0;
}
    
SERVER_EXPORT int jack_set_net_slave_process_callback(jack_net_slave_t* net, JackNetSlaveProcessCallback net_callback, void *arg)
{
     JackNetExtSlave* slave = (JackNetExtSlave*)net;
     return slave->SetProcessCallback(net_callback, arg);
}

SERVER_EXPORT int jack_net_slave_activate(jack_net_slave_t* net)
{
    JackNetExtSlave* slave = (JackNetExtSlave*)net;
    return slave->Start();
}

SERVER_EXPORT int jack_net_slave_deactivate(jack_net_slave_t* net)
{
    JackNetExtSlave* slave = (JackNetExtSlave*)net;
    return slave->Stop();
}

SERVER_EXPORT int jack_set_net_slave_buffer_size_callback(jack_net_slave_t *net, JackNetSlaveBufferSizeCallback bufsize_callback, void *arg)
{
    JackNetExtSlave* slave = (JackNetExtSlave*)net;
    return slave->SetBufferSizeCallback(bufsize_callback, arg);
}

SERVER_EXPORT int jack_set_net_slave_sample_rate_callback(jack_net_slave_t *net, JackNetSlaveSampleRateCallback samplerate_callback, void *arg)
{
    JackNetExtSlave* slave = (JackNetExtSlave*)net;
    return slave->SetSampleRateCallback(samplerate_callback, arg);
}

SERVER_EXPORT int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg)
{
    JackNetExtSlave* slave = (JackNetExtSlave*)net;
    return slave->SetShutdownCallback(shutdown_callback, arg);
}

// Master API

SERVER_EXPORT jack_net_master_t* jack_net_master_open(const char* ip, int port, const char* name, jack_master_t* request, jack_slave_t* result)
{
    JackNetExtMaster* master = new JackNetExtMaster(ip, port, name, request);    
    if (master->Open(result) == 0) {
        return (jack_net_master_t*)master;
    } else {
        delete master;
        return NULL;
    }
}

SERVER_EXPORT int jack_net_master_close(jack_net_master_t* net)
{
    JackNetExtMaster* master = (JackNetExtMaster*)net;
    master->Close();
    delete master;
    return 0;
}
SERVER_EXPORT int jack_net_master_recv(jack_net_master_t* net, int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer)
{
    JackNetExtMaster* slave = (JackNetExtMaster*)net;
    return slave->Read(audio_input, audio_input_buffer, midi_input, midi_input_buffer);
}

SERVER_EXPORT int jack_net_master_send(jack_net_master_t* net, int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer)
{
    JackNetExtMaster* slave = (JackNetExtMaster*)net;
    return slave->Write(audio_output, audio_output_buffer, midi_output, midi_output_buffer);
}

// Adapter API

SERVER_EXPORT jack_adapter_t* jack_create_adapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate)
{
    return (jack_adapter_t*)new JackNetAdapter(buffer_size, sample_rate);
}

SERVER_EXPORT int jack_destroy_adapter(jack_adapter_t* adapter)
{
    delete((JackNetAdapter*)adapter);
    return 0;
}

SERVER_EXPORT int jack_adapter_push_input(jack_adapter_t * adapter, int channels, float** buffers)
{
    JackNetAdapter* slave = (JackNetAdapter*)adapter;
    return slave->PushInput(channels, buffers);
}

SERVER_EXPORT int jack_adapter_pull_input(jack_adapter_t * adapter, int channels, float** buffers)
{
    JackNetAdapter* slave = (JackNetAdapter*)adapter;
    return slave->PullInput(channels, buffers);
}
    
SERVER_EXPORT int jack_adapter_push_output(jack_adapter_t * adapter, int channels, float** buffers)
{
    JackNetAdapter* slave = (JackNetAdapter*)adapter;
    return slave->PushOutput(channels, buffers);
}

SERVER_EXPORT int jack_adapter_pull_output(jack_adapter_t * adapter, int channels, float** buffers)
{
    JackNetAdapter* slave = (JackNetAdapter*)adapter;
    return slave->PullOutput(channels, buffers);
}
    

For faster browsing, not all history is shown. View entire blame