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