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.

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