Correct jackdmp.cpp (failures case were not correct..). Improve JackCoreAudioDriver...
[jack2.git] / common / JackNetTool.h
blobcb6f494f8c7b1cefb9b509044aa643fcf675aa7c
1 /*
2 Copyright (C) 2008 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 "JackPlatformPlug.h"
23 #include "types.h"
24 #include "transport.h"
25 #ifndef WIN32
26 #include <netinet/in.h>
27 #endif
28 #include <cmath>
30 using namespace std;
32 #ifndef htonll
33 #ifdef __BIG_ENDIAN__
34 #define htonll(x) (x)
35 #define ntohll(x) (x)
36 #else
37 #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
38 #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
39 #endif
40 #endif
42 namespace Jack
44 typedef struct _session_params session_params_t;
45 typedef struct _packet_header packet_header_t;
46 typedef struct _net_transport_data net_transport_data_t;
47 typedef struct sockaddr socket_address_t;
48 typedef struct in_addr address_t;
49 typedef jack_default_audio_sample_t sample_t;
51 //session params ******************************************************************************
53 /**
54 \brief This structure containes master/slave connection parameters, it's used to setup the whole system
56 We have :
57 - some info like version, type and packet id
58 - names
59 - network parameters (hostnames and mtu)
60 - nunber of audio and midi channels
61 - sample rate and buffersize
62 - number of audio frames in one network packet (depends on the channel number)
63 - is the NetDriver in Sync or ASync mode ?
64 - is the NetDriver linked with the master's transport
66 Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
67 are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
70 #define MASTER_PROTOCOL 1
71 #define SLAVE_PROTOCOL 1
73 struct _session_params
75 char fPacketType[7]; //packet type ('param')
76 char fProtocolVersion; //version
77 uint32_t fPacketID; //indicates the packet type
78 char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
79 char fMasterNetName[256]; //master hostname (network)
80 char fSlaveNetName[256]; //slave hostname (network)
81 uint32_t fMtu; //connection mtu
82 uint32_t fID; //slave's ID
83 uint32_t fTransportSync; //is the transport synced ?
84 uint32_t fSendAudioChannels; //number of master->slave channels
85 uint32_t fReturnAudioChannels; //number of slave->master channels
86 uint32_t fSendMidiChannels; //number of master->slave midi channels
87 uint32_t fReturnMidiChannels; //number of slave->master midi channels
88 uint32_t fSampleRate; //session sample rate
89 uint32_t fPeriodSize; //period size
90 uint32_t fFramesPerPacket; //complete frames per packet
91 uint32_t fBitdepth; //samples bitdepth (unused)
92 uint32_t fSlaveSyncMode; //is the slave in sync mode ?
93 char fNetworkMode; //fast, normal or slow mode
96 //net status **********************************************************************************
98 /**
99 \Brief This enum groups network error by type
102 enum _net_status
104 NET_SOCKET_ERROR = 0,
105 NET_CONNECT_ERROR,
106 NET_ERROR,
107 NET_SEND_ERROR,
108 NET_RECV_ERROR,
109 NET_CONNECTED,
110 NET_ROLLING
113 typedef enum _net_status net_status_t;
115 //sync packet type ****************************************************************************
118 \Brief This enum indicates the type of a sync packet (used in the initialization phase)
121 enum _sync_packet_type
123 INVALID = 0, //...
124 SLAVE_AVAILABLE, //a slave is available
125 SLAVE_SETUP, //slave configuration
126 START_MASTER, //slave is ready, start master
127 START_SLAVE, //master is ready, activate slave
128 KILL_MASTER //master must stop
131 typedef enum _sync_packet_type sync_packet_type_t;
134 //packet header *******************************************************************************
137 \Brief This structure is a complete header
139 A header indicates :
140 - it is a header
141 - the type of data the packet contains (sync, midi or audio)
142 - the path of the packet (send -master->slave- or return -slave->master-)
143 - the unique ID of the slave
144 - the sample's bitdepth (unused for now)
145 - the size of the midi data contains in the packet (indicates how much midi data will be sent)
146 - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
147 - the ID of the current cycle (used to check missing packets)
148 - the ID of the packet subcycle (for audio data)
149 - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
150 - a flag indicating if, in async mode, the previous graph was not finished or not
151 - padding to fill 64 bytes
155 struct _packet_header
157 char fPacketType[7]; //packet type ( 'headr' )
158 char fDataType; //a for audio, m for midi and s for sync
159 char fDataStream; //s for send, r for return
160 uint32_t fID; //unique ID of the slave
161 uint32_t fBitdepth; //bitdepth of the data samples
162 uint32_t fMidiDataSize; //size of midi data in bytes
163 uint32_t fNMidiPckt; //number of midi packets of the cycle
164 uint32_t fPacketSize; //packet size in bytes
165 uint32_t fCycle; //process cycle counter
166 uint32_t fSubCycle; //midi/audio subcycle counter
167 uint32_t fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
168 char fASyncWrongCycle; //is the current async cycle wrong (slave's side; 'y' or 'n')
169 char fFree[26]; //unused
172 //net timebase master
175 \Brief This enum describes timebase master's type
178 enum _net_timebase_master
180 NO_CHANGE = 0,
181 RELEASE_TIMEBASEMASTER = 1,
182 TIMEBASEMASTER = 2,
183 CONDITIONAL_TIMEBASEMASTER = 3
186 typedef enum _net_timebase_master net_timebase_master_t;
189 //transport data ******************************************************************************
192 \Brief This structure contains transport data to be sent over the network
195 struct _net_transport_data
197 uint32_t fNewState; //is it a state change
198 uint32_t fTimebaseMaster; //is there a new timebase master
199 int32_t fState; //current cycle state
200 jack_position_t fPosition; //current cycle position
203 //midi data ***********************************************************************************
206 \Brief Midi buffer and operations class
208 This class is a toolset to manipulate Midi buffers.
209 A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
210 An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
211 But for a big majority of the process cycles, this buffer is filled less than 1%,
212 Sending over a network 99% of useless data seems completely unappropriate.
213 The idea is to count effective midi data, and then send the smallest packet we can.
214 To do it, we use an intermediate buffer.
215 We have two methods to convert data from jack ports to intermediate buffer,
216 And two others to convert this intermediate buffer to a network buffer (header + payload data)
220 class SERVER_EXPORT NetMidiBuffer
222 private:
223 int fNPorts;
224 size_t fMaxBufsize;
225 int fMaxPcktSize;
226 char* fBuffer;
227 char* fNetBuffer;
228 JackMidiBuffer** fPortBuffer;
230 public:
231 NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
232 ~NetMidiBuffer();
234 void Reset();
235 size_t GetSize();
236 //utility
237 void DisplayEvents();
238 //jack<->buffer
239 int RenderFromJackPorts();
240 int RenderToJackPorts();
241 //network<->buffer
242 int RenderFromNetwork ( int subcycle, size_t copy_size );
243 int RenderToNetwork ( int subcycle, size_t total_size );
245 void SetBuffer ( int index, JackMidiBuffer* buffer );
246 JackMidiBuffer* GetBuffer ( int index );
249 // audio data *********************************************************************************
252 \Brief Audio buffer and operations class
254 This class is a toolset to manipulate audio buffers.
255 The manipulation of audio buffers is similar to midi buffer, except those buffers have fixed size.
256 The interleaving/uninterleaving operations are simplier here because audio buffers have fixed size,
257 So there is no need of an intermediate buffer as in NetMidiBuffer.
261 class SERVER_EXPORT NetAudioBuffer
263 private:
264 int fNPorts;
265 jack_nframes_t fPeriodSize;
266 jack_nframes_t fSubPeriodSize;
267 size_t fSubPeriodBytesSize;
268 char* fNetBuffer;
269 sample_t** fPortBuffer;
270 public:
271 NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
272 ~NetAudioBuffer();
274 size_t GetSize();
275 //jack<->buffer
276 void RenderFromJackPorts ( int subcycle );
277 void RenderToJackPorts ( int subcycle );
279 void SetBuffer ( int index, sample_t* buffer );
280 sample_t* GetBuffer ( int index );
283 //utility *************************************************************************************
285 //socket API management
286 SERVER_EXPORT int SocketAPIInit();
287 SERVER_EXPORT int SocketAPIEnd();
288 //n<-->h functions
289 SERVER_EXPORT void SessionParamsHToN ( session_params_t* src_params, session_params_t* dst_params );
290 SERVER_EXPORT void SessionParamsNToH ( session_params_t* src_params, session_params_t* dst_params );
291 SERVER_EXPORT void PacketHeaderHToN ( packet_header_t* src_header, packet_header_t* dst_header );
292 SERVER_EXPORT void PacketHeaderNToH ( packet_header_t* src_header, packet_header_t* dst_header );
293 SERVER_EXPORT void MidiBufferHToN ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer );
294 SERVER_EXPORT void MidiBufferNToH ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer );
295 SERVER_EXPORT void TransportDataHToN ( net_transport_data_t* src_params, net_transport_data_t* dst_params );
296 SERVER_EXPORT void TransportDataNToH ( net_transport_data_t* src_params, net_transport_data_t* dst_params );
297 //display session parameters
298 SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params );
299 //display packet header
300 SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header );
301 //get the packet type from a sesion parameters
302 SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
303 //set the packet type in a session parameters
304 SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
305 //transport utility
306 SERVER_EXPORT const char* GetTransportState ( int transport_state );
307 SERVER_EXPORT void NetTransportDataDisplay ( net_transport_data_t* data );