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.

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