Реализация динамического запроса TURN при подключении

This commit is contained in:
RoyceDa
2026-03-17 14:55:53 +02:00
parent ddcb54821f
commit 12a3e6266b
6 changed files with 81 additions and 35 deletions

View File

@@ -28,13 +28,11 @@ public class ForwardUnitService {
private Logger logger;
private Set<SFU> sfuConnections = ConcurrentHashMap.newKeySet();
private ClientManager clientManager;
private Set<RTCIceServer> turnServers = new HashSet<>();
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public ForwardUnitService(Logger logger, ClientManager clientManager) {
this.logger = logger;
this.clientManager = clientManager;
this.readAllTurnServers();
this.sfuConnectionsSheduler();
}
@@ -65,37 +63,6 @@ public class ForwardUnitService {
}, 10, 10, TimeUnit.SECONDS);
}
/**
* Читаем все TURN сервера из переменной окружения и сохраняем их для дальнейшего
* использования при организации звонков через SFU серверы.
*/
private void readAllTurnServers() {
String turnServersEnv = System.getenv("TURN_SERVERS");
if(turnServersEnv == null || turnServersEnv.isEmpty()) {
this.logger.info(Color.YELLOW + "No TURN servers configured, skipping TURN servers boot");
return;
}
String[] turnServers = turnServersEnv.split(",");
for(String turnServer : turnServers) {
String[] parts = turnServer.split("@");
if(parts.length != 2) {
this.logger.error(Color.RED + "Invalid TURN server configuration: " + turnServer);
continue;
}
String address = parts[0];
String credentialsPart = parts[1];
String[] credentialsParts = credentialsPart.split(":");
if(credentialsParts.length != 2) {
this.logger.error(Color.RED + "Invalid TURN server credentials configuration: " + credentialsPart);
continue;
}
String username = credentialsParts[0];
String credential = credentialsParts[1];
RTCIceServer iceServer = new RTCIceServer(address, username, credential);
this.turnServers.add(iceServer);
}
}
/**
* Инициализирует соединения к SFU серверам для звонков.
* Ожидается, что адреса SFU серверов и секретные ключи для них будут переданы через переменную окружения SFU_SERVERS в формате "address1@secretKey1,address2@secretKey2,...".
@@ -239,6 +206,13 @@ public class ForwardUnitService {
* @return список серверов для RTC
*/
public Set<RTCIceServer> getTurnServers() {
Set<RTCIceServer> turnServers = new HashSet<>();
for(SFU sfu : this.sfuConnections) {
RTCIceServer turnServer = sfu.getTurnServer();
if(turnServer != null) {
turnServers.add(turnServer);
}
}
return turnServers;
}