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.

768 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. //send 'SLAVE_SETUP' until 'START_MASTER' received
  155. jack_info ( "Sending parameters to %s ...", fParams.fSlaveNetName );
  156. do
  157. {
  158. SetPacketType ( &fParams, SLAVE_SETUP );
  159. if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  160. jack_error ( "Error in send : ", StrError ( NET_ERROR_CODE ) );
  161. if ( ( ( rx_bytes = fSocket.Recv ( &params, sizeof ( session_params_t ), 0 ) ) == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  162. {
  163. jack_error ( "Problem with network." );
  164. return false;
  165. }
  166. }
  167. while ( ( GetPacketType ( &params ) != START_MASTER ) && ( ++attempt < 5 ) );
  168. if ( attempt == 5 )
  169. {
  170. jack_error ( "Slave doesn't respond, exiting." );
  171. return false;
  172. }
  173. //set the new timeout for the socket
  174. if ( SetRxTimeout() == SOCKET_ERROR )
  175. {
  176. jack_error ( "Can't set rx timeout : %s", StrError ( NET_ERROR_CODE ) );
  177. return false;
  178. }
  179. //set the new rx buffer size
  180. if ( SetNetBufferSize() == SOCKET_ERROR )
  181. {
  182. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  183. return false;
  184. }
  185. return true;
  186. }
  187. int JackNetMasterInterface::SetRxTimeout()
  188. {
  189. float time = 0;
  190. //slow mode, very short timeout on recv
  191. if ( fParams.fNetworkMode == 's' )
  192. time = 1000000.f * ( static_cast<float> ( fParams.fFramesPerPacket ) / static_cast<float> ( fParams.fSampleRate ) );
  193. //normal mode, short timeout on recv
  194. else if ( fParams.fNetworkMode == 'n' )
  195. time = 2000000.f * ( static_cast<float> ( fParams.fFramesPerPacket ) / static_cast<float> ( fParams.fSampleRate ) );
  196. //fast mode, wait for the entire cycle duration
  197. else if ( fParams.fNetworkMode == 'f' )
  198. time = 750000.f * ( static_cast<float> ( fParams.fPeriodSize ) / static_cast<float> ( fParams.fSampleRate ) );
  199. return fSocket.SetTimeOut ( static_cast<int> ( time ) );
  200. }
  201. void JackNetMasterInterface::SetParams()
  202. {
  203. jack_log ( "JackNetMasterInterface::SetParams" );
  204. SetFramesPerPacket();
  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. jack_info ( "Initializing Net Slave..." );
  429. do
  430. {
  431. status = GetNetMaster();
  432. if ( status == NET_SOCKET_ERROR )
  433. return false;
  434. }
  435. while ( status != NET_CONNECTED );
  436. //then tell the master we are ready
  437. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  438. status = SendStartToMaster();
  439. if ( status == NET_ERROR )
  440. return false;
  441. }
  442. while ( status != NET_ROLLING );
  443. return true;
  444. }
  445. net_status_t JackNetSlaveInterface::GetNetMaster()
  446. {
  447. jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
  448. //utility
  449. session_params_t params;
  450. int rx_bytes = 0;
  451. unsigned char loop = 0;
  452. //socket
  453. if ( fSocket.NewSocket() == SOCKET_ERROR )
  454. {
  455. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  456. return NET_SOCKET_ERROR;
  457. }
  458. //bind the socket
  459. if ( fSocket.Bind() == SOCKET_ERROR )
  460. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  461. //timeout on receive
  462. if ( fSocket.SetTimeOut ( 2000000 ) == SOCKET_ERROR )
  463. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  464. //disable local loop
  465. if ( fSocket.SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof ( loop ) ) == SOCKET_ERROR )
  466. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  467. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  468. jack_info ( "Waiting for a master..." );
  469. do
  470. {
  471. //send 'available'
  472. if ( fSocket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  473. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  474. //filter incoming packets : don't exit while no error is detected
  475. rx_bytes = fSocket.CatchHost ( &params, sizeof ( session_params_t ), 0 );
  476. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  477. {
  478. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  479. return NET_RECV_ERROR;
  480. }
  481. }
  482. while ( strcmp ( params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &params ) != SLAVE_SETUP ) );
  483. //everything is OK, copy parameters
  484. fParams = params;
  485. //set the new buffer sizes
  486. if ( SetNetBufferSize() == SOCKET_ERROR )
  487. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  488. //connect the socket
  489. if ( fSocket.Connect() == SOCKET_ERROR )
  490. {
  491. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  492. return NET_CONNECT_ERROR;
  493. }
  494. return NET_CONNECTED;
  495. }
  496. net_status_t JackNetSlaveInterface::SendStartToMaster()
  497. {
  498. jack_log ( "JackNetSlaveInterface::SendStartToMaster()" );
  499. //tell the master to start
  500. SetPacketType ( &fParams, START_MASTER );
  501. if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  502. {
  503. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  504. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  505. }
  506. return NET_ROLLING;
  507. }
  508. void JackNetSlaveInterface::SetParams()
  509. {
  510. jack_log ( "JackNetSlaveInterface::SetParams" );
  511. JackNetInterface::SetParams();
  512. fTxHeader.fDataStream = 'r';
  513. fRxHeader.fDataStream = 's';
  514. //midi net buffers
  515. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  516. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  517. //audio net buffers
  518. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  519. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  520. //audio netbuffer length
  521. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  522. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  523. }
  524. int JackNetSlaveInterface::Recv ( size_t size, int flags )
  525. {
  526. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  527. //handle errors
  528. if ( rx_bytes == SOCKET_ERROR )
  529. {
  530. net_error_t error = fSocket.GetError();
  531. //no data isn't really an error in realtime processing, so just return 0
  532. if ( error == NET_NO_DATA )
  533. jack_error ( "No data, is the master still running ?" );
  534. //if a network error occurs, this exception will restart the driver
  535. else if ( error == NET_CONN_ERROR )
  536. {
  537. jack_error ( "Connection lost." );
  538. throw JackDriverException();
  539. }
  540. else
  541. jack_error ( "Fatal error in receive : %s", StrError ( NET_ERROR_CODE ) );
  542. }
  543. return rx_bytes;
  544. }
  545. int JackNetSlaveInterface::Send ( size_t size, int flags )
  546. {
  547. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  548. //handle errors
  549. if ( tx_bytes == SOCKET_ERROR )
  550. {
  551. net_error_t error = fSocket.GetError();
  552. //if a network error occurs, this exception will restart the driver
  553. if ( error == NET_CONN_ERROR )
  554. {
  555. jack_error ( "Connection lost." );
  556. throw JackDriverException();
  557. }
  558. else
  559. jack_error ( "Fatal error in send : %s", StrError ( NET_ERROR_CODE ) );
  560. }
  561. return tx_bytes;
  562. }
  563. int JackNetSlaveInterface::SyncRecv()
  564. {
  565. int rx_bytes = 0;
  566. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  567. //receive sync (launch the cycle)
  568. do
  569. {
  570. rx_bytes = Recv ( fParams.fMtu, 0 );
  571. //connection issue, send will detect it, so don't skip the cycle (return 0)
  572. if ( rx_bytes == SOCKET_ERROR )
  573. return rx_bytes;
  574. }
  575. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  576. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  577. return rx_bytes;
  578. }
  579. int JackNetSlaveInterface::DataRecv()
  580. {
  581. uint recvd_midi_pckt = 0;
  582. int rx_bytes = 0;
  583. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  584. while ( !fRxHeader.fIsLastPckt );
  585. {
  586. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  587. //error here, problem with recv, just skip the cycle (return -1)
  588. if ( rx_bytes == SOCKET_ERROR )
  589. return rx_bytes;
  590. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  591. {
  592. switch ( rx_head->fDataType )
  593. {
  594. case 'm': //midi
  595. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  596. fRxHeader.fCycle = rx_head->fCycle;
  597. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  598. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  599. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  600. fNetMidiCaptureBuffer->RenderToJackPorts();
  601. break;
  602. case 'a': //audio
  603. jack_info ( "recv audio : %u - %u", rx_head->fCycle, rx_head->fSubCycle );
  604. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  605. if ( !IsNextPacket() )
  606. jack_error ( "Packet(s) missing..." );
  607. fRxHeader.fCycle = rx_head->fCycle;
  608. fRxHeader.fSubCycle = rx_head->fSubCycle;
  609. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  610. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  611. break;
  612. case 's': //sync
  613. jack_info ( "NetSlave : overloaded, skipping receive." );
  614. return 0;
  615. }
  616. }
  617. }
  618. fRxHeader.fCycle = rx_head->fCycle;
  619. return 0;
  620. }
  621. int JackNetSlaveInterface::SyncSend()
  622. {
  623. //tx header
  624. if ( fParams.fSlaveSyncMode )
  625. fTxHeader.fCycle = fRxHeader.fCycle;
  626. else
  627. fTxHeader.fCycle++;
  628. fTxHeader.fSubCycle = 0;
  629. fTxHeader.fDataType = 's';
  630. fTxHeader.fIsLastPckt = ( !fParams.fReturnMidiChannels && !fParams.fReturnAudioChannels ) ? 1 : 0;
  631. fTxHeader.fPacketSize = fParams.fMtu;
  632. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  633. return Send ( fTxHeader.fPacketSize, 0 );
  634. }
  635. int JackNetSlaveInterface::DataSend()
  636. {
  637. uint subproc;
  638. //midi
  639. if ( fParams.fReturnMidiChannels )
  640. {
  641. fTxHeader.fDataType = 'm';
  642. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  643. fTxHeader.fNMidiPckt = GetNMidiPckt();
  644. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  645. {
  646. fTxHeader.fSubCycle = subproc;
  647. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels ) ? 1 : 0;
  648. fTxHeader.fPacketSize = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  649. fTxHeader.fPacketSize += sizeof ( packet_header_t );
  650. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  651. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  652. return SOCKET_ERROR;
  653. }
  654. }
  655. //audio
  656. if ( fParams.fReturnAudioChannels )
  657. {
  658. fTxHeader.fDataType = 'a';
  659. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  660. {
  661. fTxHeader.fSubCycle = subproc;
  662. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  663. fTxHeader.fPacketSize = fAudioTxLen;
  664. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  665. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  666. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  667. return SOCKET_ERROR;
  668. }
  669. }
  670. return 0;
  671. }
  672. }