Jack2  1.9.10
JackNetInterface.h
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 #include <limits.h>
00025 
00026 namespace Jack
00027 {
00028 
00029 #define DEFAULT_MULTICAST_IP "225.3.19.154"
00030 #define DEFAULT_PORT    19000
00031 #define DEFAULT_MTU     1500
00032 #define MAX_MTU         9000
00033 
00034 #define SLAVE_SETUP_RETRY   5
00035 
00036 #define MANAGER_INIT_TIMEOUT    1000000 * 2   // in usec
00037 #define MASTER_INIT_TIMEOUT     1000000 * 10  // in usec
00038 #define SLAVE_INIT_TIMEOUT      1000000 * 1   // in usec
00039 #define PACKET_TIMEOUT          500000        // in usec
00040 
00041 #define NETWORK_DEFAULT_LATENCY     2  
00042 #define NETWORK_MAX_LATENCY         30  // maximum possible latency in network master/slave loop
00043 
00048     class SERVER_EXPORT JackNetInterface
00049     {
00050 
00051         friend class JackNetExt;
00052         
00053         protected:
00054         
00055             bool fSetTimeOut;
00056             int fPacketTimeOut;
00057 
00058             void Initialize();
00059 
00060             session_params_t fParams;
00061             JackNetSocket fSocket;
00062             char fMulticastIP[32];
00063 
00064             // headers
00065             packet_header_t fTxHeader;
00066             packet_header_t fRxHeader;
00067 
00068             // transport
00069             net_transport_data_t fSendTransportData;
00070             net_transport_data_t fReturnTransportData;
00071 
00072             // network buffers
00073             char* fTxBuffer;
00074             char* fRxBuffer;
00075             char* fTxData;
00076             char* fRxData;
00077 
00078             // JACK buffers
00079             NetMidiBuffer* fNetMidiCaptureBuffer;
00080             NetMidiBuffer* fNetMidiPlaybackBuffer;
00081             NetAudioBuffer* fNetAudioCaptureBuffer;
00082             NetAudioBuffer* fNetAudioPlaybackBuffer;
00083 
00084             // utility methods
00085             int SetNetBufferSize();
00086             void FreeNetworkBuffers();
00087 
00088             // virtual methods : depends on the sub class master/slave
00089             virtual bool SetParams();
00090             virtual bool Init() = 0;
00091 
00092             // transport
00093             virtual void EncodeTransportData() = 0;
00094             virtual void DecodeTransportData() = 0;
00095 
00096             // sync packet
00097             virtual void EncodeSyncPacket(int frames = -1) = 0;
00098             virtual void DecodeSyncPacket(int& frames) = 0;
00099 
00100             virtual int SyncRecv() = 0;
00101             virtual int SyncSend() = 0;
00102             virtual int DataRecv() = 0;
00103             virtual int DataSend() = 0;
00104 
00105             virtual int Send(size_t size, int flags) = 0;
00106             virtual int Recv(size_t size, int flags) = 0;
00107 
00108             virtual void FatalRecvError() = 0;
00109             virtual void FatalSendError() = 0;
00110 
00111             int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
00112             int AudioSend(NetAudioBuffer* buffer, int audio_channels);
00113 
00114             int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
00115             int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
00116 
00117             int FinishRecv(NetAudioBuffer* buffer);
00118             
00119             void SetRcvTimeOut();
00120             void SetPacketTimeOut(int time_out)
00121             {
00122                 // New time out
00123                 fPacketTimeOut = time_out;
00124                 fSetTimeOut = false;
00125             }
00126 
00127             NetAudioBuffer* AudioBufferFactory(int nports, char* buffer);
00128 
00129         public:
00130 
00131             JackNetInterface();
00132             JackNetInterface(const char* multicast_ip, int port);
00133             JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
00134 
00135             virtual ~JackNetInterface();
00136 
00137     };
00138 
00143     class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
00144     {
00145 
00146         protected:
00147 
00148             bool fRunning;
00149             int fCurrentCycleOffset;
00150             int fMaxCycleOffset;
00151             bool fSynched;
00152        
00153             bool Init();
00154             bool SetParams();
00155 
00156             void Exit();
00157 
00158             int SyncRecv();
00159             int SyncSend();
00160 
00161             int DataRecv();
00162             int DataSend();
00163 
00164             // sync packet
00165             void EncodeSyncPacket(int frames = -1);
00166             void DecodeSyncPacket(int& frames);
00167 
00168             int Send(size_t size, int flags);
00169             int Recv(size_t size, int flags);
00170 
00171             void FatalRecvError();
00172             void FatalSendError();
00173 
00174         public:
00175 
00176             JackNetMasterInterface() 
00177                 : JackNetInterface(), 
00178                 fRunning(false), 
00179                 fCurrentCycleOffset(0), 
00180                 fMaxCycleOffset(0), 
00181                 fSynched(false)
00182             {}
00183             JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
00184                     : JackNetInterface(params, socket, multicast_ip), 
00185                     fRunning(false), 
00186                     fCurrentCycleOffset(0), 
00187                     fMaxCycleOffset(0), 
00188                     fSynched(false)
00189             {}
00190 
00191             virtual~JackNetMasterInterface()
00192             {}
00193     };
00194 
00199     class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
00200     {
00201 
00202         protected:
00203 
00204             static uint fSlaveCounter;
00205 
00206             bool Init();
00207             bool InitConnection(int time_out_sec);
00208             bool InitRendering();
00209 
00210             net_status_t SendAvailableToMaster(int count = INT_MAX); 
00211             net_status_t SendStartToMaster();
00212 
00213             bool SetParams();
00214 
00215             int SyncRecv();
00216             int SyncSend();
00217 
00218             int DataRecv();
00219             int DataSend();
00220 
00221             // sync packet
00222             void EncodeSyncPacket(int frames = -1);
00223             void DecodeSyncPacket(int& frames);
00224 
00225             int Recv(size_t size, int flags);
00226             int Send(size_t size, int flags);
00227 
00228             void FatalRecvError();
00229             void FatalSendError();
00230 
00231             void InitAPI();
00232 
00233         public:
00234 
00235             JackNetSlaveInterface() : JackNetInterface()
00236             {
00237                 InitAPI();
00238             }
00239 
00240             JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
00241             {
00242                 InitAPI();
00243             }
00244 
00245             virtual ~JackNetSlaveInterface()
00246             {
00247                 // close Socket API with the last slave
00248                 if (--fSlaveCounter == 0) {
00249                     SocketAPIEnd();
00250                 }
00251             }
00252     };
00253 }
00254 
00255 #endif