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.

988 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. }
  278. int JackNetMasterInterface::Recv ( size_t size, int flags )
  279. {
  280. int rx_bytes;
  281. if ( ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning )
  282. {
  283. net_error_t error = fSocket.GetError();
  284. //no data isn't really a network error, so just return 0 avalaible read bytes
  285. if ( error == NET_NO_DATA )
  286. return 0;
  287. else if ( error == NET_CONN_ERROR )
  288. {
  289. //fatal connection issue, exit
  290. jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
  291. //ask to the manager to properly remove the master
  292. Exit();
  293. // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
  294. ThreadExit();
  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. // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
  317. ThreadExit();
  318. }
  319. else
  320. jack_error ( "Error in master send : %s", StrError ( NET_ERROR_CODE ) );
  321. }
  322. return tx_bytes;
  323. }
  324. bool JackNetMasterInterface::IsSynched()
  325. {
  326. if (fParams.fNetworkMode == 's') {
  327. return (fCycleOffset < 3);
  328. } else {
  329. return true;
  330. }
  331. }
  332. int JackNetMasterInterface::SyncSend()
  333. {
  334. fTxHeader.fCycle++;
  335. fTxHeader.fSubCycle = 0;
  336. fTxHeader.fDataType = 's';
  337. fTxHeader.fIsLastPckt = ( fParams.fSendMidiChannels == 0 && fParams.fSendAudioChannels == 0) ? 1 : 0;
  338. fTxHeader.fPacketSize = fParams.fMtu;
  339. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  340. return Send ( fTxHeader.fPacketSize, 0 );
  341. }
  342. int JackNetMasterInterface::DataSend()
  343. {
  344. uint subproc;
  345. //midi
  346. if ( fParams.fSendMidiChannels > 0)
  347. {
  348. //set global header fields and get the number of midi packets
  349. fTxHeader.fDataType = 'm';
  350. fTxHeader.fMidiDataSize = fNetMidiCaptureBuffer->RenderFromJackPorts();
  351. fTxHeader.fNMidiPckt = GetNMidiPckt();
  352. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  353. {
  354. fTxHeader.fSubCycle = subproc;
  355. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && (fParams.fSendAudioChannels == 0)) ? 1 : 0;
  356. fTxHeader.fPacketSize = sizeof ( packet_header_t ) + fNetMidiCaptureBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  357. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  358. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  359. return SOCKET_ERROR;
  360. }
  361. }
  362. //audio
  363. if ( fParams.fSendAudioChannels > 0)
  364. {
  365. fTxHeader.fDataType = 'a';
  366. fTxHeader.fMidiDataSize = 0;
  367. fTxHeader.fNMidiPckt = 0;
  368. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  369. {
  370. fTxHeader.fSubCycle = subproc;
  371. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  372. fTxHeader.fPacketSize = fAudioTxLen;
  373. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  374. fNetAudioCaptureBuffer->RenderFromJackPorts ( subproc );
  375. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  376. return SOCKET_ERROR;
  377. }
  378. }
  379. return 0;
  380. }
  381. int JackNetMasterInterface::SyncRecv()
  382. {
  383. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  384. int rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  385. if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) )
  386. return rx_bytes;
  387. fCycleOffset = fTxHeader.fCycle - rx_head->fCycle;
  388. switch ( fParams.fNetworkMode )
  389. {
  390. case 's' :
  391. //slow mode : allow to use full bandwidth and heavy process on the slave
  392. // - extra latency is set to two cycles, one cycle for send/receive operations + one cycle for heavy process on the slave
  393. // - if the network is two fast, just wait the next cycle, this mode allows a shorter cycle duration for the master
  394. // - this mode will skip the two first cycles, thus it lets time for data to be processed and queued on the socket rx buffer
  395. //the slow mode is the safest mode because it wait twice the bandwidth relative time (send/return + process)
  396. if (fCycleOffset < 2)
  397. return 0;
  398. else
  399. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  400. if (fCycleOffset > 2) {
  401. jack_info("Warning : '%s' runs in slow network mode, but data received too late (%d cycle(s) offset)", fParams.fName, fCycleOffset);
  402. }
  403. break;
  404. case 'n' :
  405. //normal use of the network :
  406. // - extra latency is set to one cycle, what is the time needed to receive streams using full network bandwidth
  407. // - if the network is too fast, just wait the next cycle, the benefit here is the master's cycle is shorter
  408. // - indeed, data is supposed to be on the network rx buffer, so we don't have to wait for it
  409. if (fCycleOffset < 1)
  410. return 0;
  411. else
  412. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  413. if (fCycleOffset != 1)
  414. jack_info("'%s' can't run in normal network mode, data received too late (%d cycle(s) offset)", fParams.fName, fCycleOffset);
  415. break;
  416. case 'f' :
  417. //fast mode suppose the network bandwith is larger than required for the transmission (only a few channels for example)
  418. // - packets can be quickly received, quickly is here relative to the cycle duration
  419. // - here, receive data, we can't keep it queued on the rx buffer,
  420. // - but if there is a cycle offset, tell the user, that means we're not in fast mode anymore, network is too slow
  421. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  422. if (fCycleOffset != 0)
  423. jack_info("'%s' can't run in fast network mode, data received too late (%d cycle(s) offset)", fParams.fName, fCycleOffset);
  424. break;
  425. }
  426. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  427. return rx_bytes;
  428. }
  429. int JackNetMasterInterface::DataRecv()
  430. {
  431. int rx_bytes = 0;
  432. uint jumpcnt = 0;
  433. uint recvd_midi_pckt = 0;
  434. uint recvd_audio_pckt = 0;
  435. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  436. while ( !fRxHeader.fIsLastPckt )
  437. {
  438. //how much data is queued on the rx buffer ?
  439. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  440. if ( rx_bytes == SOCKET_ERROR )
  441. return rx_bytes;
  442. //if no data
  443. if ( ( rx_bytes == 0 ) && ( ++jumpcnt == fNSubProcess ) )
  444. {
  445. jack_error ( "No data from %s...", fParams.fName );
  446. jumpcnt = 0;
  447. }
  448. //else if data is valid,
  449. if ( rx_bytes && ( rx_head->fDataStream == 'r' ) && ( rx_head->fID == fParams.fID ) )
  450. {
  451. //read data
  452. switch ( rx_head->fDataType )
  453. {
  454. case 'm': //midi
  455. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  456. fRxHeader.fCycle = rx_head->fCycle;
  457. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  458. fNetMidiPlaybackBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  459. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  460. fNetMidiPlaybackBuffer->RenderToJackPorts();
  461. jumpcnt = 0;
  462. break;
  463. case 'a': //audio
  464. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  465. // SL: 25/01/09
  466. // if ( !IsNextPacket() )
  467. // jack_error ( "Packet(s) missing from '%s'...", fParams.fName );
  468. if (recvd_audio_pckt++ != rx_head->fSubCycle) {
  469. jack_error("Packet(s) missing from '%s'...", fParams.fSlaveNetName);
  470. }
  471. fRxHeader.fCycle = rx_head->fCycle;
  472. fRxHeader.fSubCycle = rx_head->fSubCycle;
  473. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  474. fNetAudioPlaybackBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  475. jumpcnt = 0;
  476. break;
  477. case 's': //sync
  478. /* SL: 25/01/09
  479. if ( rx_head->fCycle == fTxHeader.fCycle )
  480. return 0;
  481. */
  482. jack_info("NetMaster : overloaded, skipping receive from '%s'", fParams.fName);
  483. return 0;
  484. }
  485. }
  486. }
  487. return rx_bytes;
  488. }
  489. void JackNetMasterInterface::EncodeSyncPacket()
  490. {
  491. //this method contains every step of sync packet informations coding
  492. //first of all, reset sync packet
  493. memset ( fTxData, 0, fPayloadSize );
  494. //then, first step : transport
  495. if (fParams.fTransportSync) {
  496. EncodeTransportData();
  497. TransportDataHToN( &fSendTransportData, &fSendTransportData);
  498. //copy to TxBuffer
  499. memcpy ( fTxData, &fSendTransportData, sizeof ( net_transport_data_t ) );
  500. }
  501. //then others (freewheel etc.)
  502. //...
  503. }
  504. void JackNetMasterInterface::DecodeSyncPacket()
  505. {
  506. //this method contains every step of sync packet informations decoding process
  507. //first : transport
  508. if (fParams.fTransportSync) {
  509. //copy received transport data to transport data structure
  510. memcpy ( &fReturnTransportData, fRxData, sizeof ( net_transport_data_t ) );
  511. TransportDataNToH( &fReturnTransportData, &fReturnTransportData);
  512. DecodeTransportData();
  513. }
  514. //then others
  515. //...
  516. }
  517. // JackNetSlaveInterface ************************************************************************************************
  518. uint JackNetSlaveInterface::fSlaveCounter = 0;
  519. bool JackNetSlaveInterface::Init()
  520. {
  521. jack_log ( "JackNetSlaveInterface::Init()" );
  522. //set the parameters to send
  523. strcpy ( fParams.fPacketType, "params" );
  524. fParams.fProtocolVersion = SLAVE_PROTOCOL;
  525. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  526. //init loop : get a master and start, do it until connection is ok
  527. net_status_t status;
  528. do
  529. {
  530. //first, get a master, do it until a valid connection is running
  531. do
  532. {
  533. status = GetNetMaster();
  534. if ( status == NET_SOCKET_ERROR )
  535. return false;
  536. }
  537. while ( status != NET_CONNECTED );
  538. //then tell the master we are ready
  539. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  540. status = SendStartToMaster();
  541. if ( status == NET_ERROR )
  542. return false;
  543. }
  544. while ( status != NET_ROLLING );
  545. return true;
  546. }
  547. // Separate the connection protocol into two separated step
  548. bool JackNetSlaveInterface::InitConnection()
  549. {
  550. jack_log ( "JackNetSlaveInterface::InitConnection()" );
  551. //set the parameters to send
  552. strcpy (fParams.fPacketType, "params");
  553. fParams.fProtocolVersion = SLAVE_PROTOCOL;
  554. SetPacketType (&fParams, SLAVE_AVAILABLE);
  555. net_status_t status;
  556. do
  557. {
  558. //get a master
  559. status = GetNetMaster();
  560. if (status == NET_SOCKET_ERROR)
  561. return false;
  562. }
  563. while (status != NET_CONNECTED);
  564. return true;
  565. }
  566. bool JackNetSlaveInterface::InitRendering()
  567. {
  568. jack_log("JackNetSlaveInterface::InitRendering()");
  569. net_status_t status;
  570. do
  571. {
  572. //then tell the master we are ready
  573. jack_info("Initializing connection with %s...", fParams.fMasterNetName);
  574. status = SendStartToMaster();
  575. if (status == NET_ERROR)
  576. return false;
  577. }
  578. while (status != NET_ROLLING);
  579. return true;
  580. }
  581. net_status_t JackNetSlaveInterface::GetNetMaster()
  582. {
  583. jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
  584. //utility
  585. session_params_t host_params;
  586. int rx_bytes = 0;
  587. //socket
  588. if ( fSocket.NewSocket() == SOCKET_ERROR )
  589. {
  590. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  591. return NET_SOCKET_ERROR;
  592. }
  593. //bind the socket
  594. if ( fSocket.Bind() == SOCKET_ERROR )
  595. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  596. //timeout on receive
  597. if ( fSocket.SetTimeOut ( SLAVE_INIT_TIMEOUT ) == SOCKET_ERROR )
  598. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  599. //disable local loop
  600. if ( fSocket.SetLocalLoop() == SOCKET_ERROR )
  601. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  602. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  603. jack_info ( "Waiting for a master..." );
  604. do
  605. {
  606. //send 'available'
  607. session_params_t net_params;
  608. SessionParamsHToN(&fParams, &net_params);
  609. if ( fSocket.SendTo ( &net_params, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  610. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  611. //filter incoming packets : don't exit while no error is detected
  612. memset(&net_params, 0, sizeof ( session_params_t ));
  613. rx_bytes = fSocket.CatchHost ( &net_params, sizeof ( session_params_t ), 0 );
  614. SessionParamsNToH(&net_params, &host_params);
  615. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  616. {
  617. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  618. return NET_RECV_ERROR;
  619. }
  620. }
  621. while ( strcmp ( host_params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &host_params ) != SLAVE_SETUP ) );
  622. //everything is OK, copy parameters
  623. fParams = host_params;
  624. //set the new buffer sizes
  625. if ( SetNetBufferSize() == SOCKET_ERROR )
  626. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  627. //connect the socket
  628. if ( fSocket.Connect() == SOCKET_ERROR )
  629. {
  630. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  631. return NET_CONNECT_ERROR;
  632. }
  633. return NET_CONNECTED;
  634. }
  635. net_status_t JackNetSlaveInterface::SendStartToMaster()
  636. {
  637. jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
  638. //tell the master to start
  639. session_params_t net_params;
  640. SetPacketType ( &fParams, START_MASTER );
  641. SessionParamsHToN(&fParams, &net_params);
  642. if ( fSocket.Send ( &net_params, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  643. {
  644. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  645. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  646. }
  647. return NET_ROLLING;
  648. }
  649. void JackNetSlaveInterface::SetParams()
  650. {
  651. jack_log ( "JackNetSlaveInterface::SetParams" );
  652. JackNetInterface::SetParams();
  653. fTxHeader.fDataStream = 'r';
  654. fRxHeader.fDataStream = 's';
  655. //midi net buffers
  656. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  657. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  658. //audio net buffers
  659. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  660. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  661. //audio netbuffer length
  662. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  663. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  664. }
  665. int JackNetSlaveInterface::Recv ( size_t size, int flags )
  666. {
  667. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  668. //handle errors
  669. if ( rx_bytes == SOCKET_ERROR )
  670. {
  671. net_error_t error = fSocket.GetError();
  672. //no data isn't really an error in realtime processing, so just return 0
  673. if ( error == NET_NO_DATA )
  674. jack_error ( "No data, is the master still running ?" );
  675. //if a network error occurs, this exception will restart the driver
  676. else if ( error == NET_CONN_ERROR )
  677. {
  678. jack_error ( "Connection lost." );
  679. throw JackNetException();
  680. }
  681. else
  682. jack_error ( "Fatal error in slave receive : %s", StrError ( NET_ERROR_CODE ) );
  683. }
  684. packet_header_t* header = reinterpret_cast<packet_header_t*>(fRxBuffer);
  685. PacketHeaderNToH(header, header);
  686. return rx_bytes;
  687. }
  688. int JackNetSlaveInterface::Send ( size_t size, int flags )
  689. {
  690. packet_header_t* header = reinterpret_cast<packet_header_t*>(fTxBuffer);
  691. PacketHeaderHToN(header, header);
  692. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  693. //handle errors
  694. if ( tx_bytes == SOCKET_ERROR )
  695. {
  696. net_error_t error = fSocket.GetError();
  697. //if a network error occurs, this exception will restart the driver
  698. if ( error == NET_CONN_ERROR )
  699. {
  700. jack_error ( "Connection lost." );
  701. throw JackNetException();
  702. }
  703. else
  704. jack_error ( "Fatal error in slave send : %s", StrError ( NET_ERROR_CODE ) );
  705. }
  706. return tx_bytes;
  707. }
  708. int JackNetSlaveInterface::SyncRecv()
  709. {
  710. int rx_bytes = 0;
  711. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  712. //receive sync (launch the cycle)
  713. do
  714. {
  715. rx_bytes = Recv ( fParams.fMtu, 0 );
  716. //connection issue, send will detect it, so don't skip the cycle (return 0)
  717. if ( rx_bytes == SOCKET_ERROR )
  718. return rx_bytes;
  719. }
  720. while ((strcmp(rx_head->fPacketType, "header") != 0) && (rx_head->fDataType != 's'));
  721. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  722. return rx_bytes;
  723. }
  724. int JackNetSlaveInterface::DataRecv()
  725. {
  726. uint recvd_midi_pckt = 0;
  727. uint recvd_audio_pckt = 0;
  728. int rx_bytes = 0;
  729. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  730. while ( !fRxHeader.fIsLastPckt )
  731. {
  732. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  733. //error here, problem with recv, just skip the cycle (return -1)
  734. if ( rx_bytes == SOCKET_ERROR )
  735. return rx_bytes;
  736. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  737. {
  738. switch ( rx_head->fDataType )
  739. {
  740. case 'm': //midi
  741. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  742. fRxHeader.fCycle = rx_head->fCycle;
  743. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  744. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  745. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  746. fNetMidiCaptureBuffer->RenderToJackPorts();
  747. break;
  748. case 'a': //audio
  749. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  750. //SL: 25/01/09
  751. // if ( !IsNextPacket() )
  752. // jack_error ( "Packet(s) missing..." );
  753. if (recvd_audio_pckt++ != rx_head->fSubCycle) {
  754. jack_error("Packet(s) missing from '%s'...", fParams.fMasterNetName);
  755. }
  756. fRxHeader.fCycle = rx_head->fCycle;
  757. fRxHeader.fSubCycle = rx_head->fSubCycle;
  758. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  759. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  760. break;
  761. case 's': //sync
  762. jack_info ( "NetSlave : overloaded, skipping receive." );
  763. return 0;
  764. }
  765. }
  766. }
  767. fRxHeader.fCycle = rx_head->fCycle;
  768. return 0;
  769. }
  770. int JackNetSlaveInterface::SyncSend()
  771. {
  772. //tx header
  773. if ( fParams.fSlaveSyncMode )
  774. fTxHeader.fCycle = fRxHeader.fCycle;
  775. else
  776. fTxHeader.fCycle++;
  777. fTxHeader.fSubCycle = 0;
  778. fTxHeader.fDataType = 's';
  779. fTxHeader.fIsLastPckt = ( fParams.fReturnMidiChannels == 0 && fParams.fReturnAudioChannels == 0) ? 1 : 0;
  780. fTxHeader.fPacketSize = fParams.fMtu;
  781. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  782. return Send ( fTxHeader.fPacketSize, 0 );
  783. }
  784. int JackNetSlaveInterface::DataSend()
  785. {
  786. uint subproc;
  787. //midi
  788. if ( fParams.fReturnMidiChannels > 0)
  789. {
  790. fTxHeader.fDataType = 'm';
  791. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  792. fTxHeader.fNMidiPckt = GetNMidiPckt();
  793. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  794. {
  795. fTxHeader.fSubCycle = subproc;
  796. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels ) ? 1 : 0;
  797. fTxHeader.fPacketSize = sizeof ( packet_header_t ) + fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  798. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  799. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  800. return SOCKET_ERROR;
  801. }
  802. }
  803. //audio
  804. if ( fParams.fReturnAudioChannels > 0)
  805. {
  806. fTxHeader.fDataType = 'a';
  807. fTxHeader.fMidiDataSize = 0;
  808. fTxHeader.fNMidiPckt = 0;
  809. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  810. {
  811. fTxHeader.fSubCycle = subproc;
  812. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  813. fTxHeader.fPacketSize = fAudioTxLen;
  814. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  815. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  816. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  817. return SOCKET_ERROR;
  818. }
  819. }
  820. return 0;
  821. }
  822. //network sync------------------------------------------------------------------------
  823. void JackNetSlaveInterface::EncodeSyncPacket()
  824. {
  825. //this method contains every step of sync packet informations coding
  826. //first of all, reset sync packet
  827. memset ( fTxData, 0, fPayloadSize );
  828. //then first step : transport
  829. if (fParams.fTransportSync) {
  830. EncodeTransportData();
  831. TransportDataHToN( &fReturnTransportData, &fReturnTransportData);
  832. //copy to TxBuffer
  833. memcpy ( fTxData, &fReturnTransportData, sizeof ( net_transport_data_t ) );
  834. }
  835. //then others
  836. //...
  837. }
  838. void JackNetSlaveInterface::DecodeSyncPacket()
  839. {
  840. //this method contains every step of sync packet informations decoding process
  841. //first : transport
  842. if (fParams.fTransportSync) {
  843. //copy received transport data to transport data structure
  844. memcpy ( &fSendTransportData, fRxData, sizeof ( net_transport_data_t ) );
  845. TransportDataNToH( &fSendTransportData, &fSendTransportData);
  846. DecodeTransportData();
  847. }
  848. //then others
  849. //...
  850. }
  851. }