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.

828 lines
34KB

  1. /*
  2. Copyright (C) 2008-2011 Romain Moret at Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackCompilerDeps.h"
  16. #include "driver_interface.h"
  17. #include "JackNetDriver.h"
  18. #include "JackEngineControl.h"
  19. #include "JackLockedEngine.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 udp_port, int mtu, int midi_input_ports, int midi_output_ports,
  26. char* net_name, uint transport_sync, int network_latency,
  27. int celt_encoding, int opus_encoding, bool auto_save)
  28. : JackWaiterDriver(name, alias, engine, table), JackNetSlaveInterface(ip, udp_port)
  29. {
  30. jack_log("JackNetDriver::JackNetDriver ip %s, port %d", ip, udp_port);
  31. // Use the hostname if no name parameter was given
  32. if (strcmp(net_name, "") == 0) {
  33. GetHostName(net_name, JACK_CLIENT_NAME_SIZE);
  34. }
  35. fParams.fMtu = mtu;
  36. fWantedMIDICaptureChannels = midi_input_ports;
  37. fWantedMIDIPlaybackChannels = midi_output_ports;
  38. if (celt_encoding > 0) {
  39. fParams.fSampleEncoder = JackCeltEncoder;
  40. fParams.fKBps = celt_encoding;
  41. } else if (opus_encoding > 0) {
  42. fParams.fSampleEncoder = JackOpusEncoder;
  43. fParams.fKBps = opus_encoding;
  44. } else {
  45. fParams.fSampleEncoder = JackFloatEncoder;
  46. //fParams.fSampleEncoder = JackIntEncoder;
  47. }
  48. strcpy(fParams.fName, net_name);
  49. fSocket.GetName(fParams.fSlaveNetName);
  50. fParams.fTransportSync = transport_sync;
  51. fParams.fNetworkLatency = network_latency;
  52. fSendTransportData.fState = -1;
  53. fReturnTransportData.fState = -1;
  54. fLastTransportState = -1;
  55. fLastTimebaseMaster = -1;
  56. fMidiCapturePortList = NULL;
  57. fMidiPlaybackPortList = NULL;
  58. fWantedAudioCaptureChannels = -1;
  59. fWantedAudioPlaybackChannels = -1;
  60. fAutoSave = auto_save;
  61. #ifdef JACK_MONITOR
  62. fNetTimeMon = NULL;
  63. fRcvSyncUst = 0;
  64. #endif
  65. }
  66. JackNetDriver::~JackNetDriver()
  67. {
  68. delete[] fMidiCapturePortList;
  69. delete[] fMidiPlaybackPortList;
  70. #ifdef JACK_MONITOR
  71. delete fNetTimeMon;
  72. #endif
  73. }
  74. //open, close, attach and detach------------------------------------------------------
  75. int JackNetDriver::Open(jack_nframes_t buffer_size,
  76. jack_nframes_t samplerate,
  77. bool capturing,
  78. bool playing,
  79. int inchannels,
  80. int outchannels,
  81. bool monitor,
  82. const char* capture_driver_name,
  83. const char* playback_driver_name,
  84. jack_nframes_t capture_latency,
  85. jack_nframes_t playback_latency)
  86. {
  87. // Keep initial wanted values
  88. fWantedAudioCaptureChannels = inchannels;
  89. fWantedAudioPlaybackChannels = outchannels;
  90. return JackWaiterDriver::Open(buffer_size, samplerate,
  91. capturing, playing,
  92. inchannels, outchannels,
  93. monitor,
  94. capture_driver_name, playback_driver_name,
  95. capture_latency, playback_latency);
  96. }
  97. int JackNetDriver::Close()
  98. {
  99. #ifdef JACK_MONITOR
  100. if (fNetTimeMon) {
  101. fNetTimeMon->Save();
  102. }
  103. #endif
  104. FreeAll();
  105. return JackWaiterDriver::Close();
  106. }
  107. // Attach and Detach are defined as empty methods: port allocation is done when driver actually start (that is in Init)
  108. int JackNetDriver::Attach()
  109. {
  110. return 0;
  111. }
  112. int JackNetDriver::Detach()
  113. {
  114. return 0;
  115. }
  116. //init and restart--------------------------------------------------------------------
  117. /*
  118. JackNetDriver is wrapped in a JackWaitThreadedDriver decorator that behaves
  119. as a "dummy driver, until Init method returns.
  120. */
  121. bool JackNetDriver::Initialize()
  122. {
  123. jack_log("JackNetDriver::Initialize");
  124. if (fAutoSave) {
  125. SaveConnections(0);
  126. }
  127. FreePorts();
  128. // New loading, but existing socket, restart the driver
  129. if (fSocket.IsSocket()) {
  130. jack_info("Restarting driver...");
  131. FreeAll();
  132. }
  133. // Set the parameters to send
  134. fParams.fSendAudioChannels = fWantedAudioCaptureChannels;
  135. fParams.fReturnAudioChannels = fWantedAudioPlaybackChannels;
  136. fParams.fSendMidiChannels = fWantedMIDICaptureChannels;
  137. fParams.fReturnMidiChannels = fWantedMIDIPlaybackChannels;
  138. fParams.fSlaveSyncMode = fEngineControl->fSyncMode;
  139. // Display some additional infos
  140. jack_info("NetDriver started in %s mode %s Master's transport sync.",
  141. (fParams.fSlaveSyncMode) ? "sync" : "async", (fParams.fTransportSync) ? "with" : "without");
  142. // Init network
  143. if (!JackNetSlaveInterface::Init()) {
  144. jack_error("Starting network fails...");
  145. return false;
  146. }
  147. // Set global parameters
  148. if (!SetParams()) {
  149. jack_error("SetParams error...");
  150. return false;
  151. }
  152. // If -1 at connection time for audio, in/out audio channels count is sent by the master
  153. fCaptureChannels = fParams.fSendAudioChannels;
  154. fPlaybackChannels = fParams.fReturnAudioChannels;
  155. // If -1 at connection time for MIDI, in/out MIDI channels count is sent by the master (in fParams struct)
  156. // Allocate midi ports lists
  157. delete[] fMidiCapturePortList;
  158. delete[] fMidiPlaybackPortList;
  159. fMidiCapturePortList = new jack_port_id_t [fParams.fSendMidiChannels];
  160. fMidiPlaybackPortList = new jack_port_id_t [fParams.fReturnMidiChannels];
  161. assert(fMidiCapturePortList);
  162. assert(fMidiPlaybackPortList);
  163. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  164. fMidiCapturePortList[midi_port_index] = 0;
  165. }
  166. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  167. fMidiPlaybackPortList[midi_port_index] = 0;
  168. }
  169. // Register jack ports
  170. if (AllocPorts() != 0) {
  171. jack_error("Can't allocate ports.");
  172. return false;
  173. }
  174. // Init done, display parameters
  175. SessionParamsDisplay(&fParams);
  176. // Monitor
  177. #ifdef JACK_MONITOR
  178. string plot_name;
  179. // NetTimeMon
  180. plot_name = string(fParams.fName);
  181. plot_name += string("_slave");
  182. plot_name += (fEngineControl->fSyncMode) ? string("_sync") : string("_async");
  183. plot_name += string("_latency");
  184. fNetTimeMon = new JackGnuPlotMonitor<float>(128, 5, plot_name);
  185. string net_time_mon_fields[] =
  186. {
  187. string("sync decoded"),
  188. string("end of read"),
  189. string("start of write"),
  190. string("sync send"),
  191. string("end of write")
  192. };
  193. string net_time_mon_options[] =
  194. {
  195. string("set xlabel \"audio cycles\""),
  196. string("set ylabel \"% of audio cycle\"")
  197. };
  198. fNetTimeMon->SetPlotFile(net_time_mon_options, 2, net_time_mon_fields, 5);
  199. #endif
  200. // Driver parametering
  201. JackTimedDriver::SetBufferSize(fParams.fPeriodSize);
  202. JackTimedDriver::SetSampleRate(fParams.fSampleRate);
  203. JackDriver::NotifyBufferSize(fParams.fPeriodSize);
  204. JackDriver::NotifySampleRate(fParams.fSampleRate);
  205. // Transport engine parametering
  206. fEngineControl->fTransport.SetNetworkSync(fParams.fTransportSync);
  207. if (fAutoSave) {
  208. LoadConnections(0);
  209. }
  210. return true;
  211. }
  212. void JackNetDriver::FreeAll()
  213. {
  214. FreePorts();
  215. delete[] fTxBuffer;
  216. delete[] fRxBuffer;
  217. delete fNetAudioCaptureBuffer;
  218. delete fNetAudioPlaybackBuffer;
  219. delete fNetMidiCaptureBuffer;
  220. delete fNetMidiPlaybackBuffer;
  221. delete[] fMidiCapturePortList;
  222. delete[] fMidiPlaybackPortList;
  223. fTxBuffer = NULL;
  224. fRxBuffer = NULL;
  225. fNetAudioCaptureBuffer = NULL;
  226. fNetAudioPlaybackBuffer = NULL;
  227. fNetMidiCaptureBuffer = NULL;
  228. fNetMidiPlaybackBuffer = NULL;
  229. fMidiCapturePortList = NULL;
  230. fMidiPlaybackPortList = NULL;
  231. #ifdef JACK_MONITOR
  232. delete fNetTimeMon;
  233. fNetTimeMon = NULL;
  234. #endif
  235. }
  236. void JackNetDriver::UpdateLatencies()
  237. {
  238. jack_latency_range_t input_range;
  239. jack_latency_range_t output_range;
  240. jack_latency_range_t monitor_range;
  241. for (int i = 0; i < fCaptureChannels; i++) {
  242. input_range.max = input_range.min = float(fParams.fNetworkLatency * fEngineControl->fBufferSize) / 2.f;
  243. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
  244. }
  245. for (int i = 0; i < fPlaybackChannels; i++) {
  246. output_range.max = output_range.min = float(fParams.fNetworkLatency * fEngineControl->fBufferSize) / 2.f;
  247. if (!fEngineControl->fSyncMode) {
  248. output_range.max = output_range.min += fEngineControl->fBufferSize;
  249. }
  250. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
  251. if (fWithMonitorPorts) {
  252. monitor_range.min = monitor_range.max = 0;
  253. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
  254. }
  255. }
  256. }
  257. //jack ports and buffers--------------------------------------------------------------
  258. int JackNetDriver::AllocPorts()
  259. {
  260. jack_log("JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  261. /*
  262. fNetAudioCaptureBuffer fNetAudioPlaybackBuffer
  263. fSendAudioChannels fReturnAudioChannels
  264. fCapturePortList fPlaybackPortList
  265. fCaptureChannels ==> SLAVE ==> fPlaybackChannels
  266. "capture_" "playback_"
  267. */
  268. JackPort* port;
  269. jack_port_id_t port_index;
  270. char name[REAL_JACK_PORT_NAME_SIZE];
  271. char alias[REAL_JACK_PORT_NAME_SIZE];
  272. int audio_port_index;
  273. int midi_port_index;
  274. //audio
  275. for (audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++) {
  276. snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, audio_port_index + 1);
  277. snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, audio_port_index + 1);
  278. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  279. CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  280. jack_error("driver: cannot register port for %s", name);
  281. return -1;
  282. }
  283. port = fGraphManager->GetPort(port_index);
  284. port->SetAlias(alias);
  285. fCapturePortList[audio_port_index] = port_index;
  286. jack_log("JackNetDriver::AllocPorts() fCapturePortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_index, port->GetLatency());
  287. }
  288. for (audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++) {
  289. snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, audio_port_index + 1);
  290. snprintf(name, sizeof(name), "%s:playback_%d",fClientControl.fName, audio_port_index + 1);
  291. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  292. PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  293. jack_error("driver: cannot register port for %s", name);
  294. return -1;
  295. }
  296. port = fGraphManager->GetPort(port_index);
  297. port->SetAlias(alias);
  298. fPlaybackPortList[audio_port_index] = port_index;
  299. jack_log("JackNetDriver::AllocPorts() fPlaybackPortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_index, port->GetLatency());
  300. }
  301. //midi
  302. for (midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  303. snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, midi_port_index + 1);
  304. snprintf(name, sizeof (name), "%s:midi_capture_%d", fClientControl.fName, midi_port_index + 1);
  305. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  306. CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  307. jack_error("driver: cannot register port for %s", name);
  308. return -1;
  309. }
  310. port = fGraphManager->GetPort(port_index);
  311. fMidiCapturePortList[midi_port_index] = port_index;
  312. jack_log("JackNetDriver::AllocPorts() fMidiCapturePortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_index, port->GetLatency());
  313. }
  314. for (midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  315. snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, midi_port_index + 1);
  316. snprintf(name, sizeof(name), "%s:midi_playback_%d", fClientControl.fName, midi_port_index + 1);
  317. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  318. PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  319. jack_error("driver: cannot register port for %s", name);
  320. return -1;
  321. }
  322. port = fGraphManager->GetPort(port_index);
  323. fMidiPlaybackPortList[midi_port_index] = port_index;
  324. jack_log("JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_index, port->GetLatency());
  325. }
  326. UpdateLatencies();
  327. return 0;
  328. }
  329. int JackNetDriver::FreePorts()
  330. {
  331. jack_log("JackNetDriver::FreePorts");
  332. for (int audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++) {
  333. if (fCapturePortList[audio_port_index] > 0) {
  334. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[audio_port_index]);
  335. fCapturePortList[audio_port_index] = 0;
  336. }
  337. }
  338. for (int audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++) {
  339. if (fPlaybackPortList[audio_port_index] > 0) {
  340. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[audio_port_index]);
  341. fPlaybackPortList[audio_port_index] = 0;
  342. }
  343. }
  344. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  345. if (fMidiCapturePortList && fMidiCapturePortList[midi_port_index] > 0) {
  346. fGraphManager->ReleasePort(fClientControl.fRefNum, fMidiCapturePortList[midi_port_index]);
  347. fMidiCapturePortList[midi_port_index] = 0;
  348. }
  349. }
  350. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  351. if (fMidiPlaybackPortList && fMidiPlaybackPortList[midi_port_index] > 0) {
  352. fEngine->PortUnRegister(fClientControl.fRefNum, fMidiPlaybackPortList[midi_port_index]);
  353. fMidiPlaybackPortList[midi_port_index] = 0;
  354. }
  355. }
  356. return 0;
  357. }
  358. void JackNetDriver::SaveConnections(int alias)
  359. {
  360. JackDriver::SaveConnections(alias);
  361. const char** connections;
  362. if (fMidiCapturePortList) {
  363. for (int i = 0; i < fParams.fSendMidiChannels; ++i) {
  364. if (fMidiCapturePortList[i] && (connections = fGraphManager->GetConnections(fMidiCapturePortList[i])) != 0) {
  365. for (int j = 0; connections[j]; j++) {
  366. JackPort* port_id = fGraphManager->GetPort(fGraphManager->GetPort(connections[j]));
  367. fConnections.push_back(make_pair(port_id->GetType(), make_pair(fGraphManager->GetPort(fMidiCapturePortList[i])->GetName(), connections[j])));
  368. jack_info("Save connection: %s %s", fGraphManager->GetPort(fMidiCapturePortList[i])->GetName(), connections[j]);
  369. }
  370. free(connections);
  371. }
  372. }
  373. }
  374. if (fMidiPlaybackPortList) {
  375. for (int i = 0; i < fParams.fReturnMidiChannels; ++i) {
  376. if (fMidiPlaybackPortList[i] && (connections = fGraphManager->GetConnections(fMidiPlaybackPortList[i])) != 0) {
  377. for (int j = 0; connections[j]; j++) {
  378. JackPort* port_id = fGraphManager->GetPort(fGraphManager->GetPort(connections[j]));
  379. fConnections.push_back(make_pair(port_id->GetType(), make_pair(connections[j], fGraphManager->GetPort(fMidiPlaybackPortList[i])->GetName())));
  380. jack_info("Save connection: %s %s", connections[j], fGraphManager->GetPort(fMidiPlaybackPortList[i])->GetName());
  381. }
  382. free(connections);
  383. }
  384. }
  385. }
  386. }
  387. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer(int port_index)
  388. {
  389. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiCapturePortList[port_index], fEngineControl->fBufferSize));
  390. }
  391. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer(int port_index)
  392. {
  393. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize));
  394. }
  395. //transport---------------------------------------------------------------------------
  396. void JackNetDriver::DecodeTransportData()
  397. {
  398. //is there a new timebase master on the net master ?
  399. // - release timebase master only if it's a non-conditional request
  400. // - no change or no request : don't do anything
  401. // - conditional request : don't change anything too, the master will know if this slave is actually the timebase master
  402. int refnum;
  403. bool conditional;
  404. if (fSendTransportData.fTimebaseMaster == TIMEBASEMASTER) {
  405. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  406. if (refnum != -1) {
  407. fEngineControl->fTransport.ResetTimebase(refnum);
  408. }
  409. jack_info("The NetMaster is now the new timebase master.");
  410. }
  411. //is there a transport state change to handle ?
  412. if (fSendTransportData.fNewState &&(fSendTransportData.fState != fEngineControl->fTransport.GetState())) {
  413. switch (fSendTransportData.fState)
  414. {
  415. case JackTransportStopped :
  416. fEngineControl->fTransport.SetCommand(TransportCommandStop);
  417. jack_info("Master stops transport.");
  418. break;
  419. case JackTransportStarting :
  420. fEngineControl->fTransport.RequestNewPos(&fSendTransportData.fPosition);
  421. fEngineControl->fTransport.SetCommand(TransportCommandStart);
  422. jack_info("Master starts transport frame = %d", fSendTransportData.fPosition.frame);
  423. break;
  424. case JackTransportRolling :
  425. //fEngineControl->fTransport.SetCommand(TransportCommandStart);
  426. fEngineControl->fTransport.SetState(JackTransportRolling);
  427. jack_info("Master is rolling.");
  428. break;
  429. }
  430. }
  431. }
  432. void JackNetDriver::EncodeTransportData()
  433. {
  434. //is there a timebase master change ?
  435. int refnum;
  436. bool conditional;
  437. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  438. if (refnum != fLastTimebaseMaster) {
  439. //timebase master has released its function
  440. if (refnum == -1) {
  441. fReturnTransportData.fTimebaseMaster = RELEASE_TIMEBASEMASTER;
  442. jack_info("Sending a timebase master release request.");
  443. } else {
  444. //there is a new timebase master
  445. fReturnTransportData.fTimebaseMaster = (conditional) ? CONDITIONAL_TIMEBASEMASTER : TIMEBASEMASTER;
  446. jack_info("Sending a %s timebase master request.", (conditional) ? "conditional" : "non-conditional");
  447. }
  448. fLastTimebaseMaster = refnum;
  449. } else {
  450. fReturnTransportData.fTimebaseMaster = NO_CHANGE;
  451. }
  452. //update transport state and position
  453. fReturnTransportData.fState = fEngineControl->fTransport.Query(&fReturnTransportData.fPosition);
  454. //is it a new state (that the master need to know...) ?
  455. fReturnTransportData.fNewState = ((fReturnTransportData.fState == JackTransportNetStarting) &&
  456. (fReturnTransportData.fState != fLastTransportState) &&
  457. (fReturnTransportData.fState != fSendTransportData.fState));
  458. if (fReturnTransportData.fNewState) {
  459. jack_info("Sending '%s'.", GetTransportState(fReturnTransportData.fState));
  460. }
  461. fLastTransportState = fReturnTransportData.fState;
  462. }
  463. //driver processes--------------------------------------------------------------------
  464. int JackNetDriver::Read()
  465. {
  466. //buffers
  467. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  468. fNetMidiCaptureBuffer->SetBuffer(midi_port_index, GetMidiInputBuffer(midi_port_index));
  469. }
  470. for (int audio_port_index = 0; audio_port_index < fParams.fSendAudioChannels; audio_port_index++) {
  471. #ifdef OPTIMIZED_PROTOCOL
  472. if (fGraphManager->GetConnectionsNum(fCapturePortList[audio_port_index]) > 0) {
  473. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer(audio_port_index));
  474. } else {
  475. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, NULL);
  476. }
  477. #else
  478. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer(audio_port_index));
  479. #endif
  480. }
  481. #ifdef JACK_MONITOR
  482. fNetTimeMon->New();
  483. #endif
  484. switch (SyncRecv()) {
  485. case SOCKET_ERROR:
  486. return SOCKET_ERROR;
  487. case NET_PACKET_ERROR:
  488. // since sync packet is incorrect, don't decode it and continue with data
  489. break;
  490. default:
  491. // decode sync
  492. DecodeSyncPacket();
  493. break;
  494. }
  495. #ifdef JACK_MONITOR
  496. // For timing
  497. fRcvSyncUst = GetMicroSeconds();
  498. #endif
  499. #ifdef JACK_MONITOR
  500. fNetTimeMon->Add(float(GetMicroSeconds() - fRcvSyncUst) / float(fEngineControl->fPeriodUsecs) * 100.f);
  501. #endif
  502. //audio, midi or sync if driver is late
  503. switch (DataRecv()) {
  504. case SOCKET_ERROR:
  505. return SOCKET_ERROR;
  506. case NET_PACKET_ERROR:
  507. jack_time_t cur_time = GetMicroSeconds();
  508. NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
  509. break;
  510. }
  511. //take the time at the beginning of the cycle
  512. JackDriver::CycleTakeBeginTime();
  513. #ifdef JACK_MONITOR
  514. fNetTimeMon->Add(float(GetMicroSeconds() - fRcvSyncUst) / float(fEngineControl->fPeriodUsecs) * 100.f);
  515. #endif
  516. return 0;
  517. }
  518. int JackNetDriver::Write()
  519. {
  520. //buffers
  521. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  522. fNetMidiPlaybackBuffer->SetBuffer(midi_port_index, GetMidiOutputBuffer(midi_port_index));
  523. }
  524. for (int audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++) {
  525. #ifdef OPTIMIZED_PROTOCOL
  526. // Port is connected on other side...
  527. if (fNetAudioPlaybackBuffer->GetConnected(audio_port_index)
  528. && (fGraphManager->GetConnectionsNum(fPlaybackPortList[audio_port_index]) > 0)) {
  529. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, GetOutputBuffer(audio_port_index));
  530. } else {
  531. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, NULL);
  532. }
  533. #else
  534. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, GetOutputBuffer(audio_port_index));
  535. #endif
  536. }
  537. #ifdef JACK_MONITOR
  538. fNetTimeMon->AddLast(float(GetMicroSeconds() - fRcvSyncUst) / float(fEngineControl->fPeriodUsecs) * 100.f);
  539. #endif
  540. //sync
  541. EncodeSyncPacket();
  542. //send sync
  543. if (SyncSend() == SOCKET_ERROR) {
  544. return SOCKET_ERROR;
  545. }
  546. #ifdef JACK_MONITOR
  547. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  548. #endif
  549. //send data
  550. if (DataSend() == SOCKET_ERROR) {
  551. return SOCKET_ERROR;
  552. }
  553. #ifdef JACK_MONITOR
  554. fNetTimeMon->AddLast(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  555. #endif
  556. return 0;
  557. }
  558. //driver loader-----------------------------------------------------------------------
  559. #ifdef __cplusplus
  560. extern "C"
  561. {
  562. #endif
  563. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  564. {
  565. jack_driver_desc_t * desc;
  566. jack_driver_desc_filler_t filler;
  567. jack_driver_param_value_t value;
  568. desc = jack_driver_descriptor_construct("net", JackDriverMaster, "netjack slave backend component", &filler);
  569. strcpy(value.str, DEFAULT_MULTICAST_IP);
  570. jack_driver_descriptor_add_parameter(desc, &filler, "multicast-ip", 'a', JackDriverParamString, &value, NULL, "Multicast address, or explicit IP of the master", NULL);
  571. value.i = DEFAULT_PORT;
  572. jack_driver_descriptor_add_parameter(desc, &filler, "udp-net-port", 'p', JackDriverParamInt, &value, NULL, "UDP port", NULL);
  573. value.i = DEFAULT_MTU;
  574. jack_driver_descriptor_add_parameter(desc, &filler, "mtu", 'M', JackDriverParamInt, &value, NULL, "MTU to the master", NULL);
  575. value.i = -1;
  576. 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");
  577. 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");
  578. value.i = -1;
  579. jack_driver_descriptor_add_parameter(desc, &filler, "midi-in-ports", 'i', JackDriverParamInt, &value, NULL, "Number of midi input ports", "Number of MIDI input ports. If -1, MIDI physical input from the master");
  580. jack_driver_descriptor_add_parameter(desc, &filler, "midi-out-ports", 'o', JackDriverParamInt, &value, NULL, "Number of midi output ports", "Number of MIDI output ports. If -1, MIDI physical output from the master");
  581. #if HAVE_CELT
  582. value.i = -1;
  583. jack_driver_descriptor_add_parameter(desc, &filler, "celt", 'c', JackDriverParamInt, &value, NULL, "Set CELT encoding and number of kBits per channel", NULL);
  584. #endif
  585. #if HAVE_OPUS
  586. value.i = -1;
  587. jack_driver_descriptor_add_parameter(desc, &filler, "opus", 'O', JackDriverParamInt, &value, NULL, "Set Opus encoding and number of kBits per channel", NULL);
  588. #endif
  589. strcpy(value.str, "'hostname'");
  590. jack_driver_descriptor_add_parameter(desc, &filler, "client-name", 'n', JackDriverParamString, &value, NULL, "Name of the jack client", NULL);
  591. value.i = false;
  592. jack_driver_descriptor_add_parameter(desc, &filler, "auto-save", 's', JackDriverParamBool, &value, NULL, "Save/restore connection state when restarting", NULL);
  593. /*
  594. Deactivated for now..
  595. value.ui = 0U;
  596. jack_driver_descriptor_add_parameter(desc, &filler, "transport-sync", 't', JackDriverParamUInt, &value, NULL, "Sync transport with master's", NULL);
  597. */
  598. value.ui = 5U;
  599. jack_driver_descriptor_add_parameter(desc, &filler, "latency", 'l', JackDriverParamUInt, &value, NULL, "Network latency", NULL);
  600. return desc;
  601. }
  602. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  603. {
  604. char multicast_ip[32];
  605. char net_name[JACK_CLIENT_NAME_SIZE + 1] = {0};
  606. int udp_port;
  607. int mtu = DEFAULT_MTU;
  608. // Desactivated for now...
  609. uint transport_sync = 0;
  610. jack_nframes_t period_size = 1024;
  611. jack_nframes_t sample_rate = 48000;
  612. int audio_capture_ports = -1;
  613. int audio_playback_ports = -1;
  614. int midi_input_ports = -1;
  615. int midi_output_ports = -1;
  616. int celt_encoding = -1;
  617. int opus_encoding = -1;
  618. bool monitor = false;
  619. int network_latency = 5;
  620. const JSList* node;
  621. const jack_driver_param_t* param;
  622. bool auto_save = false;
  623. // Possibly use env variable for UDP port
  624. const char* default_udp_port = getenv("JACK_NETJACK_PORT");
  625. udp_port = (default_udp_port) ? atoi(default_udp_port) : DEFAULT_PORT;
  626. // Possibly use env variable for multicast IP
  627. const char* default_multicast_ip = getenv("JACK_NETJACK_MULTICAST");
  628. strcpy(multicast_ip, (default_multicast_ip) ? default_multicast_ip : DEFAULT_MULTICAST_IP);
  629. for (node = params; node; node = jack_slist_next(node)) {
  630. param = (const jack_driver_param_t*) node->data;
  631. switch (param->character)
  632. {
  633. case 'a' :
  634. assert(strlen(param->value.str) < 32);
  635. strcpy(multicast_ip, param->value.str);
  636. break;
  637. case 'p':
  638. udp_port = param->value.ui;
  639. break;
  640. case 'M':
  641. mtu = param->value.i;
  642. break;
  643. case 'C':
  644. audio_capture_ports = param->value.i;
  645. break;
  646. case 'P':
  647. audio_playback_ports = param->value.i;
  648. break;
  649. case 'i':
  650. midi_input_ports = param->value.i;
  651. break;
  652. case 'o':
  653. midi_output_ports = param->value.i;
  654. break;
  655. #if HAVE_CELT
  656. case 'c':
  657. celt_encoding = param->value.i;
  658. break;
  659. #endif
  660. #if HAVE_OPUS
  661. case 'O':
  662. opus_encoding = param->value.i;
  663. break;
  664. #endif
  665. case 'n' :
  666. strncpy(net_name, param->value.str, JACK_CLIENT_NAME_SIZE);
  667. break;
  668. case 's':
  669. auto_save = true;
  670. break;
  671. /*
  672. Deactivated for now..
  673. case 't' :
  674. transport_sync = param->value.ui;
  675. break;
  676. */
  677. case 'l' :
  678. network_latency = param->value.ui;
  679. if (network_latency > NETWORK_MAX_LATENCY) {
  680. printf("Error : network latency is limited to %d\n", NETWORK_MAX_LATENCY);
  681. return NULL;
  682. }
  683. break;
  684. }
  685. }
  686. try {
  687. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver(
  688. new Jack::JackNetDriver("system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  689. midi_input_ports, midi_output_ports,
  690. net_name, transport_sync,
  691. network_latency, celt_encoding, opus_encoding, auto_save));
  692. if (driver->Open(period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports, monitor, "from_master_", "to_master_", 0, 0) == 0) {
  693. return driver;
  694. } else {
  695. delete driver;
  696. return NULL;
  697. }
  698. } catch (...) {
  699. return NULL;
  700. }
  701. }
  702. #ifdef __cplusplus
  703. }
  704. #endif
  705. }