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.

1374 lines
51KB

  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. fPortBuffer = new sample_t*[fNPorts];
  170. fConnectedPorts = new bool[fNPorts];
  171. for (int port_index = 0; port_index < fNPorts; port_index++) {
  172. fPortBuffer[port_index] = NULL;
  173. fConnectedPorts[port_index] = true;
  174. }
  175. fLastSubCycle = 0;
  176. fPeriodSize = 0;
  177. fSubPeriodSize = 0;
  178. fSubPeriodBytesSize = 0;
  179. fCycleDuration = 0.f;
  180. fCycleBytesSize = 0;
  181. }
  182. NetAudioBuffer::~NetAudioBuffer()
  183. {
  184. delete [] fConnectedPorts;
  185. delete [] fPortBuffer;
  186. }
  187. void NetAudioBuffer::SetBuffer(int index, sample_t* buffer)
  188. {
  189. fPortBuffer[index] = buffer;
  190. }
  191. sample_t* NetAudioBuffer::GetBuffer(int index)
  192. {
  193. return fPortBuffer[index];
  194. }
  195. int NetAudioBuffer::CheckPacket(int cycle, int sub_cycle)
  196. {
  197. int res;
  198. if (sub_cycle != fLastSubCycle + 1) {
  199. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  200. res = NET_PACKET_ERROR;
  201. } else {
  202. res = 0;
  203. }
  204. fLastSubCycle = sub_cycle;
  205. return res;
  206. }
  207. void NetAudioBuffer::NextCycle()
  208. {
  209. // reset for next cycle
  210. fLastSubCycle = -1;
  211. }
  212. void NetAudioBuffer::Cleanup()
  213. {
  214. for (int port_index = 0; port_index < fNPorts; port_index++) {
  215. if (fPortBuffer[port_index]) {
  216. memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
  217. }
  218. }
  219. }
  220. //network<->buffer
  221. int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer)
  222. {
  223. int active_ports = 0;
  224. int* active_port_address = (int*)net_buffer;
  225. for (int port_index = 0; port_index < fNPorts; port_index++) {
  226. // Write the active port number
  227. if (fPortBuffer[port_index]) {
  228. *active_port_address = htonl(port_index);
  229. active_port_address++;
  230. active_ports++;
  231. assert(active_ports < 256);
  232. }
  233. }
  234. return active_ports;
  235. }
  236. void NetAudioBuffer::ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
  237. {
  238. int* active_port_address = (int*)net_buffer;
  239. for (int port_index = 0; port_index < fNPorts; port_index++) {
  240. fConnectedPorts[port_index] = false;
  241. }
  242. for (uint port_index = 0; port_index < port_num; port_index++) {
  243. int active_port = ntohl(*active_port_address);
  244. fConnectedPorts[active_port] = true;
  245. active_port_address++;
  246. }
  247. }
  248. int NetAudioBuffer::RenderFromJackPorts()
  249. {
  250. // Count active ports
  251. int active_ports = 0;
  252. for (int port_index = 0; port_index < fNPorts; port_index++) {
  253. if (fPortBuffer[port_index]) {
  254. active_ports++;
  255. }
  256. }
  257. //jack_info("active_ports %d", active_ports);
  258. return active_ports;
  259. }
  260. void NetAudioBuffer::RenderToJackPorts()
  261. {
  262. // Nothing to do
  263. NextCycle();
  264. }
  265. // Float converter
  266. NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  267. : NetAudioBuffer(params, nports, net_buffer)
  268. {
  269. fPeriodSize = params->fPeriodSize;
  270. fPacketSize = PACKET_AVAILABLE_SIZE(params);
  271. UpdateParams(max(params->fReturnAudioChannels, params->fSendAudioChannels));
  272. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
  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. }
  299. int NetFloatAudioBuffer::GetNumPackets(int active_ports)
  300. {
  301. UpdateParams(active_ports);
  302. /*
  303. jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
  304. fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
  305. */
  306. return fPeriodSize / fSubPeriodSize; // At least one packet
  307. }
  308. //jack<->buffer
  309. int NetFloatAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  310. {
  311. // Cleanup all JACK ports at the beginning of the cycle
  312. if (sub_cycle == 0) {
  313. Cleanup();
  314. }
  315. if (port_num > 0) {
  316. UpdateParams(port_num);
  317. for (uint32_t port_index = 0; port_index < port_num; port_index++) {
  318. // Only copy to active ports : read the active port number then audio data
  319. int* active_port_address = (int*)(fNetBuffer + port_index * fSubPeriodBytesSize);
  320. int active_port = ntohl(*active_port_address);
  321. RenderFromNetwork((char*)(active_port_address + 1), active_port, sub_cycle);
  322. }
  323. }
  324. return CheckPacket(cycle, sub_cycle);
  325. }
  326. int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  327. {
  328. int active_ports = 0;
  329. for (int port_index = 0; port_index < fNPorts; port_index++) {
  330. // Only copy from active ports : write the active port number then audio data
  331. if (fPortBuffer[port_index]) {
  332. int* active_port_address = (int*)(fNetBuffer + active_ports * fSubPeriodBytesSize);
  333. *active_port_address = htonl(port_index);
  334. RenderToNetwork((char*)(active_port_address + 1), port_index, sub_cycle);
  335. active_ports++;
  336. }
  337. }
  338. return port_num * fSubPeriodBytesSize;
  339. }
  340. #ifdef __BIG_ENDIAN__
  341. static inline jack_default_audio_sample_t SwapFloat(jack_default_audio_sample_t f)
  342. {
  343. union
  344. {
  345. jack_default_audio_sample_t f;
  346. unsigned char b[4];
  347. } dat1, dat2;
  348. dat1.f = f;
  349. dat2.b[0] = dat1.b[3];
  350. dat2.b[1] = dat1.b[2];
  351. dat2.b[2] = dat1.b[1];
  352. dat2.b[3] = dat1.b[0];
  353. return dat2.f;
  354. }
  355. void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
  356. {
  357. if (fPortBuffer[active_port]) {
  358. jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(net_buffer);
  359. jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
  360. for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
  361. dst[sample] = SwapFloat(src[sample]);
  362. }
  363. }
  364. }
  365. void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
  366. {
  367. for (int port_index = 0; port_index < fNPorts; port_index++ ) {
  368. jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
  369. jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(net_buffer);
  370. for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
  371. dst[sample] = SwapFloat(src[sample]);
  372. }
  373. }
  374. }
  375. #else
  376. void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
  377. {
  378. if (fPortBuffer[active_port]) {
  379. memcpy(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, net_buffer, fSubPeriodBytesSize - sizeof(int));
  380. }
  381. }
  382. void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
  383. {
  384. memcpy(net_buffer, fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(int));
  385. }
  386. #endif
  387. // Celt audio buffer *********************************************************************************
  388. #if HAVE_CELT
  389. #define KPS 32
  390. #define KPS_DIV 8
  391. NetCeltAudioBuffer::NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps)
  392. :NetAudioBuffer(params, nports, net_buffer)
  393. {
  394. fCeltMode = new CELTMode *[fNPorts];
  395. fCeltEncoder = new CELTEncoder *[fNPorts];
  396. fCeltDecoder = new CELTDecoder *[fNPorts];
  397. memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
  398. memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
  399. memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
  400. int error = CELT_OK;
  401. for (int i = 0; i < fNPorts; i++) {
  402. fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
  403. if (error != CELT_OK) {
  404. goto error;
  405. }
  406. #if HAVE_CELT_API_0_11
  407. fCeltEncoder[i] = celt_encoder_create_custom(fCeltMode[i], 1, &error);
  408. if (error != CELT_OK) {
  409. goto error;
  410. }
  411. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  412. fCeltDecoder[i] = celt_decoder_create_custom(fCeltMode[i], 1, &error);
  413. if (error != CELT_OK) {
  414. goto error;
  415. }
  416. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  417. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  418. fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
  419. if (error != CELT_OK) {
  420. goto error;
  421. }
  422. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  423. fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
  424. if (error != CELT_OK) {
  425. goto error;
  426. }
  427. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  428. #else
  429. fCeltEncoder[i] = celt_encoder_create(fCeltMode[i]);
  430. if (error != CELT_OK) {
  431. goto error;
  432. }
  433. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  434. fCeltDecoder[i] = celt_decoder_create(fCeltMode[i]);
  435. if (error != CELT_OK) {
  436. goto error;
  437. }
  438. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  439. #endif
  440. }
  441. {
  442. fPeriodSize = params->fPeriodSize;
  443. fCompressedSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
  444. jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
  445. fCompressedBuffer = new unsigned char* [fNPorts];
  446. for (int port_index = 0; port_index < fNPorts; port_index++) {
  447. fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
  448. memset(fCompressedBuffer[port_index], 0, fCompressedSizeByte * sizeof(char));
  449. }
  450. int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  451. int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  452. fNumPackets = (res1) ? (res2 + 1) : res2;
  453. jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
  454. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  455. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  456. jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  457. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  458. fCycleBytesSize = params->fMtu * fNumPackets;
  459. fLastSubCycle = -1;
  460. return;
  461. }
  462. error:
  463. FreeCelt();
  464. throw std::bad_alloc();
  465. }
  466. NetCeltAudioBuffer::~NetCeltAudioBuffer()
  467. {
  468. FreeCelt();
  469. for (int port_index = 0; port_index < fNPorts; port_index++) {
  470. delete [] fCompressedBuffer[port_index];
  471. }
  472. delete [] fCompressedBuffer;
  473. }
  474. void NetCeltAudioBuffer::FreeCelt()
  475. {
  476. for (int i = 0; i < fNPorts; i++) {
  477. if (fCeltEncoder[i]) {
  478. celt_encoder_destroy(fCeltEncoder[i]);
  479. }
  480. if (fCeltDecoder[i]) {
  481. celt_decoder_destroy(fCeltDecoder[i]);
  482. }
  483. if (fCeltMode[i]) {
  484. celt_mode_destroy(fCeltMode[i]);
  485. }
  486. }
  487. delete [] fCeltMode;
  488. delete [] fCeltEncoder;
  489. delete [] fCeltDecoder;
  490. }
  491. size_t NetCeltAudioBuffer::GetCycleSize()
  492. {
  493. return fCycleBytesSize;
  494. }
  495. float NetCeltAudioBuffer::GetCycleDuration()
  496. {
  497. return fCycleDuration;
  498. }
  499. int NetCeltAudioBuffer::GetNumPackets(int active_ports)
  500. {
  501. return fNumPackets;
  502. }
  503. int NetCeltAudioBuffer::RenderFromJackPorts()
  504. {
  505. float buffer[BUFFER_SIZE_MAX];
  506. for (int port_index = 0; port_index < fNPorts; port_index++) {
  507. if (fPortBuffer[port_index]) {
  508. memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
  509. } else {
  510. memset(buffer, 0, fPeriodSize * sizeof(sample_t));
  511. }
  512. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  513. int res = celt_encode_float(fCeltEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
  514. #else
  515. int res = celt_encode_float(fCeltEncoder[port_index], buffer, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
  516. #endif
  517. if (res != fCompressedSizeByte) {
  518. jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  519. }
  520. }
  521. // All ports active
  522. return fNPorts;
  523. }
  524. void NetCeltAudioBuffer::RenderToJackPorts()
  525. {
  526. for (int port_index = 0; port_index < fNPorts; port_index++) {
  527. if (fPortBuffer[port_index]) {
  528. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  529. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize);
  530. #else
  531. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
  532. #endif
  533. if (res != CELT_OK) {
  534. jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  535. }
  536. }
  537. }
  538. NextCycle();
  539. }
  540. //network<->buffer
  541. int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  542. {
  543. // Cleanup all JACK ports at the beginning of the cycle
  544. if (sub_cycle == 0) {
  545. Cleanup();
  546. }
  547. if (port_num > 0) {
  548. // Last packet of the cycle
  549. if (sub_cycle == fNumPackets - 1) {
  550. for (int port_index = 0; port_index < fNPorts; port_index++) {
  551. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  552. }
  553. } else {
  554. for (int port_index = 0; port_index < fNPorts; port_index++) {
  555. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  556. }
  557. }
  558. }
  559. return CheckPacket(cycle, sub_cycle);
  560. }
  561. int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  562. {
  563. // Last packet of the cycle
  564. if (sub_cycle == fNumPackets - 1) {
  565. for (int port_index = 0; port_index < fNPorts; port_index++) {
  566. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  567. }
  568. return fNPorts * fLastSubPeriodBytesSize;
  569. } else {
  570. for (int port_index = 0; port_index < fNPorts; port_index++) {
  571. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
  572. }
  573. return fNPorts * fSubPeriodBytesSize;
  574. }
  575. }
  576. #endif
  577. #if HAVE_OPUS
  578. #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
  579. NetOpusAudioBuffer::NetOpusAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps)
  580. :NetAudioBuffer(params, nports, net_buffer)
  581. {
  582. fOpusMode = new OpusCustomMode *[fNPorts];
  583. fOpusEncoder = new OpusCustomEncoder *[fNPorts];
  584. fOpusDecoder = new OpusCustomDecoder *[fNPorts];
  585. fCompressedSizesByte = new unsigned short [fNPorts];
  586. memset(fOpusMode, 0, fNPorts * sizeof(OpusCustomMode*));
  587. memset(fOpusEncoder, 0, fNPorts * sizeof(OpusCustomEncoder*));
  588. memset(fOpusDecoder, 0, fNPorts * sizeof(OpusCustomDecoder*));
  589. memset(fCompressedSizesByte, 0, fNPorts * sizeof(int));
  590. int error = OPUS_OK;
  591. for (int i = 0; i < fNPorts; i++) {
  592. /* Allocate en/decoders */
  593. fOpusMode[i] = opus_custom_mode_create(
  594. params->fSampleRate, params->fPeriodSize, &error);
  595. if (error != OPUS_OK) {
  596. goto error;
  597. }
  598. fOpusEncoder[i] = opus_custom_encoder_create(fOpusMode[i], 1,&error);
  599. if (error != OPUS_OK) {
  600. goto error;
  601. }
  602. fOpusDecoder[i] = opus_custom_decoder_create(fOpusMode[i], 1, &error);
  603. if (error != OPUS_OK) {
  604. goto error;
  605. }
  606. opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_BITRATE(kbps*1024)); // bits per second
  607. opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_COMPLEXITY(10));
  608. opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));
  609. opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_SIGNAL(OPUS_APPLICATION_RESTRICTED_LOWDELAY));
  610. }
  611. {
  612. fCompressedMaxSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
  613. fPeriodSize = params->fPeriodSize;
  614. jack_log("NetOpusAudioBuffer fCompressedMaxSizeByte %d", fCompressedMaxSizeByte);
  615. fCompressedBuffer = new unsigned char* [fNPorts];
  616. for (int port_index = 0; port_index < fNPorts; port_index++) {
  617. fCompressedBuffer[port_index] = new unsigned char[fCompressedMaxSizeByte];
  618. memset(fCompressedBuffer[port_index], 0, fCompressedMaxSizeByte * sizeof(char));
  619. }
  620. int res1 = (fNPorts * fCompressedMaxSizeByte + CDO) % PACKET_AVAILABLE_SIZE(params);
  621. int res2 = (fNPorts * fCompressedMaxSizeByte + CDO) / PACKET_AVAILABLE_SIZE(params);
  622. fNumPackets = (res1) ? (res2 + 1) : res2;
  623. jack_log("NetOpusAudioBuffer res1 = %d res2 = %d", res1, res2);
  624. fSubPeriodBytesSize = (fCompressedMaxSizeByte + CDO) / fNumPackets;
  625. fLastSubPeriodBytesSize = fSubPeriodBytesSize + (fCompressedMaxSizeByte + CDO) % fNumPackets;
  626. if (fNumPackets == 1) {
  627. fSubPeriodBytesSize = fLastSubPeriodBytesSize;
  628. }
  629. jack_log("NetOpusAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  630. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  631. fCycleBytesSize = params->fMtu * fNumPackets;
  632. fLastSubCycle = -1;
  633. return;
  634. }
  635. error:
  636. FreeOpus();
  637. throw std::bad_alloc();
  638. }
  639. NetOpusAudioBuffer::~NetOpusAudioBuffer()
  640. {
  641. FreeOpus();
  642. for (int port_index = 0; port_index < fNPorts; port_index++) {
  643. delete [] fCompressedBuffer[port_index];
  644. }
  645. delete [] fCompressedBuffer;
  646. delete [] fCompressedSizesByte;
  647. }
  648. void NetOpusAudioBuffer::FreeOpus()
  649. {
  650. for (int i = 0; i < fNPorts; i++) {
  651. if (fOpusEncoder[i]) {
  652. opus_custom_encoder_destroy(fOpusEncoder[i]);
  653. fOpusEncoder[i]=0;
  654. }
  655. if (fOpusDecoder[i]) {
  656. opus_custom_decoder_destroy(fOpusDecoder[i]);
  657. fOpusDecoder[i]=0;
  658. }
  659. if (fOpusMode[i]) {
  660. opus_custom_mode_destroy(fOpusMode[i]);
  661. fOpusMode[i]=0;
  662. }
  663. }
  664. delete [] fOpusEncoder;
  665. delete [] fOpusDecoder;
  666. delete [] fOpusMode;
  667. }
  668. size_t NetOpusAudioBuffer::GetCycleSize()
  669. {
  670. return fCycleBytesSize;
  671. }
  672. float NetOpusAudioBuffer::GetCycleDuration()
  673. {
  674. return fCycleDuration;
  675. }
  676. int NetOpusAudioBuffer::GetNumPackets(int active_ports)
  677. {
  678. return fNumPackets;
  679. }
  680. int NetOpusAudioBuffer::RenderFromJackPorts()
  681. {
  682. float buffer[BUFFER_SIZE_MAX];
  683. for (int port_index = 0; port_index < fNPorts; port_index++) {
  684. if (fPortBuffer[port_index]) {
  685. memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
  686. } else {
  687. memset(buffer, 0, fPeriodSize * sizeof(sample_t));
  688. }
  689. int res = opus_custom_encode_float(fOpusEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedMaxSizeByte);
  690. if (res <0 || res >= 65535) {
  691. fCompressedSizesByte[port_index] = 0;
  692. } else {
  693. fCompressedSizesByte[port_index] = res;
  694. }
  695. }
  696. // All ports active
  697. return fNPorts;
  698. }
  699. void NetOpusAudioBuffer::RenderToJackPorts()
  700. {
  701. for (int port_index = 0; port_index < fNPorts; port_index++) {
  702. if (fPortBuffer[port_index]) {
  703. int res = opus_custom_decode_float(fOpusDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizesByte[port_index], fPortBuffer[port_index], fPeriodSize);
  704. if (res < 0 || res != fPeriodSize) {
  705. jack_error("opus_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizesByte[port_index], res);
  706. }
  707. }
  708. }
  709. NextCycle();
  710. }
  711. //network<->buffer
  712. int NetOpusAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  713. {
  714. // Cleanup all JACK ports at the beginning of the cycle
  715. if (sub_cycle == 0) {
  716. Cleanup();
  717. }
  718. if (port_num > 0) {
  719. if (sub_cycle == 0) {
  720. for (int port_index = 0; port_index < fNPorts; port_index++) {
  721. size_t len = *((size_t*)(fNetBuffer + port_index * fSubPeriodBytesSize));
  722. fCompressedSizesByte[port_index] = ntohs(len);
  723. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + CDO + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize - CDO);
  724. }
  725. } else if (sub_cycle == fNumPackets - 1) {
  726. for (int port_index = 0; port_index < fNPorts; port_index++) {
  727. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  728. }
  729. } else {
  730. for (int port_index = 0; port_index < fNPorts; port_index++) {
  731. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  732. }
  733. }
  734. }
  735. return CheckPacket(cycle, sub_cycle);
  736. }
  737. int NetOpusAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  738. {
  739. if (sub_cycle == 0) {
  740. for (int port_index = 0; port_index < fNPorts; port_index++) {
  741. unsigned short len = htons(fCompressedSizesByte[port_index]);
  742. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, &len, CDO);
  743. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize + CDO, fCompressedBuffer[port_index], fSubPeriodBytesSize - CDO);
  744. }
  745. return fNPorts * fSubPeriodBytesSize;
  746. } else if (sub_cycle == fNumPackets - 1) {
  747. for (int port_index = 0; port_index < fNPorts; port_index++) {
  748. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fLastSubPeriodBytesSize);
  749. }
  750. return fNPorts * fLastSubPeriodBytesSize;
  751. } else {
  752. for (int port_index = 0; port_index < fNPorts; port_index++) {
  753. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fSubPeriodBytesSize);
  754. }
  755. return fNPorts * fSubPeriodBytesSize;
  756. }
  757. }
  758. #endif
  759. NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  760. : NetAudioBuffer(params, nports, net_buffer)
  761. {
  762. fPeriodSize = params->fPeriodSize;
  763. fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
  764. jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
  765. fIntBuffer = new short* [fNPorts];
  766. for (int port_index = 0; port_index < fNPorts; port_index++) {
  767. fIntBuffer[port_index] = new short[fPeriodSize];
  768. memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
  769. }
  770. int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  771. int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  772. jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1, res2);
  773. fNumPackets = (res1) ? (res2 + 1) : res2;
  774. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  775. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  776. fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
  777. jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  778. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  779. fCycleBytesSize = params->fMtu * fNumPackets;
  780. fLastSubCycle = -1;
  781. return;
  782. }
  783. NetIntAudioBuffer::~NetIntAudioBuffer()
  784. {
  785. for (int port_index = 0; port_index < fNPorts; port_index++) {
  786. delete [] fIntBuffer[port_index];
  787. }
  788. delete [] fIntBuffer;
  789. }
  790. size_t NetIntAudioBuffer::GetCycleSize()
  791. {
  792. return fCycleBytesSize;
  793. }
  794. float NetIntAudioBuffer::GetCycleDuration()
  795. {
  796. return fCycleDuration;
  797. }
  798. int NetIntAudioBuffer::GetNumPackets(int active_ports)
  799. {
  800. return fNumPackets;
  801. }
  802. int NetIntAudioBuffer::RenderFromJackPorts()
  803. {
  804. for (int port_index = 0; port_index < fNPorts; port_index++) {
  805. if (fPortBuffer[port_index]) {
  806. for (uint frame = 0; frame < fPeriodSize; frame++) {
  807. fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
  808. }
  809. }
  810. }
  811. // All ports active
  812. return fNPorts;
  813. }
  814. void NetIntAudioBuffer::RenderToJackPorts()
  815. {
  816. float coef = 1.f / 32768.f;
  817. for (int port_index = 0; port_index < fNPorts; port_index++) {
  818. if (fPortBuffer[port_index]) {
  819. for (uint frame = 0; frame < fPeriodSize; frame++) {
  820. fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
  821. }
  822. }
  823. }
  824. NextCycle();
  825. }
  826. //network<->buffer
  827. int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  828. {
  829. // Cleanup all JACK ports at the beginning of the cycle
  830. if (sub_cycle == 0) {
  831. Cleanup();
  832. }
  833. if (port_num > 0) {
  834. if (sub_cycle == fNumPackets - 1) {
  835. for (int port_index = 0; port_index < fNPorts; port_index++) {
  836. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  837. }
  838. } else {
  839. for (int port_index = 0; port_index < fNPorts; port_index++) {
  840. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  841. }
  842. }
  843. }
  844. return CheckPacket(cycle, sub_cycle);
  845. }
  846. int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  847. {
  848. // Last packet of the cycle
  849. if (sub_cycle == fNumPackets - 1) {
  850. for (int port_index = 0; port_index < fNPorts; port_index++) {
  851. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fLastSubPeriodBytesSize);
  852. }
  853. return fNPorts * fLastSubPeriodBytesSize;
  854. } else {
  855. for (int port_index = 0; port_index < fNPorts; port_index++) {
  856. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
  857. }
  858. return fNPorts * fSubPeriodBytesSize;
  859. }
  860. }
  861. // SessionParams ************************************************************************************
  862. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
  863. {
  864. memcpy(dst_params, src_params, sizeof(session_params_t));
  865. dst_params->fProtocolVersion = htonl(src_params->fProtocolVersion);
  866. dst_params->fPacketID = htonl(src_params->fPacketID);
  867. dst_params->fMtu = htonl(src_params->fMtu);
  868. dst_params->fID = htonl(src_params->fID);
  869. dst_params->fTransportSync = htonl(src_params->fTransportSync);
  870. dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
  871. dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
  872. dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
  873. dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
  874. dst_params->fSampleRate = htonl(src_params->fSampleRate);
  875. dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
  876. dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
  877. dst_params->fKBps = htonl(src_params->fKBps);
  878. dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
  879. dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
  880. }
  881. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
  882. {
  883. memcpy(dst_params, src_params, sizeof(session_params_t));
  884. dst_params->fProtocolVersion = ntohl(src_params->fProtocolVersion);
  885. dst_params->fPacketID = ntohl(src_params->fPacketID);
  886. dst_params->fMtu = ntohl(src_params->fMtu);
  887. dst_params->fID = ntohl(src_params->fID);
  888. dst_params->fTransportSync = ntohl(src_params->fTransportSync);
  889. dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
  890. dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
  891. dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
  892. dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
  893. dst_params->fSampleRate = ntohl(src_params->fSampleRate);
  894. dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
  895. dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
  896. dst_params->fKBps = ntohl(src_params->fKBps);
  897. dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
  898. dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
  899. }
  900. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
  901. {
  902. char encoder[16];
  903. switch (params->fSampleEncoder)
  904. {
  905. case JackFloatEncoder:
  906. strcpy(encoder, "float");
  907. break;
  908. case JackIntEncoder:
  909. strcpy(encoder, "integer");
  910. break;
  911. case JackCeltEncoder:
  912. strcpy(encoder, "CELT");
  913. break;
  914. case JackOpusEncoder:
  915. strcpy(encoder, "OPUS");
  916. break;
  917. }
  918. jack_info("**************** Network parameters ****************");
  919. jack_info("Name : %s", params->fName);
  920. jack_info("Protocol revision : %d", params->fProtocolVersion);
  921. jack_info("MTU : %u", params->fMtu);
  922. jack_info("Master name : %s", params->fMasterNetName);
  923. jack_info("Slave name : %s", params->fSlaveNetName);
  924. jack_info("ID : %u", params->fID);
  925. jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
  926. jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
  927. jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
  928. jack_info("Sample rate : %u frames per second", params->fSampleRate);
  929. jack_info("Period size : %u frames per period", params->fPeriodSize);
  930. jack_info("Network latency : %u cycles", params->fNetworkLatency);
  931. switch (params->fSampleEncoder) {
  932. case (JackFloatEncoder):
  933. jack_info("SampleEncoder : %s", "Float");
  934. break;
  935. case (JackIntEncoder):
  936. jack_info("SampleEncoder : %s", "16 bits integer");
  937. break;
  938. case (JackCeltEncoder):
  939. jack_info("SampleEncoder : %s", "CELT");
  940. jack_info("kBits : %d", params->fKBps);
  941. break;
  942. case (JackOpusEncoder):
  943. jack_info("SampleEncoder : %s", "OPUS");
  944. jack_info("kBits : %d", params->fKBps);
  945. break;
  946. };
  947. jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
  948. jack_info("****************************************************");
  949. }
  950. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
  951. {
  952. switch (params->fPacketID)
  953. {
  954. case 0:
  955. return SLAVE_AVAILABLE;
  956. case 1:
  957. return SLAVE_SETUP;
  958. case 2:
  959. return START_MASTER;
  960. case 3:
  961. return START_SLAVE;
  962. case 4:
  963. return KILL_MASTER;
  964. }
  965. return INVALID;
  966. }
  967. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
  968. {
  969. switch (packet_type)
  970. {
  971. case INVALID:
  972. return -1;
  973. case SLAVE_AVAILABLE:
  974. params->fPacketID = 0;
  975. break;
  976. case SLAVE_SETUP:
  977. params->fPacketID = 1;
  978. break;
  979. case START_MASTER:
  980. params->fPacketID = 2;
  981. break;
  982. case START_SLAVE:
  983. params->fPacketID = 3;
  984. break;
  985. case KILL_MASTER:
  986. params->fPacketID = 4;
  987. }
  988. return 0;
  989. }
  990. // Packet header **********************************************************************************
  991. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
  992. {
  993. memcpy(dst_header, src_header, sizeof(packet_header_t));
  994. dst_header->fDataType = htonl(src_header->fDataType);
  995. dst_header->fDataStream = htonl(src_header->fDataStream);
  996. dst_header->fID = htonl(src_header->fID);
  997. dst_header->fNumPacket = htonl(src_header->fNumPacket);
  998. dst_header->fPacketSize = htonl(src_header->fPacketSize);
  999. dst_header->fActivePorts = htonl(src_header->fActivePorts);
  1000. dst_header->fCycle = htonl(src_header->fCycle);
  1001. dst_header->fSubCycle = htonl(src_header->fSubCycle);
  1002. dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
  1003. }
  1004. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
  1005. {
  1006. memcpy(dst_header, src_header, sizeof(packet_header_t));
  1007. dst_header->fDataType = ntohl(src_header->fDataType);
  1008. dst_header->fDataStream = ntohl(src_header->fDataStream);
  1009. dst_header->fID = ntohl(src_header->fID);
  1010. dst_header->fNumPacket = ntohl(src_header->fNumPacket);
  1011. dst_header->fPacketSize = ntohl(src_header->fPacketSize);
  1012. dst_header->fActivePorts = ntohl(src_header->fActivePorts);
  1013. dst_header->fCycle = ntohl(src_header->fCycle);
  1014. dst_header->fSubCycle = ntohl(src_header->fSubCycle);
  1015. dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
  1016. }
  1017. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
  1018. {
  1019. char bitdepth[16];
  1020. jack_info("********************Header********************");
  1021. jack_info("Data type : %c", header->fDataType);
  1022. jack_info("Data stream : %c", header->fDataStream);
  1023. jack_info("ID : %u", header->fID);
  1024. jack_info("Cycle : %u", header->fCycle);
  1025. jack_info("SubCycle : %u", header->fSubCycle);
  1026. jack_info("Active ports : %u", header->fActivePorts);
  1027. jack_info("DATA packets : %u", header->fNumPacket);
  1028. jack_info("DATA size : %u", header->fPacketSize);
  1029. jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
  1030. jack_info("Bitdepth : %s", bitdepth);
  1031. jack_info("**********************************************");
  1032. }
  1033. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
  1034. {
  1035. jack_info("********************Network Transport********************");
  1036. jack_info("Transport new state : %u", data->fNewState);
  1037. jack_info("Transport timebase master : %u", data->fTimebaseMaster);
  1038. jack_info("Transport cycle state : %u", data->fState);
  1039. jack_info("**********************************************");
  1040. }
  1041. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  1042. {
  1043. dst_buffer->magic = htonl(src_buffer->magic);
  1044. dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
  1045. dst_buffer->nframes = htonl(src_buffer->nframes);
  1046. dst_buffer->write_pos = htonl(src_buffer->write_pos);
  1047. dst_buffer->event_count = htonl(src_buffer->event_count);
  1048. dst_buffer->lost_events = htonl(src_buffer->lost_events);
  1049. dst_buffer->mix_index = htonl(src_buffer->mix_index);
  1050. }
  1051. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  1052. {
  1053. dst_buffer->magic = ntohl(src_buffer->magic);
  1054. dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
  1055. dst_buffer->nframes = ntohl(src_buffer->nframes);
  1056. dst_buffer->write_pos = ntohl(src_buffer->write_pos);
  1057. dst_buffer->event_count = ntohl(src_buffer->event_count);
  1058. dst_buffer->lost_events = ntohl(src_buffer->lost_events);
  1059. dst_buffer->mix_index = ntohl(src_buffer->mix_index);
  1060. }
  1061. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  1062. {
  1063. dst_params->fNewState = htonl(src_params->fNewState);
  1064. dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
  1065. dst_params->fState = htonl(src_params->fState);
  1066. dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
  1067. dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
  1068. dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
  1069. dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
  1070. dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
  1071. dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
  1072. dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
  1073. dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
  1074. dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
  1075. dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
  1076. dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
  1077. dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
  1078. dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
  1079. dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
  1080. dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
  1081. dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
  1082. dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  1083. dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
  1084. dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
  1085. }
  1086. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  1087. {
  1088. dst_params->fNewState = ntohl(src_params->fNewState);
  1089. dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
  1090. dst_params->fState = ntohl(src_params->fState);
  1091. dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
  1092. dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
  1093. dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
  1094. dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
  1095. dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
  1096. dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
  1097. dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
  1098. dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
  1099. dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
  1100. dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
  1101. dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
  1102. dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
  1103. dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
  1104. dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
  1105. dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
  1106. dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
  1107. dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  1108. dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
  1109. dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
  1110. }
  1111. // Utility *******************************************************************************************************
  1112. SERVER_EXPORT int SocketAPIInit()
  1113. {
  1114. #ifdef WIN32
  1115. WORD wVersionRequested = MAKEWORD(2, 2);
  1116. WSADATA wsaData;
  1117. if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  1118. jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
  1119. return -1;
  1120. }
  1121. if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
  1122. jack_error("Could not find a useable version of Winsock.dll\n");
  1123. WSACleanup();
  1124. return -1;
  1125. }
  1126. #endif
  1127. return 0;
  1128. }
  1129. SERVER_EXPORT int SocketAPIEnd()
  1130. {
  1131. #ifdef WIN32
  1132. return WSACleanup();
  1133. #endif
  1134. return 0;
  1135. }
  1136. SERVER_EXPORT const char* GetTransportState(int transport_state)
  1137. {
  1138. switch (transport_state)
  1139. {
  1140. case JackTransportRolling:
  1141. return "rolling";
  1142. case JackTransportStarting:
  1143. return "starting";
  1144. case JackTransportStopped:
  1145. return "stopped";
  1146. case JackTransportNetStarting:
  1147. return "netstarting";
  1148. }
  1149. return NULL;
  1150. }
  1151. }