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.

985 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. void 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. EncodeTransportData();
  495. TransportDataHToN( &fSendTransportData, &fSendTransportData);
  496. //copy to TxBuffer
  497. memcpy ( fTxData, &fSendTransportData, sizeof ( net_transport_data_t ) );
  498. }
  499. //then others (freewheel etc.)
  500. //...
  501. }
  502. void JackNetMasterInterface::DecodeSyncPacket()
  503. {
  504. //this method contains every step of sync packet informations decoding process
  505. //first : transport
  506. if (fParams.fTransportSync) {
  507. //copy received transport data to transport data structure
  508. memcpy ( &fReturnTransportData, fRxData, sizeof ( net_transport_data_t ) );
  509. TransportDataNToH( &fReturnTransportData, &fReturnTransportData);
  510. DecodeTransportData();
  511. }
  512. //then others
  513. //...
  514. }
  515. // JackNetSlaveInterface ************************************************************************************************
  516. uint JackNetSlaveInterface::fSlaveCounter = 0;
  517. bool JackNetSlaveInterface::Init()
  518. {
  519. jack_log ( "JackNetSlaveInterface::Init()" );
  520. //set the parameters to send
  521. strcpy ( fParams.fPacketType, "params" );
  522. fParams.fProtocolVersion = SLAVE_PROTOCOL;
  523. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  524. //init loop : get a master and start, do it until connection is ok
  525. net_status_t status;
  526. do
  527. {
  528. //first, get a master, do it until a valid connection is running
  529. do
  530. {
  531. status = GetNetMaster();
  532. if ( status == NET_SOCKET_ERROR )
  533. return false;
  534. }
  535. while ( status != NET_CONNECTED );
  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. // Separate the connection protocol into two separated step
  546. bool JackNetSlaveInterface::InitConnection()
  547. {
  548. jack_log ( "JackNetSlaveInterface::InitConnection()" );
  549. //set the parameters to send
  550. strcpy (fParams.fPacketType, "params");
  551. fParams.fProtocolVersion = SLAVE_PROTOCOL;
  552. SetPacketType (&fParams, SLAVE_AVAILABLE);
  553. net_status_t status;
  554. do
  555. {
  556. //get a master
  557. status = GetNetMaster();
  558. if (status == NET_SOCKET_ERROR)
  559. return false;
  560. }
  561. while (status != NET_CONNECTED);
  562. return true;
  563. }
  564. bool JackNetSlaveInterface::InitRendering()
  565. {
  566. jack_log("JackNetSlaveInterface::InitRendering()");
  567. net_status_t status;
  568. do
  569. {
  570. //then tell the master we are ready
  571. jack_info("Initializing connection with %s...", fParams.fMasterNetName);
  572. status = SendStartToMaster();
  573. if (status == NET_ERROR)
  574. return false;
  575. }
  576. while (status != NET_ROLLING);
  577. return true;
  578. }
  579. net_status_t JackNetSlaveInterface::GetNetMaster()
  580. {
  581. jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
  582. //utility
  583. session_params_t host_params;
  584. int rx_bytes = 0;
  585. //socket
  586. if ( fSocket.NewSocket() == SOCKET_ERROR )
  587. {
  588. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  589. return NET_SOCKET_ERROR;
  590. }
  591. //bind the socket
  592. if ( fSocket.Bind() == SOCKET_ERROR )
  593. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  594. //timeout on receive
  595. if ( fSocket.SetTimeOut ( SLAVE_INIT_TIMEOUT ) == SOCKET_ERROR )
  596. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  597. //disable local loop
  598. if ( fSocket.SetLocalLoop() == SOCKET_ERROR )
  599. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  600. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  601. jack_info ( "Waiting for a master..." );
  602. do
  603. {
  604. //send 'available'
  605. session_params_t net_params;
  606. SessionParamsHToN(&fParams, &net_params);
  607. if ( fSocket.SendTo ( &net_params, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  608. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  609. //filter incoming packets : don't exit while no error is detected
  610. memset(&net_params, 0, sizeof ( session_params_t ));
  611. rx_bytes = fSocket.CatchHost ( &net_params, sizeof ( session_params_t ), 0 );
  612. SessionParamsNToH(&net_params, &host_params);
  613. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  614. {
  615. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  616. return NET_RECV_ERROR;
  617. }
  618. }
  619. while ( strcmp ( host_params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &host_params ) != SLAVE_SETUP ) );
  620. //everything is OK, copy parameters
  621. fParams = host_params;
  622. //set the new buffer sizes
  623. if ( SetNetBufferSize() == SOCKET_ERROR )
  624. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  625. //connect the socket
  626. if ( fSocket.Connect() == SOCKET_ERROR )
  627. {
  628. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  629. return NET_CONNECT_ERROR;
  630. }
  631. return NET_CONNECTED;
  632. }
  633. net_status_t JackNetSlaveInterface::SendStartToMaster()
  634. {
  635. jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
  636. //tell the master to start
  637. session_params_t net_params;
  638. SetPacketType ( &fParams, START_MASTER );
  639. SessionParamsHToN(&fParams, &net_params);
  640. if ( fSocket.Send ( &net_params, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  641. {
  642. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  643. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  644. }
  645. return NET_ROLLING;
  646. }
  647. void JackNetSlaveInterface::SetParams()
  648. {
  649. jack_log ( "JackNetSlaveInterface::SetParams" );
  650. JackNetInterface::SetParams();
  651. fTxHeader.fDataStream = 'r';
  652. fRxHeader.fDataStream = 's';
  653. //midi net buffers
  654. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  655. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  656. //audio net buffers
  657. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  658. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  659. //audio netbuffer length
  660. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  661. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  662. }
  663. int JackNetSlaveInterface::Recv ( size_t size, int flags )
  664. {
  665. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  666. //handle errors
  667. if ( rx_bytes == SOCKET_ERROR )
  668. {
  669. net_error_t error = fSocket.GetError();
  670. //no data isn't really an error in realtime processing, so just return 0
  671. if ( error == NET_NO_DATA )
  672. jack_error ( "No data, is the master still running ?" );
  673. //if a network error occurs, this exception will restart the driver
  674. else if ( error == NET_CONN_ERROR )
  675. {
  676. jack_error ( "Connection lost." );
  677. throw JackNetException();
  678. }
  679. else
  680. jack_error ( "Fatal error in slave receive : %s", StrError ( NET_ERROR_CODE ) );
  681. }
  682. packet_header_t* header = reinterpret_cast<packet_header_t*>(fRxBuffer);
  683. PacketHeaderNToH(header, header);
  684. return rx_bytes;
  685. }
  686. int JackNetSlaveInterface::Send ( size_t size, int flags )
  687. {
  688. packet_header_t* header = reinterpret_cast<packet_header_t*>(fTxBuffer);
  689. PacketHeaderHToN(header, header);
  690. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  691. //handle errors
  692. if ( tx_bytes == SOCKET_ERROR )
  693. {
  694. net_error_t error = fSocket.GetError();
  695. //if a network error occurs, this exception will restart the driver
  696. if ( error == NET_CONN_ERROR )
  697. {
  698. jack_error ( "Connection lost." );
  699. throw JackNetException();
  700. }
  701. else
  702. jack_error ( "Fatal error in slave send : %s", StrError ( NET_ERROR_CODE ) );
  703. }
  704. return tx_bytes;
  705. }
  706. int JackNetSlaveInterface::SyncRecv()
  707. {
  708. int rx_bytes = 0;
  709. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  710. //receive sync (launch the cycle)
  711. do
  712. {
  713. rx_bytes = Recv ( fParams.fMtu, 0 );
  714. //connection issue, send will detect it, so don't skip the cycle (return 0)
  715. if ( rx_bytes == SOCKET_ERROR )
  716. return rx_bytes;
  717. }
  718. while ((strcmp(rx_head->fPacketType, "header") != 0) && (rx_head->fDataType != 's'));
  719. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  720. return rx_bytes;
  721. }
  722. int JackNetSlaveInterface::DataRecv()
  723. {
  724. uint recvd_midi_pckt = 0;
  725. uint recvd_audio_pckt = 0;
  726. int rx_bytes = 0;
  727. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  728. while ( !fRxHeader.fIsLastPckt )
  729. {
  730. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  731. //error here, problem with recv, just skip the cycle (return -1)
  732. if ( rx_bytes == SOCKET_ERROR )
  733. return rx_bytes;
  734. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  735. {
  736. switch ( rx_head->fDataType )
  737. {
  738. case 'm': //midi
  739. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  740. fRxHeader.fCycle = rx_head->fCycle;
  741. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  742. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  743. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  744. fNetMidiCaptureBuffer->RenderToJackPorts();
  745. break;
  746. case 'a': //audio
  747. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  748. //SL: 25/01/09
  749. // if ( !IsNextPacket() )
  750. // jack_error ( "Packet(s) missing..." );
  751. if (recvd_audio_pckt++ != rx_head->fSubCycle) {
  752. jack_error("Packet(s) missing from '%s'...", fParams.fMasterNetName);
  753. }
  754. fRxHeader.fCycle = rx_head->fCycle;
  755. fRxHeader.fSubCycle = rx_head->fSubCycle;
  756. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  757. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  758. break;
  759. case 's': //sync
  760. jack_info ( "NetSlave : overloaded, skipping receive." );
  761. return 0;
  762. }
  763. }
  764. }
  765. fRxHeader.fCycle = rx_head->fCycle;
  766. return 0;
  767. }
  768. int JackNetSlaveInterface::SyncSend()
  769. {
  770. //tx header
  771. if ( fParams.fSlaveSyncMode )
  772. fTxHeader.fCycle = fRxHeader.fCycle;
  773. else
  774. fTxHeader.fCycle++;
  775. fTxHeader.fSubCycle = 0;
  776. fTxHeader.fDataType = 's';
  777. fTxHeader.fIsLastPckt = ( fParams.fReturnMidiChannels == 0 && fParams.fReturnAudioChannels == 0) ? 1 : 0;
  778. fTxHeader.fPacketSize = fParams.fMtu;
  779. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  780. return Send ( fTxHeader.fPacketSize, 0 );
  781. }
  782. int JackNetSlaveInterface::DataSend()
  783. {
  784. uint subproc;
  785. //midi
  786. if ( fParams.fReturnMidiChannels > 0)
  787. {
  788. fTxHeader.fDataType = 'm';
  789. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  790. fTxHeader.fNMidiPckt = GetNMidiPckt();
  791. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  792. {
  793. fTxHeader.fSubCycle = subproc;
  794. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels ) ? 1 : 0;
  795. fTxHeader.fPacketSize = sizeof ( packet_header_t ) + fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  796. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  797. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  798. return SOCKET_ERROR;
  799. }
  800. }
  801. //audio
  802. if ( fParams.fReturnAudioChannels > 0)
  803. {
  804. fTxHeader.fDataType = 'a';
  805. fTxHeader.fMidiDataSize = 0;
  806. fTxHeader.fNMidiPckt = 0;
  807. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  808. {
  809. fTxHeader.fSubCycle = subproc;
  810. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  811. fTxHeader.fPacketSize = fAudioTxLen;
  812. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  813. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  814. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  815. return SOCKET_ERROR;
  816. }
  817. }
  818. return 0;
  819. }
  820. //network sync------------------------------------------------------------------------
  821. void JackNetSlaveInterface::EncodeSyncPacket()
  822. {
  823. //this method contains every step of sync packet informations coding
  824. //first of all, reset sync packet
  825. memset ( fTxData, 0, fPayloadSize );
  826. //then first step : transport
  827. if (fParams.fTransportSync) {
  828. EncodeTransportData();
  829. TransportDataHToN( &fReturnTransportData, &fReturnTransportData);
  830. //copy to TxBuffer
  831. memcpy ( fTxData, &fReturnTransportData, sizeof ( net_transport_data_t ) );
  832. }
  833. //then others
  834. //...
  835. }
  836. void JackNetSlaveInterface::DecodeSyncPacket()
  837. {
  838. //this method contains every step of sync packet informations decoding process
  839. //first : transport
  840. if (fParams.fTransportSync) {
  841. //copy received transport data to transport data structure
  842. memcpy ( &fSendTransportData, fRxData, sizeof ( net_transport_data_t ) );
  843. TransportDataNToH( &fSendTransportData, &fSendTransportData);
  844. DecodeTransportData();
  845. }
  846. //then others
  847. //...
  848. }
  849. }