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.

735 lines
30KB

  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 "JackLockedEngine.h"
  19. #include "JackGraphManager.h"
  20. #include "JackWaitThreadedDriver.h"
  21. using namespace std;
  22. namespace Jack
  23. {
  24. JackNetDriver::JackNetDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table,
  25. const char* ip, int port, int mtu, int midi_input_ports, int midi_output_ports,
  26. char* net_name, uint transport_sync, char network_mode, int celt_encoding)
  27. : JackAudioDriver(name, alias, engine, table), JackNetSlaveInterface(ip, port)
  28. {
  29. jack_log("JackNetDriver::JackNetDriver ip %s, port %d", ip, port);
  30. // Use the hostname if no name parameter was given
  31. if (strcmp(net_name, "") == 0)
  32. GetHostName(net_name, JACK_CLIENT_NAME_SIZE);
  33. fParams.fMtu = mtu;
  34. fParams.fSendMidiChannels = midi_input_ports;
  35. fParams.fReturnMidiChannels = midi_output_ports;
  36. if (celt_encoding > 0) {
  37. fParams.fSampleEncoder = JackCeltEncoder;
  38. fParams.fKBps = celt_encoding;
  39. } else {
  40. fParams.fSampleEncoder = JackFloatEncoder;
  41. }
  42. strcpy(fParams.fName, net_name);
  43. fSocket.GetName(fParams.fSlaveNetName);
  44. fParams.fTransportSync = transport_sync;
  45. fParams.fNetworkMode = network_mode;
  46. fSendTransportData.fState = -1;
  47. fReturnTransportData.fState = -1;
  48. fLastTransportState = -1;
  49. fLastTimebaseMaster = -1;
  50. fMidiCapturePortList = NULL;
  51. fMidiPlaybackPortList = NULL;
  52. #ifdef JACK_MONITOR
  53. fNetTimeMon = NULL;
  54. fRcvSyncUst = 0;
  55. #endif
  56. }
  57. JackNetDriver::~JackNetDriver()
  58. {
  59. delete[] fMidiCapturePortList;
  60. delete[] fMidiPlaybackPortList;
  61. #ifdef JACK_MONITOR
  62. delete fNetTimeMon;
  63. #endif
  64. }
  65. //open, close, attach and detach------------------------------------------------------
  66. int JackNetDriver::Open(jack_nframes_t buffer_size, jack_nframes_t samplerate, bool capturing, bool playing,
  67. int inchannels, int outchannels, bool monitor,
  68. const char* capture_driver_name, const char* playback_driver_name,
  69. jack_nframes_t capture_latency, jack_nframes_t playback_latency)
  70. {
  71. if (JackAudioDriver::Open(buffer_size,
  72. samplerate,
  73. capturing,
  74. playing,
  75. inchannels,
  76. outchannels,
  77. monitor,
  78. capture_driver_name,
  79. playback_driver_name,
  80. capture_latency,
  81. playback_latency) == 0)
  82. {
  83. fEngineControl->fPeriod = 0;
  84. fEngineControl->fComputation = 500 * 1000;
  85. fEngineControl->fConstraint = 500 * 1000;
  86. return 0;
  87. } else {
  88. return -1;
  89. }
  90. }
  91. int JackNetDriver::Close()
  92. {
  93. #ifdef JACK_MONITOR
  94. if (fNetTimeMon)
  95. fNetTimeMon->Save();
  96. #endif
  97. FreeAll();
  98. return JackDriver::Close();
  99. }
  100. // Attach and Detach are defined as empty methods: port allocation is done when driver actually start (that is in Init)
  101. int JackNetDriver::Attach()
  102. {
  103. return 0;
  104. }
  105. int JackNetDriver::Detach()
  106. {
  107. return 0;
  108. }
  109. //init and restart--------------------------------------------------------------------
  110. /*
  111. JackNetDriver is wrapped in a JackWaitThreadedDriver decorator that behaves
  112. as a "dummy driver, until Init method returns.
  113. */
  114. bool JackNetDriver::Initialize()
  115. {
  116. jack_log("JackNetDriver::Initialize()");
  117. FreePorts();
  118. //new loading, but existing socket, restart the driver
  119. if (fSocket.IsSocket()) {
  120. jack_info("Restarting driver...");
  121. FreeAll();
  122. }
  123. //set the parameters to send
  124. fParams.fSendAudioChannels = fCaptureChannels;
  125. fParams.fReturnAudioChannels = fPlaybackChannels;
  126. fParams.fSlaveSyncMode = fEngineControl->fSyncMode;
  127. //display some additional infos
  128. jack_info("NetDriver started in %s mode %s Master's transport sync.",
  129. (fParams.fSlaveSyncMode) ? "sync" : "async", (fParams.fTransportSync) ? "with" : "without");
  130. //init network
  131. if (!JackNetSlaveInterface::Init()) {
  132. jack_error("Starting network fails...");
  133. return false;
  134. }
  135. //set global parameters
  136. if (!SetParams()) {
  137. jack_error("SetParams error...");
  138. return false;
  139. }
  140. // If -1 at conection time, in/out channels count is sent by the master
  141. fCaptureChannels = fParams.fSendAudioChannels;
  142. fPlaybackChannels = fParams.fReturnAudioChannels;
  143. //allocate midi ports lists
  144. fMidiCapturePortList = new jack_port_id_t [fParams.fSendMidiChannels];
  145. fMidiPlaybackPortList = new jack_port_id_t [fParams.fReturnMidiChannels];
  146. assert(fMidiCapturePortList);
  147. assert(fMidiPlaybackPortList);
  148. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  149. fMidiCapturePortList[midi_port_index] = 0;
  150. }
  151. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  152. fMidiPlaybackPortList[midi_port_index] = 0;
  153. }
  154. //register jack ports
  155. if (AllocPorts() != 0) {
  156. jack_error("Can't allocate ports.");
  157. return false;
  158. }
  159. //init done, display parameters
  160. SessionParamsDisplay(&fParams);
  161. //monitor
  162. #ifdef JACK_MONITOR
  163. string plot_name;
  164. //NetTimeMon
  165. plot_name = string(fParams.fName);
  166. plot_name += string("_slave");
  167. plot_name += (fEngineControl->fSyncMode) ? string("_sync") : string("_async");
  168. switch (fParams.fNetworkMode)
  169. {
  170. case 's' :
  171. plot_name += string("_slow");
  172. break;
  173. case 'n' :
  174. plot_name += string("_normal");
  175. break;
  176. case 'f' :
  177. plot_name += string("_fast");
  178. break;
  179. }
  180. fNetTimeMon = new JackGnuPlotMonitor<float>(128, 5, plot_name);
  181. string net_time_mon_fields[] =
  182. {
  183. string("sync decoded"),
  184. string("end of read"),
  185. string("start of write"),
  186. string("sync send"),
  187. string("end of write")
  188. };
  189. string net_time_mon_options[] =
  190. {
  191. string("set xlabel \"audio cycles\""),
  192. string("set ylabel \"% of audio cycle\"")
  193. };
  194. fNetTimeMon->SetPlotFile(net_time_mon_options, 2, net_time_mon_fields, 5);
  195. #endif
  196. //driver parametering
  197. JackAudioDriver::SetBufferSize(fParams.fPeriodSize);
  198. JackAudioDriver::SetSampleRate(fParams.fSampleRate);
  199. JackDriver::NotifyBufferSize(fParams.fPeriodSize);
  200. JackDriver::NotifySampleRate(fParams.fSampleRate);
  201. //transport engine parametering
  202. fEngineControl->fTransport.SetNetworkSync(fParams.fTransportSync);
  203. return true;
  204. }
  205. void JackNetDriver::FreeAll()
  206. {
  207. FreePorts();
  208. delete[] fTxBuffer;
  209. delete[] fRxBuffer;
  210. delete fNetAudioCaptureBuffer;
  211. delete fNetAudioPlaybackBuffer;
  212. delete fNetMidiCaptureBuffer;
  213. delete fNetMidiPlaybackBuffer;
  214. delete[] fMidiCapturePortList;
  215. delete[] fMidiPlaybackPortList;
  216. fTxBuffer = NULL;
  217. fRxBuffer = NULL;
  218. fNetAudioCaptureBuffer = NULL;
  219. fNetAudioPlaybackBuffer = NULL;
  220. fNetMidiCaptureBuffer = NULL;
  221. fNetMidiPlaybackBuffer = NULL;
  222. fMidiCapturePortList = NULL;
  223. fMidiPlaybackPortList = NULL;
  224. #ifdef JACK_MONITOR
  225. delete fNetTimeMon;
  226. fNetTimeMon = NULL;
  227. #endif
  228. }
  229. //jack ports and buffers--------------------------------------------------------------
  230. int JackNetDriver::AllocPorts()
  231. {
  232. jack_log("JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  233. JackPort* port;
  234. jack_port_id_t port_index;
  235. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  236. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  237. unsigned long port_flags;
  238. int audio_port_index;
  239. int midi_port_index;
  240. jack_latency_range_t range;
  241. //audio
  242. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  243. for (audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++)
  244. {
  245. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, audio_port_index + 1);
  246. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, audio_port_index + 1);
  247. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  248. static_cast<JackPortFlags>(port_flags), fEngineControl->fBufferSize, &port_index) < 0)
  249. {
  250. jack_error("driver: cannot register port for %s", name);
  251. return -1;
  252. }
  253. port = fGraphManager->GetPort(port_index);
  254. port->SetAlias(alias);
  255. //port latency
  256. range.min = range.max = fEngineControl->fBufferSize;
  257. port->SetLatencyRange(JackCaptureLatency, &range);
  258. fCapturePortList[audio_port_index] = port_index;
  259. jack_log("JackNetDriver::AllocPorts() fCapturePortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_index, port->GetLatency());
  260. }
  261. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  262. for (audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++)
  263. {
  264. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, audio_port_index + 1);
  265. snprintf(name, sizeof(name) - 1, "%s:playback_%d",fClientControl.fName, audio_port_index + 1);
  266. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  267. static_cast<JackPortFlags>(port_flags), fEngineControl->fBufferSize, &port_index) < 0)
  268. {
  269. jack_error("driver: cannot register port for %s", name);
  270. return -1;
  271. }
  272. port = fGraphManager->GetPort(port_index);
  273. port->SetAlias(alias);
  274. //port latency
  275. switch (fParams.fNetworkMode)
  276. {
  277. case 'f' :
  278. range.min = range.max = (fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize;
  279. break;
  280. case 'n' :
  281. range.min = range.max = (fEngineControl->fBufferSize + (fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize);
  282. break;
  283. case 's' :
  284. range.min = range.max = (2 * fEngineControl->fBufferSize + (fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize);
  285. break;
  286. }
  287. port->SetLatencyRange(JackPlaybackLatency, &range);
  288. fPlaybackPortList[audio_port_index] = port_index;
  289. jack_log("JackNetDriver::AllocPorts() fPlaybackPortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_index, port->GetLatency());
  290. }
  291. //midi
  292. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  293. for (midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++)
  294. {
  295. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, midi_port_index + 1);
  296. snprintf(name, sizeof (name) - 1, "%s:midi_capture_%d", fClientControl.fName, midi_port_index + 1);
  297. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  298. static_cast<JackPortFlags>(port_flags), fEngineControl->fBufferSize, &port_index) < 0)
  299. {
  300. jack_error("driver: cannot register port for %s", name);
  301. return -1;
  302. }
  303. port = fGraphManager->GetPort(port_index);
  304. //port latency
  305. range.min = range.max = fEngineControl->fBufferSize;
  306. port->SetLatencyRange(JackCaptureLatency, &range);
  307. fMidiCapturePortList[midi_port_index] = port_index;
  308. jack_log("JackNetDriver::AllocPorts() fMidiCapturePortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_index, port->GetLatency());
  309. }
  310. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  311. for (midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++)
  312. {
  313. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, midi_port_index + 1);
  314. snprintf(name, sizeof(name) - 1, "%s:midi_playback_%d", fClientControl.fName, midi_port_index + 1);
  315. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  316. static_cast<JackPortFlags>(port_flags), fEngineControl->fBufferSize, &port_index) < 0)
  317. {
  318. jack_error("driver: cannot register port for %s", name);
  319. return -1;
  320. }
  321. port = fGraphManager->GetPort(port_index);
  322. //port latency
  323. switch (fParams.fNetworkMode)
  324. {
  325. case 'f' :
  326. range.min = range.max = (fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize;
  327. break;
  328. case 'n' :
  329. range.min = range.max = (fEngineControl->fBufferSize + (fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize);
  330. break;
  331. case 's' :
  332. range.min = range.max = (2 * fEngineControl->fBufferSize + (fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize);
  333. break;
  334. }
  335. port->SetLatencyRange(JackPlaybackLatency, &range);
  336. fMidiPlaybackPortList[midi_port_index] = port_index;
  337. jack_log("JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_index, port->GetLatency());
  338. }
  339. return 0;
  340. }
  341. int JackNetDriver::FreePorts()
  342. {
  343. jack_log("JackNetDriver::FreePorts");
  344. for (int audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++) {
  345. if (fCapturePortList[audio_port_index] > 0) {
  346. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[audio_port_index]);
  347. fCapturePortList[audio_port_index] = 0;
  348. }
  349. }
  350. for (int audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++) {
  351. if (fPlaybackPortList[audio_port_index] > 0) {
  352. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[audio_port_index]);
  353. fPlaybackPortList[audio_port_index] = 0;
  354. }
  355. }
  356. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  357. if (fMidiCapturePortList && fMidiCapturePortList[midi_port_index] > 0) {
  358. fGraphManager->ReleasePort(fClientControl.fRefNum, fMidiCapturePortList[midi_port_index]);
  359. fMidiCapturePortList[midi_port_index] = 0;
  360. }
  361. }
  362. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  363. if (fMidiPlaybackPortList && fMidiPlaybackPortList[midi_port_index] > 0) {
  364. fEngine->PortUnRegister(fClientControl.fRefNum, fMidiPlaybackPortList[midi_port_index]);
  365. fMidiPlaybackPortList[midi_port_index] = 0;
  366. }
  367. }
  368. // Clear MIDI channels
  369. fParams.fSendMidiChannels = 0;
  370. fParams.fReturnMidiChannels = 0;
  371. return 0;
  372. }
  373. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer(int port_index)
  374. {
  375. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiCapturePortList[port_index], fEngineControl->fBufferSize));
  376. }
  377. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer(int port_index)
  378. {
  379. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize));
  380. }
  381. //transport---------------------------------------------------------------------------
  382. void JackNetDriver::DecodeTransportData()
  383. {
  384. //is there a new timebase master on the net master ?
  385. // - release timebase master only if it's a non-conditional request
  386. // - no change or no request : don't do anything
  387. // - conditional request : don't change anything too, the master will know if this slave is actually the timebase master
  388. int refnum;
  389. bool conditional;
  390. if (fSendTransportData.fTimebaseMaster == TIMEBASEMASTER)
  391. {
  392. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  393. if (refnum != -1)
  394. fEngineControl->fTransport.ResetTimebase(refnum);
  395. jack_info("The NetMaster is now the new timebase master.");
  396. }
  397. //is there a transport state change to handle ?
  398. if (fSendTransportData.fNewState &&(fSendTransportData.fState != fEngineControl->fTransport.GetState()))
  399. {
  400. switch (fSendTransportData.fState)
  401. {
  402. case JackTransportStopped :
  403. fEngineControl->fTransport.SetCommand(TransportCommandStop);
  404. jack_info("Master stops transport.");
  405. break;
  406. case JackTransportStarting :
  407. fEngineControl->fTransport.RequestNewPos(&fSendTransportData.fPosition);
  408. fEngineControl->fTransport.SetCommand(TransportCommandStart);
  409. jack_info("Master starts transport frame = %d", fSendTransportData.fPosition.frame);
  410. break;
  411. case JackTransportRolling :
  412. //fEngineControl->fTransport.SetCommand(TransportCommandStart);
  413. fEngineControl->fTransport.SetState(JackTransportRolling);
  414. jack_info("Master is rolling.");
  415. break;
  416. }
  417. }
  418. }
  419. void JackNetDriver::EncodeTransportData()
  420. {
  421. /* Desactivated
  422. //is there a timebase master change ?
  423. int refnum;
  424. bool conditional;
  425. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  426. if (refnum != fLastTimebaseMaster)
  427. {
  428. //timebase master has released its function
  429. if (refnum == -1)
  430. {
  431. fReturnTransportData.fTimebaseMaster = RELEASE_TIMEBASEMASTER;
  432. jack_info("Sending a timebase master release request.");
  433. }
  434. //there is a new timebase master
  435. else
  436. {
  437. fReturnTransportData.fTimebaseMaster = (conditional) ? CONDITIONAL_TIMEBASEMASTER : TIMEBASEMASTER;
  438. jack_info("Sending a %s timebase master request.", (conditional) ? "conditional" : "non-conditional");
  439. }
  440. fLastTimebaseMaster = refnum;
  441. }
  442. else
  443. fReturnTransportData.fTimebaseMaster = NO_CHANGE;
  444. */
  445. //update transport state and position
  446. fReturnTransportData.fState = fEngineControl->fTransport.Query(&fReturnTransportData.fPosition);
  447. //is it a new state (that the master need to know...) ?
  448. fReturnTransportData.fNewState = ((fReturnTransportData.fState == JackTransportNetStarting) &&
  449. (fReturnTransportData.fState != fLastTransportState) &&
  450. (fReturnTransportData.fState != fSendTransportData.fState));
  451. if (fReturnTransportData.fNewState)
  452. jack_info("Sending '%s'.", GetTransportState(fReturnTransportData.fState));
  453. fLastTransportState = fReturnTransportData.fState;
  454. }
  455. //driver processes--------------------------------------------------------------------
  456. int JackNetDriver::Read()
  457. {
  458. int midi_port_index;
  459. int audio_port_index;
  460. //buffers
  461. for (midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++)
  462. fNetMidiCaptureBuffer->SetBuffer(midi_port_index, GetMidiInputBuffer(midi_port_index));
  463. for (audio_port_index = 0; audio_port_index < fParams.fSendAudioChannels; audio_port_index++)
  464. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer(audio_port_index));
  465. #ifdef JACK_MONITOR
  466. fNetTimeMon->New();
  467. #endif
  468. //receive sync (launch the cycle)
  469. if (SyncRecv() == SOCKET_ERROR)
  470. return 0;
  471. #ifdef JACK_MONITOR
  472. // For timing
  473. fRcvSyncUst = GetMicroSeconds();
  474. #endif
  475. //decode sync
  476. //if there is an error, don't return -1, it will skip Write() and the network error probably won't be identified
  477. DecodeSyncPacket();
  478. #ifdef JACK_MONITOR
  479. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  480. #endif
  481. //audio, midi or sync if driver is late
  482. if (DataRecv() == SOCKET_ERROR)
  483. return SOCKET_ERROR;
  484. //take the time at the beginning of the cycle
  485. JackDriver::CycleTakeBeginTime();
  486. #ifdef JACK_MONITOR
  487. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  488. #endif
  489. return 0;
  490. }
  491. int JackNetDriver::Write()
  492. {
  493. int midi_port_index;
  494. int audio_port_index;
  495. //buffers
  496. for (midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++)
  497. fNetMidiPlaybackBuffer->SetBuffer (midi_port_index, GetMidiOutputBuffer (midi_port_index));
  498. for (audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++)
  499. fNetAudioPlaybackBuffer->SetBuffer (audio_port_index, GetOutputBuffer (audio_port_index));
  500. #ifdef JACK_MONITOR
  501. fNetTimeMon->Add(((float) (GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  502. #endif
  503. //sync
  504. EncodeSyncPacket();
  505. //send sync
  506. if (SyncSend() == SOCKET_ERROR)
  507. return SOCKET_ERROR;
  508. #ifdef JACK_MONITOR
  509. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float) fEngineControl->fPeriodUsecs) * 100.f);
  510. #endif
  511. //send data
  512. if (DataSend() == SOCKET_ERROR)
  513. return SOCKET_ERROR;
  514. #ifdef JACK_MONITOR
  515. fNetTimeMon->AddLast(((float)(GetMicroSeconds() - fRcvSyncUst) / (float) fEngineControl->fPeriodUsecs) * 100.f);
  516. #endif
  517. return 0;
  518. }
  519. //driver loader-----------------------------------------------------------------------
  520. #ifdef __cplusplus
  521. extern "C"
  522. {
  523. #endif
  524. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  525. {
  526. jack_driver_desc_t * desc;
  527. jack_driver_desc_filler_t filler;
  528. jack_driver_param_value_t value;
  529. desc = jack_driver_descriptor_construct("net", "netjack slave backend component", &filler);
  530. strcpy(value.str, DEFAULT_MULTICAST_IP);
  531. jack_driver_descriptor_add_parameter(desc, &filler, "multicast_ip", 'a', JackDriverParamString, &value, NULL, "Multicast Address", NULL);
  532. value.i = DEFAULT_PORT;
  533. jack_driver_descriptor_add_parameter(desc, &filler, "udp_net_port", 'p', JackDriverParamInt, &value, NULL, "UDP port", NULL);
  534. value.i = DEFAULT_MTU;
  535. jack_driver_descriptor_add_parameter(desc, &filler, "mtu", 'M', JackDriverParamInt, &value, NULL, "MTU to the master", NULL);
  536. value.i = -1;
  537. jack_driver_descriptor_add_parameter(desc, &filler, "input_ports", 'C', JackDriverParamInt, &value, NULL, "Number of audio input ports", "Number of audio input ports. If -1, audio physical input from the master");
  538. jack_driver_descriptor_add_parameter(desc, &filler, "output_ports", 'P', JackDriverParamInt, &value, NULL, "Number of audio output ports", "Number of audio output ports. If -1, audio physical output from the master");
  539. value.i = 0;
  540. jack_driver_descriptor_add_parameter(desc, &filler, "midi_in_ports", 'i', JackDriverParamInt, &value, NULL, "Number of midi input ports", NULL);
  541. jack_driver_descriptor_add_parameter(desc, &filler, "midi_out_ports", 'o', JackDriverParamInt, &value, NULL, "Number of midi output ports", NULL);
  542. #if HAVE_CELT
  543. value.i = -1;
  544. jack_driver_descriptor_add_parameter(desc, &filler, "celt", 'c', JackDriverParamInt, &value, NULL, "Set CELT encoding and number of kBits per channel", NULL);
  545. #endif
  546. strcpy(value.str, "'hostname'");
  547. jack_driver_descriptor_add_parameter(desc, &filler, "client_name", 'n', JackDriverParamString, &value, NULL, "Name of the jack client", NULL);
  548. value.ui = 1U;
  549. jack_driver_descriptor_add_parameter(desc, &filler, "transport_sync", 't', JackDriverParamUInt, &value, NULL, "Sync transport with master's", NULL);
  550. strcpy(value.str, "slow");
  551. jack_driver_descriptor_add_parameter(desc, &filler, "mode", 'm', JackDriverParamString, &value, NULL, "Slow, Normal or Fast mode.", NULL);
  552. return desc;
  553. }
  554. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  555. {
  556. char multicast_ip[16];
  557. strcpy(multicast_ip, DEFAULT_MULTICAST_IP);
  558. char net_name[JACK_CLIENT_NAME_SIZE + 1];
  559. int udp_port = DEFAULT_PORT;
  560. int mtu = DEFAULT_MTU;
  561. uint transport_sync = 1;
  562. jack_nframes_t period_size = 128;
  563. jack_nframes_t sample_rate = 48000;
  564. int audio_capture_ports = -1;
  565. int audio_playback_ports = -1;
  566. int midi_input_ports = 0;
  567. int midi_output_ports = 0;
  568. int celt_encoding = -1;
  569. bool monitor = false;
  570. char network_mode = 's';
  571. const JSList* node;
  572. const jack_driver_param_t* param;
  573. net_name[0] = 0;
  574. for (node = params; node; node = jack_slist_next(node)) {
  575. param = (const jack_driver_param_t*) node->data;
  576. switch (param->character)
  577. {
  578. case 'a' :
  579. strncpy(multicast_ip, param->value.str, 15);
  580. break;
  581. case 'p':
  582. udp_port = param->value.ui;
  583. break;
  584. case 'M':
  585. mtu = param->value.i;
  586. break;
  587. case 'C':
  588. audio_capture_ports = param->value.i;
  589. break;
  590. case 'P':
  591. audio_playback_ports = param->value.i;
  592. break;
  593. case 'i':
  594. midi_input_ports = param->value.i;
  595. break;
  596. case 'o':
  597. midi_output_ports = param->value.i;
  598. break;
  599. #if HAVE_CELT
  600. case 'c':
  601. celt_encoding = param->value.i;
  602. break;
  603. #endif
  604. case 'n' :
  605. strncpy(net_name, param->value.str, JACK_CLIENT_NAME_SIZE);
  606. break;
  607. case 't' :
  608. transport_sync = param->value.ui;
  609. break;
  610. case 'm' :
  611. if (strcmp(param->value.str, "normal") == 0)
  612. network_mode = 'n';
  613. else if (strcmp(param->value.str, "slow") == 0)
  614. network_mode = 's';
  615. else if (strcmp(param->value.str, "fast") == 0)
  616. network_mode = 'f';
  617. else
  618. jack_error("Unknown network mode, using 'normal' mode.");
  619. break;
  620. }
  621. }
  622. try {
  623. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver(
  624. new Jack::JackNetDriver("system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  625. midi_input_ports, midi_output_ports,
  626. net_name, transport_sync,
  627. network_mode, celt_encoding));
  628. if (driver->Open(period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports, monitor, "from_master_", "to_master_", 0, 0) == 0) {
  629. return driver;
  630. } else {
  631. delete driver;
  632. return NULL;
  633. }
  634. } catch (...) {
  635. return NULL;
  636. }
  637. }
  638. #ifdef __cplusplus
  639. }
  640. #endif
  641. }