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.

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