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 "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, size_t port, 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. fPort = port;
  37. fParams.fSendMidiChannels = midi_input_ports;
  38. fParams.fReturnMidiChannels = midi_output_ports;
  39. strcpy ( fParams.fName, net_name );
  40. fSockfd = 0;
  41. }
  42. JackNetDriver::~JackNetDriver()
  43. {
  44. if ( fSockfd )
  45. close ( fSockfd );
  46. delete fNetAudioCaptureBuffer;
  47. delete fNetAudioPlaybackBuffer;
  48. delete fNetMidiCaptureBuffer;
  49. delete fNetMidiPlaybackBuffer;
  50. delete[] fTxBuffer;
  51. delete[] fRxBuffer;
  52. delete[] fMulticastIP;
  53. delete[] fMidiCapturePortList;
  54. delete[] fMidiPlaybackPortList;
  55. }
  56. //*************************************initialization***********************************************************************
  57. int JackNetDriver::Open ( jack_nframes_t nframes, jack_nframes_t samplerate, bool capturing, bool playing,
  58. int inchannels, int outchannels, bool monitor,
  59. const char* capture_driver_name, const char* playback_driver_name,
  60. jack_nframes_t capture_latency, jack_nframes_t playback_latency )
  61. {
  62. int res = JackAudioDriver::Open ( nframes, samplerate, capturing, playing, inchannels, outchannels, monitor,
  63. capture_driver_name, playback_driver_name, capture_latency, playback_latency );
  64. fEngineControl->fPeriod = 0;
  65. fEngineControl->fComputation = 500 * 1000;
  66. fEngineControl->fConstraint = 500 * 1000;
  67. return res;
  68. }
  69. int JackNetDriver::Attach()
  70. {
  71. return 0;
  72. }
  73. int JackNetDriver::Detach()
  74. {
  75. return 0;
  76. }
  77. bool JackNetDriver::Init()
  78. {
  79. jack_log ( "JackNetDriver::Init()" );
  80. if ( fSockfd )
  81. Restart();
  82. //set the parameters to send
  83. strcpy ( fParams.fPacketType, "params" );
  84. fParams.fProtocolVersion = 'a';
  85. SetPacketType ( &fParams, SLAVE_AVAILABLE );
  86. gethostname ( fParams.fSlaveNetName, 255 );
  87. fParams.fSendAudioChannels = fCaptureChannels;
  88. fParams.fReturnAudioChannels = fPlaybackChannels;
  89. //init loop : get a master and start, do it until connection is ok
  90. net_status_t status;
  91. do
  92. {
  93. //first, get a master, do it until a valid connection is running
  94. jack_info ( "Initializing Net Driver..." );
  95. do
  96. {
  97. status = GetNetMaster();
  98. if ( status == SOCKET_ERROR )
  99. return false;
  100. }
  101. while ( status != CONNECTED );
  102. //then tell the master we are ready
  103. jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
  104. status = SendMasterStartSync();
  105. if ( status == NET_ERROR )
  106. return false;
  107. }
  108. while ( status != ROLLING );
  109. //driver parametering
  110. if ( SetParams() )
  111. {
  112. jack_error ( "Fatal error : can't alloc net driver ports." );
  113. return false;
  114. }
  115. //init done, display parameters
  116. SessionParamsDisplay ( &fParams );
  117. return true;
  118. }
  119. net_status_t JackNetDriver::GetNetMaster()
  120. {
  121. jack_log ( "JackNetDriver::GetNetMaster()" );
  122. //utility
  123. session_params_t params;
  124. struct sockaddr_in mcast_addr, listen_addr;
  125. struct timeval rcv_timeout;
  126. rcv_timeout.tv_sec = 2;
  127. rcv_timeout.tv_usec = 0;
  128. socklen_t addr_len = sizeof ( socket_address_t );
  129. int rx_bytes = 0;
  130. //set the multicast address
  131. mcast_addr.sin_family = AF_INET;
  132. mcast_addr.sin_port = htons ( fPort );
  133. inet_aton ( fMulticastIP, &mcast_addr.sin_addr );
  134. memset ( &mcast_addr.sin_zero, 0, 8 );
  135. //set the listening address
  136. listen_addr.sin_family = AF_INET;
  137. listen_addr.sin_port = htons ( fPort );
  138. listen_addr.sin_addr.s_addr = htonl ( INADDR_ANY );
  139. memset ( &listen_addr.sin_zero, 0, 8 );
  140. //set the master address family
  141. fMasterAddr.sin_family = AF_INET;
  142. //socket
  143. if ( fSockfd )
  144. close ( fSockfd );
  145. if ( ( fSockfd = socket ( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )
  146. {
  147. jack_error ( "Fatal error : network unreachable - %s", strerror ( errno ) );
  148. return SOCKET_ERROR;
  149. }
  150. //bind the socket
  151. if ( bind ( fSockfd, reinterpret_cast<socket_address_t*> ( &listen_addr ), addr_len ) < 0 )
  152. jack_error ( "Can't bind the socket : %s", strerror ( errno ) );
  153. //timeout on receive
  154. setsockopt ( fSockfd, SOL_SOCKET, SO_RCVTIMEO, &rcv_timeout, sizeof ( rcv_timeout ) );
  155. //send 'AVAILABLE' until 'SLAVE_SETUP' received
  156. jack_info ( "Waiting for a master..." );
  157. do
  158. {
  159. //send 'available'
  160. if ( sendto ( fSockfd, &fParams, sizeof ( session_params_t ), 0,
  161. reinterpret_cast<socket_address_t*> ( &mcast_addr ), addr_len ) < 0 )
  162. jack_error ( "Error in data send : %s", strerror ( errno ) );
  163. //filter incoming packets : don't exit while receiving wrong packets
  164. do
  165. {
  166. rx_bytes = recvfrom ( fSockfd, &params, sizeof ( session_params_t ), 0,
  167. reinterpret_cast<socket_address_t*> ( &fMasterAddr ), &addr_len );
  168. if ( ( rx_bytes < 0 ) && ( errno != EAGAIN ) )
  169. {
  170. jack_error ( "Can't receive : %s", strerror ( errno ) );
  171. return RECV_ERROR;
  172. }
  173. }
  174. while ( ( rx_bytes > 0 ) && strcmp ( params.fPacketType, fParams.fPacketType ) );
  175. }
  176. while ( ( GetPacketType ( &params ) != SLAVE_SETUP ) );
  177. //connect the socket
  178. if ( connect ( fSockfd, reinterpret_cast<socket_address_t*> ( &fMasterAddr ), sizeof ( socket_address_t ) ) < 0 )
  179. {
  180. jack_error ( "Error in connect : %s", strerror ( errno ) );
  181. return CONNECT_ERROR;
  182. }
  183. //everything is OK, copy parameters and return
  184. fParams = params;
  185. return CONNECTED;
  186. }
  187. net_status_t JackNetDriver::SendMasterStartSync()
  188. {
  189. jack_log ( "JackNetDriver::GetNetMasterStartSync()" );
  190. //tell the master to start
  191. SetPacketType ( &fParams, START_MASTER );
  192. if ( send ( fSockfd, &fParams, sizeof ( session_params_t ), 0 ) < 0 )
  193. {
  194. jack_error ( "Error in send : %s", strerror ( errno ) );
  195. return ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) ) ? NET_ERROR : SEND_ERROR;
  196. }
  197. return ROLLING;
  198. }
  199. void JackNetDriver::Restart()
  200. {
  201. jack_info ( "Restarting driver..." );
  202. close ( fSockfd );
  203. delete[] fTxBuffer;
  204. delete[] fRxBuffer;
  205. delete fNetAudioCaptureBuffer;
  206. delete fNetAudioPlaybackBuffer;
  207. delete fNetMidiCaptureBuffer;
  208. delete fNetMidiPlaybackBuffer;
  209. FreePorts();
  210. delete[] fMidiCapturePortList;
  211. delete[] fMidiPlaybackPortList;
  212. fTxBuffer = NULL;
  213. fRxBuffer = NULL;
  214. fNetAudioCaptureBuffer = NULL;
  215. fNetAudioPlaybackBuffer = NULL;
  216. fNetMidiCaptureBuffer = NULL;
  217. fNetMidiPlaybackBuffer = NULL;
  218. fMidiCapturePortList = NULL;
  219. fMidiPlaybackPortList = NULL;
  220. }
  221. int JackNetDriver::SetParams()
  222. {
  223. fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
  224. SetBufferSize ( fParams.fPeriodSize );
  225. SetSampleRate ( fParams.fSampleRate );
  226. //allocate midi ports lists
  227. fMidiCapturePortList = new jack_port_id_t [fParams.fSendMidiChannels];
  228. fMidiPlaybackPortList = new jack_port_id_t [fParams.fReturnMidiChannels];
  229. //register jack ports
  230. if ( AllocPorts() != 0 )
  231. {
  232. jack_error ( "Can't allocate ports." );
  233. return -1;
  234. }
  235. //TX header init
  236. strcpy ( fTxHeader.fPacketType, "header" );
  237. fTxHeader.fDataStream = 'r';
  238. fTxHeader.fID = fParams.fID;
  239. fTxHeader.fCycle = 0;
  240. fTxHeader.fSubCycle = 0;
  241. fTxHeader.fMidiDataSize = 0;
  242. fTxHeader.fBitdepth = fParams.fBitdepth;
  243. //RX header init
  244. strcpy ( fRxHeader.fPacketType, "header" );
  245. fRxHeader.fDataStream = 's';
  246. fRxHeader.fID = fParams.fID;
  247. fRxHeader.fCycle = 0;
  248. fRxHeader.fSubCycle = 0;
  249. fRxHeader.fMidiDataSize = 0;
  250. fRxHeader.fBitdepth = fParams.fBitdepth;
  251. //network buffers
  252. fTxBuffer = new char[fParams.fMtu];
  253. fRxBuffer = new char[fParams.fMtu];
  254. //net audio/midi buffers
  255. fTxData = fTxBuffer + sizeof ( packet_header_t );
  256. fRxData = fRxBuffer + sizeof ( packet_header_t );
  257. //midi net buffers
  258. fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
  259. fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
  260. //audio net buffers
  261. fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
  262. fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
  263. //audio netbuffer length
  264. fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
  265. fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
  266. return 0;
  267. }
  268. int JackNetDriver::AllocPorts()
  269. {
  270. jack_log ( "JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate );
  271. JackPort* port;
  272. jack_port_id_t port_id;
  273. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  274. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  275. unsigned long port_flags;
  276. //audio
  277. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  278. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  279. {
  280. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, port_index + 1 );
  281. snprintf ( name, sizeof ( name ) - 1, "%s:capture_%d", fClientControl->fName, port_index + 1 );
  282. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  283. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  284. {
  285. jack_error ( "driver: cannot register port for %s", name );
  286. return -1;
  287. }
  288. port = fGraphManager->GetPort ( port_id );
  289. port->SetAlias ( alias );
  290. port->SetLatency ( fEngineControl->fBufferSize + fCaptureLatency );
  291. fCapturePortList[port_index] = port_id;
  292. jack_log ( "JackNetDriver::AllocPorts() fCapturePortList[%d] port_index = %ld", port_index, port_id );
  293. }
  294. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  295. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  296. {
  297. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, port_index + 1 );
  298. snprintf ( name, sizeof ( name ) - 1, "%s:playback_%d",fClientControl->fName, port_index + 1 );
  299. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_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->SetAlias ( alias );
  307. port->SetLatency ( fEngineControl->fBufferSize + ( ( fEngineControl->fSyncMode ) ? 0 : fEngineControl->fBufferSize ) + fPlaybackLatency );
  308. fPlaybackPortList[port_index] = port_id;
  309. jack_log ( "JackNetDriver::AllocPorts() fPlaybackPortList[%d] port_index = %ld", port_index, port_id );
  310. }
  311. //midi
  312. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  313. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  314. {
  315. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, port_index + 1 );
  316. snprintf ( name, sizeof ( name ) - 1, "%s:midi_capture_%d", fClientControl->fName, port_index + 1 );
  317. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  318. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  319. {
  320. jack_error ( "driver: cannot register port for %s", name );
  321. return -1;
  322. }
  323. fMidiCapturePortList[port_index] = port_id;
  324. jack_log ( "JackNetDriver::AllocPorts() fMidiCapturePortList[%d] port_index = %ld", port_index, port_id );
  325. }
  326. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  327. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  328. {
  329. snprintf ( alias, sizeof ( alias ) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, port_index + 1 );
  330. snprintf ( name, sizeof ( name ) - 1, "%s:midi_playback_%d", fClientControl->fName, port_index + 1 );
  331. if ( ( port_id = fGraphManager->AllocatePort ( fClientControl->fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  332. static_cast<JackPortFlags> ( port_flags ), fEngineControl->fBufferSize ) ) == NO_PORT )
  333. {
  334. jack_error ( "driver: cannot register port for %s", name );
  335. return -1;
  336. }
  337. fMidiPlaybackPortList[port_index] = port_id;
  338. jack_log ( "JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] port_index = %ld", port_index, port_id );
  339. }
  340. return 0;
  341. }
  342. int JackNetDriver::FreePorts()
  343. {
  344. jack_log ( "JackNetDriver::FreePorts" );
  345. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  346. fGraphManager->ReleasePort ( fClientControl->fRefNum, fCapturePortList[port_index] );
  347. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  348. fGraphManager->ReleasePort ( fClientControl->fRefNum, fPlaybackPortList[port_index] );
  349. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  350. fGraphManager->ReleasePort ( fClientControl->fRefNum, fMidiCapturePortList[port_index] );
  351. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  352. fGraphManager->ReleasePort ( fClientControl->fRefNum, fMidiPlaybackPortList[port_index] );
  353. return 0;
  354. }
  355. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer ( int port_index )
  356. {
  357. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiCapturePortList[port_index], fEngineControl->fBufferSize ) );
  358. }
  359. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer ( int port_index )
  360. {
  361. return static_cast<JackMidiBuffer*> ( fGraphManager->GetBuffer ( fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize ) );
  362. }
  363. int JackNetDriver::Recv ( size_t size, int flags )
  364. {
  365. int rx_bytes;
  366. if ( ( rx_bytes = recv ( fSockfd, fRxBuffer, size, flags ) ) < 0 )
  367. {
  368. if ( errno == EAGAIN )
  369. {
  370. jack_error ( "No incoming data, is the master still running ?" );
  371. return 0;
  372. }
  373. else if ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) )
  374. {
  375. jack_error ( "Fatal error : %s.", strerror ( errno ) );
  376. throw JackDriverException();
  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. }
  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, 0 );
  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, 0 );
  500. }
  501. }
  502. return 0;
  503. }
  504. //*************************************loader*******************************************************
  505. #ifdef __cplusplus
  506. extern "C"
  507. {
  508. #endif
  509. EXPORT 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. EXPORT 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::JackWaitThreadedDriver(
  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. }