root/jack2/branches/libjacknet/common/JackNetInterface.h

Revision 3932, 6.3 kB (checked in by sletz, 6 months ago)

More flexible handling of network audio buffers.

Line 
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2008 Romain Moret at Grame
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __JackNetInterface__
22 #define __JackNetInterface__
23
24 #include "JackNetTool.h"
25
26 namespace Jack
27 {
28     /**
29     \Brief This class describes the basic Net Interface, used by both master and slave
30     */
31
32     class SERVER_EXPORT JackNetInterface
33     {
34         protected:
35             session_params_t fParams;
36             JackNetSocket fSocket;
37             char fMulticastIP[32];
38             uint fNSubProcess;
39
40             //headers
41             packet_header_t fTxHeader;
42             packet_header_t fRxHeader;
43            
44             // transport
45             net_transport_data_t fSendTransportData;
46             net_transport_data_t fReturnTransportData;
47
48             //network buffers
49             char* fTxBuffer;
50             char* fRxBuffer;
51             char* fTxData;
52             char* fRxData;
53
54             //jack buffers
55             NetMidiBuffer* fNetMidiCaptureBuffer;
56             NetMidiBuffer* fNetMidiPlaybackBuffer;
57             NetAudioBuffer* fNetAudioCaptureBuffer;
58             NetAudioBuffer* fNetAudioPlaybackBuffer;
59
60             //sizes
61             int fAudioRxLen;
62             int fAudioTxLen;
63             int fPayloadSize;
64
65             //utility methods
66             void SetFramesPerPacket();
67             int SetNetBufferSize();
68             int GetNMidiPckt();
69             bool IsNextPacket();
70
71             //virtual methods : depends on the sub class master/slave
72             virtual void SetParams();
73             virtual bool Init() = 0;
74
75             //transport
76             virtual void EncodeTransportData() = 0;
77             virtual void DecodeTransportData() = 0;
78
79             //sync packet
80             virtual void EncodeSyncPacket() = 0;
81             virtual void DecodeSyncPacket() = 0;
82
83             virtual int SyncRecv() = 0;
84             virtual int SyncSend() = 0;
85             virtual int DataRecv() = 0;
86             virtual int DataSend() = 0;
87
88             virtual int Send ( size_t size, int flags ) = 0;
89             virtual int Recv ( size_t size, int flags ) = 0;
90
91             JackNetInterface();
92             JackNetInterface ( const char* multicast_ip, int port );
93             JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip );
94
95         public:
96             virtual ~JackNetInterface();
97     };
98
99     /**
100     \Brief This class describes the Net Interface for masters (NetMaster)
101     */
102
103     class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
104     {
105         protected:
106             bool fRunning;
107             int fCycleOffset;
108
109             bool Init();
110             int SetRxTimeout();
111             void SetParams();
112            
113             void Exit();
114            
115             int SyncRecv();
116             int SyncSend();
117            
118             int DataRecv();
119             int DataSend();
120            
121              //sync packet
122             void EncodeSyncPacket();
123             void DecodeSyncPacket();
124
125             int Send ( size_t size, int flags );
126             int Recv ( size_t size, int flags );
127            
128             bool IsSynched();
129
130         public:
131             JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCycleOffset(0)
132             {}
133             JackNetMasterInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip )
134                     : JackNetInterface ( params, socket, multicast_ip )
135             {}
136             ~JackNetMasterInterface()
137             {}
138     };
139
140     /**
141     \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
142     */
143
144     class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
145     {
146         protected:
147        
148             static uint fSlaveCounter;
149        
150             bool Init();
151             bool InitConnection();
152             bool InitRendering();
153            
154             net_status_t SendAvailableToMaster();
155             net_status_t SendStartToMaster();
156            
157             void SetParams();
158            
159             int SyncRecv();
160             int SyncSend();
161            
162             int DataRecv();
163             int DataSend();
164            
165             //sync packet
166             void EncodeSyncPacket();
167             void DecodeSyncPacket();
168
169             int Recv ( size_t size, int flags );
170             int Send ( size_t size, int flags );
171
172         public:
173             JackNetSlaveInterface() : JackNetInterface()
174             {
175                 //open Socket API with the first slave
176                 if ( fSlaveCounter++ == 0 )
177                 {
178                     if ( SocketAPIInit() < 0 )
179                     {
180                         jack_error ( "Can't init Socket API, exiting..." );
181                         throw -1;
182                     }
183                 }
184             }
185             JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port )
186             {
187                 //open Socket API with the first slave
188                 if ( fSlaveCounter++ == 0 )
189                 {
190                     if ( SocketAPIInit() < 0 )
191                     {
192                         jack_error ( "Can't init Socket API, exiting..." );
193                         throw -1;
194                     }
195                 }
196             }
197             ~JackNetSlaveInterface()
198             {
199                 //close Socket API with the last slave
200                 if ( --fSlaveCounter == 0 )
201                     SocketAPIEnd();
202             }
203     };
204 }
205
206 #define DEFAULT_MULTICAST_IP "225.3.19.154"
207 #define DEFAULT_PORT 19000
208 #define DEFAULT_MTU 1500
209
210 #define SLAVE_SETUP_RETRY 5
211
212 #define MASTER_INIT_TIMEOUT 1000000     // in usec
213 #define SLAVE_INIT_TIMEOUT 2000000      // in usec
214
215 #define CYCLE_OFFSET_SLOW 2
216 #define MAX_LATENCY CYCLE_OFFSET_SLOW * 4
217
218 #endif
Note: See TracBrowser for help on using the browser.