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.

243 lines
6.6KB

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