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.

202 lines
7.7KB

  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. struct _session_params
  34. {
  35. char fPacketType[7]; //packet type ('param')
  36. char fProtocolVersion; //version
  37. uint32_t fPacketID; //indicates the packet type
  38. char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
  39. char fMasterNetName[256]; //master hostname (network)
  40. char fSlaveNetName[256]; //slave hostname (network)
  41. uint32_t fMtu; //connection mtu
  42. uint32_t fID; //slave's ID
  43. uint32_t fTransportSync; //is the transport synced ?
  44. uint32_t fSendAudioChannels; //number of master->slave channels
  45. uint32_t fReturnAudioChannels; //number of slave->master channels
  46. uint32_t fSendMidiChannels; //number of master->slave midi channels
  47. uint32_t fReturnMidiChannels; //number of slave->master midi channels
  48. uint32_t fSampleRate; //session sample rate
  49. uint32_t fPeriodSize; //period size
  50. uint32_t fFramesPerPacket; //complete frames per packet
  51. uint32_t fBitdepth; //samples bitdepth (unused)
  52. uint32_t fSlaveSyncMode; //is the slave in sync mode ?
  53. };
  54. //net status **********************************************************************************
  55. enum _net_status
  56. {
  57. NET_SOCKET_ERROR = 0,
  58. NET_CONNECT_ERROR,
  59. NET_ERROR,
  60. NET_SEND_ERROR,
  61. NET_RECV_ERROR,
  62. NET_CONNECTED,
  63. NET_ROLLING
  64. };
  65. typedef enum _net_status net_status_t;
  66. //sync packet type ****************************************************************************
  67. enum _sync_packet_type
  68. {
  69. INVALID = 0, //...
  70. SLAVE_AVAILABLE, //a slave is available
  71. SLAVE_SETUP, //slave configuration
  72. START_MASTER, //slave is ready, start master
  73. START_SLAVE, //master is ready, activate slave
  74. KILL_MASTER //master must stop
  75. };
  76. typedef enum _sync_packet_type sync_packet_type_t;
  77. //packet header *******************************************************************************
  78. struct _packet_header
  79. {
  80. char fPacketType[7]; //packet type ( 'headr' )
  81. char fDataType; //a for audio, m for midi
  82. char fDataStream; //s for send, r for return
  83. uint32_t fID; //to identify the slave
  84. uint32_t fBitdepth; //bitdepth of the data samples
  85. uint32_t fMidiDataSize; //size of midi data (if packet is 'midi typed') in bytes
  86. uint32_t fNMidiPckt; //number of midi packets of the cycle
  87. uint32_t fCycle; //process cycle counter
  88. uint32_t fSubCycle; //midi/audio subcycle counter
  89. char fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
  90. char fASyncWrongCycle; //is the current async cycle wrong (slave's side; 'y' or 'n')
  91. char fFree[29]; //unused
  92. };
  93. //transport data ******************************************************************************
  94. struct _net_transport_data
  95. {
  96. jack_position_t fCurPos;
  97. jack_transport_state_t fCurState;
  98. };
  99. //midi data ***********************************************************************************
  100. class EXPORT NetMidiBuffer
  101. {
  102. private:
  103. int fNPorts;
  104. size_t fMaxBufsize;
  105. int fMaxPcktSize;
  106. char* fBuffer;
  107. char* fNetBuffer;
  108. JackMidiBuffer** fPortBuffer;
  109. public:
  110. NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
  111. ~NetMidiBuffer();
  112. void Reset();
  113. size_t GetSize();
  114. //utility
  115. void DisplayEvents();
  116. //jack<->buffer
  117. int RenderFromJackPorts();
  118. int RenderToJackPorts();
  119. //network<->buffer
  120. int RenderFromNetwork ( int subcycle, size_t copy_size );
  121. int RenderToNetwork ( int subcycle, size_t total_size );
  122. void SetBuffer ( int index, JackMidiBuffer* buffer );
  123. JackMidiBuffer* GetBuffer ( int index );
  124. };
  125. // audio data *********************************************************************************
  126. class EXPORT NetAudioBuffer
  127. {
  128. private:
  129. int fNPorts;
  130. jack_nframes_t fPeriodSize;
  131. jack_nframes_t fSubPeriodSize;
  132. size_t fSubPeriodBytesSize;
  133. char* fNetBuffer;
  134. sample_t** fPortBuffer;
  135. public:
  136. NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
  137. ~NetAudioBuffer();
  138. size_t GetSize();
  139. //jack<->buffer
  140. void RenderFromJackPorts ( int subcycle );
  141. void RenderToJackPorts ( int subcycle );
  142. void SetBuffer ( int index, sample_t* buffer );
  143. sample_t* GetBuffer ( int index );
  144. };
  145. //utility *************************************************************************************
  146. //socket API management
  147. EXPORT int SocketAPIInit();
  148. EXPORT int SocketAPIEnd();
  149. //n<-->h functions
  150. EXPORT void SessionParamsHToN ( session_params_t* params );
  151. EXPORT void SessionParamsNToH ( session_params_t* params );
  152. EXPORT void PacketHeaderHToN ( packet_header_t* header );
  153. EXPORT void PacketHeaderNToH ( packet_header_t* header );
  154. //display session parameters
  155. EXPORT void SessionParamsDisplay ( session_params_t* params );
  156. //display packet header
  157. EXPORT void PacketHeaderDisplay ( packet_header_t* header );
  158. //get the packet type from a sesion parameters
  159. EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
  160. //set the packet type in a session parameters
  161. EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
  162. //step of network initialization
  163. EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params );
  164. //get the midi packet number for a given cycle
  165. EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size );
  166. //set the recv timeout on a socket
  167. EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params );
  168. //check if 'next' packet is really the next after 'previous'
  169. EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles );
  170. }