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.

723 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. 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 ( 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. return 0;
  251. }
  252. int JackNetDriver::AllocPorts()
  253. {
  254. jack_log ( "JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate );
  255. JackPort* port;
  256. jack_port_id_t port_id;
  257. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  258. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  259. unsigned long port_flags;
  260. int audio_port_index;
  261. uint midi_port_index;
  262. //audio
  263. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  264. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  265. {
  266. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, audio_port_index + 1 );
  267. snprintf ( name, sizeof ( name ) - 1, "%s:capture_%d", fClientControl.fName, audio_port_index + 1 );
  268. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  269. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  270. {
  271. jack_error ( "driver: cannot register port for %s", name );
  272. return -1;
  273. }
  274. port = fGraphManager->GetPort ( port_id );
  275. port->SetAlias ( alias );
  276. port->SetLatency ( fEngineControl->fBufferSize );
  277. fCapturePortList[audio_port_index] = port_id;
  278. jack_log ( "JackNetDriver::AllocPorts() fCapturePortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  279. }
  280. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  281. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  282. {
  283. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, audio_port_index + 1 );
  284. snprintf ( name, sizeof ( name ) - 1, "%s:playback_%d",fClientControl.fName, audio_port_index + 1 );
  285. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  286. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  287. {
  288. jack_error ( "driver: cannot register port for %s", name );
  289. return -1;
  290. }
  291. port = fGraphManager->GetPort ( port_id );
  292. port->SetAlias ( alias );
  293. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  294. fPlaybackPortList[audio_port_index] = port_id;
  295. jack_log ( "JackNetDriver::AllocPorts() fPlaybackPortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_id, port->GetLatency() );
  296. }
  297. //midi
  298. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  299. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  300. {
  301. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, midi_port_index + 1 );
  302. snprintf ( name, sizeof ( name ) - 1, "%s:midi_capture_%d", fClientControl.fName, midi_port_index + 1 );
  303. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  304. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  305. {
  306. jack_error ( "driver: cannot register port for %s", name );
  307. return -1;
  308. }
  309. port = fGraphManager->GetPort ( port_id );
  310. port->SetLatency ( fEngineControl->fBufferSize );
  311. fMidiCapturePortList[midi_port_index] = port_id;
  312. jack_log ( "JackNetDriver::AllocPorts() fMidiCapturePortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  313. }
  314. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  315. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  316. {
  317. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, midi_port_index + 1 );
  318. snprintf ( name, sizeof ( name ) - 1, "%s:midi_playback_%d", fClientControl.fName, midi_port_index + 1 );
  319. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  320. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  321. {
  322. jack_error ( "driver: cannot register port for %s", name );
  323. return -1;
  324. }
  325. port = fGraphManager->GetPort ( port_id );
  326. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) );
  327. fMidiPlaybackPortList[midi_port_index] = port_id;
  328. jack_log ( "JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_id, port->GetLatency() );
  329. }
  330. return 0;
  331. }
  332. int JackNetDriver::FreePorts()
  333. {
  334. jack_log ( "JackNetDriver::FreePorts" );
  335. int audio_port_index;
  336. uint midi_port_index;
  337. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  338. fGraphManager->ReleasePort ( fClientControl.fRefNum, fCapturePortList[audio_port_index] );
  339. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  340. fGraphManager->ReleasePort ( fClientControl.fRefNum, fPlaybackPortList[audio_port_index] );
  341. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  342. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiCapturePortList[midi_port_index] );
  343. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  344. fGraphManager->ReleasePort ( fClientControl.fRefNum, fMidiPlaybackPortList[midi_port_index] );
  345. return 0;
  346. }
  347. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer ( int port_index )
  348. {
  349. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiCapturePortList[port_index], fEngineControl->fBufferSize ) );
  350. }
  351. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer ( int port_index )
  352. {
  353. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize ) );
  354. }
  355. int JackNetDriver::Recv ( size_t size, int flags )
  356. {
  357. int rx_bytes;
  358. if ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR )
  359. {
  360. net_error_t error = fSocket.GetError();
  361. if ( error == NET_NO_DATA )
  362. {
  363. jack_error ( "No incoming data, is the master still running ?" );
  364. return 0;
  365. }
  366. else if ( error == NET_CONN_ERROR )
  367. {
  368. throw JackDriverException ( "Connection lost." );
  369. }
  370. else
  371. {
  372. jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) );
  373. return 0;
  374. }
  375. }
  376. return rx_bytes;
  377. }
  378. int JackNetDriver::Send ( size_t size, int flags )
  379. {
  380. int tx_bytes;
  381. if ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR )
  382. {
  383. net_error_t error = fSocket.GetError();
  384. if ( error == NET_CONN_ERROR )
  385. {
  386. throw JackDriverException ( "Connection lost." );
  387. }
  388. else
  389. jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
  390. }
  391. return tx_bytes;
  392. }
  393. //*************************************process************************************************************************
  394. int JackNetDriver::Read()
  395. {
  396. int rx_bytes;
  397. uint recvd_midi_pckt = 0;
  398. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  399. fRxHeader.fIsLastPckt = 'n';
  400. uint midi_port_index;
  401. int audio_port_index;
  402. //buffers
  403. for ( midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++ )
  404. fNetMidiCaptureBuffer->SetBuffer(midi_port_index, GetMidiInputBuffer ( midi_port_index ));
  405. for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ )
  406. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer ( audio_port_index ));
  407. //receive sync (launch the cycle)
  408. do
  409. {
  410. rx_bytes = Recv ( sizeof ( packet_header_t ), 0 );
  411. if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) )
  412. return rx_bytes;
  413. }
  414. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  415. JackDriver::CycleTakeBeginTime();
  416. //audio, midi or sync if driver is late
  417. if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels )
  418. {
  419. do
  420. {
  421. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  422. if ( rx_bytes < 1 )
  423. return rx_bytes;
  424. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  425. {
  426. switch ( rx_head->fDataType )
  427. {
  428. case 'm': //midi
  429. rx_bytes = Recv ( rx_bytes, 0 );
  430. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  431. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  432. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  433. fNetMidiCaptureBuffer->RenderToJackPorts();
  434. break;
  435. case 'a': //audio
  436. rx_bytes = Recv ( fAudioRxLen, 0 );
  437. if ( !IsNextPacket ( &fRxHeader, rx_head, fNSubProcess ) )
  438. jack_error ( "Packet(s) missing..." );
  439. fRxHeader.fCycle = rx_head->fCycle;
  440. fRxHeader.fSubCycle = rx_head->fSubCycle;
  441. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  442. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  443. break;
  444. case 's': //sync
  445. jack_info ( "NetDriver : driver overloaded, skipping receive." );
  446. fRxHeader.fCycle = rx_head->fCycle;
  447. return 0;
  448. }
  449. }
  450. }
  451. while ( fRxHeader.fIsLastPckt != 'y' );
  452. }
  453. fRxHeader.fCycle = rx_head->fCycle;
  454. return 0;
  455. }
  456. int JackNetDriver::Write()
  457. {
  458. int tx_bytes, copy_size;
  459. fTxHeader.fCycle = fRxHeader.fCycle;
  460. fTxHeader.fSubCycle = 0;
  461. fTxHeader.fIsLastPckt = 'n';
  462. uint midi_port_index;
  463. int audio_port_index;
  464. //buffers
  465. for ( midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++ )
  466. fNetMidiPlaybackBuffer->SetBuffer(midi_port_index, GetMidiOutputBuffer ( midi_port_index ));
  467. for ( audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++ )
  468. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, GetOutputBuffer ( audio_port_index ));
  469. //midi
  470. if ( fParams.fReturnMidiChannels )
  471. {
  472. fTxHeader.fDataType = 'm';
  473. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  474. fTxHeader.fNMidiPckt = GetNMidiPckt ( &fParams, fTxHeader.fMidiDataSize );
  475. for ( uint subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  476. {
  477. fTxHeader.fSubCycle = subproc;
  478. if ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels )
  479. fTxHeader.fIsLastPckt = 'y';
  480. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  481. copy_size = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  482. tx_bytes = Send ( sizeof ( packet_header_t ) + copy_size, 0 );
  483. }
  484. }
  485. //audio
  486. if ( fParams.fReturnAudioChannels )
  487. {
  488. fTxHeader.fDataType = 'a';
  489. for ( uint subproc = 0; subproc < fNSubProcess; subproc++ )
  490. {
  491. fTxHeader.fSubCycle = subproc;
  492. if ( subproc == ( fNSubProcess - 1 ) )
  493. fTxHeader.fIsLastPckt = 'y';
  494. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  495. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  496. tx_bytes = Send ( fAudioTxLen, 0 );
  497. }
  498. }
  499. return 0;
  500. }
  501. //*************************************loader*******************************************************
  502. #ifdef __cplusplus
  503. extern "C"
  504. {
  505. #endif
  506. EXPORT jack_driver_desc_t* driver_get_descriptor ()
  507. {
  508. jack_driver_desc_t* desc = ( jack_driver_desc_t* ) calloc ( 1, sizeof ( jack_driver_desc_t ) );
  509. strcpy ( desc->name, "net" );
  510. desc->nparams = 9;
  511. desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );
  512. int i = 0;
  513. strcpy ( desc->params[i].name, "multicast_ip" );
  514. desc->params[i].character = 'a';
  515. desc->params[i].type = JackDriverParamString;
  516. strcpy ( desc->params[i].value.str, DEFAULT_MULTICAST_IP );
  517. strcpy ( desc->params[i].short_desc, "Multicast Address" );
  518. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  519. i++;
  520. strcpy ( desc->params[i].name, "udp_net_port" );
  521. desc->params[i].character = 'p';
  522. desc->params[i].type = JackDriverParamInt;
  523. desc->params[i].value.i = 19000;
  524. strcpy ( desc->params[i].short_desc, "UDP port" );
  525. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  526. i++;
  527. strcpy ( desc->params[i].name, "mtu" );
  528. desc->params[i].character = 'M';
  529. desc->params[i].type = JackDriverParamInt;
  530. desc->params[i].value.i = 1500;
  531. strcpy ( desc->params[i].short_desc, "MTU to the master" );
  532. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  533. i++;
  534. strcpy ( desc->params[i].name, "input_ports" );
  535. desc->params[i].character = 'C';
  536. desc->params[i].type = JackDriverParamInt;
  537. desc->params[i].value.i = 2;
  538. strcpy ( desc->params[i].short_desc, "Number of audio input ports" );
  539. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  540. i++;
  541. strcpy ( desc->params[i].name, "output_ports" );
  542. desc->params[i].character = 'P';
  543. desc->params[i].type = JackDriverParamInt;
  544. desc->params[i].value.i = 2;
  545. strcpy ( desc->params[i].short_desc, "Number of audio output ports" );
  546. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  547. i++;
  548. strcpy ( desc->params[i].name, "midi_in_ports" );
  549. desc->params[i].character = 'i';
  550. desc->params[i].type = JackDriverParamInt;
  551. desc->params[i].value.i = 0;
  552. strcpy ( desc->params[i].short_desc, "Number of midi input ports" );
  553. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  554. i++;
  555. strcpy ( desc->params[i].name, "midi_out_ports" );
  556. desc->params[i].character = 'o';
  557. desc->params[i].type = JackDriverParamUInt;
  558. desc->params[i].value.i = 0;
  559. strcpy ( desc->params[i].short_desc, "Number of midi output ports" );
  560. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  561. i++;
  562. strcpy ( desc->params[i].name, "client_name" );
  563. desc->params[i].character = 'n';
  564. desc->params[i].type = JackDriverParamString;
  565. strcpy ( desc->params[i].value.str, "'hostname'" );
  566. strcpy ( desc->params[i].short_desc, "Name of the jack client" );
  567. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  568. i++;
  569. strcpy ( desc->params[i].name, "transport_sync" );
  570. desc->params[i].character = 't';
  571. desc->params[i].type = JackDriverParamUInt;
  572. desc->params[i].value.ui = 1U;
  573. strcpy ( desc->params[i].short_desc, "Sync transport with master's" );
  574. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  575. return desc;
  576. }
  577. EXPORT Jack::JackDriverClientInterface* driver_initialize ( Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params )
  578. {
  579. if ( SocketAPIInit() < 0 )
  580. {
  581. jack_error ( "Can't init Socket API, exiting..." );
  582. return NULL;
  583. }
  584. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  585. char name[JACK_CLIENT_NAME_SIZE];
  586. GetHostName ( name, JACK_CLIENT_NAME_SIZE );
  587. int udp_port = DEFAULT_PORT;
  588. int mtu = 1500;
  589. uint transport_sync = 1;
  590. jack_nframes_t period_size = 128;
  591. jack_nframes_t sample_rate = 48000;
  592. int audio_capture_ports = 2;
  593. int audio_playback_ports = 2;
  594. int midi_input_ports = 0;
  595. int midi_output_ports = 0;
  596. bool monitor = false;
  597. const JSList* node;
  598. const jack_driver_param_t* param;
  599. for ( node = params; node; node = jack_slist_next ( node ) )
  600. {
  601. param = ( const jack_driver_param_t* ) node->data;
  602. switch ( param->character )
  603. {
  604. case 'a' :
  605. multicast_ip = strdup ( param->value.str );
  606. break;
  607. case 'p':
  608. udp_port = param->value.ui;
  609. break;
  610. case 'M':
  611. mtu = param->value.i;
  612. break;
  613. case 'C':
  614. audio_capture_ports = param->value.i;
  615. break;
  616. case 'P':
  617. audio_playback_ports = param->value.i;
  618. break;
  619. case 'i':
  620. midi_input_ports = param->value.i;
  621. break;
  622. case 'o':
  623. midi_output_ports = param->value.i;
  624. break;
  625. case 'n' :
  626. strncpy ( name, param->value.str, JACK_CLIENT_NAME_SIZE );
  627. case 't' :
  628. transport_sync = param->value.ui;
  629. }
  630. }
  631. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver (
  632. new Jack::JackNetDriver ( "system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  633. midi_input_ports, midi_output_ports, name, transport_sync ) );
  634. if ( driver->Open ( period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports,
  635. monitor, "from_master_", "to_master_", 0, 0 ) == 0 )
  636. return driver;
  637. delete driver;
  638. return NULL;
  639. }
  640. #ifdef __cplusplus
  641. }
  642. #endif
  643. }