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.

811 lines
32KB

  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. namespace Jack
  27. {
  28. #ifdef JACK_MONITOR
  29. uint JackNetDriver::fMeasureCnt = 50;
  30. uint JackNetDriver::fMeasurePoints = 5;
  31. uint JackNetDriver::fMonitorPlotOptionsCnt = 2;
  32. std::string JackNetDriver::fMonitorPlotOptions[] =
  33. {
  34. std::string ( "set xlabel \"audio cycles\"" ),
  35. std::string ( "set ylabel \"usecs\"" )
  36. };
  37. std::string JackNetDriver::fMonitorFieldNames[] =
  38. {
  39. std::string ( "cyclestart" ),
  40. std::string ( "read end" ),
  41. std::string ( "write start" ),
  42. std::string ( "sync end" ),
  43. std::string ( "send end" )
  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. fMulticastIP = new char[strlen ( ip ) + 1];
  51. strcpy ( fMulticastIP, ip );
  52. fParams.fMtu = mtu;
  53. fParams.fSendMidiChannels = midi_input_ports;
  54. fParams.fReturnMidiChannels = midi_output_ports;
  55. strcpy ( fParams.fName, net_name );
  56. fSocket.GetName ( fParams.fSlaveNetName );
  57. fParams.fTransportSync = transport_sync;
  58. //monitor
  59. #ifdef JACK_MONITOR
  60. fMonitor = new NetMonitor<jack_time_t> ( JackNetDriver::fMeasureCnt, JackNetDriver::fMeasurePoints );
  61. fMeasure = new jack_time_t[JackNetDriver::fMeasurePoints];
  62. std::string plot_file_name = std::string ( fParams.fName );
  63. fMonitor->SetPlotFile ( plot_file_name, JackNetDriver::fMonitorPlotOptions, JackNetDriver::fMonitorPlotOptionsCnt,
  64. JackNetDriver::fMonitorFieldNames, JackNetDriver::fMeasurePoints );
  65. #endif
  66. }
  67. JackNetDriver::~JackNetDriver()
  68. {
  69. fSocket.Close();
  70. SocketAPIEnd();
  71. delete fNetAudioCaptureBuffer;
  72. delete fNetAudioPlaybackBuffer;
  73. delete fNetMidiCaptureBuffer;
  74. delete fNetMidiPlaybackBuffer;
  75. delete[] fTxBuffer;
  76. delete[] fRxBuffer;
  77. delete[] fMulticastIP;
  78. delete[] fMidiCapturePortList;
  79. delete[] fMidiPlaybackPortList;
  80. #ifdef JACK_MONITOR
  81. delete[] fMeasure;
  82. delete fMonitor;
  83. #endif
  84. }
  85. //*************************************initialization***********************************************************************
  86. int JackNetDriver::Open ( jack_nframes_t buffer_size, jack_nframes_t samplerate, bool capturing, bool playing,
  87. int inchannels, int outchannels, bool monitor,
  88. const char* capture_driver_name, const char* playback_driver_name,
  89. jack_nframes_t capture_latency, jack_nframes_t playback_latency )
  90. {
  91. int res = JackAudioDriver::Open ( buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor,
  92. capture_driver_name, playback_driver_name, capture_latency, playback_latency );
  93. fEngineControl->fPeriod = 0;
  94. fEngineControl->fComputation = 500 * 1000;
  95. fEngineControl->fConstraint = 500 * 1000;
  96. return res;
  97. }
  98. #ifdef JACK_MONITOR
  99. int JackNetDriver::Close()
  100. {
  101. std::string filename = string ( fParams.fName );
  102. fMonitor->Save ( filename );
  103. return JackDriver::Close();
  104. }
  105. #endif
  106. int JackNetDriver::Attach()
  107. {
  108. return 0;
  109. }
  110. int JackNetDriver::Detach()
  111. {
  112. return 0;
  113. }
  114. bool JackNetDriver::Init()
  115. {
  116. jack_log ( "JackNetDriver::Init()" );
  117. //new loading, but existing socket, restart the driver
  118. if ( fSocket.IsSocket() )
  119. Restart();
  120. //set the parameters to send
  121. strcpy ( fParams.fPacketType, "params" );
  122. fParams.fProtocolVersion = 'a';
  123. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  124. fParams.fSendAudioChannels = fCaptureChannels;
  125. fParams.fReturnAudioChannels = fPlaybackChannels;
  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 ms_timeout = 2000;
  165. int rx_bytes = 0;
  166. //socket
  167. if ( fSocket.NewSocket() == SOCKET_ERROR )
  168. {
  169. jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
  170. return NET_SOCKET_ERROR;
  171. }
  172. //bind the socket
  173. if ( fSocket.Bind() == SOCKET_ERROR )
  174. jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
  175. //timeout on receive
  176. if ( fSocket.SetTimeOut ( ms_timeout ) == SOCKET_ERROR )
  177. jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
  178. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  179. jack_info ( "Waiting for a master..." );
  180. do
  181. {
  182. //send 'available'
  183. if ( fSocket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
  184. jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
  185. //filter incoming packets : don't exit while receiving wrong packets
  186. do
  187. {
  188. rx_bytes = fSocket.CatchHost ( &params, sizeof ( session_params_t ), 0 );
  189. if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
  190. {
  191. jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
  192. return NET_RECV_ERROR;
  193. }
  194. }
  195. while ( ( rx_bytes > 0 ) && strcmp ( params.fPacketType, fParams.fPacketType ) );
  196. }
  197. while ( ( 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_info ( "Restarting driver..." );
  223. delete[] fTxBuffer;
  224. delete[] fRxBuffer;
  225. delete fNetAudioCaptureBuffer;
  226. delete fNetAudioPlaybackBuffer;
  227. delete fNetMidiCaptureBuffer;
  228. delete fNetMidiPlaybackBuffer;
  229. FreePorts();
  230. delete[] fMidiCapturePortList;
  231. delete[] fMidiPlaybackPortList;
  232. fTxBuffer = NULL;
  233. fRxBuffer = NULL;
  234. fNetAudioCaptureBuffer = NULL;
  235. fNetAudioPlaybackBuffer = NULL;
  236. fNetMidiCaptureBuffer = NULL;
  237. fNetMidiPlaybackBuffer = NULL;
  238. fMidiCapturePortList = NULL;
  239. fMidiPlaybackPortList = NULL;
  240. }
  241. int JackNetDriver::SetParams()
  242. {
  243. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  244. JackAudioDriver::SetBufferSize(fParams.fPeriodSize);
  245. JackAudioDriver::SetSampleRate(fParams.fSampleRate);
  246. JackDriver::NotifyBufferSize(fParams.fPeriodSize);
  247. JackDriver::NotifySampleRate(fParams.fSampleRate);
  248. //allocate midi ports lists
  249. fMidiCapturePortList = new jack_port_id_t [fParams.fSendMidiChannels];
  250. fMidiPlaybackPortList = new jack_port_id_t [fParams.fReturnMidiChannels];
  251. //register jack ports
  252. if ( AllocPorts() != 0 )
  253. {
  254. jack_error ( "Can't allocate ports." );
  255. return -1;
  256. }
  257. //TX header init
  258. strcpy ( fTxHeader.fPacketType, "header" );
  259. fTxHeader.fDataStream = 'r';
  260. fTxHeader.fID = fParams.fID;
  261. fTxHeader.fCycle = 0;
  262. fTxHeader.fSubCycle = 0;
  263. fTxHeader.fMidiDataSize = 0;
  264. fTxHeader.fBitdepth = fParams.fBitdepth;
  265. //RX header init
  266. strcpy ( fRxHeader.fPacketType, "header" );
  267. fRxHeader.fDataStream = 's';
  268. fRxHeader.fID = fParams.fID;
  269. fRxHeader.fCycle = 0;
  270. fRxHeader.fSubCycle = 0;
  271. fRxHeader.fMidiDataSize = 0;
  272. fRxHeader.fBitdepth = fParams.fBitdepth;
  273. //network buffers
  274. fTxBuffer = new char[fParams.fMtu];
  275. fRxBuffer = new char[fParams.fMtu];
  276. //net audio/midi buffers
  277. fTxData = fTxBuffer + sizeof ( packet_header_t );
  278. fRxData = fRxBuffer + sizeof ( packet_header_t );
  279. //midi net buffers
  280. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  281. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  282. //audio net buffers
  283. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  284. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  285. //audio netbuffer length
  286. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  287. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  288. //payload size
  289. fPayloadSize = fParams.fMtu - sizeof ( packet_header_t );
  290. return 0;
  291. }
  292. int JackNetDriver::AllocPorts()
  293. {
  294. jack_log ( "JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate );
  295. JackPort* port;
  296. jack_port_id_t port_id;
  297. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  298. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  299. unsigned long port_flags;
  300. int audio_port_index;
  301. uint midi_port_index;
  302. //audio
  303. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  304. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  305. {
  306. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, audio_port_index + 1 );
  307. snprintf ( name, sizeof ( name ) - 1, "%s:capture_%d", fClientControl.fName, audio_port_index + 1 );
  308. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  309. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  310. {
  311. jack_error ( "driver: cannot register port for %s", name );
  312. return -1;
  313. }
  314. port = fGraphManager->GetPort ( port_id );
  315. port->SetAlias ( alias );
  316. port->SetLatency ( fEngineControl->fBufferSize );
  317. fCapturePortList[audio_port_index] = port_id;
  318. jack_log ( "JackNetDriver::AllocPorts() fCapturePortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  319. }
  320. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  321. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  322. {
  323. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, audio_port_index + 1 );
  324. snprintf ( name, sizeof ( name ) - 1, "%s:playback_%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 + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  334. fPlaybackPortList[audio_port_index] = port_id;
  335. jack_log ( "JackNetDriver::AllocPorts() fPlaybackPortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  336. }
  337. //midi
  338. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  339. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  340. {
  341. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, midi_port_index + 1 );
  342. snprintf ( name, sizeof ( name ) - 1, "%s:midi_capture_%d", fClientControl.fName, midi_port_index + 1 );
  343. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_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->SetLatency ( fEngineControl->fBufferSize );
  351. fMidiCapturePortList[midi_port_index] = port_id;
  352. jack_log ( "JackNetDriver::AllocPorts() fMidiCapturePortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  353. }
  354. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  355. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  356. {
  357. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, midi_port_index + 1 );
  358. snprintf ( name, sizeof ( name ) - 1, "%s:midi_playback_%d", fClientControl.fName, midi_port_index + 1 );
  359. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  360. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  361. {
  362. jack_error ( "driver: cannot register port for %s", name );
  363. return -1;
  364. }
  365. port = fGraphManager->GetPort ( port_id );
  366. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  367. fMidiPlaybackPortList[midi_port_index] = port_id;
  368. jack_log ( "JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  369. }
  370. return 0;
  371. }
  372. int JackNetDriver::FreePorts()
  373. {
  374. jack_log ( "JackNetDriver::FreePorts" );
  375. int audio_port_index;
  376. uint midi_port_index;
  377. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  378. fGraphManager->ReleasePort ( fClientControl.fRefNum, fCapturePortList[audio_port_index] );
  379. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  380. fGraphManager->ReleasePort ( fClientControl.fRefNum, fPlaybackPortList[audio_port_index] );
  381. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  382. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiCapturePortList[midi_port_index] );
  383. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  384. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiPlaybackPortList[midi_port_index] );
  385. return 0;
  386. }
  387. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer ( int port_index )
  388. {
  389. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiCapturePortList[port_index], fEngineControl->fBufferSize ) );
  390. }
  391. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer ( int port_index )
  392. {
  393. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize ) );
  394. }
  395. int JackNetDriver::SetSyncPacket()
  396. {
  397. if ( fParams.fTransportSync )
  398. {
  399. //set the TransportData
  400. //copy to TxBuffer
  401. memcpy ( fTxData, &fTransportData, sizeof ( net_transport_data_t ) );
  402. }
  403. return 0;
  404. }
  405. int JackNetDriver::Recv ( size_t size, int flags )
  406. {
  407. int rx_bytes;
  408. if ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR )
  409. {
  410. net_error_t error = fSocket.GetError();
  411. if ( error == NET_NO_DATA )
  412. {
  413. jack_error ( "No incoming data, is the master still running ?" );
  414. return 0;
  415. }
  416. else if ( error == NET_CONN_ERROR )
  417. {
  418. throw JackDriverException ( "Connection lost." );
  419. }
  420. else
  421. {
  422. jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) );
  423. return 0;
  424. }
  425. }
  426. return rx_bytes;
  427. }
  428. int JackNetDriver::Send ( size_t size, int flags )
  429. {
  430. int tx_bytes;
  431. if ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR )
  432. {
  433. net_error_t error = fSocket.GetError();
  434. if ( error == NET_CONN_ERROR )
  435. throw JackDriverException ( "Connection lost." );
  436. else
  437. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  438. }
  439. return tx_bytes;
  440. }
  441. //*************************************process************************************************************************
  442. int JackNetDriver::Read()
  443. {
  444. int rx_bytes;
  445. uint recvd_midi_pckt = 0;
  446. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  447. fRxHeader.fIsLastPckt = 'n';
  448. uint midi_port_index;
  449. int audio_port_index;
  450. //buffers
  451. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  452. fNetMidiCaptureBuffer->SetBuffer(midi_port_index, GetMidiInputBuffer ( midi_port_index ));
  453. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  454. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer ( audio_port_index ));
  455. //receive sync (launch the cycle)
  456. do
  457. {
  458. rx_bytes = Recv ( fParams.fMtu, 0 );
  459. if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) )
  460. return rx_bytes;
  461. }
  462. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  463. //take the time at the beginning of the cycle
  464. JackDriver::CycleTakeBeginTime();
  465. #ifdef JACK_MONITOR
  466. fUsecCycleStart = GetMicroSeconds();
  467. fMeasure[0] = GetMicroSeconds() - fUsecCycleStart;
  468. #endif
  469. //audio, midi or sync if driver is late
  470. if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels )
  471. {
  472. do
  473. {
  474. if ( ( rx_bytes = Recv ( fParams.fMtu, MSG_PEEK ) ) == SOCKET_ERROR )
  475. return rx_bytes;
  476. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  477. {
  478. switch ( rx_head->fDataType )
  479. {
  480. case 'm': //midi
  481. rx_bytes = Recv ( rx_bytes, 0 );
  482. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  483. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  484. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  485. fNetMidiCaptureBuffer->RenderToJackPorts();
  486. break;
  487. case 'a': //audio
  488. rx_bytes = Recv ( fAudioRxLen, 0 );
  489. if ( !IsNextPacket ( &fRxHeader, rx_head, fNSubProcess ) )
  490. jack_error ( "Packet(s) missing..." );
  491. fRxHeader.fCycle = rx_head->fCycle;
  492. fRxHeader.fSubCycle = rx_head->fSubCycle;
  493. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  494. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  495. break;
  496. case 's': //sync
  497. jack_info ( "NetDriver : driver overloaded, skipping receive." );
  498. fRxHeader.fCycle = rx_head->fCycle;
  499. return 0;
  500. }
  501. }
  502. }
  503. while ( fRxHeader.fIsLastPckt != 'y' );
  504. }
  505. fRxHeader.fCycle = rx_head->fCycle;
  506. #ifdef JACK_MONITOR
  507. fMeasure[1] = GetMicroSeconds() - fUsecCycleStart;
  508. #endif
  509. return 0;
  510. }
  511. int JackNetDriver::Write()
  512. {
  513. int tx_bytes, copy_size;
  514. fTxHeader.fCycle = fRxHeader.fCycle;
  515. fTxHeader.fSubCycle = 0;
  516. fTxHeader.fIsLastPckt = 'n';
  517. uint midi_port_index;
  518. int audio_port_index;
  519. //buffers
  520. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  521. fNetMidiPlaybackBuffer->SetBuffer(midi_port_index, GetMidiOutputBuffer ( midi_port_index ));
  522. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  523. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, GetOutputBuffer ( audio_port_index ));
  524. #ifdef JACK_MONITOR
  525. fMeasure[2] = GetMicroSeconds() - fUsecCycleStart;
  526. #endif
  527. //sync
  528. fTxHeader.fDataType = 's';
  529. if ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels )
  530. fTxHeader.fIsLastPckt = 'y';
  531. memset ( fTxData, 0, fPayloadSize );
  532. SetSyncPacket();
  533. tx_bytes = Send ( fParams.fMtu, 0 );
  534. #ifdef JACK_MONITOR
  535. fMeasure[3] = GetMicroSeconds() - fUsecCycleStart;
  536. #endif
  537. //midi
  538. if ( fParams.fReturnMidiChannels )
  539. {
  540. fTxHeader.fDataType = 'm';
  541. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  542. fTxHeader.fNMidiPckt = GetNMidiPckt ( &fParams, fTxHeader.fMidiDataSize );
  543. for ( uint subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  544. {
  545. fTxHeader.fSubCycle = subproc;
  546. if ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels )
  547. fTxHeader.fIsLastPckt = 'y';
  548. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  549. copy_size = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  550. tx_bytes = Send ( sizeof ( packet_header_t ) + copy_size, 0 );
  551. }
  552. }
  553. //audio
  554. if ( fParams.fReturnAudioChannels )
  555. {
  556. fTxHeader.fDataType = 'a';
  557. for ( uint subproc = 0; subproc < fNSubProcess; subproc++ )
  558. {
  559. fTxHeader.fSubCycle = subproc;
  560. if ( subproc == ( fNSubProcess - 1 ) )
  561. fTxHeader.fIsLastPckt = 'y';
  562. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  563. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  564. tx_bytes = Send ( fAudioTxLen, 0 );
  565. }
  566. }
  567. #ifdef JACK_MONITOR
  568. fMeasure[4] = GetMicroSeconds() - fUsecCycleStart;
  569. fMonitor->Write ( fMeasure );
  570. #endif
  571. return 0;
  572. }
  573. //*************************************loader*******************************************************
  574. #ifdef __cplusplus
  575. extern "C"
  576. {
  577. #endif
  578. EXPORT jack_driver_desc_t* driver_get_descriptor ()
  579. {
  580. jack_driver_desc_t* desc = ( jack_driver_desc_t* ) calloc ( 1, sizeof ( jack_driver_desc_t ) );
  581. strcpy ( desc->name, "net" );
  582. desc->nparams = 9;
  583. desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );
  584. int i = 0;
  585. strcpy ( desc->params[i].name, "multicast_ip" );
  586. desc->params[i].character = 'a';
  587. desc->params[i].type = JackDriverParamString;
  588. strcpy ( desc->params[i].value.str, DEFAULT_MULTICAST_IP );
  589. strcpy ( desc->params[i].short_desc, "Multicast Address" );
  590. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  591. i++;
  592. strcpy ( desc->params[i].name, "udp_net_port" );
  593. desc->params[i].character = 'p';
  594. desc->params[i].type = JackDriverParamInt;
  595. desc->params[i].value.i = 19000;
  596. strcpy ( desc->params[i].short_desc, "UDP port" );
  597. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  598. i++;
  599. strcpy ( desc->params[i].name, "mtu" );
  600. desc->params[i].character = 'M';
  601. desc->params[i].type = JackDriverParamInt;
  602. desc->params[i].value.i = 1500;
  603. strcpy ( desc->params[i].short_desc, "MTU to the master" );
  604. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  605. i++;
  606. strcpy ( desc->params[i].name, "input_ports" );
  607. desc->params[i].character = 'C';
  608. desc->params[i].type = JackDriverParamInt;
  609. desc->params[i].value.i = 2;
  610. strcpy ( desc->params[i].short_desc, "Number of audio input ports" );
  611. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  612. i++;
  613. strcpy ( desc->params[i].name, "output_ports" );
  614. desc->params[i].character = 'P';
  615. desc->params[i].type = JackDriverParamInt;
  616. desc->params[i].value.i = 2;
  617. strcpy ( desc->params[i].short_desc, "Number of audio output ports" );
  618. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  619. i++;
  620. strcpy ( desc->params[i].name, "midi_in_ports" );
  621. desc->params[i].character = 'i';
  622. desc->params[i].type = JackDriverParamInt;
  623. desc->params[i].value.i = 0;
  624. strcpy ( desc->params[i].short_desc, "Number of midi input ports" );
  625. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  626. i++;
  627. strcpy ( desc->params[i].name, "midi_out_ports" );
  628. desc->params[i].character = 'o';
  629. desc->params[i].type = JackDriverParamUInt;
  630. desc->params[i].value.i = 0;
  631. strcpy ( desc->params[i].short_desc, "Number of midi output ports" );
  632. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  633. i++;
  634. strcpy ( desc->params[i].name, "client_name" );
  635. desc->params[i].character = 'n';
  636. desc->params[i].type = JackDriverParamString;
  637. strcpy ( desc->params[i].value.str, "'hostname'" );
  638. strcpy ( desc->params[i].short_desc, "Name of the jack client" );
  639. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  640. i++;
  641. strcpy ( desc->params[i].name, "transport_sync" );
  642. desc->params[i].character = 't';
  643. desc->params[i].type = JackDriverParamUInt;
  644. desc->params[i].value.ui = 1U;
  645. strcpy ( desc->params[i].short_desc, "Sync transport with master's" );
  646. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  647. return desc;
  648. }
  649. EXPORT Jack::JackDriverClientInterface* driver_initialize ( Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params )
  650. {
  651. if ( SocketAPIInit() < 0 )
  652. {
  653. jack_error ( "Can't init Socket API, exiting..." );
  654. return NULL;
  655. }
  656. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  657. char name[JACK_CLIENT_NAME_SIZE];
  658. GetHostName ( name, JACK_CLIENT_NAME_SIZE );
  659. int udp_port = DEFAULT_PORT;
  660. int mtu = 1500;
  661. uint transport_sync = 1;
  662. jack_nframes_t period_size = 128;
  663. jack_nframes_t sample_rate = 48000;
  664. int audio_capture_ports = 2;
  665. int audio_playback_ports = 2;
  666. int midi_input_ports = 0;
  667. int midi_output_ports = 0;
  668. bool monitor = false;
  669. const JSList* node;
  670. const jack_driver_param_t* param;
  671. for ( node = params; node; node = jack_slist_next ( node ) )
  672. {
  673. param = ( const jack_driver_param_t* ) node->data;
  674. switch ( param->character )
  675. {
  676. case 'a' :
  677. multicast_ip = strdup ( param->value.str );
  678. break;
  679. case 'p':
  680. udp_port = param->value.ui;
  681. break;
  682. case 'M':
  683. mtu = param->value.i;
  684. break;
  685. case 'C':
  686. audio_capture_ports = param->value.i;
  687. break;
  688. case 'P':
  689. audio_playback_ports = param->value.i;
  690. break;
  691. case 'i':
  692. midi_input_ports = param->value.i;
  693. break;
  694. case 'o':
  695. midi_output_ports = param->value.i;
  696. break;
  697. case 'n' :
  698. strncpy ( name, param->value.str, JACK_CLIENT_NAME_SIZE );
  699. case 't' :
  700. transport_sync = param->value.ui;
  701. }
  702. }
  703. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver (
  704. new Jack::JackNetDriver ( "system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  705. midi_input_ports, midi_output_ports, name, transport_sync ) );
  706. if ( driver->Open ( period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports,
  707. monitor, "from_master_", "to_master_", 0, 0 ) == 0 )
  708. return driver;
  709. delete driver;
  710. return NULL;
  711. }
  712. #ifdef __cplusplus
  713. }
  714. #endif
  715. }