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.

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