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.

880 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. fMonitor = new JackGnuPlotMonitor<float> ( JackNetDriver::fMeasureCnt, JackNetDriver::fMeasurePoints, plot_name );
  303. fMeasure = new float[JackNetDriver::fMeasurePoints];
  304. fMonitor->SetPlotFile ( JackNetDriver::fMonitorPlotOptions, JackNetDriver::fMonitorPlotOptionsCnt,
  305. JackNetDriver::fMonitorFieldNames, JackNetDriver::fMeasurePoints );
  306. #endif
  307. return 0;
  308. }
  309. int JackNetDriver::AllocPorts()
  310. {
  311. jack_log ( "JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate );
  312. JackPort* port;
  313. jack_port_id_t port_id;
  314. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  315. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  316. unsigned long port_flags;
  317. int audio_port_index;
  318. uint midi_port_index;
  319. //audio
  320. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  321. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  322. {
  323. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, audio_port_index + 1 );
  324. snprintf ( name, sizeof ( name ) - 1, "%s:capture_%d", fClientControl.fName, audio_port_index + 1 );
  325. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  326. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  327. {
  328. jack_error ( "driver: cannot register port for %s", name );
  329. return -1;
  330. }
  331. port = fGraphManager->GetPort ( port_id );
  332. port->SetAlias ( alias );
  333. port->SetLatency ( fEngineControl->fBufferSize );
  334. fCapturePortList[audio_port_index] = port_id;
  335. jack_log ( "JackNetDriver::AllocPorts() fCapturePortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  336. }
  337. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  338. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  339. {
  340. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, audio_port_index + 1 );
  341. snprintf ( name, sizeof ( name ) - 1, "%s:playback_%d",fClientControl.fName, audio_port_index + 1 );
  342. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  343. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  344. {
  345. jack_error ( "driver: cannot register port for %s", name );
  346. return -1;
  347. }
  348. port = fGraphManager->GetPort ( port_id );
  349. port->SetAlias ( alias );
  350. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  351. fPlaybackPortList[audio_port_index] = port_id;
  352. jack_log ( "JackNetDriver::AllocPorts() fPlaybackPortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  353. }
  354. //midi
  355. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  356. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  357. {
  358. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, midi_port_index + 1 );
  359. snprintf ( name, sizeof ( name ) - 1, "%s:midi_capture_%d", fClientControl.fName, midi_port_index + 1 );
  360. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  361. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  362. {
  363. jack_error ( "driver: cannot register port for %s", name );
  364. return -1;
  365. }
  366. port = fGraphManager->GetPort ( port_id );
  367. port->SetLatency ( fEngineControl->fBufferSize );
  368. fMidiCapturePortList[midi_port_index] = port_id;
  369. jack_log ( "JackNetDriver::AllocPorts() fMidiCapturePortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  370. }
  371. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  372. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  373. {
  374. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, midi_port_index + 1 );
  375. snprintf ( name, sizeof ( name ) - 1, "%s:midi_playback_%d", fClientControl.fName, midi_port_index + 1 );
  376. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  377. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  378. {
  379. jack_error ( "driver: cannot register port for %s", name );
  380. return -1;
  381. }
  382. port = fGraphManager->GetPort ( port_id );
  383. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  384. fMidiPlaybackPortList[midi_port_index] = port_id;
  385. jack_log ( "JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  386. }
  387. return 0;
  388. }
  389. int JackNetDriver::FreePorts()
  390. {
  391. jack_log ( "JackNetDriver::FreePorts" );
  392. int audio_port_index;
  393. uint midi_port_index;
  394. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  395. fGraphManager->ReleasePort ( fClientControl.fRefNum, fCapturePortList[audio_port_index] );
  396. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  397. fGraphManager->ReleasePort ( fClientControl.fRefNum, fPlaybackPortList[audio_port_index] );
  398. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  399. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiCapturePortList[midi_port_index] );
  400. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  401. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiPlaybackPortList[midi_port_index] );
  402. return 0;
  403. }
  404. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer ( int port_index )
  405. {
  406. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiCapturePortList[port_index], fEngineControl->fBufferSize ) );
  407. }
  408. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer ( int port_index )
  409. {
  410. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize ) );
  411. }
  412. int JackNetDriver::SetSyncPacket()
  413. {
  414. if ( fParams.fTransportSync )
  415. {
  416. //set the TransportData
  417. //copy to TxBuffer
  418. memcpy ( fTxData, &fTransportData, sizeof ( net_transport_data_t ) );
  419. }
  420. return 0;
  421. }
  422. //***********************************network operations*************************************************************
  423. int JackNetDriver::Recv ( size_t size, int flags )
  424. {
  425. int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
  426. //handle errors
  427. if ( rx_bytes == SOCKET_ERROR )
  428. {
  429. net_error_t error = fSocket.GetError();
  430. //no data isn't really an error in realtime processing, so just return 0
  431. if ( error == NET_NO_DATA )
  432. jack_error ( "No data, is the master still running ?" );
  433. //if a network error occurs, this exception will restart the driver
  434. else if ( error == NET_CONN_ERROR )
  435. {
  436. jack_error ( "Connection lost." );
  437. throw JackDriverException();
  438. }
  439. else
  440. jack_error ( "Fatal error in receive : %s", StrError ( NET_ERROR_CODE ) );
  441. }
  442. return rx_bytes;
  443. }
  444. int JackNetDriver::Send ( size_t size, int flags )
  445. {
  446. int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
  447. //handle errors
  448. if ( tx_bytes == SOCKET_ERROR )
  449. {
  450. net_error_t error = fSocket.GetError();
  451. //if a network error occurs, this exception will restart the driver
  452. if ( error == NET_CONN_ERROR )
  453. {
  454. jack_error ( "Connection lost." );
  455. throw JackDriverException();
  456. }
  457. else
  458. jack_error ( "Fatal error in send : %s", StrError ( NET_ERROR_CODE ) );
  459. }
  460. return tx_bytes;
  461. }
  462. //*************************************process************************************************************************
  463. int JackNetDriver::Read()
  464. {
  465. int rx_bytes;
  466. uint recvd_midi_pckt = 0;
  467. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  468. fRxHeader.fIsLastPckt = 'n';
  469. uint midi_port_index;
  470. int audio_port_index;
  471. //buffers
  472. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  473. fNetMidiCaptureBuffer->SetBuffer ( midi_port_index, GetMidiInputBuffer ( midi_port_index ) );
  474. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  475. fNetAudioCaptureBuffer->SetBuffer ( audio_port_index, GetInputBuffer ( audio_port_index ) );
  476. //receive sync (launch the cycle)
  477. do
  478. {
  479. rx_bytes = Recv ( sizeof ( packet_header_t ), 0 );
  480. //connection issue, send will detect it, so don't skip the cycle (return 0)
  481. if ( rx_bytes == SOCKET_ERROR )
  482. return 0;
  483. }
  484. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  485. //take the time at the beginning of the cycle
  486. JackDriver::CycleTakeBeginTime();
  487. //audio, midi or sync if driver is late
  488. if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels )
  489. {
  490. do
  491. {
  492. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  493. //error here, problem with recv, just skip the cycle (return -1)
  494. if ( rx_bytes == SOCKET_ERROR )
  495. return rx_bytes;
  496. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  497. {
  498. switch ( rx_head->fDataType )
  499. {
  500. case 'm': //midi
  501. rx_bytes = Recv ( rx_bytes, 0 );
  502. fRxHeader.fCycle = rx_head->fCycle;
  503. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  504. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  505. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  506. fNetMidiCaptureBuffer->RenderToJackPorts();
  507. break;
  508. case 'a': //audio
  509. rx_bytes = Recv ( fAudioRxLen, 0 );
  510. if ( !IsNextPacket ( &fRxHeader, rx_head, fNSubProcess ) )
  511. jack_error ( "Packet(s) missing..." );
  512. fRxHeader.fCycle = rx_head->fCycle;
  513. fRxHeader.fSubCycle = rx_head->fSubCycle;
  514. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  515. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  516. break;
  517. case 's': //sync
  518. jack_info ( "NetDriver : driver overloaded, skipping receive." );
  519. fRxHeader.fCycle = rx_head->fCycle;
  520. return 0;
  521. }
  522. }
  523. }
  524. while ( fRxHeader.fIsLastPckt != 'y' );
  525. }
  526. fRxHeader.fCycle = rx_head->fCycle;
  527. #ifdef JACK_MONITOR
  528. fMeasureId = 0;
  529. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  530. #endif
  531. return 0;
  532. }
  533. int JackNetDriver::Write()
  534. {
  535. uint midi_port_index;
  536. int tx_bytes, copy_size, audio_port_index;
  537. //tx header
  538. if ( fEngineControl->fSyncMode )
  539. fTxHeader.fCycle = fRxHeader.fCycle;
  540. else
  541. fTxHeader.fCycle++;
  542. fTxHeader.fSubCycle = 0;
  543. fTxHeader.fIsLastPckt = 'n';
  544. //buffers
  545. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  546. fNetMidiPlaybackBuffer->SetBuffer ( midi_port_index, GetMidiOutputBuffer ( midi_port_index ) );
  547. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  548. fNetAudioPlaybackBuffer->SetBuffer ( audio_port_index, GetOutputBuffer ( audio_port_index ) );
  549. #ifdef JACK_MONITOR
  550. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  551. #endif
  552. //sync
  553. fTxHeader.fDataType = 's';
  554. if ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels )
  555. fTxHeader.fIsLastPckt = 'y';
  556. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  557. //memset ( fTxData, 0, fPayloadSize );
  558. //SetSyncPacket();
  559. tx_bytes = Send ( sizeof ( packet_header_t ), 0 );
  560. if ( tx_bytes == SOCKET_ERROR )
  561. return tx_bytes;
  562. #ifdef JACK_MONITOR
  563. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  564. #endif
  565. //midi
  566. if ( fParams.fReturnMidiChannels )
  567. {
  568. fTxHeader.fDataType = 'm';
  569. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  570. fTxHeader.fNMidiPckt = GetNMidiPckt ( &fParams, fTxHeader.fMidiDataSize );
  571. for ( uint subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  572. {
  573. fTxHeader.fSubCycle = subproc;
  574. if ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels )
  575. fTxHeader.fIsLastPckt = 'y';
  576. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  577. copy_size = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  578. tx_bytes = Send ( sizeof ( packet_header_t ) + copy_size, 0 );
  579. if ( tx_bytes == SOCKET_ERROR )
  580. return tx_bytes;
  581. }
  582. }
  583. //audio
  584. if ( fParams.fReturnAudioChannels )
  585. {
  586. fTxHeader.fDataType = 'a';
  587. for ( uint subproc = 0; subproc < fNSubProcess; subproc++ )
  588. {
  589. fTxHeader.fSubCycle = subproc;
  590. if ( subproc == ( fNSubProcess - 1 ) )
  591. fTxHeader.fIsLastPckt = 'y';
  592. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  593. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  594. tx_bytes = Send ( fAudioTxLen, 0 );
  595. if ( tx_bytes == SOCKET_ERROR )
  596. return tx_bytes;
  597. }
  598. }
  599. #ifdef JACK_MONITOR
  600. fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f;
  601. fMonitor->Write ( fMeasure );
  602. #endif
  603. return 0;
  604. }
  605. //*************************************loader*******************************************************
  606. #ifdef __cplusplus
  607. extern "C"
  608. {
  609. #endif
  610. EXPORT jack_driver_desc_t* driver_get_descriptor ()
  611. {
  612. jack_driver_desc_t* desc = ( jack_driver_desc_t* ) calloc ( 1, sizeof ( jack_driver_desc_t ) );
  613. strcpy ( desc->name, "net" );
  614. desc->nparams = 11;
  615. desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );
  616. int i = 0;
  617. strcpy ( desc->params[i].name, "multicast_ip" );
  618. desc->params[i].character = 'a';
  619. desc->params[i].type = JackDriverParamString;
  620. strcpy ( desc->params[i].value.str, DEFAULT_MULTICAST_IP );
  621. strcpy ( desc->params[i].short_desc, "Multicast Address" );
  622. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  623. i++;
  624. strcpy ( desc->params[i].name, "udp_net_port" );
  625. desc->params[i].character = 'p';
  626. desc->params[i].type = JackDriverParamInt;
  627. desc->params[i].value.i = 19000;
  628. strcpy ( desc->params[i].short_desc, "UDP port" );
  629. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  630. i++;
  631. strcpy ( desc->params[i].name, "mtu" );
  632. desc->params[i].character = 'M';
  633. desc->params[i].type = JackDriverParamInt;
  634. desc->params[i].value.i = 1500;
  635. strcpy ( desc->params[i].short_desc, "MTU to the master" );
  636. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  637. i++;
  638. strcpy ( desc->params[i].name, "input_ports" );
  639. desc->params[i].character = 'C';
  640. desc->params[i].type = JackDriverParamInt;
  641. desc->params[i].value.i = 2;
  642. strcpy ( desc->params[i].short_desc, "Number of audio input ports" );
  643. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  644. i++;
  645. strcpy ( desc->params[i].name, "output_ports" );
  646. desc->params[i].character = 'P';
  647. desc->params[i].type = JackDriverParamInt;
  648. desc->params[i].value.i = 2;
  649. strcpy ( desc->params[i].short_desc, "Number of audio output ports" );
  650. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  651. i++;
  652. strcpy ( desc->params[i].name, "midi_in_ports" );
  653. desc->params[i].character = 'i';
  654. desc->params[i].type = JackDriverParamInt;
  655. desc->params[i].value.i = 0;
  656. strcpy ( desc->params[i].short_desc, "Number of midi input ports" );
  657. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  658. i++;
  659. strcpy ( desc->params[i].name, "midi_out_ports" );
  660. desc->params[i].character = 'o';
  661. desc->params[i].type = JackDriverParamUInt;
  662. desc->params[i].value.i = 0;
  663. strcpy ( desc->params[i].short_desc, "Number of midi output ports" );
  664. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  665. i++;
  666. strcpy ( desc->params[i].name, "client_name" );
  667. desc->params[i].character = 'n';
  668. desc->params[i].type = JackDriverParamString;
  669. strcpy ( desc->params[i].value.str, "'hostname'" );
  670. strcpy ( desc->params[i].short_desc, "Name of the jack client" );
  671. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  672. i++;
  673. strcpy ( desc->params[i].name, "transport_sync" );
  674. desc->params[i].character = 't';
  675. desc->params[i].type = JackDriverParamUInt;
  676. desc->params[i].value.ui = 1U;
  677. strcpy ( desc->params[i].short_desc, "Sync transport with master's" );
  678. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  679. i++;
  680. strcpy ( desc->params[i].name, "network_master_mode" );
  681. desc->params[i].character = 'm';
  682. desc->params[i].type = JackDriverParamChar;
  683. desc->params[i].value.ui = 's';
  684. strcpy ( desc->params[i].short_desc, "Slow network add 1 cycle latency" );
  685. strcpy ( desc->params[i].long_desc, "'s' for slow, 'f' for fast, default is slow.\
  686. Fast network will make the master waiting for the current cycle return data." );
  687. i++;
  688. strcpy ( desc->params[i].name, "network_slave_mode" );
  689. desc->params[i].character = 's';
  690. desc->params[i].type = JackDriverParamChar;
  691. desc->params[i].value.ui = 's';
  692. strcpy ( desc->params[i].short_desc, "Slow network add 1 cycle latency" );
  693. strcpy ( desc->params[i].long_desc, "'s' for slow, 'f' for fast, default is slow.\
  694. Fast network will make the slave waiting for the first cycle data." );
  695. return desc;
  696. }
  697. EXPORT Jack::JackDriverClientInterface* driver_initialize ( Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params )
  698. {
  699. if ( SocketAPIInit() < 0 )
  700. {
  701. jack_error ( "Can't init Socket API, exiting..." );
  702. return NULL;
  703. }
  704. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  705. char name[JACK_CLIENT_NAME_SIZE];
  706. GetHostName ( name, JACK_CLIENT_NAME_SIZE );
  707. int udp_port = DEFAULT_PORT;
  708. int mtu = 1500;
  709. uint transport_sync = 1;
  710. jack_nframes_t period_size = 128;
  711. jack_nframes_t sample_rate = 48000;
  712. int audio_capture_ports = 2;
  713. int audio_playback_ports = 2;
  714. int midi_input_ports = 0;
  715. int midi_output_ports = 0;
  716. bool monitor = false;
  717. char network_master_mode = 's';
  718. const JSList* node;
  719. const jack_driver_param_t* param;
  720. for ( node = params; node; node = jack_slist_next ( node ) )
  721. {
  722. param = ( const jack_driver_param_t* ) node->data;
  723. switch ( param->character )
  724. {
  725. case 'a' :
  726. multicast_ip = strdup ( param->value.str );
  727. break;
  728. case 'p':
  729. udp_port = param->value.ui;
  730. break;
  731. case 'M':
  732. mtu = param->value.i;
  733. break;
  734. case 'C':
  735. audio_capture_ports = param->value.i;
  736. break;
  737. case 'P':
  738. audio_playback_ports = param->value.i;
  739. break;
  740. case 'i':
  741. midi_input_ports = param->value.i;
  742. break;
  743. case 'o':
  744. midi_output_ports = param->value.i;
  745. break;
  746. case 'n' :
  747. strncpy ( name, param->value.str, JACK_CLIENT_NAME_SIZE );
  748. break;
  749. case 't' :
  750. transport_sync = param->value.ui;
  751. break;
  752. case 'N' :
  753. switch ( param->value.ui )
  754. {
  755. case 's' :
  756. network_master_mode = 's';
  757. break;
  758. case 'f' :
  759. network_master_mode = 'f';
  760. break;
  761. default :
  762. network_master_mode = 's';
  763. break;
  764. }
  765. break;
  766. }
  767. }
  768. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver (
  769. new Jack::JackNetDriver ( "system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  770. midi_input_ports, midi_output_ports, name, transport_sync, network_master_mode ) );
  771. if ( driver->Open ( period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports,
  772. monitor, "from_master_", "to_master_", 0, 0 ) == 0 )
  773. return driver;
  774. delete driver;
  775. return NULL;
  776. }
  777. #ifdef __cplusplus
  778. }
  779. #endif
  780. }