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.

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