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.

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