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.

761 lines
30KB

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