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.

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