Jack2 1.9.12

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