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.

1007 lines
37KB

  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 "JackNetManager.h"
  16. #include "JackArgParser.h"
  17. #include "JackServerGlobals.h"
  18. #include "JackLockedEngine.h"
  19. #include "thread.h"
  20. using namespace std;
  21. namespace Jack
  22. {
  23. //JackNetMaster******************************************************************************************************
  24. JackNetMaster::JackNetMaster(JackNetSocket& socket, session_params_t& params, const char* multicast_ip)
  25. : JackNetMasterInterface(params, socket, multicast_ip)
  26. {
  27. jack_log("JackNetMaster::JackNetMaster");
  28. //settings
  29. fName = const_cast<char*>(fParams.fName);
  30. fClient = NULL;
  31. fSendTransportData.fState = -1;
  32. fReturnTransportData.fState = -1;
  33. fLastTransportState = -1;
  34. int port_index;
  35. //jack audio ports
  36. fAudioCapturePorts = new jack_port_t* [fParams.fSendAudioChannels];
  37. for (port_index = 0; port_index < fParams.fSendAudioChannels; port_index++) {
  38. fAudioCapturePorts[port_index] = NULL;
  39. }
  40. fAudioPlaybackPorts = new jack_port_t* [fParams.fReturnAudioChannels];
  41. for (port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++) {
  42. fAudioPlaybackPorts[port_index] = NULL;
  43. }
  44. //jack midi ports
  45. fMidiCapturePorts = new jack_port_t* [fParams.fSendMidiChannels];
  46. for (port_index = 0; port_index < fParams.fSendMidiChannels; port_index++) {
  47. fMidiCapturePorts[port_index] = NULL;
  48. }
  49. fMidiPlaybackPorts = new jack_port_t* [fParams.fReturnMidiChannels];
  50. for (port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++) {
  51. fMidiPlaybackPorts[port_index] = NULL;
  52. }
  53. //monitor
  54. #ifdef JACK_MONITOR
  55. fPeriodUsecs = (int)(1000000.f * ((float) fParams.fPeriodSize / (float) fParams.fSampleRate));
  56. string plot_name;
  57. plot_name = string(fParams.fName);
  58. plot_name += string("_master");
  59. plot_name += string((fParams.fSlaveSyncMode) ? "_sync" : "_async");
  60. plot_name += string("_latency");
  61. fNetTimeMon = new JackGnuPlotMonitor<float>(128, 4, plot_name);
  62. string net_time_mon_fields[] =
  63. {
  64. string("sync send"),
  65. string("end of send"),
  66. string("sync recv"),
  67. string("end of cycle")
  68. };
  69. string net_time_mon_options[] =
  70. {
  71. string("set xlabel \"audio cycles\""),
  72. string("set ylabel \"% of audio cycle\"")
  73. };
  74. fNetTimeMon->SetPlotFile(net_time_mon_options, 2, net_time_mon_fields, 4);
  75. #endif
  76. }
  77. JackNetMaster::~JackNetMaster()
  78. {
  79. jack_log("JackNetMaster::~JackNetMaster ID = %u", fParams.fID);
  80. if (fClient) {
  81. jack_deactivate(fClient);
  82. FreePorts();
  83. jack_client_close(fClient);
  84. }
  85. delete[] fAudioCapturePorts;
  86. delete[] fAudioPlaybackPorts;
  87. delete[] fMidiCapturePorts;
  88. delete[] fMidiPlaybackPorts;
  89. #ifdef JACK_MONITOR
  90. fNetTimeMon->Save();
  91. delete fNetTimeMon;
  92. #endif
  93. }
  94. //init--------------------------------------------------------------------------------
  95. bool JackNetMaster::Init(bool auto_connect)
  96. {
  97. //network init
  98. if (!JackNetMasterInterface::Init()) {
  99. jack_error("JackNetMasterInterface::Init() error...");
  100. return false;
  101. }
  102. //set global parameters
  103. if (!SetParams()) {
  104. jack_error("SetParams error...");
  105. return false;
  106. }
  107. //jack client and process
  108. jack_status_t status;
  109. if ((fClient = jack_client_open(fName, JackNullOption, &status, NULL)) == NULL) {
  110. jack_error("Can't open a new JACK client");
  111. return false;
  112. }
  113. if (jack_set_process_callback(fClient, SetProcess, this) < 0) {
  114. goto fail;
  115. }
  116. if (jack_set_buffer_size_callback(fClient, SetBufferSize, this) < 0) {
  117. goto fail;
  118. }
  119. if (jack_set_sample_rate_callback(fClient, SetSampleRate, this) < 0) {
  120. goto fail;
  121. }
  122. if (jack_set_latency_callback(fClient, LatencyCallback, this) < 0) {
  123. goto fail;
  124. }
  125. /*
  126. if (jack_set_port_connect_callback(fClient, SetConnectCallback, this) < 0) {
  127. goto fail;
  128. }
  129. */
  130. if (AllocPorts() != 0) {
  131. jack_error("Can't allocate JACK ports");
  132. goto fail;
  133. }
  134. //process can now run
  135. fRunning = true;
  136. //finally activate jack client
  137. if (jack_activate(fClient) != 0) {
  138. jack_error("Can't activate JACK client");
  139. goto fail;
  140. }
  141. if (auto_connect) {
  142. ConnectPorts();
  143. }
  144. jack_info("New NetMaster started");
  145. return true;
  146. fail:
  147. FreePorts();
  148. jack_client_close(fClient);
  149. fClient = NULL;
  150. return false;
  151. }
  152. //jack ports--------------------------------------------------------------------------
  153. int JackNetMaster::AllocPorts()
  154. {
  155. int i;
  156. char name[32];
  157. jack_log("JackNetMaster::AllocPorts");
  158. //audio
  159. for (i = 0; i < fParams.fSendAudioChannels; i++) {
  160. snprintf(name, sizeof(name), "to_slave_%d", i+1);
  161. if ((fAudioCapturePorts[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0)) == NULL) {
  162. return -1;
  163. }
  164. }
  165. for (i = 0; i < fParams.fReturnAudioChannels; i++) {
  166. snprintf(name, sizeof(name), "from_slave_%d", i+1);
  167. if ((fAudioPlaybackPorts[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0)) == NULL) {
  168. return -1;
  169. }
  170. }
  171. //midi
  172. for (i = 0; i < fParams.fSendMidiChannels; i++) {
  173. snprintf(name, sizeof(name), "midi_to_slave_%d", i+1);
  174. if ((fMidiCapturePorts[i] = jack_port_register(fClient, name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput | JackPortIsTerminal, 0)) == NULL) {
  175. return -1;
  176. }
  177. }
  178. for (i = 0; i < fParams.fReturnMidiChannels; i++) {
  179. snprintf(name, sizeof(name), "midi_from_slave_%d", i+1);
  180. if ((fMidiPlaybackPorts[i] = jack_port_register(fClient, name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput | JackPortIsTerminal, 0)) == NULL) {
  181. return -1;
  182. }
  183. }
  184. return 0;
  185. }
  186. void JackNetMaster::ConnectPorts()
  187. {
  188. const char** ports = jack_get_ports(fClient, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical | JackPortIsOutput);
  189. if (ports != NULL) {
  190. for (int i = 0; i < fParams.fSendAudioChannels && ports[i]; i++) {
  191. jack_connect(fClient, ports[i], jack_port_name(fAudioCapturePorts[i]));
  192. }
  193. jack_free(ports);
  194. }
  195. ports = jack_get_ports(fClient, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical | JackPortIsInput);
  196. if (ports != NULL) {
  197. for (int i = 0; i < fParams.fReturnAudioChannels && ports[i]; i++) {
  198. jack_connect(fClient, jack_port_name(fAudioPlaybackPorts[i]), ports[i]);
  199. }
  200. jack_free(ports);
  201. }
  202. }
  203. void JackNetMaster::FreePorts()
  204. {
  205. jack_log("JackNetMaster::FreePorts ID = %u", fParams.fID);
  206. int port_index;
  207. for (port_index = 0; port_index < fParams.fSendAudioChannels; port_index++) {
  208. if (fAudioCapturePorts[port_index]) {
  209. jack_port_unregister(fClient, fAudioCapturePorts[port_index]);
  210. }
  211. }
  212. for (port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++) {
  213. if (fAudioPlaybackPorts[port_index]) {
  214. jack_port_unregister(fClient, fAudioPlaybackPorts[port_index]);
  215. }
  216. }
  217. for (port_index = 0; port_index < fParams.fSendMidiChannels; port_index++) {
  218. if (fMidiCapturePorts[port_index]) {
  219. jack_port_unregister(fClient, fMidiCapturePorts[port_index]);
  220. }
  221. }
  222. for (port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++) {
  223. if (fMidiPlaybackPorts[port_index]) {
  224. jack_port_unregister(fClient, fMidiPlaybackPorts[port_index]);
  225. }
  226. }
  227. }
  228. //transport---------------------------------------------------------------------------
  229. void JackNetMaster::EncodeTransportData()
  230. {
  231. //is there a new timebase master ?
  232. //TODO : check if any timebase callback has been called (and if it's conditional or not) and set correct value...
  233. fSendTransportData.fTimebaseMaster = NO_CHANGE;
  234. //update state and position
  235. fSendTransportData.fState = static_cast<uint>(jack_transport_query(fClient, &fSendTransportData.fPosition));
  236. //is it a new state ?
  237. fSendTransportData.fNewState = ((fSendTransportData.fState != fLastTransportState) && (fSendTransportData.fState != fReturnTransportData.fState));
  238. if (fSendTransportData.fNewState) {
  239. jack_info("Sending '%s' to '%s' frame = %ld", GetTransportState(fSendTransportData.fState), fParams.fName, fSendTransportData.fPosition.frame);
  240. }
  241. fLastTransportState = fSendTransportData.fState;
  242. }
  243. void JackNetMaster::DecodeTransportData()
  244. {
  245. //is there timebase master change ?
  246. if (fReturnTransportData.fTimebaseMaster != NO_CHANGE) {
  247. int timebase = 0;
  248. switch (fReturnTransportData.fTimebaseMaster)
  249. {
  250. case RELEASE_TIMEBASEMASTER :
  251. timebase = jack_release_timebase(fClient);
  252. if (timebase < 0) {
  253. jack_error("Can't release timebase master");
  254. } else {
  255. jack_info("'%s' isn't the timebase master anymore", fParams.fName);
  256. }
  257. break;
  258. case TIMEBASEMASTER :
  259. timebase = jack_set_timebase_callback(fClient, 0, SetTimebaseCallback, this);
  260. if (timebase < 0) {
  261. jack_error("Can't set a new timebase master");
  262. } else {
  263. jack_info("'%s' is the new timebase master", fParams.fName);
  264. }
  265. break;
  266. case CONDITIONAL_TIMEBASEMASTER :
  267. timebase = jack_set_timebase_callback(fClient, 1, SetTimebaseCallback, this);
  268. if (timebase != EBUSY) {
  269. if (timebase < 0)
  270. jack_error("Can't set a new timebase master");
  271. else
  272. jack_info("'%s' is the new timebase master", fParams.fName);
  273. }
  274. break;
  275. }
  276. }
  277. //is the slave in a new transport state and is this state different from master's ?
  278. if (fReturnTransportData.fNewState && (fReturnTransportData.fState != jack_transport_query(fClient, NULL))) {
  279. switch (fReturnTransportData.fState)
  280. {
  281. case JackTransportStopped :
  282. jack_transport_stop(fClient);
  283. jack_info("'%s' stops transport", fParams.fName);
  284. break;
  285. case JackTransportStarting :
  286. if (jack_transport_reposition(fClient, &fReturnTransportData.fPosition) == EINVAL)
  287. jack_error("Can't set new position");
  288. jack_transport_start(fClient);
  289. jack_info("'%s' starts transport frame = %d", fParams.fName, fReturnTransportData.fPosition.frame);
  290. break;
  291. case JackTransportNetStarting :
  292. jack_info("'%s' is ready to roll...", fParams.fName);
  293. break;
  294. case JackTransportRolling :
  295. jack_info("'%s' is rolling", fParams.fName);
  296. break;
  297. }
  298. }
  299. }
  300. void JackNetMaster::SetTimebaseCallback(jack_transport_state_t state, jack_nframes_t nframes, jack_position_t* pos, int new_pos, void* arg)
  301. {
  302. static_cast<JackNetMaster*>(arg)->TimebaseCallback(pos);
  303. }
  304. void JackNetMaster::TimebaseCallback(jack_position_t* pos)
  305. {
  306. pos->bar = fReturnTransportData.fPosition.bar;
  307. pos->beat = fReturnTransportData.fPosition.beat;
  308. pos->tick = fReturnTransportData.fPosition.tick;
  309. pos->bar_start_tick = fReturnTransportData.fPosition.bar_start_tick;
  310. pos->beats_per_bar = fReturnTransportData.fPosition.beats_per_bar;
  311. pos->beat_type = fReturnTransportData.fPosition.beat_type;
  312. pos->ticks_per_beat = fReturnTransportData.fPosition.ticks_per_beat;
  313. pos->beats_per_minute = fReturnTransportData.fPosition.beats_per_minute;
  314. }
  315. //sync--------------------------------------------------------------------------------
  316. bool JackNetMaster::IsSlaveReadyToRoll()
  317. {
  318. return (fReturnTransportData.fState == JackTransportNetStarting);
  319. }
  320. int JackNetMaster::SetBufferSize(jack_nframes_t nframes, void* arg)
  321. {
  322. JackNetMaster* obj = static_cast<JackNetMaster*>(arg);
  323. if (nframes != obj->fParams.fPeriodSize) {
  324. jack_error("Cannot currently handle buffer size change, so JackNetMaster proxy will be removed...");
  325. obj->Exit();
  326. }
  327. return 0;
  328. }
  329. int JackNetMaster::SetSampleRate(jack_nframes_t nframes, void* arg)
  330. {
  331. JackNetMaster* obj = static_cast<JackNetMaster*>(arg);
  332. if (nframes != obj->fParams.fSampleRate) {
  333. jack_error("Cannot currently handle sample rate change, so JackNetMaster proxy will be removed...");
  334. obj->Exit();
  335. }
  336. return 0;
  337. }
  338. void JackNetMaster::LatencyCallback(jack_latency_callback_mode_t mode, void* arg)
  339. {
  340. JackNetMaster* obj = static_cast<JackNetMaster*>(arg);
  341. jack_nframes_t port_latency = jack_get_buffer_size(obj->fClient);
  342. jack_latency_range_t range;
  343. //audio
  344. for (int i = 0; i < obj->fParams.fSendAudioChannels; i++) {
  345. //port latency
  346. range.min = range.max = float(obj->fParams.fNetworkLatency * port_latency) / 2.f;
  347. jack_port_set_latency_range(obj->fAudioCapturePorts[i], JackPlaybackLatency, &range);
  348. }
  349. //audio
  350. for (int i = 0; i < obj->fParams.fReturnAudioChannels; i++) {
  351. //port latency
  352. range.min = range.max = float(obj->fParams.fNetworkLatency * port_latency) / 2.f + ((obj->fParams.fSlaveSyncMode) ? 0 : port_latency);
  353. jack_port_set_latency_range(obj->fAudioPlaybackPorts[i], JackCaptureLatency, &range);
  354. }
  355. //midi
  356. for (int i = 0; i < obj->fParams.fSendMidiChannels; i++) {
  357. //port latency
  358. range.min = range.max = float(obj->fParams.fNetworkLatency * port_latency) / 2.f;
  359. jack_port_set_latency_range(obj->fMidiCapturePorts[i], JackPlaybackLatency, &range);
  360. }
  361. //midi
  362. for (int i = 0; i < obj->fParams.fReturnMidiChannels; i++) {
  363. //port latency
  364. range.min = range.max = obj->fParams.fNetworkLatency * port_latency + ((obj->fParams.fSlaveSyncMode) ? 0 : port_latency);
  365. jack_port_set_latency_range(obj->fMidiPlaybackPorts[i], JackCaptureLatency, &range);
  366. }
  367. }
  368. //process-----------------------------------------------------------------------------
  369. int JackNetMaster::SetProcess(jack_nframes_t nframes, void* arg)
  370. {
  371. try {
  372. return static_cast<JackNetMaster*>(arg)->Process();
  373. } catch (JackNetException& e) {
  374. return 0;
  375. }
  376. }
  377. void JackNetMaster::SetConnectCallback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  378. {
  379. static_cast<JackNetMaster*>(arg)->ConnectCallback(a, b, connect);
  380. }
  381. void JackNetMaster::ConnectCallback(jack_port_id_t a, jack_port_id_t b, int connect)
  382. {
  383. jack_info("JackNetMaster::ConnectCallback a = %d b = %d connect = %d", a, b, connect);
  384. if (connect) {
  385. jack_connect(fClient, jack_port_name(jack_port_by_id(fClient, a)), "system:playback_1");
  386. }
  387. }
  388. int JackNetMaster::Process()
  389. {
  390. if (!fRunning) {
  391. return 0;
  392. }
  393. #ifdef JACK_MONITOR
  394. jack_time_t begin_time = GetMicroSeconds();
  395. fNetTimeMon->New();
  396. #endif
  397. //buffers
  398. for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
  399. fNetMidiCaptureBuffer->SetBuffer(midi_port_index,
  400. static_cast<JackMidiBuffer*>(jack_port_get_buffer(fMidiCapturePorts[midi_port_index],
  401. fParams.fPeriodSize)));
  402. }
  403. for (int audio_port_index = 0; audio_port_index < fParams.fSendAudioChannels; audio_port_index++) {
  404. #ifdef OPTIMIZED_PROTOCOL
  405. if (fNetAudioCaptureBuffer->GetConnected(audio_port_index)) {
  406. // Port is connected on other side...
  407. fNetAudioCaptureBuffer->SetBuffer(audio_port_index,
  408. ((jack_port_connected(fAudioCapturePorts[audio_port_index]) > 0)
  409. ? static_cast<sample_t*>(jack_port_get_buffer(fAudioCapturePorts[audio_port_index], fParams.fPeriodSize))
  410. : NULL));
  411. } else {
  412. fNetAudioCaptureBuffer->SetBuffer(audio_port_index, NULL);
  413. }
  414. #else
  415. fNetAudioCaptureBuffer->SetBuffer(audio_port_index,
  416. static_cast<sample_t*>(jack_port_get_buffer(fAudioCapturePorts[audio_port_index],
  417. fParams.fPeriodSize)));
  418. #endif
  419. // TODO
  420. }
  421. for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
  422. fNetMidiPlaybackBuffer->SetBuffer(midi_port_index,
  423. static_cast<JackMidiBuffer*>(jack_port_get_buffer(fMidiPlaybackPorts[midi_port_index],
  424. fParams.fPeriodSize)));
  425. }
  426. for (int audio_port_index = 0; audio_port_index < fParams.fReturnAudioChannels; audio_port_index++) {
  427. #ifdef OPTIMIZED_PROTOCOL
  428. sample_t* out = (jack_port_connected(fAudioPlaybackPorts[audio_port_index]) > 0)
  429. ? static_cast<sample_t*>(jack_port_get_buffer(fAudioPlaybackPorts[audio_port_index], fParams.fPeriodSize))
  430. : NULL;
  431. if (out) {
  432. memset(out, 0, sizeof(float) * fParams.fPeriodSize);
  433. }
  434. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, out);
  435. #else
  436. sample_t* out = static_cast<sample_t*>(jack_port_get_buffer(fAudioPlaybackPorts[audio_port_index], fParams.fPeriodSize));
  437. if (out) {
  438. memset(out, 0, sizeof(float) * fParams.fPeriodSize);
  439. }
  440. fNetAudioPlaybackBuffer->SetBuffer(audio_port_index, out)));
  441. #endif
  442. }
  443. // encode the first packet
  444. EncodeSyncPacket();
  445. if (SyncSend() == SOCKET_ERROR) {
  446. return SOCKET_ERROR;
  447. }
  448. #ifdef JACK_MONITOR
  449. fNetTimeMon->Add((((float)(GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs) * 100.f);
  450. #endif
  451. // send data
  452. if (DataSend() == SOCKET_ERROR) {
  453. return SOCKET_ERROR;
  454. }
  455. #ifdef JACK_MONITOR
  456. fNetTimeMon->Add((((float)(GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs) * 100.f);
  457. #endif
  458. // receive sync
  459. int res = SyncRecv();
  460. switch (res) {
  461. case NET_SYNCHING:
  462. case SOCKET_ERROR:
  463. return res;
  464. case SYNC_PACKET_ERROR:
  465. // Since sync packet is incorrect, don't decode it and continue with data
  466. break;
  467. default:
  468. // Decode sync
  469. int unused_frames;
  470. DecodeSyncPacket(unused_frames);
  471. break;
  472. }
  473. #ifdef JACK_MONITOR
  474. fNetTimeMon->Add((((float)(GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs) * 100.f);
  475. #endif
  476. // receive data
  477. res = DataRecv();
  478. switch (res) {
  479. case 0:
  480. case SOCKET_ERROR:
  481. return res;
  482. case DATA_PACKET_ERROR:
  483. // Well not a real XRun...
  484. JackServerGlobals::fInstance->GetEngine()->NotifyClientXRun(ALL_CLIENTS);
  485. break;
  486. }
  487. #ifdef JACK_MONITOR
  488. fNetTimeMon->AddLast((((float)(GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs) * 100.f);
  489. #endif
  490. return 0;
  491. }
  492. void JackNetMaster::SaveConnections(connections_list_t& connections)
  493. {
  494. // Audio
  495. for (int i = 0; i < fParams.fSendAudioChannels; i++) {
  496. const char** connected_port = jack_port_get_all_connections(fClient, fAudioCapturePorts[i]);
  497. if (connected_port != NULL) {
  498. for (int port = 0; connected_port[port]; port++) {
  499. connections.push_back(make_pair(connected_port[port], jack_port_name(fAudioCapturePorts[i])));
  500. jack_log("INPUT %s ==> %s", connected_port[port], jack_port_name(fAudioCapturePorts[i]));
  501. }
  502. jack_free(connected_port);
  503. }
  504. }
  505. for (int i = 0; i < fParams.fReturnAudioChannels; i++) {
  506. const char** connected_port = jack_port_get_all_connections(fClient, fAudioPlaybackPorts[i]);
  507. if (connected_port != NULL) {
  508. for (int port = 0; connected_port[port]; port++) {
  509. connections.push_back(make_pair(jack_port_name(fAudioPlaybackPorts[i]), connected_port[port]));
  510. jack_log("OUTPUT %s ==> %s", jack_port_name(fAudioPlaybackPorts[i]), connected_port[port]);
  511. }
  512. jack_free(connected_port);
  513. }
  514. }
  515. // MIDI
  516. for (int i = 0; i < fParams.fSendMidiChannels; i++) {
  517. const char** connected_port = jack_port_get_all_connections(fClient, fMidiCapturePorts[i]);
  518. if (connected_port != NULL) {
  519. for (int port = 0; connected_port[port]; port++) {
  520. connections.push_back(make_pair(connected_port[port], jack_port_name(fMidiCapturePorts[i])));
  521. jack_log("INPUT %s ==> %s", connected_port[port], jack_port_name(fMidiCapturePorts[i]));
  522. }
  523. jack_free(connected_port);
  524. }
  525. }
  526. for (int i = 0; i < fParams.fReturnMidiChannels; i++) {
  527. const char** connected_port = jack_port_get_all_connections(fClient, fMidiPlaybackPorts[i]);
  528. if (connected_port != NULL) {
  529. for (int port = 0; connected_port[port]; port++) {
  530. connections.push_back(make_pair(jack_port_name(fMidiPlaybackPorts[i]), connected_port[port]));
  531. jack_log("OUTPUT %s ==> %s", jack_port_name(fMidiPlaybackPorts[i]), connected_port[port]);
  532. }
  533. jack_free(connected_port);
  534. }
  535. }
  536. }
  537. void JackNetMaster::LoadConnections(const connections_list_t& connections)
  538. {
  539. list<pair<string, string> >::const_iterator it;
  540. for (it = connections.begin(); it != connections.end(); it++) {
  541. pair<string, string> connection = *it;
  542. jack_connect(fClient, connection.first.c_str(), connection.second.c_str());
  543. }
  544. }
  545. //JackNetMasterManager***********************************************************************************************
  546. JackNetMasterManager::JackNetMasterManager(jack_client_t* client, const JSList* params) : fSocket()
  547. {
  548. jack_log("JackNetMasterManager::JackNetMasterManager");
  549. fClient = client;
  550. fName = jack_get_client_name(fClient);
  551. fGlobalID = 0;
  552. fRunning = true;
  553. fAutoConnect = false;
  554. fAutoSave = false;
  555. const JSList* node;
  556. const jack_driver_param_t* param;
  557. jack_on_shutdown(fClient, SetShutDown, this);
  558. // Possibly use env variable
  559. const char* default_udp_port = getenv("JACK_NETJACK_PORT");
  560. fSocket.SetPort((default_udp_port) ? atoi(default_udp_port) : DEFAULT_PORT);
  561. const char* default_multicast_ip = getenv("JACK_NETJACK_MULTICAST");
  562. if (default_multicast_ip) {
  563. strcpy(fMulticastIP, default_multicast_ip);
  564. } else {
  565. strcpy(fMulticastIP, DEFAULT_MULTICAST_IP);
  566. }
  567. for (node = params; node; node = jack_slist_next(node)) {
  568. param = (const jack_driver_param_t*) node->data;
  569. switch (param->character) {
  570. case 'a' :
  571. if (strlen(param->value.str) < 32) {
  572. strcpy(fMulticastIP, param->value.str);
  573. } else {
  574. jack_error("Can't use multicast address %s, using default %s", param->value.ui, DEFAULT_MULTICAST_IP);
  575. }
  576. break;
  577. case 'p':
  578. fSocket.SetPort(param->value.ui);
  579. break;
  580. case 'c':
  581. fAutoConnect = true;
  582. break;
  583. case 's':
  584. fAutoSave = true;
  585. break;
  586. }
  587. }
  588. //set sync callback
  589. jack_set_sync_callback(fClient, SetSyncCallback, this);
  590. //activate the client (for sync callback)
  591. if (jack_activate(fClient) != 0) {
  592. jack_error("Can't activate the NetManager client, transport disabled");
  593. }
  594. //launch the manager thread
  595. if (jack_client_create_thread(fClient, &fThread, 0, 0, NetManagerThread, this)) {
  596. jack_error("Can't create the NetManager control thread");
  597. }
  598. }
  599. JackNetMasterManager::~JackNetMasterManager()
  600. {
  601. jack_log("JackNetMasterManager::~JackNetMasterManager");
  602. ShutDown();
  603. }
  604. int JackNetMasterManager::CountIO(const char* type, int flags)
  605. {
  606. int count = 0;
  607. const char** ports = jack_get_ports(fClient, NULL, type, flags);
  608. if (ports != NULL) {
  609. while (ports[count]) { count++; }
  610. jack_free(ports);
  611. }
  612. return count;
  613. }
  614. void JackNetMasterManager::SetShutDown(void* arg)
  615. {
  616. static_cast<JackNetMasterManager*>(arg)->ShutDown();
  617. }
  618. void JackNetMasterManager::ShutDown()
  619. {
  620. jack_log("JackNetMasterManager::ShutDown");
  621. if (fRunning) {
  622. jack_client_kill_thread(fClient, fThread);
  623. fRunning = false;
  624. }
  625. master_list_t::iterator it;
  626. for (it = fMasterList.begin(); it != fMasterList.end(); it++) {
  627. delete (*it);
  628. }
  629. fMasterList.clear();
  630. fSocket.Close();
  631. SocketAPIEnd();
  632. }
  633. int JackNetMasterManager::SetSyncCallback(jack_transport_state_t state, jack_position_t* pos, void* arg)
  634. {
  635. return static_cast<JackNetMasterManager*>(arg)->SyncCallback(state, pos);
  636. }
  637. int JackNetMasterManager::SyncCallback(jack_transport_state_t state, jack_position_t* pos)
  638. {
  639. //check if each slave is ready to roll
  640. int res = 1;
  641. master_list_it_t it;
  642. for (it = fMasterList.begin(); it != fMasterList.end(); it++) {
  643. if (!(*it)->IsSlaveReadyToRoll()) {
  644. res = 0;
  645. }
  646. }
  647. jack_log("JackNetMasterManager::SyncCallback returns '%s'", (res) ? "true" : "false");
  648. return res;
  649. }
  650. void* JackNetMasterManager::NetManagerThread(void* arg)
  651. {
  652. JackNetMasterManager* master_manager = static_cast<JackNetMasterManager*>(arg);
  653. jack_info("Starting Jack NetManager");
  654. jack_info("Listening on '%s:%d'", master_manager->fMulticastIP, master_manager->fSocket.GetPort());
  655. master_manager->Run();
  656. return NULL;
  657. }
  658. void JackNetMasterManager::Run()
  659. {
  660. jack_log("JackNetMasterManager::Run");
  661. //utility variables
  662. int attempt = 0;
  663. //data
  664. session_params_t host_params;
  665. int rx_bytes = 0;
  666. JackNetMaster* net_master;
  667. //init socket API (win32)
  668. if (SocketAPIInit() < 0) {
  669. jack_error("Can't init Socket API, exiting...");
  670. return;
  671. }
  672. //socket
  673. if (fSocket.NewSocket() == SOCKET_ERROR) {
  674. jack_error("Can't create NetManager input socket : %s", StrError(NET_ERROR_CODE));
  675. return;
  676. }
  677. //bind the socket to the local port
  678. if (fSocket.Bind() == SOCKET_ERROR) {
  679. jack_error("Can't bind NetManager socket : %s", StrError(NET_ERROR_CODE));
  680. fSocket.Close();
  681. return;
  682. }
  683. //join multicast group
  684. if (fSocket.JoinMCastGroup(fMulticastIP) == SOCKET_ERROR) {
  685. jack_error("Can't join multicast group : %s", StrError(NET_ERROR_CODE));
  686. }
  687. //local loop
  688. if (fSocket.SetLocalLoop() == SOCKET_ERROR) {
  689. jack_error("Can't set local loop : %s", StrError(NET_ERROR_CODE));
  690. }
  691. //set a timeout on the multicast receive (the thread can now be cancelled)
  692. if (fSocket.SetTimeOut(MANAGER_INIT_TIMEOUT) == SOCKET_ERROR) {
  693. jack_error("Can't set timeout : %s", StrError(NET_ERROR_CODE));
  694. }
  695. //main loop, wait for data, deal with it and wait again
  696. do
  697. {
  698. session_params_t net_params;
  699. rx_bytes = fSocket.CatchHost(&net_params, sizeof(session_params_t), 0);
  700. SessionParamsNToH(&net_params, &host_params);
  701. if ((rx_bytes == SOCKET_ERROR) && (fSocket.GetError() != NET_NO_DATA)) {
  702. jack_error("Error in receive : %s", StrError(NET_ERROR_CODE));
  703. if (++attempt == 10) {
  704. jack_error("Can't receive on the socket, exiting net manager");
  705. return;
  706. }
  707. }
  708. if (rx_bytes == sizeof(session_params_t)) {
  709. switch (GetPacketType(&host_params))
  710. {
  711. case SLAVE_AVAILABLE:
  712. if ((net_master = InitMaster(host_params))) {
  713. SessionParamsDisplay(&net_master->fParams);
  714. } else {
  715. jack_error("Can't init new NetMaster...");
  716. }
  717. jack_info("Waiting for a slave...");
  718. break;
  719. case KILL_MASTER:
  720. if (KillMaster(&host_params)) {
  721. jack_info("Waiting for a slave...");
  722. }
  723. break;
  724. default:
  725. break;
  726. }
  727. }
  728. }
  729. while (fRunning);
  730. }
  731. JackNetMaster* JackNetMasterManager::InitMaster(session_params_t& params)
  732. {
  733. jack_log("JackNetMasterManager::InitMaster slave : %s", params.fName);
  734. //check MASTER <<==> SLAVE network protocol coherency
  735. if (params.fProtocolVersion != NETWORK_PROTOCOL) {
  736. jack_error("Error : slave '%s' is running with a different protocol %d != %d", params.fName, params.fProtocolVersion, NETWORK_PROTOCOL);
  737. return NULL;
  738. }
  739. //settings
  740. fSocket.GetName(params.fMasterNetName);
  741. params.fID = ++fGlobalID;
  742. params.fSampleRate = jack_get_sample_rate(fClient);
  743. params.fPeriodSize = jack_get_buffer_size(fClient);
  744. if (params.fSendAudioChannels == -1) {
  745. params.fSendAudioChannels = CountIO(JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical | JackPortIsOutput);
  746. jack_info("Takes physical %d audio input(s) for slave", params.fSendAudioChannels);
  747. }
  748. if (params.fReturnAudioChannels == -1) {
  749. params.fReturnAudioChannels = CountIO(JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical | JackPortIsInput);
  750. jack_info("Takes physical %d audio output(s) for slave", params.fReturnAudioChannels);
  751. }
  752. if (params.fSendMidiChannels == -1) {
  753. params.fSendMidiChannels = CountIO(JACK_DEFAULT_MIDI_TYPE, JackPortIsPhysical | JackPortIsOutput);
  754. jack_info("Takes physical %d MIDI input(s) for slave", params.fSendMidiChannels);
  755. }
  756. if (params.fReturnMidiChannels == -1) {
  757. params.fReturnMidiChannels = CountIO(JACK_DEFAULT_MIDI_TYPE, JackPortIsPhysical | JackPortIsInput);
  758. jack_info("Takes physical %d MIDI output(s) for slave", params.fReturnMidiChannels);
  759. }
  760. //create a new master and add it to the list
  761. JackNetMaster* master = new JackNetMaster(fSocket, params, fMulticastIP);
  762. if (master->Init(fAutoConnect)) {
  763. fMasterList.push_back(master);
  764. if (fAutoSave && fMasterConnectionList.find(params.fName) != fMasterConnectionList.end()) {
  765. master->LoadConnections(fMasterConnectionList[params.fName]);
  766. }
  767. return master;
  768. } else {
  769. delete master;
  770. return NULL;
  771. }
  772. }
  773. master_list_it_t JackNetMasterManager::FindMaster(uint32_t id)
  774. {
  775. jack_log("JackNetMasterManager::FindMaster ID = %u", id);
  776. master_list_it_t it;
  777. for (it = fMasterList.begin(); it != fMasterList.end(); it++) {
  778. if ((*it)->fParams.fID == id) {
  779. return it;
  780. }
  781. }
  782. return it;
  783. }
  784. int JackNetMasterManager::KillMaster(session_params_t* params)
  785. {
  786. jack_log("JackNetMasterManager::KillMaster ID = %u", params->fID);
  787. master_list_it_t master_it = FindMaster(params->fID);
  788. if (master_it != fMasterList.end()) {
  789. if (fAutoSave) {
  790. fMasterConnectionList[params->fName].clear();
  791. (*master_it)->SaveConnections(fMasterConnectionList[params->fName]);
  792. }
  793. fMasterList.erase(master_it);
  794. delete (*master_it);
  795. return 1;
  796. }
  797. return 0;
  798. }
  799. }//namespace
  800. static Jack::JackNetMasterManager* master_manager = NULL;
  801. #ifdef __cplusplus
  802. extern "C"
  803. {
  804. #endif
  805. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
  806. {
  807. jack_driver_desc_t * desc;
  808. jack_driver_desc_filler_t filler;
  809. jack_driver_param_value_t value;
  810. desc = jack_driver_descriptor_construct("netmanager", JackDriverNone, "netjack multi-cast master component", &filler);
  811. strcpy(value.str, DEFAULT_MULTICAST_IP);
  812. jack_driver_descriptor_add_parameter(desc, &filler, "multicast-ip", 'a', JackDriverParamString, &value, NULL, "Multicast address", NULL);
  813. value.i = DEFAULT_PORT;
  814. jack_driver_descriptor_add_parameter(desc, &filler, "udp-net-port", 'p', JackDriverParamInt, &value, NULL, "UDP port", NULL);
  815. value.i = false;
  816. jack_driver_descriptor_add_parameter(desc, &filler, "auto-connect", 'c', JackDriverParamBool, &value, NULL, "Auto connect netmaster to system ports", NULL);
  817. value.i = false;
  818. jack_driver_descriptor_add_parameter(desc, &filler, "auto-save", 's', JackDriverParamBool, &value, NULL, "Save/restore netmaster connection state when restarted", NULL);
  819. return desc;
  820. }
  821. SERVER_EXPORT int jack_internal_initialize(jack_client_t* jack_client, const JSList* params)
  822. {
  823. if (master_manager) {
  824. jack_error("Master Manager already loaded");
  825. return 1;
  826. } else {
  827. jack_log("Loading Master Manager");
  828. master_manager = new Jack::JackNetMasterManager(jack_client, params);
  829. return (master_manager) ? 0 : 1;
  830. }
  831. }
  832. SERVER_EXPORT int jack_initialize(jack_client_t* jack_client, const char* load_init)
  833. {
  834. JSList* params = NULL;
  835. bool parse_params = true;
  836. int res = 1;
  837. jack_driver_desc_t* desc = jack_get_descriptor();
  838. Jack::JackArgParser parser(load_init);
  839. if (parser.GetArgc() > 0) {
  840. parse_params = parser.ParseParams(desc, &params);
  841. }
  842. if (parse_params) {
  843. res = jack_internal_initialize(jack_client, params);
  844. parser.FreeParams(params);
  845. }
  846. return res;
  847. }
  848. SERVER_EXPORT void jack_finish(void* arg)
  849. {
  850. if (master_manager) {
  851. jack_log("Unloading Master Manager");
  852. delete master_manager;
  853. master_manager = NULL;
  854. }
  855. }
  856. #ifdef __cplusplus
  857. }
  858. #endif