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.

236 lines
6.3KB

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