forked from andrewpalumbo/dudestar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm17codec.cpp
executable file
·494 lines (458 loc) · 12.9 KB
/
m17codec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
Copyright (C) 2019-2021 Doug McLain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <iostream>
#include "m17codec.h"
#define M17CHARACTERS " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/."
#define DEBUG
M17Codec::M17Codec(QString callsign, char module, QString hostname, QString host, int port, bool ipv6, QString modem, QString audioin, QString audioout) :
Codec(callsign, module, hostname, host, port, ipv6, NULL, modem, audioin, audioout),
m_txrate(1)
{
m_modeinfo.callsign = callsign;
m_modeinfo.host = host;
m_modeinfo.port = port;
m_modeinfo.count = 0;
m_modeinfo.frame_number = 0;
m_modeinfo.streamid = 0;
#ifdef Q_OS_WIN
m_txtimerint = 30; // Qt timers on windows seem to be slower than desired value
#else
m_txtimerint = 36;
#endif
}
M17Codec::~M17Codec()
{
}
void M17Codec::decoder_gain_changed(qreal v){
m_c2->set_decode_gain(v);
}
void M17Codec::encode_callsign(uint8_t *callsign)
{
const std::string m17_alphabet(M17CHARACTERS);
char cs[10];
memset(cs, 0, sizeof(cs));
memcpy(cs, callsign, strlen((char *)callsign));
uint64_t encoded = 0;
for(int i = std::strlen((char *)callsign)-1; i >= 0; i--) {
auto pos = m17_alphabet.find(cs[i]);
if (pos == std::string::npos) {
pos = 0;
}
encoded *= 40;
encoded += pos;
}
for (int i=0; i<6; i++) {
callsign[i] = (encoded >> (8*(5-i)) & 0xFFU);
}
}
void M17Codec::decode_callsign(uint8_t *callsign)
{
const std::string m17_alphabet(M17CHARACTERS);
//uint8_t code[6];
uint64_t coded = callsign[0];
for (int i=1; i<6; i++)
coded = (coded << 8) | callsign[i];
if (coded > 0xee6b27ffffffu) {
std::cerr << "Callsign code is too large, 0x" << std::hex << coded << std::endl;
return;
}
//memcpy(code, callsign, 6);
memset(callsign, 0, 10);
int i = 0;
while (coded) {
if(i < 10){
callsign[i++] = m17_alphabet[coded % 40];
}
coded /= 40;
}
}
void M17Codec::decode_c2(int16_t *audio, uint8_t *c)
{
m_c2->codec2_decode(audio, c);
}
void M17Codec::encode_c2(int16_t *audio, uint8_t *c)
{
m_c2->codec2_encode(c, audio);
}
void M17Codec::process_udp()
{
QByteArray buf;
QHostAddress sender;
quint16 senderPort;
buf.resize(m_udp->pendingDatagramSize());
m_udp->readDatagram(buf.data(), buf.size(), &sender, &senderPort);
#ifdef DEBUG
fprintf(stderr, "RECV: ");
for(int i = 0; i < buf.size(); ++i){
fprintf(stderr, "%02x ", (unsigned char)buf.data()[i]);
}
fprintf(stderr, "\n");
fflush(stderr);
#endif
if((m_modeinfo.status != CONNECTED_RW) && (buf.size() == 4) && (::memcmp(buf.data(), "NACK", 4U) == 0)){
m_modeinfo.status = DISCONNECTED;
}
if((buf.size() == 4) && (::memcmp(buf.data(), "ACKN", 4U) == 0)){
if(m_modeinfo.status == CONNECTING){
m_modeinfo.status = CONNECTED_RW;
m_c2 = new CCodec2(true);
m_txtimer = new QTimer();
connect(m_txtimer, SIGNAL(timeout()), this, SLOT(transmit()));
m_rxtimer = new QTimer();
connect(m_rxtimer, SIGNAL(timeout()), this, SLOT(process_rx_data()));
m_ping_timer = new QTimer();
connect(m_ping_timer, SIGNAL(timeout()), this, SLOT(send_ping()));
m_ping_timer->start(8000);
m_audio = new AudioEngine(m_audioin, m_audioout);
m_audio->init();
}
emit update(m_modeinfo);
}
if((buf.size() == 10) && (::memcmp(buf.data(), "PING", 4U) == 0)){
if(m_modeinfo.streamid == 0){
m_modeinfo.stream_state = STREAM_IDLE;
}
m_modeinfo.count++;
emit update(m_modeinfo);
}
if((buf.size() == 54) && (::memcmp(buf.data(), "M17 ", 4U) == 0)){
uint16_t streamid = (buf.data()[4] << 8) | (buf.data()[5] & 0xff);
if( (m_modeinfo.streamid != 0) && (streamid != m_modeinfo.streamid) ){
qDebug() << "New streamid received before timeout";
m_modeinfo.streamid = 0;
m_audio->stop_playback();
}
if( !m_tx && (m_modeinfo.streamid == 0) ){
uint8_t cs[10];
::memcpy(cs, &(buf.data()[12]), 6);
decode_callsign(cs);
m_modeinfo.src = QString((char *)cs);
::memcpy(cs, &(buf.data()[6]), 6);
decode_callsign(cs);
m_modeinfo.dst = QString((char *)cs);
m_modeinfo.streamid = streamid;
m_audio->start_playback();
if((buf.data()[19] & 0x06U) == 0x04U){
m_modeinfo.type = 1;//"3200 Voice";
set_mode(true);
}
else{
m_modeinfo.type = 0;//"1600 V/D";
set_mode(false);
}
if(!m_rxtimer->isActive()){
#ifdef Q_OS_WIN
m_rxtimer->start(m_modeinfo.type ? m_rxtimerint : 32);
#else
m_rxtimer->start(m_modeinfo.type ? m_rxtimerint : m_rxtimerint*2);
#endif
}
m_modeinfo.stream_state = STREAM_NEW;
m_modeinfo.ts = QDateTime::currentMSecsSinceEpoch();
qDebug() << "M17: New stream from " << m_modeinfo.src << " to " << m_modeinfo.dst << " id == " << QString::number(m_modeinfo.streamid, 16);
}
else{
m_modeinfo.stream_state = STREAMING;
}
m_modeinfo.frame_number = (buf.data()[34] << 8) | (buf.data()[35] & 0xff);
m_rxwatchdog = 0;
int s = 8;
if(get_mode()){
s = 16;
}
for(int i = 0; i < s; ++i){
m_rxcodecq.append((uint8_t )buf.data()[36+i]);
}
if(m_modeinfo.frame_number & 0x8000){ // EOT
qDebug() << "M17 stream ended";
m_rxwatchdog = 0;
m_modeinfo.stream_state = STREAM_END;
m_modeinfo.ts = QDateTime::currentMSecsSinceEpoch();
emit update(m_modeinfo);
m_modeinfo.streamid = 0;
}
else{
emit update(m_modeinfo);
}
}
//emit update(m_modeinfo);
}
void M17Codec::hostname_lookup(QHostInfo i)
{
if (!i.addresses().isEmpty()) {
QByteArray out;
uint8_t cs[10];
memset(cs, ' ', 9);
memcpy(cs, m_modeinfo.callsign.toLocal8Bit(), m_modeinfo.callsign.size());
cs[8] = 'D';
cs[9] = 0x00;
M17Codec::encode_callsign(cs);
out.append('C');
out.append('O');
out.append('N');
out.append('N');
out.append((char *)cs, 6);
out.append(m_module);
m_address = i.addresses().first();
//m_address.setAddress("2600:1f16:d6f:600:20eb:5765:b94c:aebb");
qDebug() << "Address == " << m_address.toString();
m_udp = new QUdpSocket(this);
connect(m_udp, SIGNAL(readyRead()), this, SLOT(process_udp()));
m_udp->writeDatagram(out, m_address, m_modeinfo.port);
#ifdef DEBUG
fprintf(stderr, "CONN: ");
for(int i = 0; i < out.size(); ++i){
fprintf(stderr, "%02x ", (unsigned char)out.data()[i]);
}
fprintf(stderr, "\n");
fflush(stderr);
#endif
}
}
void M17Codec::send_ping()
{
QByteArray out;
uint8_t cs[10];
memset(cs, ' ', 9);
memcpy(cs, m_modeinfo.callsign.toLocal8Bit(), m_modeinfo.callsign.size());
cs[8] = 'D';
cs[9] = 0x00;
encode_callsign(cs);
out.append('P');
out.append('O');
out.append('N');
out.append('G');
out.append((char *)cs, 6);
m_udp->writeDatagram(out, m_address, m_modeinfo.port);
#ifdef DEBUG
fprintf(stderr, "PING: ");
for(int i = 0; i < out.size(); ++i){
fprintf(stderr, "%02x ", (unsigned char)out.data()[i]);
}
fprintf(stderr, "\n");
fflush(stderr);
#endif
}
void M17Codec::send_disconnect()
{
QByteArray out;
uint8_t cs[10];
memset(cs, ' ', 9);
memcpy(cs, m_modeinfo.callsign.toLocal8Bit(), m_modeinfo.callsign.size());
cs[8] = 'D';
cs[9] = 0x00;
encode_callsign(cs);
out.append('D');
out.append('I');
out.append('S');
out.append('C');
out.append((char *)cs, 6);
m_udp->writeDatagram(out, m_address, m_modeinfo.port);
#ifdef DEBUG
fprintf(stderr, "SEND: ");
for(int i = 0; i < out.size(); ++i){
fprintf(stderr, "%02x ", (unsigned char)out.data()[i]);
}
fprintf(stderr, "\n");
fflush(stderr);
#endif
}
void M17Codec::toggle_tx(bool tx)
{
tx ? start_tx() : stop_tx();
}
void M17Codec::start_tx()
{
m_c2->codec2_set_mode(m_txrate);
Codec::start_tx();
}
void M17Codec::transmit()
{
QByteArray txframe;
static uint16_t txstreamid = 0;
static uint16_t tx_cnt = 0;
int16_t pcm[320];
uint8_t c2[16];
#ifdef USE_FLITE
static uint16_t ttscnt = 0;
if(m_ttsid > 0){
for(int i = 0; i < 320; ++i){
if(ttscnt >= tts_audio->num_samples/2){
//audiotx_cnt = 0;
pcm[i] = 0;
}
else{
pcm[i] = tts_audio->samples[ttscnt*2] / 2;
ttscnt++;
}
}
m_c2->codec2_encode(c2, pcm);
if(get_mode()){
m_c2->codec2_encode(c2+8, pcm+160);
}
}
#endif
if(m_ttsid == 0){
if(m_audio->read(pcm, 320)){
m_c2->codec2_encode(c2, pcm);
if(get_mode()){
m_c2->codec2_encode(c2+8, pcm+160);
}
}
else{
return;
}
}
txframe.clear();
emit update_output_level(m_audio->level());
int r = get_mode() ? 0x05 : 0x07;
if(m_tx){
if(txstreamid == 0){
txstreamid = static_cast<uint16_t>((::rand() & 0xFFFF));
//std::cerr << "txstreamid == " << txstreamid << std::endl;
}
uint8_t src[10];
uint8_t dst[10];
memset(dst, ' ', 9);
memcpy(dst, m_hostname.toLocal8Bit(), m_hostname.size());
dst[8] = m_module;
dst[9] = 0x00;
encode_callsign(dst);
memset(src, ' ', 9);
memcpy(src, m_modeinfo.callsign.toLocal8Bit(), m_modeinfo.callsign.size());
src[8] = 'D';
src[9] = 0x00;
encode_callsign(src);
txframe.append('M');
txframe.append('1');
txframe.append('7');
txframe.append(' ');
txframe.append(txstreamid >> 8);
txframe.append(txstreamid & 0xff);
txframe.append((char *)dst, 6);
txframe.append((char *)src, 6);
txframe.append('\x00');
txframe.append(r); // Frame type voice only
txframe.append(14, 0x00); //Blank nonce
txframe.append((char)(tx_cnt >> 8));
txframe.append((char)tx_cnt & 0xff);
txframe.append((char *)c2, 16);
txframe.append(2, 0x00);
//QString ss = QString("%1").arg(txstreamid, 4, 16, QChar('0'));
//QString n = QString("TX %1").arg(tx_cnt, 4, 16, QChar('0'));
m_udp->writeDatagram(txframe, m_address, m_modeinfo.port);
++tx_cnt;
m_modeinfo.src = m_modeinfo.callsign;
m_modeinfo.dst = m_hostname;
m_modeinfo.type = get_mode();// ? "3200 Voice" : "1600 V/D";
m_modeinfo.frame_number = tx_cnt;
m_modeinfo.streamid = txstreamid;
emit update(m_modeinfo);
fprintf(stderr, "SEND:%d: ", txframe.size());
for(int i = 0; i < txframe.size(); ++i){
fprintf(stderr, "%02x ", (unsigned char)txframe.data()[i]);
}
fprintf(stderr, "\n");
fflush(stderr);
}
else{
const uint8_t quiet3200[] = { 0x00, 0x01, 0x43, 0x09, 0xe4, 0x9c, 0x08, 0x21 };
const uint8_t quiet1600[] = { 0x01, 0x00, 0x04, 0x00, 0x25, 0x75, 0xdd, 0xf2 };
const uint8_t *quiet = (get_mode()) ? quiet3200 : quiet1600;
uint8_t src[10];
uint8_t dst[10];
memset(dst, ' ', 9);
memcpy(dst, m_hostname.toLocal8Bit(), m_hostname.size());
dst[8] = m_module;
dst[9] = 0x00;
encode_callsign(dst);
memset(src, ' ', 9);
memcpy(src, m_modeinfo.callsign.toLocal8Bit(), m_modeinfo.callsign.size());
src[8] = 'D';
src[9] = 0x00;
M17Codec::encode_callsign(src);
tx_cnt |= 0x8000u;
txframe.append('M');
txframe.append('1');
txframe.append('7');
txframe.append(' ');
txframe.append(txstreamid >> 8);
txframe.append(txstreamid & 0xff);
txframe.append((char *)dst, 6);
txframe.append((char *)src, 6);
txframe.append('\x00');
txframe.append(r); // Frame type voice only
txframe.append(14, 0x00); //Blank nonce
txframe.append((char)(tx_cnt >> 8));
txframe.append((char)tx_cnt & 0xff);
txframe.append((char *)quiet, 8);
txframe.append((char *)quiet, 8);
txframe.append(2, 0x00);
//QString n = QString("%1").arg(tx_cnt, 4, 16, QChar('0'));
m_udp->writeDatagram(txframe, m_address, m_modeinfo.port);
txstreamid = 0;
tx_cnt = 0;
#ifdef USE_FLITE
ttscnt = 0;
#endif
m_txtimer->stop();
if(m_ttsid == 0){
m_audio->stop_capture();
}
m_modeinfo.src = m_modeinfo.callsign;
m_modeinfo.dst = m_hostname;
m_modeinfo.type = get_mode();// ? "3200 Voice" : "1600 V/D";
m_modeinfo.frame_number = tx_cnt;
m_modeinfo.streamid = txstreamid;
emit update(m_modeinfo);
fprintf(stderr, "LAST:%d: ", txframe.size());
for(int i = 0; i < txframe.size(); ++i){
fprintf(stderr, "%02x ", (unsigned char)txframe.data()[i]);
}
fprintf(stderr, "\n");
fflush(stderr);
}
}
void M17Codec::process_rx_data()
{
int16_t pcm[320];
uint8_t codec2[8];
if(m_rxwatchdog++ > 50){
qDebug() << "RX stream timeout ";
m_rxwatchdog = 0;
m_modeinfo.stream_state = STREAM_LOST;
m_modeinfo.ts = QDateTime::currentMSecsSinceEpoch();
emit update(m_modeinfo);
m_modeinfo.streamid = 0;
}
if((!m_tx) && (m_rxcodecq.size() > 7) ){
for(int i = 0; i < 8; ++i){
codec2[i] = m_rxcodecq.dequeue();
}
m_c2->codec2_decode(pcm, codec2);
int s = get_mode() ? 160 : 320;
m_audio->write(pcm, s);
emit update_output_level(m_audio->level());
}
else if ( (m_modeinfo.stream_state == STREAM_END) || (m_modeinfo.stream_state == STREAM_LOST) ){
m_rxtimer->stop();
m_audio->stop_playback();
m_rxwatchdog = 0;
m_modeinfo.streamid = 0;
m_rxcodecq.clear();
qDebug() << "M17 playback stopped";
return;
}
}