FIx doxygen and user facing and non-facing typos
[jack2.git] / common / JackNetTool.h
blobe664f9fe89a3adda974a1dd2d00ead05869eb987
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 NETWORK_PROTOCOL 8
43 #define NET_SYNCHING 0
44 #define SYNC_PACKET_ERROR -2
45 #define DATA_PACKET_ERROR -3
47 #define OPTIMIZED_PROTOCOL 1
49 #define UDP_HEADER_SIZE 64 // 40 bytes for IP header in IPV6, 20 in IPV4, 8 for UDP, so take 64
50 #define HEADER_SIZE (sizeof(packet_header_t))
52 #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - UDP_HEADER_SIZE - HEADER_SIZE)
54 namespace Jack
56 typedef struct _session_params session_params_t;
57 typedef struct _packet_header packet_header_t;
58 typedef struct _net_transport_data net_transport_data_t;
59 typedef struct sockaddr socket_address_t;
60 typedef struct in_addr address_t;
61 typedef jack_default_audio_sample_t sample_t;
63 enum JackNetEncoder {
65 JackFloatEncoder = 0,
66 JackIntEncoder = 1,
67 JackCeltEncoder = 2,
68 JackOpusEncoder = 3,
71 //session params ******************************************************************************
73 /**
74 \brief This structure contains master/slave connection parameters, it's used to setup the whole system
76 We have :
77 - some info like version, type and packet id
78 - names
79 - network parameters (hostnames and mtu)
80 - number of audio and midi channels
81 - sample rate and buffersize
82 - number of audio frames in one network packet (depends on the channel number)
83 - is the NetDriver in Sync or ASync mode ?
84 - is the NetDriver linked with the master's transport
86 Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
87 are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
90 PRE_PACKED_STRUCTURE
91 struct _session_params
93 char fPacketType[8]; //packet type ('param')
94 uint32_t fProtocolVersion; //version
95 int32_t fPacketID; //indicates the packet type
96 char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
97 char fMasterNetName[JACK_SERVER_NAME_SIZE]; //master hostname (network)
98 char fSlaveNetName[JACK_SERVER_NAME_SIZE]; //slave hostname (network)
99 uint32_t fMtu; //connection mtu
100 uint32_t fID; //slave's ID
101 uint32_t fTransportSync; //is the transport synced ?
102 int32_t fSendAudioChannels; //number of master->slave channels
103 int32_t fReturnAudioChannels; //number of slave->master channels
104 int32_t fSendMidiChannels; //number of master->slave midi channels
105 int32_t fReturnMidiChannels; //number of slave->master midi channels
106 uint32_t fSampleRate; //session sample rate
107 uint32_t fPeriodSize; //period size
108 uint32_t fSampleEncoder; //samples encoder
109 uint32_t fKBps; //KB per second for CELT encoder
110 uint32_t fSlaveSyncMode; //is the slave in sync mode ?
111 uint32_t fNetworkLatency; //network latency
112 } POST_PACKED_STRUCTURE;
114 //net status **********************************************************************************
117 \Brief This enum groups network error by type
120 enum _net_status
122 NET_SOCKET_ERROR = 0,
123 NET_CONNECT_ERROR,
124 NET_ERROR,
125 NET_SEND_ERROR,
126 NET_RECV_ERROR,
127 NET_CONNECTED,
128 NET_ROLLING
131 typedef enum _net_status net_status_t;
133 //sync packet type ****************************************************************************
136 \Brief This enum indicates the type of a sync packet (used in the initialization phase)
139 enum _sync_packet_type
141 INVALID = 0, //...
142 SLAVE_AVAILABLE, //a slave is available
143 SLAVE_SETUP, //slave configuration
144 START_MASTER, //slave is ready, start master
145 START_SLAVE, //master is ready, activate slave
146 KILL_MASTER //master must stop
149 typedef enum _sync_packet_type sync_packet_type_t;
152 //packet header *******************************************************************************
155 \Brief This structure is a complete header
157 A header indicates :
158 - it is a header
159 - the type of data the packet contains (sync, midi or audio)
160 - the path of the packet (send -master->slave- or return -slave->master-)
161 - the unique ID of the slave
162 - the sample's bitdepth (unused for now)
163 - the size of the midi data contains in the packet (indicates how much midi data will be sent)
164 - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
165 - the ID of the current cycle (used to check missing packets)
166 - the ID of the packet subcycle (for audio data)
167 - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
168 - a flag indicating if, in async mode, the previous graph was not finished or not
169 - padding to fill 64 bytes
173 PRE_PACKED_STRUCTURE
174 struct _packet_header
176 char fPacketType[8]; //packet type ('headr')
177 uint32_t fDataType; //'a' for audio, 'm' for midi and 's' for sync
178 uint32_t fDataStream; //'s' for send, 'r' for return
179 uint32_t fID; //unique ID of the slave
180 uint32_t fNumPacket; //number of data packets of the cycle
181 uint32_t fPacketSize; //packet size in bytes
182 uint32_t fActivePorts; //number of active ports
183 uint32_t fCycle; //process cycle counter
184 uint32_t fSubCycle; //midi/audio subcycle counter
185 int32_t fFrames; //process cycle size in frames (can be -1 to indicate entire buffer)
186 uint32_t fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
187 } POST_PACKED_STRUCTURE;
189 //net timebase master
192 \Brief This enum describes timebase master's type
195 enum _net_timebase_master
197 NO_CHANGE = 0,
198 RELEASE_TIMEBASEMASTER = 1,
199 TIMEBASEMASTER = 2,
200 CONDITIONAL_TIMEBASEMASTER = 3
203 typedef enum _net_timebase_master net_timebase_master_t;
206 //transport data ******************************************************************************
209 \Brief This structure contains transport data to be sent over the network
212 PRE_PACKED_STRUCTURE
213 struct _net_transport_data
215 uint32_t fNewState; //is it a state change
216 uint32_t fTimebaseMaster; //is there a new timebase master
217 int32_t fState; //current cycle state
218 jack_position_t fPosition; //current cycle position
219 } POST_PACKED_STRUCTURE;
221 //midi data ***********************************************************************************
224 \Brief Midi buffer and operations class
226 This class is a toolset to manipulate Midi buffers.
227 A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
228 An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
229 But for a big majority of the process cycles, this buffer is filled less than 1%,
230 Sending over a network 99% of useless data seems completely unappropriate.
231 The idea is to count effective midi data, and then send the smallest packet we can.
232 To do it, we use an intermediate buffer.
233 We have two methods to convert data from jack ports to intermediate buffer,
234 And two others to convert this intermediate buffer to a network buffer (header + payload data)
238 class SERVER_EXPORT NetMidiBuffer
240 private:
242 int fNPorts;
243 size_t fMaxBufsize;
244 int fMaxPcktSize;
246 char* fBuffer;
247 char* fNetBuffer;
248 JackMidiBuffer** fPortBuffer;
250 size_t fCycleBytesSize; // needed size in bytes ofr an entire cycle
252 public:
254 NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
255 ~NetMidiBuffer();
257 void Reset();
259 // needed size in bytes for an entire cycle
260 size_t GetCycleSize();
261 int GetNumPackets(int data_sizen, int max_size);
263 void SetBuffer(int index, JackMidiBuffer* buffer);
264 JackMidiBuffer* GetBuffer(int index);
266 //utility
267 void DisplayEvents();
269 //jack<->buffer
270 int RenderFromJackPorts();
271 void RenderToJackPorts();
273 //network<->buffer
274 void RenderFromNetwork(int sub_cycle, size_t copy_size);
275 int RenderToNetwork(int sub_cycle, size_t total_size);
279 // audio data *********************************************************************************
281 class SERVER_EXPORT NetAudioBuffer
284 protected:
286 int fNPorts;
287 int fLastSubCycle;
288 int fNumPackets;
290 char* fNetBuffer;
291 sample_t** fPortBuffer;
292 bool* fConnectedPorts;
294 jack_nframes_t fPeriodSize;
295 jack_nframes_t fSubPeriodSize;
296 size_t fSubPeriodBytesSize;
298 float fCycleDuration; // in sec
299 size_t fCycleBytesSize; // needed size in bytes for an entire cycle
301 int CheckPacket(int cycle, int sub_cycle);
302 void NextCycle();
303 void Cleanup();
305 public:
307 NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
308 virtual ~NetAudioBuffer();
310 bool GetConnected(int port_index) { return fConnectedPorts[port_index]; }
311 void SetConnected(int port_index, bool state) { fConnectedPorts[port_index] = state; }
313 // needed syze in bytes ofr an entire cycle
314 virtual size_t GetCycleSize() = 0;
316 // cycle duration in sec
317 virtual float GetCycleDuration() = 0;
319 virtual int GetNumPackets(int active_ports) = 0;
321 virtual void SetBuffer(int index, sample_t* buffer);
322 virtual sample_t* GetBuffer(int index);
324 //jack<->buffer
325 virtual int RenderFromJackPorts(int nframes);
326 virtual void RenderToJackPorts(int nframes);
328 //network<->buffer
329 virtual int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num) = 0;
330 virtual int RenderToNetwork(int sub_cycle, uint32_t port_num) = 0;
332 virtual int ActivePortsToNetwork(char* net_buffer);
333 virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
337 class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
340 private:
342 int fPacketSize;
344 void UpdateParams(int active_ports);
347 void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle);
348 void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle);
350 public:
352 NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
353 virtual ~NetFloatAudioBuffer();
355 // needed size in bytes for an entire cycle
356 size_t GetCycleSize();
358 // cycle duration in sec
359 float GetCycleDuration();
360 int GetNumPackets(int active_ports);
362 //jack<->buffer
363 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
364 int RenderToNetwork(int sub_cycle, uint32_t port_num);
368 #if HAVE_CELT
370 #include <celt/celt.h>
372 class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
374 private:
376 CELTMode** fCeltMode;
377 CELTEncoder** fCeltEncoder;
378 CELTDecoder** fCeltDecoder;
380 int fCompressedSizeByte;
381 unsigned char** fCompressedBuffer;
383 size_t fLastSubPeriodBytesSize;
385 void FreeCelt();
387 public:
389 NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
390 virtual ~NetCeltAudioBuffer();
392 // needed size in bytes for an entire cycle
393 size_t GetCycleSize();
395 // cycle duration in sec
396 float GetCycleDuration();
397 int GetNumPackets(int active_ports);
399 //jack<->buffer
400 int RenderFromJackPorts(int nframes);
401 void RenderToJackPorts(int nframes);
403 //network<->buffer
404 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
405 int RenderToNetwork(int sub_cycle, uint32_t port_num);
408 #endif
410 #if HAVE_OPUS
412 #include <opus/opus.h>
413 #include <opus/opus_custom.h>
415 class SERVER_EXPORT NetOpusAudioBuffer : public NetAudioBuffer
417 private:
419 OpusCustomMode** fOpusMode;
420 OpusCustomEncoder** fOpusEncoder;
421 OpusCustomDecoder** fOpusDecoder;
423 int fCompressedMaxSizeByte;
424 unsigned short* fCompressedSizesByte;
426 size_t fLastSubPeriodBytesSize;
428 unsigned char** fCompressedBuffer;
429 void FreeOpus();
431 public:
433 NetOpusAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
434 virtual ~NetOpusAudioBuffer();
436 // needed size in bytes for an entire cycle
437 size_t GetCycleSize();
439 // cycle duration in sec
440 float GetCycleDuration();
441 int GetNumPackets(int active_ports);
443 //jack<->buffer
444 int RenderFromJackPorts(int nframes);
445 void RenderToJackPorts(int nframes);
447 //network<->buffer
448 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
449 int RenderToNetwork(int sub_cycle, uint32_t port_num);
452 #endif
454 class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
456 private:
458 int fCompressedSizeByte;
460 size_t fLastSubPeriodBytesSize;
462 short** fIntBuffer;
464 public:
466 NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
467 virtual ~NetIntAudioBuffer();
469 // needed size in bytes for an entire cycle
470 size_t GetCycleSize();
472 // cycle duration in sec
473 float GetCycleDuration();
474 int GetNumPackets(int active_ports);
476 //jack<->buffer
477 int RenderFromJackPorts(int nframes);
478 void RenderToJackPorts(int nframes);
480 //network<->buffer
481 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
482 int RenderToNetwork(int sub_cycle, uint32_t port_num);
485 //utility *************************************************************************************
487 //socket API management
488 SERVER_EXPORT int SocketAPIInit();
489 SERVER_EXPORT int SocketAPIEnd();
490 //n<-->h functions
491 SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
492 SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
493 SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
494 SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
495 SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
496 SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
497 SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
498 SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
499 //display session parameters
500 SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
501 //display packet header
502 SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
503 //get the packet type from a sesion parameters
504 SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
505 //set the packet type in a session parameters
506 SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
507 //transport utility
508 SERVER_EXPORT const char* GetTransportState(int transport_state);
509 SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);