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.

237 lines
6.4KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2008-2011 Romain Moret at Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifndef __JackNetInterface__
  17. #define __JackNetInterface__
  18. #include "JackNetTool.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 MASTER_INIT_TIMEOUT 1000000 // in usec
  26. #define SLAVE_INIT_TIMEOUT 1000000 // in usec
  27. #define NETWORK_MAX_LATENCY 10
  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 FatalError() = 0;
  73. int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
  74. int AudioSend(NetAudioBuffer* buffer, int audio_channels);
  75. int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
  76. int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
  77. int FinishRecv(NetAudioBuffer* buffer);
  78. public:
  79. JackNetInterface();
  80. JackNetInterface(const char* multicast_ip, int port);
  81. JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
  82. virtual ~JackNetInterface();
  83. };
  84. /**
  85. \Brief This class describes the Net Interface for masters (NetMaster)
  86. */
  87. class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
  88. {
  89. protected:
  90. bool fRunning;
  91. int fCycleOffset;
  92. int fMaxCycleOffset;
  93. int fLastfCycleOffset;
  94. bool Init();
  95. int SetRxTimeout();
  96. bool SetParams();
  97. void Exit();
  98. int SyncRecv();
  99. int SyncSend();
  100. int DataRecv();
  101. int DataSend();
  102. // sync packet
  103. void EncodeSyncPacket();
  104. void DecodeSyncPacket();
  105. int Send ( size_t size, int flags );
  106. int Recv ( size_t size, int flags );
  107. bool IsSynched();
  108. void FatalError();
  109. public:
  110. JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCycleOffset(0), fMaxCycleOffset(0), fLastfCycleOffset(0)
  111. {}
  112. JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
  113. : JackNetInterface(params, socket, multicast_ip)
  114. {}
  115. virtual~JackNetMasterInterface()
  116. {}
  117. };
  118. /**
  119. \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
  120. */
  121. class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
  122. {
  123. protected:
  124. static uint fSlaveCounter;
  125. bool Init();
  126. bool InitConnection(int time_out);
  127. bool InitRendering();
  128. net_status_t SendAvailableToMaster(long count = LONG_MAX); // long here (and not int...)
  129. net_status_t SendStartToMaster();
  130. bool SetParams();
  131. int SyncRecv();
  132. int SyncSend();
  133. int DataRecv();
  134. int DataSend();
  135. // sync packet
  136. void EncodeSyncPacket();
  137. void DecodeSyncPacket();
  138. int Recv(size_t size, int flags);
  139. int Send(size_t size, int flags);
  140. void FatalError();
  141. void InitAPI()
  142. {
  143. // open Socket API with the first slave
  144. if (fSlaveCounter++ == 0) {
  145. if (SocketAPIInit() < 0) {
  146. jack_error("Can't init Socket API, exiting...");
  147. throw std::bad_alloc();
  148. }
  149. }
  150. }
  151. public:
  152. JackNetSlaveInterface() : JackNetInterface()
  153. {
  154. InitAPI();
  155. }
  156. JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
  157. {
  158. InitAPI();
  159. }
  160. virtual ~JackNetSlaveInterface()
  161. {
  162. // close Socket API with the last slave
  163. if (--fSlaveCounter == 0) {
  164. SocketAPIEnd();
  165. }
  166. }
  167. };
  168. }
  169. #endif