Improve Windows README.
[jack2.git] / common / JackNetTool.h
blob62775443617b111c29f49251d631731d912d0e3f
1 /*
2 Copyright (C) 2008-2011 Romain Moret at Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackMidiPort.h"
21 #include "JackTools.h"
22 #include "types.h"
23 #include "transport.h"
24 #ifndef WIN32
25 #include <netinet/in.h>
26 #endif
27 #include <cmath>
29 using namespace std;
31 #ifndef htonll
32 #ifdef __BIG_ENDIAN__
33 #define htonll(x) (x)
34 #define ntohll(x) (x)
35 #else
36 #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
37 #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
38 #endif
39 #endif
41 #define MASTER_PROTOCOL 5
42 #define SLAVE_PROTOCOL 5
44 #define NET_PACKET_ERROR -2
46 #define OPTIMIZED_PROTOCOL
48 #define HEADER_SIZE (sizeof(packet_header_t))
49 #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - sizeof(packet_header_t))
51 namespace Jack
53 typedef struct _session_params session_params_t;
54 typedef struct _packet_header packet_header_t;
55 typedef struct _net_transport_data net_transport_data_t;
56 typedef struct sockaddr socket_address_t;
57 typedef struct in_addr address_t;
58 typedef jack_default_audio_sample_t sample_t;
60 enum JackNetEncoder {
62 JackFloatEncoder = 0,
63 JackIntEncoder = 1,
64 JackCeltEncoder = 2,
67 //session params ******************************************************************************
69 /**
70 \brief This structure containes master/slave connection parameters, it's used to setup the whole system
72 We have :
73 - some info like version, type and packet id
74 - names
75 - network parameters (hostnames and mtu)
76 - nunber of audio and midi channels
77 - sample rate and buffersize
78 - number of audio frames in one network packet (depends on the channel number)
79 - is the NetDriver in Sync or ASync mode ?
80 - is the NetDriver linked with the master's transport
82 Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
83 are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
86 struct _session_params
88 char fPacketType[7]; //packet type ('param')
89 char fProtocolVersion; //version
90 uint32_t fPacketID; //indicates the packet type
91 char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
92 char fMasterNetName[256]; //master hostname (network)
93 char fSlaveNetName[256]; //slave hostname (network)
94 uint32_t fMtu; //connection mtu
95 uint32_t fID; //slave's ID
96 uint32_t fTransportSync; //is the transport synced ?
97 int32_t fSendAudioChannels; //number of master->slave channels
98 int32_t fReturnAudioChannels; //number of slave->master channels
99 int32_t fSendMidiChannels; //number of master->slave midi channels
100 int32_t fReturnMidiChannels; //number of slave->master midi channels
101 uint32_t fSampleRate; //session sample rate
102 uint32_t fPeriodSize; //period size
103 uint32_t fSampleEncoder; //samples encoder
104 uint32_t fKBps; //KB per second for CELT encoder
105 uint32_t fSlaveSyncMode; //is the slave in sync mode ?
106 uint32_t fNetworkLatency; //network latency
109 //net status **********************************************************************************
112 \Brief This enum groups network error by type
115 enum _net_status
117 NET_SOCKET_ERROR = 0,
118 NET_CONNECT_ERROR,
119 NET_ERROR,
120 NET_SEND_ERROR,
121 NET_RECV_ERROR,
122 NET_CONNECTED,
123 NET_ROLLING
126 typedef enum _net_status net_status_t;
128 //sync packet type ****************************************************************************
131 \Brief This enum indicates the type of a sync packet (used in the initialization phase)
134 enum _sync_packet_type
136 INVALID = 0, //...
137 SLAVE_AVAILABLE, //a slave is available
138 SLAVE_SETUP, //slave configuration
139 START_MASTER, //slave is ready, start master
140 START_SLAVE, //master is ready, activate slave
141 KILL_MASTER //master must stop
144 typedef enum _sync_packet_type sync_packet_type_t;
147 //packet header *******************************************************************************
150 \Brief This structure is a complete header
152 A header indicates :
153 - it is a header
154 - the type of data the packet contains (sync, midi or audio)
155 - the path of the packet (send -master->slave- or return -slave->master-)
156 - the unique ID of the slave
157 - the sample's bitdepth (unused for now)
158 - the size of the midi data contains in the packet (indicates how much midi data will be sent)
159 - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
160 - the ID of the current cycle (used to check missing packets)
161 - the ID of the packet subcycle (for audio data)
162 - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
163 - a flag indicating if, in async mode, the previous graph was not finished or not
164 - padding to fill 64 bytes
168 struct _packet_header
170 char fPacketType[7]; //packet type ('headr')
171 char fDataType; //a for audio, m for midi and s for sync
172 char fDataStream; //s for send, r for return
173 uint32_t fID; //unique ID of the slave
174 uint32_t fNumPacket; //number of data packets of the cycle
175 uint32_t fPacketSize; //packet size in bytes
176 uint32_t fActivePorts; //number of active ports
177 uint32_t fCycle; //process cycle counter
178 uint32_t fSubCycle; //midi/audio subcycle counter
179 uint32_t fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
182 //net timebase master
185 \Brief This enum describes timebase master's type
188 enum _net_timebase_master
190 NO_CHANGE = 0,
191 RELEASE_TIMEBASEMASTER = 1,
192 TIMEBASEMASTER = 2,
193 CONDITIONAL_TIMEBASEMASTER = 3
196 typedef enum _net_timebase_master net_timebase_master_t;
199 //transport data ******************************************************************************
202 \Brief This structure contains transport data to be sent over the network
205 struct _net_transport_data
207 uint32_t fNewState; //is it a state change
208 uint32_t fTimebaseMaster; //is there a new timebase master
209 int32_t fState; //current cycle state
210 jack_position_t fPosition; //current cycle position
213 //midi data ***********************************************************************************
216 \Brief Midi buffer and operations class
218 This class is a toolset to manipulate Midi buffers.
219 A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
220 An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
221 But for a big majority of the process cycles, this buffer is filled less than 1%,
222 Sending over a network 99% of useless data seems completely unappropriate.
223 The idea is to count effective midi data, and then send the smallest packet we can.
224 To do it, we use an intermediate buffer.
225 We have two methods to convert data from jack ports to intermediate buffer,
226 And two others to convert this intermediate buffer to a network buffer (header + payload data)
230 class SERVER_EXPORT NetMidiBuffer
232 private:
234 int fNPorts;
235 size_t fMaxBufsize;
236 int fMaxPcktSize;
238 char* fBuffer;
239 char* fNetBuffer;
240 JackMidiBuffer** fPortBuffer;
242 size_t fCycleBytesSize; // needed size in bytes ofr an entire cycle
244 public:
246 NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
247 ~NetMidiBuffer();
249 void Reset();
251 // needed size in bytes for an entire cycle
252 size_t GetCycleSize();
253 int GetNumPackets(int data_sizen, int max_size);
255 void SetBuffer(int index, JackMidiBuffer* buffer);
256 JackMidiBuffer* GetBuffer(int index);
258 //utility
259 void DisplayEvents();
261 //jack<->buffer
262 int RenderFromJackPorts();
263 void RenderToJackPorts();
265 //network<->buffer
266 void RenderFromNetwork(int sub_cycle, size_t copy_size);
267 int RenderToNetwork(int sub_cycle, size_t total_size);
271 // audio data *********************************************************************************
273 class SERVER_EXPORT NetAudioBuffer
276 protected:
278 int fNPorts;
279 int fLastSubCycle;
281 char* fNetBuffer;
282 sample_t** fPortBuffer;
283 bool* fConnectedPorts;
285 jack_nframes_t fPeriodSize;
286 jack_nframes_t fSubPeriodSize;
287 size_t fSubPeriodBytesSize;
289 float fCycleDuration; // in sec
290 size_t fCycleBytesSize; // needed size in bytes for an entire cycle
292 int CheckPacket(int cycle, int sub_cycle);
293 void NextCycle();
294 void Cleanup();
296 public:
298 NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
299 virtual ~NetAudioBuffer();
301 bool GetConnected(int port_index) { return fConnectedPorts[port_index]; }
302 void SetConnected(int port_index, bool state) { fConnectedPorts[port_index] = state; }
304 // needed syze in bytes ofr an entire cycle
305 virtual size_t GetCycleSize() = 0;
307 // cycle duration in sec
308 virtual float GetCycleDuration() = 0;
310 virtual int GetNumPackets(int active_ports) = 0;
312 virtual void SetBuffer(int index, sample_t* buffer);
313 virtual sample_t* GetBuffer(int index);
315 //jack<->buffer
316 virtual int RenderFromJackPorts();
317 virtual void RenderToJackPorts();
319 //network<->buffer
320 virtual int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num) = 0;
321 virtual int RenderToNetwork(int sub_cycle, uint32_t port_num) = 0;
323 virtual void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle, size_t copy_size) {}
324 virtual void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle, size_t copy_size) {}
326 virtual int ActivePortsToNetwork(char* net_buffer);
327 virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
331 class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
334 private:
336 int fPacketSize;
338 void UpdateParams(int active_ports);
340 public:
342 NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
343 virtual ~NetFloatAudioBuffer();
345 // needed size in bytes for an entire cycle
346 size_t GetCycleSize();
348 // cycle duration in sec
349 float GetCycleDuration();
350 int GetNumPackets(int active_ports);
352 //jack<->buffer
353 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
354 int RenderToNetwork(int sub_cycle, uint32_t port_num);
356 void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle);
357 void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle);
361 #if HAVE_CELT
363 #include <celt/celt.h>
365 class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
367 private:
369 CELTMode** fCeltMode;
370 CELTEncoder** fCeltEncoder;
371 CELTDecoder** fCeltDecoder;
373 int fCompressedSizeByte;
374 int fNumPackets;
376 size_t fLastSubPeriodBytesSize;
378 unsigned char** fCompressedBuffer;
380 void FreeCelt();
382 public:
384 NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
385 virtual ~NetCeltAudioBuffer();
387 // needed size in bytes for an entire cycle
388 size_t GetCycleSize();
390 // cycle duration in sec
391 float GetCycleDuration();
392 int GetNumPackets(int active_ports);
394 //jack<->buffer
395 int RenderFromJackPorts();
396 void RenderToJackPorts();
398 //network<->buffer
399 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
400 int RenderToNetwork(int sub_cycle, uint32_t port_num);
403 #endif
405 class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
407 private:
409 int fCompressedSizeByte;
410 int fNumPackets;
412 size_t fLastSubPeriodBytesSize;
414 short** fIntBuffer;
416 public:
418 NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
419 virtual ~NetIntAudioBuffer();
421 // needed size in bytes for an entire cycle
422 size_t GetCycleSize();
424 // cycle duration in sec
425 float GetCycleDuration();
426 int GetNumPackets(int active_ports);
428 //jack<->buffer
429 int RenderFromJackPorts();
430 void RenderToJackPorts();
432 //network<->buffer
433 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
434 int RenderToNetwork(int sub_cycle, uint32_t port_num);
437 //utility *************************************************************************************
439 //socket API management
440 SERVER_EXPORT int SocketAPIInit();
441 SERVER_EXPORT int SocketAPIEnd();
442 //n<-->h functions
443 SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
444 SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
445 SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
446 SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
447 SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
448 SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
449 SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
450 SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
451 //display session parameters
452 SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
453 //display packet header
454 SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
455 //get the packet type from a sesion parameters
456 SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
457 //set the packet type in a session parameters
458 SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
459 //transport utility
460 SERVER_EXPORT const char* GetTransportState(int transport_state);
461 SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);