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.

744 lines
30KB

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