jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.0KB

  1. /*
  2. Copyright (C) 2008-2011 Romain Moret at Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __JackNetInterface__
  16. #define __JackNetInterface__
  17. #include "JackNetTool.h"
  18. #include <limits.h>
  19. namespace Jack
  20. {
  21. #define DEFAULT_MULTICAST_IP "225.3.19.154"
  22. #define DEFAULT_PORT 19000
  23. #define DEFAULT_MTU 1500
  24. #define SLAVE_SETUP_RETRY 5
  25. #define MANAGER_INIT_TIMEOUT 1000000 * 2 // in usec
  26. #define MASTER_INIT_TIMEOUT 1000000 * 10 // in usec
  27. #define SLAVE_INIT_TIMEOUT 1000000 * 1 // in usec
  28. #define PACKET_TIMEOUT 500000 // in usec
  29. #define NETWORK_MAX_LATENCY 30 // maximum possible latency in network master/slave loop
  30. /**
  31. \Brief This class describes the basic Net Interface, used by both master and slave.
  32. */
  33. class SERVER_EXPORT JackNetInterface
  34. {
  35. friend class JackNetExt;
  36. protected:
  37. bool fSetTimeOut;
  38. int fPacketTimeOut;
  39. void Initialize();
  40. session_params_t fParams;
  41. JackNetSocket fSocket;
  42. char fMulticastIP[32];
  43. // headers
  44. packet_header_t fTxHeader;
  45. packet_header_t fRxHeader;
  46. // transport
  47. net_transport_data_t fSendTransportData;
  48. net_transport_data_t fReturnTransportData;
  49. // network buffers
  50. char* fTxBuffer;
  51. char* fRxBuffer;
  52. char* fTxData;
  53. char* fRxData;
  54. // JACK buffers
  55. NetMidiBuffer* fNetMidiCaptureBuffer;
  56. NetMidiBuffer* fNetMidiPlaybackBuffer;
  57. NetAudioBuffer* fNetAudioCaptureBuffer;
  58. NetAudioBuffer* fNetAudioPlaybackBuffer;
  59. // utility methods
  60. int SetNetBufferSize();
  61. void FreeNetworkBuffers();
  62. // virtual methods : depends on the sub class master/slave
  63. virtual bool SetParams();
  64. virtual bool Init() = 0;
  65. // transport
  66. virtual void EncodeTransportData() = 0;
  67. virtual void DecodeTransportData() = 0;
  68. // sync packet
  69. virtual void EncodeSyncPacket(int frames = -1) = 0;
  70. virtual void DecodeSyncPacket(int& frames) = 0;
  71. virtual int SyncRecv() = 0;
  72. virtual int SyncSend() = 0;
  73. virtual int DataRecv() = 0;
  74. virtual int DataSend() = 0;
  75. virtual int Send(size_t size, int flags) = 0;
  76. virtual int Recv(size_t size, int flags) = 0;
  77. virtual void FatalRecvError() = 0;
  78. virtual void FatalSendError() = 0;
  79. int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
  80. int AudioSend(NetAudioBuffer* buffer, int audio_channels);
  81. int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
  82. int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
  83. int FinishRecv(NetAudioBuffer* buffer);
  84. void SetRcvTimeOut();
  85. void SetPacketTimeOut(int time_out)
  86. {
  87. // New time out
  88. fPacketTimeOut = time_out;
  89. fSetTimeOut = false;
  90. }
  91. NetAudioBuffer* AudioBufferFactory(int nports, char* buffer);
  92. public:
  93. JackNetInterface();
  94. JackNetInterface(const char* multicast_ip, int port);
  95. JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
  96. virtual ~JackNetInterface();
  97. };
  98. /**
  99. \Brief This class describes the Net Interface for masters (NetMaster)
  100. */
  101. class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
  102. {
  103. protected:
  104. bool fRunning;
  105. int fCurrentCycleOffset;
  106. int fMaxCycleOffset;
  107. bool fSynched;
  108. bool Init();
  109. bool SetParams();
  110. void Exit();
  111. int SyncRecv();
  112. int SyncSend();
  113. int DataRecv();
  114. int DataSend();
  115. // sync packet
  116. void EncodeSyncPacket(int frames = -1);
  117. void DecodeSyncPacket(int& frames);
  118. int Send(size_t size, int flags);
  119. int Recv(size_t size, int flags);
  120. void FatalRecvError();
  121. void FatalSendError();
  122. public:
  123. JackNetMasterInterface()
  124. : JackNetInterface(),
  125. fRunning(false),
  126. fCurrentCycleOffset(0),
  127. fMaxCycleOffset(0),
  128. fSynched(false)
  129. {}
  130. JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
  131. : JackNetInterface(params, socket, multicast_ip),
  132. fRunning(false),
  133. fCurrentCycleOffset(0),
  134. fMaxCycleOffset(0),
  135. fSynched(false)
  136. {}
  137. virtual~JackNetMasterInterface()
  138. {}
  139. };
  140. /**
  141. \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
  142. */
  143. class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
  144. {
  145. protected:
  146. static uint fSlaveCounter;
  147. bool Init();
  148. bool InitConnection(int time_out_sec);
  149. bool InitRendering();
  150. net_status_t SendAvailableToMaster(int count = INT_MAX);
  151. net_status_t SendStartToMaster();
  152. bool SetParams();
  153. int SyncRecv();
  154. int SyncSend();
  155. int DataRecv();
  156. int DataSend();
  157. // sync packet
  158. void EncodeSyncPacket(int frames = -1);
  159. void DecodeSyncPacket(int& frames);
  160. int Recv(size_t size, int flags);
  161. int Send(size_t size, int flags);
  162. void FatalRecvError();
  163. void FatalSendError();
  164. void InitAPI();
  165. public:
  166. JackNetSlaveInterface() : JackNetInterface()
  167. {
  168. InitAPI();
  169. }
  170. JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
  171. {
  172. InitAPI();
  173. }
  174. virtual ~JackNetSlaveInterface()
  175. {
  176. // close Socket API with the last slave
  177. if (--fSlaveCounter == 0) {
  178. SocketAPIEnd();
  179. }
  180. }
  181. };
  182. }
  183. #endif