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.

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