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.

260 lines
7.2KB

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