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.

295 lines
11KB

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