Jack2  1.9.10
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 NETWORK_PROTOCOL 8
42 
43 #define NET_SYNCHING 0
44 #define SYNC_PACKET_ERROR -2
45 #define DATA_PACKET_ERROR -3
46 
47 #define OPTIMIZED_PROTOCOL 1
48 
49 #define HEADER_SIZE (sizeof(packet_header_t))
50 #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - sizeof(packet_header_t))
51 
52 namespace Jack
53 {
54  typedef struct _session_params session_params_t;
55  typedef struct _packet_header packet_header_t;
56  typedef struct _net_transport_data net_transport_data_t;
57  typedef struct sockaddr socket_address_t;
58  typedef struct in_addr address_t;
59  typedef jack_default_audio_sample_t sample_t;
60 
61  enum JackNetEncoder {
62 
63  JackFloatEncoder = 0,
64  JackIntEncoder = 1,
65  JackCeltEncoder = 2,
66  JackOpusEncoder = 3,
67  };
68 
69 //session params ******************************************************************************
70 
88  PRE_PACKED_STRUCTURE
90  {
91  char fPacketType[8]; //packet type ('param')
92  uint32_t fProtocolVersion; //version
93  int32_t fPacketID; //indicates the packet type
94  char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
95  char fMasterNetName[JACK_SERVER_NAME_SIZE]; //master hostname (network)
96  char fSlaveNetName[JACK_SERVER_NAME_SIZE]; //slave hostname (network)
97  uint32_t fMtu; //connection mtu
98  uint32_t fID; //slave's ID
99  uint32_t fTransportSync; //is the transport synced ?
100  int32_t fSendAudioChannels; //number of master->slave channels
101  int32_t fReturnAudioChannels; //number of slave->master channels
102  int32_t fSendMidiChannels; //number of master->slave midi channels
103  int32_t fReturnMidiChannels; //number of slave->master midi channels
104  uint32_t fSampleRate; //session sample rate
105  uint32_t fPeriodSize; //period size
106  uint32_t fSampleEncoder; //samples encoder
107  uint32_t fKBps; //KB per second for CELT encoder
108  uint32_t fSlaveSyncMode; //is the slave in sync mode ?
109  uint32_t fNetworkLatency; //network latency
110  } POST_PACKED_STRUCTURE;
111 
112 //net status **********************************************************************************
113 
118  enum _net_status
119  {
120  NET_SOCKET_ERROR = 0,
121  NET_CONNECT_ERROR,
122  NET_ERROR,
123  NET_SEND_ERROR,
124  NET_RECV_ERROR,
125  NET_CONNECTED,
126  NET_ROLLING
127  };
128 
129  typedef enum _net_status net_status_t;
130 
131 //sync packet type ****************************************************************************
132 
137  enum _sync_packet_type
138  {
139  INVALID = 0, //...
140  SLAVE_AVAILABLE, //a slave is available
141  SLAVE_SETUP, //slave configuration
142  START_MASTER, //slave is ready, start master
143  START_SLAVE, //master is ready, activate slave
144  KILL_MASTER //master must stop
145  };
146 
147  typedef enum _sync_packet_type sync_packet_type_t;
148 
149 
150 //packet header *******************************************************************************
151 
171  PRE_PACKED_STRUCTURE
173  {
174  char fPacketType[8]; //packet type ('headr')
175  uint32_t fDataType; //'a' for audio, 'm' for midi and 's' for sync
176  uint32_t fDataStream; //'s' for send, 'r' for return
177  uint32_t fID; //unique ID of the slave
178  uint32_t fNumPacket; //number of data packets of the cycle
179  uint32_t fPacketSize; //packet size in bytes
180  uint32_t fActivePorts; //number of active ports
181  uint32_t fCycle; //process cycle counter
182  uint32_t fSubCycle; //midi/audio subcycle counter
183  int32_t fFrames; //process cycle size in frames (can be -1 to indicate entire buffer)
184  uint32_t fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
185  } POST_PACKED_STRUCTURE;
186 
187 //net timebase master
188 
193  enum _net_timebase_master
194  {
195  NO_CHANGE = 0,
196  RELEASE_TIMEBASEMASTER = 1,
197  TIMEBASEMASTER = 2,
198  CONDITIONAL_TIMEBASEMASTER = 3
199  };
200 
201  typedef enum _net_timebase_master net_timebase_master_t;
202 
203 
204 //transport data ******************************************************************************
205 
210  PRE_PACKED_STRUCTURE
212  {
213  uint32_t fNewState; //is it a state change
214  uint32_t fTimebaseMaster; //is there a new timebase master
215  int32_t fState; //current cycle state
216  jack_position_t fPosition; //current cycle position
217  } POST_PACKED_STRUCTURE;
218 
219  //midi data ***********************************************************************************
220 
236  class SERVER_EXPORT NetMidiBuffer
237  {
238  private:
239 
240  int fNPorts;
241  size_t fMaxBufsize;
242  int fMaxPcktSize;
243 
244  char* fBuffer;
245  char* fNetBuffer;
246  JackMidiBuffer** fPortBuffer;
247 
248  size_t fCycleBytesSize; // needed size in bytes ofr an entire cycle
249 
250  public:
251 
252  NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
253  ~NetMidiBuffer();
254 
255  void Reset();
256 
257  // needed size in bytes for an entire cycle
258  size_t GetCycleSize();
259  int GetNumPackets(int data_sizen, int max_size);
260 
261  void SetBuffer(int index, JackMidiBuffer* buffer);
262  JackMidiBuffer* GetBuffer(int index);
263 
264  //utility
265  void DisplayEvents();
266 
267  //jack<->buffer
268  int RenderFromJackPorts();
269  void RenderToJackPorts();
270 
271  //network<->buffer
272  void RenderFromNetwork(int sub_cycle, size_t copy_size);
273  int RenderToNetwork(int sub_cycle, size_t total_size);
274 
275  };
276 
277 // audio data *********************************************************************************
278 
279  class SERVER_EXPORT NetAudioBuffer
280  {
281 
282  protected:
283 
284  int fNPorts;
285  int fLastSubCycle;
286  int fNumPackets;
287 
288  char* fNetBuffer;
289  sample_t** fPortBuffer;
290  bool* fConnectedPorts;
291 
292  jack_nframes_t fPeriodSize;
293  jack_nframes_t fSubPeriodSize;
294  size_t fSubPeriodBytesSize;
295 
296  float fCycleDuration; // in sec
297  size_t fCycleBytesSize; // needed size in bytes for an entire cycle
298 
299  int CheckPacket(int cycle, int sub_cycle);
300  void NextCycle();
301  void Cleanup();
302 
303  public:
304 
305  NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
306  virtual ~NetAudioBuffer();
307 
308  bool GetConnected(int port_index) { return fConnectedPorts[port_index]; }
309  void SetConnected(int port_index, bool state) { fConnectedPorts[port_index] = state; }
310 
311  // needed syze in bytes ofr an entire cycle
312  virtual size_t GetCycleSize() = 0;
313 
314  // cycle duration in sec
315  virtual float GetCycleDuration() = 0;
316 
317  virtual int GetNumPackets(int active_ports) = 0;
318 
319  virtual void SetBuffer(int index, sample_t* buffer);
320  virtual sample_t* GetBuffer(int index);
321 
322  //jack<->buffer
323  virtual int RenderFromJackPorts(int nframes);
324  virtual void RenderToJackPorts(int nframes);
325 
326  //network<->buffer
327  virtual int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num) = 0;
328  virtual int RenderToNetwork(int sub_cycle, uint32_t port_num) = 0;
329 
330  virtual int ActivePortsToNetwork(char* net_buffer);
331  virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
332 
333  };
334 
335  class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
336  {
337 
338  private:
339 
340  int fPacketSize;
341 
342  void UpdateParams(int active_ports);
343 
344 
345  void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle);
346  void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle);
347 
348  public:
349 
350  NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
351  virtual ~NetFloatAudioBuffer();
352 
353  // needed size in bytes for an entire cycle
354  size_t GetCycleSize();
355 
356  // cycle duration in sec
357  float GetCycleDuration();
358  int GetNumPackets(int active_ports);
359 
360  //jack<->buffer
361  int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
362  int RenderToNetwork(int sub_cycle, uint32_t port_num);
363 
364  };
365 
366 #if HAVE_CELT
367 
368 #include <celt/celt.h>
369 
370  class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
371  {
372  private:
373 
374  CELTMode** fCeltMode;
375  CELTEncoder** fCeltEncoder;
376  CELTDecoder** fCeltDecoder;
377 
378  int fCompressedSizeByte;
379  unsigned char** fCompressedBuffer;
380 
381  size_t fLastSubPeriodBytesSize;
382 
383  void FreeCelt();
384 
385  public:
386 
387  NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
388  virtual ~NetCeltAudioBuffer();
389 
390  // needed size in bytes for an entire cycle
391  size_t GetCycleSize();
392 
393  // cycle duration in sec
394  float GetCycleDuration();
395  int GetNumPackets(int active_ports);
396 
397  //jack<->buffer
398  int RenderFromJackPorts(int nframes);
399  void RenderToJackPorts(int nframes);
400 
401  //network<->buffer
402  int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
403  int RenderToNetwork(int sub_cycle, uint32_t port_num);
404  };
405 
406 #endif
407 
408 #if HAVE_OPUS
409 
410 #include <opus/opus.h>
411 #include <opus/opus_custom.h>
412 
413  class SERVER_EXPORT NetOpusAudioBuffer : public NetAudioBuffer
414  {
415  private:
416 
417  OpusCustomMode** fOpusMode;
418  OpusCustomEncoder** fOpusEncoder;
419  OpusCustomDecoder** fOpusDecoder;
420 
421  int fCompressedMaxSizeByte;
422  unsigned short* fCompressedSizesByte;
423 
424  size_t fLastSubPeriodBytesSize;
425 
426  unsigned char** fCompressedBuffer;
427  void FreeOpus();
428 
429  public:
430 
431  NetOpusAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
432  virtual ~NetOpusAudioBuffer();
433 
434  // needed size in bytes for an entire cycle
435  size_t GetCycleSize();
436 
437  // cycle duration in sec
438  float GetCycleDuration();
439  int GetNumPackets(int active_ports);
440 
441  //jack<->buffer
442  int RenderFromJackPorts(int nframes);
443  void RenderToJackPorts(int nframes);
444 
445  //network<->buffer
446  int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
447  int RenderToNetwork(int sub_cycle, uint32_t port_num);
448  };
449 
450 #endif
451 
452  class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
453  {
454  private:
455 
456  int fCompressedSizeByte;
457 
458  size_t fLastSubPeriodBytesSize;
459 
460  short** fIntBuffer;
461 
462  public:
463 
464  NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
465  virtual ~NetIntAudioBuffer();
466 
467  // needed size in bytes for an entire cycle
468  size_t GetCycleSize();
469 
470  // cycle duration in sec
471  float GetCycleDuration();
472  int GetNumPackets(int active_ports);
473 
474  //jack<->buffer
475  int RenderFromJackPorts(int nframes);
476  void RenderToJackPorts(int nframes);
477 
478  //network<->buffer
479  int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
480  int RenderToNetwork(int sub_cycle, uint32_t port_num);
481  };
482 
483  //utility *************************************************************************************
484 
485  //socket API management
486  SERVER_EXPORT int SocketAPIInit();
487  SERVER_EXPORT int SocketAPIEnd();
488  //n<-->h functions
489  SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
490  SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
491  SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
492  SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
493  SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
494  SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
495  SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
496  SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
497  //display session parameters
498  SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
499  //display packet header
500  SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
501  //get the packet type from a sesion parameters
502  SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
503  //set the packet type in a session parameters
504  SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
505  //transport utility
506  SERVER_EXPORT const char* GetTransportState(int transport_state);
507  SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);
508 }
This structure containes master/slave connection parameters, it's used to setup the whole system...
Definition: JackNetTool.h:89