JackNetTool.h 6.07 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
22

#include "types.h"
#include "JackConstants.h"
#include "JackMidiPort.h"
sletz's avatar
sletz committed
23
#include "JackExports.h"
sletz's avatar
sletz committed
24
25
#include "JackError.h"
#include "JackPlatformNetSocket.h"
sletz's avatar
sletz committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <string>
#include <algorithm>
#include <cmath>

namespace Jack
{
	typedef struct _session_params session_params_t;
	typedef struct _packet_header packet_header_t;
	typedef struct sockaddr socket_address_t;
	typedef struct in_addr address_t;
	typedef jack_default_audio_sample_t sample_t;

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

	struct _session_params
	{
		char fPacketType[7];			//packet type ('param')
		char fProtocolVersion;			//version
moret's avatar
cleanup    
moret committed
45
		uint32_t fPacketID;			//indicates the packet type
sletz's avatar
sletz committed
46
47
		char fMasterNetName[256];		//master hostname (network)
		char fSlaveNetName[256];		//slave hostname (network)
moret's avatar
moret committed
48
49
		uint32_t fMtu;				//connection mtu
		uint32_t fID;				//slave's ID
moret's avatar
cleanup    
moret committed
50
51
52
53
		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
moret's avatar
moret committed
54
55
56
57
		uint32_t fSampleRate;			//session sample rate
		uint32_t fPeriodSize;			//period size
		uint32_t fFramesPerPacket;		//complete frames per packet
		uint32_t fBitdepth;			//samples bitdepth (unused)
sletz's avatar
sletz committed
58
59
60
61
62
63
64
65
		char fName[JACK_CLIENT_NAME_SIZE];	//slave's name
	};


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

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

	typedef enum _net_status net_status_t;

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

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

	typedef enum _sync_packet_type sync_packet_type_t;


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

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

//midi data ***********************************************************************************

sletz's avatar
sletz committed
111
	class EXPORT NetMidiBuffer
sletz's avatar
sletz committed
112
113
114
	{
		private:
			int fNPorts;
moret's avatar
moret committed
115
			size_t fMaxBufsize;
sletz's avatar
sletz committed
116
117
118
119
120
			int fMaxPcktSize;
			//data
			char* fBuffer;
			char* fNetBuffer;
		public:
121
			NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
sletz's avatar
sletz committed
122
123
124
125
126
			~NetMidiBuffer();

			JackMidiBuffer** fPortBuffer;

			void Reset();
moret's avatar
moret committed
127
			size_t GetSize();
sletz's avatar
sletz committed
128
129
130
131
132
133
			//utility
			void DisplayEvents();
			//jack<->buffer
			int RenderFromJackPorts();
			int RenderToJackPorts();
			//network<->buffer
moret's avatar
cleanup    
moret committed
134
135
			int RenderFromNetwork ( int subcycle, size_t copy_size );
			int RenderToNetwork ( int subcycle, size_t total_size );
sletz's avatar
sletz committed
136
137
138
139
	};

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

sletz's avatar
sletz committed
140
	class EXPORT NetAudioBuffer
sletz's avatar
sletz committed
141
142
143
144
145
	{
		private:
			int fNPorts;
			jack_nframes_t fPeriodSize;
			jack_nframes_t fSubPeriodSize;
moret's avatar
moret committed
146
			size_t fSubPeriodBytesSize;
sletz's avatar
sletz committed
147
148
			char* fNetBuffer;
		public:
149
			NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer );
sletz's avatar
sletz committed
150
151
			~NetAudioBuffer();

sletz's avatar
sletz committed
152
			sample_t** fPortBuffer;
sletz's avatar
sletz committed
153

moret's avatar
moret committed
154
			size_t GetSize();
sletz's avatar
sletz committed
155
			//jack<->buffer
moret's avatar
cleanup    
moret committed
156
157
			void RenderFromJackPorts ( int subcycle );
			void RenderToJackPorts ( int subcycle );
sletz's avatar
sletz committed
158
159
160
	};

//utility *************************************************************************************
sletz's avatar
sletz committed
161
162
163
164

	//socket API management
	EXPORT int SocketAPIInit();
	EXPORT int SocketAPIEnd();
sletz's avatar
sletz committed
165
	//n<-->h functions
sletz's avatar
sletz committed
166
167
168
169
	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 );
sletz's avatar
sletz committed
170
	//display session parameters
sletz's avatar
sletz committed
171
	EXPORT void SessionParamsDisplay ( session_params_t* params );
sletz's avatar
sletz committed
172
	//display packet header
sletz's avatar
sletz committed
173
	EXPORT void PacketHeaderDisplay ( packet_header_t* header );
sletz's avatar
sletz committed
174
	//get the packet type from a sesion parameters
sletz's avatar
sletz committed
175
	EXPORT sync_packet_type_t GetPacketType ( session_params_t* params );
sletz's avatar
sletz committed
176
	//set the packet type in a session parameters
sletz's avatar
sletz committed
177
	EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
sletz's avatar
sletz committed
178
	//step of network initialization
moret's avatar
cleanup    
moret committed
179
	EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params );
sletz's avatar
sletz committed
180
	//get the midi packet number for a given cycle
moret's avatar
cleanup    
moret committed
181
	EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size );
sletz's avatar
sletz committed
182
	//set the recv timeout on a socket
sletz's avatar
sletz committed
183
	EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params );
sletz's avatar
sletz committed
184
	//check if 'next' packet is really the next after 'previous'
185
	EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles );
sletz's avatar
sletz committed
186
}