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.

204 lines
6.2KB

  1. /*
  2. Copyright (C) 2008 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 "types.h"
  16. #include "JackConstants.h"
  17. #include "JackMidiPort.h"
  18. #include "JackExports.h"
  19. #include <string>
  20. #include <algorithm>
  21. #include <cmath>
  22. #include <cstdlib>
  23. #include <cstdio>
  24. #include <iostream>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netdb.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <errno.h>
  32. namespace Jack
  33. {
  34. typedef struct _session_params session_params_t;
  35. typedef struct _packet_header packet_header_t;
  36. typedef struct sockaddr socket_address_t;
  37. typedef struct in_addr address_t;
  38. typedef jack_default_audio_sample_t sample_t;
  39. //session params ******************************************************************************
  40. struct _session_params
  41. {
  42. char fPacketType[7]; //packet type ('param')
  43. char fProtocolVersion; //version
  44. uint32_t fPacketID; //indicates the packet type
  45. char fMasterNetName[256]; //master hostname (network)
  46. char fSlaveNetName[256]; //slave hostname (network)
  47. uint32_t fMtu; //connection mtu
  48. uint32_t fID; //slave's ID
  49. uint32_t fSendAudioChannels; //number of master->slave channels
  50. uint32_t fReturnAudioChannels; //number of slave->master channels
  51. uint32_t fSendMidiChannels; //number of master->slave midi channels
  52. uint32_t fReturnMidiChannels; //number of slave->master midi channels
  53. uint32_t fSampleRate; //session sample rate
  54. uint32_t fPeriodSize; //period size
  55. uint32_t fFramesPerPacket; //complete frames per packet
  56. uint32_t fBitdepth; //samples bitdepth (unused)
  57. char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
  58. };
  59. //net status **********************************************************************************
  60. enum _net_status
  61. {
  62. SOCKET_ERROR,
  63. CONNECT_ERROR,
  64. NET_ERROR,
  65. SEND_ERROR,
  66. RECV_ERROR,
  67. CONNECTED,
  68. ROLLING
  69. };
  70. typedef enum _net_status net_status_t;
  71. //sync packet type ****************************************************************************
  72. enum _sync_packet_type
  73. {
  74. INVALID, //...
  75. SLAVE_AVAILABLE, //a slave is available
  76. SLAVE_SETUP, //slave configuration
  77. START_MASTER, //slave is ready, start master
  78. START_SLAVE, //master is ready, activate slave
  79. KILL_MASTER //master must stop
  80. };
  81. typedef enum _sync_packet_type sync_packet_type_t;
  82. //packet header *******************************************************************************
  83. struct _packet_header
  84. {
  85. char fPacketType[7]; //packet type ( 'headr' )
  86. char fDataType; //a for audio, m for midi
  87. char fDataStream; //s for send, r for return
  88. uint32_t fID; //to identify the slave
  89. uint32_t fBitdepth; //bitdepth of the data samples
  90. uint32_t fMidiDataSize; //size of midi data (if packet is 'midi typed') in bytes
  91. uint32_t fNMidiPckt; //number of midi packets of the cycle
  92. uint32_t fCycle; //process cycle counter
  93. uint32_t fSubCycle; //midi/audio subcycle counter
  94. char fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
  95. char fFree[13]; //unused
  96. };
  97. #ifndef __NetMidiBuffer__
  98. #define __NetMidiBuffer__
  99. //midi data ***********************************************************************************
  100. class EXPORT NetMidiBuffer
  101. {
  102. private:
  103. int fNPorts;
  104. size_t fMaxBufsize;
  105. int fMaxPcktSize;
  106. //data
  107. char* fBuffer;
  108. char* fNetBuffer;
  109. public:
  110. NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
  111. ~NetMidiBuffer();
  112. JackMidiBuffer** fPortBuffer;
  113. void Reset();
  114. size_t GetSize();
  115. //utility
  116. void DisplayEvents();
  117. //jack<->buffer
  118. int RenderFromJackPorts();
  119. int RenderToJackPorts();
  120. //network<->buffer
  121. int RenderFromNetwork ( int subcycle, size_t copy_size );
  122. int RenderToNetwork ( int subcycle, size_t total_size );
  123. };
  124. #endif
  125. // audio data *********************************************************************************
  126. #ifndef __NetAudioBuffer__
  127. #define __NetAudioBuffer__
  128. class EXPORT NetAudioBuffer
  129. {
  130. private:
  131. int fNPorts;
  132. jack_nframes_t fPeriodSize;
  133. jack_nframes_t fSubPeriodSize;
  134. size_t fSubPeriodBytesSize;
  135. char* fNetBuffer;
  136. public:
  137. NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
  138. ~NetAudioBuffer();
  139. sample_t** fPortBuffer;
  140. size_t GetSize();
  141. //jack<->buffer
  142. void RenderFromJackPorts ( int subcycle );
  143. void RenderToJackPorts ( int subcycle );
  144. };
  145. #endif
  146. //utility *************************************************************************************
  147. //n<-->h functions
  148. EXPORT void SessionParamsHToN ( session_params_t* params );
  149. EXPORT void SessionParamsNToH ( session_params_t* params );
  150. EXPORT void PacketHeaderHToN ( packet_header_t* header );
  151. EXPORT void PacketHeaderNToH ( packet_header_t* header );
  152. //display session parameters
  153. EXPORT void SessionParamsDisplay ( session_params_t* params );
  154. //display packet header
  155. EXPORT void PacketHeaderDisplay ( packet_header_t* header );
  156. //get the packet type from a sesion parameters
  157. EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
  158. //set the packet type in a session parameters
  159. EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
  160. //step of network initialization
  161. EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params );
  162. //get the midi packet number for a given cycle
  163. EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size );
  164. //set the recv timeout on a socket
  165. EXPORT int SetRxTimeout ( int* sockfd, session_params_t* params );
  166. //check if 'next' packet is really the next after 'previous'
  167. EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles );
  168. }