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.

219 lines
6.3KB

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