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.

212 lines
6.1KB

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