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.

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