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