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.

689 lines
27KB

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