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.

274 lines
10KB

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