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