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.

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