Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions libraries/networking/src/webrtc/WebRTCDataChannels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

#include <QJsonDocument>
#include <QJsonObject>
#include <QProcessEnvironment>

#include <rtc_base/ssl_adapter.h>

#include "../NetworkLogging.h"

Expand All @@ -20,10 +23,11 @@
// - https://webrtc.github.io/webrtc-org/native-code/native-apis/
// - https://webrtc.googlesource.com/src/+/master/api/peer_connection_interface.h

const std::string ICE_SERVER_URI = "stun://ice.vircadia.com:7337";
//const std::string ICE_SERVER_URI = "stun:ice.vircadia.com:7337";
const std::string ICE_SERVER_URI = "stun1.l.google.com:19302";
const int MAX_WEBRTC_BUFFER_SIZE = 16777216; // 16MB

// #define WEBRTC_DEBUG
#define WEBRTC_DEBUG

using namespace webrtc;

Expand Down Expand Up @@ -388,6 +392,16 @@ WebRTCDataChannels::WebRTCDataChannels(QObject* parent) :

// Create a peer connection factory.
#ifdef WEBRTC_DEBUG
// Report environment variable values.
qCDebug(networking_webrtc) << "VIRCADIA_WEBRTC_STUN:"
<< QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_STUN");
qCDebug(networking_webrtc) << "VIRCADIA_WEBRTC_INITSSL:"
<< QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_INITSSL");
qCDebug(networking_webrtc) << "VIRCADIA_WEBRTC_CRYPTO:"
<< QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_CRYPTO");
qCDebug(networking_webrtc) << "VIRCADIA_WEBRTC_SCTP:"
<< QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_SCTP");

// Numbers are per WebRTC's peer_connection_interface.h.
qCDebug(networking_webrtc) << "1. Create a new PeerConnectionFactoryInterface";
#endif
Expand All @@ -401,8 +415,30 @@ WebRTCDataChannels::WebRTCDataChannels(QObject* parent) :
dependencies.network_thread = _rtcNetworkThread.get();
dependencies.worker_thread = _rtcWorkerThread.get();
dependencies.signaling_thread = _rtcSignalingThread.get();

auto initializeSSL = QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_INITSSL", "0") == "1";
if (initializeSSL) {
#ifdef WEBRTC_DEBUG
qCDebug(networking_webrtc) << "InitializeSSL()";
#endif
rtc::InitializeSSL();
}

_peerConnectionFactory = CreateModularPeerConnectionFactory(std::move(dependencies));
if (!_peerConnectionFactory) {
if (_peerConnectionFactory) {

auto enableEncryption = QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_CRYPTO", "1") == "1";
auto enableSCTP = QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_SCTP", "1") == "1";
#ifdef WEBRTC_DEBUG
qCDebug(networking_webrtc) << "Peer connection encryption enabled =" << enableEncryption;
qCDebug(networking_webrtc) << "SCTP enabled =" << enableSCTP;
#endif
PeerConnectionFactoryInterface::Options pc_options;
pc_options.disable_encryption = !enableEncryption;
pc_options.disable_sctp_data_channels = !enableSCTP;

_peerConnectionFactory->SetOptions(pc_options);
} else {
qCWarning(networking_webrtc) << "Failed to create WebRTC peer connection factory";
}

Expand All @@ -415,6 +451,15 @@ WebRTCDataChannels::~WebRTCDataChannels() {
qCDebug(networking_webrtc) << "WebRTCDataChannels::~WebRTCDataChannels()";
#endif
reset();

auto initializeSSL = QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_INITSSL", "1") == "1";
if (initializeSSL) {
#ifdef WEBRTC_DEBUG
qCDebug(networking_webrtc) << "CleanupSSL()";
#endif
rtc::CleanupSSL();
}

_peerConnectionFactory = nullptr;
_rtcSignalingThread->Stop();
_rtcSignalingThread = nullptr;
Expand Down Expand Up @@ -546,7 +591,17 @@ rtc::scoped_refptr<PeerConnectionInterface> WebRTCDataChannels::createPeerConnec

PeerConnectionInterface::RTCConfiguration configuration;
PeerConnectionInterface::IceServer iceServer;
iceServer.uri = ICE_SERVER_URI;

auto stunURI = QProcessEnvironment::systemEnvironment().value("VIRCADIA_WEBRTC_STUN", "");
if (!stunURI.isEmpty()) {
iceServer.uri = stunURI.toStdString();
} else {
iceServer.uri = ICE_SERVER_URI;
}
#ifdef WEBRTC_DEBUG
qCDebug(networking_webrtc) << "Using ICE server:" << QString::fromStdString(iceServer.uri);
#endif

configuration.servers.push_back(iceServer);

#ifdef WEBRTC_DEBUG
Expand Down