JackNetTool.h 7.44 KB
Newer Older
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2008 Romain Moret at Grame
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
sletz's avatar
sletz committed
19
20
21

#include "JackConstants.h"
#include "JackMidiPort.h"
sletz's avatar
sletz committed
22
#include "JackExports.h"
23
#include "JackError.h"
24
#include "JackTools.h"
sletz's avatar
sletz committed
25
#include "JackPlatformNetSocket.h"
26
#include "types.h"
sletz's avatar
sletz committed
27
28
#include <cmath>

moret's avatar
moret committed
29
30
using namespace std;

sletz's avatar
sletz committed
31
32
namespace Jack
{
33
34
    typedef struct _session_params session_params_t;
    typedef struct _packet_header packet_header_t;
35
    typedef struct _net_transport_data net_transport_data_t;
36
37
38
    typedef struct sockaddr socket_address_t;
    typedef struct in_addr address_t;
    typedef jack_default_audio_sample_t sample_t;
sletz's avatar
sletz committed
39
40
41

//session params ******************************************************************************

42
43
    struct _session_params
    {
moret's avatar
moret committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        char fPacketType[7];                //packet type ('param')
        char fProtocolVersion;              //version
        uint32_t fPacketID;                 //indicates the packet type
        char fName[JACK_CLIENT_NAME_SIZE];  //slave's name
        char fMasterNetName[256];           //master hostname (network)
        char fSlaveNetName[256];            //slave hostname (network)
        uint32_t fMtu;                      //connection mtu
        uint32_t fID;                       //slave's ID
        uint32_t fTransportSync;            //is the transport synced ?
        uint32_t fSendAudioChannels;        //number of master->slave channels
        uint32_t fReturnAudioChannels;      //number of slave->master channels
        uint32_t fSendMidiChannels;         //number of master->slave midi channels
        uint32_t fReturnMidiChannels;       //number of slave->master midi channels
        uint32_t fSampleRate;               //session sample rate
        uint32_t fPeriodSize;               //period size
        uint32_t fFramesPerPacket;          //complete frames per packet
        uint32_t fBitdepth;                 //samples bitdepth (unused)
        uint32_t fSlaveSyncMode;            //is the slave in sync mode ?
62
    };
sletz's avatar
sletz committed
63
64
65

//net status **********************************************************************************

66
67
68
69
70
71
72
73
74
75
    enum  _net_status
    {
        NET_SOCKET_ERROR = 0,
        NET_CONNECT_ERROR,
        NET_ERROR,
        NET_SEND_ERROR,
        NET_RECV_ERROR,
        NET_CONNECTED,
        NET_ROLLING
    };
sletz's avatar
sletz committed
76

77
    typedef enum _net_status net_status_t;
sletz's avatar
sletz committed
78
79
80

//sync packet type ****************************************************************************

81
82
83
84
85
86
87
88
89
    enum _sync_packet_type
    {
        INVALID = 0,	    //...
        SLAVE_AVAILABLE,	//a slave is available
        SLAVE_SETUP,		//slave configuration
        START_MASTER,		//slave is ready, start master
        START_SLAVE,		//master is ready, activate slave
        KILL_MASTER		    //master must stop
    };
sletz's avatar
sletz committed
90

91
    typedef enum _sync_packet_type sync_packet_type_t;
sletz's avatar
sletz committed
92
93
94
95


//packet header *******************************************************************************

96
97
98
    struct _packet_header
    {
        char fPacketType[7];		//packet type ( 'headr' )
99
100
101
102
        char fDataType;				//a for audio, m for midi
        char fDataStream;			//s for send, r for return
        uint32_t fID;				//to identify the slave
        uint32_t fBitdepth;			//bitdepth of the data samples
103
104
        uint32_t fMidiDataSize;		//size of midi data (if packet is 'midi typed') in bytes
        uint32_t fNMidiPckt;		//number of midi packets of the cycle
105
106
107
108
        uint32_t fCycle;			//process cycle counter
        uint32_t fSubCycle;			//midi/audio subcycle counter
        char fIsLastPckt;			//is it the last packet of a given cycle ('y' or 'n')
        char fFree[13];             //unused
109
    };
sletz's avatar
sletz committed
110

111
112
//transport data ******************************************************************************

moret's avatar
moret committed
113
114
115
116
117
118
    struct _net_transport_data
    {
        char fTransportType[10];				//test value ('transport')
        jack_position_t fCurPos;
        jack_transport_state_t fCurState;
    };
119

sletz's avatar
sletz committed
120
121
//midi data ***********************************************************************************

122
123
124
125
126
127
128
129
    class EXPORT NetMidiBuffer
    {
    private:
        int fNPorts;
        size_t fMaxBufsize;
        int fMaxPcktSize;
        char* fBuffer;
        char* fNetBuffer;
sletz's avatar
Cleanup    
sletz committed
130
        JackMidiBuffer** fPortBuffer;
131

132
133
134
135
136
137
138
139
140
141
142
143
144
145
    public:
        NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
        ~NetMidiBuffer();

        void Reset();
        size_t GetSize();
        //utility
        void DisplayEvents();
        //jack<->buffer
        int RenderFromJackPorts();
        int RenderToJackPorts();
        //network<->buffer
        int RenderFromNetwork ( int subcycle, size_t copy_size );
        int RenderToNetwork ( int subcycle, size_t total_size );
146

moret's avatar
moret committed
147
148
        void SetBuffer ( int index, JackMidiBuffer* buffer );
        JackMidiBuffer* GetBuffer ( int index );
149
    };
sletz's avatar
sletz committed
150
151
152

// audio data *********************************************************************************

153
154
155
156
157
158
159
160
    class EXPORT NetAudioBuffer
    {
    private:
        int fNPorts;
        jack_nframes_t fPeriodSize;
        jack_nframes_t fSubPeriodSize;
        size_t fSubPeriodBytesSize;
        char* fNetBuffer;
sletz's avatar
Cleanup    
sletz committed
161
        sample_t** fPortBuffer;
162
163
164
165
166
167
168
169
    public:
        NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
        ~NetAudioBuffer();

        size_t GetSize();
        //jack<->buffer
        void RenderFromJackPorts ( int subcycle );
        void RenderToJackPorts ( int subcycle );
170

moret's avatar
moret committed
171
172
        void SetBuffer ( int index, sample_t* buffer );
        sample_t* GetBuffer ( int index );
173
    };
sletz's avatar
sletz committed
174
175

//utility *************************************************************************************
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

    //socket API management
    EXPORT int SocketAPIInit();
    EXPORT int SocketAPIEnd();
    //n<-->h functions
    EXPORT void SessionParamsHToN ( session_params_t* params );
    EXPORT void SessionParamsNToH ( session_params_t* params );
    EXPORT void PacketHeaderHToN ( packet_header_t* header );
    EXPORT void PacketHeaderNToH ( packet_header_t* header );
    //display session parameters
    EXPORT void SessionParamsDisplay ( session_params_t* params );
    //display packet header
    EXPORT void PacketHeaderDisplay ( packet_header_t* header );
    //get the packet type from a sesion parameters
    EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
    //set the packet type in a session parameters
    EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
    //step of network initialization
    EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params );
    //get the midi packet number for a given cycle
    EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size );
    //set the recv timeout on a socket
    EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params );
    //check if 'next' packet is really the next after 'previous'
    EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles );
sletz's avatar
sletz committed
201
}