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.

1174 lines
43KB

  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. #ifdef __APPLE__
  17. #include <mach/mach_time.h>
  18. class HardwareClock
  19. {
  20. public:
  21. HardwareClock();
  22. void Reset();
  23. void Update();
  24. float GetDeltaTime() const;
  25. double GetTime() const;
  26. private:
  27. double m_clockToSeconds;
  28. uint64_t m_startAbsTime;
  29. uint64_t m_lastAbsTime;
  30. double m_time;
  31. float m_deltaTime;
  32. };
  33. HardwareClock::HardwareClock()
  34. {
  35. mach_timebase_info_data_t info;
  36. mach_timebase_info(&info);
  37. m_clockToSeconds = (double)info.numer/info.denom/1000000000.0;
  38. Reset();
  39. }
  40. void HardwareClock::Reset()
  41. {
  42. m_startAbsTime = mach_absolute_time();
  43. m_lastAbsTime = m_startAbsTime;
  44. m_time = m_startAbsTime*m_clockToSeconds;
  45. m_deltaTime = 1.0f/60.0f;
  46. }
  47. void HardwareClock::Update()
  48. {
  49. const uint64_t currentTime = mach_absolute_time();
  50. const uint64_t dt = currentTime - m_lastAbsTime;
  51. m_time = currentTime*m_clockToSeconds;
  52. m_deltaTime = (double)dt*m_clockToSeconds;
  53. m_lastAbsTime = currentTime;
  54. }
  55. float HardwareClock::GetDeltaTime() const
  56. {
  57. return m_deltaTime;
  58. }
  59. double HardwareClock::GetTime() const
  60. {
  61. return m_time;
  62. }
  63. #endif
  64. using namespace std;
  65. namespace Jack
  66. {
  67. // NetMidiBuffer**********************************************************************************
  68. NetMidiBuffer::NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  69. {
  70. fNPorts = nports;
  71. fMaxBufsize = fNPorts * sizeof(sample_t) * params->fPeriodSize ;
  72. fMaxPcktSize = params->fMtu - sizeof(packet_header_t);
  73. fBuffer = new char[fMaxBufsize];
  74. fPortBuffer = new JackMidiBuffer* [fNPorts];
  75. for (int port_index = 0; port_index < fNPorts; port_index++)
  76. fPortBuffer[port_index] = NULL;
  77. fNetBuffer = net_buffer;
  78. fCycleSize = params->fMtu
  79. * (max(params->fSendMidiChannels, params->fReturnMidiChannels)
  80. * params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
  81. }
  82. NetMidiBuffer::~NetMidiBuffer()
  83. {
  84. delete[] fBuffer;
  85. delete[] fPortBuffer;
  86. }
  87. size_t NetMidiBuffer::GetCycleSize()
  88. {
  89. return fCycleSize;
  90. }
  91. int NetMidiBuffer::GetNumPackets(int data_size, int max_size)
  92. {
  93. return (data_size % max_size)
  94. ? (data_size / max_size + 1)
  95. : data_size / max_size;
  96. }
  97. void NetMidiBuffer::SetBuffer(int index, JackMidiBuffer* buffer)
  98. {
  99. fPortBuffer[index] = buffer;
  100. }
  101. JackMidiBuffer* NetMidiBuffer::GetBuffer(int index)
  102. {
  103. return fPortBuffer[index];
  104. }
  105. void NetMidiBuffer::DisplayEvents()
  106. {
  107. for (int port_index = 0; port_index < fNPorts; port_index++) {
  108. for (uint event = 0; event < fPortBuffer[port_index]->event_count; event++) {
  109. if (fPortBuffer[port_index]->IsValid()) {
  110. jack_info("port %d : midi event %u/%u -> time : %u, size : %u",
  111. port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
  112. fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size);
  113. }
  114. }
  115. }
  116. }
  117. int NetMidiBuffer::RenderFromJackPorts()
  118. {
  119. int pos = 0;
  120. size_t copy_size;
  121. for (int port_index = 0; port_index < fNPorts; port_index++) {
  122. char* write_pos = fBuffer + pos;
  123. copy_size = sizeof(JackMidiBuffer) + fPortBuffer[port_index]->event_count * sizeof(JackMidiEvent);
  124. memcpy(fBuffer + pos, fPortBuffer[port_index], copy_size);
  125. pos += copy_size;
  126. memcpy(fBuffer + pos,
  127. fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
  128. fPortBuffer[port_index]->write_pos);
  129. pos += fPortBuffer[port_index]->write_pos;
  130. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
  131. MidiBufferHToN(midi_buffer, midi_buffer);
  132. }
  133. return pos;
  134. }
  135. void NetMidiBuffer::RenderToJackPorts()
  136. {
  137. int pos = 0;
  138. size_t copy_size;
  139. for (int port_index = 0; port_index < fNPorts; port_index++) {
  140. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
  141. MidiBufferNToH(midi_buffer, midi_buffer);
  142. copy_size = sizeof(JackMidiBuffer) + reinterpret_cast<JackMidiBuffer*>(fBuffer + pos)->event_count * sizeof(JackMidiEvent);
  143. memcpy(fPortBuffer[port_index], fBuffer + pos, copy_size);
  144. pos += copy_size;
  145. memcpy(fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
  146. fBuffer + pos,
  147. fPortBuffer[port_index]->write_pos);
  148. pos += fPortBuffer[port_index]->write_pos;
  149. }
  150. }
  151. void NetMidiBuffer::RenderFromNetwork(int sub_cycle, size_t copy_size)
  152. {
  153. memcpy(fBuffer + sub_cycle * fMaxPcktSize, fNetBuffer, copy_size);
  154. }
  155. int NetMidiBuffer::RenderToNetwork(int sub_cycle, size_t total_size)
  156. {
  157. int size = total_size - sub_cycle * fMaxPcktSize;
  158. int copy_size = (size <= fMaxPcktSize) ? size : fMaxPcktSize;
  159. memcpy(fNetBuffer, fBuffer + sub_cycle * fMaxPcktSize, copy_size);
  160. return copy_size;
  161. }
  162. // net audio buffer *********************************************************************************
  163. //network<->buffer
  164. void NetAudioBuffer::ActivePortsToNetwork(uint32_t& port_num)
  165. {
  166. // Init active port count
  167. port_num = 0;
  168. short* active_port_address = (short*)fNetBuffer;
  169. for (int port_index = 0; port_index < fNPorts; port_index++) {
  170. // Write the active port number
  171. if (fPortBuffer[port_index]) {
  172. *active_port_address = port_index;
  173. active_port_address++;
  174. port_num++;
  175. assert(port_num < 512);
  176. }
  177. }
  178. }
  179. void NetAudioBuffer::ActivePortsFromNetwork(uint32_t port_num)
  180. {
  181. short* active_port_address = (short*)fNetBuffer;
  182. for (int port_index = 0; port_index < fNPorts; port_index++) {
  183. fPortBuffer[port_index] = NULL;
  184. }
  185. for (uint port_index = 0; port_index < port_num; port_index++) {
  186. // Use -1 when port is actually connected on other side
  187. if (*active_port_address >= 0 && *active_port_address < fNPorts) {
  188. fPortBuffer[*active_port_address] = (sample_t*)-1;
  189. } else {
  190. jack_error("ActivePortsFromNetwork: incorrect port = %d", *active_port_address);
  191. }
  192. active_port_address++;
  193. }
  194. }
  195. // Float
  196. NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  197. : NetAudioBuffer(), fPortBuffer1(params, nports)
  198. {
  199. fNetBuffer = net_buffer;
  200. }
  201. NetFloatAudioBuffer::~NetFloatAudioBuffer()
  202. {}
  203. size_t NetFloatAudioBuffer::GetCycleSize()
  204. {
  205. return fPortBuffer1.GetCycleSize();
  206. }
  207. void NetFloatAudioBuffer::SetBuffer(int index, sample_t* buffer)
  208. {
  209. fPortBuffer1.SetBuffer(index, buffer);
  210. }
  211. sample_t* NetFloatAudioBuffer::GetBuffer(int index)
  212. {
  213. return fPortBuffer1.GetBuffer(index);
  214. }
  215. void NetFloatAudioBuffer::RenderFromJackPorts()
  216. {
  217. fPortBuffer1.RenderFromJackPorts();
  218. }
  219. void NetFloatAudioBuffer::RenderToJackPorts()
  220. {
  221. fPortBuffer1.RenderToJackPorts();
  222. }
  223. //network<->buffer
  224. int NetFloatAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  225. {
  226. return fPortBuffer1.RenderFromNetwork(fNetBuffer, cycle, sub_cycle, copy_size, port_num);
  227. }
  228. int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t& port_num)
  229. {
  230. return fPortBuffer1.RenderToNetwork(fNetBuffer, sub_cycle, port_num);
  231. }
  232. void NetFloatAudioBuffer::ActivePortsToNetwork(char* net_buffer, uint32_t& port_num)
  233. {
  234. fPortBuffer1.ActivePortsToNetwork(net_buffer, port_num);
  235. }
  236. void NetFloatAudioBuffer::ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
  237. {
  238. fPortBuffer1.ActivePortsFromNetwork(net_buffer, port_num);
  239. }
  240. // New
  241. NetFloatAudioBuffer1::NetFloatAudioBuffer1(session_params_t* params, uint32_t nports, char* net_buffer)
  242. : NetAudioBuffer()
  243. {
  244. fNetBuffer = net_buffer;
  245. fNPorts = nports;
  246. fPeriodSize = params->fPeriodSize;
  247. fPacketSize = params->fMtu - sizeof(packet_header_t);
  248. if (params->fSendAudioChannels == 0 && params->fReturnAudioChannels == 0) {
  249. fSubPeriodSize = params->fPeriodSize;
  250. } else {
  251. jack_nframes_t period = (int) powf(2.f,(int)(log(float(fPacketSize)
  252. / (max(params->fReturnAudioChannels, params->fSendAudioChannels)
  253. * sizeof(sample_t))) / log(2.)));
  254. fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
  255. }
  256. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
  257. fPortBuffer = new sample_t* [fNPorts];
  258. for (int port_index = 0; port_index < fNPorts; port_index++) {
  259. fPortBuffer[port_index] = NULL;
  260. }
  261. fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
  262. fCycleSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
  263. fLastSubCycle = -1;
  264. }
  265. NetFloatAudioBuffer1::~NetFloatAudioBuffer1()
  266. {
  267. delete [] fPortBuffer;
  268. }
  269. void NetFloatAudioBuffer1::SetBuffer(int index, sample_t* buffer)
  270. {
  271. //jack_info("NetFloatAudioBuffer1::SetBuffer %d %x", index, buffer);
  272. fPortBuffer[index] = buffer;
  273. }
  274. sample_t* NetFloatAudioBuffer1::GetBuffer(int index)
  275. {
  276. return fPortBuffer[index];
  277. }
  278. // needed size in bytes for an entire cycle
  279. size_t NetFloatAudioBuffer1::GetCycleSize()
  280. {
  281. return fCycleSize;
  282. }
  283. // cycle duration in sec
  284. float NetFloatAudioBuffer1::GetCycleDuration()
  285. {
  286. return fCycleDuration;
  287. }
  288. int NetFloatAudioBuffer1::GetNumPackets()
  289. {
  290. // Count active ports
  291. int active_ports = 0;
  292. for (int port_index = 0; port_index < fNPorts; port_index++) {
  293. if (fPortBuffer[port_index]) active_ports++;
  294. }
  295. if (active_ports == 0) {
  296. fSubPeriodSize = fPeriodSize;
  297. } else {
  298. jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (active_ports * sizeof(sample_t))) / log(2.)));
  299. fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
  300. }
  301. fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t) + sizeof(uint32_t); // The port number in coded on 4 bytes
  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 NetFloatAudioBuffer1::RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  310. {
  311. int res = 0;
  312. // Cleanup all JACK ports at the beginning of the cycle
  313. if (sub_cycle == 0) {
  314. for (int port_index = 0; port_index < fNPorts; port_index++) {
  315. if (fPortBuffer[port_index]) {
  316. memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
  317. }
  318. }
  319. }
  320. if (port_num > 0) {
  321. /// Setup rendering parameters
  322. int sub_period_size, sub_period_bytes_size;
  323. if (port_num == 0) {
  324. sub_period_size = fPeriodSize;
  325. } else {
  326. jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (port_num * sizeof(sample_t))) / log(2.)));
  327. sub_period_size = (period > fPeriodSize) ? fPeriodSize : period;
  328. }
  329. sub_period_bytes_size = sub_period_size * sizeof(sample_t) + sizeof(uint32_t); // The port number in coded on 4 bytes
  330. for (uint32_t port_index = 0; port_index < port_num; port_index++) {
  331. // Only copy to active ports : read the active port number then audio data
  332. uint32_t* active_port_address = (uint32_t*)(fNetBuffer + port_index * sub_period_bytes_size);
  333. uint32_t active_port = (uint32_t)(*active_port_address);
  334. if (fPortBuffer[port_index]) {
  335. memcpy(fPortBuffer[active_port] + sub_cycle * sub_period_size, (char*)(active_port_address + 1), sub_period_bytes_size - sizeof(uint32_t));
  336. }
  337. }
  338. if (sub_cycle != fLastSubCycle + 1) {
  339. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  340. res = NET_PACKET_ERROR;
  341. }
  342. fLastSubCycle = sub_cycle;
  343. }
  344. return res;
  345. }
  346. int NetFloatAudioBuffer1::RenderToNetwork(int sub_cycle, uint32_t& port_num)
  347. {
  348. // Init active port count
  349. port_num = 0;
  350. //jack_info("NetFloatAudioBuffer1::RenderToNetwork");
  351. for (int port_index = 0; port_index < fNPorts; port_index++) {
  352. // Only copy from active ports : write the active port number then audio data
  353. if (fPortBuffer[port_index]) {
  354. jack_info("NetFloatAudioBuffer1::RenderToNetwork %d", port_index);
  355. uint32_t* active_port_address = (uint32_t*)(fNetBuffer + port_num * fSubPeriodBytesSize);
  356. *active_port_address = port_index;
  357. memcpy((char*)(active_port_address + 1), fPortBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(uint32_t));
  358. port_num++;
  359. }
  360. }
  361. return port_num * fSubPeriodBytesSize;
  362. }
  363. //network<->buffer
  364. /*
  365. void NetFloatAudioBuffer1::ActivePortsToNetwork(uint32_t& port_num)
  366. {
  367. // Init active port count
  368. port_num = 0;
  369. short* active_port_address = (short*)fNetBuffer;
  370. for (int port_index = 0; port_index < fNPorts; port_index++) {
  371. // Write the active port number
  372. if (fPortBuffer[port_index]) {
  373. *active_port_address = port_index;
  374. active_port_address++;
  375. port_num++;
  376. assert(port_num < 512);
  377. }
  378. }
  379. }
  380. void NetFloatAudioBuffer1::ActivePortsFromNetwork(uint32_t port_num)
  381. {
  382. short* active_port_address = (short*)fNetBuffer;
  383. for (int port_index = 0; port_index < fNPorts; port_index++) {
  384. fPortBuffer[port_index] = NULL;
  385. }
  386. for (uint port_index = 0; port_index < port_num; port_index++) {
  387. // Use -1 when port is actually connected on other side
  388. if (*active_port_address >= 0 && *active_port_address < fNPorts) {
  389. fPortBuffer[*active_port_address] = (sample_t*)-1;
  390. } else {
  391. jack_error("ActivePortsFromNetwork: incorrect port = %d", *active_port_address);
  392. }
  393. active_port_address++;
  394. }
  395. }
  396. */
  397. // Celt audio buffer *********************************************************************************
  398. #if HAVE_CELT
  399. #define KPS 32
  400. #define KPS_DIV 8
  401. NetCeltAudioBuffer::NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps)
  402. {
  403. int res1, res2;
  404. fNetBuffer = net_buffer;
  405. fNPorts = nports;
  406. fPeriodSize = params->fPeriodSize;
  407. fCeltMode = new CELTMode *[fNPorts];
  408. fCeltEncoder = new CELTEncoder *[fNPorts];
  409. fCeltDecoder = new CELTDecoder *[fNPorts];
  410. memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
  411. memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
  412. memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
  413. int error = CELT_OK;
  414. for (int i = 0; i < fNPorts; i++) {
  415. fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
  416. if (error != CELT_OK)
  417. goto error;
  418. #if HAVE_CELT_API_0_11
  419. fCeltEncoder[i] = celt_encoder_create_custom(fCeltMode[i], 1, &error);
  420. if (error != CELT_OK)
  421. goto error;
  422. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  423. fCeltDecoder[i] = celt_decoder_create_custom(fCeltMode[i], 1, &error);
  424. if (error != CELT_OK)
  425. goto error;
  426. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  427. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  428. fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
  429. if (error != CELT_OK)
  430. goto error;
  431. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  432. fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
  433. if (error != CELT_OK)
  434. goto error;
  435. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  436. #else
  437. fCeltEncoder[i] = celt_encoder_create(fCeltMode[i]);
  438. if (error != CELT_OK)
  439. goto error;
  440. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  441. fCeltDecoder[i] = celt_decoder_create(fCeltMode[i]);
  442. if (error != CELT_OK)
  443. goto error;
  444. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  445. #endif
  446. }
  447. fPortBuffer = new sample_t* [fNPorts];
  448. for (int port_index = 0; port_index < fNPorts; port_index++) {
  449. fPortBuffer[port_index] = NULL;
  450. }
  451. fCompressedSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
  452. fCompressedBuffer = new unsigned char* [fNPorts];
  453. for (int port_index = 0; port_index < fNPorts; port_index++) {
  454. fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
  455. }
  456. jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
  457. res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  458. res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  459. fNumPackets = (res1) ? (res2 + 1) : res2;
  460. jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
  461. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  462. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  463. jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  464. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  465. fCycleSize = params->fMtu * fNumPackets;
  466. fLastSubCycle = -1;
  467. return;
  468. error:
  469. FreeCelt();
  470. throw std::bad_alloc();
  471. }
  472. NetCeltAudioBuffer::~NetCeltAudioBuffer()
  473. {
  474. FreeCelt();
  475. for (int port_index = 0; port_index < fNPorts; port_index++) {
  476. delete [] fCompressedBuffer[port_index];
  477. }
  478. delete [] fCompressedBuffer;
  479. delete [] fPortBuffer;
  480. }
  481. void NetCeltAudioBuffer::FreeCelt()
  482. {
  483. for (int i = 0; i < fNPorts; i++) {
  484. if (fCeltEncoder[i]) {
  485. celt_encoder_destroy(fCeltEncoder[i]);
  486. }
  487. if (fCeltDecoder[i]) {
  488. celt_decoder_destroy(fCeltDecoder[i]);
  489. }
  490. if (fCeltMode[i]) {
  491. celt_mode_destroy(fCeltMode[i]);
  492. }
  493. }
  494. delete [] fCeltMode;
  495. delete [] fCeltEncoder;
  496. delete [] fCeltDecoder;
  497. }
  498. size_t NetCeltAudioBuffer::GetCycleSize()
  499. {
  500. return fCycleSize;
  501. }
  502. float NetCeltAudioBuffer::GetCycleDuration()
  503. {
  504. return fCycleDuration;
  505. }
  506. int NetCeltAudioBuffer::GetNumPackets()
  507. {
  508. return fNumPackets;
  509. }
  510. void NetCeltAudioBuffer::SetBuffer(int index, sample_t* buffer)
  511. {
  512. assert(fPortBuffer);
  513. fPortBuffer[index] = buffer;
  514. }
  515. sample_t* NetCeltAudioBuffer::GetBuffer(int index)
  516. {
  517. assert(fPortBuffer);
  518. return fPortBuffer[index];
  519. }
  520. void NetCeltAudioBuffer::RenderFromJackPorts()
  521. {
  522. float floatbuf[fPeriodSize];
  523. for (int port_index = 0; port_index < fNPorts; port_index++) {
  524. memcpy(floatbuf, fPortBuffer[port_index], fPeriodSize * sizeof(float));
  525. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  526. int res = celt_encode_float(fCeltEncoder[port_index], floatbuf, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
  527. #else
  528. int res = celt_encode_float(fCeltEncoder[port_index], floatbuf, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
  529. #endif
  530. if (res != fCompressedSizeByte) {
  531. jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  532. }
  533. }
  534. }
  535. void NetCeltAudioBuffer::RenderToJackPorts()
  536. {
  537. for (int port_index = 0; port_index < fNPorts; 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. #else
  541. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
  542. #endif
  543. if (res != CELT_OK) {
  544. jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  545. }
  546. }
  547. // reset for next cycle
  548. fLastSubCycle = -1;
  549. }
  550. //network<->buffer
  551. int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  552. {
  553. int res = 0;
  554. // Last packet of the cycle
  555. if (sub_cycle == fNumPackets - 1) {
  556. for (int port_index = 0; port_index < fNPorts; port_index++) {
  557. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  558. }
  559. } else {
  560. for (int port_index = 0; port_index < fNPorts; port_index++) {
  561. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  562. }
  563. }
  564. if (sub_cycle != fLastSubCycle + 1) {
  565. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  566. res = NET_PACKET_ERROR;
  567. }
  568. fLastSubCycle = sub_cycle;
  569. return res;
  570. }
  571. int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t& port_num)
  572. {
  573. port_num = fNPorts;
  574. // Last packet of the cycle
  575. if (sub_cycle == fNumPackets - 1) {
  576. for (int port_index = 0; port_index < fNPorts; port_index++) {
  577. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  578. }
  579. return fNPorts * fLastSubPeriodBytesSize;
  580. } else {
  581. for (int port_index = 0; port_index < fNPorts; port_index++) {
  582. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
  583. }
  584. return fNPorts * fSubPeriodBytesSize;
  585. }
  586. }
  587. #endif
  588. NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  589. {
  590. int res1, res2;
  591. fNPorts = nports;
  592. fPeriodSize = params->fPeriodSize;
  593. fNetBuffer = net_buffer;
  594. fPortBuffer = new sample_t* [fNPorts];
  595. for (int port_index = 0; port_index < fNPorts; port_index++) {
  596. fPortBuffer[port_index] = NULL;
  597. }
  598. fIntBuffer = new short* [fNPorts];
  599. for (int port_index = 0; port_index < fNPorts; port_index++) {
  600. fIntBuffer[port_index] = new short[fPeriodSize];
  601. }
  602. fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
  603. jack_log("fCompressedSizeByte %d", fCompressedSizeByte);
  604. res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  605. res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  606. jack_log("res1 = %d res2 = %d", res1, res2);
  607. fNumPackets = (res1) ? (res2 + 1) : res2;
  608. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  609. fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
  610. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  611. fLastSubPeriodSize = fLastSubPeriodBytesSize / sizeof(short);
  612. jack_log("fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  613. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  614. fCycleSize = params->fMtu * fNumPackets;
  615. fLastSubCycle = -1;
  616. return;
  617. }
  618. NetIntAudioBuffer::~NetIntAudioBuffer()
  619. {
  620. for (int port_index = 0; port_index < fNPorts; port_index++) {
  621. delete [] fIntBuffer[port_index];
  622. }
  623. delete [] fIntBuffer;
  624. delete [] fPortBuffer;
  625. }
  626. size_t NetIntAudioBuffer::GetCycleSize()
  627. {
  628. return fCycleSize;
  629. }
  630. float NetIntAudioBuffer::GetCycleDuration()
  631. {
  632. return fCycleDuration;
  633. }
  634. int NetIntAudioBuffer::GetNumPackets()
  635. {
  636. return fNumPackets;
  637. }
  638. void NetIntAudioBuffer::SetBuffer(int index, sample_t* buffer)
  639. {
  640. fPortBuffer[index] = buffer;
  641. }
  642. sample_t* NetIntAudioBuffer::GetBuffer(int index)
  643. {
  644. return fPortBuffer[index];
  645. }
  646. void NetIntAudioBuffer::RenderFromJackPorts()
  647. {
  648. for (int port_index = 0; port_index < fNPorts; port_index++) {
  649. for (unsigned int frame = 0; frame < fPeriodSize; frame++) {
  650. fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
  651. }
  652. }
  653. }
  654. void NetIntAudioBuffer::RenderToJackPorts()
  655. {
  656. float coef = 1.f / 32768.f;
  657. for (int port_index = 0; port_index < fNPorts; port_index++) {
  658. for (unsigned int frame = 0; frame < fPeriodSize; frame++) {
  659. fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
  660. }
  661. }
  662. // reset for next cycle
  663. fLastSubCycle = -1;
  664. }
  665. //network<->buffer
  666. int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, size_t copy_size, uint32_t port_num)
  667. {
  668. int res = 0;
  669. if (sub_cycle == fNumPackets - 1) {
  670. for (int port_index = 0; port_index < fNPorts; port_index++) {
  671. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  672. }
  673. } else {
  674. for (int port_index = 0; port_index < fNPorts; port_index++) {
  675. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  676. }
  677. }
  678. if (sub_cycle != fLastSubCycle + 1) {
  679. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  680. res = NET_PACKET_ERROR;
  681. }
  682. fLastSubCycle = sub_cycle;
  683. return res;
  684. }
  685. int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t& port_num)
  686. {
  687. port_num = fNPorts;
  688. // Last packet of the cycle
  689. if (sub_cycle == fNumPackets - 1) {
  690. for (int port_index = 0; port_index < fNPorts; port_index++) {
  691. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fLastSubPeriodBytesSize);
  692. }
  693. return fNPorts * fLastSubPeriodBytesSize;
  694. } else {
  695. for (int port_index = 0; port_index < fNPorts; port_index++) {
  696. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
  697. }
  698. return fNPorts * fSubPeriodBytesSize;
  699. }
  700. }
  701. // SessionParams ************************************************************************************
  702. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
  703. {
  704. memcpy(dst_params, src_params, sizeof(session_params_t));
  705. dst_params->fPacketID = htonl(src_params->fPacketID);
  706. dst_params->fMtu = htonl(src_params->fMtu);
  707. dst_params->fID = htonl(src_params->fID);
  708. dst_params->fTransportSync = htonl(src_params->fTransportSync);
  709. dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
  710. dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
  711. dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
  712. dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
  713. dst_params->fSampleRate = htonl(src_params->fSampleRate);
  714. dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
  715. dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
  716. dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
  717. dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
  718. }
  719. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
  720. {
  721. memcpy(dst_params, src_params, sizeof(session_params_t));
  722. dst_params->fPacketID = ntohl(src_params->fPacketID);
  723. dst_params->fMtu = ntohl(src_params->fMtu);
  724. dst_params->fID = ntohl(src_params->fID);
  725. dst_params->fTransportSync = ntohl(src_params->fTransportSync);
  726. dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
  727. dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
  728. dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
  729. dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
  730. dst_params->fSampleRate = ntohl(src_params->fSampleRate);
  731. dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
  732. dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
  733. dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
  734. dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
  735. }
  736. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
  737. {
  738. char encoder[16];
  739. switch (params->fSampleEncoder)
  740. {
  741. case JackFloatEncoder:
  742. strcpy(encoder, "float");
  743. break;
  744. case JackIntEncoder:
  745. strcpy(encoder, "integer");
  746. break;
  747. case JackCeltEncoder:
  748. strcpy(encoder, "CELT");
  749. break;
  750. }
  751. jack_info("**************** Network parameters ****************");
  752. jack_info("Name : %s", params->fName);
  753. jack_info("Protocol revision : %d", params->fProtocolVersion);
  754. jack_info("MTU : %u", params->fMtu);
  755. jack_info("Master name : %s", params->fMasterNetName);
  756. jack_info("Slave name : %s", params->fSlaveNetName);
  757. jack_info("ID : %u", params->fID);
  758. jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
  759. jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
  760. jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
  761. jack_info("Sample rate : %u frames per second", params->fSampleRate);
  762. jack_info("Period size : %u frames per period", params->fPeriodSize);
  763. jack_info("Network latency : %u cycles", params->fNetworkLatency);
  764. switch (params->fSampleEncoder) {
  765. case (JackFloatEncoder):
  766. jack_info("SampleEncoder : %s", "Float");
  767. break;
  768. case (JackIntEncoder):
  769. jack_info("SampleEncoder : %s", "16 bits integer");
  770. break;
  771. case (JackCeltEncoder):
  772. jack_info("SampleEncoder : %s", "CELT");
  773. jack_info("kBits : %d", params->fKBps);
  774. break;
  775. };
  776. jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
  777. jack_info("****************************************************");
  778. }
  779. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
  780. {
  781. switch (params->fPacketID)
  782. {
  783. case 0:
  784. return SLAVE_AVAILABLE;
  785. case 1:
  786. return SLAVE_SETUP;
  787. case 2:
  788. return START_MASTER;
  789. case 3:
  790. return START_SLAVE;
  791. case 4:
  792. return KILL_MASTER;
  793. }
  794. return INVALID;
  795. }
  796. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
  797. {
  798. switch (packet_type)
  799. {
  800. case INVALID:
  801. return -1;
  802. case SLAVE_AVAILABLE:
  803. params->fPacketID = 0;
  804. break;
  805. case SLAVE_SETUP:
  806. params->fPacketID = 1;
  807. break;
  808. case START_MASTER:
  809. params->fPacketID = 2;
  810. break;
  811. case START_SLAVE:
  812. params->fPacketID = 3;
  813. break;
  814. case KILL_MASTER:
  815. params->fPacketID = 4;
  816. }
  817. return 0;
  818. }
  819. // Packet header **********************************************************************************
  820. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
  821. {
  822. memcpy(dst_header, src_header, sizeof(packet_header_t));
  823. dst_header->fID = htonl(src_header->fID);
  824. dst_header->fNumPacket = htonl(src_header->fNumPacket);
  825. dst_header->fPacketSize = htonl(src_header->fPacketSize);
  826. dst_header->fActivePorts = htonl(src_header->fActivePorts);
  827. dst_header->fCycle = htonl(src_header->fCycle);
  828. dst_header->fSubCycle = htonl(src_header->fSubCycle);
  829. dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
  830. }
  831. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
  832. {
  833. memcpy(dst_header, src_header, sizeof(packet_header_t));
  834. dst_header->fID = ntohl(src_header->fID);
  835. dst_header->fNumPacket = ntohl(src_header->fNumPacket);
  836. dst_header->fPacketSize = ntohl(src_header->fPacketSize);
  837. dst_header->fActivePorts = ntohl(src_header->fActivePorts);
  838. dst_header->fCycle = ntohl(src_header->fCycle);
  839. dst_header->fSubCycle = ntohl(src_header->fSubCycle);
  840. dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
  841. }
  842. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
  843. {
  844. char bitdepth[16];
  845. jack_info("********************Header********************");
  846. jack_info("Data type : %c", header->fDataType);
  847. jack_info("Data stream : %c", header->fDataStream);
  848. jack_info("ID : %u", header->fID);
  849. jack_info("Cycle : %u", header->fCycle);
  850. jack_info("SubCycle : %u", header->fSubCycle);
  851. jack_info("Active ports : %u", header->fActivePorts);
  852. jack_info("DATA packets : %u", header->fNumPacket);
  853. jack_info("DATA size : %u", header->fPacketSize);
  854. jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
  855. jack_info("Bitdepth : %s", bitdepth);
  856. jack_info("**********************************************");
  857. }
  858. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
  859. {
  860. jack_info("********************Network Transport********************");
  861. jack_info("Transport new state : %u", data->fNewState);
  862. jack_info("Transport timebase master : %u", data->fTimebaseMaster);
  863. jack_info("Transport cycle state : %u", data->fState);
  864. jack_info("**********************************************");
  865. }
  866. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  867. {
  868. dst_buffer->magic = htonl(src_buffer->magic);
  869. dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
  870. dst_buffer->nframes = htonl(src_buffer->nframes);
  871. dst_buffer->write_pos = htonl(src_buffer->write_pos);
  872. dst_buffer->event_count = htonl(src_buffer->event_count);
  873. dst_buffer->lost_events = htonl(src_buffer->lost_events);
  874. dst_buffer->mix_index = htonl(src_buffer->mix_index);
  875. }
  876. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  877. {
  878. dst_buffer->magic = ntohl(src_buffer->magic);
  879. dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
  880. dst_buffer->nframes = ntohl(src_buffer->nframes);
  881. dst_buffer->write_pos = ntohl(src_buffer->write_pos);
  882. dst_buffer->event_count = ntohl(src_buffer->event_count);
  883. dst_buffer->lost_events = ntohl(src_buffer->lost_events);
  884. dst_buffer->mix_index = ntohl(src_buffer->mix_index);
  885. }
  886. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  887. {
  888. dst_params->fNewState = htonl(src_params->fNewState);
  889. dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
  890. dst_params->fState = htonl(src_params->fState);
  891. dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
  892. dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
  893. dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
  894. dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
  895. dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
  896. dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
  897. dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
  898. dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
  899. dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
  900. dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
  901. dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
  902. dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
  903. dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
  904. dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
  905. dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
  906. dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
  907. dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  908. dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
  909. dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
  910. }
  911. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  912. {
  913. dst_params->fNewState = ntohl(src_params->fNewState);
  914. dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
  915. dst_params->fState = ntohl(src_params->fState);
  916. dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
  917. dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
  918. dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
  919. dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
  920. dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
  921. dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
  922. dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
  923. dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
  924. dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
  925. dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
  926. dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
  927. dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
  928. dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
  929. dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
  930. dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
  931. dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
  932. dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  933. dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
  934. dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
  935. }
  936. // Utility *******************************************************************************************************
  937. SERVER_EXPORT int SocketAPIInit()
  938. {
  939. #ifdef WIN32
  940. WORD wVersionRequested = MAKEWORD(2, 2);
  941. WSADATA wsaData;
  942. if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  943. jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
  944. return -1;
  945. }
  946. if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
  947. jack_error("Could not find a useable version of Winsock.dll\n");
  948. WSACleanup();
  949. return -1;
  950. }
  951. #endif
  952. return 0;
  953. }
  954. SERVER_EXPORT int SocketAPIEnd()
  955. {
  956. #ifdef WIN32
  957. return WSACleanup();
  958. #endif
  959. return 0;
  960. }
  961. SERVER_EXPORT const char* GetTransportState(int transport_state)
  962. {
  963. switch (transport_state)
  964. {
  965. case JackTransportRolling:
  966. return "rolling";
  967. case JackTransportStarting:
  968. return "starting";
  969. case JackTransportStopped:
  970. return "stopped";
  971. case JackTransportNetStarting:
  972. return "netstarting";
  973. }
  974. return NULL;
  975. }
  976. }