Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class GameSession {
private static final String STUN = "stun";
private static final String TURN = "turn";

private static final List<TransportAddress> PUBLIC_STUN_SERVERS = List.of(
new TransportAddress("stun.cloudflare.com", 3478, Transport.UDP),
new TransportAddress("stun.l.google.com", 19302, Transport.UDP),
new TransportAddress("stun.sipgate.net", 3478, Transport.UDP));

@Getter
private final Map<Integer, Peer> peers = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -104,7 +109,13 @@ public void close() {
* Called by the client via jsonRPC
*/
public static void setIceServers(List<Map<String, Object>> iceServersData) {
GameSession.iceServers.clear();
iceServers.clear();

PUBLIC_STUN_SERVERS.forEach(stunServer -> {
var iceServer = new IceServer();
iceServer.getStunAddresses().add(stunServer);
iceServers.add(iceServer);
});

if (iceServersData.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public PeerIceModule(Peer peer) {

/**
* Updates the current iceState and informs the client via RPC
*
* @param newState the new State
*/
private void setState(IceState newState) {
Expand Down Expand Up @@ -124,16 +125,22 @@ private void createAgent() {
private void gatherCandidates() {
log.info(getLogPrefix() + "Gathering ice candidates");

List<IceServer> iceServers = getViableIceServers();

iceServers.stream()
// For STUN all servers are relevant (latency is not an issue)
GameSession.getIceServers().stream()
.flatMap(s -> s.getStunAddresses().stream())
.map(StunCandidateHarvester::new)
.forEach(agent::addCandidateHarvester);
iceServers.forEach(iceServer -> iceServer.getTurnAddresses().stream()
.map(a -> new TurnCandidateHarvester(
a, new LongTermCredential(iceServer.getTurnUsername(), iceServer.getTurnCredential())))
.forEach(agent::addCandidateHarvester));
.forEach(address -> {
log.info("Add STUN harvester for {}", address.getHostName());
agent.addCandidateHarvester(new StunCandidateHarvester(address));
});

// TURN is latency sensitive
List<IceServer> iceServers = getViableIceServers();
iceServers.forEach(iceServer -> iceServer.getTurnAddresses().forEach(address -> {
var harvester = new TurnCandidateHarvester(
address, new LongTermCredential(iceServer.getTurnUsername(), iceServer.getTurnCredential()));
log.info("Add TURN harvester for {}", address.getHostName());
agent.addCandidateHarvester(harvester);
}));

CompletableFuture<Void> gatheringFuture = CompletableFuture.runAsync(() -> {
try {
Expand Down Expand Up @@ -238,6 +245,7 @@ private List<IceServer> getViableIceServers() {

/**
* Starts harvesting local candidates if in answer mode, then initiates the actual ICE process
*
* @param remoteCandidatesMessage
*/
public synchronized void onIceMessageReceived(CandidatesMessage remoteCandidatesMessage) {
Expand Down Expand Up @@ -418,6 +426,7 @@ private synchronized void reinitIce() {

/**
* Data received from FA, prepends prefix and sends it via ICE to the other peer
*
* @param faData
* @param length
*/
Expand All @@ -430,6 +439,7 @@ void onFaDataReceived(byte[] faData, int length) {

/**
* Send date via ice to the other peer
*
* @param data
* @param offset
* @param length
Expand Down
Loading