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