From 74ab50439a834378746c9e950effd6a7d6e25246 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Tue, 20 Aug 2024 17:50:31 +0100 Subject: [PATCH] Improve connection initialisation stability --- .../core/webrtc/CustomDatagramTransport.java | 2 +- .../core/webrtc/RtcWebsocketClient.java | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/CustomDatagramTransport.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/CustomDatagramTransport.java index 558117c..1d1360c 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/CustomDatagramTransport.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/CustomDatagramTransport.java @@ -45,7 +45,7 @@ public int receive(byte[] buf, int off, int len, int waitMillis) throws IOExcept public void send(byte[] buf, int off, int len) throws IOException { System.out.println("send! " + new String(buf, off, len)); // System.out.println("send! " + bytesToHex(buf)); - socket.send(new DatagramPacket(buf, off, len, component.getDefaultCandidate().getTransportAddress())); + socket.send(new DatagramPacket(buf, off, len)); } @Override diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java index a31e3cb..a2bbbeb 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java @@ -57,6 +57,7 @@ import org.ice4j.ice.Component; import org.ice4j.ice.IceMediaStream; import org.ice4j.ice.IceProcessingState; +import org.ice4j.ice.LocalCandidate; import org.ice4j.ice.RemoteCandidate; import org.ice4j.ice.harvest.StunCandidateHarvester; import org.ice4j.ice.harvest.TurnCandidateHarvester; @@ -179,7 +180,6 @@ private void handleConnectRequest(BigInteger from, String sessionId, String mess } } } - agent.startConnectivityEstablishment(); component = agent.createComponent(stream, 5000, 5000, 6000); @@ -288,16 +288,18 @@ public void onRawStream(SCTPStream sctpStream) { System.out.println(json); send(json); - component.getLocalCandidates().forEach(candidate -> { + int i = 0; + for (LocalCandidate candidate : component.getLocalCandidates()) { var jsonAdd = Constants.GSON.toJson(new WsToMessage( - 1, from, "CANDIDATEADD " + sessionId + " " + candidate.toString() + " generation 0 ufrag " + agent.getLocalUfrag() + " network-id " + candidate.getFoundation() + 1, from, "CANDIDATEADD " + sessionId + " " + candidate.toString() + " generation 0 ufrag " + agent.getLocalUfrag() + " network-id " + i + " network-cost 0" )); + i++; System.out.println(jsonAdd); send(jsonAdd); - }); + } agent.addStateChangeListener(evt -> { - System.out.println("state change! " + evt + " " + evt.getPropertyName()); + System.out.println("state change! " + evt); if ("IceProcessingState".equals(evt.getPropertyName()) && IceProcessingState.COMPLETED.equals(evt.getNewValue())) { transport.init(component); try { @@ -340,14 +342,18 @@ private String fingerprintFor(byte[] input) { return fp.toString(); } + int candidateCount = 0; private void handleCandidateAdd(String sessionId, String message) throws UnknownHostException { // agent.candidate // activeSessions.get(sessionId).addCandidate(message); component.addUpdateRemoteCandidates(parseCandidate(message, component.getParentStream())); - component.updateRemoteCandidates(); - } - + candidateCount++; + if (candidateCount == 4) { + component.updateRemoteCandidates(); + agent.startConnectivityEstablishment(); + } + } public static RemoteCandidate parseCandidate(String value, IceMediaStream stream) { StringTokenizer tokenizer = new StringTokenizer(value); @@ -398,7 +404,6 @@ private void initialize(JsonObject message) { var turnAuthServers = message.getAsJsonArray("TurnAuthServers"); agent = new Agent(); - agent.setTrickling(true); for (JsonElement authServerElement : turnAuthServers) { var authServer = authServerElement.getAsJsonObject();