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.

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