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.

464 lines
16KB

  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. #include "JackMidiPort.h"
  16. #include "JackTools.h"
  17. #include "JackPlatformPlug.h"
  18. #include "types.h"
  19. #include "transport.h"
  20. #ifndef WIN32
  21. #include <netinet/in.h>
  22. #endif
  23. #include <cmath>
  24. using namespace std;
  25. #ifndef htonll
  26. #ifdef __BIG_ENDIAN__
  27. #define htonll(x) (x)
  28. #define ntohll(x) (x)
  29. #else
  30. #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
  31. #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
  32. #endif
  33. #endif
  34. #define MASTER_PROTOCOL 5
  35. #define SLAVE_PROTOCOL 5
  36. #define NET_PACKET_ERROR -2
  37. #define OPTIMIZED_PROTOCOL
  38. #define HEADER_SIZE (sizeof(packet_header_t))
  39. #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - sizeof(packet_header_t))
  40. namespace Jack
  41. {
  42. typedef struct _session_params session_params_t;
  43. typedef struct _packet_header packet_header_t;
  44. typedef struct _net_transport_data net_transport_data_t;
  45. typedef struct sockaddr socket_address_t;
  46. typedef struct in_addr address_t;
  47. typedef jack_default_audio_sample_t sample_t;
  48. enum JackNetEncoder {
  49. JackFloatEncoder = 0,
  50. JackIntEncoder = 1,
  51. JackCeltEncoder = 2,
  52. };
  53. //session params ******************************************************************************
  54. /**
  55. \brief This structure containes master/slave connection parameters, it's used to setup the whole system
  56. We have :
  57. - some info like version, type and packet id
  58. - names
  59. - network parameters (hostnames and mtu)
  60. - nunber of audio and midi channels
  61. - sample rate and buffersize
  62. - number of audio frames in one network packet (depends on the channel number)
  63. - is the NetDriver in Sync or ASync mode ?
  64. - is the NetDriver linked with the master's transport
  65. Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
  66. are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
  67. */
  68. struct _session_params
  69. {
  70. char fPacketType[7]; //packet type ('param')
  71. char fProtocolVersion; //version
  72. uint32_t fPacketID; //indicates the packet type
  73. char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
  74. char fMasterNetName[256]; //master hostname (network)
  75. char fSlaveNetName[256]; //slave hostname (network)
  76. uint32_t fMtu; //connection mtu
  77. uint32_t fID; //slave's ID
  78. uint32_t fTransportSync; //is the transport synced ?
  79. int32_t fSendAudioChannels; //number of master->slave channels
  80. int32_t fReturnAudioChannels; //number of slave->master channels
  81. int32_t fSendMidiChannels; //number of master->slave midi channels
  82. int32_t fReturnMidiChannels; //number of slave->master midi channels
  83. uint32_t fSampleRate; //session sample rate
  84. uint32_t fPeriodSize; //period size
  85. uint32_t fSampleEncoder; //samples encoder
  86. uint32_t fKBps; //KB per second for CELT encoder
  87. uint32_t fSlaveSyncMode; //is the slave in sync mode ?
  88. uint32_t fNetworkLatency; //network latency
  89. };
  90. //net status **********************************************************************************
  91. /**
  92. \Brief This enum groups network error by type
  93. */
  94. enum _net_status
  95. {
  96. NET_SOCKET_ERROR = 0,
  97. NET_CONNECT_ERROR,
  98. NET_ERROR,
  99. NET_SEND_ERROR,
  100. NET_RECV_ERROR,
  101. NET_CONNECTED,
  102. NET_ROLLING
  103. };
  104. typedef enum _net_status net_status_t;
  105. //sync packet type ****************************************************************************
  106. /**
  107. \Brief This enum indicates the type of a sync packet (used in the initialization phase)
  108. */
  109. enum _sync_packet_type
  110. {
  111. INVALID = 0, //...
  112. SLAVE_AVAILABLE, //a slave is available
  113. SLAVE_SETUP, //slave configuration
  114. START_MASTER, //slave is ready, start master
  115. START_SLAVE, //master is ready, activate slave
  116. KILL_MASTER //master must stop
  117. };
  118. typedef enum _sync_packet_type sync_packet_type_t;
  119. //packet header *******************************************************************************
  120. /**
  121. \Brief This structure is a complete header
  122. A header indicates :
  123. - it is a header
  124. - the type of data the packet contains (sync, midi or audio)
  125. - the path of the packet (send -master->slave- or return -slave->master-)
  126. - the unique ID of the slave
  127. - the sample's bitdepth (unused for now)
  128. - the size of the midi data contains in the packet (indicates how much midi data will be sent)
  129. - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
  130. - the ID of the current cycle (used to check missing packets)
  131. - the ID of the packet subcycle (for audio data)
  132. - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
  133. - a flag indicating if, in async mode, the previous graph was not finished or not
  134. - padding to fill 64 bytes
  135. */
  136. struct _packet_header
  137. {
  138. char fPacketType[7]; //packet type ('headr')
  139. char fDataType; //a for audio, m for midi and s for sync
  140. char fDataStream; //s for send, r for return
  141. uint32_t fID; //unique ID of the slave
  142. uint32_t fNumPacket; //number of data packets of the cycle
  143. uint32_t fPacketSize; //packet size in bytes
  144. uint32_t fActivePorts; //number of active ports
  145. uint32_t fCycle; //process cycle counter
  146. uint32_t fSubCycle; //midi/audio subcycle counter
  147. uint32_t fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
  148. };
  149. //net timebase master
  150. /**
  151. \Brief This enum describes timebase master's type
  152. */
  153. enum _net_timebase_master
  154. {
  155. NO_CHANGE = 0,
  156. RELEASE_TIMEBASEMASTER = 1,
  157. TIMEBASEMASTER = 2,
  158. CONDITIONAL_TIMEBASEMASTER = 3
  159. };
  160. typedef enum _net_timebase_master net_timebase_master_t;
  161. //transport data ******************************************************************************
  162. /**
  163. \Brief This structure contains transport data to be sent over the network
  164. */
  165. struct _net_transport_data
  166. {
  167. uint32_t fNewState; //is it a state change
  168. uint32_t fTimebaseMaster; //is there a new timebase master
  169. int32_t fState; //current cycle state
  170. jack_position_t fPosition; //current cycle position
  171. };
  172. //midi data ***********************************************************************************
  173. /**
  174. \Brief Midi buffer and operations class
  175. This class is a toolset to manipulate Midi buffers.
  176. A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
  177. An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
  178. But for a big majority of the process cycles, this buffer is filled less than 1%,
  179. Sending over a network 99% of useless data seems completely unappropriate.
  180. The idea is to count effective midi data, and then send the smallest packet we can.
  181. To do it, we use an intermediate buffer.
  182. We have two methods to convert data from jack ports to intermediate buffer,
  183. And two others to convert this intermediate buffer to a network buffer (header + payload data)
  184. */
  185. class SERVER_EXPORT NetMidiBuffer
  186. {
  187. private:
  188. int fNPorts;
  189. size_t fMaxBufsize;
  190. int fMaxPcktSize;
  191. char* fBuffer;
  192. char* fNetBuffer;
  193. JackMidiBuffer** fPortBuffer;
  194. size_t fCycleBytesSize; // needed size in bytes ofr an entire cycle
  195. public:
  196. NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  197. ~NetMidiBuffer();
  198. void Reset();
  199. // needed size in bytes for an entire cycle
  200. size_t GetCycleSize();
  201. int GetNumPackets(int data_sizen, int max_size);
  202. void SetBuffer(int index, JackMidiBuffer* buffer);
  203. JackMidiBuffer* GetBuffer(int index);
  204. //utility
  205. void DisplayEvents();
  206. //jack<->buffer
  207. int RenderFromJackPorts();
  208. void RenderToJackPorts();
  209. //network<->buffer
  210. void RenderFromNetwork(int sub_cycle, size_t copy_size);
  211. int RenderToNetwork(int sub_cycle, size_t total_size);
  212. };
  213. // audio data *********************************************************************************
  214. class SERVER_EXPORT NetAudioBuffer
  215. {
  216. protected:
  217. int fNPorts;
  218. int fLastSubCycle;
  219. char* fNetBuffer;
  220. sample_t** fPortBuffer;
  221. bool* fConnectedPorts;
  222. jack_nframes_t fPeriodSize;
  223. jack_nframes_t fSubPeriodSize;
  224. size_t fSubPeriodBytesSize;
  225. float fCycleDuration; // in sec
  226. size_t fCycleBytesSize; // needed size in bytes for an entire cycle
  227. int CheckPacket(int cycle, int sub_cycle);
  228. void NextCycle();
  229. void Cleanup();
  230. public:
  231. NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  232. virtual ~NetAudioBuffer();
  233. bool GetConnected(int port_index) { return fConnectedPorts[port_index]; }
  234. void SetConnected(int port_index, bool state) { fConnectedPorts[port_index] = state; }
  235. // needed syze in bytes ofr an entire cycle
  236. virtual size_t GetCycleSize() = 0;
  237. // cycle duration in sec
  238. virtual float GetCycleDuration() = 0;
  239. virtual int GetNumPackets(int active_ports) = 0;
  240. virtual void SetBuffer(int index, sample_t* buffer);
  241. virtual sample_t* GetBuffer(int index);
  242. //jack<->buffer
  243. virtual int RenderFromJackPorts();
  244. virtual void RenderToJackPorts();
  245. //network<->buffer
  246. virtual int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num) = 0;
  247. virtual int RenderToNetwork(int sub_cycle, uint32_t port_num) = 0;
  248. virtual void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle, size_t copy_size) {}
  249. virtual void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle, size_t copy_size) {}
  250. virtual int ActivePortsToNetwork(char* net_buffer);
  251. virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
  252. };
  253. class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
  254. {
  255. private:
  256. int fPacketSize;
  257. void UpdateParams(int active_ports);
  258. public:
  259. NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  260. virtual ~NetFloatAudioBuffer();
  261. // needed size in bytes for an entire cycle
  262. size_t GetCycleSize();
  263. // cycle duration in sec
  264. float GetCycleDuration();
  265. int GetNumPackets(int active_ports);
  266. //jack<->buffer
  267. int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
  268. int RenderToNetwork(int sub_cycle, uint32_t port_num);
  269. void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle);
  270. void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle);
  271. };
  272. #if HAVE_CELT
  273. #include <celt/celt.h>
  274. class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
  275. {
  276. private:
  277. CELTMode** fCeltMode;
  278. CELTEncoder** fCeltEncoder;
  279. CELTDecoder** fCeltDecoder;
  280. int fCompressedSizeByte;
  281. int fNumPackets;
  282. size_t fLastSubPeriodBytesSize;
  283. unsigned char** fCompressedBuffer;
  284. void FreeCelt();
  285. public:
  286. NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
  287. virtual ~NetCeltAudioBuffer();
  288. // needed size in bytes for an entire cycle
  289. size_t GetCycleSize();
  290. // cycle duration in sec
  291. float GetCycleDuration();
  292. int GetNumPackets(int active_ports);
  293. //jack<->buffer
  294. int RenderFromJackPorts();
  295. void RenderToJackPorts();
  296. //network<->buffer
  297. int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
  298. int RenderToNetwork(int sub_cycle, uint32_t port_num);
  299. };
  300. #endif
  301. class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
  302. {
  303. private:
  304. int fCompressedSizeByte;
  305. int fNumPackets;
  306. size_t fLastSubPeriodBytesSize;
  307. short** fIntBuffer;
  308. public:
  309. NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
  310. virtual ~NetIntAudioBuffer();
  311. // needed size in bytes for an entire cycle
  312. size_t GetCycleSize();
  313. // cycle duration in sec
  314. float GetCycleDuration();
  315. int GetNumPackets(int active_ports);
  316. //jack<->buffer
  317. int RenderFromJackPorts();
  318. void RenderToJackPorts();
  319. //network<->buffer
  320. int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
  321. int RenderToNetwork(int sub_cycle, uint32_t port_num);
  322. };
  323. //utility *************************************************************************************
  324. //socket API management
  325. SERVER_EXPORT int SocketAPIInit();
  326. SERVER_EXPORT int SocketAPIEnd();
  327. //n<-->h functions
  328. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
  329. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
  330. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
  331. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
  332. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
  333. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
  334. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
  335. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
  336. //display session parameters
  337. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
  338. //display packet header
  339. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
  340. //get the packet type from a sesion parameters
  341. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
  342. //set the packet type in a session parameters
  343. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
  344. //transport utility
  345. SERVER_EXPORT const char* GetTransportState(int transport_state);
  346. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);
  347. }