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.

920 lines
34KB

  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 "JackNetTool.h"
  16. #include "JackError.h"
  17. #ifdef __APPLE__
  18. #include <mach/mach_time.h>
  19. class HardwareClock
  20. {
  21. public:
  22. HardwareClock();
  23. void Reset();
  24. void Update();
  25. float GetDeltaTime() const;
  26. double GetTime() const;
  27. private:
  28. double m_clockToSeconds;
  29. uint64_t m_startAbsTime;
  30. uint64_t m_lastAbsTime;
  31. double m_time;
  32. float m_deltaTime;
  33. };
  34. HardwareClock::HardwareClock()
  35. {
  36. mach_timebase_info_data_t info;
  37. mach_timebase_info(&info);
  38. m_clockToSeconds = (double)info.numer/info.denom/1000000000.0;
  39. Reset();
  40. }
  41. void HardwareClock::Reset()
  42. {
  43. m_startAbsTime = mach_absolute_time();
  44. m_lastAbsTime = m_startAbsTime;
  45. m_time = m_startAbsTime*m_clockToSeconds;
  46. m_deltaTime = 1.0f/60.0f;
  47. }
  48. void HardwareClock::Update()
  49. {
  50. const uint64_t currentTime = mach_absolute_time();
  51. const uint64_t dt = currentTime - m_lastAbsTime;
  52. m_time = currentTime*m_clockToSeconds;
  53. m_deltaTime = (double)dt*m_clockToSeconds;
  54. m_lastAbsTime = currentTime;
  55. }
  56. float HardwareClock::GetDeltaTime() const
  57. {
  58. return m_deltaTime;
  59. }
  60. double HardwareClock::GetTime() const
  61. {
  62. return m_time;
  63. }
  64. #endif
  65. using namespace std;
  66. namespace Jack
  67. {
  68. // NetMidiBuffer**********************************************************************************
  69. NetMidiBuffer::NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  70. {
  71. fNPorts = nports;
  72. fMaxBufsize = fNPorts * sizeof(sample_t) * params->fPeriodSize;
  73. fMaxPcktSize = params->fMtu - sizeof(packet_header_t);
  74. fBuffer = new char[fMaxBufsize];
  75. fPortBuffer = new JackMidiBuffer* [fNPorts];
  76. for (int port_index = 0; port_index < fNPorts; port_index++) {
  77. fPortBuffer[port_index] = NULL;
  78. }
  79. fNetBuffer = net_buffer;
  80. fCycleBytesSize = params->fMtu
  81. * (max(params->fSendMidiChannels, params->fReturnMidiChannels)
  82. * params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
  83. }
  84. NetMidiBuffer::~NetMidiBuffer()
  85. {
  86. delete[] fBuffer;
  87. delete[] fPortBuffer;
  88. }
  89. size_t NetMidiBuffer::GetCycleSize()
  90. {
  91. return fCycleBytesSize;
  92. }
  93. int NetMidiBuffer::GetNumPackets(int data_size, int max_size)
  94. {
  95. int res1 = data_size % max_size;
  96. int res2 = data_size / max_size;
  97. return (res1) ? res2 + 1 : res2;
  98. }
  99. void NetMidiBuffer::SetBuffer(int index, JackMidiBuffer* buffer)
  100. {
  101. fPortBuffer[index] = buffer;
  102. }
  103. JackMidiBuffer* NetMidiBuffer::GetBuffer(int index)
  104. {
  105. return fPortBuffer[index];
  106. }
  107. void NetMidiBuffer::DisplayEvents()
  108. {
  109. for (int port_index = 0; port_index < fNPorts; port_index++) {
  110. for (uint event = 0; event < fPortBuffer[port_index]->event_count; event++) {
  111. if (fPortBuffer[port_index]->IsValid()) {
  112. jack_info("port %d : midi event %u/%u -> time : %u, size : %u",
  113. port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
  114. fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size);
  115. }
  116. }
  117. }
  118. }
  119. int NetMidiBuffer::RenderFromJackPorts()
  120. {
  121. int pos = 0;
  122. size_t copy_size;
  123. for (int port_index = 0; port_index < fNPorts; port_index++) {
  124. char* write_pos = fBuffer + pos;
  125. copy_size = sizeof(JackMidiBuffer) + fPortBuffer[port_index]->event_count * sizeof(JackMidiEvent);
  126. memcpy(fBuffer + pos, fPortBuffer[port_index], copy_size);
  127. pos += copy_size;
  128. memcpy(fBuffer + pos,
  129. fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
  130. fPortBuffer[port_index]->write_pos);
  131. pos += fPortBuffer[port_index]->write_pos;
  132. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
  133. MidiBufferHToN(midi_buffer, midi_buffer);
  134. }
  135. return pos;
  136. }
  137. void NetMidiBuffer::RenderToJackPorts()
  138. {
  139. int pos = 0;
  140. size_t copy_size;
  141. for (int port_index = 0; port_index < fNPorts; port_index++) {
  142. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
  143. MidiBufferNToH(midi_buffer, midi_buffer);
  144. copy_size = sizeof(JackMidiBuffer) + reinterpret_cast<JackMidiBuffer*>(fBuffer + pos)->event_count * sizeof(JackMidiEvent);
  145. memcpy(fPortBuffer[port_index], fBuffer + pos, copy_size);
  146. pos += copy_size;
  147. memcpy(fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
  148. fBuffer + pos,
  149. fPortBuffer[port_index]->write_pos);
  150. pos += fPortBuffer[port_index]->write_pos;
  151. }
  152. }
  153. void NetMidiBuffer::RenderFromNetwork(int sub_cycle, size_t copy_size)
  154. {
  155. memcpy(fBuffer + sub_cycle * fMaxPcktSize, fNetBuffer, copy_size);
  156. }
  157. int NetMidiBuffer::RenderToNetwork(int sub_cycle, size_t total_size)
  158. {
  159. int size = total_size - sub_cycle * fMaxPcktSize;
  160. int copy_size = (size <= fMaxPcktSize) ? size : fMaxPcktSize;
  161. memcpy(fNetBuffer, fBuffer + sub_cycle * fMaxPcktSize, copy_size);
  162. return copy_size;
  163. }
  164. // net audio buffer *********************************************************************************
  165. NetAudioBuffer::NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  166. {
  167. fNPorts = nports;
  168. fNetBuffer = net_buffer;
  169. fNumPackets = 0;
  170. fPortBuffer = new sample_t*[fNPorts];
  171. fConnectedPorts = new bool[fNPorts];
  172. for (int port_index = 0; port_index < fNPorts; port_index++) {
  173. fPortBuffer[port_index] = NULL;
  174. fConnectedPorts[port_index] = true;
  175. }
  176. fLastSubCycle = 0;
  177. fPeriodSize = 0;
  178. fSubPeriodSize = 0;
  179. fSubPeriodBytesSize = 0;
  180. fCycleDuration = 0.f;
  181. fCycleBytesSize = 0;
  182. }
  183. NetAudioBuffer::~NetAudioBuffer()
  184. {
  185. delete [] fConnectedPorts;
  186. delete [] fPortBuffer;
  187. }
  188. void NetAudioBuffer::SetBuffer(int index, sample_t* buffer)
  189. {
  190. fPortBuffer[index] = buffer;
  191. }
  192. sample_t* NetAudioBuffer::GetBuffer(int index)
  193. {
  194. return fPortBuffer[index];
  195. }
  196. int NetAudioBuffer::CheckPacket(int cycle, int sub_cycle)
  197. {
  198. int res;
  199. if (sub_cycle != fLastSubCycle + 1) {
  200. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  201. res = DATA_PACKET_ERROR;
  202. } else {
  203. res = 0;
  204. }
  205. fLastSubCycle = sub_cycle;
  206. return res;
  207. }
  208. void NetAudioBuffer::NextCycle()
  209. {
  210. // reset for next cycle
  211. fLastSubCycle = -1;
  212. }
  213. void NetAudioBuffer::Cleanup()
  214. {
  215. for (int port_index = 0; port_index < fNPorts; port_index++) {
  216. if (fPortBuffer[port_index]) {
  217. memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
  218. }
  219. }
  220. }
  221. //network<->buffer
  222. int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer)
  223. {
  224. int active_ports = 0;
  225. int* active_port_address = (int*)net_buffer;
  226. for (int port_index = 0; port_index < fNPorts; port_index++) {
  227. // Write the active port number
  228. if (fPortBuffer[port_index]) {
  229. *active_port_address = htonl(port_index);
  230. active_port_address++;
  231. active_ports++;
  232. assert(active_ports < 256);
  233. }
  234. }
  235. return active_ports;
  236. }
  237. void NetAudioBuffer::ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
  238. {
  239. int* active_port_address = (int*)net_buffer;
  240. for (int port_index = 0; port_index < fNPorts; port_index++) {
  241. fConnectedPorts[port_index] = false;
  242. }
  243. for (uint port_index = 0; port_index < port_num; port_index++) {
  244. int active_port = ntohl(*active_port_address);
  245. assert(active_port < fNPorts);
  246. fConnectedPorts[active_port] = true;
  247. active_port_address++;
  248. }
  249. }
  250. int NetAudioBuffer::RenderFromJackPorts(int unused_frames)
  251. {
  252. // Count active ports
  253. int active_ports = 0;
  254. for (int port_index = 0; port_index < fNPorts; port_index++) {
  255. if (fPortBuffer[port_index]) {
  256. active_ports++;
  257. }
  258. }
  259. return active_ports;
  260. }
  261. void NetAudioBuffer::RenderToJackPorts(int unused_frames)
  262. {
  263. // Nothing to do
  264. NextCycle();
  265. }
  266. // Float converter
  267. NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  268. : NetAudioBuffer(params, nports, net_buffer)
  269. {
  270. fPeriodSize = params->fPeriodSize;
  271. fPacketSize = PACKET_AVAILABLE_SIZE(params);
  272. UpdateParams(max(params->fReturnAudioChannels, params->fSendAudioChannels));
  273. fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
  274. fCycleBytesSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
  275. fLastSubCycle = -1;
  276. }
  277. NetFloatAudioBuffer::~NetFloatAudioBuffer()
  278. {}
  279. // needed size in bytes for an entire cycle
  280. size_t NetFloatAudioBuffer::GetCycleSize()
  281. {
  282. return fCycleBytesSize;
  283. }
  284. // cycle duration in sec
  285. float NetFloatAudioBuffer::GetCycleDuration()
  286. {
  287. return fCycleDuration;
  288. }
  289. void NetFloatAudioBuffer::UpdateParams(int active_ports)
  290. {
  291. if (active_ports == 0) {
  292. fSubPeriodSize = fPeriodSize;
  293. } else {
  294. jack_nframes_t period = int(powf(2.f, int(log(float(fPacketSize) / (active_ports * sizeof(sample_t))) / log(2.))));
  295. fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
  296. }
  297. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t) + sizeof(int); // The port number in coded on 4 bytes
  298. fNumPackets = fPeriodSize / fSubPeriodSize; // At least one packet
  299. }
  300. int NetFloatAudioBuffer::GetNumPackets(int active_ports)
  301. {
  302. UpdateParams(active_ports);
  303. /*
  304. jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
  305. fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
  306. */
  307. return fNumPackets;
  308. }
  309. //jack<->buffer
  310. int NetFloatAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  311. {
  312. // Cleanup all JACK ports at the beginning of the cycle
  313. if (sub_cycle == 0) {
  314. Cleanup();
  315. }
  316. if (port_num > 0) {
  317. UpdateParams(port_num);
  318. for (uint32_t port_index = 0; port_index < port_num; port_index++) {
  319. // Only copy to active ports : read the active port number then audio data
  320. int* active_port_address = (int*)(fNetBuffer + port_index * fSubPeriodBytesSize);
  321. int active_port = ntohl(*active_port_address);
  322. RenderFromNetwork((char*)(active_port_address + 1), active_port, sub_cycle);
  323. }
  324. }
  325. return CheckPacket(cycle, sub_cycle);
  326. }
  327. int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  328. {
  329. int active_ports = 0;
  330. for (int port_index = 0; port_index < fNPorts; port_index++) {
  331. // Only copy from active ports : write the active port number then audio data
  332. if (fPortBuffer[port_index]) {
  333. int* active_port_address = (int*)(fNetBuffer + active_ports * fSubPeriodBytesSize);
  334. *active_port_address = htonl(port_index);
  335. RenderToNetwork((char*)(active_port_address + 1), port_index, sub_cycle);
  336. active_ports++;
  337. }
  338. }
  339. return port_num * fSubPeriodBytesSize;
  340. }
  341. #ifdef __BIG_ENDIAN__
  342. static inline jack_default_audio_sample_t SwapFloat(jack_default_audio_sample_t f)
  343. {
  344. union
  345. {
  346. jack_default_audio_sample_t f;
  347. unsigned char b[4];
  348. } dat1, dat2;
  349. dat1.f = f;
  350. dat2.b[0] = dat1.b[3];
  351. dat2.b[1] = dat1.b[2];
  352. dat2.b[2] = dat1.b[1];
  353. dat2.b[3] = dat1.b[0];
  354. return dat2.f;
  355. }
  356. void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
  357. {
  358. if (fPortBuffer[active_port]) {
  359. jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(net_buffer);
  360. jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
  361. for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
  362. dst[sample] = SwapFloat(src[sample]);
  363. }
  364. }
  365. }
  366. void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
  367. {
  368. for (int port_index = 0; port_index < fNPorts; port_index++ ) {
  369. jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
  370. jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(net_buffer);
  371. for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
  372. dst[sample] = SwapFloat(src[sample]);
  373. }
  374. }
  375. }
  376. #else
  377. void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
  378. {
  379. if (fPortBuffer[active_port]) {
  380. memcpy(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, net_buffer, fSubPeriodBytesSize - sizeof(int));
  381. }
  382. }
  383. void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
  384. {
  385. memcpy(net_buffer, fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(int));
  386. }
  387. #endif
  388. NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  389. : NetAudioBuffer(params, nports, net_buffer)
  390. {
  391. fPeriodSize = params->fPeriodSize;
  392. fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
  393. jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
  394. fIntBuffer = new short* [fNPorts];
  395. for (int port_index = 0; port_index < fNPorts; port_index++) {
  396. fIntBuffer[port_index] = new short[fPeriodSize];
  397. memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
  398. }
  399. int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  400. int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  401. jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1, res2);
  402. fNumPackets = (res1) ? (res2 + 1) : res2;
  403. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  404. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  405. fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
  406. jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  407. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  408. fCycleBytesSize = params->fMtu * fNumPackets;
  409. fLastSubCycle = -1;
  410. }
  411. NetIntAudioBuffer::~NetIntAudioBuffer()
  412. {
  413. for (int port_index = 0; port_index < fNPorts; port_index++) {
  414. delete [] fIntBuffer[port_index];
  415. }
  416. delete [] fIntBuffer;
  417. }
  418. size_t NetIntAudioBuffer::GetCycleSize()
  419. {
  420. return fCycleBytesSize;
  421. }
  422. float NetIntAudioBuffer::GetCycleDuration()
  423. {
  424. return fCycleDuration;
  425. }
  426. int NetIntAudioBuffer::GetNumPackets(int active_ports)
  427. {
  428. return fNumPackets;
  429. }
  430. int NetIntAudioBuffer::RenderFromJackPorts(int nframes)
  431. {
  432. for (int port_index = 0; port_index < fNPorts; port_index++) {
  433. if (fPortBuffer[port_index]) {
  434. for (int frame = 0; frame < nframes; frame++) {
  435. fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32767.f);
  436. }
  437. } else {
  438. memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
  439. }
  440. }
  441. // All ports active
  442. return fNPorts;
  443. }
  444. void NetIntAudioBuffer::RenderToJackPorts(int nframes)
  445. {
  446. float coef = 1.f / 32767.f;
  447. for (int port_index = 0; port_index < fNPorts; port_index++) {
  448. if (fPortBuffer[port_index]) {
  449. for (int frame = 0; frame < nframes; frame++) {
  450. fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
  451. }
  452. }
  453. }
  454. NextCycle();
  455. }
  456. //network<->buffer
  457. int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  458. {
  459. // Cleanup all JACK ports at the beginning of the cycle
  460. if (sub_cycle == 0) {
  461. Cleanup();
  462. }
  463. if (port_num > 0) {
  464. int sub_period_bytes_size;
  465. // Last packet
  466. if (sub_cycle == fNumPackets - 1) {
  467. sub_period_bytes_size = fLastSubPeriodBytesSize;
  468. } else {
  469. sub_period_bytes_size = fSubPeriodBytesSize;
  470. }
  471. for (int port_index = 0; port_index < fNPorts; port_index++) {
  472. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * sub_period_bytes_size, sub_period_bytes_size);
  473. }
  474. }
  475. return CheckPacket(cycle, sub_cycle);
  476. }
  477. int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  478. {
  479. int sub_period_bytes_size;
  480. // Last packet
  481. if (sub_cycle == fNumPackets - 1) {
  482. sub_period_bytes_size = fLastSubPeriodBytesSize;
  483. } else {
  484. sub_period_bytes_size = fSubPeriodBytesSize;
  485. }
  486. for (int port_index = 0; port_index < fNPorts; port_index++) {
  487. memcpy(fNetBuffer + port_index * sub_period_bytes_size, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, sub_period_bytes_size);
  488. }
  489. return fNPorts * sub_period_bytes_size;
  490. }
  491. // SessionParams ************************************************************************************
  492. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
  493. {
  494. memcpy(dst_params, src_params, sizeof(session_params_t));
  495. dst_params->fProtocolVersion = htonl(src_params->fProtocolVersion);
  496. dst_params->fPacketID = htonl(src_params->fPacketID);
  497. dst_params->fMtu = htonl(src_params->fMtu);
  498. dst_params->fID = htonl(src_params->fID);
  499. dst_params->fTransportSync = htonl(src_params->fTransportSync);
  500. dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
  501. dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
  502. dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
  503. dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
  504. dst_params->fSampleRate = htonl(src_params->fSampleRate);
  505. dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
  506. dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
  507. dst_params->fKBps = htonl(src_params->fKBps);
  508. dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
  509. dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
  510. }
  511. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
  512. {
  513. memcpy(dst_params, src_params, sizeof(session_params_t));
  514. dst_params->fProtocolVersion = ntohl(src_params->fProtocolVersion);
  515. dst_params->fPacketID = ntohl(src_params->fPacketID);
  516. dst_params->fMtu = ntohl(src_params->fMtu);
  517. dst_params->fID = ntohl(src_params->fID);
  518. dst_params->fTransportSync = ntohl(src_params->fTransportSync);
  519. dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
  520. dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
  521. dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
  522. dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
  523. dst_params->fSampleRate = ntohl(src_params->fSampleRate);
  524. dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
  525. dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
  526. dst_params->fKBps = ntohl(src_params->fKBps);
  527. dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
  528. dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
  529. }
  530. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
  531. {
  532. char encoder[16];
  533. switch (params->fSampleEncoder)
  534. {
  535. case JackFloatEncoder:
  536. strcpy(encoder, "float");
  537. break;
  538. case JackIntEncoder:
  539. strcpy(encoder, "integer");
  540. break;
  541. case JackCeltEncoder:
  542. strcpy(encoder, "CELT");
  543. break;
  544. case JackOpusEncoder:
  545. strcpy(encoder, "OPUS");
  546. break;
  547. }
  548. jack_info("**************** Network parameters ****************");
  549. jack_info("Name : %s", params->fName);
  550. jack_info("Protocol revision : %d", params->fProtocolVersion);
  551. jack_info("MTU : %u", params->fMtu);
  552. jack_info("Master name : %s", params->fMasterNetName);
  553. jack_info("Slave name : %s", params->fSlaveNetName);
  554. jack_info("ID : %u", params->fID);
  555. jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
  556. jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
  557. jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
  558. jack_info("Sample rate : %u frames per second", params->fSampleRate);
  559. jack_info("Period size : %u frames per period", params->fPeriodSize);
  560. jack_info("Network latency : %u cycles", params->fNetworkLatency);
  561. switch (params->fSampleEncoder) {
  562. case (JackFloatEncoder):
  563. jack_info("SampleEncoder : %s", "Float");
  564. break;
  565. case (JackIntEncoder):
  566. jack_info("SampleEncoder : %s", "16 bits integer");
  567. break;
  568. case (JackCeltEncoder):
  569. jack_info("SampleEncoder : %s", "CELT");
  570. jack_info("kBits : %d", params->fKBps);
  571. break;
  572. case (JackOpusEncoder):
  573. jack_info("SampleEncoder : %s", "OPUS");
  574. jack_info("kBits : %d", params->fKBps);
  575. break;
  576. };
  577. jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
  578. jack_info("****************************************************");
  579. }
  580. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
  581. {
  582. switch (params->fPacketID)
  583. {
  584. case 0:
  585. return SLAVE_AVAILABLE;
  586. case 1:
  587. return SLAVE_SETUP;
  588. case 2:
  589. return START_MASTER;
  590. case 3:
  591. return START_SLAVE;
  592. case 4:
  593. return KILL_MASTER;
  594. }
  595. return INVALID;
  596. }
  597. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
  598. {
  599. switch (packet_type)
  600. {
  601. case INVALID:
  602. return -1;
  603. case SLAVE_AVAILABLE:
  604. params->fPacketID = 0;
  605. break;
  606. case SLAVE_SETUP:
  607. params->fPacketID = 1;
  608. break;
  609. case START_MASTER:
  610. params->fPacketID = 2;
  611. break;
  612. case START_SLAVE:
  613. params->fPacketID = 3;
  614. break;
  615. case KILL_MASTER:
  616. params->fPacketID = 4;
  617. }
  618. return 0;
  619. }
  620. // Packet header **********************************************************************************
  621. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
  622. {
  623. memcpy(dst_header, src_header, sizeof(packet_header_t));
  624. dst_header->fDataType = htonl(src_header->fDataType);
  625. dst_header->fDataStream = htonl(src_header->fDataStream);
  626. dst_header->fID = htonl(src_header->fID);
  627. dst_header->fNumPacket = htonl(src_header->fNumPacket);
  628. dst_header->fPacketSize = htonl(src_header->fPacketSize);
  629. dst_header->fActivePorts = htonl(src_header->fActivePorts);
  630. dst_header->fCycle = htonl(src_header->fCycle);
  631. dst_header->fSubCycle = htonl(src_header->fSubCycle);
  632. dst_header->fFrames = htonl(src_header->fFrames);
  633. dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
  634. }
  635. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
  636. {
  637. memcpy(dst_header, src_header, sizeof(packet_header_t));
  638. dst_header->fDataType = ntohl(src_header->fDataType);
  639. dst_header->fDataStream = ntohl(src_header->fDataStream);
  640. dst_header->fID = ntohl(src_header->fID);
  641. dst_header->fNumPacket = ntohl(src_header->fNumPacket);
  642. dst_header->fPacketSize = ntohl(src_header->fPacketSize);
  643. dst_header->fActivePorts = ntohl(src_header->fActivePorts);
  644. dst_header->fCycle = ntohl(src_header->fCycle);
  645. dst_header->fSubCycle = ntohl(src_header->fSubCycle);
  646. dst_header->fFrames = ntohl(src_header->fFrames);
  647. dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
  648. }
  649. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
  650. {
  651. jack_info("********************Header********************");
  652. jack_info("Data type : %c", header->fDataType);
  653. jack_info("Data stream : %c", header->fDataStream);
  654. jack_info("ID : %u", header->fID);
  655. jack_info("Cycle : %u", header->fCycle);
  656. jack_info("SubCycle : %u", header->fSubCycle);
  657. jack_info("Active ports : %u", header->fActivePorts);
  658. jack_info("DATA packets : %u", header->fNumPacket);
  659. jack_info("DATA size : %u", header->fPacketSize);
  660. jack_info("DATA frames : %d", header->fFrames);
  661. jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
  662. jack_info("**********************************************");
  663. }
  664. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
  665. {
  666. jack_info("********************Network Transport********************");
  667. jack_info("Transport new state : %u", data->fNewState);
  668. jack_info("Transport timebase master : %u", data->fTimebaseMaster);
  669. jack_info("Transport cycle state : %u", data->fState);
  670. jack_info("**********************************************");
  671. }
  672. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  673. {
  674. dst_buffer->magic = htonl(src_buffer->magic);
  675. dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
  676. dst_buffer->nframes = htonl(src_buffer->nframes);
  677. dst_buffer->write_pos = htonl(src_buffer->write_pos);
  678. dst_buffer->event_count = htonl(src_buffer->event_count);
  679. dst_buffer->lost_events = htonl(src_buffer->lost_events);
  680. }
  681. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  682. {
  683. dst_buffer->magic = ntohl(src_buffer->magic);
  684. dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
  685. dst_buffer->nframes = ntohl(src_buffer->nframes);
  686. dst_buffer->write_pos = ntohl(src_buffer->write_pos);
  687. dst_buffer->event_count = ntohl(src_buffer->event_count);
  688. dst_buffer->lost_events = ntohl(src_buffer->lost_events);
  689. }
  690. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  691. {
  692. dst_params->fNewState = htonl(src_params->fNewState);
  693. dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
  694. dst_params->fState = htonl(src_params->fState);
  695. dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
  696. dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
  697. dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
  698. dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
  699. dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
  700. dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
  701. dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
  702. dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
  703. dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
  704. dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
  705. dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
  706. dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
  707. dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
  708. dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
  709. dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
  710. dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
  711. dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  712. dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
  713. dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
  714. }
  715. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  716. {
  717. dst_params->fNewState = ntohl(src_params->fNewState);
  718. dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
  719. dst_params->fState = ntohl(src_params->fState);
  720. dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
  721. dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
  722. dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
  723. dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
  724. dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
  725. dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
  726. dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
  727. dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
  728. dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
  729. dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
  730. dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
  731. dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
  732. dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
  733. dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
  734. dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
  735. dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
  736. dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  737. dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
  738. dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
  739. }
  740. // Utility *******************************************************************************************************
  741. SERVER_EXPORT int SocketAPIInit()
  742. {
  743. #ifdef WIN32
  744. WORD wVersionRequested = MAKEWORD(2, 2);
  745. WSADATA wsaData;
  746. if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  747. jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
  748. return -1;
  749. }
  750. if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
  751. jack_error("Could not find a useable version of Winsock.dll\n");
  752. WSACleanup();
  753. return -1;
  754. }
  755. #endif
  756. return 0;
  757. }
  758. SERVER_EXPORT int SocketAPIEnd()
  759. {
  760. #ifdef WIN32
  761. return WSACleanup();
  762. #endif
  763. return 0;
  764. }
  765. SERVER_EXPORT const char* GetTransportState(int transport_state)
  766. {
  767. switch (transport_state)
  768. {
  769. case JackTransportRolling:
  770. return "rolling";
  771. case JackTransportStarting:
  772. return "starting";
  773. case JackTransportStopped:
  774. return "stopped";
  775. case JackTransportNetStarting:
  776. return "netstarting";
  777. }
  778. return NULL;
  779. }
  780. }