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.

511 lines
17KB

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