00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "JackMidiPort.h"
00021 #include "JackTools.h"
00022 #include "types.h"
00023 #include "transport.h"
00024 #ifndef WIN32
00025 #include <netinet/in.h>
00026 #endif
00027 #include <cmath>
00028
00029 using namespace std;
00030
00031 #ifndef htonll
00032 #ifdef __BIG_ENDIAN__
00033 #define htonll(x) (x)
00034 #define ntohll(x) (x)
00035 #else
00036 #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
00037 #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
00038 #endif
00039 #endif
00040
00041 #define MASTER_PROTOCOL 5
00042 #define SLAVE_PROTOCOL 5
00043
00044 #define NET_PACKET_ERROR -2
00045
00046 #define OPTIMIZED_PROTOCOL
00047
00048 #define HEADER_SIZE (sizeof(packet_header_t))
00049 #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - sizeof(packet_header_t))
00050
00051 namespace Jack
00052 {
00053 typedef struct _session_params session_params_t;
00054 typedef struct _packet_header packet_header_t;
00055 typedef struct _net_transport_data net_transport_data_t;
00056 typedef struct sockaddr socket_address_t;
00057 typedef struct in_addr address_t;
00058 typedef jack_default_audio_sample_t sample_t;
00059
00060 enum JackNetEncoder {
00061
00062 JackFloatEncoder = 0,
00063 JackIntEncoder = 1,
00064 JackCeltEncoder = 2,
00065 };
00066
00067
00068
00086 struct _session_params
00087 {
00088 char fPacketType[7];
00089 char fProtocolVersion;
00090 uint32_t fPacketID;
00091 char fName[JACK_CLIENT_NAME_SIZE];
00092 char fMasterNetName[256];
00093 char fSlaveNetName[256];
00094 uint32_t fMtu;
00095 uint32_t fID;
00096 uint32_t fTransportSync;
00097 int32_t fSendAudioChannels;
00098 int32_t fReturnAudioChannels;
00099 int32_t fSendMidiChannels;
00100 int32_t fReturnMidiChannels;
00101 uint32_t fSampleRate;
00102 uint32_t fPeriodSize;
00103 uint32_t fSampleEncoder;
00104 uint32_t fKBps;
00105 uint32_t fSlaveSyncMode;
00106 uint32_t fNetworkLatency;
00107 };
00108
00109
00110
00115 enum _net_status
00116 {
00117 NET_SOCKET_ERROR = 0,
00118 NET_CONNECT_ERROR,
00119 NET_ERROR,
00120 NET_SEND_ERROR,
00121 NET_RECV_ERROR,
00122 NET_CONNECTED,
00123 NET_ROLLING
00124 };
00125
00126 typedef enum _net_status net_status_t;
00127
00128
00129
00134 enum _sync_packet_type
00135 {
00136 INVALID = 0,
00137 SLAVE_AVAILABLE,
00138 SLAVE_SETUP,
00139 START_MASTER,
00140 START_SLAVE,
00141 KILL_MASTER
00142 };
00143
00144 typedef enum _sync_packet_type sync_packet_type_t;
00145
00146
00147
00148
00168 struct _packet_header
00169 {
00170 char fPacketType[7];
00171 char fDataType;
00172 char fDataStream;
00173 uint32_t fID;
00174 uint32_t fNumPacket;
00175 uint32_t fPacketSize;
00176 uint32_t fActivePorts;
00177 uint32_t fCycle;
00178 uint32_t fSubCycle;
00179 uint32_t fIsLastPckt;
00180 };
00181
00182
00183
00188 enum _net_timebase_master
00189 {
00190 NO_CHANGE = 0,
00191 RELEASE_TIMEBASEMASTER = 1,
00192 TIMEBASEMASTER = 2,
00193 CONDITIONAL_TIMEBASEMASTER = 3
00194 };
00195
00196 typedef enum _net_timebase_master net_timebase_master_t;
00197
00198
00199
00200
00205 struct _net_transport_data
00206 {
00207 uint32_t fNewState;
00208 uint32_t fTimebaseMaster;
00209 int32_t fState;
00210 jack_position_t fPosition;
00211 };
00212
00213
00214
00230 class SERVER_EXPORT NetMidiBuffer
00231 {
00232 private:
00233
00234 int fNPorts;
00235 size_t fMaxBufsize;
00236 int fMaxPcktSize;
00237
00238 char* fBuffer;
00239 char* fNetBuffer;
00240 JackMidiBuffer** fPortBuffer;
00241
00242 size_t fCycleBytesSize;
00243
00244 public:
00245
00246 NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
00247 ~NetMidiBuffer();
00248
00249 void Reset();
00250
00251
00252 size_t GetCycleSize();
00253 int GetNumPackets(int data_sizen, int max_size);
00254
00255 void SetBuffer(int index, JackMidiBuffer* buffer);
00256 JackMidiBuffer* GetBuffer(int index);
00257
00258
00259 void DisplayEvents();
00260
00261
00262 int RenderFromJackPorts();
00263 void RenderToJackPorts();
00264
00265
00266 void RenderFromNetwork(int sub_cycle, size_t copy_size);
00267 int RenderToNetwork(int sub_cycle, size_t total_size);
00268
00269 };
00270
00271
00272
00273 class SERVER_EXPORT NetAudioBuffer
00274 {
00275
00276 protected:
00277
00278 int fNPorts;
00279 int fLastSubCycle;
00280
00281 char* fNetBuffer;
00282 sample_t** fPortBuffer;
00283 bool* fConnectedPorts;
00284
00285 jack_nframes_t fPeriodSize;
00286 jack_nframes_t fSubPeriodSize;
00287 size_t fSubPeriodBytesSize;
00288
00289 float fCycleDuration;
00290 size_t fCycleBytesSize;
00291
00292 int CheckPacket(int cycle, int sub_cycle);
00293 void NextCycle();
00294 void Cleanup();
00295
00296 public:
00297
00298 NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
00299 virtual ~NetAudioBuffer();
00300
00301 bool GetConnected(int port_index) { return fConnectedPorts[port_index]; }
00302 void SetConnected(int port_index, bool state) { fConnectedPorts[port_index] = state; }
00303
00304
00305 virtual size_t GetCycleSize() = 0;
00306
00307
00308 virtual float GetCycleDuration() = 0;
00309
00310 virtual int GetNumPackets(int active_ports) = 0;
00311
00312 virtual void SetBuffer(int index, sample_t* buffer);
00313 virtual sample_t* GetBuffer(int index);
00314
00315
00316 virtual int RenderFromJackPorts();
00317 virtual void RenderToJackPorts();
00318
00319
00320 virtual int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num) = 0;
00321 virtual int RenderToNetwork(int sub_cycle, uint32_t port_num) = 0;
00322
00323 virtual void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle, size_t copy_size) {}
00324 virtual void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle, size_t copy_size) {}
00325
00326 virtual int ActivePortsToNetwork(char* net_buffer);
00327 virtual void ActivePortsFromNetwork(char* net_buffer, uint32_t port_num);
00328
00329 };
00330
00331 class SERVER_EXPORT NetFloatAudioBuffer : public NetAudioBuffer
00332 {
00333
00334 private:
00335
00336 int fPacketSize;
00337
00338 void UpdateParams(int active_ports);
00339
00340 public:
00341
00342 NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
00343 virtual ~NetFloatAudioBuffer();
00344
00345
00346 size_t GetCycleSize();
00347
00348
00349 float GetCycleDuration();
00350 int GetNumPackets(int active_ports);
00351
00352
00353 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
00354 int RenderToNetwork(int sub_cycle, uint32_t port_num);
00355
00356 void RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle);
00357 void RenderToNetwork(char* net_buffer, int active_port, int sub_cycle);
00358
00359 };
00360
00361 #if HAVE_CELT
00362
00363 #include <celt/celt.h>
00364
00365 class SERVER_EXPORT NetCeltAudioBuffer : public NetAudioBuffer
00366 {
00367 private:
00368
00369 CELTMode** fCeltMode;
00370 CELTEncoder** fCeltEncoder;
00371 CELTDecoder** fCeltDecoder;
00372
00373 int fCompressedSizeByte;
00374 int fNumPackets;
00375
00376 size_t fLastSubPeriodBytesSize;
00377
00378 unsigned char** fCompressedBuffer;
00379
00380 void FreeCelt();
00381
00382 public:
00383
00384 NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps);
00385 virtual ~NetCeltAudioBuffer();
00386
00387
00388 size_t GetCycleSize();
00389
00390
00391 float GetCycleDuration();
00392 int GetNumPackets(int active_ports);
00393
00394
00395 int RenderFromJackPorts();
00396 void RenderToJackPorts();
00397
00398
00399 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
00400 int RenderToNetwork(int sub_cycle, uint32_t port_num);
00401 };
00402
00403 #endif
00404
00405 class SERVER_EXPORT NetIntAudioBuffer : public NetAudioBuffer
00406 {
00407 private:
00408
00409 int fCompressedSizeByte;
00410 int fNumPackets;
00411
00412 size_t fLastSubPeriodBytesSize;
00413
00414 short** fIntBuffer;
00415
00416 public:
00417
00418 NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
00419 virtual ~NetIntAudioBuffer();
00420
00421
00422 size_t GetCycleSize();
00423
00424
00425 float GetCycleDuration();
00426 int GetNumPackets(int active_ports);
00427
00428
00429 int RenderFromJackPorts();
00430 void RenderToJackPorts();
00431
00432
00433 int RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num);
00434 int RenderToNetwork(int sub_cycle, uint32_t port_num);
00435 };
00436
00437
00438
00439
00440 SERVER_EXPORT int SocketAPIInit();
00441 SERVER_EXPORT int SocketAPIEnd();
00442
00443 SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params);
00444 SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params);
00445 SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header);
00446 SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header);
00447 SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
00448 SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer);
00449 SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params);
00450 SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params);
00451
00452 SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
00453
00454 SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
00455
00456 SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
00457
00458 SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
00459
00460 SERVER_EXPORT const char* GetTransportState(int transport_state);
00461 SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);
00462 }