JackNetTool.h 24.4 KB
Newer Older
1
/*
sletz's avatar
sletz committed
2
Copyright(C) 2008 Romain Moret at Grame
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

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.

*/
sletz's avatar
sletz committed
19
20

#include "JackMidiPort.h"
21
#include "JackTools.h"
22
#include "JackPlatformPlug.h"
23
#include "types.h"
24
#include "transport.h"
sletz's avatar
sletz committed
25
#ifndef WIN32
26
#include <netinet/in.h>
sletz's avatar
sletz committed
27
#endif
sletz's avatar
sletz committed
28
29
#include <cmath>

moret's avatar
moret committed
30
31
using namespace std;

sletz's avatar
sletz committed
32
33
34
35
36
37
38
39
40
41
#ifndef htonll
#ifdef __BIG_ENDIAN__
#define htonll(x)   (x)
#define ntohll(x)   (x)
#else
#define htonll(x)   ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
#define ntohll(x)   ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
#endif
#endif

sletz's avatar
sletz committed
42
43
namespace Jack
{
44
45
    typedef struct _session_params session_params_t;
    typedef struct _packet_header packet_header_t;
46
    typedef struct _net_transport_data net_transport_data_t;
47
48
49
    typedef struct sockaddr socket_address_t;
    typedef struct in_addr address_t;
    typedef jack_default_audio_sample_t sample_t;
sletz's avatar
sletz committed
50

51
52
53
54
55
56
    enum JackNetEncoder {

        JackFloatEncoder = 0,
        JackIntEncoder = 1,
        JackCeltEncoder = 2,
    };
sletz's avatar
sletz committed
57
58
59

//session params ******************************************************************************

moret's avatar
moret committed
60
61
62
63
64
65
66
67
68
69
70
71
    /**
    \brief This structure containes master/slave connection parameters, it's used to setup the whole system

    We have :
        - some info like version, type and packet id
        - names
        - network parameters (hostnames and mtu)
        - nunber of audio and midi channels
        - sample rate and buffersize
        - number of audio frames in one network packet (depends on the channel number)
        - is the NetDriver in Sync or ASync mode ?
        - is the NetDriver linked with the master's transport
sletz's avatar
sletz committed
72

73
    Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
sletz's avatar
sletz committed
74
    are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
moret's avatar
moret committed
75
    */
sletz's avatar
sletz committed
76

sletz's avatar
sletz committed
77
78
    #define MASTER_PROTOCOL 2
    #define SLAVE_PROTOCOL 2
79

80
81
    struct _session_params
    {
moret's avatar
moret committed
82
83
84
85
86
87
88
89
90
        char fPacketType[7];                //packet type ('param')
        char fProtocolVersion;              //version
        uint32_t fPacketID;                 //indicates the packet type
        char fName[JACK_CLIENT_NAME_SIZE];  //slave's name
        char fMasterNetName[256];           //master hostname (network)
        char fSlaveNetName[256];            //slave hostname (network)
        uint32_t fMtu;                      //connection mtu
        uint32_t fID;                       //slave's ID
        uint32_t fTransportSync;            //is the transport synced ?
sletz's avatar
sletz committed
91
92
93
94
        int32_t fSendAudioChannels;         //number of master->slave channels
        int32_t fReturnAudioChannels;       //number of slave->master channels
        int32_t fSendMidiChannels;          //number of master->slave midi channels
        int32_t fReturnMidiChannels;        //number of slave->master midi channels
moret's avatar
moret committed
95
96
        uint32_t fSampleRate;               //session sample rate
        uint32_t fPeriodSize;               //period size
97
        uint32_t fSampleEncoder;            //samples encoder
sletz's avatar
sletz committed
98
        uint32_t fKBps;                     //KB per second for CELT encoder
moret's avatar
moret committed
99
        uint32_t fSlaveSyncMode;            //is the slave in sync mode ?
100
        char fNetworkMode;                  //fast, normal or slow mode
101
    };
sletz's avatar
sletz committed
102
103
104

//net status **********************************************************************************

moret's avatar
moret committed
105
106
107
    /**
    \Brief This enum groups network error by type
    */
108

109
110
111
112
113
114
115
116
117
118
    enum  _net_status
    {
        NET_SOCKET_ERROR = 0,
        NET_CONNECT_ERROR,
        NET_ERROR,
        NET_SEND_ERROR,
        NET_RECV_ERROR,
        NET_CONNECTED,
        NET_ROLLING
    };
sletz's avatar
sletz committed
119

120
    typedef enum _net_status net_status_t;
sletz's avatar
sletz committed
121
122
123

//sync packet type ****************************************************************************

moret's avatar
moret committed
124
125
126
    /**
    \Brief This enum indicates the type of a sync packet (used in the initialization phase)
    */
127

128
129
    enum _sync_packet_type
    {
130
131
132
133
134
135
        INVALID = 0,        //...
        SLAVE_AVAILABLE,    //a slave is available
        SLAVE_SETUP,        //slave configuration
        START_MASTER,       //slave is ready, start master
        START_SLAVE,        //master is ready, activate slave
        KILL_MASTER         //master must stop
136
    };
sletz's avatar
sletz committed
137

138
    typedef enum _sync_packet_type sync_packet_type_t;
sletz's avatar
sletz committed
139
140
141
142


//packet header *******************************************************************************

moret's avatar
moret committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    /**
    \Brief This structure is a complete header

    A header indicates :
        - it is a header
        - the type of data the packet contains (sync, midi or audio)
        - the path of the packet (send -master->slave- or return -slave->master-)
        - the unique ID of the slave
        - the sample's bitdepth (unused for now)
        - the size of the midi data contains in the packet (indicates how much midi data will be sent)
        - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
        - the ID of the current cycle (used to check missing packets)
        - the ID of the packet subcycle (for audio data)
        - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
        - a flag indicating if, in async mode, the previous graph was not finished or not
        - padding to fill 64 bytes

    */
161

162
163
    struct _packet_header
    {
sletz's avatar
sletz committed
164
        char fPacketType[7];        //packet type ('headr')
165
        char fDataType;             //a for audio, m for midi and s for sync
166
        char fDataStream;           //s for send, r for return
167
        uint32_t fID;               //unique ID of the slave
sletz's avatar
sletz committed
168
        uint32_t fNumPacket;        //number of data packets of the cycle
moret's avatar
moret committed
169
        uint32_t fPacketSize;       //packet size in bytes
170
171
        uint32_t fCycle;            //process cycle counter
        uint32_t fSubCycle;         //midi/audio subcycle counter
moret's avatar
moret committed
172
        uint32_t fIsLastPckt;       //is it the last packet of a given cycle ('y' or 'n')
173
    };
sletz's avatar
sletz committed
174

175
176
177
178
179
180
181
182
//net timebase master

    /**
    \Brief This enum describes timebase master's type
    */

    enum _net_timebase_master
    {
183
184
185
186
        NO_CHANGE = 0,
        RELEASE_TIMEBASEMASTER = 1,
        TIMEBASEMASTER = 2,
        CONDITIONAL_TIMEBASEMASTER = 3
187
188
189
190
191
    };

    typedef enum _net_timebase_master net_timebase_master_t;


192
193
//transport data ******************************************************************************

moret's avatar
moret committed
194
    /**
195
    \Brief This structure contains transport data to be sent over the network
moret's avatar
moret committed
196
    */
197

moret's avatar
moret committed
198
199
    struct _net_transport_data
    {
200
201
        uint32_t fNewState;             //is it a state change
        uint32_t fTimebaseMaster;       //is there a new timebase master
moret's avatar
moret committed
202
        int32_t fState;                 //current cycle state
203
        jack_position_t fPosition;      //current cycle position
moret's avatar
moret committed
204
    };
sletz's avatar
sletz committed
205

sletz's avatar
sletz committed
206
 //midi data ***********************************************************************************
sletz's avatar
sletz committed
207

moret's avatar
moret committed
208
209
    /**
    \Brief Midi buffer and operations class
210

moret's avatar
moret committed
211
212
213
214
215
216
217
218
219
    This class is a toolset to manipulate Midi buffers.
    A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
    An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
    But for a big majority of the process cycles, this buffer is filled less than 1%,
    Sending over a network 99% of useless data seems completely unappropriate.
    The idea is to count effective midi data, and then send the smallest packet we can.
    To do it, we use an intermediate buffer.
    We have two methods to convert data from jack ports to intermediate buffer,
    And two others to convert this intermediate buffer to a network buffer (header + payload data)
220

moret's avatar
moret committed
221
    */
222

223
    class SERVER_EXPORT NetMidiBuffer
224
    {
225
        private:
sletz's avatar
sletz committed
226

227
228
229
230
231
232
            int fNPorts;
            size_t fMaxBufsize;
            int fMaxPcktSize;
            char* fBuffer;
            char* fNetBuffer;
            JackMidiBuffer** fPortBuffer;
sletz's avatar
sletz committed
233

sletz's avatar
sletz committed
234
            size_t fCycleSize;  // needed size in bytes ofr an entire cycle
235
236

        public:
sletz's avatar
sletz committed
237

sletz's avatar
sletz committed
238
            NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
239
240
241
            ~NetMidiBuffer();

            void Reset();
sletz's avatar
sletz committed
242

sletz's avatar
sletz committed
243
244
            // needed size in bytes for an entire cycle
            size_t GetCycleSize();
sletz's avatar
sletz committed
245

sletz's avatar
sletz committed
246
            int GetNumPackets(int data_sizen, int max_size);
sletz's avatar
sletz committed
247

248
249
            //utility
            void DisplayEvents();
sletz's avatar
sletz committed
250

251
252
253
            //jack<->buffer
            int RenderFromJackPorts();
            int RenderToJackPorts();
sletz's avatar
sletz committed
254

255
            //network<->buffer
sletz's avatar
sletz committed
256
257
            int RenderFromNetwork(int subcycle, size_t copy_size);
            int RenderToNetwork(int subcycle, size_t total_size);
258

sletz's avatar
sletz committed
259
260
            void SetBuffer(int index, JackMidiBuffer* buffer);
            JackMidiBuffer* GetBuffer(int index);
261
    };
sletz's avatar
sletz committed
262
263
264

// audio data *********************************************************************************

sletz's avatar
sletz committed
265
    class SERVER_EXPORT NetAudioBuffer
266
    {
sletz's avatar
sletz committed
267

268
        public:
sletz's avatar
sletz committed
269

sletz's avatar
sletz committed
270
            NetAudioBuffer()
271
            {}
sletz's avatar
sletz committed
272
            virtual ~NetAudioBuffer()
273
            {}
sletz's avatar
sletz committed
274

sletz's avatar
sletz committed
275
276
            // needed syze in bytes ofr an entire cycle
            virtual size_t GetCycleSize() = 0;
sletz's avatar
sletz committed
277

sletz's avatar
sletz committed
278
279
            // cycle duration in sec
            virtual float GetCycleDuration() = 0;
sletz's avatar
sletz committed
280

sletz's avatar
sletz committed
281
            virtual int GetNumPackets() = 0;
sletz's avatar
sletz committed
282

283
            //jack<->buffer
sletz's avatar
sletz committed
284
285
            virtual int RenderFromJackPorts() = 0;
            virtual int RenderToJackPorts() = 0;
sletz's avatar
sletz committed
286

sletz's avatar
sletz committed
287
            //network<->buffer
sletz's avatar
sletz committed
288
289
            virtual int RenderFromNetwork(int cycle, int subcycle, size_t copy_size) = 0;
            virtual int RenderToNetwork(int subcycle, size_t total_size) = 0;
290

sletz's avatar
sletz committed
291
292
            virtual void SetBuffer(int index, sample_t* buffer) = 0;
            virtual sample_t* GetBuffer(int index) = 0;
293
294
   };

moret's avatar
moret committed
295
296
    /**
    \Brief Audio buffer and operations class
297

moret's avatar
moret committed
298
299
300
301
    This class is a toolset to manipulate audio buffers.
    The manipulation of audio buffers is similar to midi buffer, except those buffers have fixed size.
    The interleaving/uninterleaving operations are simplier here because audio buffers have fixed size,
    So there is no need of an intermediate buffer as in NetMidiBuffer.
302

moret's avatar
moret committed
303
    */
sletz's avatar
sletz committed
304

305
    struct JackPortList {
sletz's avatar
sletz committed
306

307
308
309
310
311
        jack_nframes_t fPeriodSize;
        jack_nframes_t fSubPeriodSize;
        size_t fSubPeriodBytesSize;
        sample_t** fPortBuffer;
        int fNPorts;
sletz's avatar
sletz committed
312
        size_t fCycleSize; // needed size in bytes for an entire cycle
sletz's avatar
sletz committed
313
        float fCycleDuration; // in sec
sletz's avatar
sletz committed
314

sletz's avatar
sletz committed
315
        int fLastSubCycle;
sletz's avatar
sletz committed
316

317
318
319
320
        JackPortList(session_params_t* params, uint32_t nports)
        {
            fNPorts = nports;
            fPeriodSize = params->fPeriodSize;
sletz's avatar
sletz committed
321

sletz's avatar
sletz committed
322
            if (params->fSendAudioChannels == 0 && params->fReturnAudioChannels == 0) {
sletz's avatar
sletz committed
323
324
                fSubPeriodSize = params->fPeriodSize;
            } else {
sletz's avatar
sletz committed
325
                jack_nframes_t period = (int) powf(2.f,(int)(log(float((params->fMtu - sizeof(packet_header_t)))
sletz's avatar
sletz committed
326
327
                                                   / (max(params->fReturnAudioChannels, params->fSendAudioChannels) * sizeof(sample_t))) / log(2.)));
                fSubPeriodSize = (period > params->fPeriodSize) ? params->fPeriodSize : period;
sletz's avatar
sletz committed
328
            }
sletz's avatar
sletz committed
329

sletz's avatar
sletz committed
330
            fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
sletz's avatar
sletz committed
331

332
            fPortBuffer = new sample_t* [fNPorts];
sletz's avatar
sletz committed
333
            for (int port_index = 0; port_index < fNPorts; port_index++)
334
                fPortBuffer[port_index] = NULL;
sletz's avatar
sletz committed
335

sletz's avatar
sletz committed
336
            fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
sletz's avatar
sletz committed
337
            fCycleSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
sletz's avatar
sletz committed
338

sletz's avatar
sletz committed
339
340
            fLastSubCycle = -1;
        }
sletz's avatar
sletz committed
341

sletz's avatar
sletz committed
342
343
344
        int GetNumPackets()
        {
            return fPeriodSize / fSubPeriodSize;
345
        }
sletz's avatar
sletz committed
346

347
348
349
350
351
352
353
354
        JackPortList()
        {
            fNPorts = 0;
            fPeriodSize = 0;
            fSubPeriodSize = 0;
            fSubPeriodBytesSize = 0;
            fPortBuffer = 0;
        }
sletz's avatar
sletz committed
355

356
357
358
359
        ~JackPortList()
        {
            delete [] fPortBuffer;
        }
sletz's avatar
sletz committed
360

sletz's avatar
sletz committed
361
        void SetBuffer(int index, sample_t* buffer)
362
363
364
        {
            fPortBuffer[index] = buffer;
        }
sletz's avatar
sletz committed
365

sletz's avatar
sletz committed
366
        sample_t* GetBuffer(int index)
367
368
369
        {
            return fPortBuffer[index];
        }
sletz's avatar
sletz committed
370

371
372
        void Copy(sample_t** buffers)
        {
sletz's avatar
sletz committed
373
            for (int port_index = 0; port_index < fNPorts; port_index++)
374
375
                memcpy(buffers[port_index], fPortBuffer[port_index], fPeriodSize * sizeof(float));
        }
sletz's avatar
sletz committed
376

sletz's avatar
sletz committed
377
378
379
380
381
        // needed syze in bytes ofr an entire cycle
        size_t GetCycleSize()
        {
            return fCycleSize;
        }
sletz's avatar
sletz committed
382

sletz's avatar
sletz committed
383
384
385
386
387
        // cycle duration in sec
        float GetCycleDuration()
        {
            return fCycleDuration;
        }
sletz's avatar
sletz committed
388

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    #ifdef __BIG_ENDIAN__

        static inline float SwapFloat(float f)
        {
              union
              {
                float f;
                unsigned char b[4];
              } dat1, dat2;

              dat1.f = f;
              dat2.b[0] = dat1.b[3];
              dat2.b[1] = dat1.b[2];
              dat2.b[2] = dat1.b[1];
              dat2.b[3] = dat1.b[0];
              return dat2.f;
        }

sletz's avatar
sletz committed
407
        int RenderFromJackPorts()
408
        {
sletz's avatar
sletz committed
409
            return fNPorts * fSubPeriodBytesSize;  // in bytes
410
411
        }

sletz's avatar
sletz committed
412
        int RenderToJackPorts()
413
        {
sletz's avatar
sletz committed
414
415
            return fPeriodSize * sizeof(sample_t);  // in bytes TODO
        }
sletz's avatar
sletz committed
416

sletz's avatar
sletz committed
417
        //network<->buffer
sletz's avatar
sletz committed
418
        int RenderFromNetwork(char* net_buffer, int cycle,  int subcycle, size_t copy_size)
sletz's avatar
sletz committed
419
        {
420
            for (int port_index = 0; port_index < fNPorts; port_index++) {
421
422
423
424
425
                float* src = (float*)(net_buffer + port_index * fSubPeriodBytesSize);
                float* dst = (float*)(fPortBuffer[port_index] + subcycle * fSubPeriodSize);
                for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
                    dst[sample] = SwapFloat(src[sample]);
                }
sletz's avatar
sletz committed
426
            }
427
428
429
430
            if (subcycle != fLastSubCycle + 1) {
                jack_error("Packet(s) missing from... %d %d", fLastSubCycle, subcycle);
            }
            fLastSubCycle = subcycle;
sletz's avatar
sletz committed
431
432
            return copy_size;
        }
sletz's avatar
sletz committed
433

sletz's avatar
sletz committed
434
        int RenderToNetwork(char* net_buffer, int subcycle, size_t total_size)
sletz's avatar
sletz committed
435
        {
436
            for (int port_index = 0; port_index < fNPorts; port_index++) {
sletz's avatar
sletz committed
437
438
439
440
441
442
                float* src = (float*)(fPortBuffer[port_index] + subcycle * fSubPeriodSize);
                float* dst = (float*)(net_buffer + port_index * fSubPeriodBytesSize);
                for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
                    dst[sample] = SwapFloat(src[sample]);
                }
            }
sletz's avatar
sletz committed
443
444

            return fNPorts * fSubPeriodBytesSize;
445
        }
sletz's avatar
sletz committed
446

447
448
    #else

449
        int RenderFromJackPorts()
450
        {
sletz's avatar
sletz committed
451
            return fNPorts * fSubPeriodBytesSize;  // in bytes
452
453
        }

454
        int RenderToJackPorts()
sletz's avatar
sletz committed
455
456
457
458
        {
            fLastSubCycle = -1;
            return fPeriodSize * sizeof(sample_t);  // in bytes; TODO
        }
sletz's avatar
sletz committed
459

sletz's avatar
sletz committed
460
         //network<->buffer
sletz's avatar
sletz committed
461
        int RenderFromNetwork(char* net_buffer, int cycle,  int subcycle, size_t copy_size)
462
        {
sletz's avatar
sletz committed
463
464
            for (int port_index = 0; port_index < fNPorts; port_index++)
                memcpy(fPortBuffer[port_index] + subcycle * fSubPeriodSize, net_buffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
sletz's avatar
sletz committed
465
466
467
468
469
470
            if (subcycle != fLastSubCycle + 1) {
                jack_error("Packet(s) missing from... %d %d", fLastSubCycle, subcycle);
            }
            fLastSubCycle = subcycle;
            return copy_size;
        }
sletz's avatar
sletz committed
471

sletz's avatar
sletz committed
472
        int RenderToNetwork(char* net_buffer,int subcycle, size_t total_size)
sletz's avatar
sletz committed
473
        {
sletz's avatar
sletz committed
474
475
            for (int port_index = 0; port_index < fNPorts; port_index++)
                memcpy(net_buffer + port_index * fSubPeriodBytesSize, fPortBuffer[port_index] + subcycle * fSubPeriodSize, fSubPeriodBytesSize);
sletz's avatar
sletz committed
476
477

            return fNPorts * fSubPeriodBytesSize;
478
479
480
        }

    #endif
sletz's avatar
sletz committed
481

482
    };
sletz's avatar
sletz committed
483

484
    struct JackPortListAllocate : public JackPortList {
sletz's avatar
sletz committed
485

486
487
488
489
490
491
492
493
        JackPortListAllocate()
        {
            fNPorts = 0;
            fPeriodSize = 0;
            fSubPeriodSize = 0;
            fSubPeriodBytesSize = 0;
            fPortBuffer = 0;
        }
sletz's avatar
sletz committed
494

495
496
        ~JackPortListAllocate()
        {
sletz's avatar
sletz committed
497
            for (int port_index = 0; port_index < fNPorts; port_index++)
498
499
500
                delete [] fPortBuffer[port_index];
            delete [] fPortBuffer;
        }
sletz's avatar
sletz committed
501

502
503
504
505
        void Init(session_params_t* params, uint32_t nports)
        {
            fNPorts = nports;
            fPeriodSize = params->fPeriodSize;
sletz's avatar
sletz committed
506

sletz's avatar
sletz committed
507
508
509
            if (params->fSendAudioChannels == 0 && params->fReturnAudioChannels == 0) {
                fSubPeriodSize = params->fPeriodSize;
            } else {
sletz's avatar
sletz committed
510
511
512
                jack_nframes_t period = (int) powf(2.f, (int)(log(float((params->fMtu - sizeof(packet_header_t)))
                                                   / (max(params->fReturnAudioChannels, params->fSendAudioChannels) * sizeof(sample_t))) / log(2.)));
                fSubPeriodSize = (period > params->fPeriodSize) ? params->fPeriodSize : period;
sletz's avatar
sletz committed
513
            }
sletz's avatar
sletz committed
514

sletz's avatar
sletz committed
515
            fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
516
            fPortBuffer = new sample_t* [fNPorts];
sletz's avatar
sletz committed
517
            for (int port_index = 0; port_index < fNPorts; port_index++)
518
519
                fPortBuffer[port_index] = new sample_t[fPeriodSize];
        }
sletz's avatar
sletz committed
520

521
    };
522

sletz's avatar
sletz committed
523
    class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
524
    {
525
        private:
sletz's avatar
sletz committed
526

527
528
            JackPortList fPortBuffer;
            char* fNetBuffer;
sletz's avatar
sletz committed
529

530
        public:
sletz's avatar
sletz committed
531

sletz's avatar
sletz committed
532
            NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
sletz's avatar
sletz committed
533
            ~NetFloatAudioBuffer();
sletz's avatar
sletz committed
534

sletz's avatar
sletz committed
535
536
            // needed size in bytes for an entire cycle
            size_t GetCycleSize();
sletz's avatar
sletz committed
537

sletz's avatar
sletz committed
538
539
540
541
542
             // cycle duration in sec
            float GetCycleDuration()
            {
                return fPortBuffer.GetCycleDuration();
            }
sletz's avatar
sletz committed
543

sletz's avatar
sletz committed
544
545
546
547
            int GetNumPackets()
            {
                return fPortBuffer.GetNumPackets();
            }
sletz's avatar
sletz committed
548

549
            //jack<->buffer
sletz's avatar
sletz committed
550
551
            int RenderFromJackPorts();
            int RenderToJackPorts();
552

sletz's avatar
sletz committed
553
554
            void SetBuffer(int index, sample_t* buffer);
            sample_t* GetBuffer(int index);
sletz's avatar
sletz committed
555

sletz's avatar
sletz committed
556
            //network<->buffer
sletz's avatar
sletz committed
557
558
            int RenderFromNetwork(int cycle, int subcycle, size_t copy_size);
            int RenderToNetwork(int subcycle, size_t total_size);
559
    };
sletz's avatar
sletz committed
560

nedko's avatar
nedko committed
561
#if HAVE_CELT
sletz's avatar
sletz committed
562
563
564
565
566
567

#include <celt/celt.h>

    class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
    {
        private:
sletz's avatar
sletz committed
568

sletz's avatar
sletz committed
569
570
571
            CELTMode ** fCeltMode;
            CELTEncoder ** fCeltEncoder;
            CELTDecoder ** fCeltDecoder;
sletz's avatar
sletz committed
572

sletz's avatar
sletz committed
573
574
575
576
577
            int fCompressedSizeByte;
            jack_nframes_t fPeriodSize;
            int fNumPackets;
            float fCycleDuration; // in sec
            size_t fCycleSize; // needed size in bytes for an entire cycle
sletz's avatar
sletz committed
578

sletz's avatar
sletz committed
579
580
            size_t fSubPeriodBytesSize;
            size_t fLastSubPeriodBytesSize;
sletz's avatar
sletz committed
581

sletz's avatar
sletz committed
582
583
584
            sample_t** fPortBuffer;
            char* fNetBuffer;
            unsigned char ** fCompressedBuffer;
sletz's avatar
sletz committed
585

sletz's avatar
sletz committed
586
            int fNPorts;
sletz's avatar
sletz committed
587

sletz's avatar
sletz committed
588
            int fLastSubCycle;
sletz's avatar
sletz committed
589

sletz's avatar
sletz committed
590
            void FreeCelt();
sletz's avatar
sletz committed
591

sletz's avatar
sletz committed
592
        public:
sletz's avatar
sletz committed
593

594
            NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
sletz's avatar
sletz committed
595
            ~NetCeltAudioBuffer();
sletz's avatar
sletz committed
596

sletz's avatar
sletz committed
597
598
            // needed size in bytes for an entire cycle
            size_t GetCycleSize();
sletz's avatar
sletz committed
599

sletz's avatar
sletz committed
600
601
             // cycle duration in sec
            float GetCycleDuration();
sletz's avatar
sletz committed
602

sletz's avatar
sletz committed
603
            int GetNumPackets();
sletz's avatar
sletz committed
604

sletz's avatar
sletz committed
605
606
            void SetBuffer(int index, sample_t* buffer);
            sample_t* GetBuffer(int index);
sletz's avatar
sletz committed
607

sletz's avatar
sletz committed
608
609
610
611
612
613
614
615
616
617
            //jack<->buffer
            int RenderFromJackPorts();
            int RenderToJackPorts();

            //network<->buffer
            int RenderFromNetwork(int cycle, int subcycle, size_t copy_size);
            int RenderToNetwork(int subcycle, size_t total_size);
    };

#endif
sletz's avatar
sletz committed
618
619
620
621

    class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
    {
        private:
sletz's avatar
sletz committed
622

sletz's avatar
sletz committed
623
624
            int fCompressedSizeByte;
            jack_nframes_t fPeriodSize;
sletz's avatar
sletz committed
625

sletz's avatar
sletz committed
626
627
628
            int fNumPackets;
            float fCycleDuration; // in sec
            size_t fCycleSize; // needed size in bytes for an entire cycle
sletz's avatar
sletz committed
629

sletz's avatar
sletz committed
630
631
632
633
            size_t fSubPeriodSize;
            size_t fSubPeriodBytesSize;
            size_t fLastSubPeriodSize;;
            size_t fLastSubPeriodBytesSize;
sletz's avatar
sletz committed
634

sletz's avatar
sletz committed
635
636
637
            sample_t** fPortBuffer;
            char* fNetBuffer;
            short ** fIntBuffer;
sletz's avatar
sletz committed
638

sletz's avatar
sletz committed
639
            int fNPorts;
sletz's avatar
sletz committed
640

sletz's avatar
sletz committed
641
            int fLastSubCycle;
sletz's avatar
sletz committed
642

sletz's avatar
sletz committed
643
        public:
sletz's avatar
sletz committed
644

sletz's avatar
sletz committed
645
646
            NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
            ~NetIntAudioBuffer();
sletz's avatar
sletz committed
647

sletz's avatar
sletz committed
648
649
            // needed size in bytes for an entire cycle
            size_t GetCycleSize();
sletz's avatar
sletz committed
650

sletz's avatar
sletz committed
651
652
             // cycle duration in sec
            float GetCycleDuration();
sletz's avatar
sletz committed
653

sletz's avatar
sletz committed
654
            int GetNumPackets();
sletz's avatar
sletz committed
655

sletz's avatar
sletz committed
656
657
            void SetBuffer(int index, sample_t* buffer);
            sample_t* GetBuffer(int index);
sletz's avatar
sletz committed
658

sletz's avatar
sletz committed
659
660
661
662
663
664
665
666
667
            //jack<->buffer
            int RenderFromJackPorts();
            int RenderToJackPorts();

            //network<->buffer
            int RenderFromNetwork(int cycle, int subcycle, size_t copy_size);
            int RenderToNetwork(int subcycle, size_t total_size);
    };

sletz's avatar
sletz committed
668

669
    #define AUDIO_BUFFER_SIZE 8
sletz's avatar
sletz committed
670

sletz's avatar
sletz committed
671
    /*
672
673
    class SERVER_EXPORT NetBufferedAudioBuffer : public NetAudioBuffer
    {
sletz's avatar
sletz committed
674

675
        private:
676
            char* fNetBuffer;
677
678
679
            JackPortListAllocate fPortBuffer[AUDIO_BUFFER_SIZE];
            sample_t** fJackPortBuffer;
            int fMaxCycle;
sletz's avatar
sletz committed
680

681
        public:
sletz's avatar
sletz committed
682
            NetBufferedAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
683
            ~NetBufferedAudioBuffer();
684

sletz's avatar
sletz committed
685
686
            // needed syze in bytes ofr an entire cycle
            size_t GetCycleSize();
sletz's avatar
sletz committed
687

sletz's avatar
sletz committed
688
689
690
691
692
             // cycle duration in sec
            float GetCycleDuration()
            {
                return fPortBuffer[0].GetCycleDuration();
            }
sletz's avatar
sletz committed
693

694
            //jack<->buffer
sletz's avatar
sletz committed
695
696
697
            int RenderFromJackPorts(int subcycle);
            int RenderToJackPorts(int cycle, int subcycle);
            //void FinishRenderToJackPorts(int cycle);
sletz's avatar
sletz committed
698

sletz's avatar
sletz committed
699
            //network<->buffer
sletz's avatar
sletz committed
700
            int RenderFromNetwork(int subcycle, size_t copy_size)
sletz's avatar
sletz committed
701
            {
sletz's avatar
sletz committed
702
703
704
                // TODO
                return 0;
            }
sletz's avatar
sletz committed
705
            int RenderToNetwork(int subcycle, size_t total_size)
sletz's avatar
sletz committed
706
            {
sletz's avatar
sletz committed
707
708
709
                // TODO
                return 0;
            }
710

sletz's avatar
sletz committed
711
712
            void SetBuffer(int index, sample_t* buffer);
            sample_t* GetBuffer(int index);
713
    };
sletz's avatar
sletz committed
714
    */
sletz's avatar
sletz committed
715
716

//utility *************************************************************************************
717
718

    //socket API management
719
720
    SERVER_EXPORT int SocketAPIInit();
    SERVER_EXPORT int SocketAPIEnd();
721
    //n<-->h functions
sletz's avatar
sletz committed
722
723
724
725
726
727
728
729
    SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
    SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
    SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
    SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
    SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
    SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
    SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
    SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
730
    //display session parameters
sletz's avatar
sletz committed
731
    SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
732
    //display packet header
sletz's avatar
sletz committed
733
    SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
734
    //get the packet type from a sesion parameters
sletz's avatar
sletz committed
735
    SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
736
    //set the packet type in a session parameters
sletz's avatar
sletz committed
737
    SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
738
    //transport utility
sletz's avatar
sletz committed
739
740
    SERVER_EXPORT const char* GetTransportState(int transport_state);
    SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);
sletz's avatar
sletz committed
741
}