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.

803 lines
30KB

  1. /*
  2. Copyright (C) 2008 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 * (max(params->fSendMidiChannels, params->fReturnMidiChannels) *
  79. params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
  80. }
  81. NetMidiBuffer::~NetMidiBuffer()
  82. {
  83. delete[] fBuffer;
  84. delete[] fPortBuffer;
  85. }
  86. size_t NetMidiBuffer::GetCycleSize()
  87. {
  88. return fCycleSize;
  89. }
  90. int NetMidiBuffer::GetNumPackets()
  91. {
  92. /*
  93. return (data_size % PACKET_AVAILABLE_SIZE)
  94. ? (data_size / PACKET_AVAILABLE_SIZE + 1)
  95. : data_size / PACKET_AVAILABLE_SIZE;
  96. */
  97. //TODO
  98. return 0;
  99. }
  100. void NetMidiBuffer::SetBuffer ( int index, JackMidiBuffer* buffer )
  101. {
  102. fPortBuffer[index] = buffer;
  103. }
  104. JackMidiBuffer* NetMidiBuffer::GetBuffer ( int index )
  105. {
  106. return fPortBuffer[index];
  107. }
  108. void NetMidiBuffer::DisplayEvents()
  109. {
  110. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  111. {
  112. for ( uint event = 0; event < fPortBuffer[port_index]->event_count; event++ )
  113. if ( fPortBuffer[port_index]->IsValid() )
  114. jack_info ( "port %d : midi event %u/%u -> time : %u, size : %u",
  115. port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
  116. fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size );
  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. {
  125. char* write_pos = fBuffer + pos;
  126. copy_size = sizeof ( JackMidiBuffer ) + fPortBuffer[port_index]->event_count * sizeof ( JackMidiEvent );
  127. memcpy ( fBuffer + pos, fPortBuffer[port_index], copy_size );
  128. pos += copy_size;
  129. memcpy ( fBuffer + pos, 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. int NetMidiBuffer::RenderToJackPorts()
  138. {
  139. int pos = 0;
  140. int copy_size;
  141. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  142. {
  143. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
  144. MidiBufferNToH(midi_buffer, midi_buffer);
  145. copy_size = sizeof ( JackMidiBuffer ) + reinterpret_cast<JackMidiBuffer*> ( fBuffer + pos )->event_count * sizeof ( JackMidiEvent );
  146. memcpy ( fPortBuffer[port_index], fBuffer + pos, copy_size );
  147. pos += copy_size;
  148. memcpy ( fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
  149. fBuffer + pos, fPortBuffer[port_index]->write_pos );
  150. pos += fPortBuffer[port_index]->write_pos;
  151. }
  152. return pos;
  153. }
  154. int NetMidiBuffer::RenderFromNetwork ( int subcycle, size_t copy_size )
  155. {
  156. memcpy ( fBuffer + subcycle * fMaxPcktSize, fNetBuffer, copy_size );
  157. return copy_size;
  158. }
  159. int NetMidiBuffer::RenderToNetwork ( int subcycle, size_t total_size )
  160. {
  161. int size = total_size - subcycle * fMaxPcktSize;
  162. int copy_size = ( size <= fMaxPcktSize ) ? size : fMaxPcktSize;
  163. memcpy ( fNetBuffer, fBuffer + subcycle * fMaxPcktSize, copy_size );
  164. return copy_size;
  165. }
  166. // net audio buffer *********************************************************************************
  167. NetSingleAudioBuffer::NetSingleAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  168. : fPortBuffer(params, nports), fNetBuffer(net_buffer)
  169. {}
  170. NetSingleAudioBuffer::~NetSingleAudioBuffer()
  171. {}
  172. size_t NetSingleAudioBuffer::GetCycleSize()
  173. {
  174. return fPortBuffer.GetCycleSize();
  175. }
  176. void NetSingleAudioBuffer::SetBuffer ( int index, sample_t* buffer )
  177. {
  178. fPortBuffer.SetBuffer(index, buffer);
  179. }
  180. sample_t* NetSingleAudioBuffer::GetBuffer ( int index )
  181. {
  182. return fPortBuffer.GetBuffer(index);
  183. }
  184. int NetSingleAudioBuffer::RenderFromJackPorts ()
  185. {
  186. return fPortBuffer.RenderFromJackPorts();
  187. }
  188. int NetSingleAudioBuffer::RenderToJackPorts ()
  189. {
  190. return fPortBuffer.RenderToJackPorts();
  191. }
  192. //network<->buffer
  193. int NetSingleAudioBuffer::RenderFromNetwork ( int cycle, int subcycle, size_t copy_size )
  194. {
  195. return fPortBuffer.RenderFromNetwork(fNetBuffer, cycle, subcycle, copy_size);
  196. }
  197. int NetSingleAudioBuffer::RenderToNetwork (int subcycle, size_t total_size )
  198. {
  199. return fPortBuffer.RenderToNetwork(fNetBuffer, subcycle, total_size);
  200. }
  201. // Celt audio buffer *********************************************************************************
  202. #ifdef CELT
  203. #define KPS 32
  204. #define KPS_DIV 8
  205. NetCeltAudioBuffer::NetCeltAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  206. : fNetBuffer(net_buffer)
  207. {
  208. int res1, res2;
  209. jack_nframes_t period;
  210. fNPorts = nports;
  211. fPeriodSize = params->fPeriodSize;
  212. fCeltMode = new CELTMode *[fNPorts];
  213. fCeltEncoder = new CELTEncoder *[fNPorts];
  214. fCeltDecoder = new CELTDecoder *[fNPorts];
  215. memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
  216. memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
  217. memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
  218. int error = CELT_OK;
  219. for (int i = 0; i < fNPorts; i++) {
  220. fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
  221. if (error != CELT_OK)
  222. goto error;
  223. fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
  224. if (error != CELT_OK)
  225. goto error;
  226. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  227. fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
  228. if (error != CELT_OK)
  229. goto error;
  230. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  231. }
  232. fPortBuffer = new sample_t* [fNPorts];
  233. for (int port_index = 0; port_index < fNPorts; port_index++)
  234. fPortBuffer[port_index] = NULL;
  235. /*
  236. celt_int32 lookahead;
  237. celt_mode_info( celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
  238. */
  239. //fCompressedSizeByte = (KPS * params->fPeriodSize * 1024 / params->fSampleRate / 8)&(~1);
  240. fCompressedSizeByte = (params->fPeriodSize * sizeof(sample_t)) / KPS_DIV; // TODO
  241. fCompressedBuffer = new unsigned char* [fNPorts];
  242. for (int port_index = 0; port_index < fNPorts; port_index++)
  243. fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
  244. jack_log("fCompressedSizeByte %d", fCompressedSizeByte);
  245. res1 = (fNPorts * fCompressedSizeByte) % (params->fMtu - sizeof(packet_header_t));
  246. res2 = (fNPorts * fCompressedSizeByte) / (params->fMtu - sizeof(packet_header_t));
  247. jack_log("res1 = %d res2 = %d", res1, res2);
  248. fNumPackets = (res1) ? (res2 + 1) : res2;
  249. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  250. //fLastSubPeriodBytesSize = fSubPeriodBytesSize + (fCompressedSizeByte - (fSubPeriodBytesSize * fNumPackets));
  251. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  252. jack_log("fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  253. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  254. fCycleSize = params->fMtu * fNumPackets;
  255. fLastSubCycle = -1;
  256. return;
  257. error:
  258. FreeCelt();
  259. throw std::bad_alloc();
  260. }
  261. NetCeltAudioBuffer::~NetCeltAudioBuffer()
  262. {
  263. FreeCelt();
  264. for (int port_index = 0; port_index < fNPorts; port_index++)
  265. delete [] fCompressedBuffer[port_index];
  266. delete [] fCompressedBuffer;
  267. delete [] fPortBuffer;
  268. }
  269. void NetCeltAudioBuffer::FreeCelt()
  270. {
  271. for (int i = 0; i < fNPorts; i++) {
  272. if (fCeltEncoder[i])
  273. celt_encoder_destroy(fCeltEncoder[i]);
  274. if (fCeltDecoder[i])
  275. celt_decoder_destroy(fCeltDecoder[i]);
  276. if (fCeltMode[i])
  277. celt_mode_destroy(fCeltMode[i]);
  278. }
  279. delete [] fCeltMode;
  280. delete [] fCeltEncoder;
  281. delete [] fCeltDecoder;
  282. }
  283. size_t NetCeltAudioBuffer::GetCycleSize()
  284. {
  285. return fCycleSize;
  286. }
  287. float NetCeltAudioBuffer::GetCycleDuration()
  288. {
  289. return fCycleDuration;
  290. }
  291. int NetCeltAudioBuffer::GetNumPackets()
  292. {
  293. return fNumPackets;
  294. }
  295. void NetCeltAudioBuffer::SetBuffer(int index, sample_t* buffer)
  296. {
  297. fPortBuffer[index] = buffer;
  298. }
  299. sample_t* NetCeltAudioBuffer::GetBuffer(int index)
  300. {
  301. return fPortBuffer[index];
  302. }
  303. int NetCeltAudioBuffer::RenderFromJackPorts()
  304. {
  305. float floatbuf[fPeriodSize];
  306. for (int port_index = 0; port_index < fNPorts; port_index++) {
  307. memcpy(floatbuf, fPortBuffer[port_index], fPeriodSize * sizeof(float));
  308. int res = celt_encode_float(fCeltEncoder[port_index], floatbuf, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
  309. if (res != fCompressedSizeByte) {
  310. jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  311. }
  312. }
  313. return fNPorts * fCompressedSizeByte; // in bytes
  314. }
  315. int NetCeltAudioBuffer::RenderToJackPorts()
  316. {
  317. for (int port_index = 0; port_index < fNPorts; port_index++) {
  318. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
  319. if (res != CELT_OK) {
  320. jack_error("celt_decode_float error res = %d", fCompressedSizeByte, res);
  321. }
  322. }
  323. fLastSubCycle = -1;
  324. //return fPeriodSize * sizeof(sample_t); // in bytes; TODO
  325. return 0;
  326. }
  327. HardwareClock clock;
  328. //network<->buffer
  329. int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int subcycle, size_t copy_size)
  330. {
  331. //clock.Update();
  332. if (subcycle == fNumPackets - 1) {
  333. for (int port_index = 0; port_index < fNPorts; port_index++)
  334. memcpy(fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  335. } else {
  336. for (int port_index = 0; port_index < fNPorts; port_index++)
  337. memcpy(fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  338. }
  339. if (subcycle != fLastSubCycle + 1)
  340. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, subcycle);
  341. fLastSubCycle = subcycle;
  342. //clock.Update();
  343. //const float dt = clock.GetDeltaTime();
  344. //printf("Delta: %f s\n", dt);
  345. return copy_size;
  346. }
  347. int NetCeltAudioBuffer::RenderToNetwork(int subcycle, size_t total_size)
  348. {
  349. if (subcycle == fNumPackets - 1) {
  350. for (int port_index = 0; port_index < fNPorts; port_index++)
  351. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  352. } else {
  353. for (int port_index = 0; port_index < fNPorts; port_index++)
  354. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
  355. }
  356. return fNPorts * fSubPeriodBytesSize;
  357. }
  358. #endif
  359. // Buffered
  360. /*
  361. NetBufferedAudioBuffer::NetBufferedAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  362. {
  363. fMaxCycle = 0;
  364. fNetBuffer = net_buffer;
  365. for (int i = 0; i < AUDIO_BUFFER_SIZE; i++) {
  366. fPortBuffer[i].Init(params, nports);
  367. }
  368. fJackPortBuffer = new sample_t* [nports];
  369. for ( uint32_t port_index = 0; port_index < nports; port_index++ )
  370. fJackPortBuffer[port_index] = NULL;
  371. }
  372. NetBufferedAudioBuffer::~NetBufferedAudioBuffer()
  373. {
  374. delete [] fJackPortBuffer;
  375. }
  376. size_t NetBufferedAudioBuffer::GetCycleSize()
  377. {
  378. return fPortBuffer[0].GetCycleSize();
  379. }
  380. void NetBufferedAudioBuffer::SetBuffer ( int index, sample_t* buffer )
  381. {
  382. fJackPortBuffer[index] = buffer;
  383. }
  384. sample_t* NetBufferedAudioBuffer::GetBuffer ( int index )
  385. {
  386. return fJackPortBuffer[index];
  387. }
  388. void NetBufferedAudioBuffer::RenderFromJackPorts (int subcycle )
  389. {
  390. fPortBuffer[0].RenderFromJackPorts(fNetBuffer, subcycle); // Always use first buffer...
  391. }
  392. void NetBufferedAudioBuffer::RenderToJackPorts (int cycle, int subcycle)
  393. {
  394. if (cycle < fMaxCycle) {
  395. jack_info("Wrong order fCycle %d subcycle %d fMaxCycle %d", cycle, subcycle, fMaxCycle);
  396. }
  397. fPortBuffer[cycle % AUDIO_BUFFER_SIZE].RenderToJackPorts(fNetBuffer, subcycle);
  398. }
  399. void NetBufferedAudioBuffer::FinishRenderToJackPorts (int cycle)
  400. {
  401. fMaxCycle = std::max(fMaxCycle, cycle);
  402. fPortBuffer[(cycle + 1) % AUDIO_BUFFER_SIZE].Copy(fJackPortBuffer); // Copy internal buffer in JACK ports
  403. }
  404. */
  405. // SessionParams ************************************************************************************
  406. SERVER_EXPORT void SessionParamsHToN ( session_params_t* src_params, session_params_t* dst_params )
  407. {
  408. memcpy(dst_params, src_params, sizeof(session_params_t));
  409. dst_params->fPacketID = htonl ( src_params->fPacketID );
  410. dst_params->fMtu = htonl ( src_params->fMtu );
  411. dst_params->fID = htonl ( src_params->fID );
  412. dst_params->fTransportSync = htonl ( src_params->fTransportSync );
  413. dst_params->fSendAudioChannels = htonl ( src_params->fSendAudioChannels );
  414. dst_params->fReturnAudioChannels = htonl ( src_params->fReturnAudioChannels );
  415. dst_params->fSendMidiChannels = htonl ( src_params->fSendMidiChannels );
  416. dst_params->fReturnMidiChannels = htonl ( src_params->fReturnMidiChannels );
  417. dst_params->fSampleRate = htonl ( src_params->fSampleRate );
  418. dst_params->fPeriodSize = htonl ( src_params->fPeriodSize );
  419. dst_params->fBitdepth = htonl ( src_params->fBitdepth );
  420. dst_params->fSlaveSyncMode = htonl ( src_params->fSlaveSyncMode );
  421. }
  422. SERVER_EXPORT void SessionParamsNToH ( session_params_t* src_params, session_params_t* dst_params )
  423. {
  424. memcpy(dst_params, src_params, sizeof(session_params_t));
  425. dst_params->fPacketID = ntohl ( src_params->fPacketID );
  426. dst_params->fMtu = ntohl ( src_params->fMtu );
  427. dst_params->fID = ntohl ( src_params->fID );
  428. dst_params->fTransportSync = ntohl ( src_params->fTransportSync );
  429. dst_params->fSendAudioChannels = ntohl ( src_params->fSendAudioChannels );
  430. dst_params->fReturnAudioChannels = ntohl ( src_params->fReturnAudioChannels );
  431. dst_params->fSendMidiChannels = ntohl ( src_params->fSendMidiChannels );
  432. dst_params->fReturnMidiChannels = ntohl ( src_params->fReturnMidiChannels );
  433. dst_params->fSampleRate = ntohl ( src_params->fSampleRate );
  434. dst_params->fPeriodSize = ntohl ( src_params->fPeriodSize );
  435. dst_params->fBitdepth = ntohl ( src_params->fBitdepth );
  436. dst_params->fSlaveSyncMode = ntohl ( src_params->fSlaveSyncMode );
  437. }
  438. SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params )
  439. {
  440. char bitdepth[16];
  441. ( params->fBitdepth ) ? sprintf ( bitdepth, "%u", params->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  442. char mode[8];
  443. switch ( params->fNetworkMode )
  444. {
  445. case 's' :
  446. strcpy ( mode, "slow" );
  447. break;
  448. case 'n' :
  449. strcpy ( mode, "normal" );
  450. break;
  451. case 'f' :
  452. strcpy ( mode, "fast" );
  453. break;
  454. }
  455. jack_info ( "**************** Network parameters ****************" );
  456. jack_info ( "Name : %s", params->fName );
  457. jack_info ( "Protocol revision : %d", params->fProtocolVersion );
  458. jack_info ( "MTU : %u", params->fMtu );
  459. jack_info ( "Master name : %s", params->fMasterNetName );
  460. jack_info ( "Slave name : %s", params->fSlaveNetName );
  461. jack_info ( "ID : %u", params->fID );
  462. jack_info ( "Transport Sync : %s", ( params->fTransportSync ) ? "yes" : "no" );
  463. jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
  464. jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
  465. jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
  466. jack_info ( "Period size : %u frames per period", params->fPeriodSize );
  467. jack_info ( "Bitdepth : %s", bitdepth );
  468. jack_info ( "Slave mode : %s", ( params->fSlaveSyncMode ) ? "sync" : "async" );
  469. jack_info ( "Network mode : %s", mode );
  470. jack_info ( "****************************************************" );
  471. }
  472. SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
  473. {
  474. switch ( params->fPacketID )
  475. {
  476. case 0:
  477. return SLAVE_AVAILABLE;
  478. case 1:
  479. return SLAVE_SETUP;
  480. case 2:
  481. return START_MASTER;
  482. case 3:
  483. return START_SLAVE;
  484. case 4:
  485. return KILL_MASTER;
  486. }
  487. return INVALID;
  488. }
  489. SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
  490. {
  491. switch ( packet_type )
  492. {
  493. case INVALID:
  494. return -1;
  495. case SLAVE_AVAILABLE:
  496. params->fPacketID = 0;
  497. break;
  498. case SLAVE_SETUP:
  499. params->fPacketID = 1;
  500. break;
  501. case START_MASTER:
  502. params->fPacketID = 2;
  503. break;
  504. case START_SLAVE:
  505. params->fPacketID = 3;
  506. break;
  507. case KILL_MASTER:
  508. params->fPacketID = 4;
  509. }
  510. return 0;
  511. }
  512. // Packet header **********************************************************************************
  513. SERVER_EXPORT void PacketHeaderHToN ( packet_header_t* src_header, packet_header_t* dst_header )
  514. {
  515. memcpy(dst_header, src_header, sizeof(packet_header_t));
  516. dst_header->fID = htonl ( src_header->fID );
  517. dst_header->fBitdepth = htonl ( src_header->fBitdepth );
  518. dst_header->fNumPacket = htonl ( src_header->fNumPacket );
  519. dst_header->fPacketSize = htonl ( src_header->fPacketSize );
  520. dst_header->fCycle = htonl ( src_header->fCycle );
  521. dst_header->fSubCycle = htonl ( src_header->fSubCycle );
  522. dst_header->fIsLastPckt = htonl ( src_header->fIsLastPckt );
  523. }
  524. SERVER_EXPORT void PacketHeaderNToH ( packet_header_t* src_header, packet_header_t* dst_header )
  525. {
  526. memcpy(dst_header, src_header, sizeof(packet_header_t));
  527. dst_header->fID = ntohl ( src_header->fID );
  528. dst_header->fBitdepth = ntohl ( src_header->fBitdepth );
  529. dst_header->fNumPacket = ntohl ( src_header->fNumPacket );
  530. dst_header->fPacketSize = ntohl ( src_header->fPacketSize );
  531. dst_header->fCycle = ntohl ( src_header->fCycle );
  532. dst_header->fSubCycle = ntohl ( src_header->fSubCycle );
  533. dst_header->fIsLastPckt = ntohl ( src_header->fIsLastPckt );
  534. }
  535. SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header )
  536. {
  537. char bitdepth[16];
  538. ( header->fBitdepth ) ? sprintf ( bitdepth, "%u", header->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  539. jack_info ( "********************Header********************" );
  540. jack_info ( "Data type : %c", header->fDataType );
  541. jack_info ( "Data stream : %c", header->fDataStream );
  542. jack_info ( "ID : %u", header->fID );
  543. jack_info ( "Cycle : %u", header->fCycle );
  544. jack_info ( "SubCycle : %u", header->fSubCycle );
  545. jack_info ( "Midi packets : %u", header->fNumPacket );
  546. jack_info ( "Last packet : '%s'", ( header->fIsLastPckt ) ? "yes" : "no" );
  547. jack_info ( "Bitdepth : %s", bitdepth );
  548. jack_info ( "**********************************************" );
  549. }
  550. SERVER_EXPORT void NetTransportDataDisplay ( net_transport_data_t* data )
  551. {
  552. jack_info ( "********************Network Transport********************" );
  553. jack_info ( "Transport new state : %u", data->fNewState );
  554. jack_info ( "Transport timebase master : %u", data->fTimebaseMaster );
  555. jack_info ( "Transport cycle state : %u", data->fState );
  556. jack_info ( "**********************************************" );
  557. }
  558. SERVER_EXPORT void MidiBufferHToN ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer )
  559. {
  560. dst_buffer->magic = htonl(src_buffer->magic);
  561. dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
  562. dst_buffer->nframes = htonl(src_buffer->nframes);
  563. dst_buffer->write_pos = htonl(src_buffer->write_pos);
  564. dst_buffer->event_count = htonl(src_buffer->event_count);
  565. dst_buffer->lost_events = htonl(src_buffer->lost_events);
  566. dst_buffer->mix_index = htonl(src_buffer->mix_index);
  567. }
  568. SERVER_EXPORT void MidiBufferNToH ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer )
  569. {
  570. dst_buffer->magic = ntohl(src_buffer->magic);
  571. dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
  572. dst_buffer->nframes = ntohl(src_buffer->nframes);
  573. dst_buffer->write_pos = ntohl(src_buffer->write_pos);
  574. dst_buffer->event_count = ntohl(src_buffer->event_count);
  575. dst_buffer->lost_events = ntohl(src_buffer->lost_events);
  576. dst_buffer->mix_index = ntohl(src_buffer->mix_index);
  577. }
  578. SERVER_EXPORT void TransportDataHToN ( net_transport_data_t* src_params, net_transport_data_t* dst_params )
  579. {
  580. dst_params->fNewState = htonl(src_params->fNewState);
  581. dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
  582. dst_params->fState = htonl(src_params->fState);
  583. dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
  584. dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
  585. dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
  586. dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
  587. dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
  588. dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
  589. dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
  590. dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
  591. dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
  592. dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
  593. dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
  594. dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
  595. dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
  596. dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
  597. dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
  598. dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
  599. dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  600. dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
  601. dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
  602. }
  603. SERVER_EXPORT void TransportDataNToH ( net_transport_data_t* src_params, net_transport_data_t* dst_params )
  604. {
  605. dst_params->fNewState = ntohl(src_params->fNewState);
  606. dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
  607. dst_params->fState = ntohl(src_params->fState);
  608. dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
  609. dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
  610. dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
  611. dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
  612. dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
  613. dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
  614. dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
  615. dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
  616. dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
  617. dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
  618. dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
  619. dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
  620. dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
  621. dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
  622. dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
  623. dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
  624. dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  625. dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
  626. dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
  627. }
  628. // Utility *******************************************************************************************************
  629. SERVER_EXPORT int SocketAPIInit()
  630. {
  631. #ifdef WIN32
  632. WORD wVersionRequested = MAKEWORD ( 2, 2 );
  633. WSADATA wsaData;
  634. if ( WSAStartup ( wVersionRequested, &wsaData ) != 0 )
  635. {
  636. jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
  637. return -1;
  638. }
  639. if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
  640. {
  641. jack_error ( "Could not find a useable version of Winsock.dll\n" );
  642. WSACleanup();
  643. return -1;
  644. }
  645. #endif
  646. return 0;
  647. }
  648. SERVER_EXPORT int SocketAPIEnd()
  649. {
  650. #ifdef WIN32
  651. return WSACleanup();
  652. #endif
  653. return 0;
  654. }
  655. SERVER_EXPORT const char* GetTransportState ( int transport_state )
  656. {
  657. switch ( transport_state )
  658. {
  659. case JackTransportRolling:
  660. return "rolling";
  661. case JackTransportStarting:
  662. return "starting";
  663. case JackTransportStopped:
  664. return "stopped";
  665. case JackTransportNetStarting:
  666. return "netstarting";
  667. }
  668. return NULL;
  669. }
  670. }