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.

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