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.

952 lines
36KB

  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. #include "JackPlatformPlug.h"
  19. #include <assert.h>
  20. using namespace std;
  21. /*
  22. TODO : since midi buffers now uses up to BUFFER_SIZE_MAX frames,
  23. probably also use BUFFER_SIZE_MAX in everything related to MIDI events
  24. handling (see MidiBufferInit in JackMidiPort.cpp)
  25. */
  26. namespace Jack
  27. {
  28. // JackNetInterface*******************************************
  29. JackNetInterface::JackNetInterface() : fSocket()
  30. {
  31. fTxBuffer = NULL;
  32. fRxBuffer = NULL;
  33. fNetAudioCaptureBuffer = NULL;
  34. fNetAudioPlaybackBuffer = NULL;
  35. fNetMidiCaptureBuffer = NULL;
  36. fNetMidiPlaybackBuffer = NULL;
  37. }
  38. JackNetInterface::JackNetInterface ( const char* multicast_ip, int port ) : fSocket ( multicast_ip, port )
  39. {
  40. strcpy(fMulticastIP, multicast_ip);
  41. fTxBuffer = NULL;
  42. fRxBuffer = NULL;
  43. fNetAudioCaptureBuffer = NULL;
  44. fNetAudioPlaybackBuffer = NULL;
  45. fNetMidiCaptureBuffer = NULL;
  46. fNetMidiPlaybackBuffer = NULL;
  47. }
  48. JackNetInterface::JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip ) : fSocket ( socket )
  49. {
  50. fParams = params;
  51. strcpy(fMulticastIP, multicast_ip);
  52. fTxBuffer = NULL;
  53. fRxBuffer = NULL;
  54. fNetAudioCaptureBuffer = NULL;
  55. fNetAudioPlaybackBuffer = NULL;
  56. fNetMidiCaptureBuffer = NULL;
  57. fNetMidiPlaybackBuffer = NULL;
  58. }
  59. JackNetInterface::~JackNetInterface()
  60. {
  61. jack_log ( "JackNetInterface::~JackNetInterface" );
  62. fSocket.Close();
  63. delete[] fTxBuffer;
  64. delete[] fRxBuffer;
  65. delete fNetAudioCaptureBuffer;
  66. delete fNetAudioPlaybackBuffer;
  67. delete fNetMidiCaptureBuffer;
  68. delete fNetMidiPlaybackBuffer;
  69. }
  70. void JackNetInterface::SetFramesPerPacket()
  71. {
  72. jack_log ( "JackNetInterface::SetFramesPerPacket" );
  73. if (fParams.fSendAudioChannels == 0 && fParams.fReturnAudioChannels == 0) {
  74. fParams.fFramesPerPacket = fParams.fPeriodSize;
  75. } else {
  76. jack_nframes_t period = ( int ) powf ( 2.f, ( int ) ( log (float ( fParams.fMtu - sizeof ( packet_header_t ) )
  77. / ( max ( fParams.fReturnAudioChannels, fParams.fSendAudioChannels ) * sizeof ( sample_t ) ) ) / log ( 2. ) ) );
  78. fParams.fFramesPerPacket = ( period > fParams.fPeriodSize ) ? fParams.fPeriodSize : period;
  79. }
  80. }
  81. int JackNetInterface::SetNetBufferSize()
  82. {
  83. jack_log ( "JackNetInterface::SetNetBufferSize" );
  84. float audio_size, midi_size;
  85. int bufsize;
  86. //audio
  87. audio_size = fParams.fMtu * ( fParams.fPeriodSize / fParams.fFramesPerPacket );
  88. //midi
  89. midi_size = fParams.fMtu * ( max ( fParams.fSendMidiChannels, fParams.fReturnMidiChannels ) *
  90. fParams.fPeriodSize * sizeof ( sample_t ) / ( fParams.fMtu - sizeof ( packet_header_t ) ) );
  91. //bufsize = sync + audio + midi
  92. bufsize = MAX_LATENCY * (fParams.fMtu + ( int ) audio_size + ( int ) midi_size);
  93. //tx buffer
  94. if ( fSocket.SetOption ( SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof ( bufsize ) ) == SOCKET_ERROR )
  95. return SOCKET_ERROR;
  96. //rx buffer
  97. if ( fSocket.SetOption ( SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof ( bufsize ) ) == SOCKET_ERROR )
  98. return SOCKET_ERROR;
  99. return 0;
  100. }
  101. int JackNetInterface::GetNMidiPckt()
  102. {
  103. //even if there is no midi data, jack need an empty buffer to know there is no event to read
  104. //99% of the cases : all data in one packet
  105. if ( fTxHeader.fMidiDataSize <= ( fParams.fMtu - sizeof ( packet_header_t ) ) )
  106. return 1;
  107. //else, get the number of needed packets (simply slice the biiig buffer)
  108. int npckt = fTxHeader.fMidiDataSize / ( fParams.fMtu - sizeof ( packet_header_t ) );
  109. if ( fTxHeader.fMidiDataSize % ( fParams.fMtu - sizeof ( packet_header_t ) ) )
  110. return ++npckt;
  111. return npckt;
  112. }
  113. bool JackNetInterface::IsNextPacket()
  114. {
  115. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  116. //ignore first cycle
  117. if ( fRxHeader.fCycle <= 1 ) {
  118. return true;
  119. }
  120. //same PcktID (cycle), next SubPcktID (subcycle)
  121. if ( ( fRxHeader.fSubCycle < ( fNSubProcess - 1 ) ) && ( rx_head->fCycle == fRxHeader.fCycle ) && ( rx_head->fSubCycle == ( fRxHeader.fSubCycle + 1 ) ) ) {
  122. return true;
  123. }
  124. //next PcktID (cycle), SubPcktID reset to 0 (first subcyle)
  125. if ( ( rx_head->fCycle == ( fRxHeader.fCycle + 1 ) ) && ( fRxHeader.fSubCycle == ( fNSubProcess - 1 ) ) && ( rx_head->fSubCycle == 0 ) ) {
  126. return true;
  127. }
  128. //else, packet(s) missing, return false
  129. return false;
  130. }
  131. void JackNetInterface::SetParams()
  132. {
  133. //number of audio subcycles (packets)
  134. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  135. //payload size
  136. fPayloadSize = fParams.fMtu - sizeof ( packet_header_t );
  137. //TX header init
  138. strcpy ( fTxHeader.fPacketType, "header" );
  139. fTxHeader.fID = fParams.fID;
  140. fTxHeader.fCycle = 0;
  141. fTxHeader.fSubCycle = 0;
  142. fTxHeader.fMidiDataSize = 0;
  143. fTxHeader.fBitdepth = fParams.fBitdepth;
  144. fTxHeader.fIsLastPckt = 0;
  145. //RX header init
  146. strcpy ( fRxHeader.fPacketType, "header" );
  147. fRxHeader.fID = fParams.fID;
  148. fRxHeader.fCycle = 0;
  149. fRxHeader.fSubCycle = 0;
  150. fRxHeader.fMidiDataSize = 0;
  151. fRxHeader.fBitdepth = fParams.fBitdepth;
  152. fRxHeader.fIsLastPckt = 0;
  153. //network buffers
  154. fTxBuffer = new char[fParams.fMtu];
  155. fRxBuffer = new char[fParams.fMtu];
  156. assert ( fTxBuffer );
  157. assert ( fRxBuffer );
  158. //net audio/midi buffers'addresses
  159. fTxData = fTxBuffer + sizeof ( packet_header_t );
  160. fRxData = fRxBuffer + sizeof ( packet_header_t );
  161. }
  162. // JackNetMasterInterface ************************************************************************************
  163. bool JackNetMasterInterface::Init()
  164. {
  165. jack_log ( "JackNetMasterInterface::Init, ID %u.", fParams.fID );
  166. session_params_t host_params;
  167. uint attempt = 0;
  168. int rx_bytes = 0;
  169. //socket
  170. if ( fSocket.NewSocket() == SOCKET_ERROR )
  171. {
  172. jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE ) );
  173. return false;
  174. }
  175. //timeout on receive (for init)
  176. if ( fSocket.SetTimeOut ( MASTER_INIT_TIMEOUT ) < 0 )
  177. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  178. //connect
  179. if ( fSocket.Connect() == SOCKET_ERROR )
  180. {
  181. jack_error ( "Can't connect : %s", StrError ( NET_ERROR_CODE ) );
  182. return false;
  183. }
  184. //set the number of complete audio frames we can put in a packet
  185. SetFramesPerPacket();
  186. //send 'SLAVE_SETUP' until 'START_MASTER' received
  187. jack_info ( "Sending parameters to %s ...", fParams.fSlaveNetName );
  188. do
  189. {
  190. session_params_t net_params;
  191. SetPacketType ( &fParams, SLAVE_SETUP );
  192. SessionParamsHToN(&fParams, &net_params);
  193. if ( fSocket.Send ( &net_params, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  194. jack_error ( "Error in send : ", StrError ( NET_ERROR_CODE ) );
  195. memset(&net_params, 0, sizeof ( session_params_t ));
  196. if ( ( ( rx_bytes = fSocket.Recv ( &net_params, sizeof ( session_params_t ), 0 ) ) == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  197. {
  198. jack_error ( "Problem with network." );
  199. return false;
  200. }
  201. SessionParamsNToH(&net_params, &host_params);
  202. }
  203. while ( ( GetPacketType ( &host_params ) != START_MASTER ) && ( ++attempt < SLAVE_SETUP_RETRY ) );
  204. if ( attempt == SLAVE_SETUP_RETRY )
  205. {
  206. jack_error ( "Slave doesn't respond, exiting." );
  207. return false;
  208. }
  209. //set the new timeout for the socket
  210. if ( SetRxTimeout() == SOCKET_ERROR )
  211. {
  212. jack_error ( "Can't set rx timeout : %s", StrError ( NET_ERROR_CODE ) );
  213. return false;
  214. }
  215. //set the new rx buffer size
  216. if ( SetNetBufferSize() == SOCKET_ERROR )
  217. {
  218. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  219. return false;
  220. }
  221. return true;
  222. }
  223. int JackNetMasterInterface::SetRxTimeout()
  224. {
  225. jack_log ( "JackNetMasterInterface::SetRxTimeout" );
  226. float time = 0;
  227. //slow or normal mode, short timeout on recv (2 audio subcycles)
  228. if ( ( fParams.fNetworkMode == 's' ) || ( fParams.fNetworkMode == 'n' ) )
  229. time = 2000000.f * ( static_cast<float> ( fParams.fFramesPerPacket ) / static_cast<float> ( fParams.fSampleRate ) );
  230. //fast mode, wait for 75% of the entire cycle duration
  231. else if ( fParams.fNetworkMode == 'f' )
  232. time = 750000.f * ( static_cast<float> ( fParams.fPeriodSize ) / static_cast<float> ( fParams.fSampleRate ) );
  233. return fSocket.SetTimeOut ( static_cast<int> ( time ) );
  234. }
  235. void JackNetMasterInterface::SetParams()
  236. {
  237. jack_log ( "JackNetMasterInterface::SetParams" );
  238. JackNetInterface::SetParams();
  239. fTxHeader.fDataStream = 's';
  240. fRxHeader.fDataStream = 'r';
  241. //midi net buffers
  242. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fTxData );
  243. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fRxData );
  244. assert ( fNetMidiCaptureBuffer );
  245. assert ( fNetMidiPlaybackBuffer );
  246. //audio net buffers
  247. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fTxData );
  248. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fRxData );
  249. assert ( fNetAudioCaptureBuffer );
  250. assert ( fNetAudioPlaybackBuffer );
  251. //audio netbuffer length
  252. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  253. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  254. }
  255. void JackNetMasterInterface::Exit()
  256. {
  257. jack_log ( "JackNetMasterInterface::Exit, ID %u", fParams.fID );
  258. //stop process
  259. fRunning = false;
  260. //send a 'multicast euthanasia request' - new socket is required on macosx
  261. jack_info ( "Exiting '%s'", fParams.fName );
  262. SetPacketType ( &fParams, KILL_MASTER );
  263. JackNetSocket mcast_socket ( fMulticastIP, fSocket.GetPort() );
  264. session_params_t net_params;
  265. SessionParamsHToN(&fParams, &net_params);
  266. if ( mcast_socket.NewSocket() == SOCKET_ERROR )
  267. jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE ) );
  268. if ( mcast_socket.SendTo ( &net_params, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  269. jack_error ( "Can't send suicide request : %s", StrError ( NET_ERROR_CODE ) );
  270. mcast_socket.Close();
  271. // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
  272. ThreadExit();
  273. }
  274. int JackNetMasterInterface::Recv ( size_t size, int flags )
  275. {
  276. int rx_bytes;
  277. if ( ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning )
  278. {
  279. net_error_t error = fSocket.GetError();
  280. //no data isn't really a network error, so just return 0 avalaible read bytes
  281. if ( error == NET_NO_DATA )
  282. return 0;
  283. else if ( error == NET_CONN_ERROR )
  284. {
  285. //fatal connection issue, exit
  286. jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
  287. //ask to the manager to properly remove the master
  288. Exit();
  289. }
  290. else
  291. jack_error ( "Error in master receive : %s", StrError ( NET_ERROR_CODE ) );
  292. }
  293. packet_header_t* header = reinterpret_cast<packet_header_t*>(fRxBuffer);
  294. PacketHeaderNToH(header, header);
  295. return rx_bytes;
  296. }
  297. int JackNetMasterInterface::Send ( size_t size, int flags )
  298. {
  299. int tx_bytes;
  300. packet_header_t* header = reinterpret_cast<packet_header_t*>(fTxBuffer);
  301. PacketHeaderHToN(header, header);
  302. if ( ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning )
  303. {
  304. net_error_t error = fSocket.GetError();
  305. if ( error == NET_CONN_ERROR )
  306. {
  307. //fatal connection issue, exit
  308. jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
  309. Exit();
  310. }
  311. else
  312. jack_error ( "Error in master send : %s", StrError ( NET_ERROR_CODE ) );
  313. }
  314. return tx_bytes;
  315. }
  316. bool JackNetMasterInterface::IsSynched()
  317. {
  318. if (fParams.fNetworkMode == 's') {
  319. return (fCycleOffset < 3);
  320. } else {
  321. return true;
  322. }
  323. }
  324. int JackNetMasterInterface::SyncSend()
  325. {
  326. fTxHeader.fCycle++;
  327. fTxHeader.fSubCycle = 0;
  328. fTxHeader.fDataType = 's';
  329. fTxHeader.fIsLastPckt = ( fParams.fSendMidiChannels == 0 && fParams.fSendAudioChannels == 0) ? 1 : 0;
  330. fTxHeader.fPacketSize = fParams.fMtu;
  331. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  332. return Send ( fTxHeader.fPacketSize, 0 );
  333. }
  334. int JackNetMasterInterface::DataSend()
  335. {
  336. uint subproc;
  337. //midi
  338. if ( fParams.fSendMidiChannels > 0)
  339. {
  340. //set global header fields and get the number of midi packets
  341. fTxHeader.fDataType = 'm';
  342. fTxHeader.fMidiDataSize = fNetMidiCaptureBuffer->RenderFromJackPorts();
  343. fTxHeader.fNMidiPckt = GetNMidiPckt();
  344. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  345. {
  346. fTxHeader.fSubCycle = subproc;
  347. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && (fParams.fSendAudioChannels == 0)) ? 1 : 0;
  348. fTxHeader.fPacketSize = sizeof ( packet_header_t ) + fNetMidiCaptureBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  349. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  350. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  351. return SOCKET_ERROR;
  352. }
  353. }
  354. //audio
  355. if ( fParams.fSendAudioChannels > 0)
  356. {
  357. fTxHeader.fDataType = 'a';
  358. fTxHeader.fMidiDataSize = 0;
  359. fTxHeader.fNMidiPckt = 0;
  360. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  361. {
  362. fTxHeader.fSubCycle = subproc;
  363. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  364. fTxHeader.fPacketSize = fAudioTxLen;
  365. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  366. fNetAudioCaptureBuffer->RenderFromJackPorts ( subproc );
  367. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  368. return SOCKET_ERROR;
  369. }
  370. }
  371. return 0;
  372. }
  373. int JackNetMasterInterface::SyncRecv()
  374. {
  375. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  376. int rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  377. if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) )
  378. return rx_bytes;
  379. fCycleOffset = fTxHeader.fCycle - rx_head->fCycle;
  380. switch ( fParams.fNetworkMode )
  381. {
  382. case 's' :
  383. //slow mode : allow to use full bandwidth and heavy process on the slave
  384. // - extra latency is set to two cycles, one cycle for send/receive operations + one cycle for heavy process on the slave
  385. // - if the network is two fast, just wait the next cycle, this mode allows a shorter cycle duration for the master
  386. // - this mode will skip the two first cycles, thus it lets time for data to be processed and queued on the socket rx buffer
  387. //the slow mode is the safest mode because it wait twice the bandwidth relative time (send/return + process)
  388. if (fCycleOffset < 2)
  389. return 0;
  390. else
  391. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  392. if (fCycleOffset > 2) {
  393. jack_info("Warning : '%s' runs in slow network mode, but data received too late (%d cycle(s) offset)", fParams.fName, fCycleOffset);
  394. }
  395. break;
  396. case 'n' :
  397. //normal use of the network :
  398. // - extra latency is set to one cycle, what is the time needed to receive streams using full network bandwidth
  399. // - if the network is too fast, just wait the next cycle, the benefit here is the master's cycle is shorter
  400. // - indeed, data is supposed to be on the network rx buffer, so we don't have to wait for it
  401. if (fCycleOffset < 1)
  402. return 0;
  403. else
  404. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  405. if (fCycleOffset != 1)
  406. jack_info("'%s' can't run in normal network mode, data received too late (%d cycle(s) offset)", fParams.fName, fCycleOffset);
  407. break;
  408. case 'f' :
  409. //fast mode suppose the network bandwith is larger than required for the transmission (only a few channels for example)
  410. // - packets can be quickly received, quickly is here relative to the cycle duration
  411. // - here, receive data, we can't keep it queued on the rx buffer,
  412. // - but if there is a cycle offset, tell the user, that means we're not in fast mode anymore, network is too slow
  413. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  414. if (fCycleOffset != 0)
  415. jack_info("'%s' can't run in fast network mode, data received too late (%d cycle(s) offset)", fParams.fName, fCycleOffset);
  416. break;
  417. }
  418. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  419. return rx_bytes;
  420. }
  421. int JackNetMasterInterface::DataRecv()
  422. {
  423. int rx_bytes = 0;
  424. uint jumpcnt = 0;
  425. uint recvd_midi_pckt = 0;
  426. uint recvd_audio_pckt = 0;
  427. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  428. while ( !fRxHeader.fIsLastPckt )
  429. {
  430. //how much data is queued on the rx buffer ?
  431. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  432. if ( rx_bytes == SOCKET_ERROR )
  433. return rx_bytes;
  434. //if no data
  435. if ( ( rx_bytes == 0 ) && ( ++jumpcnt == fNSubProcess ) )
  436. {
  437. jack_error ( "No data from %s...", fParams.fName );
  438. jumpcnt = 0;
  439. }
  440. //else if data is valid,
  441. if ( rx_bytes && ( rx_head->fDataStream == 'r' ) && ( rx_head->fID == fParams.fID ) )
  442. {
  443. //read data
  444. switch ( rx_head->fDataType )
  445. {
  446. case 'm': //midi
  447. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  448. fRxHeader.fCycle = rx_head->fCycle;
  449. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  450. fNetMidiPlaybackBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  451. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  452. fNetMidiPlaybackBuffer->RenderToJackPorts();
  453. jumpcnt = 0;
  454. break;
  455. case 'a': //audio
  456. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  457. // SL: 25/01/09
  458. // if ( !IsNextPacket() )
  459. // jack_error ( "Packet(s) missing from '%s'...", fParams.fName );
  460. if (recvd_audio_pckt++ != rx_head->fSubCycle) {
  461. jack_error("Packet(s) missing from '%s'...", fParams.fSlaveNetName);
  462. }
  463. fRxHeader.fCycle = rx_head->fCycle;
  464. fRxHeader.fSubCycle = rx_head->fSubCycle;
  465. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  466. fNetAudioPlaybackBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  467. jumpcnt = 0;
  468. break;
  469. case 's': //sync
  470. /* SL: 25/01/09
  471. if ( rx_head->fCycle == fTxHeader.fCycle )
  472. return 0;
  473. */
  474. jack_info("NetMaster : overloaded, skipping receive from '%s'", fParams.fName);
  475. return 0;
  476. }
  477. }
  478. }
  479. return rx_bytes;
  480. }
  481. // JackNetSlaveInterface ************************************************************************************************
  482. uint JackNetSlaveInterface::fSlaveCounter = 0;
  483. bool JackNetSlaveInterface::Init()
  484. {
  485. jack_log ( "JackNetSlaveInterface::Init()" );
  486. //set the parameters to send
  487. strcpy ( fParams.fPacketType, "params" );
  488. fParams.fProtocolVersion = SLAVE_PROTOCOL;
  489. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  490. //init loop : get a master and start, do it until connection is ok
  491. net_status_t status;
  492. do
  493. {
  494. //first, get a master, do it until a valid connection is running
  495. do
  496. {
  497. status = GetNetMaster();
  498. if ( status == NET_SOCKET_ERROR )
  499. return false;
  500. }
  501. while ( status != NET_CONNECTED );
  502. //then tell the master we are ready
  503. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  504. status = SendStartToMaster();
  505. if ( status == NET_ERROR )
  506. return false;
  507. }
  508. while ( status != NET_ROLLING );
  509. return true;
  510. }
  511. // Separate the connection protocol into two separated step
  512. bool JackNetSlaveInterface::InitConnection()
  513. {
  514. jack_log ( "JackNetSlaveInterface::InitConnection()" );
  515. //set the parameters to send
  516. strcpy (fParams.fPacketType, "params");
  517. fParams.fProtocolVersion = SLAVE_PROTOCOL;
  518. SetPacketType (&fParams, SLAVE_AVAILABLE);
  519. net_status_t status;
  520. do
  521. {
  522. //get a master
  523. status = GetNetMaster();
  524. if (status == NET_SOCKET_ERROR)
  525. return false;
  526. }
  527. while (status != NET_CONNECTED);
  528. return true;
  529. }
  530. bool JackNetSlaveInterface::InitRendering()
  531. {
  532. jack_log("JackNetSlaveInterface::InitRendering()");
  533. net_status_t status;
  534. do
  535. {
  536. //then tell the master we are ready
  537. jack_info("Initializing connection with %s...", fParams.fMasterNetName);
  538. status = SendStartToMaster();
  539. if (status == NET_ERROR)
  540. return false;
  541. }
  542. while (status != NET_ROLLING);
  543. return true;
  544. }
  545. net_status_t JackNetSlaveInterface::GetNetMaster()
  546. {
  547. jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
  548. //utility
  549. session_params_t host_params;
  550. int rx_bytes = 0;
  551. //socket
  552. if ( fSocket.NewSocket() == SOCKET_ERROR )
  553. {
  554. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  555. return NET_SOCKET_ERROR;
  556. }
  557. //bind the socket
  558. if ( fSocket.Bind() == SOCKET_ERROR )
  559. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  560. //timeout on receive
  561. if ( fSocket.SetTimeOut ( SLAVE_INIT_TIMEOUT ) == SOCKET_ERROR )
  562. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  563. //disable local loop
  564. if ( fSocket.SetLocalLoop() == SOCKET_ERROR )
  565. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  566. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  567. jack_info ( "Waiting for a master..." );
  568. do
  569. {
  570. //send 'available'
  571. session_params_t net_params;
  572. SessionParamsHToN(&fParams, &net_params);
  573. if ( fSocket.SendTo ( &net_params, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  574. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  575. //filter incoming packets : don't exit while no error is detected
  576. memset(&net_params, 0, sizeof ( session_params_t ));
  577. rx_bytes = fSocket.CatchHost ( &net_params, sizeof ( session_params_t ), 0 );
  578. SessionParamsNToH(&net_params, &host_params);
  579. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  580. {
  581. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  582. return NET_RECV_ERROR;
  583. }
  584. }
  585. while ( strcmp ( host_params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &host_params ) != SLAVE_SETUP ) );
  586. //everything is OK, copy parameters
  587. fParams = host_params;
  588. //set the new buffer sizes
  589. if ( SetNetBufferSize() == SOCKET_ERROR )
  590. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  591. //connect the socket
  592. if ( fSocket.Connect() == SOCKET_ERROR )
  593. {
  594. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  595. return NET_CONNECT_ERROR;
  596. }
  597. return NET_CONNECTED;
  598. }
  599. net_status_t JackNetSlaveInterface::SendStartToMaster()
  600. {
  601. jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
  602. //tell the master to start
  603. session_params_t net_params;
  604. SetPacketType ( &fParams, START_MASTER );
  605. SessionParamsHToN(&fParams, &net_params);
  606. if ( fSocket.Send ( &net_params, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  607. {
  608. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  609. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  610. }
  611. return NET_ROLLING;
  612. }
  613. void JackNetSlaveInterface::SetParams()
  614. {
  615. jack_log ( "JackNetSlaveInterface::SetParams" );
  616. JackNetInterface::SetParams();
  617. fTxHeader.fDataStream = 'r';
  618. fRxHeader.fDataStream = 's';
  619. //midi net buffers
  620. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  621. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  622. //audio net buffers
  623. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  624. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  625. //audio netbuffer length
  626. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  627. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  628. }
  629. int JackNetSlaveInterface::Recv ( size_t size, int flags )
  630. {
  631. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  632. //handle errors
  633. if ( rx_bytes == SOCKET_ERROR )
  634. {
  635. net_error_t error = fSocket.GetError();
  636. //no data isn't really an error in realtime processing, so just return 0
  637. if ( error == NET_NO_DATA )
  638. jack_error ( "No data, is the master still running ?" );
  639. //if a network error occurs, this exception will restart the driver
  640. else if ( error == NET_CONN_ERROR )
  641. {
  642. jack_error ( "Connection lost." );
  643. throw JackNetException();
  644. }
  645. else
  646. jack_error ( "Fatal error in slave receive : %s", StrError ( NET_ERROR_CODE ) );
  647. }
  648. packet_header_t* header = reinterpret_cast<packet_header_t*>(fRxBuffer);
  649. PacketHeaderNToH(header, header);
  650. return rx_bytes;
  651. }
  652. int JackNetSlaveInterface::Send ( size_t size, int flags )
  653. {
  654. packet_header_t* header = reinterpret_cast<packet_header_t*>(fTxBuffer);
  655. PacketHeaderHToN(header, header);
  656. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  657. //handle errors
  658. if ( tx_bytes == SOCKET_ERROR )
  659. {
  660. net_error_t error = fSocket.GetError();
  661. //if a network error occurs, this exception will restart the driver
  662. if ( error == NET_CONN_ERROR )
  663. {
  664. jack_error ( "Connection lost." );
  665. throw JackNetException();
  666. }
  667. else
  668. jack_error ( "Fatal error in slave send : %s", StrError ( NET_ERROR_CODE ) );
  669. }
  670. return tx_bytes;
  671. }
  672. int JackNetSlaveInterface::SyncRecv()
  673. {
  674. int rx_bytes = 0;
  675. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  676. //receive sync (launch the cycle)
  677. do
  678. {
  679. rx_bytes = Recv ( fParams.fMtu, 0 );
  680. //connection issue, send will detect it, so don't skip the cycle (return 0)
  681. if ( rx_bytes == SOCKET_ERROR )
  682. return rx_bytes;
  683. }
  684. while ((strcmp(rx_head->fPacketType, "header") != 0) && (rx_head->fDataType != 's'));
  685. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  686. return rx_bytes;
  687. }
  688. int JackNetSlaveInterface::DataRecv()
  689. {
  690. uint recvd_midi_pckt = 0;
  691. uint recvd_audio_pckt = 0;
  692. int rx_bytes = 0;
  693. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  694. while ( !fRxHeader.fIsLastPckt )
  695. {
  696. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  697. //error here, problem with recv, just skip the cycle (return -1)
  698. if ( rx_bytes == SOCKET_ERROR )
  699. return rx_bytes;
  700. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  701. {
  702. switch ( rx_head->fDataType )
  703. {
  704. case 'm': //midi
  705. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  706. fRxHeader.fCycle = rx_head->fCycle;
  707. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  708. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  709. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  710. fNetMidiCaptureBuffer->RenderToJackPorts();
  711. break;
  712. case 'a': //audio
  713. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  714. //SL: 25/01/09
  715. // if ( !IsNextPacket() )
  716. // jack_error ( "Packet(s) missing..." );
  717. if (recvd_audio_pckt++ != rx_head->fSubCycle) {
  718. jack_error("Packet(s) missing from '%s'...", fParams.fMasterNetName);
  719. }
  720. fRxHeader.fCycle = rx_head->fCycle;
  721. fRxHeader.fSubCycle = rx_head->fSubCycle;
  722. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  723. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  724. break;
  725. case 's': //sync
  726. jack_info ( "NetSlave : overloaded, skipping receive." );
  727. return 0;
  728. }
  729. }
  730. }
  731. fRxHeader.fCycle = rx_head->fCycle;
  732. return 0;
  733. }
  734. int JackNetSlaveInterface::SyncSend()
  735. {
  736. //tx header
  737. if ( fParams.fSlaveSyncMode )
  738. fTxHeader.fCycle = fRxHeader.fCycle;
  739. else
  740. fTxHeader.fCycle++;
  741. fTxHeader.fSubCycle = 0;
  742. fTxHeader.fDataType = 's';
  743. fTxHeader.fIsLastPckt = ( fParams.fReturnMidiChannels == 0 && fParams.fReturnAudioChannels == 0) ? 1 : 0;
  744. fTxHeader.fPacketSize = fParams.fMtu;
  745. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  746. return Send ( fTxHeader.fPacketSize, 0 );
  747. }
  748. int JackNetSlaveInterface::DataSend()
  749. {
  750. uint subproc;
  751. //midi
  752. if ( fParams.fReturnMidiChannels > 0)
  753. {
  754. fTxHeader.fDataType = 'm';
  755. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  756. fTxHeader.fNMidiPckt = GetNMidiPckt();
  757. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  758. {
  759. fTxHeader.fSubCycle = subproc;
  760. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels ) ? 1 : 0;
  761. fTxHeader.fPacketSize = sizeof ( packet_header_t ) + fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  762. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  763. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  764. return SOCKET_ERROR;
  765. }
  766. }
  767. //audio
  768. if ( fParams.fReturnAudioChannels > 0)
  769. {
  770. fTxHeader.fDataType = 'a';
  771. fTxHeader.fMidiDataSize = 0;
  772. fTxHeader.fNMidiPckt = 0;
  773. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  774. {
  775. fTxHeader.fSubCycle = subproc;
  776. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  777. fTxHeader.fPacketSize = fAudioTxLen;
  778. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  779. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  780. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  781. return SOCKET_ERROR;
  782. }
  783. }
  784. return 0;
  785. }
  786. //network sync------------------------------------------------------------------------
  787. int JackNetSlaveInterface::DecodeSyncPacket()
  788. {
  789. //this method contains every step of sync packet informations decoding process
  790. //first : transport
  791. if ( fParams.fTransportSync )
  792. {
  793. //copy received transport data to transport data structure
  794. memcpy ( &fSendTransportData, fRxData, sizeof ( net_transport_data_t ) );
  795. if ( DecodeTransportData() < 0 )
  796. return -1;
  797. }
  798. //then others
  799. //...
  800. return 0;
  801. }
  802. int JackNetSlaveInterface::EncodeSyncPacket()
  803. {
  804. //this method contains every step of sync packet informations coding
  805. //first of all, reset sync packet
  806. memset ( fTxData, 0, fPayloadSize );
  807. //then first step : transport
  808. if ( fParams.fTransportSync )
  809. {
  810. if ( EncodeTransportData() < 0 )
  811. return -1;
  812. //copy to TxBuffer
  813. memcpy ( fTxData, &fReturnTransportData, sizeof ( net_transport_data_t ) );
  814. }
  815. //then others
  816. //...
  817. return 0;
  818. }
  819. }