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.

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