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.

768 lines
31KB

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