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.

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