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