2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2008 Romain Moret at Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackNetInterface.h"
21 #include "JackException.h"
22 #include "JackPlatformPlug.h"
28 TODO : since midi buffers now uses up to BUFFER_SIZE_MAX frames,
29 probably also use BUFFER_SIZE_MAX in everything related to MIDI events
30 handling (see MidiBufferInit in JackMidiPort.cpp)
35 // JackNetInterface*******************************************
37 JackNetInterface::JackNetInterface() : fSocket()
41 fNetAudioCaptureBuffer
= NULL
;
42 fNetAudioPlaybackBuffer
= NULL
;
43 fNetMidiCaptureBuffer
= NULL
;
44 fNetMidiPlaybackBuffer
= NULL
;
45 memset(&fSendTransportData
, 0, sizeof(net_transport_data_t
));
46 memset(&fReturnTransportData
, 0, sizeof(net_transport_data_t
));
49 JackNetInterface::JackNetInterface ( const char* multicast_ip
, int port
) : fSocket ( multicast_ip
, port
)
51 strcpy(fMulticastIP
, multicast_ip
);
54 fNetAudioCaptureBuffer
= NULL
;
55 fNetAudioPlaybackBuffer
= NULL
;
56 fNetMidiCaptureBuffer
= NULL
;
57 fNetMidiPlaybackBuffer
= NULL
;
58 memset(&fSendTransportData
, 0, sizeof(net_transport_data_t
));
59 memset(&fReturnTransportData
, 0, sizeof(net_transport_data_t
));
62 JackNetInterface::JackNetInterface ( session_params_t
& params
, JackNetSocket
& socket
, const char* multicast_ip
) : fSocket ( socket
)
65 strcpy(fMulticastIP
, multicast_ip
);
68 fNetAudioCaptureBuffer
= NULL
;
69 fNetAudioPlaybackBuffer
= NULL
;
70 fNetMidiCaptureBuffer
= NULL
;
71 fNetMidiPlaybackBuffer
= NULL
;
72 memset(&fSendTransportData
, 0, sizeof(net_transport_data_t
));
73 memset(&fReturnTransportData
, 0, sizeof(net_transport_data_t
));
76 JackNetInterface::~JackNetInterface()
78 jack_log ( "JackNetInterface::~JackNetInterface" );
83 delete fNetAudioCaptureBuffer
;
84 delete fNetAudioPlaybackBuffer
;
85 delete fNetMidiCaptureBuffer
;
86 delete fNetMidiPlaybackBuffer
;
89 void JackNetInterface::SetFramesPerPacket()
91 jack_log ( "JackNetInterface::SetFramesPerPacket" );
93 if (fParams
.fSendAudioChannels
== 0 && fParams
.fReturnAudioChannels
== 0) {
94 fParams
.fFramesPerPacket
= fParams
.fPeriodSize
;
96 jack_nframes_t period
= ( int ) powf ( 2.f
, ( int ) ( log (float ( fParams
.fMtu
- sizeof ( packet_header_t
) )
97 / ( max ( fParams
.fReturnAudioChannels
, fParams
.fSendAudioChannels
) * sizeof ( sample_t
) ) ) / log ( 2. ) ) );
98 fParams
.fFramesPerPacket
= ( period
> fParams
.fPeriodSize
) ? fParams
.fPeriodSize
: period
;
102 int JackNetInterface::SetNetBufferSize()
104 jack_log ( "JackNetInterface::SetNetBufferSize" );
106 float audio_size
, midi_size
;
109 audio_size
= fParams
.fMtu
* ( fParams
.fPeriodSize
/ fParams
.fFramesPerPacket
);
111 midi_size
= fParams
.fMtu
* ( max ( fParams
.fSendMidiChannels
, fParams
.fReturnMidiChannels
) *
112 fParams
.fPeriodSize
* sizeof ( sample_t
) / ( fParams
.fMtu
- sizeof ( packet_header_t
) ) );
113 //bufsize = sync + audio + midi
114 bufsize
= MAX_LATENCY
* (fParams
.fMtu
+ ( int ) audio_size
+ ( int ) midi_size
);
117 if ( fSocket
.SetOption ( SOL_SOCKET
, SO_SNDBUF
, &bufsize
, sizeof ( bufsize
) ) == SOCKET_ERROR
)
121 if ( fSocket
.SetOption ( SOL_SOCKET
, SO_RCVBUF
, &bufsize
, sizeof ( bufsize
) ) == SOCKET_ERROR
)
127 int JackNetInterface::GetNMidiPckt()
129 //even if there is no midi data, jack need an empty buffer to know there is no event to read
130 //99% of the cases : all data in one packet
131 if ( fTxHeader
.fMidiDataSize
<= ( fParams
.fMtu
- sizeof ( packet_header_t
) ) )
133 //else, get the number of needed packets (simply slice the biiig buffer)
134 int npckt
= fTxHeader
.fMidiDataSize
/ ( fParams
.fMtu
- sizeof ( packet_header_t
) );
135 if ( fTxHeader
.fMidiDataSize
% ( fParams
.fMtu
- sizeof ( packet_header_t
) ) )
140 bool JackNetInterface::IsNextPacket()
142 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
144 if ( fRxHeader
.fCycle
<= 1 ) {
147 //same PcktID (cycle), next SubPcktID (subcycle)
148 if ( ( fRxHeader
.fSubCycle
< ( fNSubProcess
- 1 ) ) && ( rx_head
->fCycle
== fRxHeader
.fCycle
) && ( rx_head
->fSubCycle
== ( fRxHeader
.fSubCycle
+ 1 ) ) ) {
151 //next PcktID (cycle), SubPcktID reset to 0 (first subcyle)
152 if ( ( rx_head
->fCycle
== ( fRxHeader
.fCycle
+ 1 ) ) && ( fRxHeader
.fSubCycle
== ( fNSubProcess
- 1 ) ) && ( rx_head
->fSubCycle
== 0 ) ) {
155 //else, packet(s) missing, return false
159 void JackNetInterface::SetParams()
161 //number of audio subcycles (packets)
162 fNSubProcess
= fParams
.fPeriodSize
/ fParams
.fFramesPerPacket
;
165 fPayloadSize
= fParams
.fMtu
- sizeof ( packet_header_t
);
168 strcpy ( fTxHeader
.fPacketType
, "header" );
169 fTxHeader
.fID
= fParams
.fID
;
170 fTxHeader
.fCycle
= 0;
171 fTxHeader
.fSubCycle
= 0;
172 fTxHeader
.fMidiDataSize
= 0;
173 fTxHeader
.fBitdepth
= fParams
.fBitdepth
;
174 fTxHeader
.fIsLastPckt
= 0;
177 strcpy ( fRxHeader
.fPacketType
, "header" );
178 fRxHeader
.fID
= fParams
.fID
;
179 fRxHeader
.fCycle
= 0;
180 fRxHeader
.fSubCycle
= 0;
181 fRxHeader
.fMidiDataSize
= 0;
182 fRxHeader
.fBitdepth
= fParams
.fBitdepth
;
183 fRxHeader
.fIsLastPckt
= 0;
186 fTxBuffer
= new char[fParams
.fMtu
];
187 fRxBuffer
= new char[fParams
.fMtu
];
188 assert ( fTxBuffer
);
189 assert ( fRxBuffer
);
191 //net audio/midi buffers'addresses
192 fTxData
= fTxBuffer
+ sizeof ( packet_header_t
);
193 fRxData
= fRxBuffer
+ sizeof ( packet_header_t
);
196 // JackNetMasterInterface ************************************************************************************
198 bool JackNetMasterInterface::Init()
200 jack_log ( "JackNetMasterInterface::Init, ID %u.", fParams
.fID
);
202 session_params_t host_params
;
207 if ( fSocket
.NewSocket() == SOCKET_ERROR
) {
208 jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE
) );
212 //timeout on receive (for init)
213 if ( fSocket
.SetTimeOut ( MASTER_INIT_TIMEOUT
) < 0 )
214 jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE
) );
217 if ( fSocket
.Connect() == SOCKET_ERROR
) {
218 jack_error ( "Can't connect : %s", StrError ( NET_ERROR_CODE
) );
222 //set the number of complete audio frames we can put in a packet
223 SetFramesPerPacket();
225 //send 'SLAVE_SETUP' until 'START_MASTER' received
226 jack_info ( "Sending parameters to %s ...", fParams
.fSlaveNetName
);
229 session_params_t net_params
;
230 SetPacketType ( &fParams
, SLAVE_SETUP
);
231 SessionParamsHToN(&fParams
, &net_params
);
233 if ( fSocket
.Send ( &net_params
, sizeof ( session_params_t
), 0 ) == SOCKET_ERROR
)
234 jack_error ( "Error in send : ", StrError ( NET_ERROR_CODE
) );
236 memset(&net_params
, 0, sizeof ( session_params_t
));
237 if ( ( ( rx_bytes
= fSocket
.Recv ( &net_params
, sizeof ( session_params_t
), 0 ) ) == SOCKET_ERROR
) && ( fSocket
.GetError() != NET_NO_DATA
) )
239 jack_error ( "Problem with network." );
243 SessionParamsNToH(&net_params
, &host_params
);
245 while ( ( GetPacketType ( &host_params
) != START_MASTER
) && ( ++attempt
< SLAVE_SETUP_RETRY
) );
246 if ( attempt
== SLAVE_SETUP_RETRY
) {
247 jack_error ( "Slave doesn't respond, exiting." );
251 //set the new timeout for the socket
252 if ( SetRxTimeout() == SOCKET_ERROR
) {
253 jack_error ( "Can't set rx timeout : %s", StrError ( NET_ERROR_CODE
) );
257 //set the new rx buffer size
258 if ( SetNetBufferSize() == SOCKET_ERROR
) {
259 jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE
) );
266 int JackNetMasterInterface::SetRxTimeout()
268 jack_log ( "JackNetMasterInterface::SetRxTimeout" );
271 //slow or normal mode, short timeout on recv (2 audio subcycles)
272 if ( ( fParams
.fNetworkMode
== 's' ) || ( fParams
.fNetworkMode
== 'n' ) )
273 time
= 2000000.f
* ( static_cast<float> ( fParams
.fFramesPerPacket
) / static_cast<float> ( fParams
.fSampleRate
) );
274 //fast mode, wait for 75% of the entire cycle duration
275 else if ( fParams
.fNetworkMode
== 'f' )
276 time
= 750000.f
* ( static_cast<float> ( fParams
.fPeriodSize
) / static_cast<float> ( fParams
.fSampleRate
) );
277 return fSocket
.SetTimeOut ( static_cast<int> ( time
) );
280 void JackNetMasterInterface::SetParams()
282 jack_log ( "JackNetMasterInterface::SetParams" );
284 JackNetInterface::SetParams();
286 fTxHeader
.fDataStream
= 's';
287 fRxHeader
.fDataStream
= 'r';
290 fNetMidiCaptureBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fSendMidiChannels
, fTxData
);
291 fNetMidiPlaybackBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fReturnMidiChannels
, fRxData
);
292 assert ( fNetMidiCaptureBuffer
);
293 assert ( fNetMidiPlaybackBuffer
);
296 fNetAudioCaptureBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fSendAudioChannels
, fTxData
);
297 fNetAudioPlaybackBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fReturnAudioChannels
, fRxData
);
298 assert ( fNetAudioCaptureBuffer
);
299 assert ( fNetAudioPlaybackBuffer
);
301 //audio netbuffer length
302 fAudioTxLen
= sizeof ( packet_header_t
) + fNetAudioCaptureBuffer
->GetSize();
303 fAudioRxLen
= sizeof ( packet_header_t
) + fNetAudioPlaybackBuffer
->GetSize();
306 void JackNetMasterInterface::Exit()
308 jack_log ( "JackNetMasterInterface::Exit, ID %u", fParams
.fID
);
313 //send a 'multicast euthanasia request' - new socket is required on macosx
314 jack_info ( "Exiting '%s'", fParams
.fName
);
315 SetPacketType ( &fParams
, KILL_MASTER
);
316 JackNetSocket
mcast_socket ( fMulticastIP
, fSocket
.GetPort() );
318 session_params_t net_params
;
319 SessionParamsHToN(&fParams
, &net_params
);
321 if ( mcast_socket
.NewSocket() == SOCKET_ERROR
)
322 jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE
) );
323 if ( mcast_socket
.SendTo ( &net_params
, sizeof ( session_params_t
), 0, fMulticastIP
) == SOCKET_ERROR
)
324 jack_error ( "Can't send suicide request : %s", StrError ( NET_ERROR_CODE
) );
326 mcast_socket
.Close();
329 int JackNetMasterInterface::Recv ( size_t size
, int flags
)
332 if ( ( ( rx_bytes
= fSocket
.Recv ( fRxBuffer
, size
, flags
) ) == SOCKET_ERROR
) && fRunning
)
334 net_error_t error
= fSocket
.GetError();
335 //no data isn't really a network error, so just return 0 avalaible read bytes
336 if ( error
== NET_NO_DATA
)
338 else if ( error
== NET_CONN_ERROR
)
340 //fatal connection issue, exit
341 jack_error ( "'%s' : %s, exiting.", fParams
.fName
, StrError ( NET_ERROR_CODE
) );
342 //ask to the manager to properly remove the master
345 // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
349 jack_error ( "Error in master receive : %s", StrError ( NET_ERROR_CODE
) );
352 packet_header_t
* header
= reinterpret_cast<packet_header_t
*>(fRxBuffer
);
353 PacketHeaderNToH(header
, header
);
357 int JackNetMasterInterface::Send ( size_t size
, int flags
)
360 packet_header_t
* header
= reinterpret_cast<packet_header_t
*>(fTxBuffer
);
361 PacketHeaderHToN(header
, header
);
363 if ( ( ( tx_bytes
= fSocket
.Send ( fTxBuffer
, size
, flags
) ) == SOCKET_ERROR
) && fRunning
)
365 net_error_t error
= fSocket
.GetError();
366 if ( error
== NET_CONN_ERROR
)
368 //fatal connection issue, exit
369 jack_error ( "'%s' : %s, exiting.", fParams
.fName
, StrError ( NET_ERROR_CODE
) );
372 // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
376 jack_error ( "Error in master send : %s", StrError ( NET_ERROR_CODE
) );
381 bool JackNetMasterInterface::IsSynched()
383 if (fParams
.fNetworkMode
== 's') {
384 return (fCycleOffset
< 3);
390 int JackNetMasterInterface::SyncSend()
393 fTxHeader
.fSubCycle
= 0;
394 fTxHeader
.fDataType
= 's';
395 fTxHeader
.fIsLastPckt
= ( fParams
.fSendMidiChannels
== 0 && fParams
.fSendAudioChannels
== 0) ? 1 : 0;
396 fTxHeader
.fPacketSize
= fParams
.fMtu
;
397 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
398 return Send ( fTxHeader
.fPacketSize
, 0 );
401 int JackNetMasterInterface::DataSend()
405 if ( fParams
.fSendMidiChannels
> 0)
407 //set global header fields and get the number of midi packets
408 fTxHeader
.fDataType
= 'm';
409 fTxHeader
.fMidiDataSize
= fNetMidiCaptureBuffer
->RenderFromJackPorts();
410 fTxHeader
.fNMidiPckt
= GetNMidiPckt();
411 for ( subproc
= 0; subproc
< fTxHeader
.fNMidiPckt
; subproc
++ )
413 fTxHeader
.fSubCycle
= subproc
;
414 fTxHeader
.fIsLastPckt
= ( ( subproc
== ( fTxHeader
.fNMidiPckt
- 1 ) ) && (fParams
.fSendAudioChannels
== 0)) ? 1 : 0;
415 fTxHeader
.fPacketSize
= sizeof ( packet_header_t
) + fNetMidiCaptureBuffer
->RenderToNetwork ( subproc
, fTxHeader
.fMidiDataSize
);
416 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
417 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)
423 if ( fParams
.fSendAudioChannels
> 0)
425 fTxHeader
.fDataType
= 'a';
426 fTxHeader
.fMidiDataSize
= 0;
427 fTxHeader
.fNMidiPckt
= 0;
428 for ( subproc
= 0; subproc
< fNSubProcess
; subproc
++ )
430 fTxHeader
.fSubCycle
= subproc
;
431 fTxHeader
.fIsLastPckt
= ( subproc
== ( fNSubProcess
- 1 ) ) ? 1 : 0;
432 fTxHeader
.fPacketSize
= fAudioTxLen
;
433 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
434 fNetAudioCaptureBuffer
->RenderFromJackPorts ( subproc
);
435 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)
443 int JackNetMasterInterface::SyncRecv()
445 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
446 int rx_bytes
= Recv ( fParams
.fMtu
, MSG_PEEK
);
448 if ( ( rx_bytes
== 0 ) || ( rx_bytes
== SOCKET_ERROR
) )
451 fCycleOffset
= fTxHeader
.fCycle
- rx_head
->fCycle
;
453 switch ( fParams
.fNetworkMode
)
456 //slow mode : allow to use full bandwidth and heavy process on the slave
457 // - extra latency is set to two cycles, one cycle for send/receive operations + one cycle for heavy process on the slave
458 // - if the network is two fast, just wait the next cycle, this mode allows a shorter cycle duration for the master
459 // - this mode will skip the two first cycles, thus it lets time for data to be processed and queued on the socket rx buffer
460 //the slow mode is the safest mode because it wait twice the bandwidth relative time (send/return + process)
461 if (fCycleOffset
< 2)
464 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
466 if (fCycleOffset
> 2) {
467 jack_info("Warning : '%s' runs in slow network mode, but data received too late (%d cycle(s) offset)", fParams
.fName
, fCycleOffset
);
472 //normal use of the network :
473 // - extra latency is set to one cycle, what is the time needed to receive streams using full network bandwidth
474 // - if the network is too fast, just wait the next cycle, the benefit here is the master's cycle is shorter
475 // - indeed, data is supposed to be on the network rx buffer, so we don't have to wait for it
476 if (fCycleOffset
< 1)
479 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
481 if (fCycleOffset
!= 1)
482 jack_info("'%s' can't run in normal network mode, data received too late (%d cycle(s) offset)", fParams
.fName
, fCycleOffset
);
486 //fast mode suppose the network bandwith is larger than required for the transmission (only a few channels for example)
487 // - packets can be quickly received, quickly is here relative to the cycle duration
488 // - here, receive data, we can't keep it queued on the rx buffer,
489 // - but if there is a cycle offset, tell the user, that means we're not in fast mode anymore, network is too slow
490 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
492 if (fCycleOffset
!= 0)
493 jack_info("'%s' can't run in fast network mode, data received too late (%d cycle(s) offset)", fParams
.fName
, fCycleOffset
);
497 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
501 int JackNetMasterInterface::DataRecv()
505 uint recvd_midi_pckt
= 0;
506 uint recvd_audio_pckt
= 0;
507 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
509 while ( !fRxHeader
.fIsLastPckt
)
511 //how much data is queued on the rx buffer ?
512 rx_bytes
= Recv ( fParams
.fMtu
, MSG_PEEK
);
514 if ( rx_bytes
== SOCKET_ERROR
)
517 if ( ( rx_bytes
== 0 ) && ( ++jumpcnt
== fNSubProcess
) )
519 jack_error ( "No data from %s...", fParams
.fName
);
522 //else if data is valid,
523 if ( rx_bytes
&& ( rx_head
->fDataStream
== 'r' ) && ( rx_head
->fID
== fParams
.fID
) )
526 switch ( rx_head
->fDataType
)
529 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
530 fRxHeader
.fCycle
= rx_head
->fCycle
;
531 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
532 fNetMidiPlaybackBuffer
->RenderFromNetwork ( rx_head
->fSubCycle
, rx_bytes
- sizeof ( packet_header_t
) );
533 if ( ++recvd_midi_pckt
== rx_head
->fNMidiPckt
)
534 fNetMidiPlaybackBuffer
->RenderToJackPorts();
539 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
541 // if ( !IsNextPacket() )
542 // jack_error ( "Packet(s) missing from '%s'...", fParams.fName );
543 if (recvd_audio_pckt
++ != rx_head
->fSubCycle
) {
544 jack_error("Packet(s) missing from '%s'...", fParams
.fSlaveNetName
);
546 fRxHeader
.fCycle
= rx_head
->fCycle
;
547 fRxHeader
.fSubCycle
= rx_head
->fSubCycle
;
548 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
549 fNetAudioPlaybackBuffer
->RenderToJackPorts ( rx_head
->fSubCycle
);
555 if ( rx_head->fCycle == fTxHeader.fCycle )
558 jack_info("NetMaster : overloaded, skipping receive from '%s'", fParams
.fName
);
566 void JackNetMasterInterface::EncodeSyncPacket()
568 //this method contains every step of sync packet informations coding
569 //first of all, reset sync packet
570 memset ( fTxData
, 0, fPayloadSize
);
572 //then, first step : transport
573 if (fParams
.fTransportSync
) {
574 EncodeTransportData();
575 TransportDataHToN( &fSendTransportData
, &fSendTransportData
);
577 memcpy ( fTxData
, &fSendTransportData
, sizeof ( net_transport_data_t
) );
579 //then others (freewheel etc.)
583 void JackNetMasterInterface::DecodeSyncPacket()
585 //this method contains every step of sync packet informations decoding process
587 if (fParams
.fTransportSync
) {
588 //copy received transport data to transport data structure
589 memcpy ( &fReturnTransportData
, fRxData
, sizeof ( net_transport_data_t
) );
590 TransportDataNToH( &fReturnTransportData
, &fReturnTransportData
);
591 DecodeTransportData();
597 // JackNetSlaveInterface ************************************************************************************************
599 uint
JackNetSlaveInterface::fSlaveCounter
= 0;
601 bool JackNetSlaveInterface::Init()
603 jack_log ( "JackNetSlaveInterface::Init()" );
605 //set the parameters to send
606 strcpy ( fParams
.fPacketType
, "params" );
607 fParams
.fProtocolVersion
= SLAVE_PROTOCOL
;
608 SetPacketType ( &fParams
, SLAVE_AVAILABLE
);
610 //init loop : get a master and start, do it until connection is ok
614 //first, get a master, do it until a valid connection is running
617 status
= SendAvailableToMaster();
618 if ( status
== NET_SOCKET_ERROR
)
621 while ( status
!= NET_CONNECTED
);
623 //then tell the master we are ready
624 jack_info ( "Initializing connection with %s...", fParams
.fMasterNetName
);
625 status
= SendStartToMaster();
626 if ( status
== NET_ERROR
)
629 while ( status
!= NET_ROLLING
);
634 // Separate the connection protocol into two separated step
636 bool JackNetSlaveInterface::InitConnection()
638 jack_log ( "JackNetSlaveInterface::InitConnection()" );
640 //set the parameters to send
641 strcpy (fParams
.fPacketType
, "params");
642 fParams
.fProtocolVersion
= SLAVE_PROTOCOL
;
643 SetPacketType (&fParams
, SLAVE_AVAILABLE
);
649 status
= SendAvailableToMaster();
650 if (status
== NET_SOCKET_ERROR
)
653 while (status
!= NET_CONNECTED
);
658 bool JackNetSlaveInterface::InitRendering()
660 jack_log("JackNetSlaveInterface::InitRendering()");
665 //then tell the master we are ready
666 jack_info("Initializing connection with %s...", fParams
.fMasterNetName
);
667 status
= SendStartToMaster();
668 if (status
== NET_ERROR
)
671 while (status
!= NET_ROLLING
);
676 net_status_t
JackNetSlaveInterface::SendAvailableToMaster()
678 jack_log ( "JackNetSlaveInterface::SendAvailableToMaster()" );
680 session_params_t host_params
;
684 if ( fSocket
.NewSocket() == SOCKET_ERROR
) {
685 jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE
) );
686 return NET_SOCKET_ERROR
;
690 if ( fSocket
.Bind() == SOCKET_ERROR
) {
691 jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE
) );
692 return NET_SOCKET_ERROR
;
696 if ( fSocket
.SetTimeOut ( SLAVE_INIT_TIMEOUT
) == SOCKET_ERROR
)
697 jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE
) );
700 if ( fSocket
.SetLocalLoop() == SOCKET_ERROR
)
701 jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE
) );
703 //send 'AVAILABLE' until 'SLAVE_SETUP' received
704 jack_info ( "Waiting for a master..." );
708 session_params_t net_params
;
709 SessionParamsHToN(&fParams
, &net_params
);
710 if ( fSocket
.SendTo ( &net_params
, sizeof ( session_params_t
), 0, fMulticastIP
) == SOCKET_ERROR
)
711 jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE
) );
713 //filter incoming packets : don't exit while no error is detected
714 memset(&net_params
, 0, sizeof ( session_params_t
));
715 rx_bytes
= fSocket
.CatchHost ( &net_params
, sizeof ( session_params_t
), 0 );
716 SessionParamsNToH(&net_params
, &host_params
);
717 if ( ( rx_bytes
== SOCKET_ERROR
) && ( fSocket
.GetError() != NET_NO_DATA
) )
719 jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE
) );
720 return NET_RECV_ERROR
;
723 while ( strcmp ( host_params
.fPacketType
, fParams
.fPacketType
) && ( GetPacketType ( &host_params
) != SLAVE_SETUP
) );
725 //everything is OK, copy parameters
726 fParams
= host_params
;
728 //set the new buffer sizes
729 if ( SetNetBufferSize() == SOCKET_ERROR
) {
730 jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE
) );
731 return NET_SOCKET_ERROR
;
735 if ( fSocket
.Connect() == SOCKET_ERROR
) {
736 jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE
) );
737 return NET_CONNECT_ERROR
;
740 return NET_CONNECTED
;
743 net_status_t
JackNetSlaveInterface::SendStartToMaster()
745 jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
747 //tell the master to start
748 session_params_t net_params
;
749 SetPacketType ( &fParams
, START_MASTER
);
750 SessionParamsHToN(&fParams
, &net_params
);
751 if ( fSocket
.Send ( &net_params
, sizeof ( session_params_t
), 0 ) == SOCKET_ERROR
)
753 jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE
) );
754 return ( fSocket
.GetError() == NET_CONN_ERROR
) ? NET_ERROR
: NET_SEND_ERROR
;
759 void JackNetSlaveInterface::SetParams()
761 jack_log ( "JackNetSlaveInterface::SetParams" );
763 JackNetInterface::SetParams();
765 fTxHeader
.fDataStream
= 'r';
766 fRxHeader
.fDataStream
= 's';
769 fNetMidiCaptureBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fSendMidiChannels
, fRxData
);
770 fNetMidiPlaybackBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fReturnMidiChannels
, fTxData
);
773 fNetAudioCaptureBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fSendAudioChannels
, fRxData
);
774 fNetAudioPlaybackBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fReturnAudioChannels
, fTxData
);
776 //audio netbuffer length
777 fAudioTxLen
= sizeof ( packet_header_t
) + fNetAudioPlaybackBuffer
->GetSize();
778 fAudioRxLen
= sizeof ( packet_header_t
) + fNetAudioCaptureBuffer
->GetSize();
781 int JackNetSlaveInterface::Recv ( size_t size
, int flags
)
783 int rx_bytes
= fSocket
.Recv ( fRxBuffer
, size
, flags
);
785 if ( rx_bytes
== SOCKET_ERROR
)
787 net_error_t error
= fSocket
.GetError();
788 //no data isn't really an error in realtime processing, so just return 0
789 if ( error
== NET_NO_DATA
)
790 jack_error ( "No data, is the master still running ?" );
791 //if a network error occurs, this exception will restart the driver
792 else if ( error
== NET_CONN_ERROR
)
794 jack_error ( "Connection lost." );
795 throw JackNetException();
798 jack_error ( "Fatal error in slave receive : %s", StrError ( NET_ERROR_CODE
) );
801 packet_header_t
* header
= reinterpret_cast<packet_header_t
*>(fRxBuffer
);
802 PacketHeaderNToH(header
, header
);
806 int JackNetSlaveInterface::Send ( size_t size
, int flags
)
808 packet_header_t
* header
= reinterpret_cast<packet_header_t
*>(fTxBuffer
);
809 PacketHeaderHToN(header
, header
);
810 int tx_bytes
= fSocket
.Send ( fTxBuffer
, size
, flags
);
813 if ( tx_bytes
== SOCKET_ERROR
)
815 net_error_t error
= fSocket
.GetError();
816 //if a network error occurs, this exception will restart the driver
817 if ( error
== NET_CONN_ERROR
)
819 jack_error ( "Connection lost." );
820 throw JackNetException();
823 jack_error ( "Fatal error in slave send : %s", StrError ( NET_ERROR_CODE
) );
828 int JackNetSlaveInterface::SyncRecv()
831 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
832 //receive sync (launch the cycle)
835 rx_bytes
= Recv ( fParams
.fMtu
, 0 );
836 //connection issue, send will detect it, so don't skip the cycle (return 0)
837 if ( rx_bytes
== SOCKET_ERROR
)
840 while ((strcmp(rx_head
->fPacketType
, "header") != 0) && (rx_head
->fDataType
!= 's'));
842 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
846 int JackNetSlaveInterface::DataRecv()
848 uint recvd_midi_pckt
= 0;
849 uint recvd_audio_pckt
= 0;
851 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
853 while ( !fRxHeader
.fIsLastPckt
)
855 rx_bytes
= Recv ( fParams
.fMtu
, MSG_PEEK
);
856 //error here, problem with recv, just skip the cycle (return -1)
858 if ( rx_bytes
== SOCKET_ERROR
)
860 if ( rx_bytes
&& ( rx_head
->fDataStream
== 's' ) && ( rx_head
->fID
== fParams
.fID
) )
862 switch ( rx_head
->fDataType
)
865 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
866 fRxHeader
.fCycle
= rx_head
->fCycle
;
867 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
868 fNetMidiCaptureBuffer
->RenderFromNetwork ( rx_head
->fSubCycle
, rx_bytes
- sizeof ( packet_header_t
) );
869 if ( ++recvd_midi_pckt
== rx_head
->fNMidiPckt
)
870 fNetMidiCaptureBuffer
->RenderToJackPorts();
874 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
876 // if ( !IsNextPacket() )
877 // jack_error ( "Packet(s) missing..." );
878 if (recvd_audio_pckt
++ != rx_head
->fSubCycle
) {
879 jack_error("Packet(s) missing from '%s'...", fParams
.fMasterNetName
);
881 fRxHeader
.fCycle
= rx_head
->fCycle
;
882 fRxHeader
.fSubCycle
= rx_head
->fSubCycle
;
883 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
884 fNetAudioCaptureBuffer
->RenderToJackPorts ( rx_head
->fSubCycle
);
888 jack_info ( "NetSlave : overloaded, skipping receive." );
893 fRxHeader
.fCycle
= rx_head
->fCycle
;
897 int JackNetSlaveInterface::SyncSend()
900 if ( fParams
.fSlaveSyncMode
)
901 fTxHeader
.fCycle
= fRxHeader
.fCycle
;
904 fTxHeader
.fSubCycle
= 0;
905 fTxHeader
.fDataType
= 's';
906 fTxHeader
.fIsLastPckt
= ( fParams
.fReturnMidiChannels
== 0 && fParams
.fReturnAudioChannels
== 0) ? 1 : 0;
907 fTxHeader
.fPacketSize
= fParams
.fMtu
;
908 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
909 return Send ( fTxHeader
.fPacketSize
, 0 );
912 int JackNetSlaveInterface::DataSend()
917 if ( fParams
.fReturnMidiChannels
> 0)
919 fTxHeader
.fDataType
= 'm';
920 fTxHeader
.fMidiDataSize
= fNetMidiPlaybackBuffer
->RenderFromJackPorts();
921 fTxHeader
.fNMidiPckt
= GetNMidiPckt();
922 for ( subproc
= 0; subproc
< fTxHeader
.fNMidiPckt
; subproc
++ )
924 fTxHeader
.fSubCycle
= subproc
;
925 fTxHeader
.fIsLastPckt
= ( ( subproc
== ( fTxHeader
.fNMidiPckt
- 1 ) ) && !fParams
.fReturnAudioChannels
) ? 1 : 0;
926 fTxHeader
.fPacketSize
= sizeof ( packet_header_t
) + fNetMidiPlaybackBuffer
->RenderToNetwork ( subproc
, fTxHeader
.fMidiDataSize
);
927 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
928 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)
934 if ( fParams
.fReturnAudioChannels
> 0)
936 fTxHeader
.fDataType
= 'a';
937 fTxHeader
.fMidiDataSize
= 0;
938 fTxHeader
.fNMidiPckt
= 0;
939 for ( subproc
= 0; subproc
< fNSubProcess
; subproc
++ )
941 fTxHeader
.fSubCycle
= subproc
;
942 fTxHeader
.fIsLastPckt
= ( subproc
== ( fNSubProcess
- 1 ) ) ? 1 : 0;
943 fTxHeader
.fPacketSize
= fAudioTxLen
;
944 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
945 fNetAudioPlaybackBuffer
->RenderFromJackPorts ( subproc
);
946 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)
953 //network sync------------------------------------------------------------------------
954 void JackNetSlaveInterface::EncodeSyncPacket()
956 //this method contains every step of sync packet informations coding
957 //first of all, reset sync packet
958 memset ( fTxData
, 0, fPayloadSize
);
959 //then first step : transport
960 if (fParams
.fTransportSync
) {
961 EncodeTransportData();
962 TransportDataHToN( &fReturnTransportData
, &fReturnTransportData
);
964 memcpy ( fTxData
, &fReturnTransportData
, sizeof ( net_transport_data_t
) );
970 void JackNetSlaveInterface::DecodeSyncPacket()
972 //this method contains every step of sync packet informations decoding process
974 if (fParams
.fTransportSync
) {
975 //copy received transport data to transport data structure
976 memcpy ( &fSendTransportData
, fRxData
, sizeof ( net_transport_data_t
) );
977 TransportDataNToH( &fSendTransportData
, &fSendTransportData
);
978 DecodeTransportData();