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.

241 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. public:
  80. JackNetInterface();
  81. JackNetInterface(const char* multicast_ip, int port);
  82. JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
  83. virtual ~JackNetInterface();
  84. };
  85. /**
  86. \Brief This class describes the Net Interface for masters (NetMaster)
  87. */
  88. class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
  89. {
  90. protected:
  91. bool fRunning;
  92. int fCurrentCycleOffset;
  93. int fMaxCycleOffset;
  94. int fLastfCycleOffset;
  95. bool Init();
  96. int SetRxTimeout();
  97. bool SetParams();
  98. void Exit();
  99. int SyncRecv();
  100. int SyncSend();
  101. int DataRecv();
  102. int DataSend();
  103. // sync packet
  104. void EncodeSyncPacket();
  105. void DecodeSyncPacket();
  106. int Send(size_t size, int flags);
  107. int Recv(size_t size, int flags);
  108. bool IsSynched();
  109. void FatalRecvError();
  110. void FatalSendError();
  111. public:
  112. JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0), fLastfCycleOffset(0)
  113. {}
  114. JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
  115. : JackNetInterface(params, socket, multicast_ip)
  116. {}
  117. virtual~JackNetMasterInterface()
  118. {}
  119. };
  120. /**
  121. \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
  122. */
  123. class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
  124. {
  125. protected:
  126. static uint fSlaveCounter;
  127. bool Init();
  128. bool InitConnection(int time_out_sec);
  129. bool InitRendering();
  130. net_status_t SendAvailableToMaster(long count = LONG_MAX); // long here (and not int...)
  131. net_status_t SendStartToMaster();
  132. bool SetParams();
  133. int SyncRecv();
  134. int SyncSend();
  135. int DataRecv();
  136. int DataSend();
  137. // sync packet
  138. void EncodeSyncPacket();
  139. void DecodeSyncPacket();
  140. int Recv(size_t size, int flags);
  141. int Send(size_t size, int flags);
  142. void FatalRecvError();
  143. void FatalSendError();
  144. void InitAPI()
  145. {
  146. // open Socket API with the first slave
  147. if (fSlaveCounter++ == 0) {
  148. if (SocketAPIInit() < 0) {
  149. jack_error("Can't init Socket API, exiting...");
  150. throw std::bad_alloc();
  151. }
  152. }
  153. }
  154. public:
  155. JackNetSlaveInterface() : JackNetInterface()
  156. {
  157. InitAPI();
  158. }
  159. JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
  160. {
  161. InitAPI();
  162. }
  163. virtual ~JackNetSlaveInterface()
  164. {
  165. // close Socket API with the last slave
  166. if (--fSlaveCounter == 0) {
  167. SocketAPIEnd();
  168. }
  169. }
  170. };
  171. }
  172. #endif