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.

245 lines
6.7KB

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