wfview/udphandler.h

314 wiersze
6.7 KiB
C
Czysty Zwykły widok Historia

2021-02-03 20:00:40 +00:00
#ifndef UDPHANDLER_H
#define UDPHANDLER_H
#include <QObject>
#include <QUdpSocket>
#include <QNetworkDatagram>
#include <QHostInfo>
#include <QTimer>
#include <QMutex>
#include <QDateTime>
2021-02-11 19:18:35 +00:00
#include <QByteArray>
2021-02-22 22:25:09 +00:00
#include <QVector>
2021-02-03 20:00:40 +00:00
// Allow easy endian-ness conversions
#include <QtEndian>
// Needed for audio
#include <QtMultimedia/QAudioOutput>
2021-02-03 20:00:40 +00:00
#include <QBuffer>
2021-02-08 08:31:48 +00:00
#include <QThread>
2021-02-03 20:00:40 +00:00
#include <QDebug>
2021-02-11 19:18:35 +00:00
#include "audiohandler.h"
#include "packettypes.h"
2021-02-08 08:31:48 +00:00
#define PURGE_SECONDS 10
#define TOKEN_RENEWAL 60000
#define PING_PERIOD 100
#define IDLE_PERIOD 100
2021-02-25 18:05:01 +00:00
#define TXAUDIO_PERIOD 10
#define AREYOUTHERE_PERIOD 500
#define WATCHDOG_PERIOD 500
#define RETRANSMIT_PERIOD 100
struct udpPreferences {
QString ipAddress;
quint16 controlLANPort;
quint16 serialLANPort;
quint16 audioLANPort;
QString username;
QString password;
QString audioOutput;
QString audioInput;
quint16 audioRXLatency;
quint16 audioTXLatency;
quint16 audioRXSampleRate;
quint8 audioRXCodec;
quint16 audioTXSampleRate;
quint8 audioTXCodec;
2021-03-09 17:22:16 +00:00
quint8 resampleQuality;
2021-03-27 16:07:17 +00:00
QString clientName;
};
void passcode(QString in, QByteArray& out);
QByteArray parseNullTerminatedString(QByteArray c, int s);
2021-02-03 20:00:40 +00:00
// Parent class that contains all common items.
class udpBase : public QObject
{
2021-02-03 20:00:40 +00:00
public:
2021-02-04 20:09:09 +00:00
~udpBase();
void init();
void dataReceived(QByteArray r);
2021-02-22 22:25:09 +00:00
void sendPing();
2021-02-24 11:00:23 +00:00
void sendRetransmitRange(quint16 first, quint16 second, quint16 third,quint16 fourth);
2021-02-22 22:25:09 +00:00
void sendControl(bool tracked,quint8 id, quint16 seq);
2021-02-03 20:00:40 +00:00
QTime timeStarted;
QUdpSocket* udp=Q_NULLPTR;
uint32_t myId = 0;
uint32_t remoteId = 0;
uint8_t authSeq = 0x00;
//uint16_t innerSendSeq = 0x8304; // Not sure why?
2021-02-03 20:00:40 +00:00
uint16_t sendSeqB = 0;
uint16_t sendSeq = 1;
2021-02-22 22:25:09 +00:00
uint16_t lastReceivedSeq = 1;
2021-02-03 20:00:40 +00:00
uint16_t pkt0SendSeq = 0;
uint16_t periodicSeq = 0;
quint64 latency = 0;
2021-02-03 20:00:40 +00:00
QString username = "";
QString password = "";
QHostAddress radioIP;
QHostAddress localIP;
bool isAuthenticated = false;
quint16 localPort=0;
quint16 port=0;
2021-02-03 20:00:40 +00:00
bool periodicRunning = false;
bool sentPacketConnect2 = false;
QTime lastReceived =QTime::currentTime();
2021-03-03 09:50:15 +00:00
QMutex udpMutex;
QMutex txBufferMutex;
QMutex rxBufferMutex;
2021-02-03 20:00:40 +00:00
struct SEQBUFENTRY {
QTime timeSent;
2021-02-03 20:00:40 +00:00
uint16_t seqNum;
QByteArray data;
2021-02-23 20:48:53 +00:00
quint8 retransmitCount;
2021-02-03 20:00:40 +00:00
};
QVector<SEQBUFENTRY> txSeqBuf;
2021-02-22 22:25:09 +00:00
QVector<quint16> rxSeqBuf;
QVector<SEQBUFENTRY> rxMissing;
2021-02-03 20:00:40 +00:00
void sendTrackedPacket(QByteArray d);
void purgeOldEntries();
QTimer* areYouThereTimer = Q_NULLPTR; // Send are-you-there packets every second until a response is received.
QTimer* pingTimer = Q_NULLPTR; // Start sending pings immediately.
QTimer* idleTimer = Q_NULLPTR; // Start watchdog once we are connected.
QTimer* watchdogTimer = Q_NULLPTR;
QTimer* retransmitTimer = Q_NULLPTR;
QDateTime lastPingSentTime;
uint16_t pingSendSeq = 0;
quint16 areYouThereCounter=0;
quint32 packetsSent=0;
quint32 packetsLost=0;
2021-03-22 09:10:03 +00:00
quint16 seqPrefix = 0;
2021-03-14 08:44:30 +00:00
private:
void sendRetransmitRequest();
2021-02-03 20:00:40 +00:00
};
// Class for all (pseudo) serial communications
class udpCivData : public udpBase
2021-02-03 20:00:40 +00:00
{
Q_OBJECT
public:
udpCivData(QHostAddress local, QHostAddress ip, quint16 civPort);
~udpCivData();
2021-02-03 20:00:40 +00:00
QMutex serialmutex;
signals:
int receive(QByteArray);
2021-02-03 20:00:40 +00:00
public slots:
void send(QByteArray d);
2021-02-03 20:00:40 +00:00
private:
void watchdog();
void dataReceived();
void sendOpenClose(bool close);
QTimer* startCivDataTimer = Q_NULLPTR;
2021-02-03 20:00:40 +00:00
};
// Class for all audio communications.
class udpAudio : public udpBase
{
Q_OBJECT
public:
2021-03-09 17:22:16 +00:00
udpAudio(QHostAddress local, QHostAddress ip, quint16 aport, quint16 rxlatency, quint16 txlatency, quint16 rxsample, quint8 rxcodec, quint16 txsample, quint8 txcodec, QString outputPort, QString inputPort,quint8 resampleQuality);
~udpAudio();
2021-02-08 08:31:48 +00:00
signals:
2021-03-22 09:10:03 +00:00
void haveAudioData(audioPacket data);
2021-03-22 09:10:03 +00:00
void setupTxAudio(const quint8 samples, const quint8 channels, const quint16 samplerate, const quint16 latency, const bool isUlaw, const bool isInput, QString port, quint8 resampleQuality);
void setupRxAudio(const quint8 samples, const quint8 channels, const quint16 samplerate, const quint16 latency, const bool isUlaw, const bool isInput, QString port, quint8 resampleQuality);
void haveChangeLatency(quint16 value);
void haveSetVolume(unsigned char value);
public slots:
void changeLatency(quint16 value);
void setVolume(unsigned char value);
2021-02-11 19:18:35 +00:00
2021-02-03 20:00:40 +00:00
private:
2021-02-12 23:56:02 +00:00
void sendTxAudio();
void dataReceived();
void watchdog();
2021-02-03 20:00:40 +00:00
QAudioFormat format;
quint16 rxLatency;
quint16 txLatency;
quint16 rxSampleRate;
quint16 txSampleRate;
quint8 rxCodec;
quint8 txCodec;
quint8 rxChannelCount = 1;
bool rxIsUlawCodec = false;
2021-02-12 14:28:55 +00:00
quint8 rxNumSamples = 8;
quint8 txChannelCount = 1;
bool txIsUlawCodec = false;
2021-02-12 14:28:55 +00:00
quint8 txNumSamples = 8;
2021-02-03 20:00:40 +00:00
bool sentPacketConnect2 = false;
uint16_t sendAudioSeq = 0;
2021-03-22 09:10:03 +00:00
audioHandler* rxaudio = Q_NULLPTR;
QThread* rxAudioThread = Q_NULLPTR;
2021-02-08 08:31:48 +00:00
2021-03-22 09:10:03 +00:00
audioHandler* txaudio = Q_NULLPTR;
QThread* txAudioThread = Q_NULLPTR;
QTimer* txAudioTimer=Q_NULLPTR;
2021-02-12 23:56:02 +00:00
2021-02-03 20:00:40 +00:00
};
// Class to handle the connection/disconnection of the radio.
class udpHandler: public udpBase
{
Q_OBJECT
public:
udpHandler(udpPreferences prefs);
2021-02-03 20:00:40 +00:00
~udpHandler();
bool streamOpened = false;
2021-02-03 20:00:40 +00:00
udpCivData* civ = Q_NULLPTR;
udpAudio* audio = Q_NULLPTR;
2021-02-03 20:00:40 +00:00
public slots:
void receiveDataFromUserToRig(QByteArray); // This slot will send data on to
void receiveFromCivStream(QByteArray);
void receiveAudioData(const audioPacket &data);
void changeLatency(quint16 value);
void setVolume(unsigned char value);
void init();
2021-02-03 20:00:40 +00:00
signals:
void haveDataFromPort(QByteArray data); // emit this when we have data, connect to rigcommander
void haveAudioData(audioPacket data); // emit this when we have data, connect to rigcommander
void haveNetworkError(QString, QString);
void haveChangeLatency(quint16 value);
void haveSetVolume(unsigned char value);
void haveNetworkStatus(QString);
2021-02-03 20:00:40 +00:00
private:
void sendAreYouThere();
void dataReceived();
void sendRequestStream();
void sendLogin();
void sendToken(uint8_t magic);
2021-02-03 20:00:40 +00:00
bool gotA8ReplyID = false;
bool gotAuthOK = false;
bool sentPacketLogin = false;
bool sentPacketConnect = false;
bool sentPacketConnect2 = false;
2021-02-07 17:40:38 +00:00
bool radioInUse = false;
quint16 controlPort;
quint16 civPort;
2021-02-13 23:28:28 +00:00
quint16 audioPort;
quint16 rxSampleRate;
quint16 txSampleRate;
quint16 rxLatency;
quint16 txLatency;
quint8 rxCodec;
quint8 txCodec;
QString audioInputPort;
QString audioOutputPort;
2021-03-09 17:22:16 +00:00
quint8 resampleQuality;
quint16 reauthInterval = 60000;
QString devName;
QString compName;
QString audioType;
//QByteArray replyId;
quint16 tokRequest;
quint32 token;
// These are for stream ident info.
char identa;
quint32 identb;
2021-02-21 15:06:27 +00:00
QByteArray usernameEncoded;
QByteArray passwordEncoded;
QTimer* tokenTimer = Q_NULLPTR;
QTimer* areYouThereTimer = Q_NULLPTR;
bool highBandwidthConnection = false;
2021-02-03 20:00:40 +00:00
};
Q_DECLARE_METATYPE(struct audioPacket)
2021-02-03 20:00:40 +00:00
#endif