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.

877 lines
36KB

  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 "JackNetDriver.h"
  17. #include "JackEngineControl.h"
  18. #include "JackClientControl.h"
  19. #include "JackGraphManager.h"
  20. #include "JackDriverLoader.h"
  21. #include "JackThreadedDriver.h"
  22. #include "JackWaitThreadedDriver.h"
  23. #include "JackException.h"
  24. #define DEFAULT_MULTICAST_IP "225.3.19.154"
  25. #define DEFAULT_PORT 19000
  26. using namespace std;
  27. namespace Jack
  28. {
  29. #ifdef JACK_MONITOR
  30. uint JackNetDriver::fMeasureCnt = 128;
  31. uint JackNetDriver::fMeasurePoints = 4;
  32. string JackNetDriver::fMonitorFieldNames[] =
  33. {
  34. string ( "end of read" ),
  35. string ( "start of write" ),
  36. string ( "sync send" ),
  37. string ( "end of write" )
  38. };
  39. uint JackNetDriver::fMonitorPlotOptionsCnt = 2;
  40. string JackNetDriver::fMonitorPlotOptions[] =
  41. {
  42. string ( "set xlabel \"audio cycles\"" ),
  43. string ( "set ylabel \"% of audio cycle\"" )
  44. };
  45. #endif
  46. JackNetDriver::JackNetDriver ( const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table,
  47. const char* ip, int port, int mtu, int midi_input_ports, int midi_output_ports,
  48. const char* net_name, uint transport_sync, char network_master_mode )
  49. : JackAudioDriver ( name, alias, engine, table ), fSocket ( ip, port )
  50. {
  51. jack_log ( "JackNetDriver::JackNetDriver ip %s, port %d", ip, port );
  52. fMulticastIP = new char[strlen ( ip ) + 1];
  53. strcpy ( fMulticastIP, ip );
  54. fParams.fMtu = mtu;
  55. fParams.fSendMidiChannels = midi_input_ports;
  56. fParams.fReturnMidiChannels = midi_output_ports;
  57. strcpy ( fParams.fName, net_name );
  58. fSocket.GetName ( fParams.fSlaveNetName );
  59. fParams.fTransportSync = transport_sync;
  60. fParams.fNetworkMasterMode = network_master_mode;
  61. #ifdef JACK_MONITOR
  62. fMonitor = NULL;
  63. fMeasure = NULL;
  64. #endif
  65. }
  66. JackNetDriver::~JackNetDriver()
  67. {
  68. fSocket.Close();
  69. SocketAPIEnd();
  70. delete fNetAudioCaptureBuffer;
  71. delete fNetAudioPlaybackBuffer;
  72. delete fNetMidiCaptureBuffer;
  73. delete fNetMidiPlaybackBuffer;
  74. delete[] fTxBuffer;
  75. delete[] fRxBuffer;
  76. delete[] fMulticastIP;
  77. delete[] fMidiCapturePortList;
  78. delete[] fMidiPlaybackPortList;
  79. #ifdef JACK_MONITOR
  80. delete[] fMeasure;
  81. delete fMonitor;
  82. #endif
  83. }
  84. //*************************************initialization***********************************************************************
  85. int JackNetDriver::Open ( jack_nframes_t buffer_size, jack_nframes_t samplerate, bool capturing, bool playing,
  86. int inchannels, int outchannels, bool monitor,
  87. const char* capture_driver_name, const char* playback_driver_name,
  88. jack_nframes_t capture_latency, jack_nframes_t playback_latency )
  89. {
  90. int res = JackAudioDriver::Open ( buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor,
  91. capture_driver_name, playback_driver_name, capture_latency, playback_latency );
  92. fEngineControl->fPeriod = 0;
  93. fEngineControl->fComputation = 500 * 1000;
  94. fEngineControl->fConstraint = 500 * 1000;
  95. return res;
  96. }
  97. #ifdef JACK_MONITOR
  98. int JackNetDriver::Close()
  99. {
  100. if ( fMonitor )
  101. fMonitor->Save();
  102. return JackDriver::Close();
  103. }
  104. #endif
  105. int JackNetDriver::Attach()
  106. {
  107. return 0;
  108. }
  109. int JackNetDriver::Detach()
  110. {
  111. return 0;
  112. }
  113. bool JackNetDriver::Init()
  114. {
  115. jack_log ( "JackNetDriver::Init()" );
  116. //new loading, but existing socket, restart the driver
  117. if ( fSocket.IsSocket() )
  118. Restart();
  119. //set the parameters to send
  120. strcpy ( fParams.fPacketType, "params" );
  121. fParams.fProtocolVersion = 'a';
  122. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  123. fParams.fSendAudioChannels = fCaptureChannels;
  124. fParams.fReturnAudioChannels = fPlaybackChannels;
  125. fParams.fSlaveSyncMode = fEngineControl->fSyncMode;
  126. //is transport sync ?
  127. if ( fParams.fTransportSync )
  128. jack_info ( "NetDriver started with Master's Transport Sync." );
  129. //init loop : get a master and start, do it until connection is ok
  130. net_status_t status;
  131. do
  132. {
  133. //first, get a master, do it until a valid connection is running
  134. jack_info ( "Initializing Net Driver..." );
  135. do
  136. {
  137. status = GetNetMaster();
  138. if ( status == NET_SOCKET_ERROR )
  139. return false;
  140. }
  141. while ( status != NET_CONNECTED );
  142. //then tell the master we are ready
  143. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  144. status = SendMasterStartSync();
  145. if ( status == NET_ERROR )
  146. return false;
  147. }
  148. while ( status != NET_ROLLING );
  149. //driver parametering
  150. if ( SetParams() )
  151. {
  152. jack_error ( "Fatal error : can't alloc net driver ports." );
  153. return false;
  154. }
  155. //init done, display parameters
  156. SessionParamsDisplay ( &fParams );
  157. return true;
  158. }
  159. net_status_t JackNetDriver::GetNetMaster()
  160. {
  161. jack_log ( "JackNetDriver::GetNetMaster()" );
  162. //utility
  163. session_params_t params;
  164. int us_timeout = 2000000;
  165. int rx_bytes = 0;
  166. unsigned char loop = 0;
  167. //socket
  168. if ( fSocket.NewSocket() == SOCKET_ERROR )
  169. {
  170. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  171. return NET_SOCKET_ERROR;
  172. }
  173. //bind the socket
  174. if ( fSocket.Bind() == SOCKET_ERROR )
  175. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  176. //timeout on receive
  177. if ( fSocket.SetTimeOut ( us_timeout ) == SOCKET_ERROR )
  178. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  179. //disable local loop
  180. if ( fSocket.SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof ( loop ) ) == SOCKET_ERROR )
  181. jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
  182. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  183. jack_info ( "Waiting for a master..." );
  184. do
  185. {
  186. //send 'available'
  187. if ( fSocket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  188. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  189. //filter incoming packets : don't exit while no error is detected
  190. rx_bytes = fSocket.CatchHost ( &params, sizeof ( session_params_t ), 0 );
  191. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  192. {
  193. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  194. return NET_RECV_ERROR;
  195. }
  196. }
  197. while ( strcmp ( params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &params ) != SLAVE_SETUP ) );
  198. //connect the socket
  199. if ( fSocket.Connect() == SOCKET_ERROR )
  200. {
  201. jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
  202. return NET_CONNECT_ERROR;
  203. }
  204. //everything is OK, copy parameters and return
  205. fParams = params;
  206. return NET_CONNECTED;
  207. }
  208. net_status_t JackNetDriver::SendMasterStartSync()
  209. {
  210. jack_log ( "JackNetDriver::GetNetMasterStartSync()" );
  211. //tell the master to start
  212. SetPacketType ( &fParams, START_MASTER );
  213. if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
  214. {
  215. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  216. return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
  217. }
  218. return NET_ROLLING;
  219. }
  220. void JackNetDriver::Restart()
  221. {
  222. jack_log ( "JackNetDriver::Restart" );
  223. jack_info ( "Restarting driver..." );
  224. delete[] fTxBuffer;
  225. fTxBuffer = NULL;
  226. delete[] fRxBuffer;
  227. fRxBuffer = NULL;
  228. delete fNetAudioCaptureBuffer;
  229. fNetAudioCaptureBuffer = NULL;
  230. delete fNetAudioPlaybackBuffer;
  231. fNetAudioPlaybackBuffer = NULL;
  232. delete fNetMidiCaptureBuffer;
  233. fNetMidiCaptureBuffer = NULL;
  234. delete fNetMidiPlaybackBuffer;
  235. fNetMidiPlaybackBuffer = NULL;
  236. FreePorts();
  237. delete[] fMidiCapturePortList;
  238. fMidiCapturePortList = NULL;
  239. delete[] fMidiPlaybackPortList;
  240. fMidiPlaybackPortList = NULL;
  241. #ifdef JACK_MONITOR
  242. delete[] fMeasure;
  243. fMeasure = NULL;
  244. delete fMonitor;
  245. fMonitor = NULL;
  246. #endif
  247. }
  248. int JackNetDriver::SetParams()
  249. {
  250. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  251. JackAudioDriver::SetBufferSize ( fParams.fPeriodSize );
  252. JackAudioDriver::SetSampleRate ( fParams.fSampleRate );
  253. JackDriver::NotifyBufferSize ( fParams.fPeriodSize );
  254. JackDriver::NotifySampleRate ( fParams.fSampleRate );
  255. //allocate midi ports lists
  256. fMidiCapturePortList = new jack_port_id_t [fParams.fSendMidiChannels];
  257. fMidiPlaybackPortList = new jack_port_id_t [fParams.fReturnMidiChannels];
  258. //register jack ports
  259. if ( AllocPorts() != 0 )
  260. {
  261. jack_error ( "Can't allocate ports." );
  262. return -1;
  263. }
  264. //TX header init
  265. strcpy ( fTxHeader.fPacketType, "header" );
  266. fTxHeader.fDataStream = 'r';
  267. fTxHeader.fID = fParams.fID;
  268. fTxHeader.fCycle = 0;
  269. fTxHeader.fSubCycle = 0;
  270. fTxHeader.fMidiDataSize = 0;
  271. fTxHeader.fBitdepth = fParams.fBitdepth;
  272. //RX header init
  273. strcpy ( fRxHeader.fPacketType, "header" );
  274. fRxHeader.fDataStream = 's';
  275. fRxHeader.fID = fParams.fID;
  276. fRxHeader.fCycle = 0;
  277. fRxHeader.fSubCycle = 0;
  278. fRxHeader.fMidiDataSize = 0;
  279. fRxHeader.fBitdepth = fParams.fBitdepth;
  280. //network buffers
  281. fTxBuffer = new char[fParams.fMtu];
  282. fRxBuffer = new char[fParams.fMtu];
  283. //net audio/midi buffers
  284. fTxData = fTxBuffer + sizeof ( packet_header_t );
  285. fRxData = fRxBuffer + sizeof ( packet_header_t );
  286. //midi net buffers
  287. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  288. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  289. //audio net buffers
  290. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  291. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  292. //audio netbuffer length
  293. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  294. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  295. //payload size
  296. fPayloadSize = fParams.fMtu - sizeof ( packet_header_t );
  297. //monitor
  298. #ifdef JACK_MONITOR
  299. string plot_name = string ( fParams.fName );
  300. plot_name += string ( "_slave" );
  301. plot_name += ( fEngineControl->fSyncMode ) ? string ( "_sync" ) : string ( "_async" );
  302. plot_name += ( fParams.fNetworkMasterMode == 'f' ) ? string ( "_fast-network" ) : string ( "_slow-network" );
  303. fMonitor = new JackGnuPlotMonitor<float> ( JackNetDriver::fMeasureCnt, JackNetDriver::fMeasurePoints, plot_name );
  304. fMeasure = new float[JackNetDriver::fMeasurePoints];
  305. fMonitor->SetPlotFile ( JackNetDriver::fMonitorPlotOptions, JackNetDriver::fMonitorPlotOptionsCnt,
  306. JackNetDriver::fMonitorFieldNames, JackNetDriver::fMeasurePoints );
  307. #endif
  308. return 0;
  309. }
  310. int JackNetDriver::AllocPorts()
  311. {
  312. jack_log ( "JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate );
  313. JackPort* port;
  314. jack_port_id_t port_id;
  315. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  316. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  317. unsigned long port_flags;
  318. int audio_port_index;
  319. uint midi_port_index;
  320. //audio
  321. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  322. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  323. {
  324. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, audio_port_index + 1 );
  325. snprintf ( name, sizeof ( name ) - 1, "%s:capture_%d", fClientControl.fName, audio_port_index + 1 );
  326. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  327. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  328. {
  329. jack_error ( "driver: cannot register port for %s", name );
  330. return -1;
  331. }
  332. port = fGraphManager->GetPort ( port_id );
  333. port->SetAlias ( alias );
  334. port->SetLatency ( fEngineControl->fBufferSize );
  335. fCapturePortList[audio_port_index] = port_id;
  336. jack_log ( "JackNetDriver::AllocPorts() fCapturePortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  337. }
  338. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  339. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  340. {
  341. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, audio_port_index + 1 );
  342. snprintf ( name, sizeof ( name ) - 1, "%s:playback_%d",fClientControl.fName, audio_port_index + 1 );
  343. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  344. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  345. {
  346. jack_error ( "driver: cannot register port for %s", name );
  347. return -1;
  348. }
  349. port = fGraphManager->GetPort ( port_id );
  350. port->SetAlias ( alias );
  351. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  352. fPlaybackPortList[audio_port_index] = port_id;
  353. jack_log ( "JackNetDriver::AllocPorts() fPlaybackPortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  354. }
  355. //midi
  356. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  357. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  358. {
  359. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, midi_port_index + 1 );
  360. snprintf ( name, sizeof ( name ) - 1, "%s:midi_capture_%d", fClientControl.fName, midi_port_index + 1 );
  361. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  362. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  363. {
  364. jack_error ( "driver: cannot register port for %s", name );
  365. return -1;
  366. }
  367. port = fGraphManager->GetPort ( port_id );
  368. port->SetLatency ( fEngineControl->fBufferSize );
  369. fMidiCapturePortList[midi_port_index] = port_id;
  370. jack_log ( "JackNetDriver::AllocPorts() fMidiCapturePortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  371. }
  372. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  373. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  374. {
  375. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, midi_port_index + 1 );
  376. snprintf ( name, sizeof ( name ) - 1, "%s:midi_playback_%d", fClientControl.fName, midi_port_index + 1 );
  377. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  378. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  379. {
  380. jack_error ( "driver: cannot register port for %s", name );
  381. return -1;
  382. }
  383. port = fGraphManager->GetPort ( port_id );
  384. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  385. fMidiPlaybackPortList[midi_port_index] = port_id;
  386. jack_log ( "JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  387. }
  388. return 0;
  389. }
  390. int JackNetDriver::FreePorts()
  391. {
  392. jack_log ( "JackNetDriver::FreePorts" );
  393. int audio_port_index;
  394. uint midi_port_index;
  395. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  396. fGraphManager->ReleasePort ( fClientControl.fRefNum, fCapturePortList[audio_port_index] );
  397. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  398. fGraphManager->ReleasePort ( fClientControl.fRefNum, fPlaybackPortList[audio_port_index] );
  399. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  400. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiCapturePortList[midi_port_index] );
  401. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  402. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiPlaybackPortList[midi_port_index] );
  403. return 0;
  404. }
  405. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer ( int port_index )
  406. {
  407. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiCapturePortList[port_index], fEngineControl->fBufferSize ) );
  408. }
  409. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer ( int port_index )
  410. {
  411. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize ) );
  412. }
  413. int JackNetDriver::SetSyncPacket()
  414. {
  415. if ( fParams.fTransportSync )
  416. {
  417. //set the TransportData
  418. //copy to TxBuffer
  419. memcpy ( fTxData, &fTransportData, sizeof ( net_transport_data_t ) );
  420. }
  421. return 0;
  422. }
  423. //***********************************network operations*************************************************************
  424. int JackNetDriver::Recv ( size_t size, int flags )
  425. {
  426. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  427. //handle errors
  428. if ( rx_bytes == SOCKET_ERROR )
  429. {
  430. net_error_t error = fSocket.GetError();
  431. //no data isn't really an error in realtime processing, so just return 0
  432. if ( error == NET_NO_DATA )
  433. jack_error ( "No data, is the master still running ?" );
  434. //if a network error occurs, this exception will restart the driver
  435. else if ( error == NET_CONN_ERROR )
  436. {
  437. jack_error ( "Connection lost." );
  438. throw JackDriverException();
  439. }
  440. else
  441. jack_error ( "Fatal error in receive : %s", StrError ( NET_ERROR_CODE ) );
  442. }
  443. return rx_bytes;
  444. }
  445. int JackNetDriver::Send ( size_t size, int flags )
  446. {
  447. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  448. //handle errors
  449. if ( tx_bytes == SOCKET_ERROR )
  450. {
  451. net_error_t error = fSocket.GetError();
  452. //if a network error occurs, this exception will restart the driver
  453. if ( error == NET_CONN_ERROR )
  454. {
  455. jack_error ( "Connection lost." );
  456. throw JackDriverException();
  457. }
  458. else
  459. jack_error ( "Fatal error in send : %s", StrError ( NET_ERROR_CODE ) );
  460. }
  461. return tx_bytes;
  462. }
  463. //*************************************process************************************************************************
  464. int JackNetDriver::Read()
  465. {
  466. int rx_bytes;
  467. uint recvd_midi_pckt = 0;
  468. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  469. fRxHeader.fIsLastPckt = 'n';
  470. uint midi_port_index;
  471. int audio_port_index;
  472. //buffers
  473. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  474. fNetMidiCaptureBuffer->SetBuffer ( midi_port_index, GetMidiInputBuffer ( midi_port_index ) );
  475. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  476. fNetAudioCaptureBuffer->SetBuffer ( audio_port_index, GetInputBuffer ( audio_port_index ) );
  477. #ifdef JACK_MONITOR
  478. fMeasureId = 0;
  479. #endif
  480. //receive sync (launch the cycle)
  481. do
  482. {
  483. rx_bytes = Recv ( fParams.fMtu, 0 );
  484. //connection issue, send will detect it, so don't skip the cycle (return 0)
  485. if ( rx_bytes == SOCKET_ERROR )
  486. return 0;
  487. }
  488. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  489. //take the time at the beginning of the cycle
  490. JackDriver::CycleTakeBeginTime();
  491. //audio, midi or sync if driver is late
  492. if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels )
  493. {
  494. do
  495. {
  496. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  497. //error here, problem with recv, just skip the cycle (return -1)
  498. if ( rx_bytes == SOCKET_ERROR )
  499. return rx_bytes;
  500. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  501. {
  502. switch ( rx_head->fDataType )
  503. {
  504. case 'm': //midi
  505. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  506. fRxHeader.fCycle = rx_head->fCycle;
  507. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  508. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  509. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  510. fNetMidiCaptureBuffer->RenderToJackPorts();
  511. break;
  512. case 'a': //audio
  513. rx_bytes = Recv ( rx_head->fPacketSize, 0 );
  514. if ( !IsNextPacket ( &fRxHeader, rx_head, fNSubProcess ) )
  515. jack_error ( "Packet(s) missing..." );
  516. fRxHeader.fCycle = rx_head->fCycle;
  517. fRxHeader.fSubCycle = rx_head->fSubCycle;
  518. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  519. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  520. break;
  521. case 's': //sync
  522. jack_info ( "NetDriver : driver overloaded, skipping receive." );
  523. fRxHeader.fCycle = rx_head->fCycle;
  524. return 0;
  525. }
  526. }
  527. }
  528. while ( fRxHeader.fIsLastPckt != 'y' );
  529. }
  530. fRxHeader.fCycle = rx_head->fCycle;
  531. #ifdef JACK_MONITOR
  532. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  533. #endif
  534. return 0;
  535. }
  536. int JackNetDriver::Write()
  537. {
  538. uint midi_port_index;
  539. int tx_bytes, audio_port_index;
  540. //tx header
  541. if ( fEngineControl->fSyncMode )
  542. fTxHeader.fCycle = fRxHeader.fCycle;
  543. else
  544. fTxHeader.fCycle++;
  545. fTxHeader.fSubCycle = 0;
  546. fTxHeader.fIsLastPckt = 'n';
  547. //buffers
  548. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  549. fNetMidiPlaybackBuffer->SetBuffer ( midi_port_index, GetMidiOutputBuffer ( midi_port_index ) );
  550. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  551. fNetAudioPlaybackBuffer->SetBuffer ( audio_port_index, GetOutputBuffer ( audio_port_index ) );
  552. #ifdef JACK_MONITOR
  553. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  554. #endif
  555. //sync
  556. fTxHeader.fDataType = 's';
  557. if ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels )
  558. fTxHeader.fIsLastPckt = 'y';
  559. fTxHeader.fPacketSize = fParams.fMtu;
  560. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  561. memset ( fTxData, 0, fPayloadSize );
  562. SetSyncPacket();
  563. tx_bytes = Send ( fTxHeader.fPacketSize, 0 );
  564. if ( tx_bytes == SOCKET_ERROR )
  565. return tx_bytes;
  566. #ifdef JACK_MONITOR
  567. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  568. #endif
  569. //midi
  570. if ( fParams.fReturnMidiChannels )
  571. {
  572. fTxHeader.fDataType = 'm';
  573. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  574. fTxHeader.fNMidiPckt = GetNMidiPckt ( &fParams, fTxHeader.fMidiDataSize );
  575. for ( uint subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  576. {
  577. fTxHeader.fSubCycle = subproc;
  578. if ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels )
  579. fTxHeader.fIsLastPckt = 'y';
  580. fTxHeader.fPacketSize = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  581. fTxHeader.fPacketSize += sizeof ( packet_header_t );
  582. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  583. tx_bytes = Send ( fTxHeader.fPacketSize, 0 );
  584. if ( tx_bytes == SOCKET_ERROR )
  585. return tx_bytes;
  586. }
  587. }
  588. //audio
  589. if ( fParams.fReturnAudioChannels )
  590. {
  591. fTxHeader.fDataType = 'a';
  592. for ( uint subproc = 0; subproc < fNSubProcess; subproc++ )
  593. {
  594. fTxHeader.fSubCycle = subproc;
  595. if ( subproc == ( fNSubProcess - 1 ) )
  596. fTxHeader.fIsLastPckt = 'y';
  597. fTxHeader.fPacketSize = fAudioTxLen;
  598. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  599. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  600. tx_bytes = Send ( fTxHeader.fPacketSize, 0 );
  601. if ( tx_bytes == SOCKET_ERROR )
  602. return tx_bytes;
  603. }
  604. }
  605. #ifdef JACK_MONITOR
  606. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  607. fMonitor->Write ( fMeasure );
  608. #endif
  609. return 0;
  610. }
  611. //*************************************loader*******************************************************
  612. #ifdef __cplusplus
  613. extern "C"
  614. {
  615. #endif
  616. EXPORT jack_driver_desc_t* driver_get_descriptor ()
  617. {
  618. jack_driver_desc_t* desc = ( jack_driver_desc_t* ) calloc ( 1, sizeof ( jack_driver_desc_t ) );
  619. strcpy ( desc->name, "net" );
  620. desc->nparams = 10;
  621. desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );
  622. int i = 0;
  623. strcpy ( desc->params[i].name, "multicast_ip" );
  624. desc->params[i].character = 'a';
  625. desc->params[i].type = JackDriverParamString;
  626. strcpy ( desc->params[i].value.str, DEFAULT_MULTICAST_IP );
  627. strcpy ( desc->params[i].short_desc, "Multicast Address" );
  628. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  629. i++;
  630. strcpy ( desc->params[i].name, "udp_net_port" );
  631. desc->params[i].character = 'p';
  632. desc->params[i].type = JackDriverParamInt;
  633. desc->params[i].value.i = 19000;
  634. strcpy ( desc->params[i].short_desc, "UDP port" );
  635. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  636. i++;
  637. strcpy ( desc->params[i].name, "mtu" );
  638. desc->params[i].character = 'M';
  639. desc->params[i].type = JackDriverParamInt;
  640. desc->params[i].value.i = 1500;
  641. strcpy ( desc->params[i].short_desc, "MTU to the master" );
  642. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  643. i++;
  644. strcpy ( desc->params[i].name, "input_ports" );
  645. desc->params[i].character = 'C';
  646. desc->params[i].type = JackDriverParamInt;
  647. desc->params[i].value.i = 2;
  648. strcpy ( desc->params[i].short_desc, "Number of audio input ports" );
  649. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  650. i++;
  651. strcpy ( desc->params[i].name, "output_ports" );
  652. desc->params[i].character = 'P';
  653. desc->params[i].type = JackDriverParamInt;
  654. desc->params[i].value.i = 2;
  655. strcpy ( desc->params[i].short_desc, "Number of audio output ports" );
  656. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  657. i++;
  658. strcpy ( desc->params[i].name, "midi_in_ports" );
  659. desc->params[i].character = 'i';
  660. desc->params[i].type = JackDriverParamInt;
  661. desc->params[i].value.i = 0;
  662. strcpy ( desc->params[i].short_desc, "Number of midi input ports" );
  663. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  664. i++;
  665. strcpy ( desc->params[i].name, "midi_out_ports" );
  666. desc->params[i].character = 'o';
  667. desc->params[i].type = JackDriverParamUInt;
  668. desc->params[i].value.i = 0;
  669. strcpy ( desc->params[i].short_desc, "Number of midi output ports" );
  670. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  671. i++;
  672. strcpy ( desc->params[i].name, "client_name" );
  673. desc->params[i].character = 'n';
  674. desc->params[i].type = JackDriverParamString;
  675. strcpy ( desc->params[i].value.str, "'hostname'" );
  676. strcpy ( desc->params[i].short_desc, "Name of the jack client" );
  677. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  678. i++;
  679. strcpy ( desc->params[i].name, "transport_sync" );
  680. desc->params[i].character = 't';
  681. desc->params[i].type = JackDriverParamUInt;
  682. desc->params[i].value.ui = 1U;
  683. strcpy ( desc->params[i].short_desc, "Sync transport with master's" );
  684. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  685. i++;
  686. strcpy ( desc->params[i].name, "network_mode" );
  687. desc->params[i].character = 'N';
  688. desc->params[i].type = JackDriverParamChar;
  689. desc->params[i].value.ui = 's';
  690. strcpy ( desc->params[i].short_desc, "Slow network add 1 cycle latency" );
  691. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  692. return desc;
  693. }
  694. EXPORT Jack::JackDriverClientInterface* driver_initialize ( Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params )
  695. {
  696. if ( SocketAPIInit() < 0 )
  697. {
  698. jack_error ( "Can't init Socket API, exiting..." );
  699. return NULL;
  700. }
  701. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  702. char name[JACK_CLIENT_NAME_SIZE];
  703. GetHostName ( name, JACK_CLIENT_NAME_SIZE );
  704. int udp_port = DEFAULT_PORT;
  705. int mtu = 1500;
  706. uint transport_sync = 1;
  707. jack_nframes_t period_size = 128;
  708. jack_nframes_t sample_rate = 48000;
  709. int audio_capture_ports = 2;
  710. int audio_playback_ports = 2;
  711. int midi_input_ports = 0;
  712. int midi_output_ports = 0;
  713. bool monitor = false;
  714. char network_master_mode = 's';
  715. const JSList* node;
  716. const jack_driver_param_t* param;
  717. for ( node = params; node; node = jack_slist_next ( node ) )
  718. {
  719. param = ( const jack_driver_param_t* ) node->data;
  720. switch ( param->character )
  721. {
  722. case 'a' :
  723. multicast_ip = strdup ( param->value.str );
  724. break;
  725. case 'p':
  726. udp_port = param->value.ui;
  727. break;
  728. case 'M':
  729. mtu = param->value.i;
  730. break;
  731. case 'C':
  732. audio_capture_ports = param->value.i;
  733. break;
  734. case 'P':
  735. audio_playback_ports = param->value.i;
  736. break;
  737. case 'i':
  738. midi_input_ports = param->value.i;
  739. break;
  740. case 'o':
  741. midi_output_ports = param->value.i;
  742. break;
  743. case 'n' :
  744. strncpy ( name, param->value.str, JACK_CLIENT_NAME_SIZE );
  745. break;
  746. case 't' :
  747. transport_sync = param->value.ui;
  748. break;
  749. case 'N' :
  750. switch ( param->value.ui )
  751. {
  752. case 's' :
  753. network_master_mode = 's';
  754. break;
  755. case 'f' :
  756. network_master_mode = 'f';
  757. break;
  758. default :
  759. network_master_mode = 's';
  760. break;
  761. }
  762. break;
  763. }
  764. }
  765. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver (
  766. new Jack::JackNetDriver ( "system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  767. midi_input_ports, midi_output_ports, name, transport_sync, network_master_mode ) );
  768. if ( driver->Open ( period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports,
  769. monitor, "from_master_", "to_master_", 0, 0 ) == 0 )
  770. return driver;
  771. delete driver;
  772. return NULL;
  773. }
  774. #ifdef __cplusplus
  775. }
  776. #endif
  777. }