Jack2  1.9.12
JackNetInterface.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 #ifndef __JackNetInterface__
21 #define __JackNetInterface__
22 
23 #include "JackNetTool.h"
24 #include <limits.h>
25 
26 namespace Jack
27 {
28 
29 #define DEFAULT_MULTICAST_IP "225.3.19.154"
30 #define DEFAULT_PORT 19000
31 #define DEFAULT_MTU 1500
32 #define MAX_MTU 9000
33 
34 #define SLAVE_SETUP_RETRY 5
35 
36 #define MANAGER_INIT_TIMEOUT 1000000 * 2 // in usec
37 #define MASTER_INIT_TIMEOUT 1000000 * 10 // in usec
38 #define SLAVE_INIT_TIMEOUT 1000000 * 1 // in usec
39 #define PACKET_TIMEOUT 1000000 // in usec
40 
41 #define NETWORK_DEFAULT_LATENCY 2
42 #define NETWORK_MAX_LATENCY 30 // maximum possible latency in network master/slave loop
43 
48  class SERVER_EXPORT JackNetInterface
49  {
50 
51  friend class JackNetExt;
52 
53  protected:
54 
55  bool fSetTimeOut;
56  int fPacketTimeOut;
57 
58  void Initialize();
59 
60  session_params_t fParams;
61  JackNetSocket fSocket;
62  char fMulticastIP[32];
63 
64  // headers
65  packet_header_t fTxHeader;
66  packet_header_t fRxHeader;
67 
68  // transport
69  net_transport_data_t fSendTransportData;
70  net_transport_data_t fReturnTransportData;
71 
72  // network buffers
73  char* fTxBuffer;
74  char* fRxBuffer;
75  char* fTxData;
76  char* fRxData;
77 
78  // JACK buffers
79  NetMidiBuffer* fNetMidiCaptureBuffer;
80  NetMidiBuffer* fNetMidiPlaybackBuffer;
81  NetAudioBuffer* fNetAudioCaptureBuffer;
82  NetAudioBuffer* fNetAudioPlaybackBuffer;
83 
84  // utility methods
85  int SetNetBufferSize();
86  void FreeNetworkBuffers();
87 
88  // virtual methods : depends on the sub class master/slave
89  virtual bool SetParams();
90  virtual bool Init() = 0;
91 
92  // transport
93  virtual void EncodeTransportData() = 0;
94  virtual void DecodeTransportData() = 0;
95 
96  // sync packet
97  virtual void EncodeSyncPacket(int frames = -1) = 0;
98  virtual void DecodeSyncPacket(int& frames) = 0;
99 
100  virtual int SyncRecv() = 0;
101  virtual int SyncSend() = 0;
102  virtual int DataRecv() = 0;
103  virtual int DataSend() = 0;
104 
105  virtual int Send(size_t size, int flags) = 0;
106  virtual int Recv(size_t size, int flags) = 0;
107 
108  virtual void FatalRecvError() = 0;
109  virtual void FatalSendError() = 0;
110 
111  int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
112  int AudioSend(NetAudioBuffer* buffer, int audio_channels);
113 
114  int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
115  int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
116 
117  int FinishRecv(NetAudioBuffer* buffer);
118 
119  void SetRcvTimeOut();
120  void SetPacketTimeOut(int time_out)
121  {
122  // New time out
123  fPacketTimeOut = time_out;
124  fSetTimeOut = false;
125  }
126 
127  NetAudioBuffer* AudioBufferFactory(int nports, char* buffer);
128 
129  public:
130 
132  JackNetInterface(const char* multicast_ip, int port);
133  JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
134 
135  virtual ~JackNetInterface();
136 
137  };
138 
143  class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
144  {
145 
146  protected:
147 
148  bool fRunning;
149  int fCurrentCycleOffset;
150  int fMaxCycleOffset;
151  bool fSynched;
152 
153  bool Init();
154  bool SetParams();
155 
156  void Exit();
157 
158  int SyncRecv();
159  int SyncSend();
160 
161  int DataRecv();
162  int DataSend();
163 
164  // sync packet
165  void EncodeSyncPacket(int frames = -1);
166  void DecodeSyncPacket(int& frames);
167 
168  int Send(size_t size, int flags);
169  int Recv(size_t size, int flags);
170 
171  void FatalRecvError();
172  void FatalSendError();
173 
174  public:
175 
177  : JackNetInterface(),
178  fRunning(false),
179  fCurrentCycleOffset(0),
180  fMaxCycleOffset(0),
181  fSynched(false)
182  {}
183  JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
184  : JackNetInterface(params, socket, multicast_ip),
185  fRunning(false),
186  fCurrentCycleOffset(0),
187  fMaxCycleOffset(0),
188  fSynched(false)
189  {}
190 
191  virtual~JackNetMasterInterface()
192  {}
193  };
194 
199  class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
200  {
201 
202  protected:
203 
204  static uint fSlaveCounter;
205 
206  bool Init();
207  bool InitConnection(int time_out_sec);
208  bool InitRendering();
209 
210  net_status_t SendAvailableToMaster(int count = INT_MAX);
211  net_status_t SendStartToMaster();
212 
213  bool SetParams();
214 
215  int SyncRecv();
216  int SyncSend();
217 
218  int DataRecv();
219  int DataSend();
220 
221  // sync packet
222  void EncodeSyncPacket(int frames = -1);
223  void DecodeSyncPacket(int& frames);
224 
225  int Recv(size_t size, int flags);
226  int Send(size_t size, int flags);
227 
228  void FatalRecvError();
229  void FatalSendError();
230 
231  void InitAPI();
232 
233  public:
234 
236  {
237  InitAPI();
238  }
239 
240  JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
241  {
242  InitAPI();
243  }
244 
245  virtual ~JackNetSlaveInterface()
246  {
247  // close Socket API with the last slave
248  if (--fSlaveCounter == 0) {
249  SocketAPIEnd();
250  }
251  }
252  };
253 }
254 
255 #endif