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.

252 lines
6.9KB

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