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.

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