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.

707 lines
29KB

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