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.

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