summary refs log tree commit diff
diff options
context:
space:
mode:
authortrilene <trilene@runbox.com>2020-07-30 19:59:54 -0400
committertrilene <trilene@runbox.com>2020-07-30 19:59:54 -0400
commitf14d141cb529c42a9e39f1966b7424c84f0cc92d (patch)
tree75ef6a8d530639e458944e2d5ac753047e819b22
parentFix error message (diff)
downloadnheko-f14d141cb529c42a9e39f1966b7424c84f0cc92d.tar.xz
Improve TURN server retrieval
-rw-r--r--src/CallManager.cpp17
-rw-r--r--src/CallManager.h1
-rw-r--r--src/ChatPage.cpp2
3 files changed, 17 insertions, 3 deletions
diff --git a/src/CallManager.cpp b/src/CallManager.cpp
index 3ddcc227..cbfd5135 100644
--- a/src/CallManager.cpp
+++ b/src/CallManager.cpp
@@ -1,4 +1,6 @@
+#include <algorithm>
 #include <cctype>
+#include <cstdint>
 #include <chrono>
 
 #include <QMediaPlaylist>
@@ -74,21 +76,23 @@ CallManager::CallManager(QSharedPointer<UserSettings> userSettings)
             });
 
   connect(&turnServerTimer_, &QTimer::timeout, this, &CallManager::retrieveTurnServer);
-  turnServerTimer_.start(2000);
 
   connect(this, &CallManager::turnServerRetrieved, this,
       [this](const mtx::responses::TurnServer &res)
             {
               nhlog::net()->info("TURN server(s) retrieved from homeserver:");
               nhlog::net()->info("username: {}", res.username);
-              nhlog::net()->info("ttl: {}", res.ttl);
+              nhlog::net()->info("ttl: {} seconds", res.ttl);
               for (const auto &u : res.uris)
                 nhlog::net()->info("uri: {}", u);
 
               // Request new credentials close to expiry
               // See https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
               turnURIs_ = getTurnURIs(res);
-              turnServerTimer_.setInterval(res.ttl * 1000 * 0.9);
+              uint32_t ttl = std::max(res.ttl, UINT32_C(3600));
+              if (res.ttl < 3600)
+                nhlog::net()->warn("Setting ttl to 1 hour");
+              turnServerTimer_.setInterval(ttl * 1000 * 0.9);
       });
 
   connect(&session_, &WebRTCSession::stateChanged, this,
@@ -350,6 +354,13 @@ CallManager::endCall()
 }
 
 void
+CallManager::refreshTurnServer()
+{
+  turnURIs_.clear();
+  turnServerTimer_.start(2000);
+}
+
+void
 CallManager::retrieveTurnServer()
 {
   http::client()->get_turn_server(
diff --git a/src/CallManager.h b/src/CallManager.h
index 6518fd13..4ed6e4c7 100644
--- a/src/CallManager.h
+++ b/src/CallManager.h
@@ -29,6 +29,7 @@ public:
         void sendInvite(const QString &roomid);
         void hangUp(mtx::events::msg::CallHangUp::Reason = mtx::events::msg::CallHangUp::Reason::User);
         bool onActiveCall();
+        void refreshTurnServer();
 
 public slots:
         void syncEvent(const mtx::events::collections::TimelineEvents &event);
diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp
index 620c977e..5ab617fa 100644
--- a/src/ChatPage.cpp
+++ b/src/ChatPage.cpp
@@ -723,6 +723,8 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
                 const bool isInitialized = cache::isInitialized();
                 const auto cacheVersion  = cache::formatVersion();
 
+                callManager_.refreshTurnServer();
+
                 if (!isInitialized) {
                         cache::setCurrentFormat();
                 } else {