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.

713 lines
24KB

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