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.

1393 lines
52KB

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