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