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"
29 #define DEFAULT_MULTICAST_IP "225.3.19.154"
30 #define DEFAULT_PORT 19000
31 #define DEFAULT_MTU 1500
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
45 \Brief This class describes the basic Net Interface, used by both master and slave.
48 class SERVER_EXPORT JackNetInterface
51 friend class JackNetExt
;
60 session_params_t fParams
;
61 JackNetSocket fSocket
;
62 char fMulticastIP
[32];
65 packet_header_t fTxHeader
;
66 packet_header_t fRxHeader
;
69 net_transport_data_t fSendTransportData
;
70 net_transport_data_t fReturnTransportData
;
79 NetMidiBuffer
* fNetMidiCaptureBuffer
;
80 NetMidiBuffer
* fNetMidiPlaybackBuffer
;
81 NetAudioBuffer
* fNetAudioCaptureBuffer
;
82 NetAudioBuffer
* fNetAudioPlaybackBuffer
;
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;
93 virtual void EncodeTransportData() = 0;
94 virtual void DecodeTransportData() = 0;
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
)
123 fPacketTimeOut
= time_out
;
127 NetAudioBuffer
* AudioBufferFactory(int nports
, char* buffer
);
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
149 int fCurrentCycleOffset
;
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();
176 JackNetMasterInterface()
177 : JackNetInterface(),
179 fCurrentCycleOffset(0),
183 JackNetMasterInterface(session_params_t
& params
, JackNetSocket
& socket
, const char* multicast_ip
)
184 : JackNetInterface(params
, socket
, multicast_ip
),
186 fCurrentCycleOffset(0),
191 virtual~JackNetMasterInterface()
196 \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
199 class SERVER_EXPORT JackNetSlaveInterface
: public JackNetInterface
204 static uint fSlaveCounter
;
207 bool InitConnection(int time_out_sec
);
208 bool InitRendering();
210 net_status_t
SendAvailableToMaster(int count
= INT_MAX
);
211 net_status_t
SendStartToMaster();
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();
235 JackNetSlaveInterface() : JackNetInterface()
240 JackNetSlaveInterface(const char* ip
, int port
) : JackNetInterface(ip
, port
)
245 virtual ~JackNetSlaveInterface()
247 // close Socket API with the last slave
248 if (--fSlaveCounter
== 0) {