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.

309 lines
12KB

  1. /*
  2. Copyright (C) 2008 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. namespace Jack
  35. {
  36. typedef struct _session_params session_params_t;
  37. typedef struct _packet_header packet_header_t;
  38. typedef struct _net_transport_data net_transport_data_t;
  39. typedef struct sockaddr socket_address_t;
  40. typedef struct in_addr address_t;
  41. typedef jack_default_audio_sample_t sample_t;
  42. //session params ******************************************************************************
  43. /**
  44. \brief This structure containes master/slave connection parameters, it's used to setup the whole system
  45. We have :
  46. - some info like version, type and packet id
  47. - names
  48. - network parameters (hostnames and mtu)
  49. - nunber of audio and midi channels
  50. - sample rate and buffersize
  51. - number of audio frames in one network packet (depends on the channel number)
  52. - is the NetDriver in Sync or ASync mode ?
  53. - is the NetDriver linked with the master's transport
  54. Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
  55. are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
  56. */
  57. #define MASTER_PROTOCOL 1
  58. #define SLAVE_PROTOCOL 1
  59. struct _session_params
  60. {
  61. char fPacketType[7]; //packet type ('param')
  62. char fProtocolVersion; //version
  63. uint32_t fPacketID; //indicates the packet type
  64. char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
  65. char fMasterNetName[256]; //master hostname (network)
  66. char fSlaveNetName[256]; //slave hostname (network)
  67. uint32_t fMtu; //connection mtu
  68. uint32_t fID; //slave's ID
  69. uint32_t fTransportSync; //is the transport synced ?
  70. uint32_t fSendAudioChannels; //number of master->slave channels
  71. uint32_t fReturnAudioChannels; //number of slave->master channels
  72. uint32_t fSendMidiChannels; //number of master->slave midi channels
  73. uint32_t fReturnMidiChannels; //number of slave->master midi channels
  74. uint32_t fSampleRate; //session sample rate
  75. uint32_t fPeriodSize; //period size
  76. uint32_t fFramesPerPacket; //complete frames per packet
  77. uint32_t fBitdepth; //samples bitdepth (unused)
  78. uint32_t fSlaveSyncMode; //is the slave in sync mode ?
  79. char fNetworkMode; //fast, normal or slow mode
  80. };
  81. //net status **********************************************************************************
  82. /**
  83. \Brief This enum groups network error by type
  84. */
  85. enum _net_status
  86. {
  87. NET_SOCKET_ERROR = 0,
  88. NET_CONNECT_ERROR,
  89. NET_ERROR,
  90. NET_SEND_ERROR,
  91. NET_RECV_ERROR,
  92. NET_CONNECTED,
  93. NET_ROLLING
  94. };
  95. typedef enum _net_status net_status_t;
  96. //sync packet type ****************************************************************************
  97. /**
  98. \Brief This enum indicates the type of a sync packet (used in the initialization phase)
  99. */
  100. enum _sync_packet_type
  101. {
  102. INVALID = 0, //...
  103. SLAVE_AVAILABLE, //a slave is available
  104. SLAVE_SETUP, //slave configuration
  105. START_MASTER, //slave is ready, start master
  106. START_SLAVE, //master is ready, activate slave
  107. KILL_MASTER //master must stop
  108. };
  109. typedef enum _sync_packet_type sync_packet_type_t;
  110. //packet header *******************************************************************************
  111. /**
  112. \Brief This structure is a complete header
  113. A header indicates :
  114. - it is a header
  115. - the type of data the packet contains (sync, midi or audio)
  116. - the path of the packet (send -master->slave- or return -slave->master-)
  117. - the unique ID of the slave
  118. - the sample's bitdepth (unused for now)
  119. - the size of the midi data contains in the packet (indicates how much midi data will be sent)
  120. - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
  121. - the ID of the current cycle (used to check missing packets)
  122. - the ID of the packet subcycle (for audio data)
  123. - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
  124. - a flag indicating if, in async mode, the previous graph was not finished or not
  125. - padding to fill 64 bytes
  126. */
  127. struct _packet_header
  128. {
  129. char fPacketType[7]; //packet type ( 'headr' )
  130. char fDataType; //a for audio, m for midi and s for sync
  131. char fDataStream; //s for send, r for return
  132. uint32_t fID; //unique ID of the slave
  133. uint32_t fBitdepth; //bitdepth of the data samples
  134. uint32_t fMidiDataSize; //size of midi data in bytes
  135. uint32_t fNMidiPckt; //number of midi packets of the cycle
  136. uint32_t fPacketSize; //packet size in bytes
  137. uint32_t fCycle; //process cycle counter
  138. uint32_t fSubCycle; //midi/audio subcycle counter
  139. uint32_t fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
  140. char fASyncWrongCycle; //is the current async cycle wrong (slave's side; 'y' or 'n')
  141. char fFree[26]; //unused
  142. };
  143. //net timebase master
  144. /**
  145. \Brief This enum describes timebase master's type
  146. */
  147. enum _net_timebase_master
  148. {
  149. NO_CHANGE = 0,
  150. RELEASE_TIMEBASEMASTER = 1,
  151. TIMEBASEMASTER = 2,
  152. CONDITIONAL_TIMEBASEMASTER = 3
  153. };
  154. typedef enum _net_timebase_master net_timebase_master_t;
  155. //transport data ******************************************************************************
  156. /**
  157. \Brief This structure contains transport data to be sent over the network
  158. */
  159. struct _net_transport_data
  160. {
  161. uint32_t fNewState; //is it a state change
  162. uint32_t fTimebaseMaster; //is there a new timebase master
  163. int32_t fState; //current cycle state
  164. jack_position_t fPosition; //current cycle position
  165. };
  166. //midi data ***********************************************************************************
  167. /**
  168. \Brief Midi buffer and operations class
  169. This class is a toolset to manipulate Midi buffers.
  170. A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
  171. An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
  172. But for a big majority of the process cycles, this buffer is filled less than 1%,
  173. Sending over a network 99% of useless data seems completely unappropriate.
  174. The idea is to count effective midi data, and then send the smallest packet we can.
  175. To do it, we use an intermediate buffer.
  176. We have two methods to convert data from jack ports to intermediate buffer,
  177. And two others to convert this intermediate buffer to a network buffer (header + payload data)
  178. */
  179. class SERVER_EXPORT NetMidiBuffer
  180. {
  181. private:
  182. int fNPorts;
  183. size_t fMaxBufsize;
  184. int fMaxPcktSize;
  185. char* fBuffer;
  186. char* fNetBuffer;
  187. JackMidiBuffer** fPortBuffer;
  188. public:
  189. NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
  190. ~NetMidiBuffer();
  191. void Reset();
  192. size_t GetSize();
  193. //utility
  194. void DisplayEvents();
  195. //jack<->buffer
  196. int RenderFromJackPorts();
  197. int RenderToJackPorts();
  198. //network<->buffer
  199. int RenderFromNetwork ( int subcycle, size_t copy_size );
  200. int RenderToNetwork ( int subcycle, size_t total_size );
  201. void SetBuffer ( int index, JackMidiBuffer* buffer );
  202. JackMidiBuffer* GetBuffer ( int index );
  203. };
  204. // audio data *********************************************************************************
  205. /**
  206. \Brief Audio buffer and operations class
  207. This class is a toolset to manipulate audio buffers.
  208. The manipulation of audio buffers is similar to midi buffer, except those buffers have fixed size.
  209. The interleaving/uninterleaving operations are simplier here because audio buffers have fixed size,
  210. So there is no need of an intermediate buffer as in NetMidiBuffer.
  211. */
  212. class SERVER_EXPORT NetAudioBuffer
  213. {
  214. private:
  215. int fNPorts;
  216. jack_nframes_t fPeriodSize;
  217. jack_nframes_t fSubPeriodSize;
  218. size_t fSubPeriodBytesSize;
  219. char* fNetBuffer;
  220. sample_t** fPortBuffer;
  221. public:
  222. NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
  223. ~NetAudioBuffer();
  224. size_t GetSize();
  225. //jack<->buffer
  226. void RenderFromJackPorts ( int subcycle );
  227. void RenderToJackPorts ( int subcycle );
  228. void SetBuffer ( int index, sample_t* buffer );
  229. sample_t* GetBuffer ( int index );
  230. };
  231. //utility *************************************************************************************
  232. //socket API management
  233. SERVER_EXPORT int SocketAPIInit();
  234. SERVER_EXPORT int SocketAPIEnd();
  235. //n<-->h functions
  236. SERVER_EXPORT void SessionParamsHToN ( session_params_t* src_params, session_params_t* dst_params );
  237. SERVER_EXPORT void SessionParamsNToH ( session_params_t* src_params, session_params_t* dst_params );
  238. SERVER_EXPORT void PacketHeaderHToN ( packet_header_t* src_header, packet_header_t* dst_header );
  239. SERVER_EXPORT void PacketHeaderNToH ( packet_header_t* src_header, packet_header_t* dst_header );
  240. SERVER_EXPORT void MidiBufferHToN ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer );
  241. SERVER_EXPORT void MidiBufferNToH ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer );
  242. SERVER_EXPORT void TransportDataHToN ( net_transport_data_t* src_params, net_transport_data_t* dst_params );
  243. SERVER_EXPORT void TransportDataNToH ( net_transport_data_t* src_params, net_transport_data_t* dst_params );
  244. //display session parameters
  245. SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params );
  246. //display packet header
  247. SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header );
  248. //get the packet type from a sesion parameters
  249. SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
  250. //set the packet type in a session parameters
  251. SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
  252. //transport utility
  253. SERVER_EXPORT const char* GetTransportState ( int transport_state );
  254. SERVER_EXPORT void NetTransportDataDisplay ( net_transport_data_t* data );
  255. }