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.

724 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 connection 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. return 0;
  369. }
  370. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer(int port_index)
  371. {
  372. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiCapturePortList[port_index], fEngineControl->fBufferSize));
  373. }
  374. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer(int port_index)
  375. {
  376. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize));
  377. }
  378. //transport---------------------------------------------------------------------------
  379. void JackNetDriver::DecodeTransportData()
  380. {
  381. //is there a new timebase master on the net master ?
  382. // - release timebase master only if it's a non-conditional request
  383. // - no change or no request : don't do anything
  384. // - conditional request : don't change anything too, the master will know if this slave is actually the timebase master
  385. int refnum;
  386. bool conditional;
  387. if (fSendTransportData.fTimebaseMaster == TIMEBASEMASTER) {
  388. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  389. if (refnum != -1)
  390. fEngineControl->fTransport.ResetTimebase(refnum);
  391. jack_info("The NetMaster is now the new timebase master.");
  392. }
  393. //is there a transport state change to handle ?
  394. if (fSendTransportData.fNewState &&(fSendTransportData.fState != fEngineControl->fTransport.GetState())) {
  395. switch (fSendTransportData.fState)
  396. {
  397. case JackTransportStopped :
  398. fEngineControl->fTransport.SetCommand(TransportCommandStop);
  399. jack_info("Master stops transport.");
  400. break;
  401. case JackTransportStarting :
  402. fEngineControl->fTransport.RequestNewPos(&fSendTransportData.fPosition);
  403. fEngineControl->fTransport.SetCommand(TransportCommandStart);
  404. jack_info("Master starts transport frame = %d", fSendTransportData.fPosition.frame);
  405. break;
  406. case JackTransportRolling :
  407. //fEngineControl->fTransport.SetCommand(TransportCommandStart);
  408. fEngineControl->fTransport.SetState(JackTransportRolling);
  409. jack_info("Master is rolling.");
  410. break;
  411. }
  412. }
  413. }
  414. void JackNetDriver::EncodeTransportData()
  415. {
  416. //is there a timebase master change ?
  417. int refnum;
  418. bool conditional;
  419. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  420. if (refnum != fLastTimebaseMaster) {
  421. //timebase master has released its function
  422. if (refnum == -1) {
  423. fReturnTransportData.fTimebaseMaster = RELEASE_TIMEBASEMASTER;
  424. jack_info("Sending a timebase master release request.");
  425. } else {
  426. //there is a new timebase master
  427. fReturnTransportData.fTimebaseMaster = (conditional) ? CONDITIONAL_TIMEBASEMASTER : TIMEBASEMASTER;
  428. jack_info("Sending a %s timebase master request.", (conditional) ? "conditional" : "non-conditional");
  429. }
  430. fLastTimebaseMaster = refnum;
  431. } else {
  432. fReturnTransportData.fTimebaseMaster = NO_CHANGE;
  433. }
  434. //update transport state and position
  435. fReturnTransportData.fState = fEngineControl->fTransport.Query(&fReturnTransportData.fPosition);
  436. //is it a new state (that the master need to know...) ?
  437. fReturnTransportData.fNewState = ((fReturnTransportData.fState == JackTransportNetStarting) &&
  438. (fReturnTransportData.fState != fLastTransportState) &&
  439. (fReturnTransportData.fState != fSendTransportData.fState));
  440. if (fReturnTransportData.fNewState)
  441. jack_info("Sending '%s'.", GetTransportState(fReturnTransportData.fState));
  442. fLastTransportState = fReturnTransportData.fState;
  443. }
  444. //driver processes--------------------------------------------------------------------
  445. int JackNetDriver::Read()
  446. {
  447. int midi_port_index;
  448. int audio_port_index;
  449. //buffers
  450. for (midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++)
  451. fNetMidiCaptureBuffer->SetBuffer(midi_port_index, GetMidiInputBuffer(midi_port_index));
  452. for (audio_port_index = 0; audio_port_index < fParams.fSendAudioChannels; audio_port_index++)
  453. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer(audio_port_index));
  454. #ifdef JACK_MONITOR
  455. fNetTimeMon->New();
  456. #endif
  457. //receive sync (launch the cycle)
  458. if (SyncRecv() == SOCKET_ERROR)
  459. return 0;
  460. #ifdef JACK_MONITOR
  461. // For timing
  462. fRcvSyncUst = GetMicroSeconds();
  463. #endif
  464. //decode sync
  465. //if there is an error, don't return -1, it will skip Write() and the network error probably won't be identified
  466. DecodeSyncPacket();
  467. #ifdef JACK_MONITOR
  468. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  469. #endif
  470. //audio, midi or sync if driver is late
  471. if (DataRecv() == SOCKET_ERROR)
  472. return SOCKET_ERROR;
  473. //take the time at the beginning of the cycle
  474. JackDriver::CycleTakeBeginTime();
  475. #ifdef JACK_MONITOR
  476. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  477. #endif
  478. return 0;
  479. }
  480. int JackNetDriver::Write()
  481. {
  482. int midi_port_index;
  483. int audio_port_index;
  484. //buffers
  485. for (midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++)
  486. fNetMidiPlaybackBuffer->SetBuffer(midi_port_index, GetMidiOutputBuffer (midi_port_index));
  487. for (audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++)
  488. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, GetOutputBuffer (audio_port_index));
  489. #ifdef JACK_MONITOR
  490. fNetTimeMon->Add(((float) (GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  491. #endif
  492. //sync
  493. EncodeSyncPacket();
  494. //send sync
  495. if (SyncSend() == SOCKET_ERROR)
  496. return SOCKET_ERROR;
  497. #ifdef JACK_MONITOR
  498. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float) fEngineControl->fPeriodUsecs) * 100.f);
  499. #endif
  500. //send data
  501. if (DataSend() == SOCKET_ERROR)
  502. return SOCKET_ERROR;
  503. #ifdef JACK_MONITOR
  504. fNetTimeMon->AddLast(((float)(GetMicroSeconds() - fRcvSyncUst) / (float) fEngineControl->fPeriodUsecs) * 100.f);
  505. #endif
  506. return 0;
  507. }
  508. //driver loader-----------------------------------------------------------------------
  509. #ifdef __cplusplus
  510. extern "C"
  511. {
  512. #endif
  513. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  514. {
  515. jack_driver_desc_t * desc;
  516. jack_driver_desc_filler_t filler;
  517. jack_driver_param_value_t value;
  518. desc = jack_driver_descriptor_construct("net", "netjack slave backend component", &filler);
  519. strcpy(value.str, DEFAULT_MULTICAST_IP);
  520. jack_driver_descriptor_add_parameter(desc, &filler, "multicast_ip", 'a', JackDriverParamString, &value, NULL, "Multicast Address", NULL);
  521. value.i = DEFAULT_PORT;
  522. jack_driver_descriptor_add_parameter(desc, &filler, "udp_net_port", 'p', JackDriverParamInt, &value, NULL, "UDP port", NULL);
  523. value.i = DEFAULT_MTU;
  524. jack_driver_descriptor_add_parameter(desc, &filler, "mtu", 'M', JackDriverParamInt, &value, NULL, "MTU to the master", NULL);
  525. value.i = -1;
  526. 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");
  527. 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");
  528. value.i = 0;
  529. jack_driver_descriptor_add_parameter(desc, &filler, "midi_in_ports", 'i', JackDriverParamInt, &value, NULL, "Number of midi input ports", NULL);
  530. jack_driver_descriptor_add_parameter(desc, &filler, "midi_out_ports", 'o', JackDriverParamInt, &value, NULL, "Number of midi output ports", NULL);
  531. #if HAVE_CELT
  532. value.i = -1;
  533. jack_driver_descriptor_add_parameter(desc, &filler, "celt", 'c', JackDriverParamInt, &value, NULL, "Set CELT encoding and number of kBits per channel", NULL);
  534. #endif
  535. strcpy(value.str, "'hostname'");
  536. jack_driver_descriptor_add_parameter(desc, &filler, "client_name", 'n', JackDriverParamString, &value, NULL, "Name of the jack client", NULL);
  537. value.ui = 1U;
  538. jack_driver_descriptor_add_parameter(desc, &filler, "transport_sync", 't', JackDriverParamUInt, &value, NULL, "Sync transport with master's", NULL);
  539. strcpy(value.str, "slow");
  540. jack_driver_descriptor_add_parameter(desc, &filler, "mode", 'm', JackDriverParamString, &value, NULL, "Slow, Normal or Fast mode.", NULL);
  541. return desc;
  542. }
  543. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  544. {
  545. char multicast_ip[16];
  546. strcpy(multicast_ip, DEFAULT_MULTICAST_IP);
  547. char net_name[JACK_CLIENT_NAME_SIZE + 1];
  548. int udp_port = DEFAULT_PORT;
  549. int mtu = DEFAULT_MTU;
  550. uint transport_sync = 1;
  551. jack_nframes_t period_size = 128;
  552. jack_nframes_t sample_rate = 48000;
  553. int audio_capture_ports = -1;
  554. int audio_playback_ports = -1;
  555. int midi_input_ports = 0;
  556. int midi_output_ports = 0;
  557. int celt_encoding = -1;
  558. bool monitor = false;
  559. char network_mode = 's';
  560. const JSList* node;
  561. const jack_driver_param_t* param;
  562. net_name[0] = 0;
  563. for (node = params; node; node = jack_slist_next(node)) {
  564. param = (const jack_driver_param_t*) node->data;
  565. switch (param->character)
  566. {
  567. case 'a' :
  568. strncpy(multicast_ip, param->value.str, 15);
  569. break;
  570. case 'p':
  571. udp_port = param->value.ui;
  572. break;
  573. case 'M':
  574. mtu = param->value.i;
  575. break;
  576. case 'C':
  577. audio_capture_ports = param->value.i;
  578. break;
  579. case 'P':
  580. audio_playback_ports = param->value.i;
  581. break;
  582. case 'i':
  583. midi_input_ports = param->value.i;
  584. break;
  585. case 'o':
  586. midi_output_ports = param->value.i;
  587. break;
  588. #if HAVE_CELT
  589. case 'c':
  590. celt_encoding = param->value.i;
  591. break;
  592. #endif
  593. case 'n' :
  594. strncpy(net_name, param->value.str, JACK_CLIENT_NAME_SIZE);
  595. break;
  596. case 't' :
  597. transport_sync = param->value.ui;
  598. break;
  599. case 'm' :
  600. if (strcmp(param->value.str, "normal") == 0)
  601. network_mode = 'n';
  602. else if (strcmp(param->value.str, "slow") == 0)
  603. network_mode = 's';
  604. else if (strcmp(param->value.str, "fast") == 0)
  605. network_mode = 'f';
  606. else
  607. jack_error("Unknown network mode, using 'normal' mode.");
  608. break;
  609. }
  610. }
  611. try {
  612. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver(
  613. new Jack::JackNetDriver("system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  614. midi_input_ports, midi_output_ports,
  615. net_name, transport_sync,
  616. network_mode, celt_encoding));
  617. if (driver->Open(period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports, monitor, "from_master_", "to_master_", 0, 0) == 0) {
  618. return driver;
  619. } else {
  620. delete driver;
  621. return NULL;
  622. }
  623. } catch (...) {
  624. return NULL;
  625. }
  626. }
  627. #ifdef __cplusplus
  628. }
  629. #endif
  630. }