Jack2 1.9.6

JackNetTool.h

00001 /*
00002 Copyright (C) 2008 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 "JackPlatformPlug.h"
00023 #include "types.h"
00024 #include "transport.h"
00025 #ifndef WIN32
00026 #include <netinet/in.h>
00027 #endif
00028 #include <cmath>
00029 
00030 using namespace std;
00031 
00032 #ifndef htonll
00033 #ifdef __BIG_ENDIAN__
00034 #define htonll(x)   (x)
00035 #define ntohll(x)   (x)
00036 #else
00037 #define htonll(x)   ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
00038 #define ntohll(x)   ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
00039 #endif
00040 #endif
00041 
00042 namespace Jack
00043 {
00044     typedef struct _session_params session_params_t;
00045     typedef struct _packet_header packet_header_t;
00046     typedef struct _net_transport_data net_transport_data_t;
00047     typedef struct sockaddr socket_address_t;
00048     typedef struct in_addr address_t;
00049     typedef jack_default_audio_sample_t sample_t;
00050 
00051 //session params ******************************************************************************
00052 
00070     #define MASTER_PROTOCOL 1
00071     #define SLAVE_PROTOCOL 1
00072 
00073     struct _session_params
00074     {
00075         char fPacketType[7];                //packet type ('param')
00076         char fProtocolVersion;              //version
00077         uint32_t fPacketID;                 //indicates the packet type
00078         char fName[JACK_CLIENT_NAME_SIZE];  //slave's name
00079         char fMasterNetName[256];           //master hostname (network)
00080         char fSlaveNetName[256];            //slave hostname (network)
00081         uint32_t fMtu;                      //connection mtu
00082         uint32_t fID;                       //slave's ID
00083         uint32_t fTransportSync;            //is the transport synced ?
00084         uint32_t fSendAudioChannels;        //number of master->slave channels
00085         uint32_t fReturnAudioChannels;      //number of slave->master channels
00086         uint32_t fSendMidiChannels;         //number of master->slave midi channels
00087         uint32_t fReturnMidiChannels;       //number of slave->master midi channels
00088         uint32_t fSampleRate;               //session sample rate
00089         uint32_t fPeriodSize;               //period size
00090         uint32_t fFramesPerPacket;          //complete frames per packet
00091         uint32_t fBitdepth;                 //samples bitdepth (unused)
00092         uint32_t fSlaveSyncMode;            //is the slave in sync mode ?
00093         char fNetworkMode;                  //fast, normal or slow mode
00094     };
00095 
00096 //net status **********************************************************************************
00097 
00102     enum  _net_status
00103     {
00104         NET_SOCKET_ERROR = 0,
00105         NET_CONNECT_ERROR,
00106         NET_ERROR,
00107         NET_SEND_ERROR,
00108         NET_RECV_ERROR,
00109         NET_CONNECTED,
00110         NET_ROLLING
00111     };
00112 
00113     typedef enum _net_status net_status_t;
00114 
00115 //sync packet type ****************************************************************************
00116 
00121     enum _sync_packet_type
00122     {
00123         INVALID = 0,        //...
00124         SLAVE_AVAILABLE,    //a slave is available
00125         SLAVE_SETUP,        //slave configuration
00126         START_MASTER,       //slave is ready, start master
00127         START_SLAVE,        //master is ready, activate slave
00128         KILL_MASTER         //master must stop
00129     };
00130 
00131     typedef enum _sync_packet_type sync_packet_type_t;
00132 
00133 
00134 //packet header *******************************************************************************
00135 
00155     struct _packet_header
00156     {
00157         char fPacketType[7];        //packet type ( 'headr' )
00158         char fDataType;             //a for audio, m for midi and s for sync
00159         char fDataStream;           //s for send, r for return
00160         uint32_t fID;               //unique ID of the slave
00161         uint32_t fBitdepth;         //bitdepth of the data samples
00162         uint32_t fMidiDataSize;     //size of midi data in bytes
00163         uint32_t fNMidiPckt;        //number of midi packets of the cycle
00164         uint32_t fPacketSize;       //packet size in bytes
00165         uint32_t fCycle;            //process cycle counter
00166         uint32_t fSubCycle;         //midi/audio subcycle counter
00167         uint32_t fIsLastPckt;       //is it the last packet of a given cycle ('y' or 'n')
00168         char fASyncWrongCycle;      //is the current async cycle wrong (slave's side; 'y' or 'n')
00169         char fFree[26];             //unused
00170     };
00171 
00172 //net timebase master
00173 
00178     enum _net_timebase_master
00179     {
00180         NO_CHANGE = 0,
00181         RELEASE_TIMEBASEMASTER = 1,
00182         TIMEBASEMASTER = 2,
00183         CONDITIONAL_TIMEBASEMASTER = 3
00184     };
00185 
00186     typedef enum _net_timebase_master net_timebase_master_t;
00187 
00188 
00189 //transport data ******************************************************************************
00190 
00195     struct _net_transport_data
00196     {
00197         uint32_t fNewState;             //is it a state change
00198         uint32_t fTimebaseMaster;       //is there a new timebase master
00199         int32_t fState;                 //current cycle state
00200         jack_position_t fPosition;      //current cycle position
00201     };
00202 
00203 //midi data ***********************************************************************************
00204 
00220     class SERVER_EXPORT NetMidiBuffer
00221     {
00222         private:
00223             int fNPorts;
00224             size_t fMaxBufsize;
00225             int fMaxPcktSize;
00226             char* fBuffer;
00227             char* fNetBuffer;
00228             JackMidiBuffer** fPortBuffer;
00229 
00230         public:
00231             NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
00232             ~NetMidiBuffer();
00233 
00234             void Reset();
00235             size_t GetSize();
00236             //utility
00237             void DisplayEvents();
00238             //jack<->buffer
00239             int RenderFromJackPorts();
00240             int RenderToJackPorts();
00241             //network<->buffer
00242             int RenderFromNetwork ( int subcycle, size_t copy_size );
00243             int RenderToNetwork ( int subcycle, size_t total_size );
00244 
00245             void SetBuffer ( int index, JackMidiBuffer* buffer );
00246             JackMidiBuffer* GetBuffer ( int index );
00247     };
00248 
00249 // audio data *********************************************************************************
00250 
00261     class SERVER_EXPORT NetAudioBuffer
00262     {
00263         private:
00264             int fNPorts;
00265             jack_nframes_t fPeriodSize;
00266             jack_nframes_t fSubPeriodSize;
00267             size_t fSubPeriodBytesSize;
00268             char* fNetBuffer;
00269             sample_t** fPortBuffer;
00270         public:
00271             NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
00272             ~NetAudioBuffer();
00273 
00274             size_t GetSize();
00275             //jack<->buffer
00276             void RenderFromJackPorts ( int subcycle );
00277             void RenderToJackPorts ( int subcycle );
00278 
00279             void SetBuffer ( int index, sample_t* buffer );
00280             sample_t* GetBuffer ( int index );
00281     };
00282 
00283 //utility *************************************************************************************
00284 
00285     //socket API management
00286     SERVER_EXPORT int SocketAPIInit();
00287     SERVER_EXPORT int SocketAPIEnd();
00288     //n<-->h functions
00289     SERVER_EXPORT void SessionParamsHToN ( session_params_t* src_params, session_params_t* dst_params );
00290     SERVER_EXPORT void SessionParamsNToH ( session_params_t* src_params, session_params_t* dst_params );
00291     SERVER_EXPORT void PacketHeaderHToN ( packet_header_t* src_header, packet_header_t* dst_header );
00292     SERVER_EXPORT void PacketHeaderNToH ( packet_header_t* src_header, packet_header_t* dst_header );
00293     SERVER_EXPORT void MidiBufferHToN ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer );
00294     SERVER_EXPORT void MidiBufferNToH ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer );
00295     SERVER_EXPORT void TransportDataHToN ( net_transport_data_t* src_params, net_transport_data_t* dst_params );
00296     SERVER_EXPORT void TransportDataNToH ( net_transport_data_t* src_params, net_transport_data_t* dst_params );
00297     //display session parameters
00298     SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params );
00299     //display packet header
00300     SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header );
00301     //get the packet type from a sesion parameters
00302     SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
00303     //set the packet type in a session parameters
00304     SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
00305     //transport utility
00306     SERVER_EXPORT const char* GetTransportState ( int transport_state );
00307     SERVER_EXPORT void NetTransportDataDisplay ( net_transport_data_t* data );
00308 }