Jack2  1.9.8
JackNetTool.h
00001 /*
00002 Copyright (C) 2008-2011 Romain Moret at Grame
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 //session params ******************************************************************************
00068 
00086     struct _session_params
00087     {
00088         char fPacketType[7];                //packet type ('param')
00089         char fProtocolVersion;              //version
00090         uint32_t fPacketID;                 //indicates the packet type
00091         char fName[JACK_CLIENT_NAME_SIZE];  //slave's name
00092         char fMasterNetName[256];           //master hostname (network)
00093         char fSlaveNetName[256];            //slave hostname (network)
00094         uint32_t fMtu;                      //connection mtu
00095         uint32_t fID;                       //slave's ID
00096         uint32_t fTransportSync;            //is the transport synced ?
00097         int32_t fSendAudioChannels;         //number of master->slave channels
00098         int32_t fReturnAudioChannels;       //number of slave->master channels
00099         int32_t fSendMidiChannels;          //number of master->slave midi channels
00100         int32_t fReturnMidiChannels;        //number of slave->master midi channels
00101         uint32_t fSampleRate;               //session sample rate
00102         uint32_t fPeriodSize;               //period size
00103         uint32_t fSampleEncoder;            //samples encoder
00104         uint32_t fKBps;                     //KB per second for CELT encoder
00105         uint32_t fSlaveSyncMode;            //is the slave in sync mode ?
00106         uint32_t fNetworkLatency;           //network latency
00107     };
00108 
00109 //net status **********************************************************************************
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 //sync packet type ****************************************************************************
00129 
00134     enum _sync_packet_type
00135     {
00136         INVALID = 0,        //...
00137         SLAVE_AVAILABLE,    //a slave is available
00138         SLAVE_SETUP,        //slave configuration
00139         START_MASTER,       //slave is ready, start master
00140         START_SLAVE,        //master is ready, activate slave
00141         KILL_MASTER         //master must stop
00142     };
00143 
00144     typedef enum _sync_packet_type sync_packet_type_t;
00145 
00146 
00147 //packet header *******************************************************************************
00148 
00168     struct _packet_header
00169     {
00170         char fPacketType[7];        //packet type ('headr')
00171         char fDataType;             //a for audio, m for midi and s for sync
00172         char fDataStream;           //s for send, r for return
00173         uint32_t fID;               //unique ID of the slave
00174         uint32_t fNumPacket;        //number of data packets of the cycle
00175         uint32_t fPacketSize;       //packet size in bytes
00176         uint32_t fActivePorts;      //number of active ports
00177         uint32_t fCycle;            //process cycle counter
00178         uint32_t fSubCycle;         //midi/audio subcycle counter
00179         uint32_t fIsLastPckt;       //is it the last packet of a given cycle ('y' or 'n')
00180     };
00181 
00182 //net timebase master
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 //transport data ******************************************************************************
00200 
00205     struct _net_transport_data
00206     {
00207         uint32_t fNewState;             //is it a state change
00208         uint32_t fTimebaseMaster;       //is there a new timebase master
00209         int32_t fState;                 //current cycle state
00210         jack_position_t fPosition;      //current cycle position
00211     };
00212 
00213  //midi data ***********************************************************************************
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;  // needed size in bytes ofr an entire cycle
00243 
00244         public:
00245 
00246             NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer);
00247             ~NetMidiBuffer();
00248 
00249             void Reset();
00250 
00251             // needed size in bytes for an entire cycle
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             //utility
00259             void DisplayEvents();
00260 
00261             //jack<->buffer
00262             int RenderFromJackPorts();
00263             void RenderToJackPorts();
00264 
00265             //network<->buffer
00266             void RenderFromNetwork(int sub_cycle, size_t copy_size);
00267             int RenderToNetwork(int sub_cycle, size_t total_size);
00268 
00269     };
00270 
00271 // audio data *********************************************************************************
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;       // in sec
00290             size_t fCycleBytesSize;     // needed size in bytes for an entire cycle
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             // needed syze in bytes ofr an entire cycle
00305             virtual size_t GetCycleSize() = 0;
00306 
00307             // cycle duration in sec
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             //jack<->buffer
00316             virtual int RenderFromJackPorts();
00317             virtual void RenderToJackPorts();
00318 
00319             //network<->buffer
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             // needed size in bytes for an entire cycle
00346             size_t GetCycleSize();
00347 
00348             // cycle duration in sec
00349             float GetCycleDuration();
00350             int GetNumPackets(int active_ports);
00351 
00352             //jack<->buffer
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             // needed size in bytes for an entire cycle
00388             size_t GetCycleSize();
00389 
00390              // cycle duration in sec
00391             float GetCycleDuration();
00392             int GetNumPackets(int active_ports);
00393 
00394             //jack<->buffer
00395             int RenderFromJackPorts();
00396             void RenderToJackPorts();
00397 
00398             //network<->buffer
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             // needed size in bytes for an entire cycle
00422             size_t GetCycleSize();
00423 
00424              // cycle duration in sec
00425             float GetCycleDuration();
00426             int GetNumPackets(int active_ports);
00427 
00428             //jack<->buffer
00429             int RenderFromJackPorts();
00430             void RenderToJackPorts();
00431 
00432             //network<->buffer
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     //utility *************************************************************************************
00438 
00439     //socket API management
00440     SERVER_EXPORT int SocketAPIInit();
00441     SERVER_EXPORT int SocketAPIEnd();
00442     //n<-->h functions
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     //display session parameters
00452     SERVER_EXPORT void SessionParamsDisplay(session_params_t* params);
00453     //display packet header
00454     SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header);
00455     //get the packet type from a sesion parameters
00456     SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params);
00457     //set the packet type in a session parameters
00458     SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type);
00459     //transport utility
00460     SERVER_EXPORT const char* GetTransportState(int transport_state);
00461     SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data);
00462 }