2 Copyright (C) 2008-2011 Romain Moret at Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackMidiPort.h"
21 #include "JackTools.h"
23 #include "transport.h"
25 #include <netinet/in.h>
36 #define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
37 #define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
41 #define MASTER_PROTOCOL 6
42 #define SLAVE_PROTOCOL 6
44 #define NET_PACKET_ERROR -2
46 #define OPTIMIZED_PROTOCOL
48 #define HEADER_SIZE (sizeof(packet_header_t))
49 #define PACKET_AVAILABLE_SIZE(params) ((params)->fMtu - sizeof(packet_header_t))
53 typedef struct _session_params session_params_t
;
54 typedef struct _packet_header packet_header_t
;
55 typedef struct _net_transport_data net_transport_data_t
;
56 typedef struct sockaddr socket_address_t
;
57 typedef struct in_addr address_t
;
58 typedef jack_default_audio_sample_t sample_t
;
68 //session params ******************************************************************************
71 \brief This structure containes master/slave connection parameters, it's used to setup the whole system
74 - some info like version, type and packet id
76 - network parameters (hostnames and mtu)
77 - nunber of audio and midi channels
78 - sample rate and buffersize
79 - number of audio frames in one network packet (depends on the channel number)
80 - is the NetDriver in Sync or ASync mode ?
81 - is the NetDriver linked with the master's transport
83 Data encoding : headers (session_params and packet_header) are encoded using HTN kind of functions but float data
84 are kept in LITTLE_ENDIAN format (to avoid 2 conversions in the more common LITTLE_ENDIAN <==> LITTLE_ENDIAN connection case).
88 struct _session_params
90 char fPacketType
[8]; //packet type ('param')
91 uint32_t fProtocolVersion
; //version
92 int32_t fPacketID
; //indicates the packet type
93 char fName
[JACK_CLIENT_NAME_SIZE
]; //slave's name
94 char fMasterNetName
[256]; //master hostname (network)
95 char fSlaveNetName
[256]; //slave hostname (network)
96 uint32_t fMtu
; //connection mtu
97 uint32_t fID
; //slave's ID
98 uint32_t fTransportSync
; //is the transport synced ?
99 int32_t fSendAudioChannels
; //number of master->slave channels
100 int32_t fReturnAudioChannels
; //number of slave->master channels
101 int32_t fSendMidiChannels
; //number of master->slave midi channels
102 int32_t fReturnMidiChannels
; //number of slave->master midi channels
103 uint32_t fSampleRate
; //session sample rate
104 uint32_t fPeriodSize
; //period size
105 uint32_t fSampleEncoder
; //samples encoder
106 uint32_t fKBps
; //KB per second for CELT encoder
107 uint32_t fSlaveSyncMode
; //is the slave in sync mode ?
108 uint32_t fNetworkLatency
; //network latency
109 } POST_PACKED_STRUCTURE
;
111 //net status **********************************************************************************
114 \Brief This enum groups network error by type
119 NET_SOCKET_ERROR
= 0,
128 typedef enum _net_status net_status_t
;
130 //sync packet type ****************************************************************************
133 \Brief This enum indicates the type of a sync packet (used in the initialization phase)
136 enum _sync_packet_type
139 SLAVE_AVAILABLE
, //a slave is available
140 SLAVE_SETUP
, //slave configuration
141 START_MASTER
, //slave is ready, start master
142 START_SLAVE
, //master is ready, activate slave
143 KILL_MASTER
//master must stop
146 typedef enum _sync_packet_type sync_packet_type_t
;
149 //packet header *******************************************************************************
152 \Brief This structure is a complete header
156 - the type of data the packet contains (sync, midi or audio)
157 - the path of the packet (send -master->slave- or return -slave->master-)
158 - the unique ID of the slave
159 - the sample's bitdepth (unused for now)
160 - the size of the midi data contains in the packet (indicates how much midi data will be sent)
161 - the number of midi packet(s) : more than one is very unusual, it depends on the midi load
162 - the ID of the current cycle (used to check missing packets)
163 - the ID of the packet subcycle (for audio data)
164 - a flag indicating this packet is the last of the cycle (for sync robustness, it's better to process this way)
165 - a flag indicating if, in async mode, the previous graph was not finished or not
166 - padding to fill 64 bytes
171 struct _packet_header
173 char fPacketType
[8]; //packet type ('headr')
174 uint32_t fDataType
; //a for audio, m for midi and s for sync
175 uint32_t fDataStream
; //s for send, r for return
176 uint32_t fID
; //unique ID of the slave
177 uint32_t fNumPacket
; //number of data packets of the cycle
178 uint32_t fPacketSize
; //packet size in bytes
179 uint32_t fActivePorts
; //number of active ports
180 uint32_t fCycle
; //process cycle counter
181 uint32_t fSubCycle
; //midi/audio subcycle counter
182 uint32_t fIsLastPckt
; //is it the last packet of a given cycle ('y' or 'n')
183 } POST_PACKED_STRUCTURE
;
185 //net timebase master
188 \Brief This enum describes timebase master's type
191 enum _net_timebase_master
194 RELEASE_TIMEBASEMASTER
= 1,
196 CONDITIONAL_TIMEBASEMASTER
= 3
199 typedef enum _net_timebase_master net_timebase_master_t
;
202 //transport data ******************************************************************************
205 \Brief This structure contains transport data to be sent over the network
209 struct _net_transport_data
211 uint32_t fNewState
; //is it a state change
212 uint32_t fTimebaseMaster
; //is there a new timebase master
213 int32_t fState
; //current cycle state
214 jack_position_t fPosition
; //current cycle position
215 } POST_PACKED_STRUCTURE
;
217 //midi data ***********************************************************************************
220 \Brief Midi buffer and operations class
222 This class is a toolset to manipulate Midi buffers.
223 A JackMidiBuffer has a fixed size, which is the same than an audio buffer size.
224 An intermediate fixed size buffer allows to uninterleave midi data (from jack ports).
225 But for a big majority of the process cycles, this buffer is filled less than 1%,
226 Sending over a network 99% of useless data seems completely unappropriate.
227 The idea is to count effective midi data, and then send the smallest packet we can.
228 To do it, we use an intermediate buffer.
229 We have two methods to convert data from jack ports to intermediate buffer,
230 And two others to convert this intermediate buffer to a network buffer (header + payload data)
234 class SERVER_EXPORT NetMidiBuffer
244 JackMidiBuffer
** fPortBuffer
;
246 size_t fCycleBytesSize
; // needed size in bytes ofr an entire cycle
250 NetMidiBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
);
255 // needed size in bytes for an entire cycle
256 size_t GetCycleSize();
257 int GetNumPackets(int data_sizen
, int max_size
);
259 void SetBuffer(int index
, JackMidiBuffer
* buffer
);
260 JackMidiBuffer
* GetBuffer(int index
);
263 void DisplayEvents();
266 int RenderFromJackPorts();
267 void RenderToJackPorts();
270 void RenderFromNetwork(int sub_cycle
, size_t copy_size
);
271 int RenderToNetwork(int sub_cycle
, size_t total_size
);
275 // audio data *********************************************************************************
277 class SERVER_EXPORT NetAudioBuffer
286 sample_t
** fPortBuffer
;
287 bool* fConnectedPorts
;
289 jack_nframes_t fPeriodSize
;
290 jack_nframes_t fSubPeriodSize
;
291 size_t fSubPeriodBytesSize
;
293 float fCycleDuration
; // in sec
294 size_t fCycleBytesSize
; // needed size in bytes for an entire cycle
296 int CheckPacket(int cycle
, int sub_cycle
);
302 NetAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
);
303 virtual ~NetAudioBuffer();
305 bool GetConnected(int port_index
) { return fConnectedPorts
[port_index
]; }
306 void SetConnected(int port_index
, bool state
) { fConnectedPorts
[port_index
] = state
; }
308 // needed syze in bytes ofr an entire cycle
309 virtual size_t GetCycleSize() = 0;
311 // cycle duration in sec
312 virtual float GetCycleDuration() = 0;
314 virtual int GetNumPackets(int active_ports
) = 0;
316 virtual void SetBuffer(int index
, sample_t
* buffer
);
317 virtual sample_t
* GetBuffer(int index
);
320 virtual int RenderFromJackPorts();
321 virtual void RenderToJackPorts();
324 virtual int RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
) = 0;
325 virtual int RenderToNetwork(int sub_cycle
, uint32_t port_num
) = 0;
327 virtual void RenderFromNetwork(char* net_buffer
, int active_port
, int sub_cycle
, size_t copy_size
) {}
328 virtual void RenderToNetwork(char* net_buffer
, int active_port
, int sub_cycle
, size_t copy_size
) {}
330 virtual int ActivePortsToNetwork(char* net_buffer
);
331 virtual void ActivePortsFromNetwork(char* net_buffer
, uint32_t port_num
);
335 class SERVER_EXPORT NetFloatAudioBuffer
: public NetAudioBuffer
342 void UpdateParams(int active_ports
);
346 NetFloatAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
);
347 virtual ~NetFloatAudioBuffer();
349 // needed size in bytes for an entire cycle
350 size_t GetCycleSize();
352 // cycle duration in sec
353 float GetCycleDuration();
354 int GetNumPackets(int active_ports
);
357 int RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
);
358 int RenderToNetwork(int sub_cycle
, uint32_t port_num
);
360 void RenderFromNetwork(char* net_buffer
, int active_port
, int sub_cycle
);
361 void RenderToNetwork(char* net_buffer
, int active_port
, int sub_cycle
);
367 #include <celt/celt.h>
369 class SERVER_EXPORT NetCeltAudioBuffer
: public NetAudioBuffer
373 CELTMode
** fCeltMode
;
374 CELTEncoder
** fCeltEncoder
;
375 CELTDecoder
** fCeltDecoder
;
377 int fCompressedSizeByte
;
380 size_t fLastSubPeriodBytesSize
;
382 unsigned char** fCompressedBuffer
;
388 NetCeltAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
, int kbps
);
389 virtual ~NetCeltAudioBuffer();
391 // needed size in bytes for an entire cycle
392 size_t GetCycleSize();
394 // cycle duration in sec
395 float GetCycleDuration();
396 int GetNumPackets(int active_ports
);
399 int RenderFromJackPorts();
400 void RenderToJackPorts();
403 int RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
);
404 int RenderToNetwork(int sub_cycle
, uint32_t port_num
);
411 #include <opus/opus.h>
412 #include <opus/opus_custom.h>
414 class SERVER_EXPORT NetOpusAudioBuffer
: public NetAudioBuffer
418 OpusCustomMode
** fOpusMode
;
419 OpusCustomEncoder
** fOpusEncoder
;
420 OpusCustomDecoder
** fOpusDecoder
;
422 unsigned short *fCompressedSizesByte
;
423 int fCompressedMaxSizeByte
;
426 size_t fLastSubPeriodBytesSize
;
428 unsigned char** fCompressedBuffer
;
433 NetOpusAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
, int kbps
);
434 virtual ~NetOpusAudioBuffer();
436 // needed size in bytes for an entire cycle
437 size_t GetCycleSize();
439 // cycle duration in sec
440 float GetCycleDuration();
441 int GetNumPackets(int active_ports
);
444 int RenderFromJackPorts();
445 void RenderToJackPorts();
448 int RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
);
449 int RenderToNetwork(int sub_cycle
, uint32_t port_num
);
454 class SERVER_EXPORT NetIntAudioBuffer
: public NetAudioBuffer
458 int fCompressedSizeByte
;
461 size_t fLastSubPeriodBytesSize
;
467 NetIntAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
);
468 virtual ~NetIntAudioBuffer();
470 // needed size in bytes for an entire cycle
471 size_t GetCycleSize();
473 // cycle duration in sec
474 float GetCycleDuration();
475 int GetNumPackets(int active_ports
);
478 int RenderFromJackPorts();
479 void RenderToJackPorts();
482 int RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
);
483 int RenderToNetwork(int sub_cycle
, uint32_t port_num
);
486 //utility *************************************************************************************
488 //socket API management
489 SERVER_EXPORT
int SocketAPIInit();
490 SERVER_EXPORT
int SocketAPIEnd();
492 SERVER_EXPORT
void SessionParamsHToN(session_params_t
* src_params
, session_params_t
* dst_params
);
493 SERVER_EXPORT
void SessionParamsNToH(session_params_t
* src_params
, session_params_t
* dst_params
);
494 SERVER_EXPORT
void PacketHeaderHToN(packet_header_t
* src_header
, packet_header_t
* dst_header
);
495 SERVER_EXPORT
void PacketHeaderNToH(packet_header_t
* src_header
, packet_header_t
* dst_header
);
496 SERVER_EXPORT
void MidiBufferHToN(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
);
497 SERVER_EXPORT
void MidiBufferNToH(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
);
498 SERVER_EXPORT
void TransportDataHToN(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
);
499 SERVER_EXPORT
void TransportDataNToH(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
);
500 //display session parameters
501 SERVER_EXPORT
void SessionParamsDisplay(session_params_t
* params
);
502 //display packet header
503 SERVER_EXPORT
void PacketHeaderDisplay(packet_header_t
* header
);
504 //get the packet type from a sesion parameters
505 SERVER_EXPORT sync_packet_type_t
GetPacketType(session_params_t
* params
);
506 //set the packet type in a session parameters
507 SERVER_EXPORT
int SetPacketType(session_params_t
* params
, sync_packet_type_t packet_type
);
509 SERVER_EXPORT
const char* GetTransportState(int transport_state
);
510 SERVER_EXPORT
void NetTransportDataDisplay(net_transport_data_t
* data
);