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.

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