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.

784 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. float time = 0;
  212. //slow mode, very short timeout on recv
  213. if ( fParams.fNetworkMode == 's' )
  214. time = 2000000.f * ( static_cast<float> ( fParams.fFramesPerPacket ) / static_cast<float> ( fParams.fSampleRate ) );
  215. //normal mode, short timeout on recv
  216. else if ( fParams.fNetworkMode == 'n' )
  217. time = 4000000.f * ( static_cast<float> ( fParams.fFramesPerPacket ) / static_cast<float> ( fParams.fSampleRate ) );
  218. //fast mode, wait for the entire cycle duration
  219. else if ( fParams.fNetworkMode == 'f' )
  220. time = 750000.f * ( static_cast<float> ( fParams.fPeriodSize ) / static_cast<float> ( fParams.fSampleRate ) );
  221. return fSocket.SetTimeOut ( static_cast<int> ( time ) );
  222. }
  223. void JackNetMasterInterface::SetParams()
  224. {
  225. jack_log ( "JackNetMasterInterface::SetParams" );
  226. JackNetInterface::SetParams();
  227. fTxHeader.fDataStream = 's';
  228. fRxHeader.fDataStream = 'r';
  229. //midi net buffers
  230. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fTxData );
  231. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fRxData );
  232. //audio net buffers
  233. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fTxData );
  234. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fRxData );
  235. //audio netbuffer length
  236. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  237. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  238. }
  239. void JackNetMasterInterface::Exit()
  240. {
  241. jack_log ( "JackNetMasterInterface::Exit, ID %u", fParams.fID );
  242. //stop process
  243. fRunning = false;
  244. //send a 'multicast euthanasia request' - new socket is required on macosx
  245. jack_info ( "Exiting '%s'", fParams.fName );
  246. SetPacketType ( &fParams, KILL_MASTER );
  247. JackNetSocket mcast_socket ( fMulticastIP, fSocket.GetPort() );
  248. if ( mcast_socket.NewSocket() == SOCKET_ERROR )
  249. jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE ) );
  250. if ( mcast_socket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  251. jack_error ( "Can't send suicide request : %s", StrError ( NET_ERROR_CODE ) );
  252. mcast_socket.Close();
  253. }
  254. int JackNetMasterInterface::Send ( size_t size, int flags )
  255. {
  256. int tx_bytes;
  257. if ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR )
  258. {
  259. net_error_t error = fSocket.GetError();
  260. if ( fRunning && ( error == NET_CONN_ERROR ) )
  261. {
  262. //fatal connection issue, exit
  263. jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
  264. Exit();
  265. }
  266. else if ( fRunning )
  267. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  268. }
  269. return tx_bytes;
  270. }
  271. int JackNetMasterInterface::Recv ( size_t size, int flags )
  272. {
  273. int rx_bytes;
  274. if ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR )
  275. {
  276. net_error_t error = fSocket.GetError();
  277. //no data isn't really a network error, so just return 0 avalaible read bytes
  278. if ( error == NET_NO_DATA )
  279. return 0;
  280. else if ( fRunning && ( error == NET_CONN_ERROR ) )
  281. {
  282. //fatal connection issue, exit
  283. jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
  284. //ask to the manager to properly remove the master
  285. Exit();
  286. }
  287. else if ( fRunning )
  288. jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) );
  289. }
  290. return rx_bytes;
  291. }
  292. int JackNetMasterInterface::SyncSend()
  293. {
  294. fTxHeader.fCycle++;
  295. fTxHeader.fSubCycle = 0;
  296. fTxHeader.fDataType = 's';
  297. fTxHeader.fIsLastPckt = ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels ) ? 1 : 0;
  298. fTxHeader.fPacketSize = fParams.fMtu;
  299. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  300. return Send ( fTxHeader.fPacketSize, 0 );
  301. }
  302. int JackNetMasterInterface::DataSend()
  303. {
  304. uint subproc;
  305. //midi
  306. if ( fParams.fSendMidiChannels )
  307. {
  308. //set global header fields and get the number of midi packets
  309. fTxHeader.fDataType = 'm';
  310. fTxHeader.fMidiDataSize = fNetMidiCaptureBuffer->RenderFromJackPorts();
  311. fTxHeader.fNMidiPckt = GetNMidiPckt();
  312. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  313. {
  314. fTxHeader.fSubCycle = subproc;
  315. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fSendAudioChannels ) ? 1 : 0;
  316. fTxHeader.fPacketSize = sizeof ( packet_header_t );
  317. fTxHeader.fPacketSize += fNetMidiCaptureBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  318. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  319. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  320. return SOCKET_ERROR;
  321. }
  322. }
  323. //audio
  324. if ( fParams.fSendAudioChannels )
  325. {
  326. fTxHeader.fDataType = 'a';
  327. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  328. {
  329. fTxHeader.fSubCycle = subproc;
  330. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  331. fTxHeader.fPacketSize = fAudioTxLen;
  332. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  333. fNetAudioCaptureBuffer->RenderFromJackPorts ( subproc );
  334. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  335. return SOCKET_ERROR;
  336. }
  337. }
  338. return 0;
  339. }
  340. int JackNetMasterInterface::SyncRecv()
  341. {
  342. int rx_bytes = 0;
  343. int cycle_offset = 0;
  344. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  345. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  346. if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) )
  347. return rx_bytes;
  348. cycle_offset = fTxHeader.fCycle - rx_head->fCycle;
  349. switch ( fParams.fNetworkMode )
  350. {
  351. case 's' :
  352. //slow mode : allow to use full bandwidth and heavy process on the slave
  353. // - extra latency is set to two cycles, one cycle for send/receive operations + one cycle for heavy process on the slave
  354. // - if the network is two fast, just wait the next cycle, this mode allows a shorter cycle duration for the master
  355. // - this mode will skip the two first cycles, thus it lets time for data to be processed and queued on the socket rx buffer
  356. //the slow mode is the safest mode because it wait twice the bandwidth relative time (send/return + process)
  357. if ( cycle_offset < 2 )
  358. return 0;
  359. else
  360. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  361. break;
  362. case 'n' :
  363. //normal use of the network :
  364. // - extra latency is set to one cycle, what is the time needed to receive streams using full network bandwidth
  365. // - if the network is too fast, just wait the next cycle, the benefit here is the master's cycle is shorter
  366. // - indeed, data is supposed to be on the network rx buffer, so we don't have to wait for it
  367. if ( cycle_offset < 1 )
  368. return 0;
  369. else
  370. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  371. break;
  372. case 'f' :
  373. //fast mode suppose the network bandwith is larger than required for the transmission (only a few channels for example)
  374. // - packets can be quickly received, quickly is here relative to the cycle duration
  375. // - here, receive data, we can't keep it queued on the rx buffer,
  376. // - but if there is a cycle offset, tell the user, that means we're not in fast mode anymore, network is too slow
  377. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  378. if ( cycle_offset )
  379. jack_error ( "'%s' can't run in fast network mode, data received too late (%d cycle(s) offset)", fParams.fName, cycle_offset );
  380. break;
  381. }
  382. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  383. return rx_bytes;
  384. }
  385. int JackNetMasterInterface::DataRecv()
  386. {
  387. int rx_bytes = 0;
  388. uint jumpcnt = 0;
  389. uint midi_recvd_pckt = 0;
  390. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  391. while ( !fRxHeader.fIsLastPckt )
  392. {
  393. //how much data is queued on the rx buffer ?
  394. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  395. if ( rx_bytes == SOCKET_ERROR )
  396. return rx_bytes;
  397. //if no data
  398. if ( ( rx_bytes == 0 ) && ( ++jumpcnt == fNSubProcess ) )
  399. {
  400. jack_error ( "No data from %s...", fParams.fName );
  401. jumpcnt = 0;
  402. }
  403. //else if data is valid,
  404. if ( rx_bytes && ( rx_head->fDataStream == 'r' ) && ( rx_head->fID == fParams.fID ) )
  405. {
  406. //read data
  407. switch ( rx_head->fDataType )
  408. {
  409. case 'm': //midi
  410. Recv ( rx_head->fPacketSize, 0 );
  411. fRxHeader.fCycle = rx_head->fCycle;
  412. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  413. fNetMidiPlaybackBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  414. if ( ++midi_recvd_pckt == rx_head->fNMidiPckt )
  415. fNetMidiPlaybackBuffer->RenderToJackPorts();
  416. jumpcnt = 0;
  417. break;
  418. case 'a': //audio
  419. Recv ( rx_head->fPacketSize, 0 );
  420. if ( !IsNextPacket() )
  421. jack_error ( "Packet(s) missing from '%s'...", fParams.fName );
  422. fRxHeader.fCycle = rx_head->fCycle;
  423. fRxHeader.fSubCycle = rx_head->fSubCycle;
  424. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  425. fNetAudioPlaybackBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  426. jumpcnt = 0;
  427. break;
  428. case 's': //sync
  429. if ( rx_head->fCycle == fTxHeader.fCycle )
  430. return 0;
  431. }
  432. }
  433. }
  434. return rx_bytes;
  435. }
  436. // JackNetSlaveInterface ************************************************************************************************
  437. bool JackNetSlaveInterface::Init()
  438. {
  439. jack_log ( "JackNetSlaveInterface::Init()" );
  440. //set the parameters to send
  441. strcpy ( fParams.fPacketType, "params" );
  442. fParams.fProtocolVersion = 'a';
  443. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  444. //init loop : get a master and start, do it until connection is ok
  445. net_status_t status;
  446. do
  447. {
  448. //first, get a master, do it until a valid connection is running
  449. do
  450. {
  451. status = GetNetMaster();
  452. if ( status == NET_SOCKET_ERROR )
  453. return false;
  454. }
  455. while ( status != NET_CONNECTED );
  456. //then tell the master we are ready
  457. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  458. status = SendStartToMaster();
  459. if ( status == NET_ERROR )
  460. return false;
  461. }
  462. while ( status != NET_ROLLING );
  463. return true;
  464. }
  465. net_status_t JackNetSlaveInterface::GetNetMaster()
  466. {
  467. jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
  468. //utility
  469. session_params_t params;
  470. int rx_bytes = 0;
  471. unsigned char loop = 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 ( 2000000 ) == SOCKET_ERROR )
  483. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  484. //disable local loop
  485. if ( fSocket.SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof ( loop ) ) == 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. }