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.

727 lines
30KB

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