Jack2 1.9.8
|
00001 /* 00002 Copyright (C) 2008-2011 Romain Moret at Grame 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 00018 */ 00019 00020 #ifndef __JackNetInterface__ 00021 #define __JackNetInterface__ 00022 00023 #include "JackNetTool.h" 00024 00025 namespace Jack 00026 { 00027 00028 #define DEFAULT_MULTICAST_IP "225.3.19.154" 00029 #define DEFAULT_PORT 19000 00030 #define DEFAULT_MTU 1500 00031 00032 #define SLAVE_SETUP_RETRY 5 00033 00034 #define MANAGER_INIT_TIMEOUT 2000000 // in usec 00035 #define MASTER_INIT_TIMEOUT 1000000 // in usec 00036 #define SLAVE_INIT_TIMEOUT 1000000 // in usec 00037 00038 #define NETWORK_MAX_LATENCY 20 00039 00044 class SERVER_EXPORT JackNetInterface 00045 { 00046 00047 protected: 00048 00049 void Initialize(); 00050 00051 session_params_t fParams; 00052 JackNetSocket fSocket; 00053 char fMulticastIP[32]; 00054 00055 // headers 00056 packet_header_t fTxHeader; 00057 packet_header_t fRxHeader; 00058 00059 // transport 00060 net_transport_data_t fSendTransportData; 00061 net_transport_data_t fReturnTransportData; 00062 00063 // network buffers 00064 char* fTxBuffer; 00065 char* fRxBuffer; 00066 char* fTxData; 00067 char* fRxData; 00068 00069 // JACK buffers 00070 NetMidiBuffer* fNetMidiCaptureBuffer; 00071 NetMidiBuffer* fNetMidiPlaybackBuffer; 00072 NetAudioBuffer* fNetAudioCaptureBuffer; 00073 NetAudioBuffer* fNetAudioPlaybackBuffer; 00074 00075 // utility methods 00076 int SetNetBufferSize(); 00077 void FreeNetworkBuffers(); 00078 00079 // virtual methods : depends on the sub class master/slave 00080 virtual bool SetParams(); 00081 virtual bool Init() = 0; 00082 00083 // transport 00084 virtual void EncodeTransportData() = 0; 00085 virtual void DecodeTransportData() = 0; 00086 00087 // sync packet 00088 virtual void EncodeSyncPacket() = 0; 00089 virtual void DecodeSyncPacket() = 0; 00090 00091 virtual int SyncRecv() = 0; 00092 virtual int SyncSend() = 0; 00093 virtual int DataRecv() = 0; 00094 virtual int DataSend() = 0; 00095 00096 virtual int Send(size_t size, int flags) = 0; 00097 virtual int Recv(size_t size, int flags) = 0; 00098 00099 virtual void FatalRecvError() = 0; 00100 virtual void FatalSendError() = 0; 00101 00102 int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels); 00103 int AudioSend(NetAudioBuffer* buffer, int audio_channels); 00104 00105 int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt); 00106 int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer); 00107 00108 int FinishRecv(NetAudioBuffer* buffer); 00109 00110 NetAudioBuffer* AudioBufferFactory(int nports, char* buffer); 00111 00112 public: 00113 00114 JackNetInterface(); 00115 JackNetInterface(const char* multicast_ip, int port); 00116 JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip); 00117 00118 virtual ~JackNetInterface(); 00119 00120 }; 00121 00126 class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface 00127 { 00128 00129 protected: 00130 00131 bool fRunning; 00132 00133 int fCurrentCycleOffset; 00134 int fMaxCycleOffset; 00135 int fLastfCycleOffset; 00136 00137 bool Init(); 00138 int SetRxTimeout(); 00139 bool SetParams(); 00140 00141 void Exit(); 00142 00143 int SyncRecv(); 00144 int SyncSend(); 00145 00146 int DataRecv(); 00147 int DataSend(); 00148 00149 // sync packet 00150 void EncodeSyncPacket(); 00151 void DecodeSyncPacket(); 00152 00153 int Send(size_t size, int flags); 00154 int Recv(size_t size, int flags); 00155 00156 bool IsSynched(); 00157 00158 void FatalRecvError(); 00159 void FatalSendError(); 00160 00161 public: 00162 00163 JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0), fLastfCycleOffset(0) 00164 {} 00165 JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip) 00166 : JackNetInterface(params, socket, multicast_ip) 00167 {} 00168 00169 virtual~JackNetMasterInterface() 00170 {} 00171 }; 00172 00177 class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface 00178 { 00179 00180 protected: 00181 00182 static uint fSlaveCounter; 00183 00184 bool Init(); 00185 bool InitConnection(int time_out_sec); 00186 bool InitRendering(); 00187 00188 net_status_t SendAvailableToMaster(long count = LONG_MAX); // long here (and not int...) 00189 net_status_t SendStartToMaster(); 00190 00191 bool SetParams(); 00192 00193 int SyncRecv(); 00194 int SyncSend(); 00195 00196 int DataRecv(); 00197 int DataSend(); 00198 00199 // sync packet 00200 void EncodeSyncPacket(); 00201 void DecodeSyncPacket(); 00202 00203 int Recv(size_t size, int flags); 00204 int Send(size_t size, int flags); 00205 00206 void FatalRecvError(); 00207 void FatalSendError(); 00208 00209 void InitAPI() 00210 { 00211 // open Socket API with the first slave 00212 if (fSlaveCounter++ == 0) { 00213 if (SocketAPIInit() < 0) { 00214 jack_error("Can't init Socket API, exiting..."); 00215 throw std::bad_alloc(); 00216 } 00217 } 00218 } 00219 00220 public: 00221 00222 JackNetSlaveInterface() : JackNetInterface() 00223 { 00224 InitAPI(); 00225 } 00226 00227 JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port) 00228 { 00229 InitAPI(); 00230 } 00231 00232 virtual ~JackNetSlaveInterface() 00233 { 00234 // close Socket API with the last slave 00235 if (--fSlaveCounter == 0) { 00236 SocketAPIEnd(); 00237 } 00238 } 00239 }; 00240 } 00241 00242 #endif