jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

807 lines
31KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2008 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.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackNetInterface.h"
  17. #include "JackException.h"
  18. #include "JackPlatformPlug.h"
  19. #include <assert.h>
  20. using namespace std;
  21. /*
  22. TODO : since midi buffers now uses up to BUFFER_SIZE_MAX frames,
  23. probably also use BUFFER_SIZE_MAX in everything related to MIDI events
  24. handling (see MidiBufferInit in JackMidiPort.cpp)
  25. */
  26. namespace Jack
  27. {
  28. // JackNetInterface*******************************************
  29. JackNetInterface::JackNetInterface() : fSocket()
  30. {
  31. fMulticastIP = NULL;
  32. fTxBuffer = NULL;
  33. fRxBuffer = NULL;
  34. fNetAudioCaptureBuffer = NULL;
  35. fNetAudioPlaybackBuffer = NULL;
  36. fNetMidiCaptureBuffer = NULL;
  37. fNetMidiPlaybackBuffer = NULL;
  38. }
  39. JackNetInterface::JackNetInterface ( const char* multicast_ip, int port ) : fSocket ( multicast_ip, port )
  40. {
  41. fMulticastIP = strdup ( multicast_ip );
  42. fTxBuffer = NULL;
  43. fRxBuffer = NULL;
  44. fNetAudioCaptureBuffer = NULL;
  45. fNetAudioPlaybackBuffer = NULL;
  46. fNetMidiCaptureBuffer = NULL;
  47. fNetMidiPlaybackBuffer = NULL;
  48. }
  49. JackNetInterface::JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip ) : fSocket ( socket )
  50. {
  51. fParams = params;
  52. fMulticastIP = strdup ( multicast_ip );
  53. fTxBuffer = NULL;
  54. fRxBuffer = NULL;
  55. fNetAudioCaptureBuffer = NULL;
  56. fNetAudioPlaybackBuffer = NULL;
  57. fNetMidiCaptureBuffer = NULL;
  58. fNetMidiPlaybackBuffer = NULL;
  59. }
  60. JackNetInterface::~JackNetInterface()
  61. {
  62. jack_log ( "JackNetInterface::~JackNetInterface" );
  63. fSocket.Close();
  64. delete[] fTxBuffer;
  65. delete[] fRxBuffer;
  66. delete[] fMulticastIP;
  67. delete fNetAudioCaptureBuffer;
  68. delete fNetAudioPlaybackBuffer;
  69. delete fNetMidiCaptureBuffer;
  70. delete fNetMidiPlaybackBuffer;
  71. }
  72. jack_nframes_t JackNetInterface::SetFramesPerPacket()
  73. {
  74. jack_log ( "JackNetInterface::SetFramesPerPacket" );
  75. if ( !fParams.fSendAudioChannels && !fParams.fReturnAudioChannels )
  76. return ( fParams.fFramesPerPacket = fParams.fPeriodSize );
  77. jack_nframes_t period = ( int ) powf ( 2.f, ( int ) ( log ( ( fParams.fMtu - sizeof ( packet_header_t ) )
  78. / ( max ( fParams.fReturnAudioChannels, fParams.fSendAudioChannels ) * sizeof ( sample_t ) ) ) / log ( 2 ) ) );
  79. return ( fParams.fFramesPerPacket = ( period > fParams.fPeriodSize ) ? fParams.fPeriodSize : period );
  80. }
  81. int JackNetInterface::SetNetBufferSize()
  82. {
  83. jack_log ( "JackNetInterface::SetNetBufferSize" );
  84. float audio_size, midi_size;
  85. int bufsize, res = 0;
  86. //audio
  87. audio_size = fParams.fMtu * ( fParams.fPeriodSize / fParams.fFramesPerPacket );
  88. //midi
  89. midi_size = fParams.fMtu * ( max ( fParams.fSendMidiChannels, fParams.fReturnMidiChannels ) *
  90. fParams.fPeriodSize * sizeof ( sample_t ) / ( fParams.fMtu - sizeof ( packet_header_t ) ) );
  91. //bufsize = sync + audio + midi
  92. bufsize = 10 * (fParams.fMtu + ( int ) audio_size + ( int ) midi_size);
  93. //tx buffer
  94. if ( fSocket.SetOption ( SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof ( bufsize ) ) == SOCKET_ERROR )
  95. res = SOCKET_ERROR;
  96. //rx buffer
  97. if ( fSocket.SetOption ( SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof ( bufsize ) ) == SOCKET_ERROR )
  98. res = SOCKET_ERROR;
  99. return res;
  100. }
  101. int JackNetInterface::GetNMidiPckt()
  102. {
  103. //even if there is no midi data, jack need an empty buffer to know there is no event to read
  104. //99% of the cases : all data in one packet
  105. if ( fTxHeader.fMidiDataSize <= ( fParams.fMtu - sizeof ( packet_header_t ) ) )
  106. return 1;
  107. //else, get the number of needed packets (simply slice the biiig buffer)
  108. int npckt = fTxHeader.fMidiDataSize / ( fParams.fMtu - sizeof ( packet_header_t ) );
  109. if ( fTxHeader.fMidiDataSize % ( fParams.fMtu - sizeof ( packet_header_t ) ) )
  110. return ++npckt;
  111. return npckt;
  112. }
  113. bool JackNetInterface::IsNextPacket()
  114. {
  115. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  116. //ignore first cycle
  117. if ( fRxHeader.fCycle <= 1 )
  118. return true;
  119. //same PcktID (cycle), next SubPcktID (subcycle)
  120. if ( ( fRxHeader.fSubCycle < ( fNSubProcess - 1 ) ) && ( rx_head->fCycle == fRxHeader.fCycle ) && ( rx_head->fSubCycle == ( fRxHeader.fSubCycle + 1 ) ) )
  121. return true;
  122. //next PcktID (cycle), SubPcktID reset to 0 (first subcyle)
  123. if ( ( rx_head->fCycle == ( fRxHeader.fCycle + 1 ) ) && ( fRxHeader.fSubCycle == ( fNSubProcess - 1 ) ) && ( rx_head->fSubCycle == 0 ) )
  124. return true;
  125. //else, packet(s) missing, return false
  126. return false;
  127. }
  128. void JackNetInterface::SetParams()
  129. {
  130. //number of audio subcycles (packets)
  131. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  132. //payload size
  133. fPayloadSize = fParams.fMtu - sizeof ( packet_header_t );
  134. //TX header init
  135. strcpy ( fTxHeader.fPacketType, "header" );
  136. fTxHeader.fID = fParams.fID;
  137. fTxHeader.fCycle = 0;
  138. fTxHeader.fSubCycle = 0;
  139. fTxHeader.fMidiDataSize = 0;
  140. fTxHeader.fBitdepth = fParams.fBitdepth;
  141. fTxHeader.fIsLastPckt = 0;
  142. //RX header init
  143. strcpy ( fRxHeader.fPacketType, "header" );
  144. fRxHeader.fID = fParams.fID;
  145. fRxHeader.fCycle = 0;
  146. fRxHeader.fSubCycle = 0;
  147. fRxHeader.fMidiDataSize = 0;
  148. fRxHeader.fBitdepth = fParams.fBitdepth;
  149. fRxHeader.fIsLastPckt = 0;
  150. //network buffers
  151. fTxBuffer = new char[fParams.fMtu];
  152. fRxBuffer = new char[fParams.fMtu];
  153. assert ( fTxBuffer );
  154. assert ( fRxBuffer );
  155. //net audio/midi buffers'addresses
  156. fTxData = fTxBuffer + sizeof ( packet_header_t );
  157. fRxData = fRxBuffer + sizeof ( packet_header_t );
  158. }
  159. // JackNetMasterInterface ************************************************************************************
  160. bool JackNetMasterInterface::Init()
  161. {
  162. jack_log ( "JackNetMasterInterface::Init, ID %u.", fParams.fID );
  163. session_params_t params;
  164. uint attempt = 0;
  165. int rx_bytes = 0;
  166. //socket
  167. if ( fSocket.NewSocket() == SOCKET_ERROR )
  168. {
  169. jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE ) );
  170. return false;
  171. }
  172. //timeout on receive (for init)
  173. if ( fSocket.SetTimeOut ( MASTER_INIT_TIMEOUT ) < 0 )
  174. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  175. //connect
  176. if ( fSocket.Connect() == SOCKET_ERROR )
  177. {
  178. jack_error ( "Can't connect : %s", StrError ( NET_ERROR_CODE ) );
  179. return false;
  180. }
  181. //set the number of complete audio frames we can put in a packet
  182. SetFramesPerPacket();
  183. //send 'SLAVE_SETUP' until 'START_MASTER' received
  184. jack_info ( "Sending parameters to %s ...", fParams.fSlaveNetName );
  185. do
  186. {
  187. SetPacketType ( &fParams, SLAVE_SETUP );
  188. if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  189. jack_error ( "Error in send : ", StrError ( NET_ERROR_CODE ) );
  190. if ( ( ( rx_bytes = fSocket.Recv ( &params, sizeof ( session_params_t ), 0 ) ) == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  191. {
  192. jack_error ( "Problem with network." );
  193. return false;
  194. }
  195. }
  196. while ( ( GetPacketType ( &params ) != START_MASTER ) && ( ++attempt < SLAVE_SETUP_RETRY ) );
  197. if ( attempt == SLAVE_SETUP_RETRY )
  198. {
  199. jack_error ( "Slave doesn't respond, exiting." );
  200. return false;
  201. }
  202. //set the new timeout for the socket
  203. if ( SetRxTimeout() == SOCKET_ERROR )
  204. {
  205. jack_error ( "Can't set rx timeout : %s", StrError ( NET_ERROR_CODE ) );
  206. return false;
  207. }
  208. //set the new rx buffer size
  209. if ( SetNetBufferSize() == SOCKET_ERROR )
  210. {
  211. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  212. return false;
  213. }
  214. return true;
  215. }
  216. int JackNetMasterInterface::SetRxTimeout()
  217. {
  218. jack_log ( "JackNetMasterInterface::SetRxTimeout" );
  219. float time = 0;
  220. //slow or normal mode, short timeout on recv (2 audio subcycles)
  221. if ( ( fParams.fNetworkMode == 's' ) || ( fParams.fNetworkMode == 'n' ) )
  222. time = 2000000.f * ( static_cast<float> ( fParams.fFramesPerPacket ) / static_cast<float> ( fParams.fSampleRate ) );
  223. //fast mode, wait for 75% of the entire cycle duration
  224. else if ( fParams.fNetworkMode == 'f' )
  225. time = 750000.f * ( static_cast<float> ( fParams.fPeriodSize ) / static_cast<float> ( fParams.fSampleRate ) );
  226. return fSocket.SetTimeOut ( static_cast<int> ( time ) );
  227. }
  228. void JackNetMasterInterface::SetParams()
  229. {
  230. jack_log ( "JackNetMasterInterface::SetParams" );
  231. JackNetInterface::SetParams();
  232. fTxHeader.fDataStream = 's';
  233. fRxHeader.fDataStream = 'r';
  234. //midi net buffers
  235. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fTxData );
  236. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fRxData );
  237. assert ( fNetMidiCaptureBuffer );
  238. assert ( fNetMidiPlaybackBuffer );
  239. //audio net buffers
  240. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fTxData );
  241. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fRxData );
  242. assert ( fNetAudioCaptureBuffer );
  243. assert ( fNetAudioPlaybackBuffer );
  244. //audio netbuffer length
  245. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  246. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  247. }
  248. void JackNetMasterInterface::Exit()
  249. {
  250. jack_log ( "JackNetMasterInterface::Exit, ID %u", fParams.fID );
  251. //stop process
  252. fRunning = false;
  253. //send a 'multicast euthanasia request' - new socket is required on macosx
  254. jack_info ( "Exiting '%s'", fParams.fName );
  255. SetPacketType ( &fParams, KILL_MASTER );
  256. JackNetSocket mcast_socket ( fMulticastIP, fSocket.GetPort() );
  257. if ( mcast_socket.NewSocket() == SOCKET_ERROR )
  258. jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE ) );
  259. if ( mcast_socket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  260. jack_error ( "Can't send suicide request : %s", StrError ( NET_ERROR_CODE ) );
  261. mcast_socket.Close();
  262. // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
  263. ThreadExit();
  264. }
  265. int JackNetMasterInterface::Send ( size_t size, int flags )
  266. {
  267. int tx_bytes;
  268. if ( ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning )
  269. {
  270. net_error_t error = fSocket.GetError();
  271. if ( error == NET_CONN_ERROR )
  272. {
  273. //fatal connection issue, exit
  274. jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
  275. Exit();
  276. }
  277. else
  278. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  279. }
  280. return tx_bytes;
  281. }
  282. int JackNetMasterInterface::Recv ( size_t size, int flags )
  283. {
  284. int rx_bytes;
  285. if ( ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning )
  286. {
  287. net_error_t error = fSocket.GetError();
  288. //no data isn't really a network error, so just return 0 avalaible read bytes
  289. if ( error == NET_NO_DATA )
  290. return 0;
  291. else if ( error == NET_CONN_ERROR )
  292. {
  293. //fatal connection issue, exit
  294. jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
  295. //ask to the manager to properly remove the master
  296. Exit();
  297. }
  298. else
  299. jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) );
  300. }
  301. return rx_bytes;
  302. }
  303. int JackNetMasterInterface::SyncSend()
  304. {
  305. fTxHeader.fCycle++;
  306. fTxHeader.fSubCycle = 0;
  307. fTxHeader.fDataType = 's';
  308. fTxHeader.fIsLastPckt = ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels ) ? 1 : 0;
  309. fTxHeader.fPacketSize = fParams.fMtu;
  310. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  311. return Send ( fTxHeader.fPacketSize, 0 );
  312. }
  313. int JackNetMasterInterface::DataSend()
  314. {
  315. uint subproc;
  316. //midi
  317. if ( fParams.fSendMidiChannels )
  318. {
  319. //set global header fields and get the number of midi packets
  320. fTxHeader.fDataType = 'm';
  321. fTxHeader.fMidiDataSize = fNetMidiCaptureBuffer->RenderFromJackPorts();
  322. fTxHeader.fNMidiPckt = GetNMidiPckt();
  323. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  324. {
  325. fTxHeader.fSubCycle = subproc;
  326. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fSendAudioChannels ) ? 1 : 0;
  327. fTxHeader.fPacketSize = sizeof ( packet_header_t );
  328. fTxHeader.fPacketSize += fNetMidiCaptureBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  329. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  330. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  331. return SOCKET_ERROR;
  332. }
  333. }
  334. //audio
  335. if ( fParams.fSendAudioChannels )
  336. {
  337. fTxHeader.fDataType = 'a';
  338. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  339. {
  340. fTxHeader.fSubCycle = subproc;
  341. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  342. fTxHeader.fPacketSize = fAudioTxLen;
  343. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  344. fNetAudioCaptureBuffer->RenderFromJackPorts ( subproc );
  345. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  346. return SOCKET_ERROR;
  347. }
  348. }
  349. return 0;
  350. }
  351. int JackNetMasterInterface::SyncRecv()
  352. {
  353. int rx_bytes = 0;
  354. int cycle_offset = 0;
  355. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  356. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  357. if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) )
  358. return rx_bytes;
  359. cycle_offset = fTxHeader.fCycle - rx_head->fCycle;
  360. switch ( fParams.fNetworkMode )
  361. {
  362. case 's' :
  363. //slow mode : allow to use full bandwidth and heavy process on the slave
  364. // - extra latency is set to two cycles, one cycle for send/receive operations + one cycle for heavy process on the slave
  365. // - if the network is two fast, just wait the next cycle, this mode allows a shorter cycle duration for the master
  366. // - this mode will skip the two first cycles, thus it lets time for data to be processed and queued on the socket rx buffer
  367. //the slow mode is the safest mode because it wait twice the bandwidth relative time (send/return + process)
  368. if ( cycle_offset < 2 )
  369. return 0;
  370. else
  371. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  372. break;
  373. case 'n' :
  374. //normal use of the network :
  375. // - extra latency is set to one cycle, what is the time needed to receive streams using full network bandwidth
  376. // - if the network is too fast, just wait the next cycle, the benefit here is the master's cycle is shorter
  377. // - indeed, data is supposed to be on the network rx buffer, so we don't have to wait for it
  378. if ( cycle_offset < 1 )
  379. return 0;
  380. else
  381. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  382. break;
  383. case 'f' :
  384. //fast mode suppose the network bandwith is larger than required for the transmission (only a few channels for example)
  385. // - packets can be quickly received, quickly is here relative to the cycle duration
  386. // - here, receive data, we can't keep it queued on the rx buffer,
  387. // - but if there is a cycle offset, tell the user, that means we're not in fast mode anymore, network is too slow
  388. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  389. if ( cycle_offset )
  390. jack_error ( "'%s' can't run in fast network mode, data received too late (%d cycle(s) offset)", fParams.fName, cycle_offset );
  391. break;
  392. }
  393. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  394. return rx_bytes;
  395. }
  396. int JackNetMasterInterface::DataRecv()
  397. {
  398. int rx_bytes = 0;
  399. uint jumpcnt = 0;
  400. uint midi_recvd_pckt = 0;
  401. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  402. while ( !fRxHeader.fIsLastPckt )
  403. {
  404. //how much data is queued on the rx buffer ?
  405. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  406. if ( rx_bytes == SOCKET_ERROR )
  407. return rx_bytes;
  408. //if no data
  409. if ( ( rx_bytes == 0 ) && ( ++jumpcnt == fNSubProcess ) )
  410. {
  411. jack_error ( "No data from %s...", fParams.fName );
  412. jumpcnt = 0;
  413. }
  414. //else if data is valid,
  415. if ( rx_bytes && ( rx_head->fDataStream == 'r' ) && ( rx_head->fID == fParams.fID ) )
  416. {
  417. //read data
  418. switch ( rx_head->fDataType )
  419. {
  420. case 'm': //midi
  421. Recv ( rx_head->fPacketSize, 0 );
  422. fRxHeader.fCycle = rx_head->fCycle;
  423. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  424. fNetMidiPlaybackBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  425. if ( ++midi_recvd_pckt == rx_head->fNMidiPckt )
  426. fNetMidiPlaybackBuffer->RenderToJackPorts();
  427. jumpcnt = 0;
  428. break;
  429. case 'a': //audio
  430. Recv ( rx_head->fPacketSize, 0 );
  431. if ( !IsNextPacket() )
  432. jack_error ( "Packet(s) missing from '%s'...", fParams.fName );
  433. fRxHeader.fCycle = rx_head->fCycle;
  434. fRxHeader.fSubCycle = rx_head->fSubCycle;
  435. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  436. fNetAudioPlaybackBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  437. jumpcnt = 0;
  438. break;
  439. case 's': //sync
  440. if ( rx_head->fCycle == fTxHeader.fCycle )
  441. return 0;
  442. break;
  443. }
  444. }
  445. }
  446. return rx_bytes;
  447. }
  448. // JackNetSlaveInterface ************************************************************************************************
  449. uint JackNetSlaveInterface::fSlaveCounter = 0;
  450. bool JackNetSlaveInterface::Init()
  451. {
  452. jack_log ( "JackNetSlaveInterface::Init()" );
  453. //set the parameters to send
  454. strcpy ( fParams.fPacketType, "params" );
  455. fParams.fProtocolVersion = 'a';
  456. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  457. //init loop : get a master and start, do it until connection is ok
  458. net_status_t status;
  459. do
  460. {
  461. //first, get a master, do it until a valid connection is running
  462. do
  463. {
  464. status = GetNetMaster();
  465. if ( status == NET_SOCKET_ERROR )
  466. return false;
  467. }
  468. while ( status != NET_CONNECTED );
  469. //then tell the master we are ready
  470. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  471. status = SendStartToMaster();
  472. if ( status == NET_ERROR )
  473. return false;
  474. }
  475. while ( status != NET_ROLLING );
  476. return true;
  477. }
  478. net_status_t JackNetSlaveInterface::GetNetMaster()
  479. {
  480. jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
  481. //utility
  482. session_params_t params;
  483. int rx_bytes = 0;
  484. //socket
  485. if ( fSocket.NewSocket() == SOCKET_ERROR )
  486. {
  487. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  488. return NET_SOCKET_ERROR;
  489. }
  490. //bind the socket
  491. if ( fSocket.Bind() == SOCKET_ERROR )
  492. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  493. //timeout on receive
  494. if ( fSocket.SetTimeOut ( SLAVE_INIT_TIMEOUT ) == SOCKET_ERROR )
  495. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  496. //disable local loop
  497. if ( fSocket.SetLocalLoop() == SOCKET_ERROR )
  498. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  499. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  500. jack_info ( "Waiting for a master..." );
  501. do
  502. {
  503. //send 'available'
  504. if ( fSocket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  505. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  506. //filter incoming packets : don't exit while no error is detected
  507. rx_bytes = fSocket.CatchHost ( &params, sizeof ( session_params_t ), 0 );
  508. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  509. {
  510. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  511. return NET_RECV_ERROR;
  512. }
  513. }
  514. while ( strcmp ( params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &params ) != SLAVE_SETUP ) );
  515. //everything is OK, copy parameters
  516. fParams = params;
  517. //set the new buffer sizes
  518. if ( SetNetBufferSize() == SOCKET_ERROR )
  519. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  520. //connect the socket
  521. if ( fSocket.Connect() == SOCKET_ERROR )
  522. {
  523. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  524. return NET_CONNECT_ERROR;
  525. }
  526. return NET_CONNECTED;
  527. }
  528. net_status_t JackNetSlaveInterface::SendStartToMaster()
  529. {
  530. jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
  531. //tell the master to start
  532. SetPacketType ( &fParams, START_MASTER );
  533. if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  534. {
  535. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  536. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  537. }
  538. return NET_ROLLING;
  539. }
  540. void JackNetSlaveInterface::SetParams()
  541. {
  542. jack_log ( "JackNetSlaveInterface::SetParams" );
  543. JackNetInterface::SetParams();
  544. fTxHeader.fDataStream = 'r';
  545. fRxHeader.fDataStream = 's';
  546. //midi net buffers
  547. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  548. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  549. //audio net buffers
  550. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  551. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  552. //audio netbuffer length
  553. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  554. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  555. }
  556. int JackNetSlaveInterface::Recv ( size_t size, int flags )
  557. {
  558. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  559. //handle errors
  560. if ( rx_bytes == SOCKET_ERROR )
  561. {
  562. net_error_t error = fSocket.GetError();
  563. //no data isn't really an error in realtime processing, so just return 0
  564. if ( error == NET_NO_DATA )
  565. jack_error ( "No data, is the master still running ?" );
  566. //if a network error occurs, this exception will restart the driver
  567. else if ( error == NET_CONN_ERROR )
  568. {
  569. jack_error ( "Connection lost." );
  570. throw JackNetException();
  571. }
  572. else
  573. jack_error ( "Fatal error in receive : %s", StrError ( NET_ERROR_CODE ) );
  574. }
  575. return rx_bytes;
  576. }
  577. int JackNetSlaveInterface::Send ( size_t size, int flags )
  578. {
  579. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  580. //handle errors
  581. if ( tx_bytes == SOCKET_ERROR )
  582. {
  583. net_error_t error = fSocket.GetError();
  584. //if a network error occurs, this exception will restart the driver
  585. if ( error == NET_CONN_ERROR )
  586. {
  587. jack_error ( "Connection lost." );
  588. throw JackNetException();
  589. }
  590. else
  591. jack_error ( "Fatal error in send : %s", StrError ( NET_ERROR_CODE ) );
  592. }
  593. return tx_bytes;
  594. }
  595. int JackNetSlaveInterface::SyncRecv()
  596. {
  597. int rx_bytes = 0;
  598. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  599. //receive sync (launch the cycle)
  600. do
  601. {
  602. rx_bytes = Recv ( fParams.fMtu, 0 );
  603. //connection issue, send will detect it, so don't skip the cycle (return 0)
  604. if ( rx_bytes == SOCKET_ERROR )
  605. return rx_bytes;
  606. }
  607. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  608. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  609. return rx_bytes;
  610. }
  611. int JackNetSlaveInterface::DataRecv()
  612. {
  613. uint recvd_midi_pckt = 0;
  614. int rx_bytes = 0;
  615. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  616. while ( !fRxHeader.fIsLastPckt )
  617. {
  618. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  619. //error here, problem with recv, just skip the cycle (return -1)
  620. if ( rx_bytes == SOCKET_ERROR )
  621. return rx_bytes;
  622. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  623. {
  624. switch ( rx_head->fDataType )
  625. {
  626. case 'm': //midi
  627. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  628. fRxHeader.fCycle = rx_head->fCycle;
  629. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  630. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  631. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  632. fNetMidiCaptureBuffer->RenderToJackPorts();
  633. break;
  634. case 'a': //audio
  635. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  636. if ( !IsNextPacket() )
  637. jack_error ( "Packet(s) missing..." );
  638. fRxHeader.fCycle = rx_head->fCycle;
  639. fRxHeader.fSubCycle = rx_head->fSubCycle;
  640. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  641. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  642. break;
  643. case 's': //sync
  644. jack_info ( "NetSlave : overloaded, skipping receive." );
  645. return 0;
  646. }
  647. }
  648. }
  649. fRxHeader.fCycle = rx_head->fCycle;
  650. return 0;
  651. }
  652. int JackNetSlaveInterface::SyncSend()
  653. {
  654. //tx header
  655. if ( fParams.fSlaveSyncMode )
  656. fTxHeader.fCycle = fRxHeader.fCycle;
  657. else
  658. fTxHeader.fCycle++;
  659. fTxHeader.fSubCycle = 0;
  660. fTxHeader.fDataType = 's';
  661. fTxHeader.fIsLastPckt = ( !fParams.fReturnMidiChannels && !fParams.fReturnAudioChannels ) ? 1 : 0;
  662. fTxHeader.fPacketSize = fParams.fMtu;
  663. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  664. return Send ( fTxHeader.fPacketSize, 0 );
  665. }
  666. int JackNetSlaveInterface::DataSend()
  667. {
  668. uint subproc;
  669. //midi
  670. if ( fParams.fReturnMidiChannels )
  671. {
  672. fTxHeader.fDataType = 'm';
  673. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  674. fTxHeader.fNMidiPckt = GetNMidiPckt();
  675. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  676. {
  677. fTxHeader.fSubCycle = subproc;
  678. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels ) ? 1 : 0;
  679. fTxHeader.fPacketSize = sizeof ( packet_header_t );
  680. fTxHeader.fPacketSize += fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  681. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  682. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  683. return SOCKET_ERROR;
  684. }
  685. }
  686. //audio
  687. if ( fParams.fReturnAudioChannels )
  688. {
  689. fTxHeader.fDataType = 'a';
  690. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  691. {
  692. fTxHeader.fSubCycle = subproc;
  693. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  694. fTxHeader.fPacketSize = fAudioTxLen;
  695. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  696. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  697. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  698. return SOCKET_ERROR;
  699. }
  700. }
  701. return 0;
  702. }
  703. }