Jack2  1.9.8
JackNetTool.h
1 /*
2 Copyright (C) 2008-2011 Romain Moret at Grame
3 
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.
8 
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.
13 
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.
17 
18 */
19 
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>
28 
29 using namespace std;
30 
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
40 
41 #define MASTER_PROTOCOL 5
42 #define SLAVE_PROTOCOL 5
43 
44 #define NET_PACKET_ERROR -2
45 
46 #define OPTIMIZED_PROTOCOL
47 
48 #define HEADER_SIZE (sizeof(packet_header_t))
49 #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - sizeof(packet_header_t))
50 
51 namespace Jack
52 {
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;
59 
60  enum JackNetEncoder {
61 
62  JackFloatEncoder = 0,
63  JackIntEncoder = 1,
64  JackCeltEncoder = 2,
65  };
66 
67 //session params ******************************************************************************
68 
87  {
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
107  };
108 
109 //net status **********************************************************************************
110 
115  enum _net_status
116  {
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
124  };
125 
126  typedef enum _net_status net_status_t;
127 
128 //sync packet type ****************************************************************************
129 
134  enum _sync_packet_type
135  {
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
142  };
143 
144  typedef enum _sync_packet_type sync_packet_type_t;
145 
146 
147 //packet header *******************************************************************************
148 
169  {
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')
180  };
181 
182 //net timebase master
183 
188  enum _net_timebase_master
189  {
190  NO_CHANGE = 0,
191  RELEASE_TIMEBASEMASTER = 1,
192  TIMEBASEMASTER = 2,
193  CONDITIONAL_TIMEBASEMASTER = 3
194  };
195 
196  typedef enum _net_timebase_master net_timebase_master_t;
197 
198 
199 //transport data ******************************************************************************
200 
206  {
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
211  };
212 
213  //midi data ***********************************************************************************
214 
230  class SERVER_EXPORT NetMidiBuffer
231  {
232  private:
233 
234  int fNPorts;
235  size_t fMaxBufsize;
236  int fMaxPcktSize;
237 
238  char* fBuffer;
239  char* fNetBuffer;
240  JackMidiBuffer** fPortBuffer;
241 
242  size_t fCycleBytesSize; // needed size in bytes ofr an entire cycle
243 
244  public:
245 
246  NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
247  ~NetMidiBuffer();
248 
249  void Reset();
250 
251  // needed size in bytes for an entire cycle
252  size_t GetCycleSize();
253  int GetNumPackets(int data_sizen, int max_size);
254 
255  void SetBuffer(int index, JackMidiBuffer* buffer);
256  JackMidiBuffer* GetBuffer(int index);
257 
258  //utility
259  void DisplayEvents();
260 
261  //jack<->buffer
262  int RenderFromJackPorts();
263  void RenderToJackPorts();
264 
265  //network<->buffer
266  void RenderFromNetwork(int sub_cycle, size_t copy_size);
267  int RenderToNetwork(int sub_cycle, size_t total_size);
268 
269  };
270 
271 // audio data *********************************************************************************
272 
273  class SERVER_EXPORT NetAudioBuffer
274  {
275 
276  protected:
277 
278  int fNPorts;
279  int fLastSubCycle;
280 
281  char* fNetBuffer;
282  sample_t** fPortBuffer;
283  bool* fConnectedPorts;
284 
285  jack_nframes_t fPeriodSize;
286  jack_nframes_t fSubPeriodSize;
287  size_t fSubPeriodBytesSize;
288 
289  float fCycleDuration; // in sec
290  size_t fCycleBytesSize; // needed size in bytes for an entire cycle
291 
292  int CheckPacket(int cycle, int sub_cycle);
293  void NextCycle();
294  void Cleanup();
295 
296  public:
297 
298  NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
299  virtual ~NetAudioBuffer();
300 
301  bool GetConnected(int port_index) { return fConnectedPorts[port_index]; }
302  void SetConnected(int port_index, bool state) { fConnectedPorts[port_index] = state; }
303 
304  // needed syze in bytes ofr an entire cycle
305  virtual size_t GetCycleSize() = 0;
306 
307  // cycle duration in sec
308  virtual float GetCycleDuration() = 0;
309 
310  virtual int GetNumPackets(int active_ports) = 0;
311 
312  virtual void SetBuffer(int index, sample_t* buffer);
313  virtual sample_t* GetBuffer(int index);
314 
315  //jack<->buffer
316  virtual int RenderFromJackPorts();
317  virtual void RenderToJackPorts();
318 
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;
322 
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) {}
325 
326  virtual int ActivePortsToNetwork(char* net_buffer);
327  virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
328 
329  };
330 
331  class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
332  {
333 
334  private:
335 
336  int fPacketSize;
337 
338  void UpdateParams(int active_ports);
339 
340  public:
341 
342  NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
343  virtual ~NetFloatAudioBuffer();
344 
345  // needed size in bytes for an entire cycle
346  size_t GetCycleSize();
347 
348  // cycle duration in sec
349  float GetCycleDuration();
350  int GetNumPackets(int active_ports);
351 
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);
355 
356  void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle);
357  void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle);
358 
359  };
360 
361 #if HAVE_CELT
362 
363 #include <celt/celt.h>
364 
365  class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
366  {
367  private:
368 
369  CELTMode** fCeltMode;
370  CELTEncoder** fCeltEncoder;
371  CELTDecoder** fCeltDecoder;
372 
373  int fCompressedSizeByte;
374  int fNumPackets;
375 
376  size_t fLastSubPeriodBytesSize;
377 
378  unsigned char** fCompressedBuffer;
379 
380  void FreeCelt();
381 
382  public:
383 
384  NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
385  virtual ~NetCeltAudioBuffer();
386 
387  // needed size in bytes for an entire cycle
388  size_t GetCycleSize();
389 
390  // cycle duration in sec
391  float GetCycleDuration();
392  int GetNumPackets(int active_ports);
393 
394  //jack<->buffer
395  int RenderFromJackPorts();
396  void RenderToJackPorts();
397 
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);
401  };
402 
403 #endif
404 
405  class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
406  {
407  private:
408 
409  int fCompressedSizeByte;
410  int fNumPackets;
411 
412  size_t fLastSubPeriodBytesSize;
413 
414  short** fIntBuffer;
415 
416  public:
417 
418  NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
419  virtual ~NetIntAudioBuffer();
420 
421  // needed size in bytes for an entire cycle
422  size_t GetCycleSize();
423 
424  // cycle duration in sec
425  float GetCycleDuration();
426  int GetNumPackets(int active_ports);
427 
428  //jack<->buffer
429  int RenderFromJackPorts();
430  void RenderToJackPorts();
431 
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);
435  };
436 
437  //utility *************************************************************************************
438 
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);
462 }