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.

765 lines
30KB

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