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.

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