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.

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