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.

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