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.

739 lines
31KB

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