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.

710 lines
24KB

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