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.

824 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 = 0;
  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 = 0;
  247. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
  248. if (fWithMonitorPorts) {
  249. monitor_range.min = monitor_range.max = 0;
  250. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
  251. }
  252. }
  253. }
  254. //jack ports and buffers--------------------------------------------------------------
  255. int JackNetDriver::AllocPorts()
  256. {
  257. jack_log("JackNetDriver::AllocPorts fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  258. /*
  259. fNetAudioCaptureBuffer fNetAudioPlaybackBuffer
  260. fSendAudioChannels fReturnAudioChannels
  261. fCapturePortList fPlaybackPortList
  262. fCaptureChannels ==> SLAVE ==> fPlaybackChannels
  263. "capture_" "playback_"
  264. */
  265. JackPort* port;
  266. jack_port_id_t port_index;
  267. char name[REAL_JACK_PORT_NAME_SIZE];
  268. char alias[REAL_JACK_PORT_NAME_SIZE];
  269. int audio_port_index;
  270. int midi_port_index;
  271. //audio
  272. for (audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++) {
  273. snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, audio_port_index + 1);
  274. snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, audio_port_index + 1);
  275. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  276. CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  277. jack_error("driver: cannot register port for %s", name);
  278. return -1;
  279. }
  280. port = fGraphManager->GetPort(port_index);
  281. port->SetAlias(alias);
  282. fCapturePortList[audio_port_index] = port_index;
  283. jack_log("JackNetDriver::AllocPorts() fCapturePortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_index, port->GetLatency());
  284. }
  285. for (audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++) {
  286. snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, audio_port_index + 1);
  287. snprintf(name, sizeof(name), "%s:playback_%d",fClientControl.fName, audio_port_index + 1);
  288. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE,
  289. PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  290. jack_error("driver: cannot register port for %s", name);
  291. return -1;
  292. }
  293. port = fGraphManager->GetPort(port_index);
  294. port->SetAlias(alias);
  295. fPlaybackPortList[audio_port_index] = port_index;
  296. jack_log("JackNetDriver::AllocPorts() fPlaybackPortList[%d] audio_port_index = %ld fPortLatency = %ld", audio_port_index, port_index, port->GetLatency());
  297. }
  298. //midi
  299. for (midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  300. snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, midi_port_index + 1);
  301. snprintf(name, sizeof (name), "%s:midi_capture_%d", fClientControl.fName, midi_port_index + 1);
  302. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  303. CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  304. jack_error("driver: cannot register port for %s", name);
  305. return -1;
  306. }
  307. port = fGraphManager->GetPort(port_index);
  308. fMidiCapturePortList[midi_port_index] = port_index;
  309. jack_log("JackNetDriver::AllocPorts() fMidiCapturePortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_index, port->GetLatency());
  310. }
  311. for (midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  312. snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, midi_port_index + 1);
  313. snprintf(name, sizeof(name), "%s:midi_playback_%d", fClientControl.fName, midi_port_index + 1);
  314. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE,
  315. PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  316. jack_error("driver: cannot register port for %s", name);
  317. return -1;
  318. }
  319. port = fGraphManager->GetPort(port_index);
  320. fMidiPlaybackPortList[midi_port_index] = port_index;
  321. jack_log("JackNetDriver::AllocPorts() fMidiPlaybackPortList[%d] midi_port_index = %ld fPortLatency = %ld", midi_port_index, port_index, port->GetLatency());
  322. }
  323. return 0;
  324. }
  325. int JackNetDriver::FreePorts()
  326. {
  327. jack_log("JackNetDriver::FreePorts");
  328. for (int audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++) {
  329. if (fCapturePortList[audio_port_index] > 0) {
  330. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[audio_port_index]);
  331. fCapturePortList[audio_port_index] = 0;
  332. }
  333. }
  334. for (int audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++) {
  335. if (fPlaybackPortList[audio_port_index] > 0) {
  336. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[audio_port_index]);
  337. fPlaybackPortList[audio_port_index] = 0;
  338. }
  339. }
  340. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  341. if (fMidiCapturePortList && fMidiCapturePortList[midi_port_index] > 0) {
  342. fGraphManager->ReleasePort(fClientControl.fRefNum, fMidiCapturePortList[midi_port_index]);
  343. fMidiCapturePortList[midi_port_index] = 0;
  344. }
  345. }
  346. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  347. if (fMidiPlaybackPortList && fMidiPlaybackPortList[midi_port_index] > 0) {
  348. fEngine->PortUnRegister(fClientControl.fRefNum, fMidiPlaybackPortList[midi_port_index]);
  349. fMidiPlaybackPortList[midi_port_index] = 0;
  350. }
  351. }
  352. return 0;
  353. }
  354. void JackNetDriver::SaveConnections(int alias)
  355. {
  356. JackDriver::SaveConnections(alias);
  357. const char** connections;
  358. if (fMidiCapturePortList) {
  359. for (int i = 0; i < fParams.fSendMidiChannels; ++i) {
  360. if (fMidiCapturePortList[i] && (connections = fGraphManager->GetConnections(fMidiCapturePortList[i])) != 0) {
  361. for (int j = 0; connections[j]; j++) {
  362. JackPort* port_id = fGraphManager->GetPort(fGraphManager->GetPort(connections[j]));
  363. fConnections.push_back(make_pair(port_id->GetType(), make_pair(fGraphManager->GetPort(fMidiCapturePortList[i])->GetName(), connections[j])));
  364. jack_info("Save connection: %s %s", fGraphManager->GetPort(fMidiCapturePortList[i])->GetName(), connections[j]);
  365. }
  366. free(connections);
  367. }
  368. }
  369. }
  370. if (fMidiPlaybackPortList) {
  371. for (int i = 0; i < fParams.fReturnMidiChannels; ++i) {
  372. if (fMidiPlaybackPortList[i] && (connections = fGraphManager->GetConnections(fMidiPlaybackPortList[i])) != 0) {
  373. for (int j = 0; connections[j]; j++) {
  374. JackPort* port_id = fGraphManager->GetPort(fGraphManager->GetPort(connections[j]));
  375. fConnections.push_back(make_pair(port_id->GetType(), make_pair(connections[j], fGraphManager->GetPort(fMidiPlaybackPortList[i])->GetName())));
  376. jack_info("Save connection: %s %s", connections[j], fGraphManager->GetPort(fMidiPlaybackPortList[i])->GetName());
  377. }
  378. free(connections);
  379. }
  380. }
  381. }
  382. }
  383. JackMidiBuffer* JackNetDriver::GetMidiInputBuffer(int port_index)
  384. {
  385. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiCapturePortList[port_index], fEngineControl->fBufferSize));
  386. }
  387. JackMidiBuffer* JackNetDriver::GetMidiOutputBuffer(int port_index)
  388. {
  389. return static_cast<JackMidiBuffer*>(fGraphManager->GetBuffer(fMidiPlaybackPortList[port_index], fEngineControl->fBufferSize));
  390. }
  391. //transport---------------------------------------------------------------------------
  392. void JackNetDriver::DecodeTransportData()
  393. {
  394. //is there a new timebase master on the net master ?
  395. // - release timebase master only if it's a non-conditional request
  396. // - no change or no request : don't do anything
  397. // - conditional request : don't change anything too, the master will know if this slave is actually the timebase master
  398. int refnum;
  399. bool conditional;
  400. if (fSendTransportData.fTimebaseMaster == TIMEBASEMASTER) {
  401. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  402. if (refnum != -1) {
  403. fEngineControl->fTransport.ResetTimebase(refnum);
  404. }
  405. jack_info("The NetMaster is now the new timebase master.");
  406. }
  407. //is there a transport state change to handle ?
  408. if (fSendTransportData.fNewState &&(fSendTransportData.fState != fEngineControl->fTransport.GetState())) {
  409. switch (fSendTransportData.fState)
  410. {
  411. case JackTransportStopped :
  412. fEngineControl->fTransport.SetCommand(TransportCommandStop);
  413. jack_info("Master stops transport.");
  414. break;
  415. case JackTransportStarting :
  416. fEngineControl->fTransport.RequestNewPos(&fSendTransportData.fPosition);
  417. fEngineControl->fTransport.SetCommand(TransportCommandStart);
  418. jack_info("Master starts transport frame = %d", fSendTransportData.fPosition.frame);
  419. break;
  420. case JackTransportRolling :
  421. //fEngineControl->fTransport.SetCommand(TransportCommandStart);
  422. fEngineControl->fTransport.SetState(JackTransportRolling);
  423. jack_info("Master is rolling.");
  424. break;
  425. }
  426. }
  427. }
  428. void JackNetDriver::EncodeTransportData()
  429. {
  430. //is there a timebase master change ?
  431. int refnum;
  432. bool conditional;
  433. fEngineControl->fTransport.GetTimebaseMaster(refnum, conditional);
  434. if (refnum != fLastTimebaseMaster) {
  435. //timebase master has released its function
  436. if (refnum == -1) {
  437. fReturnTransportData.fTimebaseMaster = RELEASE_TIMEBASEMASTER;
  438. jack_info("Sending a timebase master release request.");
  439. } else {
  440. //there is a new timebase master
  441. fReturnTransportData.fTimebaseMaster = (conditional) ? CONDITIONAL_TIMEBASEMASTER : TIMEBASEMASTER;
  442. jack_info("Sending a %s timebase master request.", (conditional) ? "conditional" : "non-conditional");
  443. }
  444. fLastTimebaseMaster = refnum;
  445. } else {
  446. fReturnTransportData.fTimebaseMaster = NO_CHANGE;
  447. }
  448. //update transport state and position
  449. fReturnTransportData.fState = fEngineControl->fTransport.Query(&fReturnTransportData.fPosition);
  450. //is it a new state (that the master need to know...) ?
  451. fReturnTransportData.fNewState = ((fReturnTransportData.fState == JackTransportNetStarting) &&
  452. (fReturnTransportData.fState != fLastTransportState) &&
  453. (fReturnTransportData.fState != fSendTransportData.fState));
  454. if (fReturnTransportData.fNewState) {
  455. jack_info("Sending '%s'.", GetTransportState(fReturnTransportData.fState));
  456. }
  457. fLastTransportState = fReturnTransportData.fState;
  458. }
  459. //driver processes--------------------------------------------------------------------
  460. int JackNetDriver::Read()
  461. {
  462. //buffers
  463. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  464. fNetMidiCaptureBuffer->SetBuffer(midi_port_index, GetMidiInputBuffer(midi_port_index));
  465. }
  466. for (int audio_port_index = 0; audio_port_index < fParams.fSendAudioChannels; audio_port_index++) {
  467. #ifdef OPTIMIZED_PROTOCOL
  468. if (fGraphManager->GetConnectionsNum(fCapturePortList[audio_port_index]) > 0) {
  469. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer(audio_port_index));
  470. } else {
  471. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, NULL);
  472. }
  473. #else
  474. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, GetInputBuffer(audio_port_index));
  475. #endif
  476. }
  477. #ifdef JACK_MONITOR
  478. fNetTimeMon->New();
  479. #endif
  480. switch (SyncRecv()) {
  481. case SOCKET_ERROR:
  482. return SOCKET_ERROR;
  483. case NET_PACKET_ERROR:
  484. // since sync packet is incorrect, don't decode it and continue with data
  485. break;
  486. default:
  487. // decode sync
  488. DecodeSyncPacket();
  489. break;
  490. }
  491. #ifdef JACK_MONITOR
  492. // For timing
  493. fRcvSyncUst = GetMicroSeconds();
  494. #endif
  495. #ifdef JACK_MONITOR
  496. fNetTimeMon->Add(float(GetMicroSeconds() - fRcvSyncUst) / float(fEngineControl->fPeriodUsecs) * 100.f);
  497. #endif
  498. //audio, midi or sync if driver is late
  499. switch (DataRecv()) {
  500. case SOCKET_ERROR:
  501. return SOCKET_ERROR;
  502. case NET_PACKET_ERROR:
  503. jack_time_t cur_time = GetMicroSeconds();
  504. NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
  505. break;
  506. }
  507. //take the time at the beginning of the cycle
  508. JackDriver::CycleTakeBeginTime();
  509. #ifdef JACK_MONITOR
  510. fNetTimeMon->Add(float(GetMicroSeconds() - fRcvSyncUst) / float(fEngineControl->fPeriodUsecs) * 100.f);
  511. #endif
  512. return 0;
  513. }
  514. int JackNetDriver::Write()
  515. {
  516. //buffers
  517. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  518. fNetMidiPlaybackBuffer->SetBuffer(midi_port_index, GetMidiOutputBuffer(midi_port_index));
  519. }
  520. for (int audio_port_index = 0; audio_port_index < fPlaybackChannels; audio_port_index++) {
  521. #ifdef OPTIMIZED_PROTOCOL
  522. // Port is connected on other side...
  523. if (fNetAudioPlaybackBuffer->GetConnected(audio_port_index)
  524. && (fGraphManager->GetConnectionsNum(fPlaybackPortList[audio_port_index]) > 0)) {
  525. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, GetOutputBuffer(audio_port_index));
  526. } else {
  527. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, NULL);
  528. }
  529. #else
  530. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, GetOutputBuffer(audio_port_index));
  531. #endif
  532. }
  533. #ifdef JACK_MONITOR
  534. fNetTimeMon->AddLast(float(GetMicroSeconds() - fRcvSyncUst) / float(fEngineControl->fPeriodUsecs) * 100.f);
  535. #endif
  536. //sync
  537. EncodeSyncPacket();
  538. //send sync
  539. if (SyncSend() == SOCKET_ERROR) {
  540. return SOCKET_ERROR;
  541. }
  542. #ifdef JACK_MONITOR
  543. fNetTimeMon->Add(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  544. #endif
  545. //send data
  546. if (DataSend() == SOCKET_ERROR) {
  547. return SOCKET_ERROR;
  548. }
  549. #ifdef JACK_MONITOR
  550. fNetTimeMon->AddLast(((float)(GetMicroSeconds() - fRcvSyncUst) / (float)fEngineControl->fPeriodUsecs) * 100.f);
  551. #endif
  552. return 0;
  553. }
  554. //driver loader-----------------------------------------------------------------------
  555. #ifdef __cplusplus
  556. extern "C"
  557. {
  558. #endif
  559. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  560. {
  561. jack_driver_desc_t * desc;
  562. jack_driver_desc_filler_t filler;
  563. jack_driver_param_value_t value;
  564. desc = jack_driver_descriptor_construct("net", JackDriverMaster, "netjack slave backend component", &filler);
  565. strcpy(value.str, DEFAULT_MULTICAST_IP);
  566. jack_driver_descriptor_add_parameter(desc, &filler, "multicast-ip", 'a', JackDriverParamString, &value, NULL, "Multicast address, or explicit IP of the master", NULL);
  567. value.i = DEFAULT_PORT;
  568. jack_driver_descriptor_add_parameter(desc, &filler, "udp-net-port", 'p', JackDriverParamInt, &value, NULL, "UDP port", NULL);
  569. value.i = DEFAULT_MTU;
  570. jack_driver_descriptor_add_parameter(desc, &filler, "mtu", 'M', JackDriverParamInt, &value, NULL, "MTU to the master", NULL);
  571. value.i = -1;
  572. 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");
  573. 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");
  574. value.i = -1;
  575. 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");
  576. 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");
  577. #if HAVE_CELT
  578. value.i = -1;
  579. jack_driver_descriptor_add_parameter(desc, &filler, "celt", 'c', JackDriverParamInt, &value, NULL, "Set CELT encoding and number of kBits per channel", NULL);
  580. #endif
  581. #if HAVE_OPUS
  582. value.i = -1;
  583. jack_driver_descriptor_add_parameter(desc, &filler, "opus", 'O', JackDriverParamInt, &value, NULL, "Set Opus encoding and number of kBits per channel", NULL);
  584. #endif
  585. strcpy(value.str, "'hostname'");
  586. jack_driver_descriptor_add_parameter(desc, &filler, "client-name", 'n', JackDriverParamString, &value, NULL, "Name of the jack client", NULL);
  587. value.i = false;
  588. jack_driver_descriptor_add_parameter(desc, &filler, "auto-save", 's', JackDriverParamBool, &value, NULL, "Save/restore connection state when restarting", NULL);
  589. /*
  590. Deactivated for now..
  591. value.ui = 0U;
  592. jack_driver_descriptor_add_parameter(desc, &filler, "transport-sync", 't', JackDriverParamUInt, &value, NULL, "Sync transport with master's", NULL);
  593. */
  594. value.ui = 5U;
  595. jack_driver_descriptor_add_parameter(desc, &filler, "latency", 'l', JackDriverParamUInt, &value, NULL, "Network latency", NULL);
  596. return desc;
  597. }
  598. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  599. {
  600. char multicast_ip[32];
  601. char net_name[JACK_CLIENT_NAME_SIZE + 1] = {0};
  602. int udp_port;
  603. int mtu = DEFAULT_MTU;
  604. // Desactivated for now...
  605. uint transport_sync = 0;
  606. jack_nframes_t period_size = 1024;
  607. jack_nframes_t sample_rate = 48000;
  608. int audio_capture_ports = -1;
  609. int audio_playback_ports = -1;
  610. int midi_input_ports = -1;
  611. int midi_output_ports = -1;
  612. int celt_encoding = -1;
  613. int opus_encoding = -1;
  614. bool monitor = false;
  615. int network_latency = 5;
  616. const JSList* node;
  617. const jack_driver_param_t* param;
  618. bool auto_save = false;
  619. // Possibly use env variable for UDP port
  620. const char* default_udp_port = getenv("JACK_NETJACK_PORT");
  621. udp_port = (default_udp_port) ? atoi(default_udp_port) : DEFAULT_PORT;
  622. // Possibly use env variable for multicast IP
  623. const char* default_multicast_ip = getenv("JACK_NETJACK_MULTICAST");
  624. strcpy(multicast_ip, (default_multicast_ip) ? default_multicast_ip : DEFAULT_MULTICAST_IP);
  625. for (node = params; node; node = jack_slist_next(node)) {
  626. param = (const jack_driver_param_t*) node->data;
  627. switch (param->character)
  628. {
  629. case 'a' :
  630. assert(strlen(param->value.str) < 32);
  631. strcpy(multicast_ip, param->value.str);
  632. break;
  633. case 'p':
  634. udp_port = param->value.ui;
  635. break;
  636. case 'M':
  637. mtu = param->value.i;
  638. break;
  639. case 'C':
  640. audio_capture_ports = param->value.i;
  641. break;
  642. case 'P':
  643. audio_playback_ports = param->value.i;
  644. break;
  645. case 'i':
  646. midi_input_ports = param->value.i;
  647. break;
  648. case 'o':
  649. midi_output_ports = param->value.i;
  650. break;
  651. #if HAVE_CELT
  652. case 'c':
  653. celt_encoding = param->value.i;
  654. break;
  655. #endif
  656. #if HAVE_OPUS
  657. case 'O':
  658. opus_encoding = param->value.i;
  659. break;
  660. #endif
  661. case 'n' :
  662. strncpy(net_name, param->value.str, JACK_CLIENT_NAME_SIZE);
  663. break;
  664. case 's':
  665. auto_save = param->value.i;
  666. break;
  667. /*
  668. Deactivated for now..
  669. case 't' :
  670. transport_sync = param->value.ui;
  671. break;
  672. */
  673. case 'l' :
  674. network_latency = param->value.ui;
  675. if (network_latency > NETWORK_MAX_LATENCY) {
  676. printf("Error : network latency is limited to %d\n", NETWORK_MAX_LATENCY);
  677. return NULL;
  678. }
  679. break;
  680. }
  681. }
  682. try {
  683. Jack::JackDriverClientInterface* driver = new Jack::JackWaitThreadedDriver(
  684. new Jack::JackNetDriver("system", "net_pcm", engine, table, multicast_ip, udp_port, mtu,
  685. midi_input_ports, midi_output_ports,
  686. net_name, transport_sync,
  687. network_latency, celt_encoding, opus_encoding, auto_save));
  688. if (driver->Open(period_size, sample_rate, 1, 1, audio_capture_ports, audio_playback_ports, monitor, "from_master_", "to_master_", 0, 0) == 0) {
  689. return driver;
  690. } else {
  691. delete driver;
  692. return NULL;
  693. }
  694. } catch (...) {
  695. return NULL;
  696. }
  697. }
  698. #ifdef __cplusplus
  699. }
  700. #endif
  701. }