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.

378 lines
14KB

  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 "JackNetSlaveInterface.h"
  17. #include "JackEngineControl.h"
  18. #include "JackClientControl.h"
  19. #include "JackGraphManager.h"
  20. #include "JackException.h"
  21. #define DEFAULT_MULTICAST_IP "225.3.19.154"
  22. #define DEFAULT_PORT 19000
  23. using namespace std;
  24. namespace Jack
  25. {
  26. JackNetSlaveInterface::JackNetSlaveInterface()
  27. {
  28. fMulticastIP = NULL;
  29. }
  30. JackNetSlaveInterface::~JackNetSlaveInterface()
  31. {
  32. SocketAPIEnd();
  33. delete[] fTxBuffer;
  34. delete[] fRxBuffer;
  35. delete[] fMulticastIP;
  36. delete fNetAudioCaptureBuffer;
  37. delete fNetAudioPlaybackBuffer;
  38. delete fNetMidiCaptureBuffer;
  39. delete fNetMidiPlaybackBuffer;
  40. }
  41. //*************************************initialization***********************************************************************
  42. bool JackNetSlaveInterface::Init()
  43. {
  44. jack_log ( "JackNetSlaveInterface::NetInit()" );
  45. //set the parameters to send
  46. strcpy ( fParams.fPacketType, "params" );
  47. fParams.fProtocolVersion = 'a';
  48. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  49. //init loop : get a master and start, do it until connection is ok
  50. net_status_t status;
  51. do
  52. {
  53. //first, get a master, do it until a valid connection is running
  54. jack_info ( "Initializing Net Driver..." );
  55. do
  56. {
  57. status = GetNetMaster();
  58. if ( status == NET_SOCKET_ERROR )
  59. return false;
  60. }
  61. while ( status != NET_CONNECTED );
  62. //then tell the master we are ready
  63. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  64. status = SendMasterStartSync();
  65. if ( status == NET_ERROR )
  66. return false;
  67. }
  68. while ( status != NET_ROLLING );
  69. return true;
  70. }
  71. net_status_t JackNetSlaveInterface::GetNetMaster()
  72. {
  73. jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
  74. //utility
  75. session_params_t params;
  76. int us_timeout = 2000000;
  77. int rx_bytes = 0;
  78. unsigned char loop = 0;
  79. //socket
  80. if ( fSocket.NewSocket() == SOCKET_ERROR )
  81. {
  82. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  83. return NET_SOCKET_ERROR;
  84. }
  85. //bind the socket
  86. if ( fSocket.Bind() == SOCKET_ERROR )
  87. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  88. //timeout on receive
  89. if ( fSocket.SetTimeOut ( us_timeout ) == SOCKET_ERROR )
  90. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  91. //disable local loop
  92. if ( fSocket.SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof ( loop ) ) == SOCKET_ERROR )
  93. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  94. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  95. jack_info ( "Waiting for a master..." );
  96. do
  97. {
  98. //send 'available'
  99. if ( fSocket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  100. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  101. //filter incoming packets : don't exit while no error is detected
  102. rx_bytes = fSocket.CatchHost ( &params, sizeof ( session_params_t ), 0 );
  103. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  104. {
  105. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  106. return NET_RECV_ERROR;
  107. }
  108. }
  109. while ( strcmp ( params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &params ) != SLAVE_SETUP ) );
  110. //connect the socket
  111. if ( fSocket.Connect() == SOCKET_ERROR )
  112. {
  113. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  114. return NET_CONNECT_ERROR;
  115. }
  116. //everything is OK, copy parameters and return
  117. fParams = params;
  118. return NET_CONNECTED;
  119. }
  120. net_status_t JackNetSlaveInterface::SendMasterStartSync()
  121. {
  122. jack_log ( "JackNetSlaveInterface::GetNetMasterStartSync()" );
  123. //tell the master to start
  124. SetPacketType ( &fParams, START_MASTER );
  125. if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  126. {
  127. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  128. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  129. }
  130. return NET_ROLLING;
  131. }
  132. int JackNetSlaveInterface::SetParams()
  133. {
  134. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  135. //TX header init
  136. strcpy ( fTxHeader.fPacketType, "header" );
  137. fTxHeader.fDataStream = 'r';
  138. fTxHeader.fID = fParams.fID;
  139. fTxHeader.fCycle = 0;
  140. fTxHeader.fSubCycle = 0;
  141. fTxHeader.fMidiDataSize = 0;
  142. fTxHeader.fBitdepth = fParams.fBitdepth;
  143. //RX header init
  144. strcpy ( fRxHeader.fPacketType, "header" );
  145. fRxHeader.fDataStream = 's';
  146. fRxHeader.fID = fParams.fID;
  147. fRxHeader.fCycle = 0;
  148. fRxHeader.fSubCycle = 0;
  149. fRxHeader.fMidiDataSize = 0;
  150. fRxHeader.fBitdepth = fParams.fBitdepth;
  151. //network buffers
  152. fTxBuffer = new char[fParams.fMtu];
  153. fRxBuffer = new char[fParams.fMtu];
  154. //net audio/midi buffers
  155. fTxData = fTxBuffer + sizeof ( packet_header_t );
  156. fRxData = fRxBuffer + sizeof ( packet_header_t );
  157. //midi net buffers
  158. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  159. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  160. //audio net buffers
  161. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  162. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  163. //audio netbuffer length
  164. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  165. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  166. //payload size
  167. fPayloadSize = fParams.fMtu - sizeof ( packet_header_t );
  168. return 0;
  169. }
  170. //***********************************network operations*************************************************************
  171. int JackNetSlaveInterface::Recv ( size_t size, int flags )
  172. {
  173. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  174. //handle errors
  175. if ( rx_bytes == SOCKET_ERROR )
  176. {
  177. net_error_t error = fSocket.GetError();
  178. //no data isn't really an error in realtime processing, so just return 0
  179. if ( error == NET_NO_DATA )
  180. jack_error ( "No data, is the master still running ?" );
  181. //if a network error occurs, this exception will restart the driver
  182. else if ( error == NET_CONN_ERROR )
  183. {
  184. jack_error ( "Connection lost." );
  185. throw JackDriverException();
  186. }
  187. else
  188. jack_error ( "Fatal error in receive : %s", StrError ( NET_ERROR_CODE ) );
  189. }
  190. return rx_bytes;
  191. }
  192. int JackNetSlaveInterface::Send ( size_t size, int flags )
  193. {
  194. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  195. //handle errors
  196. if ( tx_bytes == SOCKET_ERROR )
  197. {
  198. net_error_t error = fSocket.GetError();
  199. //if a network error occurs, this exception will restart the driver
  200. if ( error == NET_CONN_ERROR )
  201. {
  202. jack_error ( "Connection lost." );
  203. throw JackDriverException();
  204. }
  205. else
  206. jack_error ( "Fatal error in send : %s", StrError ( NET_ERROR_CODE ) );
  207. }
  208. return tx_bytes;
  209. }
  210. //**************************************processes***************************************************
  211. int JackNetSlaveInterface::SyncRecv()
  212. {
  213. int rx_bytes;
  214. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  215. fRxHeader.fIsLastPckt = 'n';
  216. //receive sync (launch the cycle)
  217. do
  218. {
  219. rx_bytes = Recv ( fParams.fMtu, 0 );
  220. //connection issue, send will detect it, so don't skip the cycle (return 0)
  221. if ( rx_bytes == SOCKET_ERROR )
  222. return rx_bytes;
  223. }
  224. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  225. return rx_bytes;
  226. }
  227. int JackNetSlaveInterface::DataRecv()
  228. {
  229. uint recvd_midi_pckt = 0;
  230. int rx_bytes;
  231. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  232. //audio, midi or sync if driver is late
  233. if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels )
  234. {
  235. do
  236. {
  237. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  238. //error here, problem with recv, just skip the cycle (return -1)
  239. if ( rx_bytes == SOCKET_ERROR )
  240. return rx_bytes;
  241. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  242. {
  243. switch ( rx_head->fDataType )
  244. {
  245. case 'm': //midi
  246. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  247. fRxHeader.fCycle = rx_head->fCycle;
  248. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  249. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  250. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  251. fNetMidiCaptureBuffer->RenderToJackPorts();
  252. break;
  253. case 'a': //audio
  254. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  255. if ( !IsNextPacket ( &fRxHeader, rx_head, fNSubProcess ) )
  256. jack_error ( "Packet(s) missing..." );
  257. fRxHeader.fCycle = rx_head->fCycle;
  258. fRxHeader.fSubCycle = rx_head->fSubCycle;
  259. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  260. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  261. break;
  262. case 's': //sync
  263. jack_info ( "NetSlave : overloaded, skipping receive." );
  264. fRxHeader.fCycle = rx_head->fCycle;
  265. return 0;
  266. }
  267. }
  268. }
  269. while ( fRxHeader.fIsLastPckt != 'y' );
  270. }
  271. fRxHeader.fCycle = rx_head->fCycle;
  272. return 0;
  273. }
  274. int JackNetSlaveInterface::SyncSend()
  275. {
  276. //tx header
  277. if ( fParams.fSlaveSyncMode )
  278. fTxHeader.fCycle = fRxHeader.fCycle;
  279. else
  280. fTxHeader.fCycle++;
  281. fTxHeader.fSubCycle = 0;
  282. //sync
  283. fTxHeader.fDataType = 's';
  284. fTxHeader.fIsLastPckt = ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels ) ? 'y' : 'n';
  285. fTxHeader.fPacketSize = fParams.fMtu;
  286. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  287. return Send ( fTxHeader.fPacketSize, 0 );
  288. }
  289. int JackNetSlaveInterface::DataSend()
  290. {
  291. int tx_bytes;
  292. //midi
  293. if ( fParams.fReturnMidiChannels )
  294. {
  295. fTxHeader.fDataType = 'm';
  296. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  297. fTxHeader.fNMidiPckt = GetNMidiPckt ( &fParams, fTxHeader.fMidiDataSize );
  298. for ( uint subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  299. {
  300. fTxHeader.fSubCycle = subproc;
  301. if ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels )
  302. fTxHeader.fIsLastPckt = 'y';
  303. fTxHeader.fPacketSize = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  304. fTxHeader.fPacketSize += sizeof ( packet_header_t );
  305. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  306. tx_bytes = Send ( fTxHeader.fPacketSize, 0 );
  307. if ( tx_bytes == SOCKET_ERROR )
  308. return tx_bytes;
  309. }
  310. }
  311. //audio
  312. if ( fParams.fReturnAudioChannels )
  313. {
  314. fTxHeader.fDataType = 'a';
  315. for ( uint subproc = 0; subproc < fNSubProcess; subproc++ )
  316. {
  317. fTxHeader.fSubCycle = subproc;
  318. if ( subproc == ( fNSubProcess - 1 ) )
  319. fTxHeader.fIsLastPckt = 'y';
  320. fTxHeader.fPacketSize = fAudioTxLen;
  321. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  322. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  323. tx_bytes = Send ( fTxHeader.fPacketSize, 0 );
  324. if ( tx_bytes == SOCKET_ERROR )
  325. return tx_bytes;
  326. }
  327. }
  328. return 0;
  329. }
  330. }