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.

218 lines
6.2KB

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