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.

697 lines
24KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-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 "JackException.h"
  24. #include "JackExports.h"
  25. #define DEFAULT_MULTICAST_IP "225.3.19.154"
  26. #define DEFAULT_PORT 19000
  27. namespace Jack
  28. {
  29. JackNetDriver::JackNetDriver ( const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table,
  30. const char* ip, size_t port, int midi_input_ports, int midi_output_ports, const char* net_name )
  31. : JackAudioDriver ( name, alias, engine, table )
  32. {
  33. fMulticastIP = new char[strlen ( ip ) + 1];
  34. strcpy ( fMulticastIP, ip );
  35. fPort = port;
  36. fParams.fSendMidiChannels = midi_input_ports;
  37. fParams.fReturnMidiChannels = midi_output_ports;
  38. strcpy ( fParams.fName, net_name );
  39. fSockfd = 0;
  40. }
  41. JackNetDriver::~JackNetDriver()
  42. {
  43. if ( fSockfd )
  44. close ( fSockfd );
  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 nframes, 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 ( nframes, 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. if ( fSockfd )
  80. Restart();
  81. //set the parameters to send
  82. strcpy ( fParams.fPacketType, "params" );
  83. fParams.fProtocolVersion = 'a';
  84. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  85. gethostname ( fParams.fSlaveNetName, 255 );
  86. fParams.fSendAudioChannels = fCaptureChannels;
  87. fParams.fReturnAudioChannels = fPlaybackChannels;
  88. //init loop : get a master and start, do it until connection is ok
  89. net_status_t status;
  90. do
  91. {
  92. //first, get a master, do it until a valid connection is running
  93. jack_info ( "Initializing Net Driver..." );
  94. do
  95. {
  96. status = GetNetMaster();
  97. if ( status == SOCKET_ERROR )
  98. return false;
  99. }
  100. while ( status != CONNECTED );
  101. //then tell the master we are ready
  102. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  103. status = SendMasterStartSync();
  104. if ( status == NET_ERROR )
  105. return false;
  106. }
  107. while ( status != ROLLING );
  108. //driver parametering
  109. if ( SetParams() )
  110. {
  111. jack_error ( "Fatal error : can't alloc net driver ports." );
  112. return false;
  113. }
  114. //init done, display parameters
  115. SessionParamsDisplay ( &fParams );
  116. return true;
  117. }
  118. net_status_t JackNetDriver::GetNetMaster()
  119. {
  120. jack_log ( "JackNetDriver::GetNetMaster()" );
  121. //utility
  122. session_params_t params;
  123. struct sockaddr_in mcast_addr, listen_addr;
  124. struct timeval rcv_timeout;
  125. rcv_timeout.tv_sec = 2;
  126. rcv_timeout.tv_usec = 0;
  127. socklen_t addr_len = sizeof ( socket_address_t );
  128. int rx_bytes = 0;
  129. //set the multicast address
  130. mcast_addr.sin_family = AF_INET;
  131. mcast_addr.sin_port = htons ( fPort );
  132. inet_aton ( fMulticastIP, &mcast_addr.sin_addr );
  133. memset ( &mcast_addr.sin_zero, 0, 8 );
  134. //set the listening address
  135. listen_addr.sin_family = AF_INET;
  136. listen_addr.sin_port = htons ( fPort );
  137. listen_addr.sin_addr.s_addr = htonl ( INADDR_ANY );
  138. memset ( &listen_addr.sin_zero, 0, 8 );
  139. //set the master address family
  140. fMasterAddr.sin_family = AF_INET;
  141. //socket
  142. if ( fSockfd )
  143. close ( fSockfd );
  144. if ( ( fSockfd = socket ( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )
  145. {
  146. jack_error ( "Fatal error : network unreachable - %s", strerror ( errno ) );
  147. return SOCKET_ERROR;
  148. }
  149. //bind the socket
  150. if ( bind ( fSockfd, reinterpret_cast<socket_address_t*> ( &listen_addr ), addr_len ) < 0 )
  151. jack_error ( "Can't bind the socket : %s", strerror ( errno ) );
  152. //timeout on receive
  153. setsockopt ( fSockfd, SOL_SOCKET, SO_RCVTIMEO, &rcv_timeout, sizeof ( rcv_timeout ) );
  154. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  155. jack_info ( "Waiting for a master..." );
  156. do
  157. {
  158. //send 'available'
  159. if ( sendto ( fSockfd, &fParams, sizeof ( session_params_t ), MSG_DONTWAIT,
  160. reinterpret_cast<socket_address_t*> ( &mcast_addr ), addr_len ) < 0 )
  161. jack_error ( "Error in data send : %s", strerror ( errno ) );
  162. //filter incoming packets : don't exit while receiving wrong packets
  163. do
  164. {
  165. rx_bytes = recvfrom ( fSockfd, &params, sizeof ( session_params_t ), 0,
  166. reinterpret_cast<socket_address_t*> ( &fMasterAddr ), &addr_len );
  167. if ( ( rx_bytes < 0 ) && ( errno != EAGAIN ) )
  168. {
  169. jack_error ( "Can't receive : %s", strerror ( errno ) );
  170. return RECV_ERROR;
  171. }
  172. }
  173. while ( ( rx_bytes > 0 ) && strcmp ( params.fPacketType, fParams.fPacketType ) );
  174. }
  175. while ( ( GetPacketType ( &params ) != SLAVE_SETUP ) );
  176. //connect the socket
  177. if ( connect ( fSockfd, reinterpret_cast<socket_address_t*> ( &fMasterAddr ), sizeof ( socket_address_t ) ) < 0 )
  178. {
  179. jack_error ( "Error in connect : %s", strerror ( errno ) );
  180. return CONNECT_ERROR;
  181. }
  182. //everything is OK, copy parameters and return
  183. fParams = params;
  184. return CONNECTED;
  185. }
  186. net_status_t JackNetDriver::SendMasterStartSync()
  187. {
  188. jack_log ( "JackNetDriver::GetNetMasterStartSync()" );
  189. //tell the master to start
  190. SetPacketType ( &fParams, START_MASTER );
  191. if ( send ( fSockfd, &fParams, sizeof ( session_params_t ), MSG_DONTWAIT ) < 0 )
  192. {
  193. jack_error ( "Error in send : %s", strerror ( errno ) );
  194. return ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) ) ? NET_ERROR : SEND_ERROR;
  195. }
  196. return ROLLING;
  197. }
  198. void JackNetDriver::Restart()
  199. {
  200. jack_info ( "Restarting driver..." );
  201. close ( fSockfd );
  202. delete[] fTxBuffer;
  203. delete[] fRxBuffer;
  204. delete fNetAudioCaptureBuffer;
  205. delete fNetAudioPlaybackBuffer;
  206. delete fNetMidiCaptureBuffer;
  207. delete fNetMidiPlaybackBuffer;
  208. FreePorts();
  209. delete[] fMidiCapturePortList;
  210. delete[] fMidiPlaybackPortList;
  211. fTxBuffer = NULL;
  212. fRxBuffer = NULL;
  213. fNetAudioCaptureBuffer = NULL;
  214. fNetAudioPlaybackBuffer = NULL;
  215. fNetMidiCaptureBuffer = NULL;
  216. fNetMidiPlaybackBuffer = NULL;
  217. fMidiCapturePortList = NULL;
  218. fMidiPlaybackPortList = NULL;
  219. }
  220. int JackNetDriver::SetParams()
  221. {
  222. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  223. SetBufferSize ( fParams.fPeriodSize );
  224. SetSampleRate ( fParams.fSampleRate );
  225. //allocate midi ports lists
  226. fMidiCapturePortList = new jack_port_id_t [fParams.fSendMidiChannels];
  227. fMidiPlaybackPortList = new jack_port_id_t [fParams.fReturnMidiChannels];
  228. //register jack ports
  229. if ( AllocPorts() != 0 )
  230. {
  231. jack_error ( "Can't allocate ports." );
  232. return -1;
  233. }
  234. //TX header init
  235. strcpy ( fTxHeader.fPacketType, "header" );
  236. fTxHeader.fDataStream = 'r';
  237. fTxHeader.fID = fParams.fID;
  238. fTxHeader.fCycle = 0;
  239. fTxHeader.fSubCycle = 0;
  240. fTxHeader.fMidiDataSize = 0;
  241. fTxHeader.fBitdepth = fParams.fBitdepth;
  242. //RX header init
  243. strcpy ( fRxHeader.fPacketType, "header" );
  244. fRxHeader.fDataStream = 's';
  245. fRxHeader.fID = fParams.fID;
  246. fRxHeader.fCycle = 0;
  247. fRxHeader.fSubCycle = 0;
  248. fRxHeader.fMidiDataSize = 0;
  249. fRxHeader.fBitdepth = fParams.fBitdepth;
  250. //network buffers
  251. fTxBuffer = new char[fParams.fMtu];
  252. fRxBuffer = new char[fParams.fMtu];
  253. //net audio/midi buffers
  254. fTxData = fTxBuffer + sizeof ( packet_header_t );
  255. fRxData = fRxBuffer + sizeof ( packet_header_t );
  256. //midi net buffers
  257. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  258. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  259. //audio net buffers
  260. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  261. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  262. //audio netbuffer length
  263. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  264. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  265. return 0;
  266. }
  267. int JackNetDriver::AllocPorts()
  268. {
  269. jack_log ( "JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate );
  270. JackPort* port;
  271. jack_port_id_t port_id;
  272. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  273. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  274. unsigned long port_flags;
  275. //audio
  276. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  277. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  278. {
  279. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, port_index + 1 );
  280. snprintf ( name, sizeof ( name ) - 1, "%s:capture_%d", fClientControl->fName, 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 + fCaptureLatency );
  290. fCapturePortList[port_index] = port_id;
  291. jack_log ( "JackNetDriver::AllocPorts() fCapturePortList[%d] port_index = %ld", port_index, port_id );
  292. }
  293. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  294. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  295. {
  296. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, port_index + 1 );
  297. snprintf ( name, sizeof ( name ) - 1, "%s:playback_%d",fClientControl->fName, port_index + 1 );
  298. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  299. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  300. {
  301. jack_error ( "driver: cannot register port for %s", name );
  302. return -1;
  303. }
  304. port = fGraphManager->GetPort ( port_id );
  305. port->SetAlias ( alias );
  306. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) + fPlaybackLatency );
  307. fPlaybackPortList[port_index] = port_id;
  308. jack_log ( "JackNetDriver::AllocPorts() fPlaybackPortList[%d] port_index = %ld", port_index, port_id );
  309. }
  310. //midi
  311. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  312. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  313. {
  314. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, port_index + 1 );
  315. snprintf ( name, sizeof ( name ) - 1, "%s:midi_capture_%d", fClientControl->fName, port_index + 1 );
  316. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  317. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  318. {
  319. jack_error ( "driver: cannot register port for %s", name );
  320. return -1;
  321. }
  322. fMidiCapturePortList[port_index] = port_id;
  323. jack_log ( "JackNetDriver::AllocPorts() fMidiCapturePortList[%d] port_index = %ld", port_index, port_id );
  324. }
  325. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  326. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  327. {
  328. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, port_index + 1 );
  329. snprintf ( name, sizeof ( name ) - 1, "%s:midi_playback_%d", fClientControl->fName, port_index + 1 );
  330. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  331. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  332. {
  333. jack_error ( "driver: cannot register port for %s", name );
  334. return -1;
  335. }
  336. fMidiPlaybackPortList[port_index] = port_id;
  337. jack_log ( "JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] port_index = %ld", port_index, port_id );
  338. }
  339. return 0;
  340. }
  341. int JackNetDriver::FreePorts()
  342. {
  343. jack_log ( "JackNetDriver::FreePorts" );
  344. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  345. fGraphManager->ReleasePort ( fClientControl->fRefNum, fCapturePortList[port_index] );
  346. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  347. fGraphManager->ReleasePort ( fClientControl->fRefNum, fPlaybackPortList[port_index] );
  348. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  349. fGraphManager->ReleasePort ( fClientControl->fRefNum, fMidiCapturePortList[port_index] );
  350. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  351. fGraphManager->ReleasePort ( fClientControl->fRefNum, fMidiPlaybackPortList[port_index] );
  352. return 0;
  353. }
  354. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer ( int port_index )
  355. {
  356. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiCapturePortList[port_index], fEngineControl->fBufferSize ) );
  357. }
  358. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer ( int port_index )
  359. {
  360. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize ) );
  361. }
  362. int JackNetDriver::Recv ( size_t size, int flags )
  363. {
  364. int rx_bytes;
  365. if ( ( rx_bytes = recv ( fSockfd, fRxBuffer, size, flags ) ) < 0 )
  366. {
  367. if ( errno == EAGAIN )
  368. {
  369. jack_error ( "No incoming data, is the master still running ?" );
  370. return 0;
  371. }
  372. else if ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) )
  373. {
  374. jack_error ( "Fatal error : %s.", strerror ( errno ) );
  375. throw JackDriverException ( "" );
  376. return -1;
  377. }
  378. else
  379. {
  380. jack_error ( "Error in receive : %s", strerror ( errno ) );
  381. return 0;
  382. }
  383. }
  384. return rx_bytes;
  385. }
  386. int JackNetDriver::Send ( size_t size, int flags )
  387. {
  388. int tx_bytes;
  389. if ( ( tx_bytes = send ( fSockfd, fTxBuffer, size, flags ) ) < 0 )
  390. {
  391. if ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) )
  392. {
  393. jack_error ( "Fatal error : %s.", strerror ( errno ) );
  394. throw JackDriverException ( "" );
  395. return -1;
  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. size_t recvd_midi_pckt = 0;
  407. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  408. fRxHeader.fIsLastPckt = 'n';
  409. //buffers
  410. for ( int 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::CycleTakeTime();
  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 ( int 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 ( size_t 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, MSG_DONTWAIT );
  487. }
  488. }
  489. //audio
  490. if ( fParams.fReturnAudioChannels )
  491. {
  492. fTxHeader.fDataType = 'a';
  493. for ( size_t 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, MSG_DONTWAIT );
  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 = 7;
  515. desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );
  516. size_t 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 = JackDriverParamUInt;
  527. desc->params[i].value.ui = 19000U;
  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, "input_ports" );
  532. desc->params[i].character = 'C';
  533. desc->params[i].type = JackDriverParamInt;
  534. desc->params[i].value.i = 2;
  535. strcpy ( desc->params[i].short_desc, "Number of audio input ports" );
  536. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  537. i++;
  538. strcpy ( desc->params[i].name, "output_ports" );
  539. desc->params[i].character = 'P';
  540. desc->params[i].type = JackDriverParamUInt;
  541. desc->params[i].value.i = 2;
  542. strcpy ( desc->params[i].short_desc, "Number of audio output ports" );
  543. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  544. i++;
  545. strcpy ( desc->params[i].name, "midi_in_ports" );
  546. desc->params[i].character = 'i';
  547. desc->params[i].type = JackDriverParamInt;
  548. desc->params[i].value.i = 0;
  549. strcpy ( desc->params[i].short_desc, "Number of midi input ports" );
  550. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  551. i++;
  552. strcpy ( desc->params[i].name, "midi_out_ports" );
  553. desc->params[i].character = 'o';
  554. desc->params[i].type = JackDriverParamUInt;
  555. desc->params[i].value.i = 0;
  556. strcpy ( desc->params[i].short_desc, "Number of midi output ports" );
  557. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  558. i++;
  559. strcpy ( desc->params[i].name, "client_name" );
  560. desc->params[i].character = 'n';
  561. desc->params[i].type = JackDriverParamString;
  562. strcpy ( desc->params[i].value.str, "'hostname'" );
  563. strcpy ( desc->params[i].short_desc, "Name of the jack client" );
  564. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  565. return desc;
  566. }
  567. EXPORT Jack::JackDriverClientInterface* driver_initialize ( Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params )
  568. {
  569. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  570. char name[JACK_CLIENT_NAME_SIZE];
  571. gethostname ( name, JACK_CLIENT_NAME_SIZE );
  572. jack_nframes_t udp_port = DEFAULT_PORT;
  573. jack_nframes_t period_size = 128;
  574. jack_nframes_t sample_rate = 48000;
  575. int audio_capture_ports = 2;
  576. int audio_playback_ports = 2;
  577. int midi_input_ports = 0;
  578. int midi_output_ports = 0;
  579. bool monitor = false;
  580. const JSList* node;
  581. const jack_driver_param_t* param;
  582. for ( node = params; node; node = jack_slist_next ( node ) )
  583. {
  584. param = ( const jack_driver_param_t* ) node->data;
  585. switch ( param->character )
  586. {
  587. case 'a' :
  588. multicast_ip = strdup ( param->value.str );
  589. break;
  590. case 'p':
  591. udp_port = param->value.ui;
  592. break;
  593. case 'C':
  594. audio_capture_ports = param->value.i;
  595. break;
  596. case 'P':
  597. audio_playback_ports = param->value.i;
  598. break;
  599. case 'i':
  600. midi_input_ports = param->value.i;
  601. break;
  602. case 'o':
  603. midi_output_ports = param->value.i;
  604. break;
  605. case 'n' :
  606. strncpy ( name, param->value.str, JACK_CLIENT_NAME_SIZE );
  607. }
  608. }
  609. Jack::JackDriverClientInterface* driver = new Jack::JackRestartThreadedDriver (
  610. new Jack::JackNetDriver ( "system", "net_pcm", engine, table, multicast_ip, udp_port, midi_input_ports, midi_output_ports, name ) );
  611. if ( driver->Open ( period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports,
  612. monitor, "from_master_", "to_master_", 0, 0 ) == 0 )
  613. return driver;
  614. delete driver;
  615. return NULL;
  616. }
  617. #ifdef __cplusplus
  618. }
  619. #endif
  620. }