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.

1004 lines
39KB

  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. jack_log ( "sizeof (session_params_t) %d", sizeof (session_params_t) );
  585. //socket
  586. if ( fSocket.NewSocket() == SOCKET_ERROR ) {
  587. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  588. return NET_SOCKET_ERROR;
  589. }
  590. //bind the socket
  591. if ( fSocket.Bind() == SOCKET_ERROR ) {
  592. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  593. return NET_SOCKET_ERROR;
  594. }
  595. //timeout on receive
  596. if ( fSocket.SetTimeOut ( SLAVE_INIT_TIMEOUT ) == SOCKET_ERROR )
  597. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  598. //disable local loop
  599. if ( fSocket.SetLocalLoop() == SOCKET_ERROR )
  600. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  601. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  602. jack_info ( "Waiting for a master..." );
  603. do
  604. {
  605. //send 'available'
  606. session_params_t net_params;
  607. memset(&net_params, 0, sizeof ( session_params_t ));
  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. SessionParamsDisplay(&net_params);
  615. jack_error ( "rx_bytes %d %d", rx_bytes, errno );
  616. SessionParamsNToH(&net_params, &host_params);
  617. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  618. {
  619. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  620. return NET_RECV_ERROR;
  621. }
  622. for (int i = 0; i < 7; i++) {
  623. jack_info ( " fPacketType %d", host_params.fPacketType[i]);
  624. }
  625. jack_info ( " received... host_params param %s %s %d %d", net_params.fPacketType, fParams.fPacketType, net_params.fPacketID, GetPacketType ( &host_params ));
  626. SessionParamsDisplay(&host_params);
  627. }
  628. while ( strcmp ( host_params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &host_params ) != SLAVE_SETUP ) );
  629. jack_info ( "SLAVE_SETUP received..." );
  630. //everything is OK, copy parameters
  631. fParams = host_params;
  632. //set the new buffer sizes
  633. if ( SetNetBufferSize() == SOCKET_ERROR ) {
  634. jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
  635. return NET_SOCKET_ERROR;
  636. }
  637. //connect the socket
  638. if ( fSocket.Connect() == SOCKET_ERROR ) {
  639. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  640. return NET_CONNECT_ERROR;
  641. }
  642. return NET_CONNECTED;
  643. }
  644. net_status_t JackNetSlaveInterface::SendStartToMaster()
  645. {
  646. jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
  647. //tell the master to start
  648. session_params_t net_params;
  649. memset(&net_params, 0, sizeof ( session_params_t ));
  650. SetPacketType ( &fParams, START_MASTER );
  651. SessionParamsHToN(&fParams, &net_params);
  652. if ( fSocket.Send ( &net_params, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  653. {
  654. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  655. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  656. }
  657. return NET_ROLLING;
  658. }
  659. void JackNetSlaveInterface::SetParams()
  660. {
  661. jack_log ( "JackNetSlaveInterface::SetParams" );
  662. JackNetInterface::SetParams();
  663. fTxHeader.fDataStream = 'r';
  664. fRxHeader.fDataStream = 's';
  665. //midi net buffers
  666. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  667. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  668. //audio net buffers
  669. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  670. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  671. //audio netbuffer length
  672. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  673. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  674. }
  675. int JackNetSlaveInterface::Recv ( size_t size, int flags )
  676. {
  677. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  678. //handle errors
  679. if ( rx_bytes == SOCKET_ERROR )
  680. {
  681. net_error_t error = fSocket.GetError();
  682. //no data isn't really an error in realtime processing, so just return 0
  683. if ( error == NET_NO_DATA )
  684. jack_error ( "No data, is the master still running ?" );
  685. //if a network error occurs, this exception will restart the driver
  686. else if ( error == NET_CONN_ERROR )
  687. {
  688. jack_error ( "Connection lost." );
  689. throw JackNetException();
  690. }
  691. else
  692. jack_error ( "Fatal error in slave receive : %s", StrError ( NET_ERROR_CODE ) );
  693. }
  694. packet_header_t* header = reinterpret_cast<packet_header_t*>(fRxBuffer);
  695. PacketHeaderNToH(header, header);
  696. return rx_bytes;
  697. }
  698. int JackNetSlaveInterface::Send ( size_t size, int flags )
  699. {
  700. packet_header_t* header = reinterpret_cast<packet_header_t*>(fTxBuffer);
  701. PacketHeaderHToN(header, header);
  702. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  703. //handle errors
  704. if ( tx_bytes == SOCKET_ERROR )
  705. {
  706. net_error_t error = fSocket.GetError();
  707. //if a network error occurs, this exception will restart the driver
  708. if ( error == NET_CONN_ERROR )
  709. {
  710. jack_error ( "Connection lost." );
  711. throw JackNetException();
  712. }
  713. else
  714. jack_error ( "Fatal error in slave send : %s", StrError ( NET_ERROR_CODE ) );
  715. }
  716. return tx_bytes;
  717. }
  718. int JackNetSlaveInterface::SyncRecv()
  719. {
  720. int rx_bytes = 0;
  721. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  722. //receive sync (launch the cycle)
  723. do
  724. {
  725. rx_bytes = Recv ( fParams.fMtu, 0 );
  726. //connection issue, send will detect it, so don't skip the cycle (return 0)
  727. if ( rx_bytes == SOCKET_ERROR )
  728. return rx_bytes;
  729. }
  730. while ((strcmp(rx_head->fPacketType, "header") != 0) && (rx_head->fDataType != 's'));
  731. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  732. return rx_bytes;
  733. }
  734. int JackNetSlaveInterface::DataRecv()
  735. {
  736. uint recvd_midi_pckt = 0;
  737. uint recvd_audio_pckt = 0;
  738. int rx_bytes = 0;
  739. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  740. while ( !fRxHeader.fIsLastPckt )
  741. {
  742. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  743. //error here, problem with recv, just skip the cycle (return -1)
  744. if ( rx_bytes == SOCKET_ERROR )
  745. return rx_bytes;
  746. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  747. {
  748. switch ( rx_head->fDataType )
  749. {
  750. case 'm': //midi
  751. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  752. fRxHeader.fCycle = rx_head->fCycle;
  753. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  754. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  755. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  756. fNetMidiCaptureBuffer->RenderToJackPorts();
  757. break;
  758. case 'a': //audio
  759. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  760. //SL: 25/01/09
  761. // if ( !IsNextPacket() )
  762. // jack_error ( "Packet(s) missing..." );
  763. if (recvd_audio_pckt++ != rx_head->fSubCycle) {
  764. jack_error("Packet(s) missing from '%s'...", fParams.fMasterNetName);
  765. }
  766. fRxHeader.fCycle = rx_head->fCycle;
  767. fRxHeader.fSubCycle = rx_head->fSubCycle;
  768. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  769. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  770. break;
  771. case 's': //sync
  772. jack_info ( "NetSlave : overloaded, skipping receive." );
  773. return 0;
  774. }
  775. }
  776. }
  777. fRxHeader.fCycle = rx_head->fCycle;
  778. return 0;
  779. }
  780. int JackNetSlaveInterface::SyncSend()
  781. {
  782. //tx header
  783. if ( fParams.fSlaveSyncMode )
  784. fTxHeader.fCycle = fRxHeader.fCycle;
  785. else
  786. fTxHeader.fCycle++;
  787. fTxHeader.fSubCycle = 0;
  788. fTxHeader.fDataType = 's';
  789. fTxHeader.fIsLastPckt = ( fParams.fReturnMidiChannels == 0 && fParams.fReturnAudioChannels == 0) ? 1 : 0;
  790. fTxHeader.fPacketSize = fParams.fMtu;
  791. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  792. return Send ( fTxHeader.fPacketSize, 0 );
  793. }
  794. int JackNetSlaveInterface::DataSend()
  795. {
  796. uint subproc;
  797. //midi
  798. if ( fParams.fReturnMidiChannels > 0)
  799. {
  800. fTxHeader.fDataType = 'm';
  801. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  802. fTxHeader.fNMidiPckt = GetNMidiPckt();
  803. for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  804. {
  805. fTxHeader.fSubCycle = subproc;
  806. fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels ) ? 1 : 0;
  807. fTxHeader.fPacketSize = sizeof ( packet_header_t ) + fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  808. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  809. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  810. return SOCKET_ERROR;
  811. }
  812. }
  813. //audio
  814. if ( fParams.fReturnAudioChannels > 0)
  815. {
  816. fTxHeader.fDataType = 'a';
  817. fTxHeader.fMidiDataSize = 0;
  818. fTxHeader.fNMidiPckt = 0;
  819. for ( subproc = 0; subproc < fNSubProcess; subproc++ )
  820. {
  821. fTxHeader.fSubCycle = subproc;
  822. fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
  823. fTxHeader.fPacketSize = fAudioTxLen;
  824. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  825. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  826. if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
  827. return SOCKET_ERROR;
  828. }
  829. }
  830. return 0;
  831. }
  832. //network sync------------------------------------------------------------------------
  833. void JackNetSlaveInterface::EncodeSyncPacket()
  834. {
  835. //this method contains every step of sync packet informations coding
  836. //first of all, reset sync packet
  837. memset ( fTxData, 0, fPayloadSize );
  838. //then first step : transport
  839. if (fParams.fTransportSync) {
  840. EncodeTransportData();
  841. TransportDataHToN( &fReturnTransportData, &fReturnTransportData);
  842. //copy to TxBuffer
  843. memcpy ( fTxData, &fReturnTransportData, sizeof ( net_transport_data_t ) );
  844. }
  845. //then others
  846. //...
  847. }
  848. void JackNetSlaveInterface::DecodeSyncPacket()
  849. {
  850. //this method contains every step of sync packet informations decoding process
  851. //first : transport
  852. if (fParams.fTransportSync) {
  853. //copy received transport data to transport data structure
  854. memcpy ( &fSendTransportData, fRxData, sizeof ( net_transport_data_t ) );
  855. TransportDataNToH( &fSendTransportData, &fSendTransportData);
  856. DecodeTransportData();
  857. }
  858. //then others
  859. //...
  860. }
  861. }