Merge branch 'master' into develop
[jack2.git] / common / JackNetInterface.h
blobc05979b0c1fb722e23c89cfebe00912fa367135e
1 /*
2 Copyright (C) 2008-2011 Romain Moret at Grame
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.
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.
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.
20 #ifndef __JackNetInterface__
21 #define __JackNetInterface__
23 #include "JackNetTool.h"
24 #include <limits.h>
26 namespace Jack
29 #define DEFAULT_MULTICAST_IP "225.3.19.154"
30 #define DEFAULT_PORT 19000
31 #define DEFAULT_MTU 1500
32 #define MAX_MTU 9000
34 #define SLAVE_SETUP_RETRY 5
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
41 #define NETWORK_DEFAULT_LATENCY 2
42 #define NETWORK_MAX_LATENCY 30 // maximum possible latency in network master/slave loop
44 /**
45 \Brief This class describes the basic Net Interface, used by both master and slave.
48 class SERVER_EXPORT JackNetInterface
51 friend class JackNetExt;
53 protected:
55 bool fSetTimeOut;
56 int fPacketTimeOut;
58 void Initialize();
60 session_params_t fParams;
61 JackNetSocket fSocket;
62 char fMulticastIP[32];
64 // headers
65 packet_header_t fTxHeader;
66 packet_header_t fRxHeader;
68 // transport
69 net_transport_data_t fSendTransportData;
70 net_transport_data_t fReturnTransportData;
72 // network buffers
73 char* fTxBuffer;
74 char* fRxBuffer;
75 char* fTxData;
76 char* fRxData;
78 // JACK buffers
79 NetMidiBuffer* fNetMidiCaptureBuffer;
80 NetMidiBuffer* fNetMidiPlaybackBuffer;
81 NetAudioBuffer* fNetAudioCaptureBuffer;
82 NetAudioBuffer* fNetAudioPlaybackBuffer;
84 // utility methods
85 int SetNetBufferSize();
86 void FreeNetworkBuffers();
88 // virtual methods : depends on the sub class master/slave
89 virtual bool SetParams();
90 virtual bool Init() = 0;
92 // transport
93 virtual void EncodeTransportData() = 0;
94 virtual void DecodeTransportData() = 0;
96 // sync packet
97 virtual void EncodeSyncPacket(int frames = -1) = 0;
98 virtual void DecodeSyncPacket(int& frames) = 0;
100 virtual int SyncRecv() = 0;
101 virtual int SyncSend() = 0;
102 virtual int DataRecv() = 0;
103 virtual int DataSend() = 0;
105 virtual int Send(size_t size, int flags) = 0;
106 virtual int Recv(size_t size, int flags) = 0;
108 virtual void FatalRecvError() = 0;
109 virtual void FatalSendError() = 0;
111 int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
112 int AudioSend(NetAudioBuffer* buffer, int audio_channels);
114 int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
115 int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
117 int FinishRecv(NetAudioBuffer* buffer);
119 void SetRcvTimeOut();
120 void SetPacketTimeOut(int time_out)
122 // New time out
123 fPacketTimeOut = time_out;
124 fSetTimeOut = false;
127 NetAudioBuffer* AudioBufferFactory(int nports, char* buffer);
129 public:
131 JackNetInterface();
132 JackNetInterface(const char* multicast_ip, int port);
133 JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
135 virtual ~JackNetInterface();
140 \Brief This class describes the Net Interface for masters (NetMaster)
143 class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
146 protected:
148 bool fRunning;
149 int fCurrentCycleOffset;
150 int fMaxCycleOffset;
151 bool fSynched;
153 bool Init();
154 bool SetParams();
156 void Exit();
158 int SyncRecv();
159 int SyncSend();
161 int DataRecv();
162 int DataSend();
164 // sync packet
165 void EncodeSyncPacket(int frames = -1);
166 void DecodeSyncPacket(int& frames);
168 int Send(size_t size, int flags);
169 int Recv(size_t size, int flags);
171 void FatalRecvError();
172 void FatalSendError();
174 public:
176 JackNetMasterInterface()
177 : JackNetInterface(),
178 fRunning(false),
179 fCurrentCycleOffset(0),
180 fMaxCycleOffset(0),
181 fSynched(false)
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)
191 virtual~JackNetMasterInterface()
196 \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
199 class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
202 protected:
204 static uint fSlaveCounter;
206 bool Init();
207 bool InitConnection(int time_out_sec);
208 bool InitRendering();
210 net_status_t SendAvailableToMaster(int count = INT_MAX);
211 net_status_t SendStartToMaster();
213 bool SetParams();
215 int SyncRecv();
216 int SyncSend();
218 int DataRecv();
219 int DataSend();
221 // sync packet
222 void EncodeSyncPacket(int frames = -1);
223 void DecodeSyncPacket(int& frames);
225 int Recv(size_t size, int flags);
226 int Send(size_t size, int flags);
228 void FatalRecvError();
229 void FatalSendError();
231 void InitAPI();
233 public:
235 JackNetSlaveInterface() : JackNetInterface()
237 InitAPI();
240 JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
242 InitAPI();
245 virtual ~JackNetSlaveInterface()
247 // close Socket API with the last slave
248 if (--fSlaveCounter == 0) {
249 SocketAPIEnd();
255 #endif