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.

704 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. }
  384. else
  385. {
  386. jack_error ( "Error in receive : %s", strerror ( errno ) );
  387. return 0;
  388. }
  389. }
  390. return rx_bytes;
  391. }
  392. int JackNetDriver::Send ( size_t size, int flags )
  393. {
  394. int tx_bytes;
  395. if ( ( tx_bytes = send ( fSockfd, fTxBuffer, size, flags ) ) < 0 )
  396. {
  397. if ( ( errno == ECONNABORTED ) || ( errno == ECONNREFUSED ) || ( errno == ECONNRESET ) )
  398. {
  399. jack_error ( "Fatal error : %s.", strerror ( errno ) );
  400. throw JackDriverException();
  401. }
  402. else
  403. jack_error ( "Error in send : %s", strerror ( errno ) );
  404. }
  405. return tx_bytes;
  406. }
  407. //*************************************process************************************************************************
  408. int JackNetDriver::Read()
  409. {
  410. int rx_bytes;
  411. size_t recvd_midi_pckt = 0;
  412. packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
  413. fRxHeader.fIsLastPckt = 'n';
  414. //buffers
  415. for ( int port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
  416. fNetMidiCaptureBuffer->fPortBuffer[port_index] = GetMidiInputBuffer ( port_index );
  417. for ( int port_index = 0; port_index < fCaptureChannels; port_index++ )
  418. fNetAudioCaptureBuffer->fPortBuffer[port_index] = GetInputBuffer ( port_index );
  419. //receive sync (launch the cycle)
  420. do
  421. {
  422. if ( ( rx_bytes = Recv ( sizeof ( packet_header_t ), 0 ) ) < 1 )
  423. return rx_bytes;
  424. }
  425. while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
  426. JackDriver::CycleTakeTime();
  427. //audio, midi or sync if driver is late
  428. if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels )
  429. {
  430. do
  431. {
  432. rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
  433. if ( rx_bytes < 1 )
  434. return rx_bytes;
  435. if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
  436. {
  437. switch ( rx_head->fDataType )
  438. {
  439. case 'm': //midi
  440. rx_bytes = Recv ( rx_bytes, MSG_DONTWAIT );
  441. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  442. fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
  443. if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
  444. fNetMidiCaptureBuffer->RenderToJackPorts();
  445. break;
  446. case 'a': //audio
  447. rx_bytes = Recv ( fAudioRxLen, MSG_DONTWAIT );
  448. if ( !IsNextPacket ( &fRxHeader, rx_head, fNSubProcess ) )
  449. jack_error ( "Packet(s) missing..." );
  450. fRxHeader.fCycle = rx_head->fCycle;
  451. fRxHeader.fSubCycle = rx_head->fSubCycle;
  452. fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
  453. fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
  454. break;
  455. case 's': //sync
  456. jack_info ( "NetDriver : driver overloaded, skipping receive." );
  457. fRxHeader.fCycle = rx_head->fCycle;
  458. return 0;
  459. }
  460. }
  461. }
  462. while ( fRxHeader.fIsLastPckt != 'y' );
  463. }
  464. fRxHeader.fCycle = rx_head->fCycle;
  465. return 0;
  466. }
  467. int JackNetDriver::Write()
  468. {
  469. int tx_bytes, copy_size;
  470. fTxHeader.fCycle = fRxHeader.fCycle;
  471. fTxHeader.fSubCycle = 0;
  472. fTxHeader.fIsLastPckt = 'n';
  473. //buffers
  474. for ( int port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
  475. fNetMidiPlaybackBuffer->fPortBuffer[port_index] = GetMidiOutputBuffer ( port_index );
  476. for ( int port_index = 0; port_index < fPlaybackChannels; port_index++ )
  477. fNetAudioPlaybackBuffer->fPortBuffer[port_index] = GetOutputBuffer ( port_index );
  478. //midi
  479. if ( fParams.fReturnMidiChannels )
  480. {
  481. fTxHeader.fDataType = 'm';
  482. fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
  483. fTxHeader.fNMidiPckt = GetNMidiPckt ( &fParams, fTxHeader.fMidiDataSize );
  484. for ( size_t subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
  485. {
  486. fTxHeader.fSubCycle = subproc;
  487. if ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels )
  488. fTxHeader.fIsLastPckt = 'y';
  489. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  490. copy_size = fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
  491. tx_bytes = Send ( sizeof ( packet_header_t ) + copy_size, MSG_DONTWAIT );
  492. }
  493. }
  494. //audio
  495. if ( fParams.fReturnAudioChannels )
  496. {
  497. fTxHeader.fDataType = 'a';
  498. for ( size_t subproc = 0; subproc < fNSubProcess; subproc++ )
  499. {
  500. fTxHeader.fSubCycle = subproc;
  501. if ( subproc == ( fNSubProcess - 1 ) )
  502. fTxHeader.fIsLastPckt = 'y';
  503. fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
  504. memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
  505. tx_bytes = Send ( fAudioTxLen, MSG_DONTWAIT );
  506. }
  507. }
  508. return 0;
  509. }
  510. //*************************************loader*******************************************************
  511. #ifdef __cplusplus
  512. extern "C"
  513. {
  514. #endif
  515. EXPORT jack_driver_desc_t* driver_get_descriptor ()
  516. {
  517. jack_driver_desc_t* desc = ( jack_driver_desc_t* ) calloc ( 1, sizeof ( jack_driver_desc_t ) );
  518. strcpy ( desc->name, "net" );
  519. desc->nparams = 7;
  520. desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );
  521. size_t i = 0;
  522. strcpy ( desc->params[i].name, "multicast_ip" );
  523. desc->params[i].character = 'a';
  524. desc->params[i].type = JackDriverParamString;
  525. strcpy ( desc->params[i].value.str, DEFAULT_MULTICAST_IP );
  526. strcpy ( desc->params[i].short_desc, "Multicast Address" );
  527. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  528. i++;
  529. strcpy ( desc->params[i].name, "udp_net_port" );
  530. desc->params[i].character = 'p';
  531. desc->params[i].type = JackDriverParamUInt;
  532. desc->params[i].value.ui = 19000U;
  533. strcpy ( desc->params[i].short_desc, "UDP port" );
  534. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  535. i++;
  536. strcpy ( desc->params[i].name, "input_ports" );
  537. desc->params[i].character = 'C';
  538. desc->params[i].type = JackDriverParamInt;
  539. desc->params[i].value.i = 2;
  540. strcpy ( desc->params[i].short_desc, "Number of audio input ports" );
  541. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  542. i++;
  543. strcpy ( desc->params[i].name, "output_ports" );
  544. desc->params[i].character = 'P';
  545. desc->params[i].type = JackDriverParamUInt;
  546. desc->params[i].value.i = 2;
  547. strcpy ( desc->params[i].short_desc, "Number of audio output ports" );
  548. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  549. i++;
  550. strcpy ( desc->params[i].name, "midi_in_ports" );
  551. desc->params[i].character = 'i';
  552. desc->params[i].type = JackDriverParamInt;
  553. desc->params[i].value.i = 0;
  554. strcpy ( desc->params[i].short_desc, "Number of midi input ports" );
  555. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  556. i++;
  557. strcpy ( desc->params[i].name, "midi_out_ports" );
  558. desc->params[i].character = 'o';
  559. desc->params[i].type = JackDriverParamUInt;
  560. desc->params[i].value.i = 0;
  561. strcpy ( desc->params[i].short_desc, "Number of midi output ports" );
  562. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  563. i++;
  564. strcpy ( desc->params[i].name, "client_name" );
  565. desc->params[i].character = 'n';
  566. desc->params[i].type = JackDriverParamString;
  567. strcpy ( desc->params[i].value.str, "'hostname'" );
  568. strcpy ( desc->params[i].short_desc, "Name of the jack client" );
  569. strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
  570. return desc;
  571. }
  572. EXPORT Jack::JackDriverClientInterface* driver_initialize ( Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params )
  573. {
  574. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  575. char name[JACK_CLIENT_NAME_SIZE];
  576. gethostname ( name, JACK_CLIENT_NAME_SIZE );
  577. jack_nframes_t udp_port = DEFAULT_PORT;
  578. jack_nframes_t period_size = 128;
  579. jack_nframes_t sample_rate = 48000;
  580. int audio_capture_ports = 2;
  581. int audio_playback_ports = 2;
  582. int midi_input_ports = 0;
  583. int midi_output_ports = 0;
  584. bool monitor = false;
  585. const JSList* node;
  586. const jack_driver_param_t* param;
  587. for ( node = params; node; node = jack_slist_next ( node ) )
  588. {
  589. param = ( const jack_driver_param_t* ) node->data;
  590. switch ( param->character )
  591. {
  592. case 'a' :
  593. multicast_ip = strdup ( param->value.str );
  594. break;
  595. case 'p':
  596. udp_port = param->value.ui;
  597. break;
  598. case 'C':
  599. audio_capture_ports = param->value.i;
  600. break;
  601. case 'P':
  602. audio_playback_ports = param->value.i;
  603. break;
  604. case 'i':
  605. midi_input_ports = param->value.i;
  606. break;
  607. case 'o':
  608. midi_output_ports = param->value.i;
  609. break;
  610. case 'n' :
  611. strncpy ( name, param->value.str, JACK_CLIENT_NAME_SIZE );
  612. }
  613. }
  614. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver(
  615. new Jack::JackNetDriver("system", "net_pcm", engine, table, multicast_ip, udp_port, midi_input_ports, midi_output_ports, name));
  616. if (driver->Open (period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports,
  617. monitor, "from_master_", "to_master_", 0, 0 ) == 0)
  618. return driver;
  619. delete driver;
  620. return NULL;
  621. }
  622. #ifdef __cplusplus
  623. }
  624. #endif
  625. }