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.

993 lines
38KB

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