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.

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