Jack2  1.9.10
JackNetTool.cpp
1 /*
2 Copyright (C) 2008-2011 Romain Moret at Grame
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #include "JackNetTool.h"
21 #include "JackError.h"
22 
23 #ifdef __APPLE__
24 
25 #include <mach/mach_time.h>
26 
27 class HardwareClock
28 {
29  public:
30 
31  HardwareClock();
32 
33  void Reset();
34  void Update();
35 
36  float GetDeltaTime() const;
37  double GetTime() const;
38 
39  private:
40 
41  double m_clockToSeconds;
42 
43  uint64_t m_startAbsTime;
44  uint64_t m_lastAbsTime;
45 
46  double m_time;
47  float m_deltaTime;
48 };
49 
50 HardwareClock::HardwareClock()
51 {
52  mach_timebase_info_data_t info;
53  mach_timebase_info(&info);
54  m_clockToSeconds = (double)info.numer/info.denom/1000000000.0;
55  Reset();
56 }
57 
58 void HardwareClock::Reset()
59 {
60  m_startAbsTime = mach_absolute_time();
61  m_lastAbsTime = m_startAbsTime;
62  m_time = m_startAbsTime*m_clockToSeconds;
63  m_deltaTime = 1.0f/60.0f;
64 }
65 
66 void HardwareClock::Update()
67 {
68  const uint64_t currentTime = mach_absolute_time();
69  const uint64_t dt = currentTime - m_lastAbsTime;
70 
71  m_time = currentTime*m_clockToSeconds;
72  m_deltaTime = (double)dt*m_clockToSeconds;
73  m_lastAbsTime = currentTime;
74 }
75 
76 float HardwareClock::GetDeltaTime() const
77 {
78  return m_deltaTime;
79 }
80 
81 double HardwareClock::GetTime() const
82 {
83  return m_time;
84 }
85 
86 #endif
87 
88 using namespace std;
89 
90 namespace Jack
91 {
92 // NetMidiBuffer**********************************************************************************
93 
94  NetMidiBuffer::NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
95  {
96  fNPorts = nports;
97  fMaxBufsize = fNPorts * sizeof(sample_t) * params->fPeriodSize;
98  fMaxPcktSize = params->fMtu - sizeof(packet_header_t);
99  fBuffer = new char[fMaxBufsize];
100  fPortBuffer = new JackMidiBuffer* [fNPorts];
101  for (int port_index = 0; port_index < fNPorts; port_index++) {
102  fPortBuffer[port_index] = NULL;
103  }
104  fNetBuffer = net_buffer;
105  fCycleBytesSize = params->fMtu
106  * (max(params->fSendMidiChannels, params->fReturnMidiChannels)
107  * params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
108  }
109 
110  NetMidiBuffer::~NetMidiBuffer()
111  {
112  delete[] fBuffer;
113  delete[] fPortBuffer;
114  }
115 
116  size_t NetMidiBuffer::GetCycleSize()
117  {
118  return fCycleBytesSize;
119  }
120 
121  int NetMidiBuffer::GetNumPackets(int data_size, int max_size)
122  {
123  int res1 = data_size % max_size;
124  int res2 = data_size / max_size;
125  return (res1) ? res2 + 1 : res2;
126  }
127 
128  void NetMidiBuffer::SetBuffer(int index, JackMidiBuffer* buffer)
129  {
130  fPortBuffer[index] = buffer;
131  }
132 
133  JackMidiBuffer* NetMidiBuffer::GetBuffer(int index)
134  {
135  return fPortBuffer[index];
136  }
137 
138  void NetMidiBuffer::DisplayEvents()
139  {
140  for (int port_index = 0; port_index < fNPorts; port_index++) {
141  for (uint event = 0; event < fPortBuffer[port_index]->event_count; event++) {
142  if (fPortBuffer[port_index]->IsValid()) {
143  jack_info("port %d : midi event %u/%u -> time : %u, size : %u",
144  port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
145  fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size);
146  }
147  }
148  }
149  }
150 
151  int NetMidiBuffer::RenderFromJackPorts()
152  {
153  int pos = 0;
154  size_t copy_size;
155 
156  for (int port_index = 0; port_index < fNPorts; port_index++) {
157  char* write_pos = fBuffer + pos;
158  copy_size = sizeof(JackMidiBuffer) + fPortBuffer[port_index]->event_count * sizeof(JackMidiEvent);
159  memcpy(fBuffer + pos, fPortBuffer[port_index], copy_size);
160  pos += copy_size;
161  memcpy(fBuffer + pos,
162  fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
163  fPortBuffer[port_index]->write_pos);
164  pos += fPortBuffer[port_index]->write_pos;
165  JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
166  MidiBufferHToN(midi_buffer, midi_buffer);
167  }
168  return pos;
169  }
170 
171  void NetMidiBuffer::RenderToJackPorts()
172  {
173  int pos = 0;
174  size_t copy_size;
175 
176  for (int port_index = 0; port_index < fNPorts; port_index++) {
177  JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
178  MidiBufferNToH(midi_buffer, midi_buffer);
179  copy_size = sizeof(JackMidiBuffer) + reinterpret_cast<JackMidiBuffer*>(fBuffer + pos)->event_count * sizeof(JackMidiEvent);
180  memcpy(fPortBuffer[port_index], fBuffer + pos, copy_size);
181  pos += copy_size;
182  memcpy(fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
183  fBuffer + pos,
184  fPortBuffer[port_index]->write_pos);
185  pos += fPortBuffer[port_index]->write_pos;
186  }
187  }
188 
189  void NetMidiBuffer::RenderFromNetwork(int sub_cycle, size_t copy_size)
190  {
191  memcpy(fBuffer + sub_cycle * fMaxPcktSize, fNetBuffer, copy_size);
192  }
193 
194  int NetMidiBuffer::RenderToNetwork(int sub_cycle, size_t total_size)
195  {
196  int size = total_size - sub_cycle * fMaxPcktSize;
197  int copy_size = (size <= fMaxPcktSize) ? size : fMaxPcktSize;
198  memcpy(fNetBuffer, fBuffer + sub_cycle * fMaxPcktSize, copy_size);
199  return copy_size;
200  }
201 
202 // net audio buffer *********************************************************************************
203 
204  NetAudioBuffer::NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
205  {
206  fNPorts = nports;
207  fNetBuffer = net_buffer;
208  fNumPackets = 0;
209 
210  fPortBuffer = new sample_t*[fNPorts];
211  fConnectedPorts = new bool[fNPorts];
212 
213  for (int port_index = 0; port_index < fNPorts; port_index++) {
214  fPortBuffer[port_index] = NULL;
215  fConnectedPorts[port_index] = true;
216  }
217 
218  fLastSubCycle = 0;
219  fPeriodSize = 0;
220  fSubPeriodSize = 0;
221  fSubPeriodBytesSize = 0;
222  fCycleDuration = 0.f;
223  fCycleBytesSize = 0;
224  }
225 
226  NetAudioBuffer::~NetAudioBuffer()
227  {
228  delete [] fConnectedPorts;
229  delete [] fPortBuffer;
230  }
231 
232  void NetAudioBuffer::SetBuffer(int index, sample_t* buffer)
233  {
234  fPortBuffer[index] = buffer;
235  }
236 
237  sample_t* NetAudioBuffer::GetBuffer(int index)
238  {
239  return fPortBuffer[index];
240  }
241 
242  int NetAudioBuffer::CheckPacket(int cycle, int sub_cycle)
243  {
244  int res;
245 
246  if (sub_cycle != fLastSubCycle + 1) {
247  jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
248  res = DATA_PACKET_ERROR;
249  } else {
250  res = 0;
251  }
252 
253  fLastSubCycle = sub_cycle;
254  return res;
255  }
256 
257  void NetAudioBuffer::NextCycle()
258  {
259  // reset for next cycle
260  fLastSubCycle = -1;
261  }
262 
263  void NetAudioBuffer::Cleanup()
264  {
265  for (int port_index = 0; port_index < fNPorts; port_index++) {
266  if (fPortBuffer[port_index]) {
267  memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
268  }
269  }
270  }
271 
272  //network<->buffer
273 
274  int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer)
275  {
276  int active_ports = 0;
277  int* active_port_address = (int*)net_buffer;
278 
279  for (int port_index = 0; port_index < fNPorts; port_index++) {
280  // Write the active port number
281  if (fPortBuffer[port_index]) {
282  *active_port_address = htonl(port_index);
283  active_port_address++;
284  active_ports++;
285  assert(active_ports < 256);
286  }
287  }
288 
289  return active_ports;
290  }
291 
292  void NetAudioBuffer::ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
293  {
294  int* active_port_address = (int*)net_buffer;
295 
296  for (int port_index = 0; port_index < fNPorts; port_index++) {
297  fConnectedPorts[port_index] = false;
298  }
299 
300  for (uint port_index = 0; port_index < port_num; port_index++) {
301  int active_port = ntohl(*active_port_address);
302  fConnectedPorts[active_port] = true;
303  active_port_address++;
304  }
305  }
306 
307  int NetAudioBuffer::RenderFromJackPorts(int unused_frames)
308  {
309  // Count active ports
310  int active_ports = 0;
311  for (int port_index = 0; port_index < fNPorts; port_index++) {
312  if (fPortBuffer[port_index]) {
313  active_ports++;
314  }
315  }
316 
317  return active_ports;
318  }
319 
320  void NetAudioBuffer::RenderToJackPorts(int unused_frames)
321  {
322  // Nothing to do
323  NextCycle();
324  }
325 
326  // Float converter
327 
328  NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
329  : NetAudioBuffer(params, nports, net_buffer)
330  {
331  fPeriodSize = params->fPeriodSize;
332  fPacketSize = PACKET_AVAILABLE_SIZE(params);
333 
334  UpdateParams(max(params->fReturnAudioChannels, params->fSendAudioChannels));
335 
336  fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
337 
338  fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
339  fCycleBytesSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
340 
341  fLastSubCycle = -1;
342  }
343 
344  NetFloatAudioBuffer::~NetFloatAudioBuffer()
345  {}
346 
347  // needed size in bytes for an entire cycle
348  size_t NetFloatAudioBuffer::GetCycleSize()
349  {
350  return fCycleBytesSize;
351  }
352 
353  // cycle duration in sec
354  float NetFloatAudioBuffer::GetCycleDuration()
355  {
356  return fCycleDuration;
357  }
358 
359  void NetFloatAudioBuffer::UpdateParams(int active_ports)
360  {
361  if (active_ports == 0) {
362  fSubPeriodSize = fPeriodSize;
363  } else {
364  jack_nframes_t period = int(powf(2.f, int(log(float(fPacketSize) / (active_ports * sizeof(sample_t))) / log(2.))));
365  fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
366  }
367 
368  fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t) + sizeof(int); // The port number in coded on 4 bytes
369  fNumPackets = fPeriodSize / fSubPeriodSize; // At least one packet
370  }
371 
372  int NetFloatAudioBuffer::GetNumPackets(int active_ports)
373  {
374  UpdateParams(active_ports);
375 
376  /*
377  jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
378  fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
379  */
380  return fNumPackets;
381  }
382 
383  //jack<->buffer
384 
385  int NetFloatAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
386  {
387  // Cleanup all JACK ports at the beginning of the cycle
388  if (sub_cycle == 0) {
389  Cleanup();
390  }
391 
392  if (port_num > 0) {
393  UpdateParams(port_num);
394  for (uint32_t port_index = 0; port_index < port_num; port_index++) {
395  // Only copy to active ports : read the active port number then audio data
396  int* active_port_address = (int*)(fNetBuffer + port_index * fSubPeriodBytesSize);
397  int active_port = ntohl(*active_port_address);
398  RenderFromNetwork((char*)(active_port_address + 1), active_port, sub_cycle);
399  }
400  }
401 
402  return CheckPacket(cycle, sub_cycle);
403  }
404 
405  int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
406  {
407  int active_ports = 0;
408 
409  for (int port_index = 0; port_index < fNPorts; port_index++) {
410  // Only copy from active ports : write the active port number then audio data
411  if (fPortBuffer[port_index]) {
412  int* active_port_address = (int*)(fNetBuffer + active_ports * fSubPeriodBytesSize);
413  *active_port_address = htonl(port_index);
414  RenderToNetwork((char*)(active_port_address + 1), port_index, sub_cycle);
415  active_ports++;
416  }
417  }
418 
419  return port_num * fSubPeriodBytesSize;
420  }
421 
422 #ifdef __BIG_ENDIAN__
423 
424  static inline jack_default_audio_sample_t SwapFloat(jack_default_audio_sample_t f)
425  {
426  union
427  {
428  jack_default_audio_sample_t f;
429  unsigned char b[4];
430  } dat1, dat2;
431 
432  dat1.f = f;
433  dat2.b[0] = dat1.b[3];
434  dat2.b[1] = dat1.b[2];
435  dat2.b[2] = dat1.b[1];
436  dat2.b[3] = dat1.b[0];
437  return dat2.f;
438  }
439 
440  void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
441  {
442  if (fPortBuffer[active_port]) {
443  jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(net_buffer);
444  jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
445  for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
446  dst[sample] = SwapFloat(src[sample]);
447  }
448  }
449  }
450 
451  void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
452  {
453  for (int port_index = 0; port_index < fNPorts; port_index++ ) {
454  jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
455  jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(net_buffer);
456  for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
457  dst[sample] = SwapFloat(src[sample]);
458  }
459  }
460  }
461 
462 #else
463 
464  void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
465  {
466  if (fPortBuffer[active_port]) {
467  memcpy(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, net_buffer, fSubPeriodBytesSize - sizeof(int));
468  }
469  }
470 
471  void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
472  {
473  memcpy(net_buffer, fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(int));
474  }
475 
476 #endif
477  // Celt audio buffer *********************************************************************************
478 
479 #if HAVE_CELT
480 
481  #define KPS 32
482  #define KPS_DIV 8
483 
484  NetCeltAudioBuffer::NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps)
485  :NetAudioBuffer(params, nports, net_buffer)
486  {
487  fCeltMode = new CELTMode*[fNPorts];
488  fCeltEncoder = new CELTEncoder*[fNPorts];
489  fCeltDecoder = new CELTDecoder*[fNPorts];
490 
491  memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
492  memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
493  memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
494 
495  int error = CELT_OK;
496 
497  for (int i = 0; i < fNPorts; i++) {
498  fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
499  if (error != CELT_OK) {
500  jack_log("NetCeltAudioBuffer celt_mode_create err = %d", error);
501  goto error;
502  }
503 
504  #if HAVE_CELT_API_0_11
505 
506  fCeltEncoder[i] = celt_encoder_create_custom(fCeltMode[i], 1, &error);
507  if (error != CELT_OK) {
508  jack_log("NetCeltAudioBuffer celt_encoder_create_custom err = %d", error);
509  goto error;
510  }
511  celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
512 
513  fCeltDecoder[i] = celt_decoder_create_custom(fCeltMode[i], 1, &error);
514  if (error != CELT_OK) {
515  jack_log("NetCeltAudioBuffer celt_decoder_create_custom err = %d", error);
516  goto error;
517  }
518  celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
519 
520  #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
521 
522  fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
523  if (error != CELT_OK) {
524  jack_log("NetCeltAudioBuffer celt_mode_create err = %d", error);
525  goto error;
526  }
527  celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
528 
529  fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
530  if (error != CELT_OK) {
531  jack_log("NetCeltAudioBuffer celt_decoder_create err = %d", error);
532  goto error;
533  }
534  celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
535 
536  #else
537 
538  fCeltEncoder[i] = celt_encoder_create(fCeltMode[i]);
539  if (error != CELT_OK) {
540  jack_log("NetCeltAudioBuffer celt_encoder_create err = %d", error);
541  goto error;
542  }
543  celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
544 
545  fCeltDecoder[i] = celt_decoder_create(fCeltMode[i]);
546  if (error != CELT_OK) {
547  jack_log("NetCeltAudioBuffer celt_decoder_create err = %d", error);
548  goto error;
549  }
550  celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
551 
552  #endif
553  }
554 
555  {
556  fPeriodSize = params->fPeriodSize;
557 
558  fCompressedSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
559  jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
560 
561  fCompressedBuffer = new unsigned char* [fNPorts];
562  for (int port_index = 0; port_index < fNPorts; port_index++) {
563  fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
564  memset(fCompressedBuffer[port_index], 0, fCompressedSizeByte * sizeof(char));
565  }
566 
567  int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
568  int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
569 
570  fNumPackets = (res1) ? (res2 + 1) : res2;
571 
572  jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
573 
574  fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
575  fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
576 
577  jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
578 
579  fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
580  fCycleBytesSize = params->fMtu * fNumPackets;
581 
582  fLastSubCycle = -1;
583  return;
584  }
585 
586  error:
587 
588  FreeCelt();
589  throw std::bad_alloc();
590  }
591 
592  NetCeltAudioBuffer::~NetCeltAudioBuffer()
593  {
594  FreeCelt();
595 
596  for (int port_index = 0; port_index < fNPorts; port_index++) {
597  delete [] fCompressedBuffer[port_index];
598  }
599 
600  delete [] fCompressedBuffer;
601  }
602 
603  void NetCeltAudioBuffer::FreeCelt()
604  {
605  for (int i = 0; i < fNPorts; i++) {
606  if (fCeltEncoder[i]) {
607  celt_encoder_destroy(fCeltEncoder[i]);
608  }
609  if (fCeltDecoder[i]) {
610  celt_decoder_destroy(fCeltDecoder[i]);
611  }
612  if (fCeltMode[i]) {
613  celt_mode_destroy(fCeltMode[i]);
614  }
615  }
616 
617  delete [] fCeltMode;
618  delete [] fCeltEncoder;
619  delete [] fCeltDecoder;
620  }
621 
622  size_t NetCeltAudioBuffer::GetCycleSize()
623  {
624  return fCycleBytesSize;
625  }
626 
627  float NetCeltAudioBuffer::GetCycleDuration()
628  {
629  return fCycleDuration;
630  }
631 
632  int NetCeltAudioBuffer::GetNumPackets(int active_ports)
633  {
634  return fNumPackets;
635  }
636 
637  int NetCeltAudioBuffer::RenderFromJackPorts(int nframes)
638  {
639  float buffer[BUFFER_SIZE_MAX];
640 
641  for (int port_index = 0; port_index < fNPorts; port_index++) {
642  if (fPortBuffer[port_index]) {
643  memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
644  } else {
645  memset(buffer, 0, fPeriodSize * sizeof(sample_t));
646  }
647  #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
648  //int res = celt_encode_float(fCeltEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
649  int res = celt_encode_float(fCeltEncoder[port_index], buffer, nframes, fCompressedBuffer[port_index], fCompressedSizeByte);
650  #else
651  int res = celt_encode_float(fCeltEncoder[port_index], buffer, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
652  #endif
653  if (res != fCompressedSizeByte) {
654  jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
655  }
656  }
657 
658  // All ports active
659  return fNPorts;
660  }
661 
662  void NetCeltAudioBuffer::RenderToJackPorts(int nframes)
663  {
664  for (int port_index = 0; port_index < fNPorts; port_index++) {
665  if (fPortBuffer[port_index]) {
666  #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
667  //int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize);
668  int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], nframes);
669  #else
670  int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
671  #endif
672  if (res != CELT_OK) {
673  jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
674  }
675  }
676  }
677 
678  NextCycle();
679  }
680 
681  //network<->buffer
682  int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
683  {
684  // Cleanup all JACK ports at the beginning of the cycle
685  if (sub_cycle == 0) {
686  Cleanup();
687  }
688 
689  if (port_num > 0) {
690 
691  int sub_period_bytes_size;
692 
693  // Last packet of the cycle
694  if (sub_cycle == fNumPackets - 1) {
695  sub_period_bytes_size = fLastSubPeriodBytesSize;
696  } else {
697  sub_period_bytes_size = fSubPeriodBytesSize;
698  }
699 
700  for (int port_index = 0; port_index < fNPorts; port_index++) {
701  memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * sub_period_bytes_size, sub_period_bytes_size);
702  }
703  }
704 
705  return CheckPacket(cycle, sub_cycle);
706  }
707 
708  int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
709  {
710  int sub_period_bytes_size;
711 
712  // Last packet of the cycle
713  if (sub_cycle == fNumPackets - 1) {
714  sub_period_bytes_size = fLastSubPeriodBytesSize;
715  } else {
716  sub_period_bytes_size = fSubPeriodBytesSize;
717  }
718 
719  for (int port_index = 0; port_index < fNPorts; port_index++) {
720  memcpy(fNetBuffer + port_index * sub_period_bytes_size, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, sub_period_bytes_size);
721  }
722  return fNPorts * sub_period_bytes_size;
723  }
724 
725 #endif
726 
727 
728 #if HAVE_OPUS
729 #define CDO (sizeof(short))
730  NetOpusAudioBuffer::NetOpusAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps)
731  :NetAudioBuffer(params, nports, net_buffer)
732  {
733  fOpusMode = new OpusCustomMode*[fNPorts];
734  fOpusEncoder = new OpusCustomEncoder*[fNPorts];
735  fOpusDecoder = new OpusCustomDecoder*[fNPorts];
736  fCompressedSizesByte = new unsigned short[fNPorts];
737 
738  memset(fOpusMode, 0, fNPorts * sizeof(OpusCustomMode*));
739  memset(fOpusEncoder, 0, fNPorts * sizeof(OpusCustomEncoder*));
740  memset(fOpusDecoder, 0, fNPorts * sizeof(OpusCustomDecoder*));
741  memset(fCompressedSizesByte, 0, fNPorts * sizeof(short));
742 
743  int error = OPUS_OK;
744 
745  for (int i = 0; i < fNPorts; i++) {
746  /* Allocate en/decoders */
747  fOpusMode[i] = opus_custom_mode_create(params->fSampleRate, params->fPeriodSize, &error);
748  if (error != OPUS_OK) {
749  jack_log("NetOpusAudioBuffer opus_custom_mode_create err = %d", error);
750  goto error;
751  }
752 
753  fOpusEncoder[i] = opus_custom_encoder_create(fOpusMode[i], 1, &error);
754  if (error != OPUS_OK) {
755  jack_log("NetOpusAudioBuffer opus_custom_encoder_create err = %d", error);
756  goto error;
757  }
758 
759  fOpusDecoder[i] = opus_custom_decoder_create(fOpusMode[i], 1, &error);
760  if (error != OPUS_OK) {
761  jack_log("NetOpusAudioBuffer opus_custom_decoder_create err = %d", error);
762  goto error;
763  }
764 
765  opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_BITRATE(kbps*1024)); // bits per second
766  opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_COMPLEXITY(10));
767  opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));
768  opus_custom_encoder_ctl(fOpusEncoder[i], OPUS_SET_SIGNAL(OPUS_APPLICATION_RESTRICTED_LOWDELAY));
769  }
770 
771  {
772  fCompressedMaxSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
773  fPeriodSize = params->fPeriodSize;
774  jack_log("NetOpusAudioBuffer fCompressedMaxSizeByte %d", fCompressedMaxSizeByte);
775 
776  fCompressedBuffer = new unsigned char* [fNPorts];
777  for (int port_index = 0; port_index < fNPorts; port_index++) {
778  fCompressedBuffer[port_index] = new unsigned char[fCompressedMaxSizeByte];
779  memset(fCompressedBuffer[port_index], 0, fCompressedMaxSizeByte * sizeof(char));
780  }
781 
782  int res1 = (fNPorts * fCompressedMaxSizeByte + CDO) % PACKET_AVAILABLE_SIZE(params);
783  int res2 = (fNPorts * fCompressedMaxSizeByte + CDO) / PACKET_AVAILABLE_SIZE(params);
784 
785  fNumPackets = (res1) ? (res2 + 1) : res2;
786 
787  jack_log("NetOpusAudioBuffer res1 = %d res2 = %d", res1, res2);
788 
789  fSubPeriodBytesSize = (fCompressedMaxSizeByte + CDO) / fNumPackets;
790  fLastSubPeriodBytesSize = fSubPeriodBytesSize + (fCompressedMaxSizeByte + CDO) % fNumPackets;
791 
792  if (fNumPackets == 1) {
793  fSubPeriodBytesSize = fLastSubPeriodBytesSize;
794  }
795 
796  jack_log("NetOpusAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
797 
798  fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
799  fCycleBytesSize = params->fMtu * fNumPackets;
800 
801  fLastSubCycle = -1;
802  return;
803  }
804 
805  error:
806 
807  FreeOpus();
808  throw std::bad_alloc();
809  }
810 
811  NetOpusAudioBuffer::~NetOpusAudioBuffer()
812  {
813  FreeOpus();
814 
815  for (int port_index = 0; port_index < fNPorts; port_index++) {
816  delete [] fCompressedBuffer[port_index];
817  }
818 
819  delete [] fCompressedBuffer;
820  delete [] fCompressedSizesByte;
821  }
822 
823  void NetOpusAudioBuffer::FreeOpus()
824  {
825  for (int i = 0; i < fNPorts; i++) {
826  if (fOpusEncoder[i]) {
827  opus_custom_encoder_destroy(fOpusEncoder[i]);
828  fOpusEncoder[i] = 0;
829  }
830  if (fOpusDecoder[i]) {
831  opus_custom_decoder_destroy(fOpusDecoder[i]);
832  fOpusDecoder[i] = 0;
833  }
834  if (fOpusMode[i]) {
835  opus_custom_mode_destroy(fOpusMode[i]);
836  fOpusMode[i] = 0;
837  }
838  }
839 
840  delete [] fOpusEncoder;
841  delete [] fOpusDecoder;
842  delete [] fOpusMode;
843  }
844 
845  size_t NetOpusAudioBuffer::GetCycleSize()
846  {
847  return fCycleBytesSize;
848  }
849 
850  float NetOpusAudioBuffer::GetCycleDuration()
851  {
852  return fCycleDuration;
853  }
854 
855  int NetOpusAudioBuffer::GetNumPackets(int active_ports)
856  {
857  return fNumPackets;
858  }
859 
860  int NetOpusAudioBuffer::RenderFromJackPorts(int nframes)
861  {
862  float buffer[BUFFER_SIZE_MAX];
863 
864  for (int port_index = 0; port_index < fNPorts; port_index++) {
865  if (fPortBuffer[port_index]) {
866  memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
867  } else {
868  memset(buffer, 0, fPeriodSize * sizeof(sample_t));
869  }
870  int res = opus_custom_encode_float(fOpusEncoder[port_index], buffer, ((nframes == -1) ? fPeriodSize : nframes), fCompressedBuffer[port_index], fCompressedMaxSizeByte);
871  if (res < 0 || res >= 65535) {
872  jack_error("opus_custom_encode_float error res = %d", res);
873  fCompressedSizesByte[port_index] = 0;
874  } else {
875  fCompressedSizesByte[port_index] = res;
876  }
877  }
878 
879  // All ports active
880  return fNPorts;
881  }
882 
883  void NetOpusAudioBuffer::RenderToJackPorts(int nframes)
884  {
885  for (int port_index = 0; port_index < fNPorts; port_index++) {
886  if (fPortBuffer[port_index]) {
887  int res = opus_custom_decode_float(fOpusDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizesByte[port_index], fPortBuffer[port_index], ((nframes == -1) ? fPeriodSize : nframes));
888  if (res < 0 || res != ((nframes == -1) ? fPeriodSize : nframes)) {
889  jack_error("opus_custom_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizesByte[port_index], res);
890  }
891  }
892  }
893 
894  NextCycle();
895  }
896 
897  //network<->buffer
898  int NetOpusAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
899  {
900  // Cleanup all JACK ports at the beginning of the cycle
901  if (sub_cycle == 0) {
902  Cleanup();
903  }
904 
905  if (port_num > 0) {
906  if (sub_cycle == 0) {
907  for (int port_index = 0; port_index < fNPorts; port_index++) {
908  size_t len = *((size_t*)(fNetBuffer + port_index * fSubPeriodBytesSize));
909  fCompressedSizesByte[port_index] = ntohs(len);
910  memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + CDO + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize - CDO);
911  }
912  } else if (sub_cycle == fNumPackets - 1) {
913  for (int port_index = 0; port_index < fNPorts; port_index++) {
914  memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
915  }
916  } else {
917  for (int port_index = 0; port_index < fNPorts; port_index++) {
918  memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
919  }
920  }
921  }
922 
923  return CheckPacket(cycle, sub_cycle);
924  }
925 
926  int NetOpusAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
927  {
928  if (sub_cycle == 0) {
929  for (int port_index = 0; port_index < fNPorts; port_index++) {
930  unsigned short len = htons(fCompressedSizesByte[port_index]);
931  memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, &len, CDO);
932  memcpy(fNetBuffer + port_index * fSubPeriodBytesSize + CDO, fCompressedBuffer[port_index], fSubPeriodBytesSize - CDO);
933  }
934  return fNPorts * fSubPeriodBytesSize;
935  } else if (sub_cycle == fNumPackets - 1) {
936  for (int port_index = 0; port_index < fNPorts; port_index++) {
937  memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fLastSubPeriodBytesSize);
938  }
939  return fNPorts * fLastSubPeriodBytesSize;
940  } else {
941  for (int port_index = 0; port_index < fNPorts; port_index++) {
942  memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize - CDO, fSubPeriodBytesSize);
943  }
944  return fNPorts * fSubPeriodBytesSize;
945  }
946  }
947 
948 #endif
949 
950  NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
951  : NetAudioBuffer(params, nports, net_buffer)
952  {
953  fPeriodSize = params->fPeriodSize;
954 
955  fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
956  jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
957 
958  fIntBuffer = new short* [fNPorts];
959  for (int port_index = 0; port_index < fNPorts; port_index++) {
960  fIntBuffer[port_index] = new short[fPeriodSize];
961  memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
962  }
963 
964  int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
965  int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
966 
967  jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1, res2);
968 
969  fNumPackets = (res1) ? (res2 + 1) : res2;
970 
971  fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
972  fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
973 
974  fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
975 
976  jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
977 
978  fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
979  fCycleBytesSize = params->fMtu * fNumPackets;
980 
981  fLastSubCycle = -1;
982  }
983 
984  NetIntAudioBuffer::~NetIntAudioBuffer()
985  {
986  for (int port_index = 0; port_index < fNPorts; port_index++) {
987  delete [] fIntBuffer[port_index];
988  }
989 
990  delete [] fIntBuffer;
991  }
992 
993  size_t NetIntAudioBuffer::GetCycleSize()
994  {
995  return fCycleBytesSize;
996  }
997 
998  float NetIntAudioBuffer::GetCycleDuration()
999  {
1000  return fCycleDuration;
1001  }
1002 
1003  int NetIntAudioBuffer::GetNumPackets(int active_ports)
1004  {
1005  return fNumPackets;
1006  }
1007 
1008  int NetIntAudioBuffer::RenderFromJackPorts(int nframes)
1009  {
1010  for (int port_index = 0; port_index < fNPorts; port_index++) {
1011  if (fPortBuffer[port_index]) {
1012  for (int frame = 0; frame < nframes; frame++) {
1013  fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32767.f);
1014  }
1015  } else {
1016  memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
1017  }
1018  }
1019 
1020  // All ports active
1021  return fNPorts;
1022  }
1023 
1024  void NetIntAudioBuffer::RenderToJackPorts(int nframes)
1025  {
1026  float coef = 1.f / 32767.f;
1027  for (int port_index = 0; port_index < fNPorts; port_index++) {
1028  if (fPortBuffer[port_index]) {
1029  for (int frame = 0; frame < nframes; frame++) {
1030  fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
1031  }
1032  }
1033  }
1034 
1035  NextCycle();
1036  }
1037 
1038  //network<->buffer
1039  int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
1040  {
1041  // Cleanup all JACK ports at the beginning of the cycle
1042  if (sub_cycle == 0) {
1043  Cleanup();
1044  }
1045 
1046  if (port_num > 0) {
1047  int sub_period_bytes_size;
1048 
1049  // Last packet
1050  if (sub_cycle == fNumPackets - 1) {
1051  sub_period_bytes_size = fLastSubPeriodBytesSize;
1052  } else {
1053  sub_period_bytes_size = fSubPeriodBytesSize;
1054  }
1055 
1056  for (int port_index = 0; port_index < fNPorts; port_index++) {
1057  memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * sub_period_bytes_size, sub_period_bytes_size);
1058  }
1059  }
1060 
1061  return CheckPacket(cycle, sub_cycle);
1062  }
1063 
1064  int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
1065  {
1066  int sub_period_bytes_size;
1067 
1068  // Last packet
1069  if (sub_cycle == fNumPackets - 1) {
1070  sub_period_bytes_size = fLastSubPeriodBytesSize;
1071  } else {
1072  sub_period_bytes_size = fSubPeriodBytesSize;
1073  }
1074 
1075  for (int port_index = 0; port_index < fNPorts; port_index++) {
1076  memcpy(fNetBuffer + port_index * sub_period_bytes_size, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, sub_period_bytes_size);
1077  }
1078  return fNPorts * sub_period_bytes_size;
1079  }
1080 
1081 // SessionParams ************************************************************************************
1082 
1083  SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
1084  {
1085  memcpy(dst_params, src_params, sizeof(session_params_t));
1086  dst_params->fProtocolVersion = htonl(src_params->fProtocolVersion);
1087  dst_params->fPacketID = htonl(src_params->fPacketID);
1088  dst_params->fMtu = htonl(src_params->fMtu);
1089  dst_params->fID = htonl(src_params->fID);
1090  dst_params->fTransportSync = htonl(src_params->fTransportSync);
1091  dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
1092  dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
1093  dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
1094  dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
1095  dst_params->fSampleRate = htonl(src_params->fSampleRate);
1096  dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
1097  dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
1098  dst_params->fKBps = htonl(src_params->fKBps);
1099  dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
1100  dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
1101  }
1102 
1103  SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
1104  {
1105  memcpy(dst_params, src_params, sizeof(session_params_t));
1106  dst_params->fProtocolVersion = ntohl(src_params->fProtocolVersion);
1107  dst_params->fPacketID = ntohl(src_params->fPacketID);
1108  dst_params->fMtu = ntohl(src_params->fMtu);
1109  dst_params->fID = ntohl(src_params->fID);
1110  dst_params->fTransportSync = ntohl(src_params->fTransportSync);
1111  dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
1112  dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
1113  dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
1114  dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
1115  dst_params->fSampleRate = ntohl(src_params->fSampleRate);
1116  dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
1117  dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
1118  dst_params->fKBps = ntohl(src_params->fKBps);
1119  dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
1120  dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
1121  }
1122 
1123  SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
1124  {
1125  char encoder[16];
1126  switch (params->fSampleEncoder)
1127  {
1128  case JackFloatEncoder:
1129  strcpy(encoder, "float");
1130  break;
1131  case JackIntEncoder:
1132  strcpy(encoder, "integer");
1133  break;
1134  case JackCeltEncoder:
1135  strcpy(encoder, "CELT");
1136  break;
1137  case JackOpusEncoder:
1138  strcpy(encoder, "OPUS");
1139  break;
1140  }
1141 
1142  jack_info("**************** Network parameters ****************");
1143  jack_info("Name : %s", params->fName);
1144  jack_info("Protocol revision : %d", params->fProtocolVersion);
1145  jack_info("MTU : %u", params->fMtu);
1146  jack_info("Master name : %s", params->fMasterNetName);
1147  jack_info("Slave name : %s", params->fSlaveNetName);
1148  jack_info("ID : %u", params->fID);
1149  jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
1150  jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
1151  jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
1152  jack_info("Sample rate : %u frames per second", params->fSampleRate);
1153  jack_info("Period size : %u frames per period", params->fPeriodSize);
1154  jack_info("Network latency : %u cycles", params->fNetworkLatency);
1155  switch (params->fSampleEncoder) {
1156  case (JackFloatEncoder):
1157  jack_info("SampleEncoder : %s", "Float");
1158  break;
1159  case (JackIntEncoder):
1160  jack_info("SampleEncoder : %s", "16 bits integer");
1161  break;
1162  case (JackCeltEncoder):
1163  jack_info("SampleEncoder : %s", "CELT");
1164  jack_info("kBits : %d", params->fKBps);
1165  break;
1166  case (JackOpusEncoder):
1167  jack_info("SampleEncoder : %s", "OPUS");
1168  jack_info("kBits : %d", params->fKBps);
1169  break;
1170  };
1171  jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
1172  jack_info("****************************************************");
1173  }
1174 
1175  SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
1176  {
1177  switch (params->fPacketID)
1178  {
1179  case 0:
1180  return SLAVE_AVAILABLE;
1181  case 1:
1182  return SLAVE_SETUP;
1183  case 2:
1184  return START_MASTER;
1185  case 3:
1186  return START_SLAVE;
1187  case 4:
1188  return KILL_MASTER;
1189  }
1190  return INVALID;
1191  }
1192 
1193  SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
1194  {
1195  switch (packet_type)
1196  {
1197  case INVALID:
1198  return -1;
1199  case SLAVE_AVAILABLE:
1200  params->fPacketID = 0;
1201  break;
1202  case SLAVE_SETUP:
1203  params->fPacketID = 1;
1204  break;
1205  case START_MASTER:
1206  params->fPacketID = 2;
1207  break;
1208  case START_SLAVE:
1209  params->fPacketID = 3;
1210  break;
1211  case KILL_MASTER:
1212  params->fPacketID = 4;
1213  }
1214  return 0;
1215  }
1216 
1217 // Packet header **********************************************************************************
1218 
1219  SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
1220  {
1221  memcpy(dst_header, src_header, sizeof(packet_header_t));
1222  dst_header->fDataType = htonl(src_header->fDataType);
1223  dst_header->fDataStream = htonl(src_header->fDataStream);
1224  dst_header->fID = htonl(src_header->fID);
1225  dst_header->fNumPacket = htonl(src_header->fNumPacket);
1226  dst_header->fPacketSize = htonl(src_header->fPacketSize);
1227  dst_header->fActivePorts = htonl(src_header->fActivePorts);
1228  dst_header->fCycle = htonl(src_header->fCycle);
1229  dst_header->fSubCycle = htonl(src_header->fSubCycle);
1230  dst_header->fFrames = htonl(src_header->fFrames);
1231  dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
1232  }
1233 
1234  SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
1235  {
1236  memcpy(dst_header, src_header, sizeof(packet_header_t));
1237  dst_header->fDataType = ntohl(src_header->fDataType);
1238  dst_header->fDataStream = ntohl(src_header->fDataStream);
1239  dst_header->fID = ntohl(src_header->fID);
1240  dst_header->fNumPacket = ntohl(src_header->fNumPacket);
1241  dst_header->fPacketSize = ntohl(src_header->fPacketSize);
1242  dst_header->fActivePorts = ntohl(src_header->fActivePorts);
1243  dst_header->fCycle = ntohl(src_header->fCycle);
1244  dst_header->fSubCycle = ntohl(src_header->fSubCycle);
1245  dst_header->fFrames = ntohl(src_header->fFrames);
1246  dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
1247  }
1248 
1249  SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
1250  {
1251  jack_info("********************Header********************");
1252  jack_info("Data type : %c", header->fDataType);
1253  jack_info("Data stream : %c", header->fDataStream);
1254  jack_info("ID : %u", header->fID);
1255  jack_info("Cycle : %u", header->fCycle);
1256  jack_info("SubCycle : %u", header->fSubCycle);
1257  jack_info("Active ports : %u", header->fActivePorts);
1258  jack_info("DATA packets : %u", header->fNumPacket);
1259  jack_info("DATA size : %u", header->fPacketSize);
1260  jack_info("DATA frames : %d", header->fFrames);
1261  jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
1262  jack_info("**********************************************");
1263  }
1264 
1265  SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
1266  {
1267  jack_info("********************Network Transport********************");
1268  jack_info("Transport new state : %u", data->fNewState);
1269  jack_info("Transport timebase master : %u", data->fTimebaseMaster);
1270  jack_info("Transport cycle state : %u", data->fState);
1271  jack_info("**********************************************");
1272  }
1273 
1274  SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
1275  {
1276  dst_buffer->magic = htonl(src_buffer->magic);
1277  dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
1278  dst_buffer->nframes = htonl(src_buffer->nframes);
1279  dst_buffer->write_pos = htonl(src_buffer->write_pos);
1280  dst_buffer->event_count = htonl(src_buffer->event_count);
1281  dst_buffer->lost_events = htonl(src_buffer->lost_events);
1282  }
1283 
1284  SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
1285  {
1286  dst_buffer->magic = ntohl(src_buffer->magic);
1287  dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
1288  dst_buffer->nframes = ntohl(src_buffer->nframes);
1289  dst_buffer->write_pos = ntohl(src_buffer->write_pos);
1290  dst_buffer->event_count = ntohl(src_buffer->event_count);
1291  dst_buffer->lost_events = ntohl(src_buffer->lost_events);
1292  }
1293 
1294  SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
1295  {
1296  dst_params->fNewState = htonl(src_params->fNewState);
1297  dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
1298  dst_params->fState = htonl(src_params->fState);
1299  dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
1300  dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
1301  dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
1302  dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
1303  dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
1304  dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
1305  dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
1306  dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
1307  dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
1308  dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
1309  dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
1310  dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
1311  dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
1312  dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
1313  dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
1314  dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
1315  dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
1316  dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
1317  dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
1318  }
1319 
1320  SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
1321  {
1322  dst_params->fNewState = ntohl(src_params->fNewState);
1323  dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
1324  dst_params->fState = ntohl(src_params->fState);
1325  dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
1326  dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
1327  dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
1328  dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
1329  dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
1330  dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
1331  dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
1332  dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
1333  dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
1334  dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
1335  dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
1336  dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
1337  dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
1338  dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
1339  dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
1340  dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
1341  dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
1342  dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
1343  dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
1344  }
1345 
1346 // Utility *******************************************************************************************************
1347 
1348  SERVER_EXPORT int SocketAPIInit()
1349  {
1350 #ifdef WIN32
1351  WORD wVersionRequested = MAKEWORD(2, 2);
1352  WSADATA wsaData;
1353 
1354  if (WSAStartup(wVersionRequested, &wsaData) != 0) {
1355  jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
1356  return -1;
1357  }
1358 
1359  if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
1360  jack_error("Could not find a useable version of Winsock.dll\n");
1361  WSACleanup();
1362  return -1;
1363  }
1364 #endif
1365  return 0;
1366  }
1367 
1368  SERVER_EXPORT int SocketAPIEnd()
1369  {
1370 #ifdef WIN32
1371  return WSACleanup();
1372 #endif
1373  return 0;
1374  }
1375 
1376  SERVER_EXPORT const char* GetTransportState(int transport_state)
1377  {
1378  switch (transport_state)
1379  {
1380  case JackTransportRolling:
1381  return "rolling";
1382  case JackTransportStarting:
1383  return "starting";
1384  case JackTransportStopped:
1385  return "stopped";
1386  case JackTransportNetStarting:
1387  return "netstarting";
1388  }
1389  return NULL;
1390  }
1391 }