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.

840 lines
33KB

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