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.

696 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. #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, size_t port, int midi_input_ports, int midi_output_ports, const char* net_name )
  30. : JackAudioDriver ( name, alias, engine, table )
  31. {
  32. fMulticastIP = new char[strlen ( ip ) + 1];
  33. strcpy ( fMulticastIP, ip );
  34. fPort = port;
  35. fParams.fSendMidiChannels = midi_input_ports;
  36. fParams.fReturnMidiChannels = midi_output_ports;
  37. strcpy ( fParams.fName, net_name );
  38. fSockfd = 0;
  39. }
  40. JackNetDriver::~JackNetDriver()
  41. {
  42. if ( fSockfd )
  43. close ( fSockfd );
  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 nframes, 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 ( nframes, 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. if ( fSockfd )
  79. Restart();
  80. //set the parameters to send
  81. strcpy ( fParams.fPacketType, "params" );
  82. fParams.fProtocolVersion = 'a';
  83. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  84. gethostname ( fParams.fSlaveNetName, 255 );
  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 == SOCKET_ERROR )
  97. return false;
  98. }
  99. while ( status != 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 != 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. struct sockaddr_in mcast_addr, listen_addr;
  123. struct timeval rcv_timeout;
  124. rcv_timeout.tv_sec = 2;
  125. rcv_timeout.tv_usec = 0;
  126. socklen_t addr_len = sizeof ( socket_address_t );
  127. int rx_bytes = 0;
  128. //set the multicast address
  129. mcast_addr.sin_family = AF_INET;
  130. mcast_addr.sin_port = htons ( fPort );
  131. inet_aton ( fMulticastIP, &mcast_addr.sin_addr );
  132. memset ( &mcast_addr.sin_zero, 0, 8 );
  133. //set the listening address
  134. listen_addr.sin_family = AF_INET;
  135. listen_addr.sin_port = htons ( fPort );
  136. listen_addr.sin_addr.s_addr = htonl ( INADDR_ANY );
  137. memset ( &listen_addr.sin_zero, 0, 8 );
  138. //set the master address family
  139. fMasterAddr.sin_family = AF_INET;
  140. //socket
  141. if ( fSockfd )
  142. close ( fSockfd );
  143. if ( ( fSockfd = socket ( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )
  144. {
  145. jack_error ( "Fatal error : network unreachable - %s", strerror ( errno ) );
  146. return SOCKET_ERROR;
  147. }
  148. //bind the socket
  149. if ( bind ( fSockfd, reinterpret_cast<socket_address_t*> ( &listen_addr ), addr_len ) < 0 )
  150. jack_error ( "Can't bind the socket : %s", strerror ( errno ) );
  151. //timeout on receive
  152. setsockopt ( fSockfd, SOL_SOCKET, SO_RCVTIMEO, &rcv_timeout, sizeof ( rcv_timeout ) );
  153. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  154. jack_info ( "Waiting for a master..." );
  155. do
  156. {
  157. //send 'available'
  158. if ( sendto ( fSockfd, &fParams, sizeof ( session_params_t ), MSG_DONTWAIT,
  159. reinterpret_cast<socket_address_t*> ( &mcast_addr ), addr_len ) < 0 )
  160. jack_error ( "Error in data send : %s", strerror ( errno ) );
  161. //filter incoming packets : don't exit while receiving wrong packets
  162. do
  163. {
  164. rx_bytes = recvfrom ( fSockfd, &params, sizeof ( session_params_t ), 0,
  165. reinterpret_cast<socket_address_t*> ( &fMasterAddr ), &addr_len );
  166. if ( ( rx_bytes < 0 ) && ( errno != EAGAIN ) )
  167. {
  168. jack_error ( "Can't receive : %s", strerror ( errno ) );
  169. return RECV_ERROR;
  170. }
  171. }
  172. while ( ( rx_bytes > 0 ) && strcmp ( params.fPacketType, fParams.fPacketType ) );
  173. }
  174. while ( ( GetPacketType ( &params ) != SLAVE_SETUP ) );
  175. //connect the socket
  176. if ( connect ( fSockfd, reinterpret_cast<socket_address_t*> ( &fMasterAddr ), sizeof ( socket_address_t ) ) < 0 )
  177. {
  178. jack_error ( "Error in connect : %s", strerror ( errno ) );
  179. return CONNECT_ERROR;
  180. }
  181. //everything is OK, copy parameters and return
  182. fParams = params;
  183. return CONNECTED;
  184. }
  185. net_status_t JackNetDriver::SendMasterStartSync()
  186. {
  187. jack_log ( "JackNetDriver::GetNetMasterStartSync()" );
  188. //tell the master to start
  189. SetPacketType ( &fParams, START_MASTER );
  190. if ( send ( fSockfd, &fParams, sizeof ( session_params_t ), MSG_DONTWAIT ) < 0 )
  191. {
  192. jack_error ( "Error in send : %s", strerror ( errno ) );
  193. return ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) ) ? NET_ERROR : SEND_ERROR;
  194. }
  195. return ROLLING;
  196. }
  197. void JackNetDriver::Restart()
  198. {
  199. jack_info ( "Restarting driver..." );
  200. close ( fSockfd );
  201. delete[] fTxBuffer;
  202. delete[] fRxBuffer;
  203. delete fNetAudioCaptureBuffer;
  204. delete fNetAudioPlaybackBuffer;
  205. delete fNetMidiCaptureBuffer;
  206. delete fNetMidiPlaybackBuffer;
  207. FreePorts();
  208. delete[] fMidiCapturePortList;
  209. delete[] fMidiPlaybackPortList;
  210. fTxBuffer = NULL;
  211. fRxBuffer = NULL;
  212. fNetAudioCaptureBuffer = NULL;
  213. fNetAudioPlaybackBuffer = NULL;
  214. fNetMidiCaptureBuffer = NULL;
  215. fNetMidiPlaybackBuffer = NULL;
  216. fMidiCapturePortList = NULL;
  217. fMidiPlaybackPortList = NULL;
  218. }
  219. int JackNetDriver::SetParams()
  220. {
  221. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  222. SetBufferSize ( fParams.fPeriodSize );
  223. SetSampleRate ( fParams.fSampleRate );
  224. //allocate midi ports lists
  225. fMidiCapturePortList = new jack_port_id_t [fParams.fSendMidiChannels];
  226. fMidiPlaybackPortList = new jack_port_id_t [fParams.fReturnMidiChannels];
  227. //register jack ports
  228. if ( AllocPorts() != 0 )
  229. {
  230. jack_error ( "Can't allocate ports." );
  231. return -1;
  232. }
  233. //TX header init
  234. strcpy ( fTxHeader.fPacketType, "header" );
  235. fTxHeader.fDataStream = 'r';
  236. fTxHeader.fID = fParams.fID;
  237. fTxHeader.fCycle = 0;
  238. fTxHeader.fSubCycle = 0;
  239. fTxHeader.fMidiDataSize = 0;
  240. fTxHeader.fBitdepth = fParams.fBitdepth;
  241. //RX header init
  242. strcpy ( fRxHeader.fPacketType, "header" );
  243. fRxHeader.fDataStream = 's';
  244. fRxHeader.fID = fParams.fID;
  245. fRxHeader.fCycle = 0;
  246. fRxHeader.fSubCycle = 0;
  247. fRxHeader.fMidiDataSize = 0;
  248. fRxHeader.fBitdepth = fParams.fBitdepth;
  249. //network buffers
  250. fTxBuffer = new char[fParams.fMtu];
  251. fRxBuffer = new char[fParams.fMtu];
  252. //net audio/midi buffers
  253. fTxData = fTxBuffer + sizeof ( packet_header_t );
  254. fRxData = fRxBuffer + sizeof ( packet_header_t );
  255. //midi net buffers
  256. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  257. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  258. //audio net buffers
  259. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  260. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  261. //audio netbuffer length
  262. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  263. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  264. return 0;
  265. }
  266. int JackNetDriver::AllocPorts()
  267. {
  268. jack_log ( "JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate );
  269. JackPort* port;
  270. jack_port_id_t port_id;
  271. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  272. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  273. unsigned long port_flags;
  274. //audio
  275. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  276. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  277. {
  278. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, port_index + 1 );
  279. snprintf ( name, sizeof ( name ) - 1, "%s:capture_%d", fClientControl->fName, port_index + 1 );
  280. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  281. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  282. {
  283. jack_error ( "driver: cannot register port for %s", name );
  284. return -1;
  285. }
  286. port = fGraphManager->GetPort ( port_id );
  287. port->SetAlias ( alias );
  288. port->SetLatency ( fEngineControl->fBufferSize + fCaptureLatency );
  289. fCapturePortList[port_index] = port_id;
  290. jack_log ( "JackNetDriver::AllocPorts() fCapturePortList[%d] port_index = %ld", port_index, port_id );
  291. }
  292. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  293. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  294. {
  295. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, port_index + 1 );
  296. snprintf ( name, sizeof ( name ) - 1, "%s:playback_%d",fClientControl->fName, port_index + 1 );
  297. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  298. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  299. {
  300. jack_error ( "driver: cannot register port for %s", name );
  301. return -1;
  302. }
  303. port = fGraphManager->GetPort ( port_id );
  304. port->SetAlias ( alias );
  305. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) + fPlaybackLatency );
  306. fPlaybackPortList[port_index] = port_id;
  307. jack_log ( "JackNetDriver::AllocPorts() fPlaybackPortList[%d] port_index = %ld", port_index, port_id );
  308. }
  309. //midi
  310. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  311. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  312. {
  313. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, port_index + 1 );
  314. snprintf ( name, sizeof ( name ) - 1, "%s:midi_capture_%d", fClientControl->fName, 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. fMidiCapturePortList[port_index] = port_id;
  322. jack_log ( "JackNetDriver::AllocPorts() fMidiCapturePortList[%d] port_index = %ld", port_index, port_id );
  323. }
  324. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  325. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  326. {
  327. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, port_index + 1 );
  328. snprintf ( name, sizeof ( name ) - 1, "%s:midi_playback_%d", fClientControl->fName, port_index + 1 );
  329. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  330. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  331. {
  332. jack_error ( "driver: cannot register port for %s", name );
  333. return -1;
  334. }
  335. fMidiPlaybackPortList[port_index] = port_id;
  336. jack_log ( "JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] port_index = %ld", port_index, port_id );
  337. }
  338. return 0;
  339. }
  340. int JackNetDriver::FreePorts()
  341. {
  342. jack_log ( "JackNetDriver::FreePorts" );
  343. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  344. fGraphManager->ReleasePort ( fClientControl->fRefNum, fCapturePortList[port_index] );
  345. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  346. fGraphManager->ReleasePort ( fClientControl->fRefNum, fPlaybackPortList[port_index] );
  347. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  348. fGraphManager->ReleasePort ( fClientControl->fRefNum, fMidiCapturePortList[port_index] );
  349. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  350. fGraphManager->ReleasePort ( fClientControl->fRefNum, fMidiPlaybackPortList[port_index] );
  351. return 0;
  352. }
  353. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer ( int port_index )
  354. {
  355. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiCapturePortList[port_index], fEngineControl->fBufferSize ) );
  356. }
  357. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer ( int port_index )
  358. {
  359. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize ) );
  360. }
  361. int JackNetDriver::Recv ( size_t size, int flags )
  362. {
  363. int rx_bytes;
  364. if ( ( rx_bytes = recv ( fSockfd, fRxBuffer, size, flags ) ) < 0 )
  365. {
  366. if ( errno == EAGAIN )
  367. {
  368. jack_error ( "No incoming data, is the master still running ?" );
  369. return 0;
  370. }
  371. else if ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) )
  372. {
  373. jack_error ( "Fatal error : %s.", strerror ( errno ) );
  374. throw JackDriverException ( "" );
  375. return -1;
  376. }
  377. else
  378. {
  379. jack_error ( "Error in receive : %s", strerror ( errno ) );
  380. return 0;
  381. }
  382. }
  383. return rx_bytes;
  384. }
  385. int JackNetDriver::Send ( size_t size, int flags )
  386. {
  387. int tx_bytes;
  388. if ( ( tx_bytes = send ( fSockfd, fTxBuffer, size, flags ) ) < 0 )
  389. {
  390. if ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) )
  391. {
  392. jack_error ( "Fatal error : %s.", strerror ( errno ) );
  393. throw JackDriverException ( "" );
  394. return -1;
  395. }
  396. else
  397. jack_error ( "Error in send : %s", strerror ( errno ) );
  398. }
  399. return tx_bytes;
  400. }
  401. //*************************************process************************************************************************
  402. int JackNetDriver::Read()
  403. {
  404. int rx_bytes;
  405. size_t recvd_midi_pckt = 0;
  406. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  407. fRxHeader.fIsLastPckt = 'n';
  408. //buffers
  409. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  410. fNetMidiCaptureBuffer->fPortBuffer[port_index] = GetMidiInputBuffer ( port_index );
  411. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  412. fNetAudioCaptureBuffer->fPortBuffer[port_index] = GetInputBuffer ( port_index );
  413. //receive sync (launch the cycle)
  414. do
  415. {
  416. if ( ( rx_bytes = Recv ( sizeof ( packet_header_t ), 0 ) ) < 1 )
  417. return rx_bytes;
  418. }
  419. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  420. JackDriver::CycleTakeTime();
  421. //audio, midi or sync if driver is late
  422. if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels )
  423. {
  424. do
  425. {
  426. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  427. if ( rx_bytes < 1 )
  428. return rx_bytes;
  429. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  430. {
  431. switch ( rx_head->fDataType )
  432. {
  433. case 'm': //midi
  434. rx_bytes = Recv ( rx_bytes, MSG_DONTWAIT );
  435. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  436. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  437. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  438. fNetMidiCaptureBuffer->RenderToJackPorts();
  439. break;
  440. case 'a': //audio
  441. rx_bytes = Recv ( fAudioRxLen, MSG_DONTWAIT );
  442. if ( !IsNextPacket ( &fRxHeader, rx_head, fNSubProcess ) )
  443. jack_error ( "Packet(s) missing..." );
  444. fRxHeader.fCycle = rx_head->fCycle;
  445. fRxHeader.fSubCycle = rx_head->fSubCycle;
  446. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  447. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  448. break;
  449. case 's': //sync
  450. jack_info ( "NetDriver : driver overloaded, skipping receive." );
  451. fRxHeader.fCycle = rx_head->fCycle;
  452. return 0;
  453. }
  454. }
  455. }
  456. while ( fRxHeader.fIsLastPckt != 'y' );
  457. }
  458. fRxHeader.fCycle = rx_head->fCycle;
  459. return 0;
  460. }
  461. int JackNetDriver::Write()
  462. {
  463. int tx_bytes, copy_size;
  464. fTxHeader.fCycle = fRxHeader.fCycle;
  465. fTxHeader.fSubCycle = 0;
  466. fTxHeader.fIsLastPckt = 'n';
  467. //buffers
  468. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  469. fNetMidiPlaybackBuffer->fPortBuffer[port_index] = GetMidiOutputBuffer ( port_index );
  470. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  471. fNetAudioPlaybackBuffer->fPortBuffer[port_index] = GetOutputBuffer ( port_index );
  472. //midi
  473. if ( fParams.fReturnMidiChannels )
  474. {
  475. fTxHeader.fDataType = 'm';
  476. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  477. fTxHeader.fNMidiPckt = GetNMidiPckt ( &fParams, fTxHeader.fMidiDataSize );
  478. for ( size_t subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  479. {
  480. fTxHeader.fSubCycle = subproc;
  481. if ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels )
  482. fTxHeader.fIsLastPckt = 'y';
  483. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  484. copy_size = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  485. tx_bytes = Send ( sizeof ( packet_header_t ) + copy_size, MSG_DONTWAIT );
  486. }
  487. }
  488. //audio
  489. if ( fParams.fReturnAudioChannels )
  490. {
  491. fTxHeader.fDataType = 'a';
  492. for ( size_t subproc = 0; subproc < fNSubProcess; subproc++ )
  493. {
  494. fTxHeader.fSubCycle = subproc;
  495. if ( subproc == ( fNSubProcess - 1 ) )
  496. fTxHeader.fIsLastPckt = 'y';
  497. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  498. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  499. tx_bytes = Send ( fAudioTxLen, MSG_DONTWAIT );
  500. }
  501. }
  502. return 0;
  503. }
  504. //*************************************loader*******************************************************
  505. #ifdef __cplusplus
  506. extern "C"
  507. {
  508. #endif
  509. jack_driver_desc_t* driver_get_descriptor ()
  510. {
  511. jack_driver_desc_t* desc = ( jack_driver_desc_t* ) calloc ( 1, sizeof ( jack_driver_desc_t ) );
  512. strcpy ( desc->name, "net" );
  513. desc->nparams = 7;
  514. desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );
  515. size_t i = 0;
  516. strcpy ( desc->params[i].name, "multicast_ip" );
  517. desc->params[i].character = 'a';
  518. desc->params[i].type = JackDriverParamString;
  519. strcpy ( desc->params[i].value.str, DEFAULT_MULTICAST_IP );
  520. strcpy ( desc->params[i].short_desc, "Multicast Address" );
  521. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  522. i++;
  523. strcpy ( desc->params[i].name, "udp_net_port" );
  524. desc->params[i].character = 'p';
  525. desc->params[i].type = JackDriverParamUInt;
  526. desc->params[i].value.ui = 19000U;
  527. strcpy ( desc->params[i].short_desc, "UDP port" );
  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 = JackDriverParamUInt;
  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. Jack::JackDriverClientInterface* driver_initialize ( Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params )
  567. {
  568. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  569. char name[JACK_CLIENT_NAME_SIZE];
  570. gethostname ( name, JACK_CLIENT_NAME_SIZE );
  571. jack_nframes_t udp_port = DEFAULT_PORT;
  572. jack_nframes_t period_size = 128;
  573. jack_nframes_t sample_rate = 48000;
  574. int audio_capture_ports = 2;
  575. int audio_playback_ports = 2;
  576. int midi_input_ports = 0;
  577. int midi_output_ports = 0;
  578. bool monitor = false;
  579. const JSList* node;
  580. const jack_driver_param_t* param;
  581. for ( node = params; node; node = jack_slist_next ( node ) )
  582. {
  583. param = ( const jack_driver_param_t* ) node->data;
  584. switch ( param->character )
  585. {
  586. case 'a' :
  587. multicast_ip = strdup ( param->value.str );
  588. break;
  589. case 'p':
  590. udp_port = param->value.ui;
  591. break;
  592. case 'C':
  593. audio_capture_ports = param->value.i;
  594. break;
  595. case 'P':
  596. audio_playback_ports = param->value.i;
  597. break;
  598. case 'i':
  599. midi_input_ports = param->value.i;
  600. break;
  601. case 'o':
  602. midi_output_ports = param->value.i;
  603. break;
  604. case 'n' :
  605. strncpy ( name, param->value.str, JACK_CLIENT_NAME_SIZE );
  606. }
  607. }
  608. Jack::JackDriverClientInterface* driver = new Jack::JackRestartThreadedDriver (
  609. new Jack::JackNetDriver ( "system", "net_pcm", engine, table, multicast_ip, udp_port, midi_input_ports, midi_output_ports, name ) );
  610. if ( driver->Open ( period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports,
  611. monitor, "from_master_", "to_master_", 0, 0 ) == 0 )
  612. return driver;
  613. delete driver;
  614. return NULL;
  615. }
  616. #ifdef __cplusplus
  617. }
  618. #endif
  619. }