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.

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