This repository has been archived on 2026-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2019-04-08 13:55:15 +02:00

103 lines
3.6 KiB
C++

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Michael Zanetti <michael.zanetti@guh.io> *
* *
* This file is part of nymea. *
* *
* nymea 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, version 2 of the License. *
* *
* nymea 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 nymea. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef JANUSCONNECTOR_H
#define JANUSCONNECTOR_H
#include <QObject>
#include <QLocalSocket>
#include <QTimer>
#include <QDateTime>
#include <QTcpServer>
class JanusConnector : public QObject
{
Q_OBJECT
public:
class WebRtcSession {
public:
QString sessionId; // This should be unique but persistent for each remote device
qint64 janusSessionId = -1; // the session id for the janus session.
qint64 janusChannelId = -1;
bool connectedToJanus = false;
QVariantMap offer;
bool offerSent = false;
QVariantList trickles;
QVariantMap webRtcUp;
bool webRtcConnected = false;
bool matchJanusSessionId(qint64 id) {
return (janusSessionId == id) || (janusSessionId == id +1) || (janusSessionId == id -1);
}
};
explicit JanusConnector(QObject *parent = nullptr);
void sendWebRtcHandshakeMessage(const QString &sessionId, const QVariantMap &message);
bool sendKeepAliveMessage(const QString &sessionId);
void setTurnCredentials(const QVariantMap &turnCredentials);
signals:
void connected();
void webRtcHandshakeMessageReceived(const QString &sessionId, const QVariantMap &message);
void requestTURNCredentials();
private slots:
void onDisconnected();
void onError(QLocalSocket::LocalSocketError socketError);
void onReadyRead();
void heartbeat();
void processQueue();
void newTurnServerConnection();
private:
QHash<QString, WebRtcSession*> m_pendingRequests;
bool connectToJanus();
void disconnectFromJanus();
void createSession(WebRtcSession *session);
void createChannel(WebRtcSession *session);
void writeToJanus(const QByteArray &data);
private:
QLocalSocket *m_socket = nullptr;
QDateTime m_lastUnconfirmedPing;
QTimer m_pingTimer;
QHash<QString, WebRtcSession*> m_sessions;
QStringList m_wantedAcks;
QTcpServer *m_turnCredentialsServer = nullptr;
QList<QTcpSocket*> m_pendingTurnCredentialRequests;
};
QDebug operator<<(QDebug debug, const JanusConnector::WebRtcSession &session);
QDebug operator<<(QDebug debug, JanusConnector::WebRtcSession *session);
#endif // JANUSCONNECTOR_H