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.

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