Jack2  1.9.8
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 
25 namespace Jack
26 {
27 
28 #define DEFAULT_MULTICAST_IP "225.3.19.154"
29 #define DEFAULT_PORT 19000
30 #define DEFAULT_MTU 1500
31 
32 #define SLAVE_SETUP_RETRY 5
33 
34 #define MANAGER_INIT_TIMEOUT 2000000 // in usec
35 #define MASTER_INIT_TIMEOUT 1000000 // in usec
36 #define SLAVE_INIT_TIMEOUT 1000000 // in usec
37 
38 #define NETWORK_MAX_LATENCY 20
39 
44  class SERVER_EXPORT JackNetInterface
45  {
46 
47  protected:
48 
49  void Initialize();
50 
51  session_params_t fParams;
52  JackNetSocket fSocket;
53  char fMulticastIP[32];
54 
55  // headers
56  packet_header_t fTxHeader;
57  packet_header_t fRxHeader;
58 
59  // transport
60  net_transport_data_t fSendTransportData;
61  net_transport_data_t fReturnTransportData;
62 
63  // network buffers
64  char* fTxBuffer;
65  char* fRxBuffer;
66  char* fTxData;
67  char* fRxData;
68 
69  // JACK buffers
70  NetMidiBuffer* fNetMidiCaptureBuffer;
71  NetMidiBuffer* fNetMidiPlaybackBuffer;
72  NetAudioBuffer* fNetAudioCaptureBuffer;
73  NetAudioBuffer* fNetAudioPlaybackBuffer;
74 
75  // utility methods
76  int SetNetBufferSize();
77  void FreeNetworkBuffers();
78 
79  // virtual methods : depends on the sub class master/slave
80  virtual bool SetParams();
81  virtual bool Init() = 0;
82 
83  // transport
84  virtual void EncodeTransportData() = 0;
85  virtual void DecodeTransportData() = 0;
86 
87  // sync packet
88  virtual void EncodeSyncPacket() = 0;
89  virtual void DecodeSyncPacket() = 0;
90 
91  virtual int SyncRecv() = 0;
92  virtual int SyncSend() = 0;
93  virtual int DataRecv() = 0;
94  virtual int DataSend() = 0;
95 
96  virtual int Send(size_t size, int flags) = 0;
97  virtual int Recv(size_t size, int flags) = 0;
98 
99  virtual void FatalRecvError() = 0;
100  virtual void FatalSendError() = 0;
101 
102  int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
103  int AudioSend(NetAudioBuffer* buffer, int audio_channels);
104 
105  int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
106  int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
107 
108  int FinishRecv(NetAudioBuffer* buffer);
109 
110  NetAudioBuffer* AudioBufferFactory(int nports, char* buffer);
111 
112  public:
113 
115  JackNetInterface(const char* multicast_ip, int port);
116  JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
117 
118  virtual ~JackNetInterface();
119 
120  };
121 
126  class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
127  {
128 
129  protected:
130 
131  bool fRunning;
132 
133  int fCurrentCycleOffset;
134  int fMaxCycleOffset;
135  int fLastfCycleOffset;
136 
137  bool Init();
138  int SetRxTimeout();
139  bool SetParams();
140 
141  void Exit();
142 
143  int SyncRecv();
144  int SyncSend();
145 
146  int DataRecv();
147  int DataSend();
148 
149  // sync packet
150  void EncodeSyncPacket();
151  void DecodeSyncPacket();
152 
153  int Send(size_t size, int flags);
154  int Recv(size_t size, int flags);
155 
156  bool IsSynched();
157 
158  void FatalRecvError();
159  void FatalSendError();
160 
161  public:
162 
163  JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0), fLastfCycleOffset(0)
164  {}
165  JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
166  : JackNetInterface(params, socket, multicast_ip)
167  {}
168 
169  virtual~JackNetMasterInterface()
170  {}
171  };
172 
177  class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
178  {
179 
180  protected:
181 
182  static uint fSlaveCounter;
183 
184  bool Init();
185  bool InitConnection(int time_out_sec);
186  bool InitRendering();
187 
188  net_status_t SendAvailableToMaster(long count = LONG_MAX); // long here (and not int...)
189  net_status_t SendStartToMaster();
190 
191  bool SetParams();
192 
193  int SyncRecv();
194  int SyncSend();
195 
196  int DataRecv();
197  int DataSend();
198 
199  // sync packet
200  void EncodeSyncPacket();
201  void DecodeSyncPacket();
202 
203  int Recv(size_t size, int flags);
204  int Send(size_t size, int flags);
205 
206  void FatalRecvError();
207  void FatalSendError();
208 
209  void InitAPI()
210  {
211  // open Socket API with the first slave
212  if (fSlaveCounter++ == 0) {
213  if (SocketAPIInit() < 0) {
214  jack_error("Can't init Socket API, exiting...");
215  throw std::bad_alloc();
216  }
217  }
218  }
219 
220  public:
221 
223  {
224  InitAPI();
225  }
226 
227  JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
228  {
229  InitAPI();
230  }
231 
232  virtual ~JackNetSlaveInterface()
233  {
234  // close Socket API with the last slave
235  if (--fSlaveCounter == 0) {
236  SocketAPIEnd();
237  }
238  }
239  };
240 }
241 
242 #endif