JackNetTool.h 11.2 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
21

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

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

sletz's avatar
sletz committed
31
32
namespace Jack
{
33
34
    typedef struct _session_params session_params_t;
    typedef struct _packet_header packet_header_t;
35
    typedef struct _net_transport_data net_transport_data_t;
36
37
38
    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
39
40
41

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

moret's avatar
moret committed
42
43
44
45
46
47
48
49
50
51
52
53
    /**
    \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
54
55
56
        
    Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
    are kept in LITLE_ENDIAN formet (to avoing 2 conversion in the more common LITLE_ENDIAN <==> LITLE_ENDIAN connection scheme.
moret's avatar
moret committed
57
    */
58

59
60
    struct _session_params
    {
moret's avatar
moret committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        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 ?
        uint32_t fSendAudioChannels;        //number of master->slave channels
        uint32_t fReturnAudioChannels;      //number of slave->master channels
        uint32_t fSendMidiChannels;         //number of master->slave midi channels
        uint32_t fReturnMidiChannels;       //number of slave->master midi channels
        uint32_t fSampleRate;               //session sample rate
        uint32_t fPeriodSize;               //period size
        uint32_t fFramesPerPacket;          //complete frames per packet
        uint32_t fBitdepth;                 //samples bitdepth (unused)
moret's avatar
moret committed
78
        uint32_t fSlaveSyncMode;            //is the slave in sync mode ?
79
        char fNetworkMode;                  //fast, normal or slow mode
80
    };
sletz's avatar
sletz committed
81
82
83

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

moret's avatar
moret committed
84
85
86
    /**
    \Brief This enum groups network error by type
    */
87

88
89
90
91
92
93
94
95
96
97
    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
98

99
    typedef enum _net_status net_status_t;
sletz's avatar
sletz committed
100
101
102

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

moret's avatar
moret committed
103
104
105
    /**
    \Brief This enum indicates the type of a sync packet (used in the initialization phase)
    */
106

107
108
    enum _sync_packet_type
    {
109
110
111
112
113
114
        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
115
    };
sletz's avatar
sletz committed
116

117
    typedef enum _sync_packet_type sync_packet_type_t;
sletz's avatar
sletz committed
118
119
120
121


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

moret's avatar
moret committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    /**
    \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

    */
140

141
142
    struct _packet_header
    {
143
        char fPacketType[7];        //packet type ( 'headr' )
144
        char fDataType;             //a for audio, m for midi and s for sync
145
        char fDataStream;           //s for send, r for return
146
        uint32_t fID;               //unique ID of the slave
147
        uint32_t fBitdepth;         //bitdepth of the data samples
moret's avatar
moret committed
148
        uint32_t fMidiDataSize;     //size of midi data in bytes
149
        uint32_t fNMidiPckt;        //number of midi packets of the cycle
moret's avatar
moret committed
150
        uint32_t fPacketSize;       //packet size in bytes
151
152
        uint32_t fCycle;            //process cycle counter
        uint32_t fSubCycle;         //midi/audio subcycle counter
moret's avatar
moret committed
153
        uint32_t fIsLastPckt;       //is it the last packet of a given cycle ('y' or 'n')
154
        char fASyncWrongCycle;      //is the current async cycle wrong (slave's side; 'y' or 'n')
moret's avatar
moret committed
155
        char fFree[26];             //unused
156
    };
sletz's avatar
sletz committed
157

158
159
160
161
162
163
164
165
//net timebase master

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

    enum _net_timebase_master
    {
166
167
168
169
        NO_CHANGE = 0,
        RELEASE_TIMEBASEMASTER = 1,
        TIMEBASEMASTER = 2,
        CONDITIONAL_TIMEBASEMASTER = 3
170
171
172
173
174
    };

    typedef enum _net_timebase_master net_timebase_master_t;


175
176
//transport data ******************************************************************************

moret's avatar
moret committed
177
    /**
178
    \Brief This structure contains transport data to be sent over the network
moret's avatar
moret committed
179
    */
180

moret's avatar
moret committed
181
182
    struct _net_transport_data
    {
183
184
        uint32_t fNewState;             //is it a state change
        uint32_t fTimebaseMaster;       //is there a new timebase master
moret's avatar
moret committed
185
        int32_t fState;                 //current cycle state
186
        jack_position_t fPosition;      //current cycle position
moret's avatar
moret committed
187
    };
188

sletz's avatar
sletz committed
189
190
//midi data ***********************************************************************************

moret's avatar
moret committed
191
192
    /**
    \Brief Midi buffer and operations class
193

moret's avatar
moret committed
194
195
196
197
198
199
200
201
202
    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)
203

moret's avatar
moret committed
204
    */
205

206
    class SERVER_EXPORT NetMidiBuffer
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
        private:
            int fNPorts;
            size_t fMaxBufsize;
            int fMaxPcktSize;
            char* fBuffer;
            char* fNetBuffer;
            JackMidiBuffer** fPortBuffer;

        public:
            NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
            ~NetMidiBuffer();

            void Reset();
            size_t GetSize();
            //utility
            void DisplayEvents();
            //jack<->buffer
            int RenderFromJackPorts();
            int RenderToJackPorts();
            //network<->buffer
            int RenderFromNetwork ( int subcycle, size_t copy_size );
            int RenderToNetwork ( int subcycle, size_t total_size );

            void SetBuffer ( int index, JackMidiBuffer* buffer );
            JackMidiBuffer* GetBuffer ( int index );
233
    };
sletz's avatar
sletz committed
234
235
236

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

moret's avatar
moret committed
237
238
    /**
    \Brief Audio buffer and operations class
239

moret's avatar
moret committed
240
241
242
243
    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.
244

moret's avatar
moret committed
245
    */
246

247
    class SERVER_EXPORT NetAudioBuffer
248
    {
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
        private:
            int fNPorts;
            jack_nframes_t fPeriodSize;
            jack_nframes_t fSubPeriodSize;
            size_t fSubPeriodBytesSize;
            char* fNetBuffer;
            sample_t** fPortBuffer;
        public:
            NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
            ~NetAudioBuffer();

            size_t GetSize();
            //jack<->buffer
            void RenderFromJackPorts ( int subcycle );
            void RenderToJackPorts ( int subcycle );

            void SetBuffer ( int index, sample_t* buffer );
            sample_t* GetBuffer ( int index );
267
    };
sletz's avatar
sletz committed
268
269

//utility *************************************************************************************
270
271

    //socket API management
272
273
    SERVER_EXPORT int SocketAPIInit();
    SERVER_EXPORT int SocketAPIEnd();
274
    //n<-->h functions
275
276
277
278
    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 );
279
    //display session parameters
280
    SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params );
281
    //display packet header
282
    SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header );
283
    //get the packet type from a sesion parameters
284
    SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
285
    //set the packet type in a session parameters
286
    SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
287
    //transport utility
288
    SERVER_EXPORT const char* GetTransportState ( int transport_state );
sletz's avatar
sletz committed
289
}