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.

843 lines
34KB

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