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"
29 // JackNetInterface*******************************************
31 JackNetInterface::JackNetInterface() : fSocket()
36 fNetAudioCaptureBuffer
= NULL
;
37 fNetAudioPlaybackBuffer
= NULL
;
38 fNetMidiCaptureBuffer
= NULL
;
39 fNetMidiPlaybackBuffer
= NULL
;
42 JackNetInterface::JackNetInterface ( const char* multicast_ip
, int port
) : fSocket ( multicast_ip
, port
)
44 fMulticastIP
= strdup ( multicast_ip
);
47 fNetAudioCaptureBuffer
= NULL
;
48 fNetAudioPlaybackBuffer
= NULL
;
49 fNetMidiCaptureBuffer
= NULL
;
50 fNetMidiPlaybackBuffer
= NULL
;
53 JackNetInterface::JackNetInterface ( session_params_t
& params
, JackNetSocket
& socket
, const char* multicast_ip
) : fSocket ( socket
)
56 fMulticastIP
= strdup ( multicast_ip
);
59 fNetAudioCaptureBuffer
= NULL
;
60 fNetAudioPlaybackBuffer
= NULL
;
61 fNetMidiCaptureBuffer
= NULL
;
62 fNetMidiPlaybackBuffer
= NULL
;
65 JackNetInterface::~JackNetInterface()
67 jack_log ( "JackNetInterface::~JackNetInterface" );
72 delete[] fMulticastIP
;
73 delete fNetAudioCaptureBuffer
;
74 delete fNetAudioPlaybackBuffer
;
75 delete fNetMidiCaptureBuffer
;
76 delete fNetMidiPlaybackBuffer
;
79 jack_nframes_t
JackNetInterface::SetFramesPerPacket()
81 jack_log ( "JackNetInterface::SetFramesPerPacket" );
83 if ( !fParams
.fSendAudioChannels
&& !fParams
.fReturnAudioChannels
)
84 return ( fParams
.fFramesPerPacket
= fParams
.fPeriodSize
);
85 jack_nframes_t period
= ( int ) powf ( 2.f
, ( int ) ( log ( ( fParams
.fMtu
- sizeof ( packet_header_t
) )
86 / ( max ( fParams
.fReturnAudioChannels
, fParams
.fSendAudioChannels
) * sizeof ( sample_t
) ) ) / log ( 2 ) ) );
87 return ( fParams
.fFramesPerPacket
= ( period
> fParams
.fPeriodSize
) ? fParams
.fPeriodSize
: period
);
90 int JackNetInterface::SetNetBufferSize()
92 jack_log ( "JackNetInterface::SetNetBufferSize" );
94 float audio_size
, midi_size
;
97 audio_size
= fParams
.fMtu
* ( fParams
.fPeriodSize
/ fParams
.fFramesPerPacket
);
99 midi_size
= fParams
.fMtu
* ( max ( fParams
.fSendMidiChannels
, fParams
.fReturnMidiChannels
) *
100 fParams
.fPeriodSize
* sizeof ( sample_t
) / ( fParams
.fMtu
- sizeof ( packet_header_t
) ) );
101 //bufsize = sync + audio + midi
102 bufsize
= fParams
.fMtu
+ ( int ) audio_size
+ ( int ) midi_size
;
105 if ( fSocket
.SetOption ( SOL_SOCKET
, SO_SNDBUF
, &bufsize
, sizeof ( bufsize
) ) == SOCKET_ERROR
)
109 if ( fSocket
.SetOption ( SOL_SOCKET
, SO_RCVBUF
, &bufsize
, sizeof ( bufsize
) ) == SOCKET_ERROR
)
115 int JackNetInterface::GetNMidiPckt()
117 //even if there is no midi data, jack need an empty buffer to know there is no event to read
118 //99% of the cases : all data in one packet
119 if ( fTxHeader
.fMidiDataSize
<= ( fParams
.fMtu
- sizeof ( packet_header_t
) ) )
121 //else, get the number of needed packets (simply slice the biiig buffer)
122 int npckt
= fTxHeader
.fMidiDataSize
/ ( fParams
.fMtu
- sizeof ( packet_header_t
) );
123 if ( fTxHeader
.fMidiDataSize
% ( fParams
.fMtu
- sizeof ( packet_header_t
) ) )
128 bool JackNetInterface::IsNextPacket()
130 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
132 if ( fRxHeader
.fCycle
<= 1 )
134 //same PcktID (cycle), next SubPcktID (subcycle)
135 if ( ( fRxHeader
.fSubCycle
< ( fNSubProcess
- 1 ) ) && ( rx_head
->fCycle
== fRxHeader
.fCycle
) && ( rx_head
->fSubCycle
== ( fRxHeader
.fSubCycle
+ 1 ) ) )
137 //next PcktID (cycle), SubPcktID reset to 0 (first subcyle)
138 if ( ( rx_head
->fCycle
== ( fRxHeader
.fCycle
+ 1 ) ) && ( fRxHeader
.fSubCycle
== ( fNSubProcess
- 1 ) ) && ( rx_head
->fSubCycle
== 0 ) )
140 //else, packet(s) missing, return false
144 void JackNetInterface::SetParams()
146 //number of audio subcycles (packets)
147 fNSubProcess
= fParams
.fPeriodSize
/ fParams
.fFramesPerPacket
;
150 fPayloadSize
= fParams
.fMtu
- sizeof ( packet_header_t
);
153 strcpy ( fTxHeader
.fPacketType
, "header" );
154 fTxHeader
.fID
= fParams
.fID
;
155 fTxHeader
.fCycle
= 0;
156 fTxHeader
.fSubCycle
= 0;
157 fTxHeader
.fMidiDataSize
= 0;
158 fTxHeader
.fBitdepth
= fParams
.fBitdepth
;
159 fTxHeader
.fIsLastPckt
= 0;
162 strcpy ( fRxHeader
.fPacketType
, "header" );
163 fRxHeader
.fID
= fParams
.fID
;
164 fRxHeader
.fCycle
= 0;
165 fRxHeader
.fSubCycle
= 0;
166 fRxHeader
.fMidiDataSize
= 0;
167 fRxHeader
.fBitdepth
= fParams
.fBitdepth
;
168 fRxHeader
.fIsLastPckt
= 0;
171 fTxBuffer
= new char[fParams
.fMtu
];
172 fRxBuffer
= new char[fParams
.fMtu
];
173 assert ( fTxBuffer
);
174 assert ( fRxBuffer
);
176 //net audio/midi buffers'addresses
177 fTxData
= fTxBuffer
+ sizeof ( packet_header_t
);
178 fRxData
= fRxBuffer
+ sizeof ( packet_header_t
);
181 // JackNetMasterInterface ************************************************************************************
183 bool JackNetMasterInterface::Init()
185 jack_log ( "JackNetMasterInterface::Init, ID %u.", fParams
.fID
);
187 session_params_t params
;
192 if ( fSocket
.NewSocket() == SOCKET_ERROR
)
194 jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE
) );
198 //timeout on receive (for init)
199 if ( fSocket
.SetTimeOut ( MASTER_INIT_TIMEOUT
) < 0 )
200 jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE
) );
203 if ( fSocket
.Connect() == SOCKET_ERROR
)
205 jack_error ( "Can't connect : %s", StrError ( NET_ERROR_CODE
) );
209 //set the number of complete audio frames we can put in a packet
210 SetFramesPerPacket();
212 //send 'SLAVE_SETUP' until 'START_MASTER' received
213 jack_info ( "Sending parameters to %s ...", fParams
.fSlaveNetName
);
216 SetPacketType ( &fParams
, SLAVE_SETUP
);
217 if ( fSocket
.Send ( &fParams
, sizeof ( session_params_t
), 0 ) == SOCKET_ERROR
)
218 jack_error ( "Error in send : ", StrError ( NET_ERROR_CODE
) );
219 if ( ( ( rx_bytes
= fSocket
.Recv ( ¶ms
, sizeof ( session_params_t
), 0 ) ) == SOCKET_ERROR
) && ( fSocket
.GetError() != NET_NO_DATA
) )
221 jack_error ( "Problem with network." );
225 while ( ( GetPacketType ( ¶ms
) != START_MASTER
) && ( ++attempt
< SLAVE_SETUP_RETRY
) );
226 if ( attempt
== SLAVE_SETUP_RETRY
)
228 jack_error ( "Slave doesn't respond, exiting." );
232 //set the new timeout for the socket
233 if ( SetRxTimeout() == SOCKET_ERROR
)
235 jack_error ( "Can't set rx timeout : %s", StrError ( NET_ERROR_CODE
) );
239 //set the new rx buffer size
240 if ( SetNetBufferSize() == SOCKET_ERROR
)
242 jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE
) );
249 int JackNetMasterInterface::SetRxTimeout()
251 jack_log ( "JackNetMasterInterface::SetRxTimeout" );
254 //slow or normal mode, short timeout on recv (2 audio subcycles)
255 if ( ( fParams
.fNetworkMode
== 's' ) || ( fParams
.fNetworkMode
== 'n' ) )
256 time
= 2000000.f
* ( static_cast<float> ( fParams
.fFramesPerPacket
) / static_cast<float> ( fParams
.fSampleRate
) );
257 //fast mode, wait for 75% of the entire cycle duration
258 else if ( fParams
.fNetworkMode
== 'f' )
259 time
= 750000.f
* ( static_cast<float> ( fParams
.fPeriodSize
) / static_cast<float> ( fParams
.fSampleRate
) );
260 return fSocket
.SetTimeOut ( static_cast<int> ( time
) );
263 void JackNetMasterInterface::SetParams()
265 jack_log ( "JackNetMasterInterface::SetParams" );
267 JackNetInterface::SetParams();
269 fTxHeader
.fDataStream
= 's';
270 fRxHeader
.fDataStream
= 'r';
273 fNetMidiCaptureBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fSendMidiChannels
, fTxData
);
274 fNetMidiPlaybackBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fReturnMidiChannels
, fRxData
);
275 assert ( fNetMidiCaptureBuffer
);
276 assert ( fNetMidiPlaybackBuffer
);
279 fNetAudioCaptureBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fSendAudioChannels
, fTxData
);
280 fNetAudioPlaybackBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fReturnAudioChannels
, fRxData
);
281 assert ( fNetAudioCaptureBuffer
);
282 assert ( fNetAudioPlaybackBuffer
);
284 //audio netbuffer length
285 fAudioTxLen
= sizeof ( packet_header_t
) + fNetAudioPlaybackBuffer
->GetSize();
286 fAudioRxLen
= sizeof ( packet_header_t
) + fNetAudioCaptureBuffer
->GetSize();
289 void JackNetMasterInterface::Exit()
291 jack_log ( "JackNetMasterInterface::Exit, ID %u", fParams
.fID
);
296 //send a 'multicast euthanasia request' - new socket is required on macosx
297 jack_info ( "Exiting '%s'", fParams
.fName
);
298 SetPacketType ( &fParams
, KILL_MASTER
);
299 JackNetSocket
mcast_socket ( fMulticastIP
, fSocket
.GetPort() );
300 if ( mcast_socket
.NewSocket() == SOCKET_ERROR
)
301 jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE
) );
302 if ( mcast_socket
.SendTo ( &fParams
, sizeof ( session_params_t
), 0, fMulticastIP
) == SOCKET_ERROR
)
303 jack_error ( "Can't send suicide request : %s", StrError ( NET_ERROR_CODE
) );
304 mcast_socket
.Close();
306 // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
310 int JackNetMasterInterface::Send ( size_t size
, int flags
)
313 if ( ( ( tx_bytes
= fSocket
.Send ( fTxBuffer
, size
, flags
) ) == SOCKET_ERROR
) && fRunning
)
315 net_error_t error
= fSocket
.GetError();
316 if ( error
== NET_CONN_ERROR
)
318 //fatal connection issue, exit
319 jack_error ( "'%s' : %s, exiting.", fParams
.fName
, StrError ( NET_ERROR_CODE
) );
323 jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE
) );
328 int JackNetMasterInterface::Recv ( size_t size
, int flags
)
331 if ( ( ( rx_bytes
= fSocket
.Recv ( fRxBuffer
, size
, flags
) ) == SOCKET_ERROR
) && fRunning
)
333 net_error_t error
= fSocket
.GetError();
334 //no data isn't really a network error, so just return 0 avalaible read bytes
335 if ( error
== NET_NO_DATA
)
337 else if ( error
== NET_CONN_ERROR
)
339 //fatal connection issue, exit
340 jack_error ( "'%s' : %s, exiting.", fParams
.fName
, StrError ( NET_ERROR_CODE
) );
341 //ask to the manager to properly remove the master
345 jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE
) );
350 int JackNetMasterInterface::SyncSend()
353 fTxHeader
.fSubCycle
= 0;
354 fTxHeader
.fDataType
= 's';
355 fTxHeader
.fIsLastPckt
= ( !fParams
.fSendMidiChannels
&& !fParams
.fSendAudioChannels
) ? 1 : 0;
356 fTxHeader
.fPacketSize
= fParams
.fMtu
;
357 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
358 return Send ( fTxHeader
.fPacketSize
, 0 );
361 int JackNetMasterInterface::DataSend()
365 if ( fParams
.fSendMidiChannels
)
367 //set global header fields and get the number of midi packets
368 fTxHeader
.fDataType
= 'm';
369 fTxHeader
.fMidiDataSize
= fNetMidiCaptureBuffer
->RenderFromJackPorts();
370 fTxHeader
.fNMidiPckt
= GetNMidiPckt();
371 for ( subproc
= 0; subproc
< fTxHeader
.fNMidiPckt
; subproc
++ )
373 fTxHeader
.fSubCycle
= subproc
;
374 fTxHeader
.fIsLastPckt
= ( ( subproc
== ( fTxHeader
.fNMidiPckt
- 1 ) ) && !fParams
.fSendAudioChannels
) ? 1 : 0;
375 fTxHeader
.fPacketSize
= sizeof ( packet_header_t
);
376 fTxHeader
.fPacketSize
+= fNetMidiCaptureBuffer
->RenderToNetwork ( subproc
, fTxHeader
.fMidiDataSize
);
377 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
378 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)
384 if ( fParams
.fSendAudioChannels
)
386 fTxHeader
.fDataType
= 'a';
387 for ( subproc
= 0; subproc
< fNSubProcess
; subproc
++ )
389 fTxHeader
.fSubCycle
= subproc
;
390 fTxHeader
.fIsLastPckt
= ( subproc
== ( fNSubProcess
- 1 ) ) ? 1 : 0;
391 fTxHeader
.fPacketSize
= fAudioTxLen
;
392 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
393 fNetAudioCaptureBuffer
->RenderFromJackPorts ( subproc
);
394 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)
402 int JackNetMasterInterface::SyncRecv()
405 int cycle_offset
= 0;
406 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
407 rx_bytes
= Recv ( fParams
.fMtu
, MSG_PEEK
);
408 if ( ( rx_bytes
== 0 ) || ( rx_bytes
== SOCKET_ERROR
) )
411 cycle_offset
= fTxHeader
.fCycle
- rx_head
->fCycle
;
413 switch ( fParams
.fNetworkMode
)
416 //slow mode : allow to use full bandwidth and heavy process on the slave
417 // - extra latency is set to two cycles, one cycle for send/receive operations + one cycle for heavy process on the slave
418 // - if the network is two fast, just wait the next cycle, this mode allows a shorter cycle duration for the master
419 // - this mode will skip the two first cycles, thus it lets time for data to be processed and queued on the socket rx buffer
420 //the slow mode is the safest mode because it wait twice the bandwidth relative time (send/return + process)
421 if ( cycle_offset
< 2 )
424 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
428 //normal use of the network :
429 // - extra latency is set to one cycle, what is the time needed to receive streams using full network bandwidth
430 // - if the network is too fast, just wait the next cycle, the benefit here is the master's cycle is shorter
431 // - indeed, data is supposed to be on the network rx buffer, so we don't have to wait for it
432 if ( cycle_offset
< 1 )
435 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
439 //fast mode suppose the network bandwith is larger than required for the transmission (only a few channels for example)
440 // - packets can be quickly received, quickly is here relative to the cycle duration
441 // - here, receive data, we can't keep it queued on the rx buffer,
442 // - but if there is a cycle offset, tell the user, that means we're not in fast mode anymore, network is too slow
443 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
445 jack_error ( "'%s' can't run in fast network mode, data received too late (%d cycle(s) offset)", fParams
.fName
, cycle_offset
);
449 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
453 int JackNetMasterInterface::DataRecv()
457 uint midi_recvd_pckt
= 0;
458 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
460 while ( !fRxHeader
.fIsLastPckt
)
462 //how much data is queued on the rx buffer ?
463 rx_bytes
= Recv ( fParams
.fMtu
, MSG_PEEK
);
464 if ( rx_bytes
== SOCKET_ERROR
)
467 if ( ( rx_bytes
== 0 ) && ( ++jumpcnt
== fNSubProcess
) )
469 jack_error ( "No data from %s...", fParams
.fName
);
472 //else if data is valid,
473 if ( rx_bytes
&& ( rx_head
->fDataStream
== 'r' ) && ( rx_head
->fID
== fParams
.fID
) )
476 switch ( rx_head
->fDataType
)
479 Recv ( rx_head
->fPacketSize
, 0 );
480 fRxHeader
.fCycle
= rx_head
->fCycle
;
481 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
482 fNetMidiPlaybackBuffer
->RenderFromNetwork ( rx_head
->fSubCycle
, rx_bytes
- sizeof ( packet_header_t
) );
483 if ( ++midi_recvd_pckt
== rx_head
->fNMidiPckt
)
484 fNetMidiPlaybackBuffer
->RenderToJackPorts();
489 Recv ( rx_head
->fPacketSize
, 0 );
490 if ( !IsNextPacket() )
491 jack_error ( "Packet(s) missing from '%s'...", fParams
.fName
);
492 fRxHeader
.fCycle
= rx_head
->fCycle
;
493 fRxHeader
.fSubCycle
= rx_head
->fSubCycle
;
494 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
495 fNetAudioPlaybackBuffer
->RenderToJackPorts ( rx_head
->fSubCycle
);
500 if ( rx_head
->fCycle
== fTxHeader
.fCycle
)
509 // JackNetSlaveInterface ************************************************************************************************
511 uint
JackNetSlaveInterface::fSlaveCounter
= 0;
513 bool JackNetSlaveInterface::Init()
515 jack_log ( "JackNetSlaveInterface::Init()" );
517 //set the parameters to send
518 strcpy ( fParams
.fPacketType
, "params" );
519 fParams
.fProtocolVersion
= 'a';
520 SetPacketType ( &fParams
, SLAVE_AVAILABLE
);
522 //init loop : get a master and start, do it until connection is ok
526 //first, get a master, do it until a valid connection is running
529 status
= GetNetMaster();
530 if ( status
== NET_SOCKET_ERROR
)
533 while ( status
!= NET_CONNECTED
);
535 //then tell the master we are ready
536 jack_info ( "Initializing connection with %s...", fParams
.fMasterNetName
);
537 status
= SendStartToMaster();
538 if ( status
== NET_ERROR
)
541 while ( status
!= NET_ROLLING
);
546 net_status_t
JackNetSlaveInterface::GetNetMaster()
548 jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
550 session_params_t params
;
554 if ( fSocket
.NewSocket() == SOCKET_ERROR
)
556 jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE
) );
557 return NET_SOCKET_ERROR
;
561 if ( fSocket
.Bind() == SOCKET_ERROR
)
562 jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE
) );
565 if ( fSocket
.SetTimeOut ( SLAVE_INIT_TIMEOUT
) == SOCKET_ERROR
)
566 jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE
) );
569 if ( fSocket
.SetLocalLoop() == SOCKET_ERROR
)
570 jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE
) );
572 //send 'AVAILABLE' until 'SLAVE_SETUP' received
573 jack_info ( "Waiting for a master..." );
577 if ( fSocket
.SendTo ( &fParams
, sizeof ( session_params_t
), 0, fMulticastIP
) == SOCKET_ERROR
)
578 jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE
) );
579 //filter incoming packets : don't exit while no error is detected
580 rx_bytes
= fSocket
.CatchHost ( ¶ms
, sizeof ( session_params_t
), 0 );
581 if ( ( rx_bytes
== SOCKET_ERROR
) && ( fSocket
.GetError() != NET_NO_DATA
) )
583 jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE
) );
584 return NET_RECV_ERROR
;
587 while ( strcmp ( params
.fPacketType
, fParams
.fPacketType
) && ( GetPacketType ( ¶ms
) != SLAVE_SETUP
) );
589 //everything is OK, copy parameters
592 //set the new buffer sizes
593 if ( SetNetBufferSize() == SOCKET_ERROR
)
594 jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE
) );
597 if ( fSocket
.Connect() == SOCKET_ERROR
)
599 jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE
) );
600 return NET_CONNECT_ERROR
;
603 return NET_CONNECTED
;
606 net_status_t
JackNetSlaveInterface::SendStartToMaster()
608 jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
610 //tell the master to start
611 SetPacketType ( &fParams
, START_MASTER
);
612 if ( fSocket
.Send ( &fParams
, sizeof ( session_params_t
), 0 ) == SOCKET_ERROR
)
614 jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE
) );
615 return ( fSocket
.GetError() == NET_CONN_ERROR
) ? NET_ERROR
: NET_SEND_ERROR
;
620 void JackNetSlaveInterface::SetParams()
622 jack_log ( "JackNetSlaveInterface::SetParams" );
624 JackNetInterface::SetParams();
626 fTxHeader
.fDataStream
= 'r';
627 fRxHeader
.fDataStream
= 's';
630 fNetMidiCaptureBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fSendMidiChannels
, fRxData
);
631 fNetMidiPlaybackBuffer
= new NetMidiBuffer ( &fParams
, fParams
.fReturnMidiChannels
, fTxData
);
634 fNetAudioCaptureBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fSendAudioChannels
, fRxData
);
635 fNetAudioPlaybackBuffer
= new NetAudioBuffer ( &fParams
, fParams
.fReturnAudioChannels
, fTxData
);
637 //audio netbuffer length
638 fAudioTxLen
= sizeof ( packet_header_t
) + fNetAudioPlaybackBuffer
->GetSize();
639 fAudioRxLen
= sizeof ( packet_header_t
) + fNetAudioCaptureBuffer
->GetSize();
642 int JackNetSlaveInterface::Recv ( size_t size
, int flags
)
644 int rx_bytes
= fSocket
.Recv ( fRxBuffer
, size
, flags
);
646 if ( rx_bytes
== SOCKET_ERROR
)
648 net_error_t error
= fSocket
.GetError();
649 //no data isn't really an error in realtime processing, so just return 0
650 if ( error
== NET_NO_DATA
)
651 jack_error ( "No data, is the master still running ?" );
652 //if a network error occurs, this exception will restart the driver
653 else if ( error
== NET_CONN_ERROR
)
655 jack_error ( "Connection lost." );
656 throw JackNetException();
659 jack_error ( "Fatal error in receive : %s", StrError ( NET_ERROR_CODE
) );
664 int JackNetSlaveInterface::Send ( size_t size
, int flags
)
666 int tx_bytes
= fSocket
.Send ( fTxBuffer
, size
, flags
);
668 if ( tx_bytes
== SOCKET_ERROR
)
670 net_error_t error
= fSocket
.GetError();
671 //if a network error occurs, this exception will restart the driver
672 if ( error
== NET_CONN_ERROR
)
674 jack_error ( "Connection lost." );
675 throw JackNetException();
678 jack_error ( "Fatal error in send : %s", StrError ( NET_ERROR_CODE
) );
683 int JackNetSlaveInterface::SyncRecv()
686 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
687 //receive sync (launch the cycle)
690 rx_bytes
= Recv ( fParams
.fMtu
, 0 );
691 //connection issue, send will detect it, so don't skip the cycle (return 0)
692 if ( rx_bytes
== SOCKET_ERROR
)
695 while ( !rx_bytes
&& ( rx_head
->fDataType
!= 's' ) );
696 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
700 int JackNetSlaveInterface::DataRecv()
702 uint recvd_midi_pckt
= 0;
704 packet_header_t
* rx_head
= reinterpret_cast<packet_header_t
*> ( fRxBuffer
);
706 while ( !fRxHeader
.fIsLastPckt
)
708 rx_bytes
= Recv ( fParams
.fMtu
, MSG_PEEK
);
709 //error here, problem with recv, just skip the cycle (return -1)
711 if ( rx_bytes
== SOCKET_ERROR
)
713 if ( rx_bytes
&& ( rx_head
->fDataStream
== 's' ) && ( rx_head
->fID
== fParams
.fID
) )
715 switch ( rx_head
->fDataType
)
718 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
719 fRxHeader
.fCycle
= rx_head
->fCycle
;
720 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
721 fNetMidiCaptureBuffer
->RenderFromNetwork ( rx_head
->fSubCycle
, rx_bytes
- sizeof ( packet_header_t
) );
722 if ( ++recvd_midi_pckt
== rx_head
->fNMidiPckt
)
723 fNetMidiCaptureBuffer
->RenderToJackPorts();
727 rx_bytes
= Recv ( rx_head
->fPacketSize
, 0 );
728 if ( !IsNextPacket() )
729 jack_error ( "Packet(s) missing..." );
730 fRxHeader
.fCycle
= rx_head
->fCycle
;
731 fRxHeader
.fSubCycle
= rx_head
->fSubCycle
;
732 fRxHeader
.fIsLastPckt
= rx_head
->fIsLastPckt
;
733 fNetAudioCaptureBuffer
->RenderToJackPorts ( rx_head
->fSubCycle
);
737 jack_info ( "NetSlave : overloaded, skipping receive." );
742 fRxHeader
.fCycle
= rx_head
->fCycle
;
746 int JackNetSlaveInterface::SyncSend()
749 if ( fParams
.fSlaveSyncMode
)
750 fTxHeader
.fCycle
= fRxHeader
.fCycle
;
753 fTxHeader
.fSubCycle
= 0;
754 fTxHeader
.fDataType
= 's';
755 fTxHeader
.fIsLastPckt
= ( !fParams
.fReturnMidiChannels
&& !fParams
.fReturnAudioChannels
) ? 1 : 0;
756 fTxHeader
.fPacketSize
= fParams
.fMtu
;
757 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
758 return Send ( fTxHeader
.fPacketSize
, 0 );
761 int JackNetSlaveInterface::DataSend()
766 if ( fParams
.fReturnMidiChannels
)
768 fTxHeader
.fDataType
= 'm';
769 fTxHeader
.fMidiDataSize
= fNetMidiPlaybackBuffer
->RenderFromJackPorts();
770 fTxHeader
.fNMidiPckt
= GetNMidiPckt();
771 for ( subproc
= 0; subproc
< fTxHeader
.fNMidiPckt
; subproc
++ )
773 fTxHeader
.fSubCycle
= subproc
;
774 fTxHeader
.fIsLastPckt
= ( ( subproc
== ( fTxHeader
.fNMidiPckt
- 1 ) ) && !fParams
.fReturnAudioChannels
) ? 1 : 0;
775 fTxHeader
.fPacketSize
= sizeof ( packet_header_t
);
776 fTxHeader
.fPacketSize
+= fNetMidiPlaybackBuffer
->RenderToNetwork ( subproc
, fTxHeader
.fMidiDataSize
);
777 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
778 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)
784 if ( fParams
.fReturnAudioChannels
)
786 fTxHeader
.fDataType
= 'a';
787 for ( subproc
= 0; subproc
< fNSubProcess
; subproc
++ )
789 fTxHeader
.fSubCycle
= subproc
;
790 fTxHeader
.fIsLastPckt
= ( subproc
== ( fNSubProcess
- 1 ) ) ? 1 : 0;
791 fTxHeader
.fPacketSize
= fAudioTxLen
;
792 memcpy ( fTxBuffer
, &fTxHeader
, sizeof ( packet_header_t
) );
793 fNetAudioPlaybackBuffer
->RenderFromJackPorts ( subproc
);
794 if ( Send ( fTxHeader
.fPacketSize
, 0 ) == SOCKET_ERROR
)