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.

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