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.

663 lines
25KB

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