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.

258 lines
7.1KB

  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_MULTICAST_IF "any"
  23. #define DEFAULT_PORT 19000
  24. #define DEFAULT_MTU 1500
  25. #define MAX_MTU 9000
  26. #define SLAVE_SETUP_RETRY 5
  27. #define MANAGER_INIT_TIMEOUT 1000000 * 2 // in usec
  28. #define MASTER_INIT_TIMEOUT 1000000 * 10 // in usec
  29. #define SLAVE_INIT_TIMEOUT 1000000 * 1 // in usec
  30. #define PACKET_TIMEOUT 1000000 // in usec
  31. #define NETWORK_DEFAULT_LATENCY 2
  32. #define NETWORK_MAX_LATENCY 30 // maximum possible latency in network master/slave loop
  33. /**
  34. \Brief This class describes the basic Net Interface, used by both master and slave.
  35. */
  36. class SERVER_EXPORT JackNetInterface
  37. {
  38. friend class JackNetExt;
  39. protected:
  40. bool fSetTimeOut;
  41. int fPacketTimeOut;
  42. void Initialize();
  43. session_params_t fParams;
  44. JackNetSocket fSocket;
  45. char fMulticastIP[32];
  46. char fMulticastIF[32];
  47. // headers
  48. packet_header_t fTxHeader;
  49. packet_header_t fRxHeader;
  50. // transport
  51. net_transport_data_t fSendTransportData;
  52. net_transport_data_t fReturnTransportData;
  53. // network buffers
  54. char* fTxBuffer;
  55. char* fRxBuffer;
  56. char* fTxData;
  57. char* fRxData;
  58. // JACK buffers
  59. NetMidiBuffer* fNetMidiCaptureBuffer;
  60. NetMidiBuffer* fNetMidiPlaybackBuffer;
  61. NetAudioBuffer* fNetAudioCaptureBuffer;
  62. NetAudioBuffer* fNetAudioPlaybackBuffer;
  63. // utility methods
  64. int SetNetBufferSize();
  65. void FreeNetworkBuffers();
  66. // virtual methods : depends on the sub class master/slave
  67. virtual bool SetParams();
  68. virtual bool Init() = 0;
  69. // transport
  70. virtual void EncodeTransportData() = 0;
  71. virtual void DecodeTransportData() = 0;
  72. // sync packet
  73. virtual void EncodeSyncPacket(int frames = -1) = 0;
  74. virtual void DecodeSyncPacket(int& frames) = 0;
  75. virtual int SyncRecv() = 0;
  76. virtual int SyncSend() = 0;
  77. virtual int DataRecv() = 0;
  78. virtual int DataSend() = 0;
  79. virtual int Send(size_t size, int flags) = 0;
  80. virtual int Recv(size_t size, int flags) = 0;
  81. virtual void FatalRecvError() = 0;
  82. virtual void FatalSendError() = 0;
  83. int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
  84. int AudioSend(NetAudioBuffer* buffer, int audio_channels);
  85. int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
  86. int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
  87. int FinishRecv(NetAudioBuffer* buffer);
  88. void SetRcvTimeOut();
  89. void SetPacketTimeOut(int time_out)
  90. {
  91. // New time out
  92. fPacketTimeOut = time_out;
  93. fSetTimeOut = false;
  94. }
  95. NetAudioBuffer* AudioBufferFactory(int nports, char* buffer);
  96. public:
  97. JackNetInterface();
  98. JackNetInterface(const char* multicast_ip, int port);
  99. JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
  100. virtual ~JackNetInterface();
  101. };
  102. /**
  103. \Brief This class describes the Net Interface for masters (NetMaster)
  104. */
  105. class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
  106. {
  107. protected:
  108. bool fRunning;
  109. int fCurrentCycleOffset;
  110. int fMaxCycleOffset;
  111. bool fSynched;
  112. bool Init();
  113. bool SetParams();
  114. void Exit();
  115. int SyncRecv();
  116. int SyncSend();
  117. int DataRecv();
  118. int DataSend();
  119. // sync packet
  120. void EncodeSyncPacket(int frames = -1);
  121. void DecodeSyncPacket(int& frames);
  122. int Send(size_t size, int flags);
  123. int Recv(size_t size, int flags);
  124. void FatalRecvError();
  125. void FatalSendError();
  126. public:
  127. JackNetMasterInterface()
  128. : JackNetInterface(),
  129. fRunning(false),
  130. fCurrentCycleOffset(0),
  131. fMaxCycleOffset(0),
  132. fSynched(false)
  133. {}
  134. JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
  135. : JackNetInterface(params, socket, multicast_ip),
  136. fRunning(false),
  137. fCurrentCycleOffset(0),
  138. fMaxCycleOffset(0),
  139. fSynched(false)
  140. {}
  141. virtual~JackNetMasterInterface()
  142. {}
  143. };
  144. /**
  145. \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
  146. */
  147. class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
  148. {
  149. protected:
  150. static uint fSlaveCounter;
  151. bool Init();
  152. bool InitConnection(int time_out_sec);
  153. bool InitRendering();
  154. net_status_t SendAvailableToMaster(int count = INT_MAX);
  155. net_status_t SendStartToMaster();
  156. bool SetParams();
  157. int SyncRecv();
  158. int SyncSend();
  159. int DataRecv();
  160. int DataSend();
  161. // sync packet
  162. void EncodeSyncPacket(int frames = -1);
  163. void DecodeSyncPacket(int& frames);
  164. int Recv(size_t size, int flags);
  165. int Send(size_t size, int flags);
  166. void FatalRecvError();
  167. void FatalSendError();
  168. void InitAPI();
  169. public:
  170. JackNetSlaveInterface() : JackNetInterface()
  171. {
  172. InitAPI();
  173. }
  174. JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
  175. {
  176. InitAPI();
  177. }
  178. virtual ~JackNetSlaveInterface()
  179. {
  180. // close Socket API with the last slave
  181. if (--fSlaveCounter == 0) {
  182. SocketAPIEnd();
  183. }
  184. }
  185. };
  186. }
  187. #endif