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.

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